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
8 # Modifications for Akaros:
9 # Copyright (c) 2015 Google Inc
10 # Barret Rhoden <brho@cs.berkeley.edu>
12 # - Added a tab_length parameter, set it to 4
13 # - Set tree = 0, since we do not have a Linux kernel tree
14 # - No KERN_ checks for printk
15 # - ENOSYS can be used in more places
16 # - Block comments do not need to end on a trailing line
17 # - Can use spaces for aligning (more than tab_length spaces) and thus also at
18 # the beginning of a line
19 # - Can indent cases once more than its switch
20 # - Casting pointers can be (struct foo*) or (struct foo *)
21 # - Don't prod about updating MAINTAINERS
22 # - Allow C99 comments
28 use Term::ANSIColor qw(:constants);
31 my $D = dirname(abs_path($P));
35 use Getopt::Long qw(:config no_auto_abbrev);
62 my $configuration_file = ".checkpatch.conf";
63 my $max_line_length = 80;
64 my $ignore_perl_version = 0;
65 my $minimum_perl_version = 5.10.0;
66 my $min_conf_desc_length = 4;
67 my $spelling_file = "$D/spelling.txt";
69 my $codespellfile = "/usr/share/codespell/dictionary.txt";
77 Usage: $P [OPTION]... [FILE]...
82 --no-tree run without a kernel tree
83 --no-signoff do not check for 'Signed-off-by' line
84 --patch treat FILE as patchfile (default)
85 --emacs emacs compile window format
86 --terse one line per report
87 --showfile emit diffed file position, not input file position
88 -f, --file treat FILE as regular source file
89 --subjective, --strict enable more subjective tests
90 --types TYPE(,TYPE2...) show only these comma separated message types
91 --ignore TYPE(,TYPE2...) ignore various comma separated message types
92 --max-line-length=n set the maximum line length, if exceeded, warn
93 --min-conf-desc-length=n set the min description length, if shorter, warn
94 --show-types show the message "types" in the output
95 --root=PATH PATH to the kernel tree root
96 --no-summary suppress the per-file summary
97 --mailback only produce a report in case of warnings/errors
98 --summary-file include the filename in summary
99 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
100 'values', 'possible', 'type', and 'attr' (default
102 --test-only=WORD report only warnings/errors containing WORD
104 --fix EXPERIMENTAL - may create horrible results
105 If correctable single-line errors exist, create
106 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
107 with potential errors corrected to the preferred
109 --fix-inplace EXPERIMENTAL - may create horrible results
110 Is the same as --fix, but overwrites the input
111 file. It's your fault if there's no backup or git
112 --ignore-perl-version override checking of perl version. expect
114 --codespell Use the codespell dictionary for spelling/typos
115 (default:/usr/share/codespell/dictionary.txt)
116 --codespellfile Use this codespell dictionary
117 --color Use colors when output is STDOUT (default: on)
118 -h, --help, --version display this help and exit
120 When FILE is - read standard input.
126 my $conf = which_conf($configuration_file);
129 open(my $conffile, '<', "$conf")
130 or warn "$P: Can't find a readable $configuration_file file $!\n";
132 while (<$conffile>) {
135 $line =~ s/\s*\n?$//g;
139 next if ($line =~ m/^\s*#/);
140 next if ($line =~ m/^\s*$/);
142 my @words = split(" ", $line);
143 foreach my $word (@words) {
144 last if ($word =~ m/^#/);
145 push (@conf_args, $word);
149 unshift(@ARGV, @conf_args) if @conf_args;
153 'q|quiet+' => \$quiet,
155 'signoff!' => \$chk_signoff,
156 'patch!' => \$chk_patch,
159 'showfile!' => \$showfile,
161 'subjective!' => \$check,
162 'strict!' => \$check,
163 'ignore=s' => \@ignore,
165 'show-types!' => \$show_types,
166 'max-line-length=i' => \$max_line_length,
167 'min-conf-desc-length=i' => \$min_conf_desc_length,
169 'summary!' => \$summary,
170 'mailback!' => \$mailback,
171 'summary-file!' => \$summary_file,
173 'fix-inplace!' => \$fix_inplace,
174 'ignore-perl-version!' => \$ignore_perl_version,
175 'debug=s' => \%debug,
176 'test-only=s' => \$tst_only,
177 'codespell!' => \$codespell,
178 'codespellfile=s' => \$codespellfile,
186 $fix = 1 if ($fix_inplace);
187 $check_orig = $check;
191 if ($^V && $^V lt $minimum_perl_version) {
192 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
193 if (!$ignore_perl_version) {
199 print "$P: no input files\n";
203 sub hash_save_array_words {
204 my ($hashRef, $arrayRef) = @_;
206 my @array = split(/,/, join(',', @$arrayRef));
207 foreach my $word (@array) {
208 $word =~ s/\s*\n?$//g;
211 $word =~ tr/[a-z]/[A-Z]/;
213 next if ($word =~ m/^\s*#/);
214 next if ($word =~ m/^\s*$/);
220 sub hash_show_words {
221 my ($hashRef, $prefix) = @_;
223 if (keys %$hashRef) {
224 print "\nNOTE: $prefix message types:";
225 foreach my $word (sort keys %$hashRef) {
232 hash_save_array_words(\%ignore_type, \@ignore);
233 hash_save_array_words(\%use_type, \@use);
236 my $dbg_possible = 0;
239 for my $key (keys %debug) {
241 eval "\${dbg_$key} = '$debug{$key}';";
245 my $rpt_cleaners = 0;
254 if (!top_of_kernel_tree($root)) {
255 die "$P: $root: --root does not point at a valid tree\n";
258 if (top_of_kernel_tree('.')) {
260 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
261 top_of_kernel_tree($1)) {
266 if (!defined $root) {
267 print "Must be run from the top-level dir. of a kernel tree\n";
272 my $emitted_corrupt = 0;
275 [A-Za-z_][A-Za-z\d_]*
276 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
278 our $Storage = qr{extern|static|asmlinkage};
291 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
292 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
293 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
294 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
295 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
297 # Notes to $Attribute:
298 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
319 ____cacheline_aligned|
320 ____cacheline_aligned_in_smp|
321 ____cacheline_internodealigned_in_smp|
325 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
326 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
327 our $Lval = qr{$Ident(?:$Member)*};
329 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
330 our $Binary = qr{(?i)0b[01]+$Int_type?};
331 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
332 our $Int = qr{[0-9]+$Int_type?};
333 our $Octal = qr{0[0-7]+$Int_type?};
334 our $String = qr{"[X\t]*"};
335 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
336 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
337 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
338 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
339 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
340 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
341 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
342 our $Arithmetic = qr{\+|-|\*|\/|%};
346 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
349 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
353 our $NonptrTypeMisordered;
354 our $NonptrTypeWithAttr;
358 our $DeclareMisordered;
360 our $NON_ASCII_UTF8 = qr{
361 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
362 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
363 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
364 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
365 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
366 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
367 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
371 [\x09\x0A\x0D\x20-\x7E] # ASCII
375 our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
376 our $typeOtherOSTypedefs = qr{(?x:
377 u_(?:char|short|int|long) | # bsd
378 u(?:nchar|short|int|long) # sysv
380 our $typeKernelTypedefs = qr{(?x:
381 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
384 our $typeTypedefs = qr{(?x:
386 $typeOtherOSTypedefs\b|
387 $typeKernelTypedefs\b
390 our $logFunctions = qr{(?x:
391 printk(?:_ratelimited|_once|)|
392 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
393 WARN(?:_RATELIMIT|_ONCE|)|
396 seq_vprintf|seq_printf|seq_puts
399 our $signature_tags = qr{(?xi:
410 our @typeListMisordered = (
411 qr{char\s+(?:un)?signed},
412 qr{int\s+(?:(?:un)?signed\s+)?short\s},
413 qr{int\s+short(?:\s+(?:un)?signed)},
414 qr{short\s+int(?:\s+(?:un)?signed)},
415 qr{(?:un)?signed\s+int\s+short},
416 qr{short\s+(?:un)?signed},
417 qr{long\s+int\s+(?:un)?signed},
418 qr{int\s+long\s+(?:un)?signed},
419 qr{long\s+(?:un)?signed\s+int},
420 qr{int\s+(?:un)?signed\s+long},
421 qr{int\s+(?:un)?signed},
422 qr{int\s+long\s+long\s+(?:un)?signed},
423 qr{long\s+long\s+int\s+(?:un)?signed},
424 qr{long\s+long\s+(?:un)?signed\s+int},
425 qr{long\s+long\s+(?:un)?signed},
426 qr{long\s+(?:un)?signed},
431 qr{(?:(?:un)?signed\s+)?char},
432 qr{(?:(?:un)?signed\s+)?short\s+int},
433 qr{(?:(?:un)?signed\s+)?short},
434 qr{(?:(?:un)?signed\s+)?int},
435 qr{(?:(?:un)?signed\s+)?long\s+int},
436 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
437 qr{(?:(?:un)?signed\s+)?long\s+long},
438 qr{(?:(?:un)?signed\s+)?long},
447 qr{${Ident}_handler},
448 qr{${Ident}_handler_fn},
451 our @typeListFile = ();
452 our @typeListWithAttr = (
454 qr{struct\s+$InitAttribute\s+$Ident},
455 qr{union\s+$InitAttribute\s+$Ident},
458 our @modifierList = (
461 our @modifierListFile = ();
463 our @mode_permission_funcs = (
465 ["module_param_(?:array|named|string)", 4],
466 ["module_param_array_named", 5],
467 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
468 ["proc_create(?:_data|)", 2],
469 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
472 #Create a search pattern for all these functions to speed up a loop below
473 our $mode_perms_search = "";
474 foreach my $entry (@mode_permission_funcs) {
475 $mode_perms_search .= '|' if ($mode_perms_search ne "");
476 $mode_perms_search .= $entry->[0];
479 our $mode_perms_world_writable = qr{
487 our $allowed_asm_includes = qr{(?x:
493 # memory.h: ARM has a custom one
495 # Load common spelling mistakes and build regular expression list.
499 if (open(my $spelling, '<', $spelling_file)) {
500 while (<$spelling>) {
503 $line =~ s/\s*\n?$//g;
506 next if ($line =~ m/^\s*#/);
507 next if ($line =~ m/^\s*$/);
509 my ($suspect, $fix) = split(/\|\|/, $line);
511 $spelling_fix{$suspect} = $fix;
515 warn "No typos will be found - file '$spelling_file': $!\n";
519 if (open(my $spelling, '<', $codespellfile)) {
520 while (<$spelling>) {
523 $line =~ s/\s*\n?$//g;
526 next if ($line =~ m/^\s*#/);
527 next if ($line =~ m/^\s*$/);
528 next if ($line =~ m/, disabled/i);
532 my ($suspect, $fix) = split(/->/, $line);
534 $spelling_fix{$suspect} = $fix;
538 warn "No codespell typos will be found - file '$codespellfile': $!\n";
542 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
545 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
546 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
547 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
548 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
549 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
555 (?:$Modifier\s+|const\s+)*
557 (?:typeof|__typeof__)\s*\([^\)]*\)|
561 (?:\s+$Modifier|\s+const)*
563 $NonptrTypeMisordered = qr{
564 (?:$Modifier\s+|const\s+)*
568 (?:\s+$Modifier|\s+const)*
570 $NonptrTypeWithAttr = qr{
571 (?:$Modifier\s+|const\s+)*
573 (?:typeof|__typeof__)\s*\([^\)]*\)|
577 (?:\s+$Modifier|\s+const)*
581 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
582 (?:\s+$Inline|\s+$Modifier)*
584 $TypeMisordered = qr{
585 $NonptrTypeMisordered
586 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
587 (?:\s+$Inline|\s+$Modifier)*
589 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
590 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
594 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
596 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
597 # requires at least perl version v5.10.0
598 # Any use must be runtime checked with $^V
600 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
601 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
602 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
604 our $declaration_macros = qr{(?x:
605 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
606 (?:$Storage\s+)?LIST_HEAD\s*\(|
607 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
612 return "" if (!defined($string));
614 while ($string =~ /^\s*\(.*\)\s*$/) {
615 $string =~ s@^\s*\(\s*@@;
616 $string =~ s@\s*\)\s*$@@;
619 $string =~ s@\s+@ @g;
624 sub seed_camelcase_file {
627 return if (!(-f $file));
631 open(my $include_file, '<', "$file")
632 or warn "$P: Can't read '$file' $!\n";
633 my $text = <$include_file>;
634 close($include_file);
636 my @lines = split('\n', $text);
638 foreach my $line (@lines) {
639 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
640 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
642 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
644 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
650 my $camelcase_seeded = 0;
651 sub seed_camelcase_includes {
652 return if ($camelcase_seeded);
655 my $camelcase_cache = "";
656 my @include_files = ();
658 $camelcase_seeded = 1;
661 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
662 chomp $git_last_include_commit;
663 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
665 my $last_mod_date = 0;
666 $files = `find $root/include -name "*.h"`;
667 @include_files = split('\n', $files);
668 foreach my $file (@include_files) {
669 my $date = POSIX::strftime("%Y%m%d%H%M",
670 localtime((stat $file)[9]));
671 $last_mod_date = $date if ($last_mod_date < $date);
673 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
676 if ($camelcase_cache ne "" && -f $camelcase_cache) {
677 open(my $camelcase_file, '<', "$camelcase_cache")
678 or warn "$P: Can't read '$camelcase_cache' $!\n";
679 while (<$camelcase_file>) {
683 close($camelcase_file);
689 $files = `git ls-files "include/*.h"`;
690 @include_files = split('\n', $files);
693 foreach my $file (@include_files) {
694 seed_camelcase_file($file);
697 if ($camelcase_cache ne "") {
698 unlink glob ".checkpatch-camelcase.*";
699 open(my $camelcase_file, '>', "$camelcase_cache")
700 or warn "$P: Can't write '$camelcase_cache' $!\n";
701 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
702 print $camelcase_file ("$_\n");
704 close($camelcase_file);
708 sub git_commit_info {
709 my ($commit, $id, $desc) = @_;
711 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
713 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
714 $output =~ s/^\s*//gm;
715 my @lines = split("\n", $output);
717 return ($id, $desc) if ($#lines < 0);
719 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
720 # Maybe one day convert this block of bash into something that returns
721 # all matching commit ids, but it's very slow...
723 # echo "checking commits $1..."
724 # git rev-list --remotes | grep -i "^$1" |
725 # while read line ; do
726 # git log --format='%H %s' -1 $line |
727 # echo "commit $(cut -c 1-12,41-)"
729 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
731 $id = substr($lines[0], 0, 12);
732 $desc = substr($lines[0], 41);
738 $chk_signoff = 0 if ($file);
743 my @fixed_inserted = ();
744 my @fixed_deleted = ();
748 for my $filename (@ARGV) {
751 open($FILE, '-|', "diff -u /dev/null $filename") ||
752 die "$P: $filename: diff failed - $!\n";
753 } elsif ($filename eq '-') {
754 open($FILE, '<&STDIN');
756 open($FILE, '<', "$filename") ||
757 die "$P: $filename: open failed - $!\n";
759 if ($filename eq '-') {
760 $vname = 'Your patch';
770 if ($#ARGV > 0 && $quiet == 0) {
771 print '-' x length($vname) . "\n";
773 print '-' x length($vname) . "\n";
776 if (!process($filename)) {
782 @fixed_inserted = ();
785 @modifierListFile = ();
791 hash_show_words(\%use_type, "Used");
792 hash_show_words(\%ignore_type, "Ignored");
797 NOTE: perl $^V is not modern enough to detect all possible issues.
798 An upgrade to at least perl v5.10.0 is suggested.
804 NOTE: If any of the errors are false positives, please report
805 them to the maintainer, see CHECKPATCH in MAINTAINERS.
812 sub top_of_kernel_tree {
816 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
817 "README", "Documentation", "arch", "include", "drivers",
818 "fs", "init", "ipc", "kernel", "lib", "scripts",
821 foreach my $check (@tree_check) {
822 if (! -e $root . '/' . $check) {
830 my ($formatted_email) = @_;
836 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
839 $comment = $3 if defined $3;
840 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
842 $comment = $2 if defined $2;
843 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
845 $comment = $2 if defined $2;
846 $formatted_email =~ s/$address.*$//;
847 $name = $formatted_email;
849 $name =~ s/^\"|\"$//g;
850 # If there's a name left after stripping spaces and
851 # leading quotes, and the address doesn't have both
852 # leading and trailing angle brackets, the address
854 # "joe smith joe@smith.com" bad
855 # "joe smith <joe@smith.com" bad
856 if ($name ne "" && $address !~ /^<[^>]+>$/) {
864 $name =~ s/^\"|\"$//g;
865 $address = trim($address);
866 $address =~ s/^\<|\>$//g;
868 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
869 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
873 return ($name, $address, $comment);
877 my ($name, $address) = @_;
882 $name =~ s/^\"|\"$//g;
883 $address = trim($address);
885 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
886 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
891 $formatted_email = "$address";
893 $formatted_email = "$name <$address>";
896 return $formatted_email;
902 foreach my $path (split(/:/, $ENV{PATH})) {
903 if (-e "$path/$bin") {
914 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
915 if (-e "$path/$conf") {
916 return "$path/$conf";
928 for my $c (split(//, $str)) {
932 for (; ($n % $tab_length) != 0; $n++) {
944 (my $res = shift) =~ tr/\t/ /c;
951 # Drop the diff line leader and expand tabs
953 $line = expand_tabs($line);
955 # Pick the indent from the front of the line.
956 my ($white) = ($line =~ /^(\s*)/);
958 return (length($line), length($white));
961 my $sanitise_quote = '';
963 sub sanitise_line_reset {
964 my ($in_comment) = @_;
967 $sanitise_quote = '*/';
969 $sanitise_quote = '';
982 # Always copy over the diff marker.
983 $res = substr($line, 0, 1);
985 for ($off = 1; $off < length($line); $off++) {
986 $c = substr($line, $off, 1);
988 # Comments we are wacking completly including the begin
989 # and end, all to $;.
990 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
991 $sanitise_quote = '*/';
993 substr($res, $off, 2, "$;$;");
997 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
998 $sanitise_quote = '';
999 substr($res, $off, 2, "$;$;");
1003 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1004 $sanitise_quote = '//';
1006 substr($res, $off, 2, $sanitise_quote);
1011 # A \ in a string means ignore the next character.
1012 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1014 substr($res, $off, 2, 'XX');
1019 if ($c eq "'" || $c eq '"') {
1020 if ($sanitise_quote eq '') {
1021 $sanitise_quote = $c;
1023 substr($res, $off, 1, $c);
1025 } elsif ($sanitise_quote eq $c) {
1026 $sanitise_quote = '';
1030 #print "c<$c> SQ<$sanitise_quote>\n";
1031 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1032 substr($res, $off, 1, $;);
1033 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1034 substr($res, $off, 1, $;);
1035 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1036 substr($res, $off, 1, 'X');
1038 substr($res, $off, 1, $c);
1042 if ($sanitise_quote eq '//') {
1043 $sanitise_quote = '';
1046 # The pathname on a #include may be surrounded by '<' and '>'.
1047 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1048 my $clean = 'X' x length($1);
1049 $res =~ s@\<.*\>@<$clean>@;
1051 # The whole of a #error is a string.
1052 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1053 my $clean = 'X' x length($1);
1054 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1060 sub get_quoted_string {
1061 my ($line, $rawline) = @_;
1063 return "" if ($line !~ m/($String)/g);
1064 return substr($rawline, $-[0], $+[0] - $-[0]);
1067 sub ctx_statement_block {
1068 my ($linenr, $remain, $off) = @_;
1069 my $line = $linenr - 1;
1072 my $coff = $off - 1;
1086 @stack = (['', 0]) if ($#stack == -1);
1088 #warn "CSB: blk<$blk> remain<$remain>\n";
1089 # If we are about to drop off the end, pull in more
1092 for (; $remain > 0; $line++) {
1093 last if (!defined $lines[$line]);
1094 next if ($lines[$line] =~ /^-/);
1097 $blk .= $lines[$line] . "\n";
1098 $len = length($blk);
1102 # Bail if there is no further context.
1103 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1107 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1113 $c = substr($blk, $off, 1);
1114 $remainder = substr($blk, $off);
1116 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1118 # Handle nested #if/#else.
1119 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1120 push(@stack, [ $type, $level ]);
1121 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1122 ($type, $level) = @{$stack[$#stack - 1]};
1123 } elsif ($remainder =~ /^#\s*endif\b/) {
1124 ($type, $level) = @{pop(@stack)};
1127 # Statement ends at the ';' or a close '}' at the
1129 if ($level == 0 && $c eq ';') {
1133 # An else is really a conditional as long as its not else if
1134 if ($level == 0 && $coff_set == 0 &&
1135 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1136 $remainder =~ /^(else)(?:\s|{)/ &&
1137 $remainder !~ /^else\s+if\b/) {
1138 $coff = $off + length($1) - 1;
1140 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1141 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1144 if (($type eq '' || $type eq '(') && $c eq '(') {
1148 if ($type eq '(' && $c eq ')') {
1150 $type = ($level != 0)? '(' : '';
1152 if ($level == 0 && $coff < $soff) {
1155 #warn "CSB: mark coff<$coff>\n";
1158 if (($type eq '' || $type eq '{') && $c eq '{') {
1162 if ($type eq '{' && $c eq '}') {
1164 $type = ($level != 0)? '{' : '';
1167 if (substr($blk, $off + 1, 1) eq ';') {
1173 # Preprocessor commands end at the newline unless escaped.
1174 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1182 # We are truly at the end, so shuffle to the next line.
1189 my $statement = substr($blk, $soff, $off - $soff + 1);
1190 my $condition = substr($blk, $soff, $coff - $soff + 1);
1192 #warn "STATEMENT<$statement>\n";
1193 #warn "CONDITION<$condition>\n";
1195 #print "coff<$coff> soff<$off> loff<$loff>\n";
1197 return ($statement, $condition,
1198 $line, $remain + 1, $off - $loff + 1, $level);
1201 sub statement_lines {
1204 # Strip the diff line prefixes and rip blank lines at start and end.
1205 $stmt =~ s/(^|\n)./$1/g;
1209 my @stmt_lines = ($stmt =~ /\n/g);
1211 return $#stmt_lines + 2;
1214 sub statement_rawlines {
1217 my @stmt_lines = ($stmt =~ /\n/g);
1219 return $#stmt_lines + 2;
1222 sub statement_block_size {
1225 $stmt =~ s/(^|\n)./$1/g;
1231 my @stmt_lines = ($stmt =~ /\n/g);
1232 my @stmt_statements = ($stmt =~ /;/g);
1234 my $stmt_lines = $#stmt_lines + 2;
1235 my $stmt_statements = $#stmt_statements + 1;
1237 if ($stmt_lines > $stmt_statements) {
1240 return $stmt_statements;
1244 sub ctx_statement_full {
1245 my ($linenr, $remain, $off) = @_;
1246 my ($statement, $condition, $level);
1250 # Grab the first conditional/block pair.
1251 ($statement, $condition, $linenr, $remain, $off, $level) =
1252 ctx_statement_block($linenr, $remain, $off);
1253 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1254 push(@chunks, [ $condition, $statement ]);
1255 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1256 return ($level, $linenr, @chunks);
1259 # Pull in the following conditional/block pairs and see if they
1260 # could continue the statement.
1262 ($statement, $condition, $linenr, $remain, $off, $level) =
1263 ctx_statement_block($linenr, $remain, $off);
1264 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1265 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1267 push(@chunks, [ $condition, $statement ]);
1270 return ($level, $linenr, @chunks);
1274 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1276 my $start = $linenr - 1;
1283 my @stack = ($level);
1284 for ($line = $start; $remain > 0; $line++) {
1285 next if ($rawlines[$line] =~ /^-/);
1288 $blk .= $rawlines[$line];
1290 # Handle nested #if/#else.
1291 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1292 push(@stack, $level);
1293 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1294 $level = $stack[$#stack - 1];
1295 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1296 $level = pop(@stack);
1299 foreach my $c (split(//, $lines[$line])) {
1300 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1306 if ($c eq $close && $level > 0) {
1308 last if ($level == 0);
1309 } elsif ($c eq $open) {
1314 if (!$outer || $level <= 1) {
1315 push(@res, $rawlines[$line]);
1318 last if ($level == 0);
1321 return ($level, @res);
1323 sub ctx_block_outer {
1324 my ($linenr, $remain) = @_;
1326 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1330 my ($linenr, $remain) = @_;
1332 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1336 my ($linenr, $remain, $off) = @_;
1338 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1341 sub ctx_block_level {
1342 my ($linenr, $remain) = @_;
1344 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1346 sub ctx_statement_level {
1347 my ($linenr, $remain, $off) = @_;
1349 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1352 sub ctx_locate_comment {
1353 my ($first_line, $end_line) = @_;
1355 # Catch a comment on the end of the line itself.
1356 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1357 return $current_comment if (defined $current_comment);
1359 # Look through the context and try and figure out if there is a
1362 $current_comment = '';
1363 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1364 my $line = $rawlines[$linenr - 1];
1366 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1369 if ($line =~ m@/\*@) {
1372 if (!$in_comment && $current_comment ne '') {
1373 $current_comment = '';
1375 $current_comment .= $line . "\n" if ($in_comment);
1376 if ($line =~ m@\*/@) {
1381 chomp($current_comment);
1382 return($current_comment);
1384 sub ctx_has_comment {
1385 my ($first_line, $end_line) = @_;
1386 my $cmt = ctx_locate_comment($first_line, $end_line);
1388 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1389 ##print "CMMT: $cmt\n";
1391 return ($cmt ne '');
1395 my ($linenr, $cnt) = @_;
1397 my $offset = $linenr - 1;
1402 $line = $rawlines[$offset++];
1403 next if (defined($line) && $line =~ /^-/);
1415 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1418 $coded = sprintf("^%c", unpack('C', $2) + 64);
1427 my $av_preprocessor = 0;
1432 sub annotate_reset {
1433 $av_preprocessor = 0;
1435 @av_paren_type = ('E');
1436 $av_pend_colon = 'O';
1439 sub annotate_values {
1440 my ($stream, $type) = @_;
1443 my $var = '_' x length($stream);
1446 print "$stream\n" if ($dbg_values > 1);
1448 while (length($cur)) {
1449 @av_paren_type = ('E') if ($#av_paren_type < 0);
1450 print " <" . join('', @av_paren_type) .
1451 "> <$type> <$av_pending>" if ($dbg_values > 1);
1452 if ($cur =~ /^(\s+)/o) {
1453 print "WS($1)\n" if ($dbg_values > 1);
1454 if ($1 =~ /\n/ && $av_preprocessor) {
1455 $type = pop(@av_paren_type);
1456 $av_preprocessor = 0;
1459 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1460 print "CAST($1)\n" if ($dbg_values > 1);
1461 push(@av_paren_type, $type);
1464 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1465 print "DECLARE($1)\n" if ($dbg_values > 1);
1468 } elsif ($cur =~ /^($Modifier)\s*/) {
1469 print "MODIFIER($1)\n" if ($dbg_values > 1);
1472 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1473 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1474 $av_preprocessor = 1;
1475 push(@av_paren_type, $type);
1481 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1482 print "UNDEF($1)\n" if ($dbg_values > 1);
1483 $av_preprocessor = 1;
1484 push(@av_paren_type, $type);
1486 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1487 print "PRE_START($1)\n" if ($dbg_values > 1);
1488 $av_preprocessor = 1;
1490 push(@av_paren_type, $type);
1491 push(@av_paren_type, $type);
1494 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1495 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1496 $av_preprocessor = 1;
1498 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1502 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1503 print "PRE_END($1)\n" if ($dbg_values > 1);
1505 $av_preprocessor = 1;
1507 # Assume all arms of the conditional end as this
1508 # one does, and continue as if the #endif was not here.
1509 pop(@av_paren_type);
1510 push(@av_paren_type, $type);
1513 } elsif ($cur =~ /^(\\\n)/o) {
1514 print "PRECONT($1)\n" if ($dbg_values > 1);
1516 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1517 print "ATTR($1)\n" if ($dbg_values > 1);
1518 $av_pending = $type;
1521 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1522 print "SIZEOF($1)\n" if ($dbg_values > 1);
1528 } elsif ($cur =~ /^(if|while|for)\b/o) {
1529 print "COND($1)\n" if ($dbg_values > 1);
1533 } elsif ($cur =~/^(case)/o) {
1534 print "CASE($1)\n" if ($dbg_values > 1);
1535 $av_pend_colon = 'C';
1538 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1539 print "KEYWORD($1)\n" if ($dbg_values > 1);
1542 } elsif ($cur =~ /^(\()/o) {
1543 print "PAREN('$1')\n" if ($dbg_values > 1);
1544 push(@av_paren_type, $av_pending);
1548 } elsif ($cur =~ /^(\))/o) {
1549 my $new_type = pop(@av_paren_type);
1550 if ($new_type ne '_') {
1552 print "PAREN('$1') -> $type\n"
1553 if ($dbg_values > 1);
1555 print "PAREN('$1')\n" if ($dbg_values > 1);
1558 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1559 print "FUNC($1)\n" if ($dbg_values > 1);
1563 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1564 if (defined $2 && $type eq 'C' || $type eq 'T') {
1565 $av_pend_colon = 'B';
1566 } elsif ($type eq 'E') {
1567 $av_pend_colon = 'L';
1569 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1572 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1573 print "IDENT($1)\n" if ($dbg_values > 1);
1576 } elsif ($cur =~ /^($Assignment)/o) {
1577 print "ASSIGN($1)\n" if ($dbg_values > 1);
1580 } elsif ($cur =~/^(;|{|})/) {
1581 print "END($1)\n" if ($dbg_values > 1);
1583 $av_pend_colon = 'O';
1585 } elsif ($cur =~/^(,)/) {
1586 print "COMMA($1)\n" if ($dbg_values > 1);
1589 } elsif ($cur =~ /^(\?)/o) {
1590 print "QUESTION($1)\n" if ($dbg_values > 1);
1593 } elsif ($cur =~ /^(:)/o) {
1594 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1596 substr($var, length($res), 1, $av_pend_colon);
1597 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1602 $av_pend_colon = 'O';
1604 } elsif ($cur =~ /^(\[)/o) {
1605 print "CLOSE($1)\n" if ($dbg_values > 1);
1608 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1611 print "OPV($1)\n" if ($dbg_values > 1);
1618 substr($var, length($res), 1, $variant);
1621 } elsif ($cur =~ /^($Operators)/o) {
1622 print "OP($1)\n" if ($dbg_values > 1);
1623 if ($1 ne '++' && $1 ne '--') {
1627 } elsif ($cur =~ /(^.)/o) {
1628 print "C($1)\n" if ($dbg_values > 1);
1631 $cur = substr($cur, length($1));
1632 $res .= $type x length($1);
1636 return ($res, $var);
1640 my ($possible, $line) = @_;
1641 my $notPermitted = qr{(?:
1658 ^(?:typedef|struct|enum)\b
1660 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1661 if ($possible !~ $notPermitted) {
1662 # Check for modifiers.
1663 $possible =~ s/\s*$Storage\s*//g;
1664 $possible =~ s/\s*$Sparse\s*//g;
1665 if ($possible =~ /^\s*$/) {
1667 } elsif ($possible =~ /\s/) {
1668 $possible =~ s/\s*$Type\s*//g;
1669 for my $modifier (split(' ', $possible)) {
1670 if ($modifier !~ $notPermitted) {
1671 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1672 push(@modifierListFile, $modifier);
1677 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1678 push(@typeListFile, $possible);
1682 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1691 return defined $use_type{$type} if (scalar keys %use_type > 0);
1693 return !defined $ignore_type{$type};
1697 my ($level, $type, $msg) = @_;
1699 if (!show_type($type) ||
1700 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1704 if (-t STDOUT && $color) {
1705 if ($level eq 'ERROR') {
1707 } elsif ($level eq 'WARNING') {
1713 $output .= $prefix . $level . ':';
1715 $output .= BLUE if (-t STDOUT && $color);
1716 $output .= "$type:";
1718 $output .= RESET if (-t STDOUT && $color);
1719 $output .= ' ' . $msg . "\n";
1722 my @lines = split("\n", $output, -1);
1723 splice(@lines, 1, 1);
1724 $output = join("\n", @lines);
1726 $output = (split('\n', $output))[0] . "\n" if ($terse);
1728 push(our @report, $output);
1737 sub fixup_current_range {
1738 my ($lineRef, $offset, $length) = @_;
1740 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1743 my $no = $o + $offset;
1744 my $nl = $l + $length;
1745 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1749 sub fix_inserted_deleted_lines {
1750 my ($linesRef, $insertedRef, $deletedRef) = @_;
1752 my $range_last_linenr = 0;
1753 my $delta_offset = 0;
1758 my $next_insert = 0;
1759 my $next_delete = 0;
1763 my $inserted = @{$insertedRef}[$next_insert++];
1764 my $deleted = @{$deletedRef}[$next_delete++];
1766 foreach my $old_line (@{$linesRef}) {
1768 my $line = $old_line; #don't modify the array
1769 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
1771 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1772 $range_last_linenr = $new_linenr;
1773 fixup_current_range(\$line, $delta_offset, 0);
1776 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1777 $deleted = @{$deletedRef}[$next_delete++];
1779 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1782 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1783 push(@lines, ${$inserted}{'LINE'});
1784 $inserted = @{$insertedRef}[$next_insert++];
1786 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1790 push(@lines, $line);
1800 sub fix_insert_line {
1801 my ($linenr, $line) = @_;
1807 push(@fixed_inserted, $inserted);
1810 sub fix_delete_line {
1811 my ($linenr, $line) = @_;
1818 push(@fixed_deleted, $deleted);
1822 my ($type, $msg) = @_;
1824 if (report("ERROR", $type, $msg)) {
1832 my ($type, $msg) = @_;
1834 if (report("WARNING", $type, $msg)) {
1842 my ($type, $msg) = @_;
1844 if ($check && report("CHECK", $type, $msg)) {
1852 sub check_absolute_file {
1853 my ($absolute, $herecurr) = @_;
1854 my $file = $absolute;
1856 ##print "absolute<$absolute>\n";
1858 # See if any suffix of this path is a path within the tree.
1859 while ($file =~ s@^[^/]*/@@) {
1860 if (-f "$root/$file") {
1861 ##print "file<$file>\n";
1869 # It is, so see if the prefix is acceptable.
1870 my $prefix = $absolute;
1871 substr($prefix, -length($file)) = '';
1873 ##print "prefix<$prefix>\n";
1874 if ($prefix ne ".../") {
1875 WARN("USE_RELATIVE_PATH",
1876 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1883 $string =~ s/^\s+|\s+$//g;
1891 $string =~ s/^\s+//;
1899 $string =~ s/\s+$//;
1904 sub string_find_replace {
1905 my ($string, $find, $replace) = @_;
1907 $string =~ s/$find/$replace/g;
1915 my $source_indent = $tab_length;
1916 my $max_spaces_before_tab = $source_indent - 1;
1917 my $spaces_to_tab = " " x $source_indent;
1919 #convert leading spaces to tabs
1920 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1921 #Remove spaces before a tab
1922 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1927 sub pos_last_openparen {
1932 my $opens = $line =~ tr/\(/\(/;
1933 my $closes = $line =~ tr/\)/\)/;
1935 my $last_openparen = 0;
1937 if (($opens == 0) || ($closes >= $opens)) {
1941 my $len = length($line);
1943 for ($pos = 0; $pos < $len; $pos++) {
1944 my $string = substr($line, $pos);
1945 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1946 $pos += length($1) - 1;
1947 } elsif (substr($line, $pos, 1) eq '(') {
1948 $last_openparen = $pos;
1949 } elsif (index($string, '(') == -1) {
1954 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1958 my $filename = shift;
1964 my $stashrawline="";
1974 my $in_header_lines = $file ? 0 : 1;
1975 my $in_commit_log = 0; #Scanning lines before patch
1976 my $commit_log_possible_stack_dump = 0;
1977 my $commit_log_long_line = 0;
1978 my $commit_log_has_diff = 0;
1979 my $reported_maintainer_file = 0;
1980 my $non_utf8_charset = 0;
1982 my $last_blank_line = 0;
1983 my $last_coalesced_string_linenr = -1;
1991 # Trace the real file/line as we go.
1997 my $comment_edge = 0;
2001 my $prev_values = 'E';
2004 my %suppress_ifbraces;
2005 my %suppress_whiletrailers;
2006 my %suppress_export;
2007 my $suppress_statement = 0;
2009 my %signatures = ();
2011 # Pre-scan the patch sanitizing the lines.
2012 # Pre-scan the patch looking for any __setup documentation.
2014 my @setup_docs = ();
2017 my $camelcase_file_seeded = 0;
2019 sanitise_line_reset();
2021 foreach my $rawline (@rawlines) {
2025 push(@fixed, $rawline) if ($fix);
2027 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2029 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2034 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2043 # Guestimate if this is a continuing comment. Run
2044 # the context looking for a comment "edge". If this
2045 # edge is a close comment then we must be in a comment
2049 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2050 next if (defined $rawlines[$ln - 1] &&
2051 $rawlines[$ln - 1] =~ /^-/);
2053 #print "RAW<$rawlines[$ln - 1]>\n";
2054 last if (!defined $rawlines[$ln - 1]);
2055 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2056 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2061 if (defined $edge && $edge eq '*/') {
2065 # Guestimate if this is a continuing comment. If this
2066 # is the start of a diff block and this line starts
2067 # ' *' then it is very likely a comment.
2068 if (!defined $edge &&
2069 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2074 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2075 sanitise_line_reset($in_comment);
2077 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2078 # Standardise the strings and chars within the input to
2079 # simplify matching -- only bother with positive lines.
2080 $line = sanitise_line($rawline);
2082 push(@lines, $line);
2085 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2090 #print "==>$rawline\n";
2091 #print "-->$line\n";
2093 if ($setup_docs && $line =~ /^\+/) {
2094 push(@setup_docs, $line);
2103 foreach my $line (@lines) {
2106 my $sline = $line; #copy of $line
2107 $sline =~ s/$;/ /g; #with comments as spaces
2109 my $rawline = $rawlines[$linenr - 1];
2111 #extract the line range in the file after the patch is applied
2112 if (!$in_commit_log &&
2113 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2115 $first_line = $linenr + 1;
2125 %suppress_ifbraces = ();
2126 %suppress_whiletrailers = ();
2127 %suppress_export = ();
2128 $suppress_statement = 0;
2131 # track the line number as we move through the hunk, note that
2132 # new versions of GNU diff omit the leading space on completely
2133 # blank context lines so we need to count that too.
2134 } elsif ($line =~ /^( |\+|$)/) {
2136 $realcnt-- if ($realcnt != 0);
2138 # Measure the line length and indent.
2139 ($length, $indent) = line_stats($rawline);
2141 # Track the previous line.
2142 ($prevline, $stashline) = ($stashline, $line);
2143 ($previndent, $stashindent) = ($stashindent, $indent);
2144 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2146 #warn "line<$line>\n";
2148 } elsif ($realcnt == 1) {
2152 my $hunk_line = ($realcnt != 0);
2154 $here = "#$linenr: " if (!$file);
2155 $here = "#$realline: " if ($file);
2158 # extract the filename as it passes
2159 if ($line =~ /^diff --git.*?(\S+)$/) {
2161 $realfile =~ s@^([^/]*)/@@ if (!$file);
2164 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2166 $realfile =~ s@^([^/]*)/@@ if (!$file);
2170 if (!$file && $tree && $p1_prefix ne '' &&
2171 -e "$root/$p1_prefix") {
2172 WARN("PATCH_PREFIX",
2173 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2176 if ($realfile =~ m@^include/asm/@) {
2177 ERROR("MODIFIED_INCLUDE_ASM",
2178 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2183 #make up the handle for any error we report on this line
2185 $prefix = "$realfile:$realline: "
2188 $prefix = "$filename:$realline: ";
2190 $prefix = "$filename:$linenr: ";
2195 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2198 $check = $check_orig;
2203 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2205 my $hereline = "$here\n$rawline\n";
2206 my $herecurr = "$here\n$rawline\n";
2207 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2209 $cnt_lines++ if ($realcnt != 0);
2211 # Check if the commit log has what seems like a diff which can confuse patch
2212 if ($in_commit_log && !$commit_log_has_diff &&
2213 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2214 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2215 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2216 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2217 ERROR("DIFF_IN_COMMIT_MSG",
2218 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2219 $commit_log_has_diff = 1;
2222 # Check for incorrect file permissions
2223 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2224 my $permhere = $here . "FILE: $realfile\n";
2225 if ($realfile !~ m@scripts/@ &&
2226 $realfile !~ /\.(py|pl|awk|sh)$/) {
2227 ERROR("EXECUTE_PERMISSIONS",
2228 "do not set execute permissions for source files\n" . $permhere);
2232 # Check the patch for a signoff:
2233 if ($line =~ /^\s*signed-off-by:/i) {
2238 # Check if MAINTAINERS is being updated. If so, there's probably no need to
2239 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
2240 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2241 $reported_maintainer_file = 1;
2244 # Check signature styles
2245 if (!$in_header_lines &&
2246 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2247 my $space_before = $1;
2249 my $space_after = $3;
2251 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2253 if ($sign_off !~ /$signature_tags/) {
2254 WARN("BAD_SIGN_OFF",
2255 "Non-standard signature: $sign_off\n" . $herecurr);
2257 if (defined $space_before && $space_before ne "") {
2258 if (WARN("BAD_SIGN_OFF",
2259 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2261 $fixed[$fixlinenr] =
2262 "$ucfirst_sign_off $email";
2265 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2266 if (WARN("BAD_SIGN_OFF",
2267 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2269 $fixed[$fixlinenr] =
2270 "$ucfirst_sign_off $email";
2274 if (!defined $space_after || $space_after ne " ") {
2275 if (WARN("BAD_SIGN_OFF",
2276 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2278 $fixed[$fixlinenr] =
2279 "$ucfirst_sign_off $email";
2283 my ($email_name, $email_address, $comment) = parse_email($email);
2284 my $suggested_email = format_email(($email_name, $email_address));
2285 if ($suggested_email eq "") {
2286 ERROR("BAD_SIGN_OFF",
2287 "Unrecognized email address: '$email'\n" . $herecurr);
2289 my $dequoted = $suggested_email;
2290 $dequoted =~ s/^"//;
2291 $dequoted =~ s/" </ </;
2292 # Don't force email to have quotes
2293 # Allow just an angle bracketed address
2294 if ("$dequoted$comment" ne $email &&
2295 "<$email_address>$comment" ne $email &&
2296 "$suggested_email$comment" ne $email) {
2297 WARN("BAD_SIGN_OFF",
2298 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2302 # Check for duplicate signatures
2303 my $sig_nospace = $line;
2304 $sig_nospace =~ s/\s//g;
2305 $sig_nospace = lc($sig_nospace);
2306 if (defined $signatures{$sig_nospace}) {
2307 WARN("BAD_SIGN_OFF",
2308 "Duplicate signature\n" . $herecurr);
2310 $signatures{$sig_nospace} = 1;
2314 # Check email subject for common tools that don't need to be mentioned
2315 if ($in_header_lines &&
2316 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2317 WARN("EMAIL_SUBJECT",
2318 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2321 # Check for old stable address
2322 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2323 ERROR("STABLE_ADDRESS",
2324 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2327 # Check for unwanted Gerrit info
2328 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2329 ERROR("GERRIT_CHANGE_ID",
2330 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2333 # Check for line lengths > 75 in commit log, warn once
2334 if ($in_commit_log && !$commit_log_long_line &&
2335 length($line) > 75 &&
2336 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2337 # file delta changes
2338 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2340 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2341 # A Fixes: or Link: line
2342 $commit_log_possible_stack_dump)) {
2343 WARN("COMMIT_LOG_LONG_LINE",
2344 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2345 $commit_log_long_line = 1;
2348 # Check if the commit log is in a possible stack dump
2349 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2350 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2351 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2353 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2354 # stack dump address
2355 $commit_log_possible_stack_dump = 1;
2358 # Reset possible stack dump if a blank line is found
2359 if ($in_commit_log && $commit_log_possible_stack_dump &&
2361 $commit_log_possible_stack_dump = 0;
2364 # Check for git id commit length and improperly formed commit descriptions
2365 if ($in_commit_log &&
2366 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2367 ($line =~ /\b[0-9a-f]{12,40}\b/i &&
2368 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2369 my $init_char = "c";
2370 my $orig_commit = "";
2377 my $id = '0123456789ab';
2378 my $orig_desc = "commit description";
2379 my $description = "";
2381 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2383 $orig_commit = lc($2);
2384 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2385 $orig_commit = lc($1);
2388 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2389 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2390 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2391 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2392 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2395 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2396 defined $rawlines[$linenr] &&
2397 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2400 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2401 defined $rawlines[$linenr] &&
2402 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2403 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2405 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2406 $orig_desc .= " " . $1;
2410 ($id, $description) = git_commit_info($orig_commit,
2413 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2414 ERROR("GIT_COMMIT_ID",
2415 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2419 ## Check for added, moved or deleted files
2420 # if (!$reported_maintainer_file && !$in_commit_log &&
2421 # ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2422 # $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2423 # ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2424 # (defined($1) || defined($2))))) {
2425 # $reported_maintainer_file = 1;
2426 # WARN("FILE_PATH_CHANGES",
2427 # "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2430 # Check for wrappage within a valid hunk of the file
2431 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2432 ERROR("CORRUPTED_PATCH",
2433 "patch seems to be corrupt (line wrapped?)\n" .
2434 $herecurr) if (!$emitted_corrupt++);
2437 # Check for absolute kernel paths.
2439 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2442 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2443 check_absolute_file($1, $herecurr)) {
2446 check_absolute_file($file, $herecurr);
2451 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2452 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2453 $rawline !~ m/^$UTF8*$/) {
2454 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2456 my $blank = copy_spacing($rawline);
2457 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2458 my $hereptr = "$hereline$ptr\n";
2461 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2464 # Check if it's the start of a commit log
2465 # (not a header line and we haven't seen the patch filename)
2466 if ($in_header_lines && $realfile =~ /^$/ &&
2467 !($rawline =~ /^\s+\S/ ||
2468 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2469 $in_header_lines = 0;
2473 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2474 # declined it, i.e defined some charset where it is missing.
2475 if ($in_header_lines &&
2476 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2478 $non_utf8_charset = 1;
2481 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2482 $rawline =~ /$NON_ASCII_UTF8/) {
2483 WARN("UTF8_BEFORE_PATCH",
2484 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2487 # Check for various typo / spelling mistakes
2488 if (defined($misspellings) &&
2489 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2490 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2492 my $typo_fix = $spelling_fix{lc($typo)};
2493 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2494 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2495 my $msg_type = \&WARN;
2496 $msg_type = \&CHK if ($file);
2497 if (&{$msg_type}("TYPO_SPELLING",
2498 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2500 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2505 # ignore non-hunk lines and lines being removed
2506 next if (!$hunk_line || $line =~ /^-/);
2508 #trailing whitespace
2509 if ($line =~ /^\+.*\015/) {
2510 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2511 if (ERROR("DOS_LINE_ENDINGS",
2512 "DOS line endings\n" . $herevet) &&
2514 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2516 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2517 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2518 if (ERROR("TRAILING_WHITESPACE",
2519 "trailing whitespace\n" . $herevet) &&
2521 $fixed[$fixlinenr] =~ s/\s+$//;
2527 # Check for FSF mailing addresses.
2528 if ($rawline =~ /\bwrite to the Free/i ||
2529 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2530 $rawline =~ /\b51\s+Franklin\s+St/i) {
2531 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2532 my $msg_type = \&ERROR;
2533 $msg_type = \&CHK if ($file);
2534 &{$msg_type}("FSF_MAILING_ADDRESS",
2535 "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)
2538 # check for Kconfig help text having a real description
2539 # Only applies when adding the entry originally, after that we do not have
2540 # sufficient context to determine whether it is indeed long enough.
2541 if ($realfile =~ /Kconfig/ &&
2542 $line =~ /^\+\s*config\s+/) {
2545 my $ln = $linenr + 1;
2549 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2550 $f = $lines[$ln - 1];
2551 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2552 $is_end = $lines[$ln - 1] =~ /^\+/;
2554 next if ($f =~ /^-/);
2555 last if (!$file && $f =~ /^\@\@/);
2557 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2559 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2566 next if ($f =~ /^$/);
2567 if ($f =~ /^\s*config\s/) {
2573 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2574 WARN("CONFIG_DESCRIPTION",
2575 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2577 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2580 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2581 if ($realfile =~ /Kconfig/ &&
2582 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2583 WARN("CONFIG_EXPERIMENTAL",
2584 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2587 # discourage the use of boolean for type definition attributes of Kconfig options
2588 if ($realfile =~ /Kconfig/ &&
2589 $line =~ /^\+\s*\bboolean\b/) {
2590 WARN("CONFIG_TYPE_BOOLEAN",
2591 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2594 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2595 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2598 'EXTRA_AFLAGS' => 'asflags-y',
2599 'EXTRA_CFLAGS' => 'ccflags-y',
2600 'EXTRA_CPPFLAGS' => 'cppflags-y',
2601 'EXTRA_LDFLAGS' => 'ldflags-y',
2604 WARN("DEPRECATED_VARIABLE",
2605 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2608 # check for DT compatible documentation
2609 if (defined $root &&
2610 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2611 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2613 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2615 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2616 my $vp_file = $dt_path . "vendor-prefixes.txt";
2618 foreach my $compat (@compats) {
2619 my $compat2 = $compat;
2620 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2621 my $compat3 = $compat;
2622 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2623 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2625 WARN("UNDOCUMENTED_DT_STRING",
2626 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2629 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2631 `grep -Eq "^$vendor\\b" $vp_file`;
2633 WARN("UNDOCUMENTED_DT_STRING",
2634 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2639 # check we are in a valid source file if not then ignore this hunk
2640 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2642 # line length limit (with some exclusions)
2644 # There are a few types of lines that may extend beyond $max_line_length:
2645 # logging functions like pr_info that end in a string
2646 # lines with a single string
2647 # #defines that are a single string
2649 # There are 3 different line length message types:
2650 # LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2651 # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2652 # LONG_LINE all other lines longer than $max_line_length
2654 # if LONG_LINE is ignored, the other 2 types are also ignored
2657 if ($line =~ /^\+/ && $length > $max_line_length) {
2658 my $msg_type = "LONG_LINE";
2660 # Check the allowed long line types first
2662 # logging functions that end in a string that starts
2663 # before $max_line_length
2664 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2665 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2668 # lines with only strings (w/ possible termination)
2669 # #defines with only strings
2670 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2671 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2674 # Otherwise set the alternate message types
2676 # a comment starts before $max_line_length
2677 } elsif ($line =~ /($;[\s$;]*)$/ &&
2678 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2679 $msg_type = "LONG_LINE_COMMENT"
2681 # a quoted string starts before $max_line_length
2682 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2683 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2684 $msg_type = "LONG_LINE_STRING"
2687 if ($msg_type ne "" &&
2688 (show_type("LONG_LINE") || show_type($msg_type))) {
2690 "line over $max_line_length characters\n" . $herecurr);
2694 # check for adding lines without a newline.
2695 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2696 WARN("MISSING_EOF_NEWLINE",
2697 "adding a line without newline at end of file\n" . $herecurr);
2700 # Blackfin: use hi/lo macros
2701 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2702 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2703 my $herevet = "$here\n" . cat_vet($line) . "\n";
2705 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2707 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2708 my $herevet = "$here\n" . cat_vet($line) . "\n";
2710 "use the HI() macro, not (... >> 16)\n" . $herevet);
2714 # check we are in a valid source file C or perl if not then ignore this hunk
2715 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2717 ## at the beginning of a line any tabs must come first and anything
2718 ## more than tab_length must use tabs.
2719 # if ($rawline =~ /^\+\s* \t\s*\S/ ||
2720 # $rawline =~ /^\+\s* \s*/) {
2721 # my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2722 # $rpt_cleaners = 1;
2723 # if (ERROR("CODE_INDENT",
2724 # "code indent should use tabs where possible\n" . $herevet) &&
2726 # $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2730 # check for space before tabs.
2731 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2732 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2733 if (WARN("SPACE_BEFORE_TAB",
2734 "please, no space before tabs\n" . $herevet) &&
2736 while ($fixed[$fixlinenr] =~
2737 s/(^\+.*) {$tab_length,$tab_length}\t/$1\t\t/) {}
2738 while ($fixed[$fixlinenr] =~
2739 s/(^\+.*) +\t/$1\t/) {}
2743 # check for && or || at the start of a line
2744 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2745 CHK("LOGICAL_CONTINUATIONS",
2746 "Logical continuations should be on the previous line\n" . $hereprev);
2749 # check multi-line statement indentation matches previous line
2750 if ($^V && $^V ge 5.10.0 &&
2751 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2752 $prevline =~ /^\+(\t*)(.*)$/;
2756 my $pos = pos_last_openparen($rest);
2758 $line =~ /^(\+| )([ \t]*)/;
2761 my $goodtabindent = $oldindent .
2762 "\t" x ($pos / $tab_length) .
2763 " " x ($pos % $tab_length);
2764 my $goodspaceindent = $oldindent . " " x $pos;
2766 if ($newindent ne $goodtabindent &&
2767 $newindent ne $goodspaceindent) {
2769 if (CHK("PARENTHESIS_ALIGNMENT",
2770 "Alignment should match open parenthesis\n" . $hereprev) &&
2771 $fix && $line =~ /^\+/) {
2772 $fixed[$fixlinenr] =~
2773 s/^\+[ \t]*/\+$goodtabindent/;
2779 # check for space after cast like "(int) foo" or "(struct foo) bar"
2780 # avoid checking a few false positives:
2781 # "sizeof(<type>)" or "__alignof__(<type>)"
2782 # function pointer declarations like "(*foo)(int) = bar;"
2783 # structure definitions like "(struct foo) { 0 };"
2784 # multiline macros that define functions
2785 # known attributes or the __attribute__ keyword
2786 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2787 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
2789 "No space is necessary after a cast\n" . $herecurr) &&
2791 $fixed[$fixlinenr] =~
2792 s/(\(\s*$Type\s*\))[ \t]+/$1/;
2796 # Block comment styles
2797 # Networking with an initial /*
2798 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2799 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2800 $rawline =~ /^\+[ \t]*\*/ &&
2802 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2803 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2806 # Block comments use * on subsequent lines
2807 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
2808 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
2809 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2810 $rawline =~ /^\+/ && #line is new
2811 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2812 WARN("BLOCK_COMMENT_STYLE",
2813 "Block comments use * on subsequent lines\n" . $hereprev);
2816 ## Block comments use */ on trailing lines
2817 # if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2818 # $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2819 # $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2820 # $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
2821 # WARN("BLOCK_COMMENT_STYLE",
2822 # "Block comments use a trailing */ on a separate line\n" . $herecurr);
2825 # check for missing blank lines after struct/union declarations
2826 # with exceptions for various attributes and macros
2827 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2829 !($line =~ /^\+\s*$/ ||
2830 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2831 $line =~ /^\+\s*MODULE_/i ||
2832 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2833 $line =~ /^\+[a-z_]*init/ ||
2834 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2835 $line =~ /^\+\s*DECLARE/ ||
2836 $line =~ /^\+\s*__setup/)) {
2837 if (CHK("LINE_SPACING",
2838 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2840 fix_insert_line($fixlinenr, "\+");
2844 # check for multiple consecutive blank lines
2845 if ($prevline =~ /^[\+ ]\s*$/ &&
2846 $line =~ /^\+\s*$/ &&
2847 $last_blank_line != ($linenr - 1)) {
2848 if (CHK("LINE_SPACING",
2849 "Please don't use multiple blank lines\n" . $hereprev) &&
2851 fix_delete_line($fixlinenr, $rawline);
2854 $last_blank_line = $linenr;
2857 # check for missing blank lines after declarations
2858 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2859 # actual declarations
2860 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2861 # function pointer declarations
2862 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2863 # foo bar; where foo is some local typedef or #define
2864 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2865 # known declaration macros
2866 $prevline =~ /^\+\s+$declaration_macros/) &&
2867 # for "else if" which can look like "$Ident $Ident"
2868 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2869 # other possible extensions of declaration lines
2870 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2871 # not starting a section or a macro "\" extended line
2872 $prevline =~ /(?:\{\s*|\\)$/) &&
2873 # looks like a declaration
2874 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2875 # function pointer declarations
2876 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2877 # foo bar; where foo is some local typedef or #define
2878 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2879 # known declaration macros
2880 $sline =~ /^\+\s+$declaration_macros/ ||
2881 # start of struct or union or enum
2882 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
2883 # start or end of block or continuation of declaration
2884 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2885 # bitfield continuation
2886 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2887 # other possible extensions of declaration lines
2888 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2889 # indentation of previous and current line are the same
2890 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2891 if (WARN("LINE_SPACING",
2892 "Missing a blank line after declarations\n" . $hereprev) &&
2894 fix_insert_line($fixlinenr, "\+");
2898 ## check for spaces at the beginning of a line.
2900 ## 1) within comments
2901 ## 2) indented preprocessor commands
2902 ## 3) hanging labels
2903 # if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2904 # my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2905 # if (WARN("LEADING_SPACE",
2906 # "please, no spaces at the start of a line\n" . $herevet) &&
2908 # $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2912 # check we are in a valid C source file if not then ignore this hunk
2913 next if ($realfile !~ /\.(h|c)$/);
2915 # check indentation of any line with a bare else
2916 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
2917 # if the previous line is a break or return and is indented 1 tab more...
2918 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2919 my $tabs = length($1) + 1;
2920 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2921 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2922 defined $lines[$linenr] &&
2923 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2924 WARN("UNNECESSARY_ELSE",
2925 "else is not generally useful after a break or return\n" . $hereprev);
2929 # check indentation of a line with a break;
2930 # if the previous line is a goto or return and is indented the same # of tabs
2931 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2933 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2934 WARN("UNNECESSARY_BREAK",
2935 "break is not useful after a goto or return\n" . $hereprev);
2939 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2940 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2941 WARN("CONFIG_EXPERIMENTAL",
2942 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2945 # check for RCS/CVS revision markers
2946 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2948 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2951 # Blackfin: don't use __builtin_bfin_[cs]sync
2952 if ($line =~ /__builtin_bfin_csync/) {
2953 my $herevet = "$here\n" . cat_vet($line) . "\n";
2955 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2957 if ($line =~ /__builtin_bfin_ssync/) {
2958 my $herevet = "$here\n" . cat_vet($line) . "\n";
2960 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2963 # check for old HOTPLUG __dev<foo> section markings
2964 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2965 WARN("HOTPLUG_SECTION",
2966 "Using $1 is unnecessary\n" . $herecurr);
2969 # Check for potential 'bare' types
2970 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2972 #print "LINE<$line>\n";
2973 if ($linenr >= $suppress_statement &&
2974 $realcnt && $sline =~ /.\s*\S/) {
2975 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2976 ctx_statement_block($linenr, $realcnt, 0);
2977 $stat =~ s/\n./\n /g;
2978 $cond =~ s/\n./\n /g;
2980 #print "linenr<$linenr> <$stat>\n";
2981 # If this statement has no statement boundaries within
2982 # it there is no point in retrying a statement scan
2983 # until we hit end of it.
2984 my $frag = $stat; $frag =~ s/;+\s*$//;
2985 if ($frag !~ /(?:{|;)/) {
2986 #print "skip<$line_nr_next>\n";
2987 $suppress_statement = $line_nr_next;
2990 # Find the real next line.
2991 $realline_next = $line_nr_next;
2992 if (defined $realline_next &&
2993 (!defined $lines[$realline_next - 1] ||
2994 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3001 # Ignore goto labels.
3002 if ($s =~ /$Ident:\*$/s) {
3004 # Ignore functions being called
3005 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3007 } elsif ($s =~ /^.\s*else\b/s) {
3009 # declarations always start with types
3010 } 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) {
3013 possible($type, "A:" . $s);
3015 # definitions in global scope can only start with types
3016 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3017 possible($1, "B:" . $s);
3020 # any (foo ... *) is a pointer cast, and foo is a type
3021 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3022 possible($1, "C:" . $s);
3025 # Check for any sort of function declaration.
3026 # int foo(something bar, other baz);
3027 # void (*store_gdt)(x86_descr_ptr *);
3028 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3029 my ($name_len) = length($1);
3032 substr($ctx, 0, $name_len + 1, '');
3033 $ctx =~ s/\)[^\)]*$//;
3035 for my $arg (split(/\s*,\s*/, $ctx)) {
3036 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3038 possible($1, "D:" . $s);
3046 # Checks which may be anchored in the context.
3049 ## Check for switch () and associated case and default
3050 ## statements should be at the same indent.
3051 # if ($line=~/\bswitch\s*\(.*\)/) {
3054 # my @ctx = ctx_block_outer($linenr, $realcnt);
3056 # for my $ctx (@ctx) {
3057 # my ($clen, $cindent) = line_stats($ctx);
3058 # if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3059 # $indent != $cindent) {
3060 # $err .= "$sep$ctx\n";
3067 # ERROR("SWITCH_CASE_INDENT_LEVEL",
3068 # "switch and case should be at the same indent\n$hereline$err");
3072 # if/while/etc brace do not go on next line, unless defining a do while loop,
3073 # or if that brace on the next line is for something else
3074 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3075 my $pre_ctx = "$1$2";
3077 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3079 if ($line =~ /^\+\t{6,}/) {
3080 WARN("DEEP_INDENTATION",
3081 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3084 my $ctx_cnt = $realcnt - $#ctx - 1;
3085 my $ctx = join("\n", @ctx);
3087 my $ctx_ln = $linenr;
3088 my $ctx_skip = $realcnt;
3090 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3091 defined $lines[$ctx_ln - 1] &&
3092 $lines[$ctx_ln - 1] =~ /^-/)) {
3093 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3094 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3098 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3099 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3101 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3103 "that open brace { should be on the previous line\n" .
3104 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3106 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3107 $ctx =~ /\)\s*\;\s*$/ &&
3108 defined $lines[$ctx_ln - 1])