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
13 use Term::ANSIColor qw(:constants);
16 my $D = dirname(abs_path($P));
20 use Getopt::Long qw(:config no_auto_abbrev);
50 my $configuration_file = ".checkpatch.conf";
51 my $max_line_length = 80;
52 my $ignore_perl_version = 0;
53 my $minimum_perl_version = 5.10.0;
54 my $min_conf_desc_length = 4;
55 my $spelling_file = "$D/spelling.txt";
57 my $codespellfile = "/usr/share/codespell/dictionary.txt";
58 my $conststructsfile = "$D/const_structs.checkpatch";
59 my $typedefsfile = "";
61 my $allow_c99_comments = 1;
67 Usage: $P [OPTION]... [FILE]...
72 --no-tree run without a kernel tree
73 --no-signoff do not check for 'Signed-off-by' line
74 --patch treat FILE as patchfile (default)
75 --emacs emacs compile window format
76 --terse one line per report
77 --showfile emit diffed file position, not input file position
78 -g, --git treat FILE as a single commit or git revision range
79 single git commit with:
83 multiple git commits with:
87 git merges are ignored
88 -f, --file treat FILE as regular source file
89 --subjective, --strict enable more subjective tests
90 --list-types list the possible message types
91 --types TYPE(,TYPE2...) show only these comma separated message types
92 --ignore TYPE(,TYPE2...) ignore various comma separated message types
93 --show-types show the specific message type in the output
94 --max-line-length=n set the maximum line length, if exceeded, warn
95 --min-conf-desc-length=n set the min description length, if shorter, warn
96 --root=PATH PATH to the kernel tree root
97 --no-summary suppress the per-file summary
98 --mailback only produce a report in case of warnings/errors
99 --summary-file include the filename in summary
100 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
101 'values', 'possible', 'type', and 'attr' (default
103 --test-only=WORD report only warnings/errors containing WORD
105 --fix EXPERIMENTAL - may create horrible results
106 If correctable single-line errors exist, create
107 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
108 with potential errors corrected to the preferred
110 --fix-inplace EXPERIMENTAL - may create horrible results
111 Is the same as --fix, but overwrites the input
112 file. It's your fault if there's no backup or git
113 --ignore-perl-version override checking of perl version. expect
115 --codespell Use the codespell dictionary for spelling/typos
116 (default:/usr/share/codespell/dictionary.txt)
117 --codespellfile Use this codespell dictionary
118 --typedefsfile Read additional types from this file
119 --color Use colors when output is STDOUT (default: on)
120 -h, --help, --version display this help and exit
122 When FILE is - read standard input.
130 return grep { !$seen{$_}++ } @_;
140 open(my $script, '<', abs_path($P)) or
141 die "$P: Can't read '$P' $!\n";
143 my $text = <$script>;
147 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
150 @types = sort(uniq(@types));
151 print("#\tMessage type\n\n");
152 foreach my $type (@types) {
153 print(++$count . "\t" . $type . "\n");
159 my $conf = which_conf($configuration_file);
162 open(my $conffile, '<', "$conf")
163 or warn "$P: Can't find a readable $configuration_file file $!\n";
165 while (<$conffile>) {
168 $line =~ s/\s*\n?$//g;
172 next if ($line =~ m/^\s*#/);
173 next if ($line =~ m/^\s*$/);
175 my @words = split(" ", $line);
176 foreach my $word (@words) {
177 last if ($word =~ m/^#/);
178 push (@conf_args, $word);
182 unshift(@ARGV, @conf_args) if @conf_args;
186 'q|quiet+' => \$quiet,
188 'signoff!' => \$chk_signoff,
189 'patch!' => \$chk_patch,
192 'showfile!' => \$showfile,
195 'subjective!' => \$check,
196 'strict!' => \$check,
197 'ignore=s' => \@ignore,
199 'show-types!' => \$show_types,
200 'list-types!' => \$list_types,
201 'max-line-length=i' => \$max_line_length,
202 'min-conf-desc-length=i' => \$min_conf_desc_length,
204 'summary!' => \$summary,
205 'mailback!' => \$mailback,
206 'summary-file!' => \$summary_file,
208 'fix-inplace!' => \$fix_inplace,
209 'ignore-perl-version!' => \$ignore_perl_version,
210 'debug=s' => \%debug,
211 'test-only=s' => \$tst_only,
212 'codespell!' => \$codespell,
213 'codespellfile=s' => \$codespellfile,
214 'typedefsfile=s' => \$typedefsfile,
222 list_types(0) if ($list_types);
224 $fix = 1 if ($fix_inplace);
225 $check_orig = $check;
229 if ($^V && $^V lt $minimum_perl_version) {
230 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
231 if (!$ignore_perl_version) {
236 #if no filenames are given, push '-' to read patch from stdin
241 sub hash_save_array_words {
242 my ($hashRef, $arrayRef) = @_;
244 my @array = split(/,/, join(',', @$arrayRef));
245 foreach my $word (@array) {
246 $word =~ s/\s*\n?$//g;
249 $word =~ tr/[a-z]/[A-Z]/;
251 next if ($word =~ m/^\s*#/);
252 next if ($word =~ m/^\s*$/);
258 sub hash_show_words {
259 my ($hashRef, $prefix) = @_;
261 if (keys %$hashRef) {
262 print "\nNOTE: $prefix message types:";
263 foreach my $word (sort keys %$hashRef) {
270 hash_save_array_words(\%ignore_type, \@ignore);
271 hash_save_array_words(\%use_type, \@use);
274 my $dbg_possible = 0;
277 for my $key (keys %debug) {
279 eval "\${dbg_$key} = '$debug{$key}';";
283 my $rpt_cleaners = 0;
292 if (!top_of_kernel_tree($root)) {
293 die "$P: $root: --root does not point at a valid tree\n";
296 if (top_of_kernel_tree('.')) {
298 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
299 top_of_kernel_tree($1)) {
304 if (!defined $root) {
305 print "Must be run from the top-level dir. of a kernel tree\n";
310 my $emitted_corrupt = 0;
313 [A-Za-z_][A-Za-z\d_]*
314 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
316 our $Storage = qr{extern|static|asmlinkage};
329 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
330 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
331 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
332 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
333 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
335 # Notes to $Attribute:
336 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
357 ____cacheline_aligned|
358 ____cacheline_aligned_in_smp|
359 ____cacheline_internodealigned_in_smp|
363 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
364 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
365 our $Lval = qr{$Ident(?:$Member)*};
367 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
368 our $Binary = qr{(?i)0b[01]+$Int_type?};
369 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
370 our $Int = qr{[0-9]+$Int_type?};
371 our $Octal = qr{0[0-7]+$Int_type?};
372 our $String = qr{"[X\t]*"};
373 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
374 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
375 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
376 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
377 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
378 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
379 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
380 our $Arithmetic = qr{\+|-|\*|\/|%};
384 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
387 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
391 our $NonptrTypeMisordered;
392 our $NonptrTypeWithAttr;
396 our $DeclareMisordered;
398 our $NON_ASCII_UTF8 = qr{
399 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
400 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
401 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
402 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
403 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
404 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
405 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
409 [\x09\x0A\x0D\x20-\x7E] # ASCII
413 our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
414 our $typeOtherOSTypedefs = qr{(?x:
415 u_(?:char|short|int|long) | # bsd
416 u(?:nchar|short|int|long) # sysv
418 our $typeKernelTypedefs = qr{(?x:
419 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
422 our $typeTypedefs = qr{(?x:
424 $typeOtherOSTypedefs\b|
425 $typeKernelTypedefs\b
428 our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
430 our $logFunctions = qr{(?x:
431 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
432 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
433 WARN(?:_RATELIMIT|_ONCE|)|
436 seq_vprintf|seq_printf|seq_puts
439 our $signature_tags = qr{(?xi:
450 our @typeListMisordered = (
451 qr{char\s+(?:un)?signed},
452 qr{int\s+(?:(?:un)?signed\s+)?short\s},
453 qr{int\s+short(?:\s+(?:un)?signed)},
454 qr{short\s+int(?:\s+(?:un)?signed)},
455 qr{(?:un)?signed\s+int\s+short},
456 qr{short\s+(?:un)?signed},
457 qr{long\s+int\s+(?:un)?signed},
458 qr{int\s+long\s+(?:un)?signed},
459 qr{long\s+(?:un)?signed\s+int},
460 qr{int\s+(?:un)?signed\s+long},
461 qr{int\s+(?:un)?signed},
462 qr{int\s+long\s+long\s+(?:un)?signed},
463 qr{long\s+long\s+int\s+(?:un)?signed},
464 qr{long\s+long\s+(?:un)?signed\s+int},
465 qr{long\s+long\s+(?:un)?signed},
466 qr{long\s+(?:un)?signed},
471 qr{(?:(?:un)?signed\s+)?char},
472 qr{(?:(?:un)?signed\s+)?short\s+int},
473 qr{(?:(?:un)?signed\s+)?short},
474 qr{(?:(?:un)?signed\s+)?int},
475 qr{(?:(?:un)?signed\s+)?long\s+int},
476 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
477 qr{(?:(?:un)?signed\s+)?long\s+long},
478 qr{(?:(?:un)?signed\s+)?long},
487 qr{${Ident}_handler},
488 qr{${Ident}_handler_fn},
492 our $C90_int_types = qr{(?x:
493 long\s+long\s+int\s+(?:un)?signed|
494 long\s+long\s+(?:un)?signed\s+int|
495 long\s+long\s+(?:un)?signed|
496 (?:(?:un)?signed\s+)?long\s+long\s+int|
497 (?:(?:un)?signed\s+)?long\s+long|
498 int\s+long\s+long\s+(?:un)?signed|
499 int\s+(?:(?:un)?signed\s+)?long\s+long|
501 long\s+int\s+(?:un)?signed|
502 long\s+(?:un)?signed\s+int|
503 long\s+(?:un)?signed|
504 (?:(?:un)?signed\s+)?long\s+int|
505 (?:(?:un)?signed\s+)?long|
506 int\s+long\s+(?:un)?signed|
507 int\s+(?:(?:un)?signed\s+)?long|
510 (?:(?:un)?signed\s+)?int
513 our @typeListFile = ();
514 our @typeListWithAttr = (
516 qr{struct\s+$InitAttribute\s+$Ident},
517 qr{union\s+$InitAttribute\s+$Ident},
520 our @modifierList = (
523 our @modifierListFile = ();
525 our @mode_permission_funcs = (
527 ["module_param_(?:array|named|string)", 4],
528 ["module_param_array_named", 5],
529 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
530 ["proc_create(?:_data|)", 2],
531 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
532 ["IIO_DEV_ATTR_[A-Z_]+", 1],
533 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
534 ["SENSOR_TEMPLATE(?:_2|)", 3],
538 #Create a search pattern for all these functions to speed up a loop below
539 our $mode_perms_search = "";
540 foreach my $entry (@mode_permission_funcs) {
541 $mode_perms_search .= '|' if ($mode_perms_search ne "");
542 $mode_perms_search .= $entry->[0];
545 our $mode_perms_world_writable = qr{
553 our %mode_permission_string_types = (
572 #Create a search pattern for all these strings to speed up a loop below
573 our $mode_perms_string_search = "";
574 foreach my $entry (keys %mode_permission_string_types) {
575 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
576 $mode_perms_string_search .= $entry;
579 our $allowed_asm_includes = qr{(?x:
585 # memory.h: ARM has a custom one
587 # Load common spelling mistakes and build regular expression list.
591 if (open(my $spelling, '<', $spelling_file)) {
592 while (<$spelling>) {
595 $line =~ s/\s*\n?$//g;
598 next if ($line =~ m/^\s*#/);
599 next if ($line =~ m/^\s*$/);
601 my ($suspect, $fix) = split(/\|\|/, $line);
603 $spelling_fix{$suspect} = $fix;
607 warn "No typos will be found - file '$spelling_file': $!\n";
611 if (open(my $spelling, '<', $codespellfile)) {
612 while (<$spelling>) {
615 $line =~ s/\s*\n?$//g;
618 next if ($line =~ m/^\s*#/);
619 next if ($line =~ m/^\s*$/);
620 next if ($line =~ m/, disabled/i);
624 my ($suspect, $fix) = split(/->/, $line);
626 $spelling_fix{$suspect} = $fix;
630 warn "No codespell typos will be found - file '$codespellfile': $!\n";
634 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
637 my ($wordsRef, $file) = @_;
639 if (open(my $words, '<', $file)) {
643 $line =~ s/\s*\n?$//g;
646 next if ($line =~ m/^\s*#/);
647 next if ($line =~ m/^\s*$/);
649 print("$file: '$line' invalid - ignored\n");
653 $$wordsRef .= '|' if ($$wordsRef ne "");
663 my $const_structs = "";
664 read_words(\$const_structs, $conststructsfile)
665 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
667 my $typeOtherTypedefs = "";
668 if (length($typedefsfile)) {
669 read_words(\$typeOtherTypedefs, $typedefsfile)
670 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
672 $typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
675 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
676 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
677 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
678 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
679 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
685 (?:$Modifier\s+|const\s+)*
687 (?:typeof|__typeof__)\s*\([^\)]*\)|
691 (?:\s+$Modifier|\s+const)*
693 $NonptrTypeMisordered = qr{
694 (?:$Modifier\s+|const\s+)*
698 (?:\s+$Modifier|\s+const)*
700 $NonptrTypeWithAttr = qr{
701 (?:$Modifier\s+|const\s+)*
703 (?:typeof|__typeof__)\s*\([^\)]*\)|
707 (?:\s+$Modifier|\s+const)*
711 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
712 (?:\s+$Inline|\s+$Modifier)*
714 $TypeMisordered = qr{
715 $NonptrTypeMisordered
716 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
717 (?:\s+$Inline|\s+$Modifier)*
719 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
720 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
724 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
726 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
727 # requires at least perl version v5.10.0
728 # Any use must be runtime checked with $^V
730 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
731 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
732 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
734 our $declaration_macros = qr{(?x:
735 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
736 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
737 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
742 return "" if (!defined($string));
744 while ($string =~ /^\s*\(.*\)\s*$/) {
745 $string =~ s@^\s*\(\s*@@;
746 $string =~ s@\s*\)\s*$@@;
749 $string =~ s@\s+@ @g;
754 sub seed_camelcase_file {
757 return if (!(-f $file));
761 open(my $include_file, '<', "$file")
762 or warn "$P: Can't read '$file' $!\n";
763 my $text = <$include_file>;
764 close($include_file);
766 my @lines = split('\n', $text);
768 foreach my $line (@lines) {
769 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
770 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
772 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
774 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
780 sub is_maintained_obsolete {
783 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
785 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
787 return $status =~ /obsolete/i;
790 my $camelcase_seeded = 0;
791 sub seed_camelcase_includes {
792 return if ($camelcase_seeded);
795 my $camelcase_cache = "";
796 my @include_files = ();
798 $camelcase_seeded = 1;
801 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
802 chomp $git_last_include_commit;
803 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
805 my $last_mod_date = 0;
806 $files = `find $root/include -name "*.h"`;
807 @include_files = split('\n', $files);
808 foreach my $file (@include_files) {
809 my $date = POSIX::strftime("%Y%m%d%H%M",
810 localtime((stat $file)[9]));
811 $last_mod_date = $date if ($last_mod_date < $date);
813 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
816 if ($camelcase_cache ne "" && -f $camelcase_cache) {
817 open(my $camelcase_file, '<', "$camelcase_cache")
818 or warn "$P: Can't read '$camelcase_cache' $!\n";
819 while (<$camelcase_file>) {
823 close($camelcase_file);
829 $files = `git ls-files "include/*.h"`;
830 @include_files = split('\n', $files);
833 foreach my $file (@include_files) {
834 seed_camelcase_file($file);
837 if ($camelcase_cache ne "") {
838 unlink glob ".checkpatch-camelcase.*";
839 open(my $camelcase_file, '>', "$camelcase_cache")
840 or warn "$P: Can't write '$camelcase_cache' $!\n";
841 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
842 print $camelcase_file ("$_\n");
844 close($camelcase_file);
848 sub git_commit_info {
849 my ($commit, $id, $desc) = @_;
851 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
853 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
854 $output =~ s/^\s*//gm;
855 my @lines = split("\n", $output);
857 return ($id, $desc) if ($#lines < 0);
859 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
860 # Maybe one day convert this block of bash into something that returns
861 # all matching commit ids, but it's very slow...
863 # echo "checking commits $1..."
864 # git rev-list --remotes | grep -i "^$1" |
865 # while read line ; do
866 # git log --format='%H %s' -1 $line |
867 # echo "commit $(cut -c 1-12,41-)"
869 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
872 $id = substr($lines[0], 0, 12);
873 $desc = substr($lines[0], 41);
879 $chk_signoff = 0 if ($file);
884 my @fixed_inserted = ();
885 my @fixed_deleted = ();
888 # If input is git commits, extract all commits from the commit expressions.
889 # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
890 die "$P: No git repository found\n" if ($git && !-e ".git");
894 foreach my $commit_expr (@ARGV) {
896 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
897 $git_range = "-$2 $1";
898 } elsif ($commit_expr =~ m/\.\./) {
899 $git_range = "$commit_expr";
901 $git_range = "-1 $commit_expr";
903 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
904 foreach my $line (split(/\n/, $lines)) {
905 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
906 next if (!defined($1) || !defined($2));
909 unshift(@commits, $sha1);
910 $git_commits{$sha1} = $subject;
913 die "$P: no git commits after extraction!\n" if (@commits == 0);
918 for my $filename (@ARGV) {
921 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
922 die "$P: $filename: git format-patch failed - $!\n";
924 open($FILE, '-|', "diff -u /dev/null $filename") ||
925 die "$P: $filename: diff failed - $!\n";
926 } elsif ($filename eq '-') {
927 open($FILE, '<&STDIN');
929 open($FILE, '<', "$filename") ||
930 die "$P: $filename: open failed - $!\n";
932 if ($filename eq '-') {
933 $vname = 'Your patch';
935 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
945 if ($#ARGV > 0 && $quiet == 0) {
946 print '-' x length($vname) . "\n";
948 print '-' x length($vname) . "\n";
951 if (!process($filename)) {
957 @fixed_inserted = ();
960 @modifierListFile = ();
966 hash_show_words(\%use_type, "Used");
967 hash_show_words(\%ignore_type, "Ignored");
972 NOTE: perl $^V is not modern enough to detect all possible issues.
973 An upgrade to at least perl v5.10.0 is suggested.
979 NOTE: If any of the errors are false positives, please report
980 them to the maintainer, see CHECKPATCH in MAINTAINERS.
987 sub top_of_kernel_tree {
991 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
992 "README", "Documentation", "arch", "include", "drivers",
993 "fs", "init", "ipc", "kernel", "lib", "scripts",
996 foreach my $check (@tree_check) {
997 if (! -e $root . '/' . $check) {
1005 my ($formatted_email) = @_;
1011 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1014 $comment = $3 if defined $3;
1015 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1017 $comment = $2 if defined $2;
1018 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1020 $comment = $2 if defined $2;
1021 $formatted_email =~ s/$address.*$//;
1022 $name = $formatted_email;
1023 $name = trim($name);
1024 $name =~ s/^\"|\"$//g;
1025 # If there's a name left after stripping spaces and
1026 # leading quotes, and the address doesn't have both
1027 # leading and trailing angle brackets, the address
1029 # "joe smith joe@smith.com" bad
1030 # "joe smith <joe@smith.com" bad
1031 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1038 $name = trim($name);
1039 $name =~ s/^\"|\"$//g;
1040 $address = trim($address);
1041 $address =~ s/^\<|\>$//g;
1043 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1044 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1045 $name = "\"$name\"";
1048 return ($name, $address, $comment);
1052 my ($name, $address) = @_;
1054 my $formatted_email;
1056 $name = trim($name);
1057 $name =~ s/^\"|\"$//g;
1058 $address = trim($address);
1060 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1061 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1062 $name = "\"$name\"";
1065 if ("$name" eq "") {
1066 $formatted_email = "$address";
1068 $formatted_email = "$name <$address>";
1071 return $formatted_email;
1077 foreach my $path (split(/:/, $ENV{PATH})) {
1078 if (-e "$path/$bin") {
1079 return "$path/$bin";
1089 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1090 if (-e "$path/$conf") {
1091 return "$path/$conf";
1103 for my $c (split(//, $str)) {
1107 for (; ($n % 8) != 0; $n++) {
1119 (my $res = shift) =~ tr/\t/ /c;
1126 # Drop the diff line leader and expand tabs
1128 $line = expand_tabs($line);
1130 # Pick the indent from the front of the line.
1131 my ($white) = ($line =~ /^(\s*)/);
1133 return (length($line), length($white));
1136 my $sanitise_quote = '';
1138 sub sanitise_line_reset {
1139 my ($in_comment) = @_;
1142 $sanitise_quote = '*/';
1144 $sanitise_quote = '';
1157 # Always copy over the diff marker.
1158 $res = substr($line, 0, 1);
1160 for ($off = 1; $off < length($line); $off++) {
1161 $c = substr($line, $off, 1);
1163 # Comments we are wacking completly including the begin
1164 # and end, all to $;.
1165 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1166 $sanitise_quote = '*/';
1168 substr($res, $off, 2, "$;$;");
1172 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1173 $sanitise_quote = '';
1174 substr($res, $off, 2, "$;$;");
1178 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1179 $sanitise_quote = '//';
1181 substr($res, $off, 2, $sanitise_quote);
1186 # A \ in a string means ignore the next character.
1187 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1189 substr($res, $off, 2, 'XX');
1194 if ($c eq "'" || $c eq '"') {
1195 if ($sanitise_quote eq '') {
1196 $sanitise_quote = $c;
1198 substr($res, $off, 1, $c);
1200 } elsif ($sanitise_quote eq $c) {
1201 $sanitise_quote = '';
1205 #print "c<$c> SQ<$sanitise_quote>\n";
1206 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1207 substr($res, $off, 1, $;);
1208 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1209 substr($res, $off, 1, $;);
1210 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1211 substr($res, $off, 1, 'X');
1213 substr($res, $off, 1, $c);
1217 if ($sanitise_quote eq '//') {
1218 $sanitise_quote = '';
1221 # The pathname on a #include may be surrounded by '<' and '>'.
1222 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1223 my $clean = 'X' x length($1);
1224 $res =~ s@\<.*\>@<$clean>@;
1226 # The whole of a #error is a string.
1227 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1228 my $clean = 'X' x length($1);
1229 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1232 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1234 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1240 sub get_quoted_string {
1241 my ($line, $rawline) = @_;
1243 return "" if ($line !~ m/($String)/g);
1244 return substr($rawline, $-[0], $+[0] - $-[0]);
1247 sub ctx_statement_block {
1248 my ($linenr, $remain, $off) = @_;
1249 my $line = $linenr - 1;
1252 my $coff = $off - 1;
1266 @stack = (['', 0]) if ($#stack == -1);
1268 #warn "CSB: blk<$blk> remain<$remain>\n";
1269 # If we are about to drop off the end, pull in more
1272 for (; $remain > 0; $line++) {
1273 last if (!defined $lines[$line]);
1274 next if ($lines[$line] =~ /^-/);
1277 $blk .= $lines[$line] . "\n";
1278 $len = length($blk);
1282 # Bail if there is no further context.
1283 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1287 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1293 $c = substr($blk, $off, 1);
1294 $remainder = substr($blk, $off);
1296 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1298 # Handle nested #if/#else.
1299 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1300 push(@stack, [ $type, $level ]);
1301 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1302 ($type, $level) = @{$stack[$#stack - 1]};
1303 } elsif ($remainder =~ /^#\s*endif\b/) {
1304 ($type, $level) = @{pop(@stack)};
1307 # Statement ends at the ';' or a close '}' at the
1309 if ($level == 0 && $c eq ';') {
1313 # An else is really a conditional as long as its not else if
1314 if ($level == 0 && $coff_set == 0 &&
1315 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1316 $remainder =~ /^(else)(?:\s|{)/ &&
1317 $remainder !~ /^else\s+if\b/) {
1318 $coff = $off + length($1) - 1;
1320 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1321 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1324 if (($type eq '' || $type eq '(') && $c eq '(') {
1328 if ($type eq '(' && $c eq ')') {
1330 $type = ($level != 0)? '(' : '';
1332 if ($level == 0 && $coff < $soff) {
1335 #warn "CSB: mark coff<$coff>\n";
1338 if (($type eq '' || $type eq '{') && $c eq '{') {
1342 if ($type eq '{' && $c eq '}') {
1344 $type = ($level != 0)? '{' : '';
1347 if (substr($blk, $off + 1, 1) eq ';') {
1353 # Preprocessor commands end at the newline unless escaped.
1354 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1362 # We are truly at the end, so shuffle to the next line.
1369 my $statement = substr($blk, $soff, $off - $soff + 1);
1370 my $condition = substr($blk, $soff, $coff - $soff + 1);
1372 #warn "STATEMENT<$statement>\n";
1373 #warn "CONDITION<$condition>\n";
1375 #print "coff<$coff> soff<$off> loff<$loff>\n";
1377 return ($statement, $condition,
1378 $line, $remain + 1, $off - $loff + 1, $level);
1381 sub statement_lines {
1384 # Strip the diff line prefixes and rip blank lines at start and end.
1385 $stmt =~ s/(^|\n)./$1/g;
1389 my @stmt_lines = ($stmt =~ /\n/g);
1391 return $#stmt_lines + 2;
1394 sub statement_rawlines {
1397 my @stmt_lines = ($stmt =~ /\n/g);
1399 return $#stmt_lines + 2;
1402 sub statement_block_size {
1405 $stmt =~ s/(^|\n)./$1/g;
1411 my @stmt_lines = ($stmt =~ /\n/g);
1412 my @stmt_statements = ($stmt =~ /;/g);
1414 my $stmt_lines = $#stmt_lines + 2;
1415 my $stmt_statements = $#stmt_statements + 1;
1417 if ($stmt_lines > $stmt_statements) {
1420 return $stmt_statements;
1424 sub ctx_statement_full {
1425 my ($linenr, $remain, $off) = @_;
1426 my ($statement, $condition, $level);
1430 # Grab the first conditional/block pair.
1431 ($statement, $condition, $linenr, $remain, $off, $level) =
1432 ctx_statement_block($linenr, $remain, $off);
1433 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1434 push(@chunks, [ $condition, $statement ]);
1435 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1436 return ($level, $linenr, @chunks);
1439 # Pull in the following conditional/block pairs and see if they
1440 # could continue the statement.
1442 ($statement, $condition, $linenr, $remain, $off, $level) =
1443 ctx_statement_block($linenr, $remain, $off);
1444 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1445 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1447 push(@chunks, [ $condition, $statement ]);
1450 return ($level, $linenr, @chunks);
1454 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1456 my $start = $linenr - 1;
1463 my @stack = ($level);
1464 for ($line = $start; $remain > 0; $line++) {
1465 next if ($rawlines[$line] =~ /^-/);
1468 $blk .= $rawlines[$line];
1470 # Handle nested #if/#else.
1471 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1472 push(@stack, $level);
1473 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1474 $level = $stack[$#stack - 1];
1475 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1476 $level = pop(@stack);
1479 foreach my $c (split(//, $lines[$line])) {
1480 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1486 if ($c eq $close && $level > 0) {
1488 last if ($level == 0);
1489 } elsif ($c eq $open) {
1494 if (!$outer || $level <= 1) {
1495 push(@res, $rawlines[$line]);
1498 last if ($level == 0);
1501 return ($level, @res);
1503 sub ctx_block_outer {
1504 my ($linenr, $remain) = @_;
1506 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1510 my ($linenr, $remain) = @_;
1512 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1516 my ($linenr, $remain, $off) = @_;
1518 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1521 sub ctx_block_level {
1522 my ($linenr, $remain) = @_;
1524 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1526 sub ctx_statement_level {
1527 my ($linenr, $remain, $off) = @_;
1529 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1532 sub ctx_locate_comment {
1533 my ($first_line, $end_line) = @_;
1535 # Catch a comment on the end of the line itself.
1536 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1537 return $current_comment if (defined $current_comment);
1539 # Look through the context and try and figure out if there is a
1542 $current_comment = '';
1543 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1544 my $line = $rawlines[$linenr - 1];
1546 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1549 if ($line =~ m@/\*@) {
1552 if (!$in_comment && $current_comment ne '') {
1553 $current_comment = '';
1555 $current_comment .= $line . "\n" if ($in_comment);
1556 if ($line =~ m@\*/@) {
1561 chomp($current_comment);
1562 return($current_comment);
1564 sub ctx_has_comment {
1565 my ($first_line, $end_line) = @_;
1566 my $cmt = ctx_locate_comment($first_line, $end_line);
1568 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1569 ##print "CMMT: $cmt\n";
1571 return ($cmt ne '');
1575 my ($linenr, $cnt) = @_;
1577 my $offset = $linenr - 1;
1582 $line = $rawlines[$offset++];
1583 next if (defined($line) && $line =~ /^-/);
1595 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1598 $coded = sprintf("^%c", unpack('C', $2) + 64);
1607 my $av_preprocessor = 0;
1612 sub annotate_reset {
1613 $av_preprocessor = 0;
1615 @av_paren_type = ('E');
1616 $av_pend_colon = 'O';
1619 sub annotate_values {
1620 my ($stream, $type) = @_;
1623 my $var = '_' x length($stream);
1626 print "$stream\n" if ($dbg_values > 1);
1628 while (length($cur)) {
1629 @av_paren_type = ('E') if ($#av_paren_type < 0);
1630 print " <" . join('', @av_paren_type) .
1631 "> <$type> <$av_pending>" if ($dbg_values > 1);
1632 if ($cur =~ /^(\s+)/o) {
1633 print "WS($1)\n" if ($dbg_values > 1);
1634 if ($1 =~ /\n/ && $av_preprocessor) {
1635 $type = pop(@av_paren_type);
1636 $av_preprocessor = 0;
1639 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1640 print "CAST($1)\n" if ($dbg_values > 1);
1641 push(@av_paren_type, $type);
1644 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1645 print "DECLARE($1)\n" if ($dbg_values > 1);
1648 } elsif ($cur =~ /^($Modifier)\s*/) {
1649 print "MODIFIER($1)\n" if ($dbg_values > 1);
1652 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1653 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1654 $av_preprocessor = 1;
1655 push(@av_paren_type, $type);
1661 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1662 print "UNDEF($1)\n" if ($dbg_values > 1);
1663 $av_preprocessor = 1;
1664 push(@av_paren_type, $type);
1666 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1667 print "PRE_START($1)\n" if ($dbg_values > 1);
1668 $av_preprocessor = 1;
1670 push(@av_paren_type, $type);
1671 push(@av_paren_type, $type);
1674 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1675 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1676 $av_preprocessor = 1;
1678 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1682 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1683 print "PRE_END($1)\n" if ($dbg_values > 1);
1685 $av_preprocessor = 1;
1687 # Assume all arms of the conditional end as this
1688 # one does, and continue as if the #endif was not here.
1689 pop(@av_paren_type);
1690 push(@av_paren_type, $type);
1693 } elsif ($cur =~ /^(\\\n)/o) {
1694 print "PRECONT($1)\n" if ($dbg_values > 1);
1696 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1697 print "ATTR($1)\n" if ($dbg_values > 1);
1698 $av_pending = $type;
1701 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1702 print "SIZEOF($1)\n" if ($dbg_values > 1);
1708 } elsif ($cur =~ /^(if|while|for)\b/o) {
1709 print "COND($1)\n" if ($dbg_values > 1);
1713 } elsif ($cur =~/^(case)/o) {
1714 print "CASE($1)\n" if ($dbg_values > 1);
1715 $av_pend_colon = 'C';
1718 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1719 print "KEYWORD($1)\n" if ($dbg_values > 1);
1722 } elsif ($cur =~ /^(\()/o) {
1723 print "PAREN('$1')\n" if ($dbg_values > 1);
1724 push(@av_paren_type, $av_pending);
1728 } elsif ($cur =~ /^(\))/o) {
1729 my $new_type = pop(@av_paren_type);
1730 if ($new_type ne '_') {
1732 print "PAREN('$1') -> $type\n"
1733 if ($dbg_values > 1);
1735 print "PAREN('$1')\n" if ($dbg_values > 1);
1738 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1739 print "FUNC($1)\n" if ($dbg_values > 1);
1743 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1744 if (defined $2 && $type eq 'C' || $type eq 'T') {
1745 $av_pend_colon = 'B';
1746 } elsif ($type eq 'E') {
1747 $av_pend_colon = 'L';
1749 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1752 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1753 print "IDENT($1)\n" if ($dbg_values > 1);
1756 } elsif ($cur =~ /^($Assignment)/o) {
1757 print "ASSIGN($1)\n" if ($dbg_values > 1);
1760 } elsif ($cur =~/^(;|{|})/) {
1761 print "END($1)\n" if ($dbg_values > 1);
1763 $av_pend_colon = 'O';
1765 } elsif ($cur =~/^(,)/) {
1766 print "COMMA($1)\n" if ($dbg_values > 1);
1769 } elsif ($cur =~ /^(\?)/o) {
1770 print "QUESTION($1)\n" if ($dbg_values > 1);
1773 } elsif ($cur =~ /^(:)/o) {
1774 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1776 substr($var, length($res), 1, $av_pend_colon);
1777 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1782 $av_pend_colon = 'O';
1784 } elsif ($cur =~ /^(\[)/o) {
1785 print "CLOSE($1)\n" if ($dbg_values > 1);
1788 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1791 print "OPV($1)\n" if ($dbg_values > 1);
1798 substr($var, length($res), 1, $variant);
1801 } elsif ($cur =~ /^($Operators)/o) {
1802 print "OP($1)\n" if ($dbg_values > 1);
1803 if ($1 ne '++' && $1 ne '--') {
1807 } elsif ($cur =~ /(^.)/o) {
1808 print "C($1)\n" if ($dbg_values > 1);
1811 $cur = substr($cur, length($1));
1812 $res .= $type x length($1);
1816 return ($res, $var);
1820 my ($possible, $line) = @_;
1821 my $notPermitted = qr{(?:
1838 ^(?:typedef|struct|enum)\b
1840 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1841 if ($possible !~ $notPermitted) {
1842 # Check for modifiers.
1843 $possible =~ s/\s*$Storage\s*//g;
1844 $possible =~ s/\s*$Sparse\s*//g;
1845 if ($possible =~ /^\s*$/) {
1847 } elsif ($possible =~ /\s/) {
1848 $possible =~ s/\s*$Type\s*//g;
1849 for my $modifier (split(' ', $possible)) {
1850 if ($modifier !~ $notPermitted) {
1851 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1852 push(@modifierListFile, $modifier);
1857 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1858 push(@typeListFile, $possible);
1862 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1871 $type =~ tr/[a-z]/[A-Z]/;
1873 return defined $use_type{$type} if (scalar keys %use_type > 0);
1875 return !defined $ignore_type{$type};
1879 my ($level, $type, $msg) = @_;
1881 if (!show_type($type) ||
1882 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1886 if (-t STDOUT && $color) {
1887 if ($level eq 'ERROR') {
1889 } elsif ($level eq 'WARNING') {
1895 $output .= $prefix . $level . ':';
1897 $output .= BLUE if (-t STDOUT && $color);
1898 $output .= "$type:";
1900 $output .= RESET if (-t STDOUT && $color);
1901 $output .= ' ' . $msg . "\n";
1904 my @lines = split("\n", $output, -1);
1905 splice(@lines, 1, 1);
1906 $output = join("\n", @lines);
1908 $output = (split('\n', $output))[0] . "\n" if ($terse);
1910 push(our @report, $output);
1919 sub fixup_current_range {
1920 my ($lineRef, $offset, $length) = @_;
1922 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1925 my $no = $o + $offset;
1926 my $nl = $l + $length;
1927 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1931 sub fix_inserted_deleted_lines {
1932 my ($linesRef, $insertedRef, $deletedRef) = @_;
1934 my $range_last_linenr = 0;
1935 my $delta_offset = 0;
1940 my $next_insert = 0;
1941 my $next_delete = 0;
1945 my $inserted = @{$insertedRef}[$next_insert++];
1946 my $deleted = @{$deletedRef}[$next_delete++];
1948 foreach my $old_line (@{$linesRef}) {
1950 my $line = $old_line; #don't modify the array
1951 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
1953 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1954 $range_last_linenr = $new_linenr;
1955 fixup_current_range(\$line, $delta_offset, 0);
1958 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1959 $deleted = @{$deletedRef}[$next_delete++];
1961 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1964 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1965 push(@lines, ${$inserted}{'LINE'});
1966 $inserted = @{$insertedRef}[$next_insert++];
1968 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1972 push(@lines, $line);
1982 sub fix_insert_line {
1983 my ($linenr, $line) = @_;
1989 push(@fixed_inserted, $inserted);
1992 sub fix_delete_line {
1993 my ($linenr, $line) = @_;
2000 push(@fixed_deleted, $deleted);
2004 my ($type, $msg) = @_;
2006 if (report("ERROR", $type, $msg)) {
2014 my ($type, $msg) = @_;
2016 if (report("WARNING", $type, $msg)) {
2024 my ($type, $msg) = @_;
2026 if ($check && report("CHECK", $type, $msg)) {
2034 sub check_absolute_file {
2035 my ($absolute, $herecurr) = @_;
2036 my $file = $absolute;
2038 ##print "absolute<$absolute>\n";
2040 # See if any suffix of this path is a path within the tree.
2041 while ($file =~ s@^[^/]*/@@) {
2042 if (-f "$root/$file") {
2043 ##print "file<$file>\n";
2051 # It is, so see if the prefix is acceptable.
2052 my $prefix = $absolute;
2053 substr($prefix, -length($file)) = '';
2055 ##print "prefix<$prefix>\n";
2056 if ($prefix ne ".../") {
2057 WARN("USE_RELATIVE_PATH",
2058 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2065 $string =~ s/^\s+|\s+$//g;
2073 $string =~ s/^\s+//;
2081 $string =~ s/\s+$//;
2086 sub string_find_replace {
2087 my ($string, $find, $replace) = @_;
2089 $string =~ s/$find/$replace/g;
2097 my $source_indent = 8;
2098 my $max_spaces_before_tab = $source_indent - 1;
2099 my $spaces_to_tab = " " x $source_indent;
2101 #convert leading spaces to tabs
2102 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2103 #Remove spaces before a tab
2104 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2109 sub pos_last_openparen {
2114 my $opens = $line =~ tr/\(/\(/;
2115 my $closes = $line =~ tr/\)/\)/;
2117 my $last_openparen = 0;
2119 if (($opens == 0) || ($closes >= $opens)) {
2123 my $len = length($line);
2125 for ($pos = 0; $pos < $len; $pos++) {
2126 my $string = substr($line, $pos);
2127 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2128 $pos += length($1) - 1;
2129 } elsif (substr($line, $pos, 1) eq '(') {
2130 $last_openparen = $pos;
2131 } elsif (index($string, '(') == -1) {
2136 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2140 my $filename = shift;
2146 my $stashrawline="";
2156 my $in_header_lines = $file ? 0 : 1;
2157 my $in_commit_log = 0; #Scanning lines before patch
2158 my $has_commit_log = 0; #Encountered lines before patch
2159 my $commit_log_possible_stack_dump = 0;
2160 my $commit_log_long_line = 0;
2161 my $commit_log_has_diff = 0;
2162 my $reported_maintainer_file = 0;
2163 my $non_utf8_charset = 0;
2165 my $last_blank_line = 0;
2166 my $last_coalesced_string_linenr = -1;
2174 # Trace the real file/line as we go.
2179 my $context_function; #undef'd unless there's a known function
2181 my $comment_edge = 0;
2185 my $prev_values = 'E';
2188 my %suppress_ifbraces;
2189 my %suppress_whiletrailers;
2190 my %suppress_export;
2191 my $suppress_statement = 0;
2193 my %signatures = ();
2195 # Pre-scan the patch sanitizing the lines.
2196 # Pre-scan the patch looking for any __setup documentation.
2198 my @setup_docs = ();
2201 my $camelcase_file_seeded = 0;
2203 sanitise_line_reset();
2205 foreach my $rawline (@rawlines) {
2209 push(@fixed, $rawline) if ($fix);
2211 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2213 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2218 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2227 # Guestimate if this is a continuing comment. Run
2228 # the context looking for a comment "edge". If this
2229 # edge is a close comment then we must be in a comment
2233 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2234 next if (defined $rawlines[$ln - 1] &&
2235 $rawlines[$ln - 1] =~ /^-/);
2237 #print "RAW<$rawlines[$ln - 1]>\n";
2238 last if (!defined $rawlines[$ln - 1]);
2239 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2240 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2245 if (defined $edge && $edge eq '*/') {
2249 # Guestimate if this is a continuing comment. If this
2250 # is the start of a diff block and this line starts
2251 # ' *' then it is very likely a comment.
2252 if (!defined $edge &&
2253 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2258 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2259 sanitise_line_reset($in_comment);
2261 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2262 # Standardise the strings and chars within the input to
2263 # simplify matching -- only bother with positive lines.
2264 $line = sanitise_line($rawline);
2266 push(@lines, $line);
2269 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2274 #print "==>$rawline\n";
2275 #print "-->$line\n";
2277 if ($setup_docs && $line =~ /^\+/) {
2278 push(@setup_docs, $line);
2287 foreach my $line (@lines) {
2290 my $sline = $line; #copy of $line
2291 $sline =~ s/$;/ /g; #with comments as spaces
2293 my $rawline = $rawlines[$linenr - 1];
2295 #extract the line range in the file after the patch is applied
2296 if (!$in_commit_log &&
2297 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2300 $first_line = $linenr + 1;
2310 %suppress_ifbraces = ();
2311 %suppress_whiletrailers = ();
2312 %suppress_export = ();
2313 $suppress_statement = 0;
2314 if ($context =~ /\b(\w+)\s*\(/) {
2315 $context_function = $1;
2317 undef $context_function;
2321 # track the line number as we move through the hunk, note that
2322 # new versions of GNU diff omit the leading space on completely
2323 # blank context lines so we need to count that too.
2324 } elsif ($line =~ /^( |\+|$)/) {
2326 $realcnt-- if ($realcnt != 0);
2328 # Measure the line length and indent.
2329 ($length, $indent) = line_stats($rawline);
2331 # Track the previous line.
2332 ($prevline, $stashline) = ($stashline, $line);
2333 ($previndent, $stashindent) = ($stashindent, $indent);
2334 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2336 #warn "line<$line>\n";
2338 } elsif ($realcnt == 1) {
2342 my $hunk_line = ($realcnt != 0);
2344 $here = "#$linenr: " if (!$file);
2345 $here = "#$realline: " if ($file);
2348 # extract the filename as it passes
2349 if ($line =~ /^diff --git.*?(\S+)$/) {
2351 $realfile =~ s@^([^/]*)/@@ if (!$file);
2354 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2356 $realfile =~ s@^([^/]*)/@@ if (!$file);
2360 if (!$file && $tree && $p1_prefix ne '' &&
2361 -e "$root/$p1_prefix") {
2362 WARN("PATCH_PREFIX",
2363 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2366 if ($realfile =~ m@^include/asm/@) {
2367 ERROR("MODIFIED_INCLUDE_ASM",
2368 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2373 #make up the handle for any error we report on this line
2375 $prefix = "$realfile:$realline: "
2378 $prefix = "$filename:$realline: ";
2380 $prefix = "$filename:$linenr: ";
2385 if (is_maintained_obsolete($realfile)) {
2387 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2389 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2392 $check = $check_orig;
2397 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2399 my $hereline = "$here\n$rawline\n";
2400 my $herecurr = "$here\n$rawline\n";
2401 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2403 $cnt_lines++ if ($realcnt != 0);
2405 # Check if the commit log has what seems like a diff which can confuse patch
2406 if ($in_commit_log && !$commit_log_has_diff &&
2407 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2408 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2409 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2410 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2411 ERROR("DIFF_IN_COMMIT_MSG",
2412 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2413 $commit_log_has_diff = 1;
2416 # Check for incorrect file permissions
2417 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2418 my $permhere = $here . "FILE: $realfile\n";
2419 if ($realfile !~ m@scripts/@ &&
2420 $realfile !~ /\.(py|pl|awk|sh)$/) {
2421 ERROR("EXECUTE_PERMISSIONS",
2422 "do not set execute permissions for source files\n" . $permhere);
2426 # Check the patch for a signoff:
2427 if ($line =~ /^\s*signed-off-by:/i) {
2432 # Check if MAINTAINERS is being updated. If so, there's probably no need to
2433 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
2434 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2435 $reported_maintainer_file = 1;
2438 # Check signature styles
2439 if (!$in_header_lines &&
2440 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2441 my $space_before = $1;
2443 my $space_after = $3;
2445 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2447 if ($sign_off !~ /$signature_tags/) {
2448 WARN("BAD_SIGN_OFF",
2449 "Non-standard signature: $sign_off\n" . $herecurr);
2451 if (defined $space_before && $space_before ne "") {
2452 if (WARN("BAD_SIGN_OFF",
2453 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2455 $fixed[$fixlinenr] =
2456 "$ucfirst_sign_off $email";
2459 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2460 if (WARN("BAD_SIGN_OFF",
2461 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2463 $fixed[$fixlinenr] =
2464 "$ucfirst_sign_off $email";
2468 if (!defined $space_after || $space_after ne " ") {
2469 if (WARN("BAD_SIGN_OFF",
2470 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2472 $fixed[$fixlinenr] =
2473 "$ucfirst_sign_off $email";
2477 my ($email_name, $email_address, $comment) = parse_email($email);
2478 my $suggested_email = format_email(($email_name, $email_address));
2479 if ($suggested_email eq "") {
2480 ERROR("BAD_SIGN_OFF",
2481 "Unrecognized email address: '$email'\n" . $herecurr);
2483 my $dequoted = $suggested_email;
2484 $dequoted =~ s/^"//;
2485 $dequoted =~ s/" </ </;
2486 # Don't force email to have quotes
2487 # Allow just an angle bracketed address
2488 if ("$dequoted$comment" ne $email &&
2489 "<$email_address>$comment" ne $email &&
2490 "$suggested_email$comment" ne $email) {
2491 WARN("BAD_SIGN_OFF",
2492 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2496 # Check for duplicate signatures
2497 my $sig_nospace = $line;
2498 $sig_nospace =~ s/\s//g;
2499 $sig_nospace = lc($sig_nospace);
2500 if (defined $signatures{$sig_nospace}) {
2501 WARN("BAD_SIGN_OFF",
2502 "Duplicate signature\n" . $herecurr);
2504 $signatures{$sig_nospace} = 1;
2508 # Check email subject for common tools that don't need to be mentioned
2509 if ($in_header_lines &&
2510 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2511 WARN("EMAIL_SUBJECT",
2512 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2515 # Check for old stable address
2516 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2517 ERROR("STABLE_ADDRESS",
2518 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2521 # Check for unwanted Gerrit info
2522 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2523 ERROR("GERRIT_CHANGE_ID",
2524 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2527 # Check if the commit log is in a possible stack dump
2528 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2529 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2530 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2532 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2533 # stack dump address
2534 $commit_log_possible_stack_dump = 1;
2537 # Check for line lengths > 75 in commit log, warn once
2538 if ($in_commit_log && !$commit_log_long_line &&
2539 length($line) > 75 &&
2540 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2541 # file delta changes
2542 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2544 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2545 # A Fixes: or Link: line
2546 $commit_log_possible_stack_dump)) {
2547 WARN("COMMIT_LOG_LONG_LINE",
2548 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2549 $commit_log_long_line = 1;
2552 # Reset possible stack dump if a blank line is found
2553 if ($in_commit_log && $commit_log_possible_stack_dump &&
2555 $commit_log_possible_stack_dump = 0;
2558 # Check for git id commit length and improperly formed commit descriptions
2559 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2560 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2561 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2562 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2563 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2564 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2565 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2566 my $init_char = "c";
2567 my $orig_commit = "";
2574 my $id = '0123456789ab';
2575 my $orig_desc = "commit description";
2576 my $description = "";
2578 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2580 $orig_commit = lc($2);
2581 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2582 $orig_commit = lc($1);
2585 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2586 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2587 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2588 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2589 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2592 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2593 defined $rawlines[$linenr] &&
2594 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2597 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2598 defined $rawlines[$linenr] &&
2599 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2600 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2602 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2603 $orig_desc .= " " . $1;
2607 ($id, $description) = git_commit_info($orig_commit,
2611 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2612 ERROR("GIT_COMMIT_ID",
2613 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2617 # Check for added, moved or deleted files
2618 if (!$reported_maintainer_file && !$in_commit_log &&
2619 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2620 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2621 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2622 (defined($1) || defined($2))))) {
2624 $reported_maintainer_file = 1;
2625 WARN("FILE_PATH_CHANGES",
2626 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2629 # Check for wrappage within a valid hunk of the file
2630 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2631 ERROR("CORRUPTED_PATCH",
2632 "patch seems to be corrupt (line wrapped?)\n" .
2633 $herecurr) if (!$emitted_corrupt++);
2636 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2637 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2638 $rawline !~ m/^$UTF8*$/) {
2639 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2641 my $blank = copy_spacing($rawline);
2642 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2643 my $hereptr = "$hereline$ptr\n";
2646 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2649 # Check if it's the start of a commit log
2650 # (not a header line and we haven't seen the patch filename)
2651 if ($in_header_lines && $realfile =~ /^$/ &&
2652 !($rawline =~ /^\s+(?:\S|$)/ ||
2653 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
2654 $in_header_lines = 0;
2656 $has_commit_log = 1;
2659 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2660 # declined it, i.e defined some charset where it is missing.
2661 if ($in_header_lines &&
2662 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2664 $non_utf8_charset = 1;
2667 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2668 $rawline =~ /$NON_ASCII_UTF8/) {
2669 WARN("UTF8_BEFORE_PATCH",
2670 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2673 # Check for absolute kernel paths in commit message
2674 if ($tree && $in_commit_log) {
2675 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2678 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2679 check_absolute_file($1, $herecurr)) {
2682 check_absolute_file($file, $herecurr);
2687 # Check for various typo / spelling mistakes
2688 if (defined($misspellings) &&
2689 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2690 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2692 my $typo_fix = $spelling_fix{lc($typo)};
2693 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2694 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2695 my $msg_type = \&WARN;
2696 $msg_type = \&CHK if ($file);
2697 if (&{$msg_type}("TYPO_SPELLING",
2698 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2700 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2705 # ignore non-hunk lines and lines being removed
2706 next if (!$hunk_line || $line =~ /^-/);
2708 #trailing whitespace
2709 if ($line =~ /^\+.*\015/) {
2710 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2711 if (ERROR("DOS_LINE_ENDINGS",
2712 "DOS line endings\n" . $herevet) &&
2714 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2716 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2717 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2718 if (ERROR("TRAILING_WHITESPACE",
2719 "trailing whitespace\n" . $herevet) &&
2721 $fixed[$fixlinenr] =~ s/\s+$//;
2727 # Check for FSF mailing addresses.
2728 if ($rawline =~ /\bwrite to the Free/i ||
2729 $rawline =~ /\b675\s+Mass\s+Ave/i ||
2730 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2731 $rawline =~ /\b51\s+Franklin\s+St/i) {
2732 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2733 my $msg_type = \&ERROR;
2734 $msg_type = \&CHK if ($file);
2735 &{$msg_type}("FSF_MAILING_ADDRESS",
2736 "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)
2739 # check for Kconfig help text having a real description
2740 # Only applies when adding the entry originally, after that we do not have
2741 # sufficient context to determine whether it is indeed long enough.
2742 if ($realfile =~ /Kconfig/ &&
2743 $line =~ /^\+\s*config\s+/) {
2746 my $ln = $linenr + 1;
2750 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2751 $f = $lines[$ln - 1];
2752 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2753 $is_end = $lines[$ln - 1] =~ /^\+/;
2755 next if ($f =~ /^-/);
2756 last if (!$file && $f =~ /^\@\@/);
2758 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2760 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2767 next if ($f =~ /^$/);
2768 if ($f =~ /^\s*config\s/) {
2774 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2775 WARN("CONFIG_DESCRIPTION",
2776 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2778 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2781 # check for MAINTAINERS entries that don't have the right form
2782 if ($realfile =~ /^MAINTAINERS$/ &&
2783 $rawline =~ /^\+[A-Z]:/ &&
2784 $rawline !~ /^\+[A-Z]:\t\S/) {
2785 if (WARN("MAINTAINERS_STYLE",
2786 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2788 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2792 # discourage the use of boolean for type definition attributes of Kconfig options
2793 if ($realfile =~ /Kconfig/ &&
2794 $line =~ /^\+\s*\bboolean\b/) {
2795 WARN("CONFIG_TYPE_BOOLEAN",
2796 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2799 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2800 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2803 'EXTRA_AFLAGS' => 'asflags-y',
2804 'EXTRA_CFLAGS' => 'ccflags-y',
2805 'EXTRA_CPPFLAGS' => 'cppflags-y',
2806 'EXTRA_LDFLAGS' => 'ldflags-y',
2809 WARN("DEPRECATED_VARIABLE",
2810 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2813 # check for DT compatible documentation
2814 if (defined $root &&
2815 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2816 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2818 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2820 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2821 my $vp_file = $dt_path . "vendor-prefixes.txt";
2823 foreach my $compat (@compats) {
2824 my $compat2 = $compat;
2825 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2826 my $compat3 = $compat;
2827 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2828 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2830 WARN("UNDOCUMENTED_DT_STRING",
2831 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2834 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2836 `grep -Eq "^$vendor\\b" $vp_file`;
2838 WARN("UNDOCUMENTED_DT_STRING",
2839 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2844 # check we are in a valid source file if not then ignore this hunk
2845 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
2847 # line length limit (with some exclusions)
2849 # There are a few types of lines that may extend beyond $max_line_length:
2850 # logging functions like pr_info that end in a string
2851 # lines with a single string
2852 # #defines that are a single string
2854 # There are 3 different line length message types:
2855 # LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2856 # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2857 # LONG_LINE all other lines longer than $max_line_length
2859 # if LONG_LINE is ignored, the other 2 types are also ignored
2862 if ($line =~ /^\+/ && $length > $max_line_length) {
2863 my $msg_type = "LONG_LINE";
2865 # Check the allowed long line types first
2867 # logging functions that end in a string that starts
2868 # before $max_line_length
2869 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2870 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2873 # lines with only strings (w/ possible termination)
2874 # #defines with only strings
2875 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2876 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2879 # EFI_GUID is another special case
2880 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2883 # Otherwise set the alternate message types
2885 # a comment starts before $max_line_length
2886 } elsif ($line =~ /($;[\s$;]*)$/ &&
2887 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2888 $msg_type = "LONG_LINE_COMMENT"
2890 # a quoted string starts before $max_line_length
2891 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2892 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2893 $msg_type = "LONG_LINE_STRING"
2896 if ($msg_type ne "" &&
2897 (show_type("LONG_LINE") || show_type($msg_type))) {
2899 "line over $max_line_length characters\n" . $herecurr);
2903 # check for adding lines without a newline.
2904 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2905 WARN("MISSING_EOF_NEWLINE",
2906 "adding a line without newline at end of file\n" . $herecurr);
2909 # Blackfin: use hi/lo macros
2910 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2911 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2912 my $herevet = "$here\n" . cat_vet($line) . "\n";
2914 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2916 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2917 my $herevet = "$here\n" . cat_vet($line) . "\n";
2919 "use the HI() macro, not (... >> 16)\n" . $herevet);
2923 # check we are in a valid source file C or perl if not then ignore this hunk
2924 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2926 # at the beginning of a line any tabs must come first and anything
2927 # more than 8 must use tabs.
2928 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2929 $rawline =~ /^\+\s* \s*/) {
2930 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2932 if (ERROR("CODE_INDENT",
2933 "code indent should use tabs where possible\n" . $herevet) &&
2935 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2939 # check for space before tabs.
2940 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2941 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2942 if (WARN("SPACE_BEFORE_TAB",
2943 "please, no space before tabs\n" . $herevet) &&
2945 while ($fixed[$fixlinenr] =~
2946 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2947 while ($fixed[$fixlinenr] =~
2948 s/(^\+.*) +\t/$1\t/) {}
2952 # check for && or || at the start of a line
2953 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2954 CHK("LOGICAL_CONTINUATIONS",
2955 "Logical continuations should be on the previous line\n" . $hereprev);
2958 # check indentation starts on a tab stop
2959 if ($^V && $^V ge 5.10.0 &&
2960 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2961 my $indent = length($1);
2964 "Statements should start on a tabstop\n" . $herecurr) &&
2966 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2971 # check multi-line statement indentation matches previous line
2972 if ($^V && $^V ge 5.10.0 &&
2973 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2974 $prevline =~ /^\+(\t*)(.*)$/;
2978 my $pos = pos_last_openparen($rest);
2980 $line =~ /^(\+| )([ \t]*)/;
2983 my $goodtabindent = $oldindent .
2986 my $goodspaceindent = $oldindent . " " x $pos;
2988 if ($newindent ne $goodtabindent &&
2989 $newindent ne $goodspaceindent) {
2991 if (CHK("PARENTHESIS_ALIGNMENT",
2992 "Alignment should match open parenthesis\n" . $hereprev) &&
2993 $fix && $line =~ /^\+/) {
2994 $fixed[$fixlinenr] =~
2995 s/^\+[ \t]*/\+$goodtabindent/;
3001 # check for space after cast like "(int) foo" or "(struct foo) bar"
3002 # avoid checking a few false positives:
3003 # "sizeof(<type>)" or "__alignof__(<type>)"
3004 # function pointer declarations like "(*foo)(int) = bar;"
3005 # structure definitions like "(struct foo) { 0 };"
3006 # multiline macros that define functions
3007 # known attributes or the __attribute__ keyword
3008 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3009 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3011 "No space is necessary after a cast\n" . $herecurr) &&
3013 $fixed[$fixlinenr] =~
3014 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3018 # Block comment styles
3019 # Networking with an initial /*
3020 if ($realfile =~ m@^(drivers/net/|net/)@ &&
3021 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3022 $rawline =~ /^\+[ \t]*\*/ &&
3024 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3025 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3028 # Block comments use * on subsequent lines
3029 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3030 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
3031 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3032 $rawline =~ /^\+/ && #line is new
3033 $rawline !~ /^\+[ \t]*\*/) { #no leading *
3034 WARN("BLOCK_COMMENT_STYLE",
3035 "Block comments use * on subsequent lines\n" . $hereprev);
3038 # Block comments use */ on trailing lines
3039 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
3040 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3041 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3042 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
3043 WARN("BLOCK_COMMENT_STYLE",
3044 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3047 # Block comment * alignment
3048 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3049 $line =~ /^\+[ \t]*$;/ && #leading comment
3050 $rawline =~ /^\+[ \t]*\*/ && #leading *
3051 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3052 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3053 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3055 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3057 $oldindent = expand_tabs($1);
3059 $prevrawline =~ m@^\+(.*/?)\*@;
3060 $oldindent = expand_tabs($1);
3062 $rawline =~ m@^\+([ \t]*)\*@;
3064 $newindent = expand_tabs($newindent);
3065 if (length($oldindent) ne length($newindent)) {
3066 WARN("BLOCK_COMMENT_STYLE",
3067 "Block comments should align the * on each line\n" . $hereprev);
3071 # check for missing blank lines after struct/union declarations
3072 # with exceptions for various attributes and macros
3073 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3075 !($line =~ /^\+\s*$/ ||
3076 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3077 $line =~ /^\+\s*MODULE_/i ||
3078 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3079 $line =~ /^\+[a-z_]*init/ ||
3080 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3081 $line =~ /^\+\s*DECLARE/ ||
3082 $line =~ /^\+\s*__setup/)) {
3083 if (CHK("LINE_SPACING",
3084 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3086 fix_insert_line($fixlinenr, "\+");
3090 # check for multiple consecutive blank lines
3091 if ($prevline =~ /^[\+ ]\s*$/ &&
3092 $line =~ /^\+\s*$/ &&
3093 $last_blank_line != ($linenr - 1)) {
3094 if (CHK("LINE_SPACING",
3095 "Please don't use multiple blank lines\n" . $hereprev) &&
3097 fix_delete_line($fixlinenr, $rawline);
3100 $last_blank_line = $linenr;
3103 # check for missing blank lines after declarations
3104 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3105 # actual declarations
3106 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3107 # function pointer declarations
3108 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3109 # foo bar; where foo is some local typedef or #define
3110 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3111 # known declaration macros
3112 $prevline =~ /^\+\s+$declaration_macros/) &&
3113 # for "else if" which can look like "$Ident $Ident"
3114 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3115 # other possible extensions of declaration lines
3116 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3117 # not starting a section or a macro "\" extended line
3118 $prevline =~ /(?:\{\s*|\\)$/) &&
3119 # looks like a declaration
3120 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3121 # function pointer declarations
3122 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3123 # foo bar; where foo is some local typedef or #define
3124 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3125 # known declaration macros
3126 $sline =~ /^\+\s+$declaration_macros/ ||
3127 # start of struct or union or enum
3128 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
3129 # start or end of block or continuation of declaration
3130 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3131 # bitfield continuation
3132 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3133 # other possible extensions of declaration lines
3134 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3135 # indentation of previous and current line are the same
3136 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3137 if (WARN("LINE_SPACING",
3138 "Missing a blank line after declarations\n" . $hereprev) &&
3140 fix_insert_line($fixlinenr, "\+");
3144 # check for spaces at the beginning of a line.
3146 # 1) within comments
3147 # 2) indented preprocessor commands
3149 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
3150 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3151 if (WARN("LEADING_SPACE",
3152 "please, no spaces at the start of a line\n" . $herevet) &&
3154 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3158 # check we are in a valid C source file if not then ignore this hunk
3159 next if ($realfile !~ /\.(h|c)$/);
3161 # check if this appears to be the start function declaration, save the name
3162 if ($sline =~ /^\+\{\s*$/ &&
3163 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3164 $context_function = $1;
3167 # check if this appears to be the end of function declaration
3168 if ($sline =~ /^\+\}\s*$/) {
3169 undef $context_function;
3172 # check indentation of any line with a bare else
3173 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
3174 # if the previous line is a break or return and is indented 1 tab more...
3175 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3176 my $tabs = length($1) + 1;
3177 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3178 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3179 defined $lines[$linenr] &&
3180 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3181 WARN("UNNECESSARY_ELSE",
3182 "else is not generally useful after a break or return\n" . $hereprev);
3186 # check indentation of a line with a break;
3187 # if the previous line is a goto or return and is indented the same # of tabs
3188 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3190 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3191 WARN("UNNECESSARY_BREAK",
3192 "break is not useful after a goto or return\n" . $hereprev);
3196 # check for RCS/CVS revision markers
3197 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3199 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3202 # Blackfin: don't use __builtin_bfin_[cs]sync
3203 if ($line =~ /__builtin_bfin_csync/) {
3204 my $herevet = "$here\n" . cat_vet($line) . "\n";
3206 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
3208 if ($line =~ /__builtin_bfin_ssync/) {
3209 my $herevet = "$here\n" . cat_vet($line) . "\n";
3211 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
3214 # check for old HOTPLUG __dev<foo> section markings
3215 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3216 WARN("HOTPLUG_SECTION",
3217 "Using $1 is unnecessary\n" . $herecurr);
3220 # Check for potential 'bare' types
3221 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3223 #print "LINE<$line>\n";
3224 if ($linenr > $suppress_statement &&
3225 $realcnt && $sline =~ /.\s*\S/) {
3226 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3227 ctx_statement_block($linenr, $realcnt, 0);
3228 $stat =~ s/\n./\n /g;
3229 $cond =~ s/\n./\n /g;
3231 #print "linenr<$linenr> <$stat>\n";
3232 # If this statement has no statement boundaries within
3233 # it there is no point in retrying a statement scan
3234 # until we hit end of it.
3235 my $frag = $stat; $frag =~ s/;+\s*$//;
3236 if ($frag !~ /(?:{|;)/) {
3237 #print "skip<$line_nr_next>\n";
3238 $suppress_statement = $line_nr_next;
3241 # Find the real next line.
3242 $realline_next = $line_nr_next;
3243 if (defined $realline_next &&
3244 (!defined $lines[$realline_next - 1] ||
3245 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3252 # Ignore goto labels.
3253 if ($s =~ /$Ident:\*$/s) {
3255 # Ignore functions being called
3256 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3258 } elsif ($s =~ /^.\s*else\b/s) {
3260 # declarations always start with types
3261 } 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) {
3264 possible($type, "A:" . $s);
3266 # definitions in global scope can only start with types
3267 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3268 possible($1, "B:" . $s);
3271 # any (foo ... *) is a pointer cast, and foo is a type
3272 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3273 possible($1, "C:" . $s);
3276 # Check for any sort of function declaration.
3277 # int foo(something bar, other baz);
3278 # void (*store_gdt)(x86_descr_ptr *);
3279 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3280 my ($name_len) = length($1);
3283 substr($ctx, 0, $name_len + 1, '');
3284 $ctx =~ s/\)[^\)]*$//;
3286 for my $arg (split(/\s*,\s*/, $ctx)) {
3287 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3289 possible($1, "D:" . $s);
3297 # Checks which may be anchored in the context.
3300 # Check for switch () and associated case and default
3301 # statements should be at the same indent.
3302 if ($line=~/\bswitch\s*\(.*\)/) {
3305 my @ctx = ctx_block_outer($linenr, $realcnt);
3307 for my $ctx (@ctx) {
3308 my ($clen, $cindent) = line_stats($ctx);
3309 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3310 $indent != $cindent) {
3311 $err .= "$sep$ctx\n";
3318 ERROR("SWITCH_CASE_INDENT_LEVEL",
3319 "switch and case should be at the same indent\n$hereline$err");
3323 # if/while/etc brace do not go on next line, unless defining a do while loop,
3324 # or if that brace on the next line is for something else
3325 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3326 my $pre_ctx = "$1$2";
3328 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3330 if ($line =~ /^\+\t{6,}/) {
3331 WARN("DEEP_INDENTATION",
3332 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3335 my $ctx_cnt = $realcnt - $#ctx - 1;
3336 my $ctx = join("\n", @ctx);
3338 my $ctx_ln = $linenr;
3339 my $ctx_skip = $realcnt;
3341 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3342 defined $lines[$ctx_ln - 1] &&
3343 $lines[$ctx_ln - 1] =~ /^-/)) {
3344 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3345 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3349 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3350 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3352 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3354 "that open brace { should be on the previous line\n" .
3355 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3357 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3358 $ctx =~ /\)\s*\;\s*$/ &&
3359 defined $lines[$ctx_ln - 1])
3361 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3362 if ($nindent > $indent) {
3363 WARN("TRAILING_SEMICOLON",
3364 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3365 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3370 # Check relative indent for conditionals and blocks.
3371 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3372 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3373 ctx_statement_block($linenr, $realcnt, 0)
3374 if (!defined $stat);
3375 my ($s, $c) = ($stat, $cond);
3377 substr($s, 0, length($c), '');
3379 # remove inline comments
3383 # Find out how long the conditional actually is.
3384 my @newlines = ($c =~ /\n/gs);
3385 my $cond_lines = 1 + $#newlines;
3387 # Make sure we remove the line prefixes as we have
3388 # none on the first line, and are going to readd them
3391 while ($s =~ /\n\s+\\\n/) {
3392 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3395 # We want to check the first line inside the block
3396 # starting at the end of the conditional, so remove:
3397 # 1) any blank line termination
3398 # 2) any opening brace { on end of the line
3400 my $continuation = 0;
3402 $s =~ s/^.*\bdo\b//;
3404 if ($s =~ s/^\s*\\//) {
3407 if ($s =~ s/^\s*?\n//) {
3412 # Also ignore a loop construct at the end of a
3413 # preprocessor statement.
3414 if (($prevline =~ /^.\s*#\s*define\s/ ||
3415 $prevline =~ /\\\s*$/) && $continuation == 0) {
3421 while ($cond_ptr != $cond_lines) {
3422 $cond_ptr = $cond_lines;
3424 # If we see an #else/#elif then the code
3426 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3431 # 1) blank lines, they should be at 0,
3432 # 2) preprocessor lines, and
3434 if ($continuation ||
3436 $s =~ /^\s*#\s*?/ ||
3437 $s =~ /^\s*$Ident\s*:/) {
3438 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3439 if ($s =~ s/^.*?\n//) {
3445 my (undef, $sindent) = line_stats("+" . $s);
3446 my $stat_real = raw_line($linenr, $cond_lines);
3448 # Check if either of these lines are modified, else
3449 # this is not this patch's fault.
3450 if (!defined($stat_real) ||
3451 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3454 if (defined($stat_real) && $cond_lines > 1) {
3455 $stat_real = "[...]\n$stat_real";
3458 #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";
3460 if ($check && $s ne '' &&
3461 (($sindent % 8) != 0 ||
3462 ($sindent < $indent) ||
3463 ($sindent == $indent &&
3464 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3465 ($sindent > $indent + 8))) {
3466 WARN("SUSPECT_CODE_INDENT",
3467 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3471 # Track the 'values' across context and added lines.
3472 my $opline = $line; $opline =~ s/^./ /;
3473 my ($curr_values, $curr_vars) =
3474 annotate_values($opline . "\n", $prev_values);
3475 $curr_values = $prev_values . $curr_values;
3477 my $outline = $opline; $outline =~ s/\t/ /g;
3478 print "$linenr > .$outline\n";
3479 print "$linenr > $curr_values\n";
3480 print "$linenr > $curr_vars\n";
3482 $prev_values = substr($curr_values, -1);
3484 #ignore lines not being added
3485 next if ($line =~ /^[^\+]/);
3487 # check for dereferences that span multiple lines
3488 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3489 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3490 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3492 $line =~ /^.\s*($Lval)/;
3495 WARN("MULTILINE_DEREFERENCE",
3496 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3499 # check for declarations of signed or unsigned without int
3500 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3503 $var = "" if (!defined $var);
3504 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3508 $pointer = "" if (!defined $pointer);
3510 if (WARN("UNSPECIFIED_INT",
3511 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3513 my $decl = trim($sign) . " int ";
3514 my $comp_pointer = $pointer;
3515 $comp_pointer =~ s/\s//g;
3516 $decl .= $comp_pointer;
3517 $decl = rtrim($decl) if ($var eq "");
3518 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3523 # TEST: allow direct testing of the type matcher.
3525 if ($line =~ /^.\s*$Declare\s*$/) {
3527 "TEST: is type\n" . $herecurr);
3528 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3529 ERROR("TEST_NOT_TYPE",
3530 "TEST: is not type ($1 is)\n". $herecurr);
3534 # TEST: allow direct testing of the attribute matcher.
3536 if ($line =~ /^.\s*$Modifier\s*$/) {
3538 "TEST: is attr\n" . $herecurr);
3539 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3540 ERROR("TEST_NOT_ATTR",
3541 "TEST: is not attr ($1 is)\n". $herecurr);
3546 # check for initialisation to aggregates open brace on the next line
3547 if ($line =~ /^.\s*{/ &&
3548 $prevline =~ /(?:^|[^=])=\s*$/) {
3549 if (ERROR("OPEN_BRACE",
3550 "that open brace { should be on the previous line\n" . $hereprev) &&
3551 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3552 fix_delete_line($fixlinenr - 1, $prevrawline);
3553 fix_delete_line($fixlinenr, $rawline);
3554 my $fixedline = $prevrawline;
3555 $fixedline =~ s/\s*=\s*$/ = {/;
3556 fix_insert_line($fixlinenr, $fixedline);
3558 $fixedline =~ s/^(.\s*){\s*/$1/;
3559 fix_insert_line($fixlinenr, $fixedline);
3564 # Checks which are anchored on the added line.
3567 # check for malformed paths in #include statements (uses RAW line)
3568 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3570 if ($path =~ m{//}) {
3571 ERROR("MALFORMED_INCLUDE",
3572 "malformed #include filename\n" . $herecurr);
3574 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3575 ERROR("UAPI_INCLUDE",
3576 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3580 # no C99 // comments
3581 if ($line =~ m{//}) {
3582 if (ERROR("C99_COMMENTS",
3583 "do not use C99 // comments\n" . $herecurr) &&
3585 my $line = $fixed[$fixlinenr];
3586 if ($line =~ /\/\/(.*)$/) {
3587 my $comment = trim($1);
3588 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3592 # Remove C99 comments.
3594 $opline =~ s@//.*@@;
3596 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3597 # the whole statement.
3598 #print "APW <$lines[$realline_next - 1]>\n";
3599 if (defined $realline_next &&
3600 exists $lines[$realline_next - 1] &&
3601 !defined $suppress_export{$realline_next} &&
3602 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3603 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3604 # Handle definitions which produce identifiers with
3607 # EXPORT_SYMBOL(something_foo);
3609 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3610 $name =~ /^${Ident}_$2/) {
3611 #print "FOO C name<$name>\n";
3612 $suppress_export{$realline_next} = 1;
3614 } elsif ($stat !~ /(?:
3616 ^.DEFINE_$Ident\(\Q$name\E\)|
3617 ^.DECLARE_$Ident\(\Q$name\E\)|
3618 ^.LIST_HEAD\(\Q$name\E\)|
3619 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3620 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3622 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3623 $suppress_export{$realline_next} = 2;
3625 $suppress_export{$realline_next} = 1;
3628 if (!defined $suppress_export{$linenr} &&
3629 $prevline =~ /^.\s*$/ &&
3630 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3631 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3632 #print "FOO B <$lines[$linenr - 1]>\n";
3633 $suppress_export{$linenr} = 2;
3635 if (defined $suppress_export{$linenr} &&
3636 $suppress_export{$linenr} == 2) {
3637 WARN("EXPORT_SYMBOL",
3638 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3641 # check for global initialisers.
3642 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3643 if (ERROR("GLOBAL_INITIALISERS",
3644 "do not initialise globals to $1\n" . $herecurr) &&
3646 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3649 # check for static initialisers.
3650 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3651 if (ERROR("INITIALISED_STATIC",
3652 "do not initialise statics to $1\n" .
3655 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3659 # check for misordered declarations of char/short/int/long with signed/unsigned
3660 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3662 WARN("MISORDERED_TYPE",
3663 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3666 # check for static const char * arrays.
3667 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3668 WARN("STATIC_CONST_CHAR_ARRAY",
3669 "static const char * array should probably be static const char * const\n" .
3673 # check for static char foo[] = "bar" declarations.
3674 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3675 WARN("STATIC_CONST_CHAR_ARRAY",
3676 "static char array declaration should probably be static const char\n" .
3680 # check for const <foo> const where <foo> is not a pointer or array type
3681 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3683 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3685 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3686 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3688 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3692 # check for non-global char *foo[] = {"bar", ...} declarations.
3693 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3694 WARN("STATIC_CONST_CHAR_ARRAY",
3695 "char * array declaration might be better as static const\n" .
3699 # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3700 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3702 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3704 if (WARN("ARRAY_SIZE",
3705 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3707 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3712 # check for function declarations without arguments like "int foo()"
3713 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3714 if (ERROR("FUNCTION_WITHOUT_ARGS",
3715 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3717 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3721 # check for new typedefs, only function parameters and sparse annotations
3723 if ($line =~ /\btypedef\s/ &&
3724 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3725 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3726 $line !~ /\b$typeTypedefs\b/ &&
3727 $line !~ /\b__bitwise\b/) {
3728 WARN("NEW_TYPEDEFS",
3729 "do not add new typedefs\n" . $herecurr);
3732 # * goes on variable not on type
3734 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3736 my ($ident, $from, $to) = ($1, $2, $2);
3738 # Should start with a space.
3739 $to =~ s/^(\S)/ $1/;
3740 # Should not end with a space.
3742 # '*'s should not have spaces between.
3743 while ($to =~ s/\*\s+\*/\*\*/) {
3746 ## print "1: from<$from> to<$to> ident<$ident>\n";
3748 if (ERROR("POINTER_LOCATION",
3749 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3751 my $sub_from = $ident;
3752 my $sub_to = $ident;
3753 $sub_to =~ s/\Q$from\E/$to/;
3754 $fixed[$fixlinenr] =~
3755 s@\Q$sub_from\E@$sub_to@;
3759 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3761 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3763 # Should start with a space.
3764 $to =~ s/^(\S)/ $1/;
3765 # Should not end with a space.
3767 # '*'s should not have spaces between.
3768 while ($to =~ s/\*\s+\*/\*\*/) {
3770 # Modifiers should have spaces.
3771 $to =~ s/(\b$Modifier$)/$1 /;
3773 ## print "2: from<$from> to<$to> ident<$ident>\n";
3774 if ($from ne $to && $ident !~ /^$Modifier$/) {
3775 if (ERROR("POINTER_LOCATION",
3776 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3779 my $sub_from = $match;
3780 my $sub_to = $match;
3781 $sub_to =~ s/\Q$from\E/$to/;
3782 $fixed[$fixlinenr] =~
3783 s@\Q$sub_from\E@$sub_to@;
3788 # avoid BUG() or BUG_ON()
3789 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3790 my $msg_type = \&WARN;
3791 $msg_type = \&CHK if ($file);
3792 &{$msg_type}("AVOID_BUG",
3793 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3796 # avoid LINUX_VERSION_CODE
3797 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3798 WARN("LINUX_VERSION_CODE",
3799 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3802 # check for uses of printk_ratelimit
3803 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3804 WARN("PRINTK_RATELIMITED",
3805 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3808 # printk should use KERN_* levels. Note that follow on printk's on the
3809 # same line do not need a level, so we use the current block context
3810 # to try and find and validate the current printk. In summary the current
3811 # printk includes all preceding printk's which have no newline on the end.
3812 # we assume the first bad printk is the one to report.
3813 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3815 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3816 #print "CHECK<$lines[$ln - 1]\n";
3817 # we have a preceding printk if it ends
3818 # with "\n" ignore it, else it is to blame
3819 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3820 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3827 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3828 "printk() should include KERN_ facility level\n" . $herecurr);
3832 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3834 my $level = lc($orig);
3835 $level = "warn" if ($level eq "warning");
3836 my $level2 = $level;
3837 $level2 = "dbg" if ($level eq "debug");
3838 WARN("PREFER_PR_LEVEL",
3839 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3842 if ($line =~ /\bpr_warning\s*\(/) {
3843 if (WARN("PREFER_PR_LEVEL",
3844 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3846 $fixed[$fixlinenr] =~
3847 s/\bpr_warning\b/pr_warn/;
3851 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3853 my $level = lc($orig);
3854 $level = "warn" if ($level eq "warning");
3855 $level = "dbg" if ($level eq "debug");
3856 WARN("PREFER_DEV_LEVEL",
3857 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3860 # ENOSYS means "bad syscall nr" and nothing else. This will have a small
3861 # number of false positives, but assembly files are not checked, so at
3862 # least the arch entry code will not trigger this warning.
3863 if ($line =~ /\bENOSYS\b/) {
3865 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3868 # function brace can't be on same line, except for #defines of do while,
3869 # or if closed on same line
3870 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3871 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
3872 if (ERROR("OPEN_BRACE",
3873 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3875 fix_delete_line($fixlinenr, $rawline);
3876 my $fixed_line = $rawline;
3877 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3880 fix_insert_line($fixlinenr, ltrim($line1));
3881 fix_insert_line($fixlinenr, "\+{");
3882 if ($line2 !~ /^\s*$/) {
3883 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3888 # open braces for enum, union and struct go on the same line.
3889 if ($line =~ /^.\s*{/ &&
3890 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3891 if (ERROR("OPEN_BRACE",
3892 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3893 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3894 fix_delete_line($fixlinenr - 1, $prevrawline);
3895 fix_delete_line($fixlinenr, $rawline);
3896 my $fixedline = rtrim($prevrawline) . " {";
3897 fix_insert_line($fixlinenr, $fixedline);
3898 $fixedline = $rawline;
3899 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3900 if ($fixedline !~ /^\+\s*$/) {
3901 fix_insert_line($fixlinenr, $fixedline);
3906 # missing space after union, struct or enum definition
3907 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3909 "missing space after $1 definition\n" . $herecurr) &&
3911 $fixed[$fixlinenr] =~
3912 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3916 # Function pointer declarations
3917 # check spacing between type, funcptr, and args
3918 # canonical declaration is "type (*funcptr)(args...)"
3919 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3921 my $pre_pointer_space = $2;
3922 my $post_pointer_space = $3;
3924 my $post_funcname_space = $5;
3925 my $pre_args_space = $6;
3927 # the $Declare variable will capture all spaces after the type
3928 # so check it for a missing trailing missing space but pointer return types
3929 # don't need a space so don't warn for those.
3930 my $post_declare_space = "";
3931 if ($declare =~ /(\s+)$/) {
3932 $post_declare_space = $1;
3933 $declare = rtrim($declare);
3935 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3937 "missing space after return type\n" . $herecurr);
3938 $post_declare_space = " ";
3941 # unnecessary space "type (*funcptr)(args...)"
3942 # This test is not currently implemented because these declarations are
3944 # int foo(int bar, ...)
3945 # and this is form shouldn't/doesn't generate a checkpatch warning.
3947 # elsif ($declare =~ /\s{2,}$/) {
3949 # "Multiple spaces after return type\n" . $herecurr);
3952 # unnecessary space "type ( *funcptr)(args...)"
3953 if (defined $pre_pointer_space &&
3954 $pre_pointer_space =~ /^\s/) {
3956 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3959 # unnecessary space "type (* funcptr)(args...)"
3960 if (defined $post_pointer_space &&
3961 $post_pointer_space =~ /^\s/) {
3963 "Unnecessary space before function pointer name\n" . $herecurr);
3966 # unnecessary space "type (*funcptr )(args...)"
3967 if (defined $post_funcname_space &&
3968 $post_funcname_space =~ /^\s/) {
3970 "Unnecessary space after function pointer name\n" . $herecurr);
3973 # unnecessary space "type (*funcptr) (args...)"
3974 if (defined $pre_args_space &&
3975 $pre_args_space =~ /^\s/) {
3977 "Unnecessary space before function pointer arguments\n" . $herecurr);
3980 if (show_type("SPACING") && $fix) {
3981 $fixed[$fixlinenr] =~
3982 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3986 # check for spacing round square brackets; allowed:
3987 # 1. with a type on the left -- int [] a;
3988 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3989 # 3. inside a curly brace -- = { [0...10] = 5 }
3990 while ($line =~ /(.*?\s)\[/g) {
3991 my ($where, $prefix) = ($-[1], $1);
3992 if ($prefix !~ /$Type\s+$/ &&
3993 ($where != 0 || $prefix !~ /^.\s+$/) &&
3994 $prefix !~ /[{,]\s+$/) {
3995 if (ERROR("BRACKET_SPACE",
3996 "space prohibited before open square bracket '['\n" . $herecurr) &&
3998 $fixed[$fixlinenr] =~
3999 s/^(\+.*?)\s+\[/$1\[/;
4004 # check for spaces between functions and their parentheses.
4005 while ($line =~ /($Ident)\s+\(/g) {
4007 my $ctx_before = substr($line, 0, $-[1]);
4008 my $ctx = "$ctx_before$name";
4010 # Ignore those directives where spaces _are_ permitted.
4012 if|for|while|switch|return|case|
4013 volatile|__volatile__|
4014 __attribute__|format|__extension__|
4017 # cpp #define statements have non-optional spaces, ie
4018 # if there is a space between the name and the open
4019 # parenthesis it is simply not a parameter group.
4020 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4022 # cpp #elif statement condition may start with a (
4023 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4025 # If this whole things ends with a type its most
4026 # likely a typedef for a function.
4027 } elsif ($ctx =~ /$Type$/) {
4031 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4033 $fixed[$fixlinenr] =~
4034 s/\b$name\s+\(/$name\(/;
4039 # Check operator spacing.
4040 if (!($line=~/\#\s*include/)) {
4041 my $fixed_line = "";
4045 <<=|>>=|<=|>=|==|!=|
4046 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4047 =>|->|<<|>>|<|>|=|!|~|
4048 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
4051 my @elements = split(/($ops|;)/, $opline);
4053 ## print("element count: <" . $#elements . ">\n");
4054 ## foreach my $el (@elements) {
4055 ## print("el: <$el>\n");
4058 my @fix_elements = ();
4061 foreach my $el (@elements) {
4062 push(@fix_elements, substr($rawline, $off, length($el)));
4063 $off += length($el);
4068 my $blank = copy_spacing($opline);
4069 my $last_after = -1;
4071 for (my $n = 0; $n < $#elements; $n += 2) {
4073 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4075 ## print("n: <$n> good: <$good>\n");
4077 $off += length($elements[$n]);
4079 # Pick up the preceding and succeeding characters.
4080 my $ca = substr($opline, 0, $off);
4082 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4083 $cc = substr($opline, $off + length($elements[$n + 1]));
4085 my $cb = "$ca$;$cc";
4088 $a = 'V' if ($elements[$n] ne '');
4089 $a = 'W' if ($elements[$n] =~ /\s$/);
4090 $a = 'C' if ($elements[$n] =~ /$;$/);
4091 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4092 $a = 'O' if ($elements[$n] eq '');
4093 $a = 'E' if ($ca =~ /^\s*$/);
4095 my $op = $elements[$n + 1];
4098 if (defined $elements[$n + 2]) {
4099 $c = 'V' if ($elements[$n + 2] ne '');
4100 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4101 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4102 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4103 $c = 'O' if ($elements[$n + 2] eq '');
4104 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4109 my $ctx = "${a}x${c}";
4111 my $at = "(ctx:$ctx)";
4113 my $ptr = substr($blank, 0, $off) . "^";
4114 my $hereptr = "$hereline$ptr\n";
4116 # Pull out the value of this operator.
4117 my $op_type = substr($curr_values, $off + 1, 1);
4119 # Get the full operator variant.
4120 my $opv = $op . substr($curr_vars, $off, 1);
4122 # Ignore operators passed as parameters.
4123 if ($op_type ne 'V' &&
4124 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
4127 # } elsif ($op =~ /^$;+$/) {
4129 # ; should have either the end of line or a space or \ after it
4130 } elsif ($op eq ';') {
4131 if ($ctx !~ /.x[WEBC]/ &&
4132 $cc !~ /^\\/ && $cc !~ /^;/) {
4133 if (ERROR("SPACING",
4134 "space required after that '$op' $at\n" . $hereptr)) {
4135 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4141 } elsif ($op eq '//') {
4143 # : when part of a bitfield
4144 } elsif ($opv eq ':B') {
4145 # skip the bitfield test for now
4149 } elsif ($op eq '->') {
4150 if ($ctx =~ /Wx.|.xW/) {
4151 if (ERROR("SPACING",
4152 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4153 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4154 if (defined $fix_elements[$n + 2]) {
4155 $fix_elements[$n + 2] =~ s/^\s+//;
4161 # , must not have a space before and must have a space on the right.
4162 } elsif ($op eq ',') {
4163 my $rtrim_before = 0;
4164 my $space_after = 0;
4165 if ($ctx =~ /Wx./) {
4166 if (ERROR("SPACING",
4167 "space prohibited before that '$op' $at\n" . $hereptr)) {
4172 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
4173 if (ERROR("SPACING",
4174 "space required after that '$op' $at\n" . $hereptr)) {
4180 if ($rtrim_before || $space_after) {
4181 if ($rtrim_before) {
4182 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4184 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4191 # '*' as part of a type definition -- reported already.
4192 } elsif ($opv eq '*_') {
4193 #warn "'*' is part of type\n";
4195 # unary operators should have a space before and
4196 # none after. May be left adjacent to another
4197 # unary operator, or a cast
4198 } elsif ($op eq '!' || $op eq '~' ||
4199 $opv eq '*U' || $opv eq '-U' ||
4200 $opv eq '&U' || $opv eq '&&U') {
4201 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
4202 if (ERROR("SPACING",
4203 "space required before that '$op' $at\n" . $hereptr)) {
4204 if ($n != $last_after + 2) {
4205 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4210 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4211 # A unary '*' may be const
4213 } elsif ($ctx =~ /.xW/) {
4214 if (ERROR("SPACING",
4215 "space prohibited after that '$op' $at\n" . $hereptr)) {
4216 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4217 if (defined $fix_elements[$n + 2]) {
4218 $fix_elements[$n + 2] =~ s/^\s+//;
4224 # unary ++ and unary -- are allowed no space on one side.
4225 } elsif ($op eq '++' or $op eq '--') {
4226 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
4227 if (ERROR("SPACING",
4228 "space required one side of that '$op' $at\n" . $hereptr)) {
4229 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4233 if ($ctx =~ /Wx[BE]/ ||
4234 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
4235 if (ERROR("SPACING",
4236 "space prohibited before that '$op' $at\n" . $hereptr)) {
4237 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4241 if ($ctx =~ /ExW/) {
4242 if (ERROR("SPACING",
4243 "space prohibited after that '$op' $at\n" . $hereptr)) {
4244 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4245 if (defined $fix_elements[$n + 2]) {
4246 $fix_elements[$n + 2] =~ s/^\s+//;
4252 # << and >> may either have or not have spaces both sides
4253 } elsif ($op eq '<<' or $op eq '>>' or
4254 $op eq '&' or $op eq '^' or $op eq '|' or
4255 $op eq '+' or $op eq '-' or
4256 $op eq '*' or $op eq '/' or
4260 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4262 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4263 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4264 $fix_elements[$n + 2] =~ s/^\s+//;
4267 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4269 "space preferred before that '$op' $at\n" . $hereptr)) {
4270 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4274 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
4275 if (ERROR("SPACING",
4276 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4277 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4278 if (defined $fix_elements[$n + 2]) {
4279 $fix_elements[$n + 2] =~ s/^\s+//;
4285 # A colon needs no spaces before when it is
4286 # terminating a case value or a label.
4287 } elsif ($opv eq ':C' || $opv eq ':L') {
4288 if ($ctx =~ /Wx./) {
4289 if (ERROR("SPACING",
4290 "space prohibited before that '$op' $at\n" . $hereptr)) {
4291 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4296 # All the others need spaces both sides.
4297 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4300 # Ignore email addresses <foo@bar>
4302 $cc =~ /^\S+\@\S+>/) ||
4304 $ca =~ /<\S+\@\S+$/))
4309 # for asm volatile statements
4310 # ignore a colon with another
4311 # colon immediately before or after
4313 ($ca =~ /:$/ || $cc =~ /^:/)) {
4317 # messages are ERROR, but ?: are CHK
4319 my $msg_type = \&ERROR;
4320 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4322 if (&{$msg_type}("SPACING",
4323 "spaces required around that '$op' $at\n" . $hereptr)) {
4324 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4325 if (defined $fix_elements[$n + 2]) {
4326 $fix_elements[$n + 2] =~ s/^\s+//;
4332 $off += length($elements[$n + 1]);
4334 ## print("n: <$n> GOOD: <$good>\n");
4336 $fixed_line = $fixed_line . $good;
4339 if (($#elements % 2) == 0) {
4340 $fixed_line = $fixed_line . $fix_elements[$#elements];
4343 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4344 $fixed[$fixlinenr] = $fixed_line;
4350 # check for whitespace before a non-naked semicolon
4351 if ($line =~ /^\+.*\S\s+;\s*$/) {
4353 "space prohibited before semicolon\n" . $herecurr) &&
4355 1 while $fixed[$fixlinenr] =~
4356 s/^(\+.*\S)\s+;/$1;/;
4360 # check for multiple assignments
4361 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4362 CHK("MULTIPLE_ASSIGNMENTS",
4363 "multiple assignments should be avoided\n" . $herecurr);
4366 ## # check for multiple declarations, allowing for a function declaration
4368 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4369 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4371 ## # Remove any bracketed sections to ensure we do not
4372 ## # falsly report the parameters of functions.
4374 ## while ($ln =~ s/\([^\(\)]*\)//g) {
4376 ## if ($ln =~ /,/) {
4377 ## WARN("MULTIPLE_DECLARATION",
4378 ## "declaring multiple variables together should be avoided\n" . $herecurr);
4382 #need space before brace following if, while, etc
4383 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
4385 if (ERROR("SPACING",
4386 "space required before the open brace '{'\n" . $herecurr) &&
4388 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
4392 ## # check for blank lines before declarations
4393 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4394 ## $prevrawline =~ /^.\s*$/) {
4396 ## "No blank lines before declarations\n" . $hereprev);
4400 # closing brace should have a space following it when it has anything
4402 if ($line =~ /}(?!(?:,|;|\)))\S/) {
4403 if (ERROR("SPACING",
4404 "space required after that close brace '}'\n" . $herecurr) &&
4406 $fixed[$fixlinenr] =~
4407 s/}((?!(?:,|;|\)))\S)/} $1/;
4411 # check spacing on square brackets
4412 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
4413 if (ERROR("SPACING",
4414 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4416 $fixed[$fixlinenr] =~
4420 if ($line =~ /\s\]/) {
4421 if (ERROR("SPACING",
4422 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4424 $fixed[$fixlinenr] =~
4429 # check spacing on parentheses
4430 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4431 $line !~ /for\s*\(\s+;/) {
4432 if (ERROR("SPACING",
4433 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4435 $fixed[$fixlinenr] =~
4439 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4440 $line !~ /for\s*\(.*;\s+\)/ &&
4441 $line !~ /:\s+\)/) {
4442 if (ERROR("SPACING",
4443 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4445 $fixed[$fixlinenr] =~
4450 # check unnecessary parentheses around addressof/dereference single $Lvals
4451 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4453 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4455 if (CHK("UNNECESSARY_PARENTHESES",
4456 "Unnecessary parentheses around $var\n" . $herecurr) &&
4458 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4462 # check for unnecessary parentheses around function pointer uses
4463 # ie: (foo->bar)(); should be foo->bar();
4464 # but not "if (foo->bar) (" to avoid some false positives
4465 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4467 if (CHK("UNNECESSARY_PARENTHESES",
4468 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4470 my $var2 = deparenthesize($var);
4472 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4476 #goto labels aren't indented, allow a single space however
4477 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4478 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
4479 if (WARN("INDENTED_LABEL",
4480 "labels should not be indented\n" . $herecurr) &&
4482 $fixed[$fixlinenr] =~
4487 # return is not a function
4488 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4490 if ($^V && $^V ge 5.10.0 &&
4491 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4493 $value = deparenthesize($value);
4494 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4495 ERROR("RETURN_PARENTHESES",
4496 "return is not a function, parentheses are not required\n" . $herecurr);
4498 } elsif ($spacing !~ /\s+/) {
4500 "space required before the open parenthesis '('\n" . $herecurr);
4504 # unnecessary return in a void function
4505 # at end-of-function, with the previous line a single leading tab, then return;
4506 # and the line before that not a goto label target like "out:"
4507 if ($sline =~ /^[ \+]}\s*$/ &&
4508 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4510 $lines[$linenr - 3] =~ /^[ +]/ &&
4511 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4513 "void function return statements are not generally useful\n" . $hereprev);
4516 # if statements using unnecessary parentheses - ie: if ((foo == bar))
4517 if ($^V && $^V ge 5.10.0 &&
4518 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4519 my $openparens = $1;
4520 my $count = $openparens =~ tr@\(@\(@;
4522 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4523 my $comp = $4; #Not $1 because of $LvalOrFunc
4524 $msg = " - maybe == should be = ?" if ($comp eq "==");
4525 WARN("UNNECESSARY_PARENTHESES",
4526 "Unnecessary parentheses$msg\n" . $herecurr);
4530 # comparisons with a constant or upper case identifier on the left
4531 # avoid cases like "foo + BAR < baz"
4532 # only fix matches surrounded by parentheses to avoid incorrect
4533 # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4534 if ($^V && $^V ge 5.10.0 &&
4535 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4540 my $newcomp = $comp;
4541 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4542 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4543 WARN("CONSTANT_COMPARISON",
4544 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4548 } elsif ($comp eq "<=") {
4550 } elsif ($comp eq ">") {
4552 } elsif ($comp eq ">=") {
4555 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4559 # Return of what appears to be an errno should normally be negative
4560 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
4562 if ($name ne 'EOF' && $name ne 'ERROR') {
4563 WARN("USE_NEGATIVE_ERRNO",
4564 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
4568 # Need a space before open parenthesis after if, while etc
4569 if ($line =~ /\b(if|while|for|switch)\(/) {
4570 if (ERROR("SPACING",
4571 "space required before the open parenthesis '('\n" . $herecurr) &&
4573 $fixed[$fixlinenr] =~
4574 s/\b(if|while|for|switch)\(/$1 \(/;
4578 # Check for illegal assignment in if conditional -- and check for trailing
4579 # statements after the conditional.
4580 if ($line =~ /do\s*(?!{)/) {
4581 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4582 ctx_statement_block($linenr, $realcnt, 0)
4583 if (!defined $stat);
4584 my ($stat_next) = ctx_statement_block($line_nr_next,
4585 $remain_next, $off_next);
4586 $stat_next =~ s/\n./\n /g;
4587 ##print "stat<$stat> stat_next<$stat_next>\n";
4589 if ($stat_next =~ /^\s*while\b/) {
4590 # If the statement carries leading newlines,
4591 # then count those as offsets.
4593 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4595 statement_rawlines($whitespace) - 1;
4597 $suppress_whiletrailers{$line_nr_next +
4601 if (!defined $suppress_whiletrailers{$linenr} &&
4602 defined($stat) && defined($cond) &&
4603 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4604 my ($s, $c) = ($stat, $cond);
4606 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4607 ERROR("ASSIGN_IN_IF",
4608 "do not use assignment in if condition\n" . $herecurr);
4611 # Find out what is on the end of the line after the
4613 substr($s, 0, length($c), '');
4615 $s =~ s/$;//g; # Remove any comments
4616 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4617 $c !~ /}\s*while\s*/)
4619 # Find out how long the conditional actually is.
4620 my @newlines = ($c =~ /\n/gs);
4621 my $cond_lines = 1 + $#newlines;
4624 $stat_real = raw_line($linenr, $cond_lines)
4625 . "\n" if ($cond_lines);
4626 if (defined($stat_real) && $cond_lines > 1) {
4627 $stat_real = "[...]\n$stat_real";
4630 ERROR("TRAILING_STATEMENTS",
4631 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4635 # Check for bitwise tests written as boolean
4647 WARN("HEXADECIMAL_BOOLEAN_TEST",
4648 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4651 # if and else should not have general statements after it
4652 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4654 $s =~ s/$;//g; # Remove any comments
4655 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4656 ERROR("TRAILING_STATEMENTS",
4657 "trailing statements should be on next line\n" . $herecurr);
4660 # if should not continue a brace
4661 if ($line =~ /}\s*if\b/) {
4662 ERROR("TRAILING_STATEMENTS",
4663 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
4666 # case and default should not have general statements after them
4667 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4669 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4673 ERROR("TRAILING_STATEMENTS",
4674 "trailing statements should be on next line\n" . $herecurr);
4677 # Check for }<nl>else {, these must be at the same
4678 # indent level to be relevant to each other.
4679 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4680 $previndent == $indent) {
4681 if (ERROR("ELSE_AFTER_BRACE",
4682 "else should follow close brace '}'\n" . $hereprev) &&
4683 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4684 fix_delete_line($fixlinenr - 1, $prevrawline);
4685 fix_delete_line($fixlinenr, $rawline);
4686 my $fixedline = $prevrawline;
4687 $fixedline =~ s/}\s*$//;
4688 if ($fixedline !~ /^\+\s*$/) {
4689 fix_insert_line($fixlinenr, $fixedline);
4691 $fixedline = $rawline;
4692 $fixedline =~ s/^(.\s*)else/$1} else/;
4693 fix_insert_line($fixlinenr, $fixedline);
4697 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4698 $previndent == $indent) {
4699 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4701 # Find out what is on the end of the line after the
4703 substr($s, 0, length($c), '');
4706 if ($s =~ /^\s*;/) {
4707 if (ERROR("WHILE_AFTER_BRACE",
4708 "while should follow close brace '}'\n" . $hereprev) &&
4709 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4710 fix_delete_line($fixlinenr - 1, $prevrawline);
4711 fix_delete_line($fixlinenr, $rawline);
4712 my $fixedline = $prevrawline;
4713 my $trailing = $rawline;
4714 $trailing =~ s/^\+//;
4715 $trailing = trim($trailing);
4716 $fixedline =~ s/}\s*$/} $trailing/;
4717 fix_insert_line($fixlinenr, $fixedline);
4722 #Specific variable tests
4723 while ($line =~ m{($Constant|$Lval)}g) {
4726 #gcc binary extension
4727 if ($var =~ /^$Binary$/) {
4728 if (WARN("GCC_BINARY_CONSTANT",
4729 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4731 my $hexval = sprintf("0x%x", oct($var));
4732 $fixed[$fixlinenr] =~
4733 s/\b$var\b/$hexval/;
4738 if ($var !~ /^$Constant$/ &&
4739 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4740 #Ignore Page<foo> variants
4741 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4742 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4743 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4744 #Ignore some three character SI units explicitly, like MiB and KHz
4745 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
4746 while ($var =~ m{($Ident)}g) {
4748 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4750 seed_camelcase_includes();
4751 if (!$file && !$camelcase_file_seeded) {
4752 seed_camelcase_file($realfile);
4753 $camelcase_file_seeded = 1;
4756 if (!defined $camelcase{$word}) {
4757 $camelcase{$word} = 1;
4759 "Avoid CamelCase: <$word>\n" . $herecurr);
4765 #no spaces allowed after \ in define
4766 if ($line =~ /\#\s*define.*\\\s+$/) {
4767 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4768 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4770 $fixed[$fixlinenr] =~ s/\s+$//;
4774 # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4775 # itself <asm/foo.h> (uses RAW line)
4776 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4778 my $checkfile = "include/linux/$file";
4779 if (-f "$root/$checkfile" &&
4780 $realfile ne $checkfile &&
4781 $1 !~ /$allowed_asm_includes/)
4783 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4784 if ($asminclude > 0) {
4785 if ($realfile =~ m{^arch/}) {
4786 CHK("ARCH_INCLUDE_LINUX",
4787 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4789 WARN("INCLUDE_LINUX",
4790 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4796 # multi-statement macros should be enclosed in a do while loop, grab the
4797 # first statement and ensure its the whole macro if its not enclosed
4798 # in a known good container
4799 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4800 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4803 my ($off, $dstat, $dcond, $rest);
4805 my $has_flow_statement = 0;
4806 my $has_arg_concat = 0;
4807 ($dstat, $dcond, $ln, $cnt, $off) =
4808 ctx_statement_block($linenr, $realcnt, 0);
4810 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4811 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4813 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4814 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
4816 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4817 my $define_args = $1;
4818 my $define_stmt = $dstat;
4821 if (defined $define_args && $define_args ne "") {
4822 $define_args = substr($define_args, 1, length($define_args) - 2);
4823 $define_args =~ s/\s*//g;
4824 @def_args = split(",", $define_args);
4828 $dstat =~ s/\\\n.//g;
4829 $dstat =~ s/^\s*//s;
4830 $dstat =~ s/\s*$//s;
4832 # Flatten any parentheses and braces
4833 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4834 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4835 $dstat =~ s/.\[[^\[\]]*\]/1/)
4839 # Flatten any obvious string concatentation.
4840 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4841 $dstat =~ s/$Ident\s*($String)/$1/)
4845 # Make asm volatile uses seem like a generic function
4846 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4848 my $exceptions = qr{
4861 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4864 my $herectx = $here . "\n";
4865 my $stmt_cnt = statement_rawlines($ctx);
4867 for (my $n = 0; $n < $stmt_cnt; $n++) {
4868 $herectx .= raw_line($linenr, $n) . "\n";
4872 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4873 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4874 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4875 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4876 $dstat !~ /$exceptions/ &&
4877 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4878 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4879 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4880 $dstat !~ /^for\s*$Constant$/ && # for (...)
4881 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4882 $dstat !~ /^do\s*{/ && # do {...
4883 $dstat !~ /^\(\{/ && # ({...
4884 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4886 if ($dstat =~ /^\s*if\b/) {
4887 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4888 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4889 } elsif ($dstat =~ /;/) {
4890 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4891 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4893 ERROR("COMPLEX_MACRO",
4894 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4899 # Make $define_stmt single line, comment-free, etc
4900 my @stmt_array = split('\n', $define_stmt);
4903 foreach my $l (@stmt_array) {
4908 } elsif ($l =~ /^[\+ ]/) {
4909 $define_stmt .= substr($l, 1);
4912 $define_stmt =~ s/$;//g;
4913 $define_stmt =~ s/\s+/ /g;
4914 $define_stmt = trim($define_stmt);
4916 # check if any macro arguments are reused (ignore '...' and 'type')
4917 foreach my $arg (@def_args) {
4918 next if ($arg =~ /\.\.\./);
4919 next if ($arg =~ /^type$/i);
4920 my $tmp = $define_stmt;
4921 $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
4922 $tmp =~ s/\#+\s*$arg\b//g;
4923 $tmp =~ s/\b$arg\s*\#\#//g;
4924 my $use_cnt = $tmp =~ s/\b$arg\b//g;
4926 CHK("MACRO_ARG_REUSE",
4927 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
4929 # check if any macro arguments may have other precedence issues
4930 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
4931 ((defined($1) && $1 ne ',') ||
4932 (defined($2) && $2 ne ','))) {
4933 CHK("MACRO_ARG_PRECEDENCE",
4934 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
4938 # check for macros with flow control, but without ## concatenation
4939 # ## concatenation is commonly a macro that defines a function so ignore those
4940 if ($has_flow_statement && !$has_arg_concat) {
4941 my $herectx = $here . "\n";
4942 my $cnt = statement_rawlines($ctx);
4944 for (my $n = 0; $n < $cnt; $n++) {
4945 $herectx .= raw_line($linenr, $n) . "\n";
4947 WARN("MACRO_WITH_FLOW_CONTROL",
4948 "Macros with flow control statements should be avoided\n" . "$herectx");
4951 # check for line continuations outside of #defines, preprocessor #, and asm
4954 if ($prevline !~ /^..*\\$/ &&
4955 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4956 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4957 $line =~ /^\+.*\\$/) {
4958 WARN("LINE_CONTINUATIONS",
4959 "Avoid unnecessary line continuations\n" . $herecurr);
4963 # do {} while (0) macro tests:
4964 # single-statement macros do not need to be enclosed in do while (0) loop,
4965 # macro should not end with a semicolon
4966 if ($^V && $^V ge 5.10.0 &&
4967 $realfile !~ m@/vmlinux.lds.h$@ &&
4968 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4971 my ($off, $dstat, $dcond, $rest);
4973 ($dstat, $dcond, $ln, $cnt, $off) =
4974 ctx_statement_block($linenr, $realcnt, 0);
4977 $dstat =~ s/\\\n.//g;
4980 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4985 my $cnt = statement_rawlines($ctx);
4986 my $herectx = $here . "\n";
4988 for (my $n = 0; $n < $cnt; $n++) {
4989 $herectx .= raw_line($linenr, $n) . "\n";
4992 if (($stmts =~ tr/;/;/) == 1 &&
4993 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4994 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4995 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4997 if (defined $semis && $semis ne "") {
4998 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4999 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5001 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5003 my $cnt = statement_rawlines($ctx);
5004 my $herectx = $here . "\n";
5006 for (my $n = 0; $n < $cnt; $n++) {
5007 $herectx .= raw_line($linenr, $n) . "\n";
5010 WARN("TRAILING_SEMICOLON",
5011 "macros should not use a trailing semicolon\n" . "$herectx");
5015 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5016 # all assignments may have only one of the following with an assignment:
5019 # VMLINUX_SYMBOL(...)
5020 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
5021 WARN("MISSING_VMLINUX_SYMBOL",
5022 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
5025 # check for redundant bracing round if etc
5026 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5027 my ($level, $endln, @chunks) =
5028 ctx_statement_full($linenr, $realcnt, 1);
5029 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5030 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5031 if ($#chunks > 0 && $level == 0) {
5035 my $herectx = $here . "\n";
5036 my $ln = $linenr - 1;
5037 for my $chunk (@chunks) {
5038 my ($cond, $block) = @{$chunk};
5040 # If the condition carries leading newlines, then count those as offsets.
5041 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5042 my $offset = statement_rawlines($whitespace) - 1;
5044 $allowed[$allow] = 0;
5045 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5047 # We have looked at and allowed this specific line.
5048 $suppress_ifbraces{$ln + $offset} = 1;
5050 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5051 $ln += statement_rawlines($block) - 1;
5053 substr($block, 0, length($cond), '');
5055 $seen++ if ($block =~ /^\s*{/);
5057 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5058 if (statement_lines($cond) > 1) {
5059 #print "APW: ALLOWED: cond<$cond>\n";
5060 $allowed[$allow] = 1;
5062 if ($block =~/\b(?:if|for|while)\b/) {
5063 #print "APW: ALLOWED: block<$block>\n";
5064 $allowed[$allow] = 1;
5066 if (statement_block_size($block) > 1) {
5067 #print "APW: ALLOWED: lines block<$block>\n";
5068 $allowed[$allow] = 1;
5073 my $sum_allowed = 0;
5074 foreach (@allowed) {
5077 if ($sum_allowed == 0) {
5079 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5080 } elsif ($sum_allowed != $allow &&
5083 "braces {} should be used on all arms of this statement\n" . $herectx);
5088 if (!defined $suppress_ifbraces{$linenr - 1} &&
5089 $line =~ /\b(if|while|for|else)\b/) {
5092 # Check the pre-context.
5093 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5094 #print "APW: ALLOWED: pre<$1>\n";
5098 my ($level, $endln, @chunks) =
5099 ctx_statement_full($linenr, $realcnt, $-[0]);
5101 # Check the condition.
5102 my ($cond, $block) = @{$chunks[0]};
5103 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5104 if (defined $cond) {
5105 substr($block, 0, length($cond), '');
5107 if (statement_lines($cond) > 1) {
5108 #print "APW: ALLOWED: cond<$cond>\n";
5111 if ($block =~/\b(?:if|for|while)\b/) {
5112 #print "APW: ALLOWED: block<$block>\n";
5115 if (statement_block_size($block) > 1) {
5116 #print "APW: ALLOWED: lines block<$block>\n";
5119 # Check the post-context.
5120 if (defined $chunks[1]) {
5121 my ($cond, $block) = @{$chunks[1]};
5122 if (defined $cond) {
5123 substr($block, 0, length($cond), '');
5125 if ($block =~ /^\s*\{/) {
5126 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5130 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5131 my $herectx = $here . "\n";
5132 my $cnt = statement_rawlines($block);
5134 for (my $n = 0; $n < $cnt; $n++) {
5135 $herectx .= raw_line($linenr, $n) . "\n";
5139 "braces {} are not necessary for single statement blocks\n" . $herectx);
5143 # check for single line unbalanced braces
5144 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5145 $sline =~ /^.\s*else\s*\{\s*$/) {
5146 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5149 # check for unnecessary blank lines around braces
5150 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5152 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5153 $fix && $prevrawline =~ /^\+/) {
5154 fix_delete_line($fixlinenr - 1, $prevrawline);
5157 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5159 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5161 fix_delete_line($fixlinenr, $rawline);
5165 # no volatiles please
5166 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5167 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5169 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5172 # Check for user-visible strings broken across lines, which breaks the ability
5173 # to grep for the string. Make exceptions when the previous string ends in a
5174 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5175 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5176 if ($line =~ /^\+\s*$String/ &&
5177 $prevline =~ /"\s*$/ &&
5178 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5179 if (WARN("SPLIT_STRING",
5180 "quoted string split across lines\n" . $hereprev) &&
5182 $prevrawline =~ /^\+.*"\s*$/ &&
5183 $last_coalesced_string_linenr != $linenr - 1) {
5184 my $extracted_string = get_quoted_string($line, $rawline);
5185 my $comma_close = "";
5186 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5190 fix_delete_line($fixlinenr - 1, $prevrawline);
5191 fix_delete_line($fixlinenr, $rawline);
5192 my $fixedline = $prevrawline;
5193 $fixedline =~ s/"\s*$//;
5194 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5195 fix_insert_line($fixlinenr - 1, $fixedline);
5196 $fixedline = $rawline;
5197 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5198 if ($fixedline !~ /\+\s*$/) {
5199 fix_insert_line($fixlinenr, $fixedline);
5201 $last_coalesced_string_linenr = $linenr;
5205 # check for missing a space in a string concatenation
5206 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5207 WARN('MISSING_SPACE',
5208 "break quoted strings at a space character\n" . $hereprev);
5211 # check for an embedded function name in a string when the function is known
5212 # This does not work very well for -f --file checking as it depends on patch
5213 # context providing the function name or a single line form for in-file
5214 # function declarations
5215 if ($line =~ /^\+.*$String/ &&
5216 defined($context_function) &&
5217 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5218 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5219 WARN("EMBEDDED_FUNCTION_NAME",
5220 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5223 # check for spaces before a quoted newline
5224 if ($rawline =~ /^.*\".*\s\\n/) {
5225 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5226 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5228 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5233 # concatenated string without spaces between elements
5234 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5235 CHK("CONCATENATED_STRING",
5236 "Concatenated strings should use spaces between elements\n" . $herecurr);
5239 # uncoalesced string fragments
5240 if ($line =~ /$String\s*"/) {
5241 WARN("STRING_FRAGMENTS",
5242 "Consecutive strings are generally better as a single string\n" . $herecurr);
5245 # check for non-standard and hex prefixed decimal printf formats
5246 my $show_L = 1; #don't show the same defect twice
5248 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5249 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5250 $string =~ s/%%/__/g;
5252 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5254 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5258 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5260 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5263 # check for 0x<decimal>
5264 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5265 ERROR("PRINTF_0XDECIMAL",
5266 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5270 # check for line continuations in quoted strings with odd counts of "
5271 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5272 WARN("LINE_CONTINUATIONS",
5273 "Avoid line continuations in quoted strings\n" . $herecurr);
5277 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5278 CHK("REDUNDANT_CODE",
5279 "if this code is redundant consider removing it\n" .
5283 # check for needless "if (<foo>) fn(<foo>)" uses
5284 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5285 my $tested = quotemeta($1);
5286 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5287 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5289 if (WARN('NEEDLESS_IF',
5290 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5293 my $leading_tabs = "";
5294 my $new_leading_tabs = "";
5295 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5300 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5301 $new_leading_tabs = $1;
5302 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5309 fix_delete_line($fixlinenr - 1, $prevrawline);
5310 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5316 # check for unnecessary "Out of Memory" messages
5317 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5318 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5319 (defined $1 || defined $3) &&
5322 my $testline = $lines[$linenr - 3];
5324 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5325 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5327 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
5329 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5333 # check for logging functions with KERN_<LEVEL>
5334 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5335 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5337 if (WARN("UNNECESSARY_KERN_LEVEL",
5338 "Possible unnecessary $level\n" . $herecurr) &&
5340 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5344 # check for logging continuations
5345 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5346 WARN("LOGGING_CONTINUATION",
5347 "Avoid logging continuation uses where feasible\n" . $herecurr);
5350 # check for mask then right shift without a parentheses
5351 if ($^V && $^V ge 5.10.0 &&
5352 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5353 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5354 WARN("MASK_THEN_SHIFT",
5355 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5358 # check for pointer comparisons to NULL
5359 if ($^V && $^V ge 5.10.0) {
5360 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5363 $equal = "" if ($4 eq "!=");
5364 if (CHK("COMPARISON_TO_NULL",
5365 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5367 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5372 # check for bad placement of section $InitAttribute (e.g.: __initdata)
5373 if ($line =~ /(\b$InitAttribute\b)/) {
5375 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5378 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5379 ERROR("MISPLACED_INIT",
5380 "$attr should be placed after $var\n" . $herecurr)) ||
5381 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5382 WARN("MISPLACED_INIT",
5383 "$attr should be placed after $var\n" . $herecurr))) &&
5385 $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;
5390 # check for $InitAttributeData (ie: __initdata) with const
5391 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5393 $attr =~ /($InitAttributePrefix)(.*)/;
5394 my $attr_prefix = $1;
5396 if (ERROR("INIT_ATTRIBUTE",
5397 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5399 $fixed[$fixlinenr] =~
5400 s/$InitAttributeData/${attr_prefix}initconst/;
5404 # check for $InitAttributeConst (ie: __initconst) without const
5405 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5407 if (ERROR("INIT_ATTRIBUTE",
5408 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5410 my $lead = $fixed[$fixlinenr] =~
5411 /(^\+\s*(?:static\s+))/;
5413 $lead = "$lead " if ($lead !~ /^\+$/);
5414 $lead = "${lead}const ";
5415 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5419 # check for __read_mostly with const non-pointer (should just be const)
5420 if ($line =~ /\b__read_mostly\b/ &&
5421 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5422 if (ERROR("CONST_READ_MOSTLY",
5423 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5425 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5429 # don't use __constant_<foo> functions outside of include/uapi/
5430 if ($realfile !~ m@^include/uapi/@ &&
5431 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5432 my $constant_func = $1;
5433 my $func = $constant_func;
5434 $func =~ s/^__constant_//;
5435 if (WARN("CONSTANT_CONVERSION",
5436 "$constant_func should be $func\n" . $herecurr) &&
5438 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5442 # prefer usleep_range over udelay
5443 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
5445 # ignore udelay's < 10, however
5446 if (! ($delay < 10) ) {
5448 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5450 if ($delay > 2000) {
5452 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
5456 # warn about unexpectedly long msleep's
5457 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5460 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5464 # check for comparisons of jiffies
5465 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5466 WARN("JIFFIES_COMPARISON",
5467 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5470 # check for comparisons of get_jiffies_64()
5471 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5472 WARN("JIFFIES_COMPARISON",
5473 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5476 # warn about #ifdefs in C files
5477 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5478 # print "#ifdef in C files should be avoided\n";
5479 # print "$herecurr";
5483 # warn about spacing in #ifdefs
5484 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
5485 if (ERROR("SPACING",
5486 "exactly one space required after that #$1\n" . $herecurr) &&
5488 $fixed[$fixlinenr] =~
5489 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5494 # check for spinlock_t definitions without a comment.
5495 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5496 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5498 if (!ctx_has_comment($first_line, $linenr)) {
5499 CHK("UNCOMMENTED_DEFINITION",
5500 "$1 definition without comment\n" . $herecurr);
5503 # check for memory barriers without a comment.
5509 read_barrier_depends
5511 my $barrier_stems = qr{
5519 my $all_barriers = qr{
5521 smp_(?:$barrier_stems)|
5522 virt_(?:$barrier_stems)
5525 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
5526 if (!ctx_has_comment($first_line, $linenr)) {
5527 WARN("MEMORY_BARRIER",
5528 "memory barrier without comment\n" . $herecurr);
5532 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5534 if ($realfile !~ m@^include/asm-generic/@ &&
5535 $realfile !~ m@/barrier\.h$@ &&
5536 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5537 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5538 WARN("MEMORY_BARRIER",
5539 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5542 # check for waitqueue_active without a comment.
5543 if ($line =~ /\bwaitqueue_active\s*\(/) {
5544 if (!ctx_has_comment($first_line, $linenr)) {
5545 WARN("WAITQUEUE_ACTIVE",
5546 "waitqueue_active without comment\n" . $herecurr);
5550 # check of hardware specific defines
5551 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5553 "architecture specific defines should be avoided\n" . $herecurr);
5556 # Check that the storage class is at the beginning of a declaration
5557 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5558 WARN("STORAGE_CLASS",
5559 "storage class should be at the beginning of the declaration\n" . $herecurr)
5562 # check the location of the inline attribute, that it is between
5563 # storage class and type.
5564 if ($line =~ /\b$Type\s+$Inline\b/ ||
5565 $line =~ /\b$Inline\s+$Storage\b/) {
5566 ERROR("INLINE_LOCATION",
5567 "inline keyword should sit between storage class and type\n" . $herecurr);
5570 # Check for __inline__ and __inline, prefer inline
5571 if ($realfile !~ m@\binclude/uapi/@ &&
5572 $line =~ /\b(__inline__|__inline)\b/) {
5574 "plain inline is preferred over $1\n" . $herecurr) &&
5576 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5581 # Check for __attribute__ packed, prefer __packed
5582 if ($realfile !~ m@\binclude/uapi/@ &&
5583 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5584 WARN("PREFER_PACKED",
5585 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5588 # Check for __attribute__ aligned, prefer __aligned
5589 if ($realfile !~ m@\binclude/uapi/@ &&
5590 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5591 WARN("PREFER_ALIGNED",
5592 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5595 # Check for __attribute__ format(printf, prefer __printf
5596 if ($realfile !~ m@\binclude/uapi/@ &&
5597 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5598 if (WARN("PREFER_PRINTF",
5599 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5601 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5606 # Check for __attribute__ format(scanf, prefer __scanf
5607 if ($realfile !~ m@\binclude/uapi/@ &&
5608 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5609 if (WARN("PREFER_SCANF",
5610 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5612 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5616 # Check for __attribute__ weak, or __weak declarations (may have link issues)
5617 if ($^V && $^V ge 5.10.0 &&
5618 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5619 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5620 $line =~ /\b__weak\b/)) {
5621 ERROR("WEAK_DECLARATION",
5622 "Using weak declarations can have unintended link defects\n" . $herecurr);
5625 # check for c99 types like uint8_t used outside of uapi/ and tools/
5626 if ($realfile !~ m@\binclude/uapi/@ &&
5627 $realfile !~ m@\btools/@ &&
5628 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5630 if ($type =~ /\b($typeC99Typedefs)\b/) {
5632 my $kernel_type = 'u';
5633 $kernel_type = 's' if ($type =~ /^_*[si]/);
5636 if (CHK("PREFER_KERNEL_TYPES",
5637 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5639 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5644 # check for cast of C90 native int or longer types constants
5645 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5648 if (WARN("TYPECAST_INT_CONSTANT",
5649 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5652 my $newconst = $const;
5653 $newconst =~ s/${Int_type}$//;
5654 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5655 if ($cast =~ /\blong\s+long\b/) {
5657 } elsif ($cast =~ /\blong\b/) {
5660 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5664 # check for sizeof(&)
5665 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5666 WARN("SIZEOF_ADDRESS",
5667 "sizeof(& should be avoided\n" . $herecurr);
5670 # check for sizeof without parenthesis
5671 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5672 if (WARN("SIZEOF_PARENTHESIS",
5673 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5675 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5679 # check for struct spinlock declarations
5680 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5681 WARN("USE_SPINLOCK_T",
5682 "struct spinlock should be spinlock_t\n" . $herecurr);
5685 # check for seq_printf uses that could be seq_puts
5686 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5687 my $fmt = get_quoted_string($line, $rawline);
5690 if (WARN("PREFER_SEQ_PUTS",
5691 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5693 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5698 # check for vsprintf extension %p<foo> misuses
5699 if ($^V && $^V ge 5.10.0 &&
5701 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5702 $1 !~ /^_*volatile_*$/) {
5703 my $bad_extension = "";
5704 my $lc = $stat =~ tr@\n@@;
5705 $lc = $lc + $linenr;
5706 for (my $count = $linenr; $count <= $lc; $count++) {
5707 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5709 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNO]).)/) {
5710 $bad_extension = $1;
5714 if ($bad_extension ne "") {
5715 my $stat_real = raw_line($linenr, 0);
5716 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5717 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5719 WARN("VSPRINTF_POINTER_EXTENSION",
5720 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5724 # Check for misused memsets
5725 if ($^V && $^V ge 5.10.0 &&
5727 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5733 if ($ms_size =~ /^(0x|)0$/i) {
5735 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5736 } elsif ($ms_size =~ /^(0x|)1$/i) {
5738 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5742 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5743 # if ($^V && $^V ge 5.10.0 &&
5745 # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5746 # if (WARN("PREFER_ETHER_ADDR_COPY",
5747 # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5749 # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5753 # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5754 # if ($^V && $^V ge 5.10.0 &&
5756 # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5757 # WARN("PREFER_ETHER_ADDR_EQUAL",
5758 # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5761 # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5762 # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5763 # if ($^V && $^V ge 5.10.0 &&
5765 # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5769 # if ($ms_val =~ /^(?:0x|)0+$/i) {
5770 # if (WARN("PREFER_ETH_ZERO_ADDR",
5771 # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5773 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5775 # } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5776 # if (WARN("PREFER_ETH_BROADCAST_ADDR",
5777 # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5779 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5784 # typecasts on min/max could be min_t/max_t
5785 if ($^V && $^V ge 5.10.0 &&
5787 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5788 if (defined $2 || defined $7) {
5790 my $cast1 = deparenthesize($2);
5792 my $cast2 = deparenthesize($7);
5796 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5797 $cast = "$cast1 or $cast2";
5798 } elsif ($cast1 ne "") {
5804 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5808 # check usleep_range arguments
5809 if ($^V && $^V ge 5.10.0 &&
5811 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5815 WARN("USLEEP_RANGE",
5816 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5817 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5819 WARN("USLEEP_RANGE",
5820 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5824 # check for naked sscanf
5825 if ($^V && $^V ge 5.10.0 &&
5827 $line =~ /\bsscanf\b/ &&
5828 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5829 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5830 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5831 my $lc = $stat =~ tr@\n@@;
5832 $lc = $lc + $linenr;
5833 my $stat_real = raw_line($linenr, 0);
5834 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5835 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5837 WARN("NAKED_SSCANF",
5838 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5841 # check for simple sscanf that should be kstrto<foo>
5842 if ($^V && $^V ge 5.10.0 &&
5844 $line =~ /\bsscanf\b/) {
5845 my $lc = $stat =~ tr@\n@@;
5846 $lc = $lc + $linenr;
5847 my $stat_real = raw_line($linenr, 0);
5848 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5849 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5851 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5853 my $count = $format =~ tr@%@%@;
5855 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5856 WARN("SSCANF_TO_KSTRTO",
5857 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5862 # check for new externs in .h files.
5863 if ($realfile =~ /\.h$/ &&
5864 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5865 if (CHK("AVOID_EXTERNS",
5866 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
5868 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
5872 # check for new externs in .c files.
5873 if ($realfile =~ /\.c$/ && defined $stat &&
5874 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5876 my $function_name = $1;
5877 my $paren_space = $2;
5880 if (defined $cond) {
5881 substr($s, 0, length($cond), '');
5883 if ($s =~ /^\s*;/ &&
5884 $function_name ne 'uninitialized_var')
5886 WARN("AVOID_EXTERNS",
5887 "externs should be avoided in .c files\n" . $herecurr);
5890 if ($paren_space =~ /\n/) {
5891 WARN("FUNCTION_ARGUMENTS",
5892 "arguments for function declarations should follow identifier\n" . $herecurr);
5895 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5896 $stat =~ /^.\s*extern\s+/)
5898 WARN("AVOID_EXTERNS",
5899 "externs should be avoided in .c files\n" . $herecurr);
5902 # check for function declarations that have arguments without identifier names
5903 if (defined $stat &&
5904 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5906 my $args = trim($1);
5907 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5909 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5910 WARN("FUNCTION_ARGUMENTS",
5911 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5916 # check for function definitions
5917 if ($^V && $^V ge 5.10.0 &&
5919 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
5920 $context_function = $1;
5922 # check for multiline function definition with misplaced open brace
5924 my $cnt = statement_rawlines($stat);
5925 my $herectx = $here . "\n";
5926 for (my $n = 0; $n < $cnt; $n++) {
5927 my $rl = raw_line($linenr, $n);
5928 $herectx .= $rl . "\n";
5929 $ok = 1 if ($rl =~ /^[ \+]\{/);
5930 $ok = 1 if ($rl =~ /\{/ && $n == 0);
5931 last if $rl =~ /^[ \+].*\{/;
5935 "open brace '{' following function definitions go on the next line\n" . $herectx);
5939 # checks for new __setup's
5940 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5943 if (!grep(/$name/, @setup_docs)) {
5944 CHK("UNDOCUMENTED_SETUP",
5945 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
5949 # check for pointless casting of kmalloc return
5950 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5951 WARN("UNNECESSARY_CASTS",
5952 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
5956 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5957 if ($^V && $^V ge 5.10.0 &&
5958 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5959 CHK("ALLOC_SIZEOF_STRUCT",
5960 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5963 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5964 if ($^V && $^V ge 5.10.0 &&
5966 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
5970 my $newfunc = "kmalloc_array";
5971 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
5974 if ($a1 =~ /^sizeof\s*\S/) {
5978 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5979 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5981 my $herectx = $here . "\n";
5982 my $cnt = statement_rawlines($stat);
5983 for (my $n = 0; $n < $cnt; $n++) {
5984 $herectx .= raw_line($linenr, $n) . "\n";
5986 if (WARN("ALLOC_WITH_MULTIPLY",
5987 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
5990 $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;
5995 # check for krealloc arg reuse
5996 if ($^V && $^V ge 5.10.0 &&
5997 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5998 WARN("KREALLOC_ARG_REUSE",
5999 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6002 # check for alloc argument mismatch
6003 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6004 WARN("ALLOC_ARRAY_ARGS",
6005 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6008 # check for multiple semicolons
6009 if ($line =~ /;\s*;\s*$/) {
6010 if (WARN("ONE_SEMICOLON",
6011 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6013 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6017 # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6018 if ($realfile !~ m@^include/uapi/@ &&
6019 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6021 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6022 if (CHK("BIT_MACRO",
6023 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6025 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6029 # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6030 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6032 if (WARN("PREFER_IS_ENABLED",
6033 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6035 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6039 # check for case / default statements not preceded by break/fallthrough/switch
6040 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6042 my $has_statement = 0;
6044 my $prevline = $linenr;
6045 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6047 my $rline = $rawlines[$prevline - 1];
6048 my $fline = $lines[$prevline - 1];
6049 last if ($fline =~ /^\@\@/);
6050 next if ($fline =~ /^\-/);
6051 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6052 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6053 next if ($fline =~ /^.[\s$;]*$/);
6056 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6058 if (!$has_break && $has_statement) {
6059 WARN("MISSING_BREAK",
6060 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6064 # check for switch/default statements without a break;
6065 if ($^V && $^V ge 5.10.0 &&
6067 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6069 my $herectx = $here . "\n";
6070 my $cnt = statement_rawlines($stat);
6071 for (my $n = 0; $n < $cnt; $n++) {
6072 $herectx .= raw_line($linenr, $n) . "\n";
6074 WARN("DEFAULT_NO_BREAK",
6075 "switch default: should use break\n" . $herectx);
6078 # check for gcc specific __FUNCTION__
6079 if ($line =~ /\b__FUNCTION__\b/) {
6080 if (WARN("USE_FUNC",
6081 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6083 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6087 # check for uses of __DATE__, __TIME__, __TIMESTAMP__
6088 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6090 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6093 # check for use of yield()
6094 if ($line =~ /\byield\s*\(\s*\)/) {
6096 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6099 # check for comparisons against true and false
6100 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6108 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6110 my $type = lc($otype);
6111 if ($type =~ /^(?:true|false)$/) {
6112 if (("$test" eq "==" && "$type" eq "true") ||
6113 ("$test" eq "!=" && "$type" eq "false")) {
6117 CHK("BOOL_COMPARISON",
6118 "Using comparison to $otype is error prone\n" . $herecurr);
6120 ## maybe suggesting a correct construct would better
6121 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6126 # check for semaphores initialized locked
6127 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6128 WARN("CONSIDER_COMPLETION",
6129 "consider using a completion\n" . $herecurr);
6132 # recommend kstrto* over simple_strto* and strict_strto*
6133 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6134 WARN("CONSIDER_KSTRTO",
6135 "$1 is obsolete, use k$3 instead\n" . $herecurr);
6138 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
6139 if ($line =~ /^.\s*__initcall\s*\(/) {
6140 WARN("USE_DEVICE_INITCALL",
6141 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6144 # check for various structs that are normally const (ops, kgdb, device_tree)
6145 # and avoid what seem like struct definitions 'struct foo {'
6146 if ($line !~ /\bconst\b/ &&
6147 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6148 WARN("CONST_STRUCT",
6149 "struct $1 should normally be const\n" . $herecurr);
6152 # use of NR_CPUS is usually wrong
6153 # ignore definitions of NR_CPUS and usage to define arrays as likely right
6154 if ($line =~ /\bNR_CPUS\b/ &&
6155 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6156 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6157 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6158 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6159 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6162 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6165 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6166 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6167 ERROR("DEFINE_ARCH_HAS",
6168 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6171 # likely/unlikely comparisons similar to "(likely(foo) > 0)"
6172 if ($^V && $^V ge 5.10.0 &&
6173 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6174 WARN("LIKELY_MISUSE",
6175 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6178 # whine mightly about in_atomic
6179 if ($line =~ /\bin_atomic\s*\(/) {
6180 if ($realfile =~ m@^drivers/@) {
6182 "do not use in_atomic in drivers\n" . $herecurr);
6183 } elsif ($realfile !~ m@^kernel/@) {
6185 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6189 # whine about ACCESS_ONCE
6190 if ($^V && $^V ge 5.10.0 &&
6191 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6195 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6197 if (WARN("PREFER_WRITE_ONCE",
6198 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6200 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6203 if (WARN("PREFER_READ_ONCE",
6204 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6206 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6211 # check for mutex_trylock_recursive usage
6212 if ($line =~ /mutex_trylock_recursive/) {
6214 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6217 # check for lockdep_set_novalidate_class
6218 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6219 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6220 if ($realfile !~ m@^kernel/lockdep@ &&
6221 $realfile !~ m@^include/linux/lockdep@ &&
6222 $realfile !~ m@^drivers/base/core@) {
6224 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6228 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6229 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6230 WARN("EXPORTED_WORLD_WRITABLE",
6231 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6234 # Mode permission misuses where it seems decimal should be octal
6235 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6236 if ($^V && $^V ge 5.10.0 &&
6238 $line =~ /$mode_perms_search/) {
6239 foreach my $entry (@mode_permission_funcs) {
6240 my $func = $entry->[0];
6241 my $arg_pos = $entry->[1];
6243 my $lc = $stat =~ tr@\n@@;
6244 $lc = $lc + $linenr;
6245 my $stat_real = raw_line($linenr, 0);
6246 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6247 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6253 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6255 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6256 if ($stat =~ /$test/) {
6258 $val = $6 if ($skip_args ne "");
6259 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6260 ($val =~ /^$Octal$/ && length($val) ne 4)) {
6261 ERROR("NON_OCTAL_PERMISSIONS",
6262 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6264 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6265 ERROR("EXPORTED_WORLD_WRITABLE",
6266 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6272 # check for uses of S_<PERMS> that could be octal for readability
6273 if ($line =~ /\b$mode_perms_string_search\b/) {
6279 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6280 $curpos = pos($line);
6283 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6285 $to |= $mode_permission_string_types{$match};
6286 $val .= '\s*\|\s*' if ($val ne "");
6290 $oval =~ s/^\s*\|\s*//;
6291 $oval =~ s/\s*\|\s*$//;
6292 my $octal = sprintf("%04o", $to);
6293 if (WARN("SYMBOLIC_PERMS",
6294 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6296 $fixed[$fixlinenr] =~ s/$val/$octal/;
6300 # validate content of MODULE_LICENSE against list from include/linux/module.h
6301 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6302 my $extracted_string = get_quoted_string($line, $rawline);
6303 my $valid_licenses = qr{
6306 GPL\ and\ additional\ rights|
6312 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6313 WARN("MODULE_LICENSE",
6314 "unknown module license " . $extracted_string . "\n" . $herecurr);
6319 # If we have no input at all, then there is nothing to report on
6320 # so just keep quiet.
6321 if ($#rawlines == -1) {
6325 # In mailback mode only produce a report in the negative, for
6326 # things that appear to be patches.
6327 if ($mailback && ($clean == 1 || !$is_patch)) {
6331 # This is not a patch, and we are are in 'no-patch' mode so
6333 if (!$chk_patch && !$is_patch) {
6337 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
6338 ERROR("NOT_UNIFIED_DIFF",
6339 "Does not appear to be a unified-diff format patch\n");
6341 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6342 ERROR("MISSING_SIGN_OFF",
6343 "Missing Signed-off-by: line(s)\n");
6346 print report_dump();
6347 if ($summary && !($clean == 1 && $quiet == 1)) {
6348 print "$filename " if ($summary_file);
6349 print "total: $cnt_error errors, $cnt_warn warnings, " .
6350 (($check)? "$cnt_chk checks, " : "") .
6351 "$cnt_lines lines checked\n";
6355 # If there were any defects found and not already fixing them
6356 if (!$clean and !$fix) {
6359 NOTE: For some of the reported defects, checkpatch may be able to
6360 mechanically convert to the typical style using --fix or --fix-inplace.
6363 # If there were whitespace errors which cleanpatch can fix
6364 # then suggest that.
6365 if ($rpt_cleaners) {
6369 NOTE: Whitespace errors detected.
6370 You may wish to use scripts/cleanpatch or scripts/cleanfile
6375 if ($clean == 0 && $fix &&
6376 ("@rawlines" ne "@fixed" ||
6377 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
6378 my $newfile = $filename;
6379 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6383 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6385 open($f, '>', $newfile)
6386 or die "$P: Can't open $newfile for write\n";
6387 foreach my $fixed_line (@fixed) {
6390 if ($linecount > 3) {
6391 $fixed_line =~ s/^\+//;
6392 print $f $fixed_line . "\n";
6395 print $f $fixed_line . "\n";
6403 Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6405 Do _NOT_ trust the results written to this file.
6406 Do _NOT_ submit these changes without inspecting them for correctness.
6408 This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6409 No warranties, expressed or implied...
6417 print "$vname has no obvious style problems and is ready for submission.\n";
6419 print "$vname has style problems, please review.\n";