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
16 use Getopt::Long qw(:config no_auto_abbrev);
42 my $configuration_file = ".checkpatch.conf";
43 my $max_line_length = 80;
44 my $ignore_perl_version = 0;
45 my $minimum_perl_version = 5.10.0;
51 Usage: $P [OPTION]... [FILE]...
56 --no-tree run without a kernel tree
57 --no-signoff do not check for 'Signed-off-by' line
58 --patch treat FILE as patchfile (default)
59 --emacs emacs compile window format
60 --terse one line per report
61 -f, --file treat FILE as regular source file
62 --subjective, --strict enable more subjective tests
63 --types TYPE(,TYPE2...) show only these comma separated message types
64 --ignore TYPE(,TYPE2...) ignore various comma separated message types
65 --max-line-length=n set the maximum line length, if exceeded, warn
66 --show-types show the message "types" in the output
67 --root=PATH PATH to the kernel tree root
68 --no-summary suppress the per-file summary
69 --mailback only produce a report in case of warnings/errors
70 --summary-file include the filename in summary
71 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
72 'values', 'possible', 'type', and 'attr' (default
74 --test-only=WORD report only warnings/errors containing WORD
76 --fix EXPERIMENTAL - may create horrible results
77 If correctable single-line errors exist, create
78 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
79 with potential errors corrected to the preferred
81 --fix-inplace EXPERIMENTAL - may create horrible results
82 Is the same as --fix, but overwrites the input
83 file. It's your fault if there's no backup or git
84 --ignore-perl-version override checking of perl version. expect
86 -h, --help, --version display this help and exit
88 When FILE is - read standard input.
94 my $conf = which_conf($configuration_file);
97 open(my $conffile, '<', "$conf")
98 or warn "$P: Can't find a readable $configuration_file file $!\n";
100 while (<$conffile>) {
103 $line =~ s/\s*\n?$//g;
107 next if ($line =~ m/^\s*#/);
108 next if ($line =~ m/^\s*$/);
110 my @words = split(" ", $line);
111 foreach my $word (@words) {
112 last if ($word =~ m/^#/);
113 push (@conf_args, $word);
117 unshift(@ARGV, @conf_args) if @conf_args;
121 'q|quiet+' => \$quiet,
123 'signoff!' => \$chk_signoff,
124 'patch!' => \$chk_patch,
128 'subjective!' => \$check,
129 'strict!' => \$check,
130 'ignore=s' => \@ignore,
132 'show-types!' => \$show_types,
133 'max-line-length=i' => \$max_line_length,
135 'summary!' => \$summary,
136 'mailback!' => \$mailback,
137 'summary-file!' => \$summary_file,
139 'fix-inplace!' => \$fix_inplace,
140 'ignore-perl-version!' => \$ignore_perl_version,
141 'debug=s' => \%debug,
142 'test-only=s' => \$tst_only,
149 $fix = 1 if ($fix_inplace);
150 $check_orig = $check;
154 if ($^V && $^V lt $minimum_perl_version) {
155 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
156 if (!$ignore_perl_version) {
162 print "$P: no input files\n";
166 sub hash_save_array_words {
167 my ($hashRef, $arrayRef) = @_;
169 my @array = split(/,/, join(',', @$arrayRef));
170 foreach my $word (@array) {
171 $word =~ s/\s*\n?$//g;
174 $word =~ tr/[a-z]/[A-Z]/;
176 next if ($word =~ m/^\s*#/);
177 next if ($word =~ m/^\s*$/);
183 sub hash_show_words {
184 my ($hashRef, $prefix) = @_;
186 if ($quiet == 0 && keys %$hashRef) {
187 print "NOTE: $prefix message types:";
188 foreach my $word (sort keys %$hashRef) {
195 hash_save_array_words(\%ignore_type, \@ignore);
196 hash_save_array_words(\%use_type, \@use);
199 my $dbg_possible = 0;
202 for my $key (keys %debug) {
204 eval "\${dbg_$key} = '$debug{$key}';";
208 my $rpt_cleaners = 0;
217 if (!top_of_kernel_tree($root)) {
218 die "$P: $root: --root does not point at a valid tree\n";
221 if (top_of_kernel_tree('.')) {
223 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
224 top_of_kernel_tree($1)) {
229 if (!defined $root) {
230 print "Must be run from the top-level dir. of a kernel tree\n";
235 my $emitted_corrupt = 0;
238 [A-Za-z_][A-Za-z\d_]*
239 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
241 our $Storage = qr{extern|static|asmlinkage};
253 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
254 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
255 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
256 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
257 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
259 # Notes to $Attribute:
260 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
280 ____cacheline_aligned|
281 ____cacheline_aligned_in_smp|
282 ____cacheline_internodealigned_in_smp|
286 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
287 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
288 our $Lval = qr{$Ident(?:$Member)*};
290 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
291 our $Binary = qr{(?i)0b[01]+$Int_type?};
292 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
293 our $Int = qr{[0-9]+$Int_type?};
294 our $Octal = qr{0[0-7]+$Int_type?};
295 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
296 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
297 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
298 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
299 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
300 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
301 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
302 our $Arithmetic = qr{\+|-|\*|\/|%};
306 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
309 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
312 our $NonptrTypeMisordered;
313 our $NonptrTypeWithAttr;
317 our $DeclareMisordered;
319 our $NON_ASCII_UTF8 = qr{
320 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
321 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
322 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
323 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
324 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
325 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
326 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
330 [\x09\x0A\x0D\x20-\x7E] # ASCII
334 our $typeTypedefs = qr{(?x:
335 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
339 our $logFunctions = qr{(?x:
340 printk(?:_ratelimited|_once|)|
341 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
342 WARN(?:_RATELIMIT|_ONCE|)|
345 seq_vprintf|seq_printf|seq_puts
348 our $signature_tags = qr{(?xi:
359 our @typeListMisordered = (
360 qr{char\s+(?:un)?signed},
361 qr{int\s+(?:(?:un)?signed\s+)?short\s},
362 qr{int\s+short(?:\s+(?:un)?signed)},
363 qr{short\s+int(?:\s+(?:un)?signed)},
364 qr{(?:un)?signed\s+int\s+short},
365 qr{short\s+(?:un)?signed},
366 qr{long\s+int\s+(?:un)?signed},
367 qr{int\s+long\s+(?:un)?signed},
368 qr{long\s+(?:un)?signed\s+int},
369 qr{int\s+(?:un)?signed\s+long},
370 qr{int\s+(?:un)?signed},
371 qr{int\s+long\s+long\s+(?:un)?signed},
372 qr{long\s+long\s+int\s+(?:un)?signed},
373 qr{long\s+long\s+(?:un)?signed\s+int},
374 qr{long\s+long\s+(?:un)?signed},
375 qr{long\s+(?:un)?signed},
380 qr{(?:(?:un)?signed\s+)?char},
381 qr{(?:(?:un)?signed\s+)?short\s+int},
382 qr{(?:(?:un)?signed\s+)?short},
383 qr{(?:(?:un)?signed\s+)?int},
384 qr{(?:(?:un)?signed\s+)?long\s+int},
385 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
386 qr{(?:(?:un)?signed\s+)?long\s+long},
387 qr{(?:(?:un)?signed\s+)?long},
396 qr{${Ident}_handler},
397 qr{${Ident}_handler_fn},
400 our @typeListWithAttr = (
402 qr{struct\s+$InitAttribute\s+$Ident},
403 qr{union\s+$InitAttribute\s+$Ident},
406 our @modifierList = (
410 our @mode_permission_funcs = (
412 ["module_param_(?:array|named|string)", 4],
413 ["module_param_array_named", 5],
414 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
415 ["proc_create(?:_data|)", 2],
416 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
419 #Create a search pattern for all these functions to speed up a loop below
420 our $mode_perms_search = "";
421 foreach my $entry (@mode_permission_funcs) {
422 $mode_perms_search .= '|' if ($mode_perms_search ne "");
423 $mode_perms_search .= $entry->[0];
426 our $declaration_macros = qr{(?x:
427 (?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(|
428 (?:$Storage\s+)?LIST_HEAD\s*\(
431 our $allowed_asm_includes = qr{(?x:
435 # memory.h: ARM has a custom one
438 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
439 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
440 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
441 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
442 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
444 (?:$Modifier\s+|const\s+)*
446 (?:typeof|__typeof__)\s*\([^\)]*\)|
450 (?:\s+$Modifier|\s+const)*
452 $NonptrTypeMisordered = qr{
453 (?:$Modifier\s+|const\s+)*
457 (?:\s+$Modifier|\s+const)*
459 $NonptrTypeWithAttr = qr{
460 (?:$Modifier\s+|const\s+)*
462 (?:typeof|__typeof__)\s*\([^\)]*\)|
466 (?:\s+$Modifier|\s+const)*
470 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
471 (?:\s+$Inline|\s+$Modifier)*
473 $TypeMisordered = qr{
474 $NonptrTypeMisordered
475 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
476 (?:\s+$Inline|\s+$Modifier)*
478 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
479 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
483 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
485 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
486 # requires at least perl version v5.10.0
487 # Any use must be runtime checked with $^V
489 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
490 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
491 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
495 return "" if (!defined($string));
497 while ($string =~ /^\s*\(.*\)\s*$/) {
498 $string =~ s@^\s*\(\s*@@;
499 $string =~ s@\s*\)\s*$@@;
502 $string =~ s@\s+@ @g;
507 sub seed_camelcase_file {
510 return if (!(-f $file));
514 open(my $include_file, '<', "$file")
515 or warn "$P: Can't read '$file' $!\n";
516 my $text = <$include_file>;
517 close($include_file);
519 my @lines = split('\n', $text);
521 foreach my $line (@lines) {
522 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
523 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
525 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
527 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
533 my $camelcase_seeded = 0;
534 sub seed_camelcase_includes {
535 return if ($camelcase_seeded);
538 my $camelcase_cache = "";
539 my @include_files = ();
541 $camelcase_seeded = 1;
544 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
545 chomp $git_last_include_commit;
546 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
548 my $last_mod_date = 0;
549 $files = `find $root/include -name "*.h"`;
550 @include_files = split('\n', $files);
551 foreach my $file (@include_files) {
552 my $date = POSIX::strftime("%Y%m%d%H%M",
553 localtime((stat $file)[9]));
554 $last_mod_date = $date if ($last_mod_date < $date);
556 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
559 if ($camelcase_cache ne "" && -f $camelcase_cache) {
560 open(my $camelcase_file, '<', "$camelcase_cache")
561 or warn "$P: Can't read '$camelcase_cache' $!\n";
562 while (<$camelcase_file>) {
566 close($camelcase_file);
572 $files = `git ls-files "include/*.h"`;
573 @include_files = split('\n', $files);
576 foreach my $file (@include_files) {
577 seed_camelcase_file($file);
580 if ($camelcase_cache ne "") {
581 unlink glob ".checkpatch-camelcase.*";
582 open(my $camelcase_file, '>', "$camelcase_cache")
583 or warn "$P: Can't write '$camelcase_cache' $!\n";
584 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
585 print $camelcase_file ("$_\n");
587 close($camelcase_file);
591 sub git_commit_info {
592 my ($commit, $id, $desc) = @_;
594 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
596 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
597 $output =~ s/^\s*//gm;
598 my @lines = split("\n", $output);
600 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
601 # Maybe one day convert this block of bash into something that returns
602 # all matching commit ids, but it's very slow...
604 # echo "checking commits $1..."
605 # git rev-list --remotes | grep -i "^$1" |
606 # while read line ; do
607 # git log --format='%H %s' -1 $line |
608 # echo "commit $(cut -c 1-12,41-)"
610 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
612 $id = substr($lines[0], 0, 12);
613 $desc = substr($lines[0], 41);
619 $chk_signoff = 0 if ($file);
624 my @fixed_inserted = ();
625 my @fixed_deleted = ();
629 for my $filename (@ARGV) {
632 open($FILE, '-|', "diff -u /dev/null $filename") ||
633 die "$P: $filename: diff failed - $!\n";
634 } elsif ($filename eq '-') {
635 open($FILE, '<&STDIN');
637 open($FILE, '<', "$filename") ||
638 die "$P: $filename: open failed - $!\n";
640 if ($filename eq '-') {
641 $vname = 'Your patch';
650 if (!process($filename)) {
656 @fixed_inserted = ();
663 sub top_of_kernel_tree {
667 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
668 "README", "Documentation", "arch", "include", "drivers",
669 "fs", "init", "ipc", "kernel", "lib", "scripts",
672 foreach my $check (@tree_check) {
673 if (! -e $root . '/' . $check) {
681 my ($formatted_email) = @_;
687 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
690 $comment = $3 if defined $3;
691 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
693 $comment = $2 if defined $2;
694 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
696 $comment = $2 if defined $2;
697 $formatted_email =~ s/$address.*$//;
698 $name = $formatted_email;
700 $name =~ s/^\"|\"$//g;
701 # If there's a name left after stripping spaces and
702 # leading quotes, and the address doesn't have both
703 # leading and trailing angle brackets, the address
705 # "joe smith joe@smith.com" bad
706 # "joe smith <joe@smith.com" bad
707 if ($name ne "" && $address !~ /^<[^>]+>$/) {
715 $name =~ s/^\"|\"$//g;
716 $address = trim($address);
717 $address =~ s/^\<|\>$//g;
719 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
720 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
724 return ($name, $address, $comment);
728 my ($name, $address) = @_;
733 $name =~ s/^\"|\"$//g;
734 $address = trim($address);
736 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
737 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
742 $formatted_email = "$address";
744 $formatted_email = "$name <$address>";
747 return $formatted_email;
753 foreach my $path (split(/:/, $ENV{PATH})) {
754 if (-e "$path/$bin") {
765 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
766 if (-e "$path/$conf") {
767 return "$path/$conf";
779 for my $c (split(//, $str)) {
783 for (; ($n % 8) != 0; $n++) {
795 (my $res = shift) =~ tr/\t/ /c;
802 # Drop the diff line leader and expand tabs
804 $line = expand_tabs($line);
806 # Pick the indent from the front of the line.
807 my ($white) = ($line =~ /^(\s*)/);
809 return (length($line), length($white));
812 my $sanitise_quote = '';
814 sub sanitise_line_reset {
815 my ($in_comment) = @_;
818 $sanitise_quote = '*/';
820 $sanitise_quote = '';
833 # Always copy over the diff marker.
834 $res = substr($line, 0, 1);
836 for ($off = 1; $off < length($line); $off++) {
837 $c = substr($line, $off, 1);
839 # Comments we are wacking completly including the begin
840 # and end, all to $;.
841 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
842 $sanitise_quote = '*/';
844 substr($res, $off, 2, "$;$;");
848 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
849 $sanitise_quote = '';
850 substr($res, $off, 2, "$;$;");
854 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
855 $sanitise_quote = '//';
857 substr($res, $off, 2, $sanitise_quote);
862 # A \ in a string means ignore the next character.
863 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
865 substr($res, $off, 2, 'XX');
870 if ($c eq "'" || $c eq '"') {
871 if ($sanitise_quote eq '') {
872 $sanitise_quote = $c;
874 substr($res, $off, 1, $c);
876 } elsif ($sanitise_quote eq $c) {
877 $sanitise_quote = '';
881 #print "c<$c> SQ<$sanitise_quote>\n";
882 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
883 substr($res, $off, 1, $;);
884 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
885 substr($res, $off, 1, $;);
886 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
887 substr($res, $off, 1, 'X');
889 substr($res, $off, 1, $c);
893 if ($sanitise_quote eq '//') {
894 $sanitise_quote = '';
897 # The pathname on a #include may be surrounded by '<' and '>'.
898 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
899 my $clean = 'X' x length($1);
900 $res =~ s@\<.*\>@<$clean>@;
902 # The whole of a #error is a string.
903 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
904 my $clean = 'X' x length($1);
905 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
911 sub get_quoted_string {
912 my ($line, $rawline) = @_;
914 return "" if ($line !~ m/(\"[X]+\")/g);
915 return substr($rawline, $-[0], $+[0] - $-[0]);
918 sub ctx_statement_block {
919 my ($linenr, $remain, $off) = @_;
920 my $line = $linenr - 1;
937 @stack = (['', 0]) if ($#stack == -1);
939 #warn "CSB: blk<$blk> remain<$remain>\n";
940 # If we are about to drop off the end, pull in more
943 for (; $remain > 0; $line++) {
944 last if (!defined $lines[$line]);
945 next if ($lines[$line] =~ /^-/);
948 $blk .= $lines[$line] . "\n";
953 # Bail if there is no further context.
954 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
958 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
964 $c = substr($blk, $off, 1);
965 $remainder = substr($blk, $off);
967 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
969 # Handle nested #if/#else.
970 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
971 push(@stack, [ $type, $level ]);
972 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
973 ($type, $level) = @{$stack[$#stack - 1]};
974 } elsif ($remainder =~ /^#\s*endif\b/) {
975 ($type, $level) = @{pop(@stack)};
978 # Statement ends at the ';' or a close '}' at the
980 if ($level == 0 && $c eq ';') {
984 # An else is really a conditional as long as its not else if
985 if ($level == 0 && $coff_set == 0 &&
986 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
987 $remainder =~ /^(else)(?:\s|{)/ &&
988 $remainder !~ /^else\s+if\b/) {
989 $coff = $off + length($1) - 1;
991 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
992 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
995 if (($type eq '' || $type eq '(') && $c eq '(') {
999 if ($type eq '(' && $c eq ')') {
1001 $type = ($level != 0)? '(' : '';
1003 if ($level == 0 && $coff < $soff) {
1006 #warn "CSB: mark coff<$coff>\n";
1009 if (($type eq '' || $type eq '{') && $c eq '{') {
1013 if ($type eq '{' && $c eq '}') {
1015 $type = ($level != 0)? '{' : '';
1018 if (substr($blk, $off + 1, 1) eq ';') {
1024 # Preprocessor commands end at the newline unless escaped.
1025 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1033 # We are truly at the end, so shuffle to the next line.
1040 my $statement = substr($blk, $soff, $off - $soff + 1);
1041 my $condition = substr($blk, $soff, $coff - $soff + 1);
1043 #warn "STATEMENT<$statement>\n";
1044 #warn "CONDITION<$condition>\n";
1046 #print "coff<$coff> soff<$off> loff<$loff>\n";
1048 return ($statement, $condition,
1049 $line, $remain + 1, $off - $loff + 1, $level);
1052 sub statement_lines {
1055 # Strip the diff line prefixes and rip blank lines at start and end.
1056 $stmt =~ s/(^|\n)./$1/g;
1060 my @stmt_lines = ($stmt =~ /\n/g);
1062 return $#stmt_lines + 2;
1065 sub statement_rawlines {
1068 my @stmt_lines = ($stmt =~ /\n/g);
1070 return $#stmt_lines + 2;
1073 sub statement_block_size {
1076 $stmt =~ s/(^|\n)./$1/g;
1082 my @stmt_lines = ($stmt =~ /\n/g);
1083 my @stmt_statements = ($stmt =~ /;/g);
1085 my $stmt_lines = $#stmt_lines + 2;
1086 my $stmt_statements = $#stmt_statements + 1;
1088 if ($stmt_lines > $stmt_statements) {
1091 return $stmt_statements;
1095 sub ctx_statement_full {
1096 my ($linenr, $remain, $off) = @_;
1097 my ($statement, $condition, $level);
1101 # Grab the first conditional/block pair.
1102 ($statement, $condition, $linenr, $remain, $off, $level) =
1103 ctx_statement_block($linenr, $remain, $off);
1104 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1105 push(@chunks, [ $condition, $statement ]);
1106 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1107 return ($level, $linenr, @chunks);
1110 # Pull in the following conditional/block pairs and see if they
1111 # could continue the statement.
1113 ($statement, $condition, $linenr, $remain, $off, $level) =
1114 ctx_statement_block($linenr, $remain, $off);
1115 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1116 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1118 push(@chunks, [ $condition, $statement ]);
1121 return ($level, $linenr, @chunks);
1125 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1127 my $start = $linenr - 1;
1134 my @stack = ($level);
1135 for ($line = $start; $remain > 0; $line++) {
1136 next if ($rawlines[$line] =~ /^-/);
1139 $blk .= $rawlines[$line];
1141 # Handle nested #if/#else.
1142 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1143 push(@stack, $level);
1144 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1145 $level = $stack[$#stack - 1];
1146 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1147 $level = pop(@stack);
1150 foreach my $c (split(//, $lines[$line])) {
1151 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1157 if ($c eq $close && $level > 0) {
1159 last if ($level == 0);
1160 } elsif ($c eq $open) {
1165 if (!$outer || $level <= 1) {
1166 push(@res, $rawlines[$line]);
1169 last if ($level == 0);
1172 return ($level, @res);
1174 sub ctx_block_outer {
1175 my ($linenr, $remain) = @_;
1177 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1181 my ($linenr, $remain) = @_;
1183 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1187 my ($linenr, $remain, $off) = @_;
1189 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1192 sub ctx_block_level {
1193 my ($linenr, $remain) = @_;
1195 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1197 sub ctx_statement_level {
1198 my ($linenr, $remain, $off) = @_;
1200 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1203 sub ctx_locate_comment {
1204 my ($first_line, $end_line) = @_;
1206 # Catch a comment on the end of the line itself.
1207 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1208 return $current_comment if (defined $current_comment);
1210 # Look through the context and try and figure out if there is a
1213 $current_comment = '';
1214 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1215 my $line = $rawlines[$linenr - 1];
1217 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1220 if ($line =~ m@/\*@) {
1223 if (!$in_comment && $current_comment ne '') {
1224 $current_comment = '';
1226 $current_comment .= $line . "\n" if ($in_comment);
1227 if ($line =~ m@\*/@) {
1232 chomp($current_comment);
1233 return($current_comment);
1235 sub ctx_has_comment {
1236 my ($first_line, $end_line) = @_;
1237 my $cmt = ctx_locate_comment($first_line, $end_line);
1239 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1240 ##print "CMMT: $cmt\n";
1242 return ($cmt ne '');
1246 my ($linenr, $cnt) = @_;
1248 my $offset = $linenr - 1;
1253 $line = $rawlines[$offset++];
1254 next if (defined($line) && $line =~ /^-/);
1266 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1269 $coded = sprintf("^%c", unpack('C', $2) + 64);
1278 my $av_preprocessor = 0;
1283 sub annotate_reset {
1284 $av_preprocessor = 0;
1286 @av_paren_type = ('E');
1287 $av_pend_colon = 'O';
1290 sub annotate_values {
1291 my ($stream, $type) = @_;
1294 my $var = '_' x length($stream);
1297 print "$stream\n" if ($dbg_values > 1);
1299 while (length($cur)) {
1300 @av_paren_type = ('E') if ($#av_paren_type < 0);
1301 print " <" . join('', @av_paren_type) .
1302 "> <$type> <$av_pending>" if ($dbg_values > 1);
1303 if ($cur =~ /^(\s+)/o) {
1304 print "WS($1)\n" if ($dbg_values > 1);
1305 if ($1 =~ /\n/ && $av_preprocessor) {
1306 $type = pop(@av_paren_type);
1307 $av_preprocessor = 0;
1310 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1311 print "CAST($1)\n" if ($dbg_values > 1);
1312 push(@av_paren_type, $type);
1315 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1316 print "DECLARE($1)\n" if ($dbg_values > 1);
1319 } elsif ($cur =~ /^($Modifier)\s*/) {
1320 print "MODIFIER($1)\n" if ($dbg_values > 1);
1323 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1324 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1325 $av_preprocessor = 1;
1326 push(@av_paren_type, $type);
1332 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1333 print "UNDEF($1)\n" if ($dbg_values > 1);
1334 $av_preprocessor = 1;
1335 push(@av_paren_type, $type);
1337 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1338 print "PRE_START($1)\n" if ($dbg_values > 1);
1339 $av_preprocessor = 1;
1341 push(@av_paren_type, $type);
1342 push(@av_paren_type, $type);
1345 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1346 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1347 $av_preprocessor = 1;
1349 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1353 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1354 print "PRE_END($1)\n" if ($dbg_values > 1);
1356 $av_preprocessor = 1;
1358 # Assume all arms of the conditional end as this
1359 # one does, and continue as if the #endif was not here.
1360 pop(@av_paren_type);
1361 push(@av_paren_type, $type);
1364 } elsif ($cur =~ /^(\\\n)/o) {
1365 print "PRECONT($1)\n" if ($dbg_values > 1);
1367 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1368 print "ATTR($1)\n" if ($dbg_values > 1);
1369 $av_pending = $type;
1372 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1373 print "SIZEOF($1)\n" if ($dbg_values > 1);
1379 } elsif ($cur =~ /^(if|while|for)\b/o) {
1380 print "COND($1)\n" if ($dbg_values > 1);
1384 } elsif ($cur =~/^(case)/o) {
1385 print "CASE($1)\n" if ($dbg_values > 1);
1386 $av_pend_colon = 'C';
1389 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1390 print "KEYWORD($1)\n" if ($dbg_values > 1);
1393 } elsif ($cur =~ /^(\()/o) {
1394 print "PAREN('$1')\n" if ($dbg_values > 1);
1395 push(@av_paren_type, $av_pending);
1399 } elsif ($cur =~ /^(\))/o) {
1400 my $new_type = pop(@av_paren_type);
1401 if ($new_type ne '_') {
1403 print "PAREN('$1') -> $type\n"
1404 if ($dbg_values > 1);
1406 print "PAREN('$1')\n" if ($dbg_values > 1);
1409 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1410 print "FUNC($1)\n" if ($dbg_values > 1);
1414 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1415 if (defined $2 && $type eq 'C' || $type eq 'T') {
1416 $av_pend_colon = 'B';
1417 } elsif ($type eq 'E') {
1418 $av_pend_colon = 'L';
1420 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1423 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1424 print "IDENT($1)\n" if ($dbg_values > 1);
1427 } elsif ($cur =~ /^($Assignment)/o) {
1428 print "ASSIGN($1)\n" if ($dbg_values > 1);
1431 } elsif ($cur =~/^(;|{|})/) {
1432 print "END($1)\n" if ($dbg_values > 1);
1434 $av_pend_colon = 'O';
1436 } elsif ($cur =~/^(,)/) {
1437 print "COMMA($1)\n" if ($dbg_values > 1);
1440 } elsif ($cur =~ /^(\?)/o) {
1441 print "QUESTION($1)\n" if ($dbg_values > 1);
1444 } elsif ($cur =~ /^(:)/o) {
1445 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1447 substr($var, length($res), 1, $av_pend_colon);
1448 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1453 $av_pend_colon = 'O';
1455 } elsif ($cur =~ /^(\[)/o) {
1456 print "CLOSE($1)\n" if ($dbg_values > 1);
1459 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1462 print "OPV($1)\n" if ($dbg_values > 1);
1469 substr($var, length($res), 1, $variant);
1472 } elsif ($cur =~ /^($Operators)/o) {
1473 print "OP($1)\n" if ($dbg_values > 1);
1474 if ($1 ne '++' && $1 ne '--') {
1478 } elsif ($cur =~ /(^.)/o) {
1479 print "C($1)\n" if ($dbg_values > 1);
1482 $cur = substr($cur, length($1));
1483 $res .= $type x length($1);
1487 return ($res, $var);
1491 my ($possible, $line) = @_;
1492 my $notPermitted = qr{(?:
1509 ^(?:typedef|struct|enum)\b
1511 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1512 if ($possible !~ $notPermitted) {
1513 # Check for modifiers.
1514 $possible =~ s/\s*$Storage\s*//g;
1515 $possible =~ s/\s*$Sparse\s*//g;
1516 if ($possible =~ /^\s*$/) {
1518 } elsif ($possible =~ /\s/) {
1519 $possible =~ s/\s*$Type\s*//g;
1520 for my $modifier (split(' ', $possible)) {
1521 if ($modifier !~ $notPermitted) {
1522 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1523 push(@modifierList, $modifier);
1528 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1529 push(@typeList, $possible);
1533 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1542 return defined $use_type{$type} if (scalar keys %use_type > 0);
1544 return !defined $ignore_type{$type};
1548 my ($level, $type, $msg) = @_;
1550 if (!show_type($type) ||
1551 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1556 $line = "$prefix$level:$type: $msg\n";
1558 $line = "$prefix$level: $msg\n";
1560 $line = (split('\n', $line))[0] . "\n" if ($terse);
1562 push(our @report, $line);
1571 sub fixup_current_range {
1572 my ($lineRef, $offset, $length) = @_;
1574 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1577 my $no = $o + $offset;
1578 my $nl = $l + $length;
1579 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1583 sub fix_inserted_deleted_lines {
1584 my ($linesRef, $insertedRef, $deletedRef) = @_;
1586 my $range_last_linenr = 0;
1587 my $delta_offset = 0;
1592 my $next_insert = 0;
1593 my $next_delete = 0;
1597 my $inserted = @{$insertedRef}[$next_insert++];
1598 my $deleted = @{$deletedRef}[$next_delete++];
1600 foreach my $old_line (@{$linesRef}) {
1602 my $line = $old_line; #don't modify the array
1603 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1605 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1606 $range_last_linenr = $new_linenr;
1607 fixup_current_range(\$line, $delta_offset, 0);
1610 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1611 $deleted = @{$deletedRef}[$next_delete++];
1613 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1616 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1617 push(@lines, ${$inserted}{'LINE'});
1618 $inserted = @{$insertedRef}[$next_insert++];
1620 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1624 push(@lines, $line);
1634 sub fix_insert_line {
1635 my ($linenr, $line) = @_;
1641 push(@fixed_inserted, $inserted);
1644 sub fix_delete_line {
1645 my ($linenr, $line) = @_;
1652 push(@fixed_deleted, $deleted);
1656 my ($type, $msg) = @_;
1658 if (report("ERROR", $type, $msg)) {
1666 my ($type, $msg) = @_;
1668 if (report("WARNING", $type, $msg)) {
1676 my ($type, $msg) = @_;
1678 if ($check && report("CHECK", $type, $msg)) {
1686 sub check_absolute_file {
1687 my ($absolute, $herecurr) = @_;
1688 my $file = $absolute;
1690 ##print "absolute<$absolute>\n";
1692 # See if any suffix of this path is a path within the tree.
1693 while ($file =~ s@^[^/]*/@@) {
1694 if (-f "$root/$file") {
1695 ##print "file<$file>\n";
1703 # It is, so see if the prefix is acceptable.
1704 my $prefix = $absolute;
1705 substr($prefix, -length($file)) = '';
1707 ##print "prefix<$prefix>\n";
1708 if ($prefix ne ".../") {
1709 WARN("USE_RELATIVE_PATH",
1710 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1717 $string =~ s/^\s+|\s+$//g;
1725 $string =~ s/^\s+//;
1733 $string =~ s/\s+$//;
1738 sub string_find_replace {
1739 my ($string, $find, $replace) = @_;
1741 $string =~ s/$find/$replace/g;
1749 my $source_indent = 8;
1750 my $max_spaces_before_tab = $source_indent - 1;
1751 my $spaces_to_tab = " " x $source_indent;
1753 #convert leading spaces to tabs
1754 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1755 #Remove spaces before a tab
1756 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1761 sub pos_last_openparen {
1766 my $opens = $line =~ tr/\(/\(/;
1767 my $closes = $line =~ tr/\)/\)/;
1769 my $last_openparen = 0;
1771 if (($opens == 0) || ($closes >= $opens)) {
1775 my $len = length($line);
1777 for ($pos = 0; $pos < $len; $pos++) {
1778 my $string = substr($line, $pos);
1779 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1780 $pos += length($1) - 1;
1781 } elsif (substr($line, $pos, 1) eq '(') {
1782 $last_openparen = $pos;
1783 } elsif (index($string, '(') == -1) {
1788 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1792 my $filename = shift;
1798 my $stashrawline="";
1809 my $in_header_lines = $file ? 0 : 1;
1810 my $in_commit_log = 0; #Scanning lines before patch
1811 my $reported_maintainer_file = 0;
1812 my $non_utf8_charset = 0;
1814 my $last_blank_line = 0;
1822 # Trace the real file/line as we go.
1828 my $comment_edge = 0;
1832 my $prev_values = 'E';
1835 my %suppress_ifbraces;
1836 my %suppress_whiletrailers;
1837 my %suppress_export;
1838 my $suppress_statement = 0;
1840 my %signatures = ();
1842 # Pre-scan the patch sanitizing the lines.
1843 # Pre-scan the patch looking for any __setup documentation.
1845 my @setup_docs = ();
1848 my $camelcase_file_seeded = 0;
1850 sanitise_line_reset();
1852 foreach my $rawline (@rawlines) {
1856 push(@fixed, $rawline) if ($fix);
1858 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1860 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1865 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1874 # Guestimate if this is a continuing comment. Run
1875 # the context looking for a comment "edge". If this
1876 # edge is a close comment then we must be in a comment
1880 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1881 next if (defined $rawlines[$ln - 1] &&
1882 $rawlines[$ln - 1] =~ /^-/);
1884 #print "RAW<$rawlines[$ln - 1]>\n";
1885 last if (!defined $rawlines[$ln - 1]);
1886 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1887 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1892 if (defined $edge && $edge eq '*/') {
1896 # Guestimate if this is a continuing comment. If this
1897 # is the start of a diff block and this line starts
1898 # ' *' then it is very likely a comment.
1899 if (!defined $edge &&
1900 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1905 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1906 sanitise_line_reset($in_comment);
1908 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1909 # Standardise the strings and chars within the input to
1910 # simplify matching -- only bother with positive lines.
1911 $line = sanitise_line($rawline);
1913 push(@lines, $line);
1916 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1921 #print "==>$rawline\n";
1922 #print "-->$line\n";
1924 if ($setup_docs && $line =~ /^\+/) {
1925 push(@setup_docs, $line);
1934 foreach my $line (@lines) {
1937 my $sline = $line; #copy of $line
1938 $sline =~ s/$;/ /g; #with comments as spaces
1940 my $rawline = $rawlines[$linenr - 1];
1942 #extract the line range in the file after the patch is applied
1943 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1945 $first_line = $linenr + 1;
1955 %suppress_ifbraces = ();
1956 %suppress_whiletrailers = ();
1957 %suppress_export = ();
1958 $suppress_statement = 0;
1961 # track the line number as we move through the hunk, note that
1962 # new versions of GNU diff omit the leading space on completely
1963 # blank context lines so we need to count that too.
1964 } elsif ($line =~ /^( |\+|$)/) {
1966 $realcnt-- if ($realcnt != 0);
1968 # Measure the line length and indent.
1969 ($length, $indent) = line_stats($rawline);
1971 # Track the previous line.
1972 ($prevline, $stashline) = ($stashline, $line);
1973 ($previndent, $stashindent) = ($stashindent, $indent);
1974 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1976 #warn "line<$line>\n";
1978 } elsif ($realcnt == 1) {
1982 my $hunk_line = ($realcnt != 0);
1984 #make up the handle for any error we report on this line
1985 $prefix = "$filename:$realline: " if ($emacs && $file);
1986 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1988 $here = "#$linenr: " if (!$file);
1989 $here = "#$realline: " if ($file);
1992 # extract the filename as it passes
1993 if ($line =~ /^diff --git.*?(\S+)$/) {
1995 $realfile =~ s@^([^/]*)/@@ if (!$file);
1998 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2000 $realfile =~ s@^([^/]*)/@@ if (!$file);
2004 if (!$file && $tree && $p1_prefix ne '' &&
2005 -e "$root/$p1_prefix") {
2006 WARN("PATCH_PREFIX",
2007 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2010 if ($realfile =~ m@^include/asm/@) {
2011 ERROR("MODIFIED_INCLUDE_ASM",
2012 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2018 if ($realfile =~ m@^(drivers/net/|net/)@) {
2021 $check = $check_orig;
2026 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2028 my $hereline = "$here\n$rawline\n";
2029 my $herecurr = "$here\n$rawline\n";
2030 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2032 $cnt_lines++ if ($realcnt != 0);
2034 # Check for incorrect file permissions
2035 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2036 my $permhere = $here . "FILE: $realfile\n";
2037 if ($realfile !~ m@scripts/@ &&
2038 $realfile !~ /\.(py|pl|awk|sh)$/) {
2039 ERROR("EXECUTE_PERMISSIONS",
2040 "do not set execute permissions for source files\n" . $permhere);
2044 # Check the patch for a signoff:
2045 if ($line =~ /^\s*signed-off-by:/i) {
2050 # Check signature styles
2051 if (!$in_header_lines &&
2052 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2053 my $space_before = $1;
2055 my $space_after = $3;
2057 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2059 if ($sign_off !~ /$signature_tags/) {
2060 WARN("BAD_SIGN_OFF",
2061 "Non-standard signature: $sign_off\n" . $herecurr);
2063 if (defined $space_before && $space_before ne "") {
2064 if (WARN("BAD_SIGN_OFF",
2065 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2067 $fixed[$fixlinenr] =
2068 "$ucfirst_sign_off $email";
2071 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2072 if (WARN("BAD_SIGN_OFF",
2073 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2075 $fixed[$fixlinenr] =
2076 "$ucfirst_sign_off $email";
2080 if (!defined $space_after || $space_after ne " ") {
2081 if (WARN("BAD_SIGN_OFF",
2082 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2084 $fixed[$fixlinenr] =
2085 "$ucfirst_sign_off $email";
2089 my ($email_name, $email_address, $comment) = parse_email($email);
2090 my $suggested_email = format_email(($email_name, $email_address));
2091 if ($suggested_email eq "") {
2092 ERROR("BAD_SIGN_OFF",
2093 "Unrecognized email address: '$email'\n" . $herecurr);
2095 my $dequoted = $suggested_email;
2096 $dequoted =~ s/^"//;
2097 $dequoted =~ s/" </ </;
2098 # Don't force email to have quotes
2099 # Allow just an angle bracketed address
2100 if ("$dequoted$comment" ne $email &&
2101 "<$email_address>$comment" ne $email &&
2102 "$suggested_email$comment" ne $email) {
2103 WARN("BAD_SIGN_OFF",
2104 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2108 # Check for duplicate signatures
2109 my $sig_nospace = $line;
2110 $sig_nospace =~ s/\s//g;
2111 $sig_nospace = lc($sig_nospace);
2112 if (defined $signatures{$sig_nospace}) {
2113 WARN("BAD_SIGN_OFF",
2114 "Duplicate signature\n" . $herecurr);
2116 $signatures{$sig_nospace} = 1;
2120 # Check for old stable address
2121 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2122 ERROR("STABLE_ADDRESS",
2123 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2126 # Check for unwanted Gerrit info
2127 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2128 ERROR("GERRIT_CHANGE_ID",
2129 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2132 # Check for improperly formed commit descriptions
2133 if ($in_commit_log &&
2134 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2135 $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2136 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2138 my $orig_commit = lc($2);
2139 my $id = '01234567890ab';
2140 my $desc = 'commit description';
2141 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2142 ERROR("GIT_COMMIT_ID",
2143 "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2146 # Check for added, moved or deleted files
2147 if (!$reported_maintainer_file && !$in_commit_log &&
2148 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2149 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2150 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2151 (defined($1) || defined($2))))) {
2152 $reported_maintainer_file = 1;
2153 WARN("FILE_PATH_CHANGES",
2154 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2157 # Check for wrappage within a valid hunk of the file
2158 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2159 ERROR("CORRUPTED_PATCH",
2160 "patch seems to be corrupt (line wrapped?)\n" .
2161 $herecurr) if (!$emitted_corrupt++);
2164 # Check for absolute kernel paths.
2166 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2169 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2170 check_absolute_file($1, $herecurr)) {
2173 check_absolute_file($file, $herecurr);
2178 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2179 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2180 $rawline !~ m/^$UTF8*$/) {
2181 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2183 my $blank = copy_spacing($rawline);
2184 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2185 my $hereptr = "$hereline$ptr\n";
2188 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2191 # Check if it's the start of a commit log
2192 # (not a header line and we haven't seen the patch filename)
2193 if ($in_header_lines && $realfile =~ /^$/ &&
2194 !($rawline =~ /^\s+\S/ ||
2195 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2196 $in_header_lines = 0;
2200 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2201 # declined it, i.e defined some charset where it is missing.
2202 if ($in_header_lines &&
2203 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2205 $non_utf8_charset = 1;
2208 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2209 $rawline =~ /$NON_ASCII_UTF8/) {
2210 WARN("UTF8_BEFORE_PATCH",
2211 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2214 # ignore non-hunk lines and lines being removed
2215 next if (!$hunk_line || $line =~ /^-/);
2217 #trailing whitespace
2218 if ($line =~ /^\+.*\015/) {
2219 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2220 if (ERROR("DOS_LINE_ENDINGS",
2221 "DOS line endings\n" . $herevet) &&
2223 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2225 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2226 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2227 if (ERROR("TRAILING_WHITESPACE",
2228 "trailing whitespace\n" . $herevet) &&
2230 $fixed[$fixlinenr] =~ s/\s+$//;
2236 # Check for FSF mailing addresses.
2237 if ($rawline =~ /\bwrite to the Free/i ||
2238 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2239 $rawline =~ /\b51\s+Franklin\s+St/i) {
2240 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2241 my $msg_type = \&ERROR;
2242 $msg_type = \&CHK if ($file);
2243 &{$msg_type}("FSF_MAILING_ADDRESS",
2244 "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)
2247 # check for Kconfig help text having a real description
2248 # Only applies when adding the entry originally, after that we do not have
2249 # sufficient context to determine whether it is indeed long enough.
2250 if ($realfile =~ /Kconfig/ &&
2251 $line =~ /^\+\s*config\s+/) {
2254 my $ln = $linenr + 1;
2258 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2259 $f = $lines[$ln - 1];
2260 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2261 $is_end = $lines[$ln - 1] =~ /^\+/;
2263 next if ($f =~ /^-/);
2264 last if (!$file && $f =~ /^\@\@/);
2266 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2268 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2275 next if ($f =~ /^$/);
2276 if ($f =~ /^\s*config\s/) {
2282 WARN("CONFIG_DESCRIPTION",
2283 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2284 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2287 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2288 if ($realfile =~ /Kconfig/ &&
2289 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2290 WARN("CONFIG_EXPERIMENTAL",
2291 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2294 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2295 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2298 'EXTRA_AFLAGS' => 'asflags-y',
2299 'EXTRA_CFLAGS' => 'ccflags-y',
2300 'EXTRA_CPPFLAGS' => 'cppflags-y',
2301 'EXTRA_LDFLAGS' => 'ldflags-y',
2304 WARN("DEPRECATED_VARIABLE",
2305 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2308 # check for DT compatible documentation
2309 if (defined $root &&
2310 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2311 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2313 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2315 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2316 my $vp_file = $dt_path . "vendor-prefixes.txt";
2318 foreach my $compat (@compats) {
2319 my $compat2 = $compat;
2320 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2321 my $compat3 = $compat;
2322 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2323 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2325 WARN("UNDOCUMENTED_DT_STRING",
2326 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2329 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2331 `grep -Eq "^$vendor\\b" $vp_file`;
2333 WARN("UNDOCUMENTED_DT_STRING",
2334 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2339 # check we are in a valid source file if not then ignore this hunk
2340 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2343 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2344 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2345 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
2346 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2347 $length > $max_line_length)
2350 "line over $max_line_length characters\n" . $herecurr);
2353 # Check for user-visible strings broken across lines, which breaks the ability
2354 # to grep for the string. Make exceptions when the previous string ends in a
2355 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2356 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2357 if ($line =~ /^\+\s*"/ &&
2358 $prevline =~ /"\s*$/ &&
2359 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2360 WARN("SPLIT_STRING",
2361 "quoted string split across lines\n" . $hereprev);
2364 # check for spaces before a quoted newline
2365 if ($rawline =~ /^.*\".*\s\\n/) {
2366 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2367 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2369 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2374 # check for adding lines without a newline.
2375 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2376 WARN("MISSING_EOF_NEWLINE",
2377 "adding a line without newline at end of file\n" . $herecurr);
2380 # Blackfin: use hi/lo macros
2381 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2382 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2383 my $herevet = "$here\n" . cat_vet($line) . "\n";
2385 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2387 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2388 my $herevet = "$here\n" . cat_vet($line) . "\n";
2390 "use the HI() macro, not (... >> 16)\n" . $herevet);
2394 # check we are in a valid source file C or perl if not then ignore this hunk
2395 next if ($realfile !~ /\.(h|c|pl)$/);
2397 # at the beginning of a line any tabs must come first and anything
2398 # more than 8 must use tabs.
2399 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2400 $rawline =~ /^\+\s* \s*/) {
2401 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2403 if (ERROR("CODE_INDENT",
2404 "code indent should use tabs where possible\n" . $herevet) &&
2406 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2410 # check for space before tabs.
2411 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2412 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2413 if (WARN("SPACE_BEFORE_TAB",
2414 "please, no space before tabs\n" . $herevet) &&
2416 while ($fixed[$fixlinenr] =~
2417 s/(^\+.*) {8,8}+\t/$1\t\t/) {}
2418 while ($fixed[$fixlinenr] =~
2419 s/(^\+.*) +\t/$1\t/) {}
2423 # check for && or || at the start of a line
2424 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2425 CHK("LOGICAL_CONTINUATIONS",
2426 "Logical continuations should be on the previous line\n" . $hereprev);
2429 # check multi-line statement indentation matches previous line
2430 if ($^V && $^V ge 5.10.0 &&
2431 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2432 $prevline =~ /^\+(\t*)(.*)$/;
2436 my $pos = pos_last_openparen($rest);
2438 $line =~ /^(\+| )([ \t]*)/;
2441 my $goodtabindent = $oldindent .
2444 my $goodspaceindent = $oldindent . " " x $pos;
2446 if ($newindent ne $goodtabindent &&
2447 $newindent ne $goodspaceindent) {
2449 if (CHK("PARENTHESIS_ALIGNMENT",
2450 "Alignment should match open parenthesis\n" . $hereprev) &&
2451 $fix && $line =~ /^\+/) {
2452 $fixed[$fixlinenr] =~
2453 s/^\+[ \t]*/\+$goodtabindent/;
2459 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
2461 "No space is necessary after a cast\n" . $herecurr) &&
2463 $fixed[$fixlinenr] =~
2464 s/(\(\s*$Type\s*\))[ \t]+/$1/;
2468 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2469 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2470 $rawline =~ /^\+[ \t]*\*/ &&
2472 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2473 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2476 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2477 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2478 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2479 $rawline =~ /^\+/ && #line is new
2480 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2481 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2482 "networking block comments start with * on subsequent lines\n" . $hereprev);
2485 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2486 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2487 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2488 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2489 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
2490 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2491 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2494 # check for missing blank lines after struct/union declarations
2495 # with exceptions for various attributes and macros
2496 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2498 !($line =~ /^\+\s*$/ ||
2499 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2500 $line =~ /^\+\s*MODULE_/i ||
2501 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2502 $line =~ /^\+[a-z_]*init/ ||
2503 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2504 $line =~ /^\+\s*DECLARE/ ||
2505 $line =~ /^\+\s*__setup/)) {
2506 if (CHK("LINE_SPACING",
2507 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2509 fix_insert_line($fixlinenr, "\+");
2513 # check for multiple consecutive blank lines
2514 if ($prevline =~ /^[\+ ]\s*$/ &&
2515 $line =~ /^\+\s*$/ &&
2516 $last_blank_line != ($linenr - 1)) {
2517 if (CHK("LINE_SPACING",
2518 "Please don't use multiple blank lines\n" . $hereprev) &&
2520 fix_delete_line($fixlinenr, $rawline);
2523 $last_blank_line = $linenr;
2526 # check for missing blank lines after declarations
2527 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2528 # actual declarations
2529 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2530 # function pointer declarations
2531 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2532 # foo bar; where foo is some local typedef or #define
2533 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2534 # known declaration macros
2535 $prevline =~ /^\+\s+$declaration_macros/) &&
2536 # for "else if" which can look like "$Ident $Ident"
2537 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2538 # other possible extensions of declaration lines
2539 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2540 # not starting a section or a macro "\" extended line
2541 $prevline =~ /(?:\{\s*|\\)$/) &&
2542 # looks like a declaration
2543 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2544 # function pointer declarations
2545 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2546 # foo bar; where foo is some local typedef or #define
2547 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2548 # known declaration macros
2549 $sline =~ /^\+\s+$declaration_macros/ ||
2550 # start of struct or union or enum
2551 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
2552 # start or end of block or continuation of declaration
2553 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2554 # bitfield continuation
2555 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2556 # other possible extensions of declaration lines
2557 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2558 # indentation of previous and current line are the same
2559 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2560 if (WARN("LINE_SPACING",
2561 "Missing a blank line after declarations\n" . $hereprev) &&
2563 fix_insert_line($fixlinenr, "\+");
2567 # check for spaces at the beginning of a line.
2569 # 1) within comments
2570 # 2) indented preprocessor commands
2572 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2573 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2574 if (WARN("LEADING_SPACE",
2575 "please, no spaces at the start of a line\n" . $herevet) &&
2577 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2581 # check we are in a valid C source file if not then ignore this hunk
2582 next if ($realfile !~ /\.(h|c)$/);
2584 # check indentation of any line with a bare else
2585 # if the previous line is a break or return and is indented 1 tab more...
2586 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2587 my $tabs = length($1) + 1;
2588 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2589 WARN("UNNECESSARY_ELSE",
2590 "else is not generally useful after a break or return\n" . $hereprev);
2594 # check indentation of a line with a break;
2595 # if the previous line is a goto or return and is indented the same # of tabs
2596 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2598 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2599 WARN("UNNECESSARY_BREAK",
2600 "break is not useful after a goto or return\n" . $hereprev);
2604 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2605 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2606 WARN("CONFIG_EXPERIMENTAL",
2607 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2610 # check for RCS/CVS revision markers
2611 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2613 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2616 # Blackfin: don't use __builtin_bfin_[cs]sync
2617 if ($line =~ /__builtin_bfin_csync/) {
2618 my $herevet = "$here\n" . cat_vet($line) . "\n";
2620 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2622 if ($line =~ /__builtin_bfin_ssync/) {
2623 my $herevet = "$here\n" . cat_vet($line) . "\n";
2625 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2628 # check for old HOTPLUG __dev<foo> section markings
2629 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2630 WARN("HOTPLUG_SECTION",
2631 "Using $1 is unnecessary\n" . $herecurr);
2634 # Check for potential 'bare' types
2635 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2637 #print "LINE<$line>\n";
2638 if ($linenr >= $suppress_statement &&
2639 $realcnt && $sline =~ /.\s*\S/) {
2640 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2641 ctx_statement_block($linenr, $realcnt, 0);
2642 $stat =~ s/\n./\n /g;
2643 $cond =~ s/\n./\n /g;
2645 #print "linenr<$linenr> <$stat>\n";
2646 # If this statement has no statement boundaries within
2647 # it there is no point in retrying a statement scan
2648 # until we hit end of it.
2649 my $frag = $stat; $frag =~ s/;+\s*$//;
2650 if ($frag !~ /(?:{|;)/) {
2651 #print "skip<$line_nr_next>\n";
2652 $suppress_statement = $line_nr_next;
2655 # Find the real next line.
2656 $realline_next = $line_nr_next;
2657 if (defined $realline_next &&
2658 (!defined $lines[$realline_next - 1] ||
2659 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2666 # Ignore goto labels.
2667 if ($s =~ /$Ident:\*$/s) {
2669 # Ignore functions being called
2670 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2672 } elsif ($s =~ /^.\s*else\b/s) {
2674 # declarations always start with types
2675 } 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) {
2678 possible($type, "A:" . $s);
2680 # definitions in global scope can only start with types
2681 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2682 possible($1, "B:" . $s);
2685 # any (foo ... *) is a pointer cast, and foo is a type
2686 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2687 possible($1, "C:" . $s);
2690 # Check for any sort of function declaration.
2691 # int foo(something bar, other baz);
2692 # void (*store_gdt)(x86_descr_ptr *);
2693 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2694 my ($name_len) = length($1);
2697 substr($ctx, 0, $name_len + 1, '');
2698 $ctx =~ s/\)[^\)]*$//;
2700 for my $arg (split(/\s*,\s*/, $ctx)) {
2701 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2703 possible($1, "D:" . $s);
2711 # Checks which may be anchored in the context.
2714 # Check for switch () and associated case and default
2715 # statements should be at the same indent.
2716 if ($line=~/\bswitch\s*\(.*\)/) {
2719 my @ctx = ctx_block_outer($linenr, $realcnt);
2721 for my $ctx (@ctx) {
2722 my ($clen, $cindent) = line_stats($ctx);
2723 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2724 $indent != $cindent) {
2725 $err .= "$sep$ctx\n";
2732 ERROR("SWITCH_CASE_INDENT_LEVEL",
2733 "switch and case should be at the same indent\n$hereline$err");
2737 # if/while/etc brace do not go on next line, unless defining a do while loop,
2738 # or if that brace on the next line is for something else
2739 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2740 my $pre_ctx = "$1$2";
2742 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2744 if ($line =~ /^\+\t{6,}/) {
2745 WARN("DEEP_INDENTATION",
2746 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2749 my $ctx_cnt = $realcnt - $#ctx - 1;
2750 my $ctx = join("\n", @ctx);
2752 my $ctx_ln = $linenr;
2753 my $ctx_skip = $realcnt;
2755 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2756 defined $lines[$ctx_ln - 1] &&
2757 $lines[$ctx_ln - 1] =~ /^-/)) {
2758 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2759 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2763 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2764 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2766 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2768 "that open brace { should be on the previous line\n" .
2769 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2771 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2772 $ctx =~ /\)\s*\;\s*$/ &&
2773 defined $lines[$ctx_ln - 1])
2775 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2776 if ($nindent > $indent) {
2777 WARN("TRAILING_SEMICOLON",
2778 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2779 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2784 # Check relative indent for conditionals and blocks.
2785 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2786 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2787 ctx_statement_block($linenr, $realcnt, 0)
2788 if (!defined $stat);
2789 my ($s, $c) = ($stat, $cond);
2791 substr($s, 0, length($c), '');
2793 # Make sure we remove the line prefixes as we have
2794 # none on the first line, and are going to readd them
2798 # Find out how long the conditional actually is.
2799 my @newlines = ($c =~ /\n/gs);
2800 my $cond_lines = 1 + $#newlines;
2802 # We want to check the first line inside the block
2803 # starting at the end of the conditional, so remove:
2804 # 1) any blank line termination
2805 # 2) any opening brace { on end of the line
2807 my $continuation = 0;
2809 $s =~ s/^.*\bdo\b//;
2811 if ($s =~ s/^\s*\\//) {
2814 if ($s =~ s/^\s*?\n//) {
2819 # Also ignore a loop construct at the end of a
2820 # preprocessor statement.
2821 if (($prevline =~ /^.\s*#\s*define\s/ ||
2822 $prevline =~ /\\\s*$/) && $continuation == 0) {
2828 while ($cond_ptr != $cond_lines) {
2829 $cond_ptr = $cond_lines;
2831 # If we see an #else/#elif then the code
2833 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2838 # 1) blank lines, they should be at 0,
2839 # 2) preprocessor lines, and
2841 if ($continuation ||
2843 $s =~ /^\s*#\s*?/ ||
2844 $s =~ /^\s*$Ident\s*:/) {
2845 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2846 if ($s =~ s/^.*?\n//) {
2852 my (undef, $sindent) = line_stats("+" . $s);
2853 my $stat_real = raw_line($linenr, $cond_lines);
2855 # Check if either of these lines are modified, else
2856 # this is not this patch's fault.
2857 if (!defined($stat_real) ||
2858 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2861 if (defined($stat_real) && $cond_lines > 1) {
2862 $stat_real = "[...]\n$stat_real";
2865 #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";
2867 if ($check && (($sindent % 8) != 0 ||
2868 ($sindent <= $indent && $s ne ''))) {
2869 WARN("SUSPECT_CODE_INDENT",
2870 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2874 # Track the 'values' across context and added lines.
2875 my $opline = $line; $opline =~ s/^./ /;
2876 my ($curr_values, $curr_vars) =
2877 annotate_values($opline . "\n", $prev_values);
2878 $curr_values = $prev_values . $curr_values;
2880 my $outline = $opline; $outline =~ s/\t/ /g;
2881 print "$linenr > .$outline\n";
2882 print "$linenr > $curr_values\n";
2883 print "$linenr > $curr_vars\n";
2885 $prev_values = substr($curr_values, -1);
2887 #ignore lines not being added
2888 next if ($line =~ /^[^\+]/);
2890 # TEST: allow direct testing of the type matcher.
2892 if ($line =~ /^.\s*$Declare\s*$/) {
2894 "TEST: is type\n" . $herecurr);
2895 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2896 ERROR("TEST_NOT_TYPE",
2897 "TEST: is not type ($1 is)\n". $herecurr);
2901 # TEST: allow direct testing of the attribute matcher.
2903 if ($line =~ /^.\s*$Modifier\s*$/) {
2905 "TEST: is attr\n" . $herecurr);
2906 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2907 ERROR("TEST_NOT_ATTR",
2908 "TEST: is not attr ($1 is)\n". $herecurr);
2913 # check for initialisation to aggregates open brace on the next line
2914 if ($line =~ /^.\s*{/ &&
2915 $prevline =~ /(?:^|[^=])=\s*$/) {
2916 if (ERROR("OPEN_BRACE",
2917 "that open brace { should be on the previous line\n" . $hereprev) &&
2918 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2919 fix_delete_line($fixlinenr - 1, $prevrawline);
2920 fix_delete_line($fixlinenr, $rawline);
2921 my $fixedline = $prevrawline;
2922 $fixedline =~ s/\s*=\s*$/ = {/;
2923 fix_insert_line($fixlinenr, $fixedline);
2925 $fixedline =~ s/^(.\s*){\s*/$1/;
2926 fix_insert_line($fixlinenr, $fixedline);
2931 # Checks which are anchored on the added line.
2934 # check for malformed paths in #include statements (uses RAW line)
2935 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2937 if ($path =~ m{//}) {
2938 ERROR("MALFORMED_INCLUDE",
2939 "malformed #include filename\n" . $herecurr);
2941 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2942 ERROR("UAPI_INCLUDE",
2943 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2947 # no C99 // comments
2948 if ($line =~ m{//}) {
2949 if (ERROR("C99_COMMENTS",
2950 "do not use C99 // comments\n" . $herecurr) &&
2952 my $line = $fixed[$fixlinenr];
2953 if ($line =~ /\/\/(.*)$/) {
2954 my $comment = trim($1);
2955 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
2959 # Remove C99 comments.
2961 $opline =~ s@//.*@@;
2963 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2964 # the whole statement.
2965 #print "APW <$lines[$realline_next - 1]>\n";
2966 if (defined $realline_next &&
2967 exists $lines[$realline_next - 1] &&
2968 !defined $suppress_export{$realline_next} &&
2969 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2970 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2971 # Handle definitions which produce identifiers with
2974 # EXPORT_SYMBOL(something_foo);
2976 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2977 $name =~ /^${Ident}_$2/) {
2978 #print "FOO C name<$name>\n";
2979 $suppress_export{$realline_next} = 1;
2981 } elsif ($stat !~ /(?:
2983 ^.DEFINE_$Ident\(\Q$name\E\)|
2984 ^.DECLARE_$Ident\(\Q$name\E\)|
2985 ^.LIST_HEAD\(\Q$name\E\)|
2986 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2987 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2989 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2990 $suppress_export{$realline_next} = 2;
2992 $suppress_export{$realline_next} = 1;
2995 if (!defined $suppress_export{$linenr} &&
2996 $prevline =~ /^.\s*$/ &&
2997 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2998 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2999 #print "FOO B <$lines[$linenr - 1]>\n";
3000 $suppress_export{$linenr} = 2;
3002 if (defined $suppress_export{$linenr} &&
3003 $suppress_export{$linenr} == 2) {
3004 WARN("EXPORT_SYMBOL",
3005 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3008 # check for global initialisers.
3009 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3010 if (ERROR("GLOBAL_INITIALISERS",
3011 "do not initialise globals to 0 or NULL\n" .
3014 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3017 # check for static initialisers.
3018 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3019 if (ERROR("INITIALISED_STATIC",
3020 "do not initialise statics to 0 or NULL\n" .
3023 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3027 # check for misordered declarations of char/short/int/long with signed/unsigned
3028 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3030 WARN("MISORDERED_TYPE",
3031 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3034 # check for static const char * arrays.
3035 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3036 WARN("STATIC_CONST_CHAR_ARRAY",
3037 "static const char * array should probably be static const char * const\n" .
3041 # check for static char foo[] = "bar" declarations.
3042 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3043 WARN("STATIC_CONST_CHAR_ARRAY",
3044 "static char array declaration should probably be static const char\n" .
3048 # check for non-global char *foo[] = {"bar", ...} declarations.
3049 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3050 WARN("STATIC_CONST_CHAR_ARRAY",
3051 "char * array declaration might be better as static const\n" .
3055 # check for function declarations without arguments like "int foo()"
3056 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3057 if (ERROR("FUNCTION_WITHOUT_ARGS",
3058 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3060 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3064 # check for uses of DEFINE_PCI_DEVICE_TABLE
3065 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3066 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3067 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3069 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
3073 # check for new typedefs, only function parameters and sparse annotations
3075 if ($line =~ /\btypedef\s/ &&
3076 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3077 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3078 $line !~ /\b$typeTypedefs\b/ &&
3079 $line !~ /\b__bitwise(?:__|)\b/) {
3080 WARN("NEW_TYPEDEFS",
3081 "do not add new typedefs\n" . $herecurr);
3084 # * goes on variable not on type
3086 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3088 my ($ident, $from, $to) = ($1, $2, $2);
3090 # Should start with a space.
3091 $to =~ s/^(\S)/ $1/;
3092 # Should not end with a space.
3094 # '*'s should not have spaces between.
3095 while ($to =~ s/\*\s+\*/\*\*/) {
3098 ## print "1: from<$from> to<$to> ident<$ident>\n";
3100 if (ERROR("POINTER_LOCATION",
3101 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3103 my $sub_from = $ident;
3104 my $sub_to = $ident;
3105 $sub_to =~ s/\Q$from\E/$to/;
3106 $fixed[$fixlinenr] =~
3107 s@\Q$sub_from\E@$sub_to@;
3111 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3113 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3115 # Should start with a space.
3116 $to =~ s/^(\S)/ $1/;
3117 # Should not end with a space.
3119 # '*'s should not have spaces between.
3120 while ($to =~ s/\*\s+\*/\*\*/) {
3122 # Modifiers should have spaces.
3123 $to =~ s/(\b$Modifier$)/$1 /;
3125 ## print "2: from<$from> to<$to> ident<$ident>\n";
3126 if ($from ne $to && $ident !~ /^$Modifier$/) {
3127 if (ERROR("POINTER_LOCATION",
3128 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3131 my $sub_from = $match;
3132 my $sub_to = $match;
3133 $sub_to =~ s/\Q$from\E/$to/;
3134 $fixed[$fixlinenr] =~
3135 s@\Q$sub_from\E@$sub_to@;
3140 # # no BUG() or BUG_ON()
3141 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
3142 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3143 # print "$herecurr";
3147 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3148 WARN("LINUX_VERSION_CODE",
3149 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3152 # check for uses of printk_ratelimit
3153 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3154 WARN("PRINTK_RATELIMITED",
3155 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3158 # printk should use KERN_* levels. Note that follow on printk's on the
3159 # same line do not need a level, so we use the current block context
3160 # to try and find and validate the current printk. In summary the current
3161 # printk includes all preceding printk's which have no newline on the end.
3162 # we assume the first bad printk is the one to report.
3163 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3165 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3166 #print "CHECK<$lines[$ln - 1]\n";
3167 # we have a preceding printk if it ends
3168 # with "\n" ignore it, else it is to blame
3169 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3170 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3177 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3178 "printk() should include KERN_ facility level\n" . $herecurr);
3182 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3184 my $level = lc($orig);
3185 $level = "warn" if ($level eq "warning");
3186 my $level2 = $level;
3187 $level2 = "dbg" if ($level eq "debug");
3188 WARN("PREFER_PR_LEVEL",
3189 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3192 if ($line =~ /\bpr_warning\s*\(/) {
3193 if (WARN("PREFER_PR_LEVEL",
3194 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3196 $fixed[$fixlinenr] =~
3197 s/\bpr_warning\b/pr_warn/;
3201 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3203 my $level = lc($orig);
3204 $level = "warn" if ($level eq "warning");
3205 $level = "dbg" if ($level eq "debug");
3206 WARN("PREFER_DEV_LEVEL",
3207 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3210 # function brace can't be on same line, except for #defines of do while,
3211 # or if closed on same line
3212 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3213 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3214 if (ERROR("OPEN_BRACE",
3215 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3217 fix_delete_line($fixlinenr, $rawline);
3218 my $fixed_line = $rawline;
3219 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3222 fix_insert_line($fixlinenr, ltrim($line1));
3223 fix_insert_line($fixlinenr, "\+{");
3224 if ($line2 !~ /^\s*$/) {
3225 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3230 # open braces for enum, union and struct go on the same line.
3231 if ($line =~ /^.\s*{/ &&
3232 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3233 if (ERROR("OPEN_BRACE",
3234 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3235 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3236 fix_delete_line($fixlinenr - 1, $prevrawline);
3237 fix_delete_line($fixlinenr, $rawline);
3238 my $fixedline = rtrim($prevrawline) . " {";
3239 fix_insert_line($fixlinenr, $fixedline);
3240 $fixedline = $rawline;
3241 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3242 if ($fixedline !~ /^\+\s*$/) {
3243 fix_insert_line($fixlinenr, $fixedline);
3248 # missing space after union, struct or enum definition
3249 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3251 "missing space after $1 definition\n" . $herecurr) &&
3253 $fixed[$fixlinenr] =~
3254 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3258 # Function pointer declarations
3259 # check spacing between type, funcptr, and args
3260 # canonical declaration is "type (*funcptr)(args...)"
3261 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3263 my $pre_pointer_space = $2;
3264 my $post_pointer_space = $3;
3266 my $post_funcname_space = $5;
3267 my $pre_args_space = $6;
3269 # the $Declare variable will capture all spaces after the type
3270 # so check it for a missing trailing missing space but pointer return types
3271 # don't need a space so don't warn for those.
3272 my $post_declare_space = "";
3273 if ($declare =~ /(\s+)$/) {
3274 $post_declare_space = $1;
3275 $declare = rtrim($declare);
3277 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3279 "missing space after return type\n" . $herecurr);
3280 $post_declare_space = " ";
3283 # unnecessary space "type (*funcptr)(args...)"
3284 # This test is not currently implemented because these declarations are
3286 # int foo(int bar, ...)
3287 # and this is form shouldn't/doesn't generate a checkpatch warning.
3289 # elsif ($declare =~ /\s{2,}$/) {
3291 # "Multiple spaces after return type\n" . $herecurr);
3294 # unnecessary space "type ( *funcptr)(args...)"
3295 if (defined $pre_pointer_space &&
3296 $pre_pointer_space =~ /^\s/) {
3298 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3301 # unnecessary space "type (* funcptr)(args...)"
3302 if (defined $post_pointer_space &&
3303 $post_pointer_space =~ /^\s/) {
3305 "Unnecessary space before function pointer name\n" . $herecurr);
3308 # unnecessary space "type (*funcptr )(args...)"
3309 if (defined $post_funcname_space &&
3310 $post_funcname_space =~ /^\s/) {
3312 "Unnecessary space after function pointer name\n" . $herecurr);
3315 # unnecessary space "type (*funcptr) (args...)"
3316 if (defined $pre_args_space &&
3317 $pre_args_space =~ /^\s/) {
3319 "Unnecessary space before function pointer arguments\n" . $herecurr);
3322 if (show_type("SPACING") && $fix) {
3323 $fixed[$fixlinenr] =~
3324 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3328 # check for spacing round square brackets; allowed:
3329 # 1. with a type on the left -- int [] a;
3330 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3331 # 3. inside a curly brace -- = { [0...10] = 5 }
3332 while ($line =~ /(.*?\s)\[/g) {
3333 my ($where, $prefix) = ($-[1], $1);
3334 if ($prefix !~ /$Type\s+$/ &&
3335 ($where != 0 || $prefix !~ /^.\s+$/) &&
3336 $prefix !~ /[{,]\s+$/) {
3337 if (ERROR("BRACKET_SPACE",
3338 "space prohibited before open square bracket '['\n" . $herecurr) &&
3340 $fixed[$fixlinenr] =~
3341 s/^(\+.*?)\s+\[/$1\[/;
3346 # check for spaces between functions and their parentheses.
3347 while ($line =~ /($Ident)\s+\(/g) {
3349 my $ctx_before = substr($line, 0, $-[1]);
3350 my $ctx = "$ctx_before$name";
3352 # Ignore those directives where spaces _are_ permitted.
3354 if|for|while|switch|return|case|
3355 volatile|__volatile__|
3356 __attribute__|format|__extension__|
3359 # cpp #define statements have non-optional spaces, ie
3360 # if there is a space between the name and the open
3361 # parenthesis it is simply not a parameter group.
3362 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3364 # cpp #elif statement condition may start with a (
3365 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3367 # If this whole things ends with a type its most
3368 # likely a typedef for a function.
3369 } elsif ($ctx =~ /$Type$/) {
3373 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3375 $fixed[$fixlinenr] =~
3376 s/\b$name\s+\(/$name\(/;
3381 # Check operator spacing.
3382 if (!($line=~/\#\s*include/)) {
3383 my $fixed_line = "";
3387 <<=|>>=|<=|>=|==|!=|
3388 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3389 =>|->|<<|>>|<|>|=|!|~|
3390 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
3393 my @elements = split(/($ops|;)/, $opline);
3395 ## print("element count: <" . $#elements . ">\n");
3396 ## foreach my $el (@elements) {
3397 ## print("el: <$el>\n");
3400 my @fix_elements = ();
3403 foreach my $el (@elements) {
3404 push(@fix_elements, substr($rawline, $off, length($el)));
3405 $off += length($el);
3410 my $blank = copy_spacing($opline);
3411 my $last_after = -1;
3413 for (my $n = 0; $n < $#elements; $n += 2) {
3415 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3417 ## print("n: <$n> good: <$good>\n");
3419 $off += length($elements[$n]);
3421 # Pick up the preceding and succeeding characters.
3422 my $ca = substr($opline, 0, $off);
3424 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3425 $cc = substr($opline, $off + length($elements[$n + 1]));
3427 my $cb = "$ca$;$cc";
3430 $a = 'V' if ($elements[$n] ne '');
3431 $a = 'W' if ($elements[$n] =~ /\s$/);
3432 $a = 'C' if ($elements[$n] =~ /$;$/);
3433 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3434 $a = 'O' if ($elements[$n] eq '');
3435 $a = 'E' if ($ca =~ /^\s*$/);
3437 my $op = $elements[$n + 1];
3440 if (defined $elements[$n + 2]) {
3441 $c = 'V' if ($elements[$n + 2] ne '');
3442 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
3443 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
3444 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3445 $c = 'O' if ($elements[$n + 2] eq '');
3446 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
3451 my $ctx = "${a}x${c}";
3453 my $at = "(ctx:$ctx)";
3455 my $ptr = substr($blank, 0, $off) . "^";
3456 my $hereptr = "$hereline$ptr\n";
3458 # Pull out the value of this operator.
3459 my $op_type = substr($curr_values, $off + 1, 1);
3461 # Get the full operator variant.
3462 my $opv = $op . substr($curr_vars, $off, 1);
3464 # Ignore operators passed as parameters.
3465 if ($op_type ne 'V' &&
3466 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3469 # } elsif ($op =~ /^$;+$/) {
3471 # ; should have either the end of line or a space or \ after it
3472 } elsif ($op eq ';') {
3473 if ($ctx !~ /.x[WEBC]/ &&
3474 $cc !~ /^\\/ && $cc !~ /^;/) {
3475 if (ERROR("SPACING",
3476 "space required after that '$op' $at\n" . $hereptr)) {
3477 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3483 } elsif ($op eq '//') {
3485 # : when part of a bitfield
3486 } elsif ($opv eq ':B') {
3487 # skip the bitfield test for now
3491 } elsif ($op eq '->') {
3492 if ($ctx =~ /Wx.|.xW/) {
3493 if (ERROR("SPACING",
3494 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3495 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3496 if (defined $fix_elements[$n + 2]) {
3497 $fix_elements[$n + 2] =~ s/^\s+//;
3503 # , must have a space on the right.
3504 } elsif ($op eq ',') {
3505 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3506 if (ERROR("SPACING",
3507 "space required after that '$op' $at\n" . $hereptr)) {
3508 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3514 # '*' as part of a type definition -- reported already.
3515 } elsif ($opv eq '*_') {
3516 #warn "'*' is part of type\n";
3518 # unary operators should have a space before and
3519 # none after. May be left adjacent to another
3520 # unary operator, or a cast
3521 } elsif ($op eq '!' || $op eq '~' ||
3522 $opv eq '*U' || $opv eq '-U' ||
3523 $opv eq '&U' || $opv eq '&&U') {
3524 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3525 if (ERROR("SPACING",
3526 "space required before that '$op' $at\n" . $hereptr)) {
3527 if ($n != $last_after + 2) {
3528 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3533 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3534 # A unary '*' may be const
3536 } elsif ($ctx =~ /.xW/) {
3537 if (ERROR("SPACING",
3538 "space prohibited after that '$op' $at\n" . $hereptr)) {
3539 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
3540 if (defined $fix_elements[$n + 2]) {
3541 $fix_elements[$n + 2] =~ s/^\s+//;
3547 # unary ++ and unary -- are allowed no space on one side.
3548 } elsif ($op eq '++' or $op eq '--') {
3549 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3550 if (ERROR("SPACING",
3551 "space required one side of that '$op' $at\n" . $hereptr)) {
3552 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3556 if ($ctx =~ /Wx[BE]/ ||
3557 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3558 if (ERROR("SPACING",
3559 "space prohibited before that '$op' $at\n" . $hereptr)) {
3560 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3564 if ($ctx =~ /ExW/) {
3565 if (ERROR("SPACING",
3566 "space prohibited after that '$op' $at\n" . $hereptr)) {
3567 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3568 if (defined $fix_elements[$n + 2]) {
3569 $fix_elements[$n + 2] =~ s/^\s+//;
3575 # << and >> may either have or not have spaces both sides
3576 } elsif ($op eq '<<' or $op eq '>>' or
3577 $op eq '&' or $op eq '^' or $op eq '|' or
3578 $op eq '+' or $op eq '-' or
3579 $op eq '*' or $op eq '/' or
3582 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3583 if (ERROR("SPACING",
3584 "need consistent spacing around '$op' $at\n" . $hereptr)) {
3585 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3586 if (defined $fix_elements[$n + 2]) {
3587 $fix_elements[$n + 2] =~ s/^\s+//;
3593 # A colon needs no spaces before when it is
3594 # terminating a case value or a label.
3595 } elsif ($opv eq ':C' || $opv eq ':L') {
3596 if ($ctx =~ /Wx./) {
3597 if (ERROR("SPACING",
3598 "space prohibited before that '$op' $at\n" . $hereptr)) {
3599 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3604 # All the others need spaces both sides.
3605 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3608 # Ignore email addresses <foo@bar>
3610 $cc =~ /^\S+\@\S+>/) ||
3612 $ca =~ /<\S+\@\S+$/))
3617 # messages are ERROR, but ?: are CHK
3619 my $msg_type = \&ERROR;
3620 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3622 if (&{$msg_type}("SPACING",
3623 "spaces required around that '$op' $at\n" . $hereptr)) {
3624 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3625 if (defined $fix_elements[$n + 2]) {
3626 $fix_elements[$n + 2] =~ s/^\s+//;
3632 $off += length($elements[$n + 1]);
3634 ## print("n: <$n> GOOD: <$good>\n");
3636 $fixed_line = $fixed_line . $good;
3639 if (($#elements % 2) == 0) {
3640 $fixed_line = $fixed_line . $fix_elements[$#elements];
3643 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3644 $fixed[$fixlinenr] = $fixed_line;
3650 # check for whitespace before a non-naked semicolon
3651 if ($line =~ /^\+.*\S\s+;\s*$/) {
3653 "space prohibited before semicolon\n" . $herecurr) &&
3655 1 while $fixed[$fixlinenr] =~
3656 s/^(\+.*\S)\s+;/$1;/;
3660 # check for multiple assignments
3661 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3662 CHK("MULTIPLE_ASSIGNMENTS",
3663 "multiple assignments should be avoided\n" . $herecurr);
3666 ## # check for multiple declarations, allowing for a function declaration
3668 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3669 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3671 ## # Remove any bracketed sections to ensure we do not
3672 ## # falsly report the parameters of functions.
3674 ## while ($ln =~ s/\([^\(\)]*\)//g) {
3676 ## if ($ln =~ /,/) {
3677 ## WARN("MULTIPLE_DECLARATION",
3678 ## "declaring multiple variables together should be avoided\n" . $herecurr);
3682 #need space before brace following if, while, etc
3683 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3685 if (ERROR("SPACING",
3686 "space required before the open brace '{'\n" . $herecurr) &&
3688 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
3692 ## # check for blank lines before declarations
3693 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3694 ## $prevrawline =~ /^.\s*$/) {
3696 ## "No blank lines before declarations\n" . $hereprev);
3700 # closing brace should have a space following it when it has anything
3702 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3703 if (ERROR("SPACING",
3704 "space required after that close brace '}'\n" . $herecurr) &&
3706 $fixed[$fixlinenr] =~
3707 s/}((?!(?:,|;|\)))\S)/} $1/;
3711 # check spacing on square brackets
3712 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3713 if (ERROR("SPACING",
3714 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3716 $fixed[$fixlinenr] =~
3720 if ($line =~ /\s\]/) {
3721 if (ERROR("SPACING",
3722 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3724 $fixed[$fixlinenr] =~
3729 # check spacing on parentheses
3730 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3731 $line !~ /for\s*\(\s+;/) {
3732 if (ERROR("SPACING",
3733 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3735 $fixed[$fixlinenr] =~
3739 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3740 $line !~ /for\s*\(.*;\s+\)/ &&
3741 $line !~ /:\s+\)/) {
3742 if (ERROR("SPACING",
3743 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3745 print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3746 $fixed[$fixlinenr] =~
3751 # check unnecessary parentheses around addressof/dereference single $Lvals
3752 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3754 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3755 CHK("UNNECESSARY_PARENTHESES",
3756 "Unnecessary parentheses around $1\n" . $herecurr);
3759 #goto labels aren't indented, allow a single space however
3760 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3761 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3762 if (WARN("INDENTED_LABEL",
3763 "labels should not be indented\n" . $herecurr) &&
3765 $fixed[$fixlinenr] =~
3770 # return is not a function
3771 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3773 if ($^V && $^V ge 5.10.0 &&
3774 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3776 $value = deparenthesize($value);
3777 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3778 ERROR("RETURN_PARENTHESES",
3779 "return is not a function, parentheses are not required\n" . $herecurr);
3781 } elsif ($spacing !~ /\s+/) {
3783 "space required before the open parenthesis '('\n" . $herecurr);
3787 # unnecessary return in a void function
3788 # at end-of-function, with the previous line a single leading tab, then return;
3789 # and the line before that not a goto label target like "out:"
3790 if ($sline =~ /^[ \+]}\s*$/ &&
3791 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3793 $lines[$linenr - 3] =~ /^[ +]/ &&
3794 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
3796 "void function return statements are not generally useful\n" . $hereprev);
3799 # if statements using unnecessary parentheses - ie: if ((foo == bar))
3800 if ($^V && $^V ge 5.10.0 &&
3801 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3802 my $openparens = $1;
3803 my $count = $openparens =~ tr@\(@\(@;
3805 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3806 my $comp = $4; #Not $1 because of $LvalOrFunc
3807 $msg = " - maybe == should be = ?" if ($comp eq "==");
3808 WARN("UNNECESSARY_PARENTHESES",
3809 "Unnecessary parentheses$msg\n" . $herecurr);
3813 # Return of what appears to be an errno should normally be -'ve
3814 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3816 if ($name ne 'EOF' && $name ne 'ERROR') {
3817 WARN("USE_NEGATIVE_ERRNO",
3818 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3822 # Need a space before open parenthesis after if, while etc
3823 if ($line =~ /\b(if|while|for|switch)\(/) {
3824 if (ERROR("SPACING",
3825 "space required before the open parenthesis '('\n" . $herecurr) &&
3827 $fixed[$fixlinenr] =~
3828 s/\b(if|while|for|switch)\(/$1 \(/;
3832 # Check for illegal assignment in if conditional -- and check for trailing
3833 # statements after the conditional.
3834 if ($line =~ /do\s*(?!{)/) {
3835 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3836 ctx_statement_block($linenr, $realcnt, 0)
3837 if (!defined $stat);
3838 my ($stat_next) = ctx_statement_block($line_nr_next,
3839 $remain_next, $off_next);
3840 $stat_next =~ s/\n./\n /g;
3841 ##print "stat<$stat> stat_next<$stat_next>\n";
3843 if ($stat_next =~ /^\s*while\b/) {
3844 # If the statement carries leading newlines,
3845 # then count those as offsets.
3847 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3849 statement_rawlines($whitespace) - 1;
3851 $suppress_whiletrailers{$line_nr_next +
3855 if (!defined $suppress_whiletrailers{$linenr} &&
3856 defined($stat) && defined($cond) &&
3857 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3858 my ($s, $c) = ($stat, $cond);
3860 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3861 ERROR("ASSIGN_IN_IF",
3862 "do not use assignment in if condition\n" . $herecurr);
3865 # Find out what is on the end of the line after the
3867 substr($s, 0, length($c), '');
3869 $s =~ s/$;//g; # Remove any comments
3870 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3871 $c !~ /}\s*while\s*/)
3873 # Find out how long the conditional actually is.
3874 my @newlines = ($c =~ /\n/gs);
3875 my $cond_lines = 1 + $#newlines;
3878 $stat_real = raw_line($linenr, $cond_lines)
3879 . "\n" if ($cond_lines);
3880 if (defined($stat_real) && $cond_lines > 1) {
3881 $stat_real = "[...]\n$stat_real";
3884 ERROR("TRAILING_STATEMENTS",
3885 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3889 # Check for bitwise tests written as boolean
3901 WARN("HEXADECIMAL_BOOLEAN_TEST",
3902 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3905 # if and else should not have general statements after it
3906 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3908 $s =~ s/$;//g; # Remove any comments
3909 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3910 ERROR("TRAILING_STATEMENTS",
3911 "trailing statements should be on next line\n" . $herecurr);
3914 # if should not continue a brace
3915 if ($line =~ /}\s*if\b/) {
3916 ERROR("TRAILING_STATEMENTS",
3917 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
3920 # case and default should not have general statements after them
3921 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3923 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3927 ERROR("TRAILING_STATEMENTS",
3928 "trailing statements should be on next line\n" . $herecurr);
3931 # Check for }<nl>else {, these must be at the same
3932 # indent level to be relevant to each other.
3933 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3934 $previndent == $indent) {
3935 if (ERROR("ELSE_AFTER_BRACE",
3936 "else should follow close brace '}'\n" . $hereprev) &&
3937 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3938 fix_delete_line($fixlinenr - 1, $prevrawline);
3939 fix_delete_line($fixlinenr, $rawline);
3940 my $fixedline = $prevrawline;
3941 $fixedline =~ s/}\s*$//;
3942 if ($fixedline !~ /^\+\s*$/) {
3943 fix_insert_line($fixlinenr, $fixedline);
3945 $fixedline = $rawline;
3946 $fixedline =~ s/^(.\s*)else/$1} else/;
3947 fix_insert_line($fixlinenr, $fixedline);
3951 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3952 $previndent == $indent) {
3953 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3955 # Find out what is on the end of the line after the
3957 substr($s, 0, length($c), '');
3960 if ($s =~ /^\s*;/) {
3961 if (ERROR("WHILE_AFTER_BRACE",
3962 "while should follow close brace '}'\n" . $hereprev) &&
3963 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3964 fix_delete_line($fixlinenr - 1, $prevrawline);
3965 fix_delete_line($fixlinenr, $rawline);
3966 my $fixedline = $prevrawline;
3967 my $trailing = $rawline;
3968 $trailing =~ s/^\+//;
3969 $trailing = trim($trailing);
3970 $fixedline =~ s/}\s*$/} $trailing/;
3971 fix_insert_line($fixlinenr, $fixedline);
3976 #Specific variable tests
3977 while ($line =~ m{($Constant|$Lval)}g) {
3980 #gcc binary extension
3981 if ($var =~ /^$Binary$/) {
3982 if (WARN("GCC_BINARY_CONSTANT",
3983 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3985 my $hexval = sprintf("0x%x", oct($var));
3986 $fixed[$fixlinenr] =~
3987 s/\b$var\b/$hexval/;
3992 if ($var !~ /^$Constant$/ &&
3993 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3994 #Ignore Page<foo> variants
3995 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3996 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3997 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3998 while ($var =~ m{($Ident)}g) {
4000 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4002 seed_camelcase_includes();
4003 if (!$file && !$camelcase_file_seeded) {
4004 seed_camelcase_file($realfile);
4005 $camelcase_file_seeded = 1;
4008 if (!defined $camelcase{$word}) {
4009 $camelcase{$word} = 1;
4011 "Avoid CamelCase: <$word>\n" . $herecurr);
4017 #no spaces allowed after \ in define
4018 if ($line =~ /\#\s*define.*\\\s+$/) {
4019 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4020 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4022 $fixed[$fixlinenr] =~ s/\s+$//;
4026 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4027 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4029 my $checkfile = "include/linux/$file";
4030 if (-f "$root/$checkfile" &&
4031 $realfile ne $checkfile &&
4032 $1 !~ /$allowed_asm_includes/)
4034 if ($realfile =~ m{^arch/}) {
4035 CHK("ARCH_INCLUDE_LINUX",
4036 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4038 WARN("INCLUDE_LINUX",
4039 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4044 # multi-statement macros should be enclosed in a do while loop, grab the
4045 # first statement and ensure its the whole macro if its not enclosed
4046 # in a known good container
4047 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4048 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4051 my ($off, $dstat, $dcond, $rest);
4053 ($dstat, $dcond, $ln, $cnt, $off) =
4054 ctx_statement_block($linenr, $realcnt, 0);
4056 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4057 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4059 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4061 $dstat =~ s/\\\n.//g;
4062 $dstat =~ s/^\s*//s;
4063 $dstat =~ s/\s*$//s;
4065 # Flatten any parentheses and braces
4066 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4067 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4068 $dstat =~ s/\[[^\[\]]*\]/1/)
4072 # Flatten any obvious string concatentation.
4073 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4074 $dstat =~ s/$Ident\s*("X*")/$1/)
4078 my $exceptions = qr{
4090 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4092 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4093 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4094 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4095 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4096 $dstat !~ /$exceptions/ &&
4097 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4098 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4099 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4100 $dstat !~ /^for\s*$Constant$/ && # for (...)
4101 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4102 $dstat !~ /^do\s*{/ && # do {...
4103 $dstat !~ /^\({/ && # ({...
4104 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4107 my $herectx = $here . "\n";
4108 my $cnt = statement_rawlines($ctx);
4110 for (my $n = 0; $n < $cnt; $n++) {
4111 $herectx .= raw_line($linenr, $n) . "\n";
4114 if ($dstat =~ /;/) {
4115 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4116 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4118 ERROR("COMPLEX_MACRO",
4119 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
4123 # check for line continuations outside of #defines, preprocessor #, and asm
4126 if ($prevline !~ /^..*\\$/ &&
4127 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4128 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4129 $line =~ /^\+.*\\$/) {
4130 WARN("LINE_CONTINUATIONS",
4131 "Avoid unnecessary line continuations\n" . $herecurr);
4135 # do {} while (0) macro tests:
4136 # single-statement macros do not need to be enclosed in do while (0) loop,
4137 # macro should not end with a semicolon
4138 if ($^V && $^V ge 5.10.0 &&
4139 $realfile !~ m@/vmlinux.lds.h$@ &&
4140 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4143 my ($off, $dstat, $dcond, $rest);
4145 ($dstat, $dcond, $ln, $cnt, $off) =
4146 ctx_statement_block($linenr, $realcnt, 0);
4149 $dstat =~ s/\\\n.//g;
4151 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4156 my $cnt = statement_rawlines($ctx);
4157 my $herectx = $here . "\n";
4159 for (my $n = 0; $n < $cnt; $n++) {
4160 $herectx .= raw_line($linenr, $n) . "\n";
4163 if (($stmts =~ tr/;/;/) == 1 &&
4164 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4165 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4166 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4168 if (defined $semis && $semis ne "") {
4169 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4170 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4172 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4174 my $cnt = statement_rawlines($ctx);
4175 my $herectx = $here . "\n";
4177 for (my $n = 0; $n < $cnt; $n++) {
4178 $herectx .= raw_line($linenr, $n) . "\n";
4181 WARN("TRAILING_SEMICOLON",
4182 "macros should not use a trailing semicolon\n" . "$herectx");
4186 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4187 # all assignments may have only one of the following with an assignment:
4190 # VMLINUX_SYMBOL(...)
4191 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4192 WARN("MISSING_VMLINUX_SYMBOL",
4193 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4196 # check for redundant bracing round if etc
4197 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4198 my ($level, $endln, @chunks) =
4199 ctx_statement_full($linenr, $realcnt, 1);
4200 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4201 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4202 if ($#chunks > 0 && $level == 0) {
4206 my $herectx = $here . "\n";
4207 my $ln = $linenr - 1;
4208 for my $chunk (@chunks) {
4209 my ($cond, $block) = @{$chunk};
4211 # If the condition carries leading newlines, then count those as offsets.
4212 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4213 my $offset = statement_rawlines($whitespace) - 1;
4215 $allowed[$allow] = 0;
4216 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4218 # We have looked at and allowed this specific line.
4219 $suppress_ifbraces{$ln + $offset} = 1;
4221 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4222 $ln += statement_rawlines($block) - 1;
4224 substr($block, 0, length($cond), '');
4226 $seen++ if ($block =~ /^\s*{/);
4228 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4229 if (statement_lines($cond) > 1) {
4230 #print "APW: ALLOWED: cond<$cond>\n";
4231 $allowed[$allow] = 1;
4233 if ($block =~/\b(?:if|for|while)\b/) {
4234 #print "APW: ALLOWED: block<$block>\n";
4235 $allowed[$allow] = 1;
4237 if (statement_block_size($block) > 1) {
4238 #print "APW: ALLOWED: lines block<$block>\n";
4239 $allowed[$allow] = 1;
4244 my $sum_allowed = 0;
4245 foreach (@allowed) {
4248 if ($sum_allowed == 0) {
4250 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4251 } elsif ($sum_allowed != $allow &&
4254 "braces {} should be used on all arms of this statement\n" . $herectx);
4259 if (!defined $suppress_ifbraces{$linenr - 1} &&
4260 $line =~ /\b(if|while|for|else)\b/) {
4263 # Check the pre-context.
4264 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4265 #print "APW: ALLOWED: pre<$1>\n";
4269 my ($level, $endln, @chunks) =
4270 ctx_statement_full($linenr, $realcnt, $-[0]);
4272 # Check the condition.
4273 my ($cond, $block) = @{$chunks[0]};
4274 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4275 if (defined $cond) {
4276 substr($block, 0, length($cond), '');
4278 if (statement_lines($cond) > 1) {
4279 #print "APW: ALLOWED: cond<$cond>\n";
4282 if ($block =~/\b(?:if|for|while)\b/) {
4283 #print "APW: ALLOWED: block<$block>\n";
4286 if (statement_block_size($block) > 1) {
4287 #print "APW: ALLOWED: lines block<$block>\n";
4290 # Check the post-context.
4291 if (defined $chunks[1]) {
4292 my ($cond, $block) = @{$chunks[1]};
4293 if (defined $cond) {
4294 substr($block, 0, length($cond), '');
4296 if ($block =~ /^\s*\{/) {
4297 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4301 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
4302 my $herectx = $here . "\n";
4303 my $cnt = statement_rawlines($block);
4305 for (my $n = 0; $n < $cnt; $n++) {
4306 $herectx .= raw_line($linenr, $n) . "\n";
4310 "braces {} are not necessary for single statement blocks\n" . $herectx);
4314 # check for unnecessary blank lines around braces
4315 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4317 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4319 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4321 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4324 # no volatiles please
4325 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4326 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4328 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4332 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4333 CHK("REDUNDANT_CODE",
4334 "if this code is redundant consider removing it\n" .
4338 # check for needless "if (<foo>) fn(<foo>)" uses
4339 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4340 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4341 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4343 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
4347 # check for unnecessary "Out of Memory" messages
4348 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4349 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4350 (defined $1 || defined $3) &&
4353 my $testline = $lines[$linenr - 3];
4355 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4356 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4358 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4360 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4364 # check for bad placement of section $InitAttribute (e.g.: __initdata)
4365 if ($line =~ /(\b$InitAttribute\b)/) {
4367 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4370 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4371 ERROR("MISPLACED_INIT",
4372 "$attr should be placed after $var\n" . $herecurr)) ||
4373 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4374 WARN("MISPLACED_INIT",
4375 "$attr should be placed after $var\n" . $herecurr))) &&
4377 $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;
4382 # check for $InitAttributeData (ie: __initdata) with const
4383 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4385 $attr =~ /($InitAttributePrefix)(.*)/;
4386 my $attr_prefix = $1;
4388 if (ERROR("INIT_ATTRIBUTE",
4389 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4391 $fixed[$fixlinenr] =~
4392 s/$InitAttributeData/${attr_prefix}initconst/;
4396 # check for $InitAttributeConst (ie: __initconst) without const
4397 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4399 if (ERROR("INIT_ATTRIBUTE",
4400 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4402 my $lead = $fixed[$fixlinenr] =~
4403 /(^\+\s*(?:static\s+))/;
4405 $lead = "$lead " if ($lead !~ /^\+$/);
4406 $lead = "${lead}const ";
4407 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4411 # don't use __constant_<foo> functions outside of include/uapi/
4412 if ($realfile !~ m@^include/uapi/@ &&
4413 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4414 my $constant_func = $1;
4415 my $func = $constant_func;
4416 $func =~ s/^__constant_//;
4417 if (WARN("CONSTANT_CONVERSION",
4418 "$constant_func should be $func\n" . $herecurr) &&
4420 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4424 # prefer usleep_range over udelay
4425 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
4427 # ignore udelay's < 10, however
4428 if (! ($delay < 10) ) {
4430 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4432 if ($delay > 2000) {
4434 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
4438 # warn about unexpectedly long msleep's
4439 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4442 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4446 # check for comparisons of jiffies
4447 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4448 WARN("JIFFIES_COMPARISON",
4449 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4452 # check for comparisons of get_jiffies_64()
4453 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4454 WARN("JIFFIES_COMPARISON",
4455 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4458 # warn about #ifdefs in C files
4459 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
4460 # print "#ifdef in C files should be avoided\n";
4461 # print "$herecurr";
4465 # warn about spacing in #ifdefs
4466 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
4467 if (ERROR("SPACING",
4468 "exactly one space required after that #$1\n" . $herecurr) &&
4470 $fixed[$fixlinenr] =~
4471 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4476 # check for spinlock_t definitions without a comment.
4477 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4478 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4480 if (!ctx_has_comment($first_line, $linenr)) {
4481 CHK("UNCOMMENTED_DEFINITION",
4482 "$1 definition without comment\n" . $herecurr);
4485 # check for memory barriers without a comment.
4486 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4487 if (!ctx_has_comment($first_line, $linenr)) {
4488 WARN("MEMORY_BARRIER",
4489 "memory barrier without comment\n" . $herecurr);
4492 # check of hardware specific defines
4493 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4495 "architecture specific defines should be avoided\n" . $herecurr);
4498 # Check that the storage class is at the beginning of a declaration
4499 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4500 WARN("STORAGE_CLASS",
4501 "storage class should be at the beginning of the declaration\n" . $herecurr)
4504 # check the location of the inline attribute, that it is between
4505 # storage class and type.
4506 if ($line =~ /\b$Type\s+$Inline\b/ ||
4507 $line =~ /\b$Inline\s+$Storage\b/) {
4508 ERROR("INLINE_LOCATION",
4509 "inline keyword should sit between storage class and type\n" . $herecurr);
4512 # Check for __inline__ and __inline, prefer inline
4513 if ($realfile !~ m@\binclude/uapi/@ &&
4514 $line =~ /\b(__inline__|__inline)\b/) {
4516 "plain inline is preferred over $1\n" . $herecurr) &&
4518 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4523 # Check for __attribute__ packed, prefer __packed
4524 if ($realfile !~ m@\binclude/uapi/@ &&
4525 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4526 WARN("PREFER_PACKED",
4527 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
4530 # Check for __attribute__ aligned, prefer __aligned
4531 if ($realfile !~ m@\binclude/uapi/@ &&
4532 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4533 WARN("PREFER_ALIGNED",
4534 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
4537 # Check for __attribute__ format(printf, prefer __printf
4538 if ($realfile !~ m@\binclude/uapi/@ &&
4539 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4540 if (WARN("PREFER_PRINTF",
4541 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4543 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4548 # Check for __attribute__ format(scanf, prefer __scanf
4549 if ($realfile !~ m@\binclude/uapi/@ &&
4550 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4551 if (WARN("PREFER_SCANF",
4552 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4554 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4558 # check for sizeof(&)
4559 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4560 WARN("SIZEOF_ADDRESS",
4561 "sizeof(& should be avoided\n" . $herecurr);
4564 # check for sizeof without parenthesis
4565 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4566 if (WARN("SIZEOF_PARENTHESIS",
4567 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4569 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4573 # check for line continuations in quoted strings with odd counts of "
4574 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4575 WARN("LINE_CONTINUATIONS",
4576 "Avoid line continuations in quoted strings\n" . $herecurr);
4579 # check for struct spinlock declarations
4580 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4581 WARN("USE_SPINLOCK_T",
4582 "struct spinlock should be spinlock_t\n" . $herecurr);
4585 # check for seq_printf uses that could be seq_puts
4586 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4587 my $fmt = get_quoted_string($line, $rawline);
4588 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4589 if (WARN("PREFER_SEQ_PUTS",
4590 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4592 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4597 # Check for misused memsets
4598 if ($^V && $^V ge 5.10.0 &&
4600 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4606 if ($ms_size =~ /^(0x|)0$/i) {
4608 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4609 } elsif ($ms_size =~ /^(0x|)1$/i) {
4611 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4615 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4616 if ($^V && $^V ge 5.10.0 &&
4617 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4618 if (WARN("PREFER_ETHER_ADDR_COPY",
4619 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4621 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
4625 # typecasts on min/max could be min_t/max_t
4626 if ($^V && $^V ge 5.10.0 &&
4628 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4629 if (defined $2 || defined $7) {
4631 my $cast1 = deparenthesize($2);
4633 my $cast2 = deparenthesize($7);
4637 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4638 $cast = "$cast1 or $cast2";
4639 } elsif ($cast1 ne "") {
4645 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4649 # check usleep_range arguments
4650 if ($^V && $^V ge 5.10.0 &&
4652 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4656 WARN("USLEEP_RANGE",
4657 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4658 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4660 WARN("USLEEP_RANGE",
4661 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4665 # check for naked sscanf
4666 if ($^V && $^V ge 5.10.0 &&
4668 $line =~ /\bsscanf\b/ &&
4669 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4670 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4671 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4672 my $lc = $stat =~ tr@\n@@;
4673 $lc = $lc + $linenr;
4674 my $stat_real = raw_line($linenr, 0);
4675 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4676 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4678 WARN("NAKED_SSCANF",
4679 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4682 # check for simple sscanf that should be kstrto<foo>
4683 if ($^V && $^V ge 5.10.0 &&
4685 $line =~ /\bsscanf\b/) {
4686 my $lc = $stat =~ tr@\n@@;
4687 $lc = $lc + $linenr;
4688 my $stat_real = raw_line($linenr, 0);
4689 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4690 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4692 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4694 my $count = $format =~ tr@%@%@;
4696 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4697 WARN("SSCANF_TO_KSTRTO",
4698 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4703 # check for new externs in .h files.
4704 if ($realfile =~ /\.h$/ &&
4705 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4706 if (CHK("AVOID_EXTERNS",
4707 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
4709 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4713 # check for new externs in .c files.
4714 if ($realfile =~ /\.c$/ && defined $stat &&
4715 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4717 my $function_name = $1;
4718 my $paren_space = $2;
4721 if (defined $cond) {
4722 substr($s, 0, length($cond), '');
4724 if ($s =~ /^\s*;/ &&
4725 $function_name ne 'uninitialized_var')
4727 WARN("AVOID_EXTERNS",
4728 "externs should be avoided in .c files\n" . $herecurr);
4731 if ($paren_space =~ /\n/) {
4732 WARN("FUNCTION_ARGUMENTS",
4733 "arguments for function declarations should follow identifier\n" . $herecurr);
4736 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4737 $stat =~ /^.\s*extern\s+/)
4739 WARN("AVOID_EXTERNS",
4740 "externs should be avoided in .c files\n" . $herecurr);
4743 # checks for new __setup's
4744 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4747 if (!grep(/$name/, @setup_docs)) {
4748 CHK("UNDOCUMENTED_SETUP",
4749 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4753 # check for pointless casting of kmalloc return
4754 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4755 WARN("UNNECESSARY_CASTS",
4756 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
4760 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4761 if ($^V && $^V ge 5.10.0 &&
4762 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4763 CHK("ALLOC_SIZEOF_STRUCT",
4764 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4767 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4768 if ($^V && $^V ge 5.10.0 &&
4769 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
4773 my $newfunc = "kmalloc_array";
4774 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
4777 if ($a1 =~ /^sizeof\s*\S/) {
4781 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4782 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
4783 if (WARN("ALLOC_WITH_MULTIPLY",
4784 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4786 $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;
4792 # check for krealloc arg reuse
4793 if ($^V && $^V ge 5.10.0 &&
4794 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4795 WARN("KREALLOC_ARG_REUSE",
4796 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4799 # check for alloc argument mismatch
4800 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4801 WARN("ALLOC_ARRAY_ARGS",
4802 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4805 # check for multiple semicolons
4806 if ($line =~ /;\s*;\s*$/) {
4807 if (WARN("ONE_SEMICOLON",
4808 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4810 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
4814 # check for case / default statements not preceded by break/fallthrough/switch
4815 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4817 my $has_statement = 0;
4819 my $prevline = $linenr;
4820 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
4822 my $rline = $rawlines[$prevline - 1];
4823 my $fline = $lines[$prevline - 1];
4824 last if ($fline =~ /^\@\@/);
4825 next if ($fline =~ /^\-/);
4826 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4827 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4828 next if ($fline =~ /^.[\s$;]*$/);
4831 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4833 if (!$has_break && $has_statement) {
4834 WARN("MISSING_BREAK",
4835 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4839 # check for switch/default statements without a break;
4840 if ($^V && $^V ge 5.10.0 &&
4842 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4844 my $herectx = $here . "\n";
4845 my $cnt = statement_rawlines($stat);
4846 for (my $n = 0; $n < $cnt; $n++) {
4847 $herectx .= raw_line($linenr, $n) . "\n";
4849 WARN("DEFAULT_NO_BREAK",
4850 "switch default: should use break\n" . $herectx);
4853 # check for gcc specific __FUNCTION__
4854 if ($line =~ /\b__FUNCTION__\b/) {
4855 if (WARN("USE_FUNC",
4856 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4858 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
4862 # check for use of yield()
4863 if ($line =~ /\byield\s*\(\s*\)/) {
4865 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4868 # check for comparisons against true and false
4869 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4877 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4879 my $type = lc($otype);
4880 if ($type =~ /^(?:true|false)$/) {
4881 if (("$test" eq "==" && "$type" eq "true") ||
4882 ("$test" eq "!=" && "$type" eq "false")) {
4886 CHK("BOOL_COMPARISON",
4887 "Using comparison to $otype is error prone\n" . $herecurr);
4889 ## maybe suggesting a correct construct would better
4890 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4895 # check for semaphores initialized locked
4896 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
4897 WARN("CONSIDER_COMPLETION",
4898 "consider using a completion\n" . $herecurr);
4901 # recommend kstrto* over simple_strto* and strict_strto*
4902 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
4903 WARN("CONSIDER_KSTRTO",
4904 "$1 is obsolete, use k$3 instead\n" . $herecurr);
4907 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
4908 if ($line =~ /^.\s*__initcall\s*\(/) {
4909 WARN("USE_DEVICE_INITCALL",
4910 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
4913 # check for various ops structs, ensure they are const.
4914 my $struct_ops = qr{acpi_dock_ops|
4915 address_space_operations|
4917 block_device_operations|
4922 file_lock_operations|
4932 lock_manager_operations|
4938 pipe_buf_operations|
4939 platform_hibernation_ops|
4940 platform_suspend_ops|
4945 soc_pcmcia_socket_ops|
4951 if ($line !~ /\bconst\b/ &&
4952 $line =~ /\bstruct\s+($struct_ops)\b/) {
4953 WARN("CONST_STRUCT",
4954 "struct $1 should normally be const\n" .
4958 # use of NR_CPUS is usually wrong
4959 # ignore definitions of NR_CPUS and usage to define arrays as likely right
4960 if ($line =~ /\bNR_CPUS\b/ &&
4961 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4962 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
4963 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4964 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4965 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
4968 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4971 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4972 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4973 ERROR("DEFINE_ARCH_HAS",
4974 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4977 # check for %L{u,d,i} in strings
4979 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4980 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4981 $string =~ s/%%/__/g;
4982 if ($string =~ /(?<!%)%L[udi]/) {
4984 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4989 # whine mightly about in_atomic
4990 if ($line =~ /\bin_atomic\s*\(/) {
4991 if ($realfile =~ m@^drivers/@) {
4993 "do not use in_atomic in drivers\n" . $herecurr);
4994 } elsif ($realfile !~ m@^kernel/@) {
4996 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5000 # check for lockdep_set_novalidate_class
5001 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5002 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5003 if ($realfile !~ m@^kernel/lockdep@ &&
5004 $realfile !~ m@^include/linux/lockdep@ &&
5005 $realfile !~ m@^drivers/base/core@) {
5007 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
5011 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5012 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5013 WARN("EXPORTED_WORLD_WRITABLE",
5014 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5017 # Mode permission misuses where it seems decimal should be octal
5018 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5019 if ($^V && $^V ge 5.10.0 &&
5020 $line =~ /$mode_perms_search/) {
5021 foreach my $entry (@mode_permission_funcs) {
5022 my $func = $entry->[0];
5023 my $arg_pos = $entry->[1];
5028 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5030 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5031 if ($line =~ /$test/) {
5033 $val = $6 if ($skip_args ne "");
5035 if ($val !~ /^0$/ &&
5036 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5037 length($val) ne 4)) {
5038 ERROR("NON_OCTAL_PERMISSIONS",
5039 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5046 # If we have no input at all, then there is nothing to report on
5047 # so just keep quiet.
5048 if ($#rawlines == -1) {
5052 # In mailback mode only produce a report in the negative, for
5053 # things that appear to be patches.
5054 if ($mailback && ($clean == 1 || !$is_patch)) {
5058 # This is not a patch, and we are are in 'no-patch' mode so
5060 if (!$chk_patch && !$is_patch) {
5065 ERROR("NOT_UNIFIED_DIFF",
5066 "Does not appear to be a unified-diff format patch\n");
5068 if ($is_patch && $chk_signoff && $signoff == 0) {
5069 ERROR("MISSING_SIGN_OFF",
5070 "Missing Signed-off-by: line(s)\n");
5073 print report_dump();
5074 if ($summary && !($clean == 1 && $quiet == 1)) {
5075 print "$filename " if ($summary_file);
5076 print "total: $cnt_error errors, $cnt_warn warnings, " .
5077 (($check)? "$cnt_chk checks, " : "") .
5078 "$cnt_lines lines checked\n";
5079 print "\n" if ($quiet == 0);
5084 if ($^V lt 5.10.0) {
5085 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5086 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5089 # If there were whitespace errors which cleanpatch can fix
5090 # then suggest that.
5091 if ($rpt_cleaners) {
5092 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5093 print " scripts/cleanfile\n\n";
5098 hash_show_words(\%use_type, "Used");
5099 hash_show_words(\%ignore_type, "Ignored");
5101 if ($clean == 0 && $fix &&
5102 ("@rawlines" ne "@fixed" ||
5103 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
5104 my $newfile = $filename;
5105 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
5109 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5111 open($f, '>', $newfile)
5112 or die "$P: Can't open $newfile for write\n";
5113 foreach my $fixed_line (@fixed) {
5116 if ($linecount > 3) {
5117 $fixed_line =~ s/^\+//;
5118 print $f $fixed_line . "\n";
5121 print $f $fixed_line . "\n";
5128 Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5130 Do _NOT_ trust the results written to this file.
5131 Do _NOT_ submit these changes without inspecting them for correctness.
5133 This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5134 No warranties, expressed or implied...
5140 if ($clean == 1 && $quiet == 0) {
5141 print "$vname has no obvious style problems and is ready for submission.\n"
5143 if ($clean == 0 && $quiet == 0) {
5145 $vname has style problems, please review.
5147 If any of these errors are false positives, please report
5148 them to the maintainer, see CHECKPATCH in MAINTAINERS.