1#!/usr/bin/env perl 2# SPDX-License-Identifier: GPL-2.0 3# 4# (c) 2001, Dave Jones. (the file handling bit) 5# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) 6# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite) 7# (c) 2008-2010 Andy Whitcroft <apw@canonical.com> 8# (c) 2010-2018 Joe Perches <joe@perches.com> 9 10use strict; 11use warnings; 12use POSIX; 13use File::Basename; 14use Cwd 'abs_path'; 15use Term::ANSIColor qw(:constants); 16use Encode qw(decode encode); 17 18my $P = $0; 19my $D = dirname(abs_path($P)); 20 21my $V = '0.32'; 22 23use Getopt::Long qw(:config no_auto_abbrev); 24 25my $quiet = 0; 26my $verbose = 0; 27my %verbose_messages = (); 28my %verbose_emitted = (); 29my $tree = 1; 30my $chk_signoff = 1; 31my $chk_patch = 1; 32my $tst_only; 33my $emacs = 0; 34my $terse = 0; 35my $showfile = 0; 36my $file = 0; 37my $git = 0; 38my %git_commits = (); 39my $check = 0; 40my $check_orig = 0; 41my $summary = 1; 42my $mailback = 0; 43my $summary_file = 0; 44my $show_types = 0; 45my $list_types = 0; 46my $fix = 0; 47my $fix_inplace = 0; 48my $root; 49my $gitroot = $ENV{'GIT_DIR'}; 50$gitroot = ".git" if !defined($gitroot); 51my %debug; 52my %camelcase = (); 53my %use_type = (); 54my @use = (); 55my %ignore_type = (); 56my @ignore = (); 57my $help = 0; 58my $configuration_file = ".checkpatch.conf"; 59my $max_line_length = 100; 60my $ignore_perl_version = 0; 61my $minimum_perl_version = 5.10.0; 62my $min_conf_desc_length = 4; 63my $spelling_file = "$D/spelling.txt"; 64my $codespell = 0; 65my $codespellfile = "/usr/share/codespell/dictionary.txt"; 66my $user_codespellfile = ""; 67my $conststructsfile = "$D/const_structs.checkpatch"; 68my $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst"; 69my $typedefsfile; 70my $color = "auto"; 71my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE 72# git output parsing needs US English output, so first set backtick child process LANGUAGE 73my $git_command ='export LANGUAGE=en_US.UTF-8; git'; 74my $tabsize = 8; 75my ${CONFIG_} = "CONFIG_"; 76 77sub help { 78 my ($exitcode) = @_; 79 80 print << "EOM"; 81Usage: $P [OPTION]... [FILE]... 82Version: $V 83 84Options: 85 -q, --quiet quiet 86 -v, --verbose verbose mode 87 --no-tree run without a kernel tree 88 --no-signoff do not check for 'Signed-off-by' line 89 --patch treat FILE as patchfile (default) 90 --emacs emacs compile window format 91 --terse one line per report 92 --showfile emit diffed file position, not input file position 93 -g, --git treat FILE as a single commit or git revision range 94 single git commit with: 95 <rev> 96 <rev>^ 97 <rev>~n 98 multiple git commits with: 99 <rev1>..<rev2> 100 <rev1>...<rev2> 101 <rev>-<count> 102 git merges are ignored 103 -f, --file treat FILE as regular source file 104 --subjective, --strict enable more subjective tests 105 --list-types list the possible message types 106 --types TYPE(,TYPE2...) show only these comma separated message types 107 --ignore TYPE(,TYPE2...) ignore various comma separated message types 108 --show-types show the specific message type in the output 109 --max-line-length=n set the maximum line length, (default $max_line_length) 110 if exceeded, warn on patches 111 requires --strict for use with --file 112 --min-conf-desc-length=n set the min description length, if shorter, warn 113 --tab-size=n set the number of spaces for tab (default $tabsize) 114 --root=PATH PATH to the kernel tree root 115 --no-summary suppress the per-file summary 116 --mailback only produce a report in case of warnings/errors 117 --summary-file include the filename in summary 118 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 119 'values', 'possible', 'type', and 'attr' (default 120 is all off) 121 --test-only=WORD report only warnings/errors containing WORD 122 literally 123 --fix EXPERIMENTAL - may create horrible results 124 If correctable single-line errors exist, create 125 "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 126 with potential errors corrected to the preferred 127 checkpatch style 128 --fix-inplace EXPERIMENTAL - may create horrible results 129 Is the same as --fix, but overwrites the input 130 file. It's your fault if there's no backup or git 131 --ignore-perl-version override checking of perl version. expect 132 runtime errors. 133 --codespell Use the codespell dictionary for spelling/typos 134 (default:$codespellfile) 135 --codespellfile Use this codespell dictionary 136 --typedefsfile Read additional types from this file 137 --color[=WHEN] Use colors 'always', 'never', or only when output 138 is a terminal ('auto'). Default is 'auto'. 139 --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default 140 ${CONFIG_}) 141 -h, --help, --version display this help and exit 142 143When FILE is - read standard input. 144EOM 145 146 exit($exitcode); 147} 148 149sub uniq { 150 my %seen; 151 return grep { !$seen{$_}++ } @_; 152} 153 154sub list_types { 155 my ($exitcode) = @_; 156 157 my $count = 0; 158 159 local $/ = undef; 160 161 open(my $script, '<', abs_path($P)) or 162 die "$P: Can't read '$P' $!\n"; 163 164 my $text = <$script>; 165 close($script); 166 167 my %types = (); 168 # Also catch when type or level is passed through a variable 169 while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 170 if (defined($1)) { 171 if (exists($types{$2})) { 172 $types{$2} .= ",$1" if ($types{$2} ne $1); 173 } else { 174 $types{$2} = $1; 175 } 176 } else { 177 $types{$2} = "UNDETERMINED"; 178 } 179 } 180 181 print("#\tMessage type\n\n"); 182 if ($color) { 183 print(" ( Color coding: "); 184 print(RED . "ERROR" . RESET); 185 print(" | "); 186 print(YELLOW . "WARNING" . RESET); 187 print(" | "); 188 print(GREEN . "CHECK" . RESET); 189 print(" | "); 190 print("Multiple levels / Undetermined"); 191 print(" )\n\n"); 192 } 193 194 foreach my $type (sort keys %types) { 195 my $orig_type = $type; 196 if ($color) { 197 my $level = $types{$type}; 198 if ($level eq "ERROR") { 199 $type = RED . $type . RESET; 200 } elsif ($level eq "WARN") { 201 $type = YELLOW . $type . RESET; 202 } elsif ($level eq "CHK") { 203 $type = GREEN . $type . RESET; 204 } 205 } 206 print(++$count . "\t" . $type . "\n"); 207 if ($verbose && exists($verbose_messages{$orig_type})) { 208 my $message = $verbose_messages{$orig_type}; 209 $message =~ s/\n/\n\t/g; 210 print("\t" . $message . "\n\n"); 211 } 212 } 213 214 exit($exitcode); 215} 216 217my $conf = which_conf($configuration_file); 218if (-f $conf) { 219 my @conf_args; 220 open(my $conffile, '<', "$conf") 221 or warn "$P: Can't find a readable $configuration_file file $!\n"; 222 223 while (<$conffile>) { 224 my $line = $_; 225 226 $line =~ s/\s*\n?$//g; 227 $line =~ s/^\s*//g; 228 $line =~ s/\s+/ /g; 229 230 next if ($line =~ m/^\s*#/); 231 next if ($line =~ m/^\s*$/); 232 233 my @words = split(" ", $line); 234 foreach my $word (@words) { 235 last if ($word =~ m/^#/); 236 push (@conf_args, $word); 237 } 238 } 239 close($conffile); 240 unshift(@ARGV, @conf_args) if @conf_args; 241} 242 243sub load_docs { 244 open(my $docs, '<', "$docsfile") 245 or warn "$P: Can't read the documentation file $docsfile $!\n"; 246 247 my $type = ''; 248 my $desc = ''; 249 my $in_desc = 0; 250 251 while (<$docs>) { 252 chomp; 253 my $line = $_; 254 $line =~ s/\s+$//; 255 256 if ($line =~ /^\s*\*\*(.+)\*\*$/) { 257 if ($desc ne '') { 258 $verbose_messages{$type} = trim($desc); 259 } 260 $type = $1; 261 $desc = ''; 262 $in_desc = 1; 263 } elsif ($in_desc) { 264 if ($line =~ /^(?:\s{4,}|$)/) { 265 $line =~ s/^\s{4}//; 266 $desc .= $line; 267 $desc .= "\n"; 268 } else { 269 $verbose_messages{$type} = trim($desc); 270 $type = ''; 271 $desc = ''; 272 $in_desc = 0; 273 } 274 } 275 } 276 277 if ($desc ne '') { 278 $verbose_messages{$type} = trim($desc); 279 } 280 close($docs); 281} 282 283# Perl's Getopt::Long allows options to take optional arguments after a space. 284# Prevent --color by itself from consuming other arguments 285foreach (@ARGV) { 286 if ($_ eq "--color" || $_ eq "-color") { 287 $_ = "--color=$color"; 288 } 289} 290 291GetOptions( 292 'q|quiet+' => \$quiet, 293 'v|verbose!' => \$verbose, 294 'tree!' => \$tree, 295 'signoff!' => \$chk_signoff, 296 'patch!' => \$chk_patch, 297 'emacs!' => \$emacs, 298 'terse!' => \$terse, 299 'showfile!' => \$showfile, 300 'f|file!' => \$file, 301 'g|git!' => \$git, 302 'subjective!' => \$check, 303 'strict!' => \$check, 304 'ignore=s' => \@ignore, 305 'types=s' => \@use, 306 'show-types!' => \$show_types, 307 'list-types!' => \$list_types, 308 'max-line-length=i' => \$max_line_length, 309 'min-conf-desc-length=i' => \$min_conf_desc_length, 310 'tab-size=i' => \$tabsize, 311 'root=s' => \$root, 312 'summary!' => \$summary, 313 'mailback!' => \$mailback, 314 'summary-file!' => \$summary_file, 315 'fix!' => \$fix, 316 'fix-inplace!' => \$fix_inplace, 317 'ignore-perl-version!' => \$ignore_perl_version, 318 'debug=s' => \%debug, 319 'test-only=s' => \$tst_only, 320 'codespell!' => \$codespell, 321 'codespellfile=s' => \$user_codespellfile, 322 'typedefsfile=s' => \$typedefsfile, 323 'color=s' => \$color, 324 'no-color' => \$color, #keep old behaviors of -nocolor 325 'nocolor' => \$color, #keep old behaviors of -nocolor 326 'kconfig-prefix=s' => \${CONFIG_}, 327 'h|help' => \$help, 328 'version' => \$help 329) or $help = 2; 330 331if ($user_codespellfile) { 332 # Use the user provided codespell file unconditionally 333 $codespellfile = $user_codespellfile; 334} elsif (!(-f $codespellfile)) { 335 # If /usr/share/codespell/dictionary.txt is not present, try to find it 336 # under codespell's install directory: <codespell_root>/data/dictionary.txt 337 if (($codespell || $help) && which("python3") ne "") { 338 my $python_codespell_dict = << "EOF"; 339 340import os.path as op 341import codespell_lib 342codespell_dir = op.dirname(codespell_lib.__file__) 343codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt') 344print(codespell_file, end='') 345EOF 346 347 my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`; 348 $codespellfile = $codespell_dict if (-f $codespell_dict); 349 } 350} 351 352# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0 353# $help is 2 if invalid option is passed - exitcode: 1 354help($help - 1) if ($help); 355 356die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); 357die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse); 358 359if ($color =~ /^[01]$/) { 360 $color = !$color; 361} elsif ($color =~ /^always$/i) { 362 $color = 1; 363} elsif ($color =~ /^never$/i) { 364 $color = 0; 365} elsif ($color =~ /^auto$/i) { 366 $color = (-t STDOUT); 367} else { 368 die "$P: Invalid color mode: $color\n"; 369} 370 371load_docs() if ($verbose); 372list_types(0) if ($list_types); 373 374$fix = 1 if ($fix_inplace); 375$check_orig = $check; 376 377my $exit = 0; 378 379my $perl_version_ok = 1; 380if ($^V && $^V lt $minimum_perl_version) { 381 $perl_version_ok = 0; 382 printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 383 exit(1) if (!$ignore_perl_version); 384} 385 386#if no filenames are given, push '-' to read patch from stdin 387if ($#ARGV < 0) { 388 push(@ARGV, '-'); 389} 390 391# skip TAB size 1 to avoid additional checks on $tabsize - 1 392die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); 393 394sub hash_save_array_words { 395 my ($hashRef, $arrayRef) = @_; 396 397 my @array = split(/,/, join(',', @$arrayRef)); 398 foreach my $word (@array) { 399 $word =~ s/\s*\n?$//g; 400 $word =~ s/^\s*//g; 401 $word =~ s/\s+/ /g; 402 $word =~ tr/[a-z]/[A-Z]/; 403 404 next if ($word =~ m/^\s*#/); 405 next if ($word =~ m/^\s*$/); 406 407 $hashRef->{$word}++; 408 } 409} 410 411sub hash_show_words { 412 my ($hashRef, $prefix) = @_; 413 414 if (keys %$hashRef) { 415 print "\nNOTE: $prefix message types:"; 416 foreach my $word (sort keys %$hashRef) { 417 print " $word"; 418 } 419 print "\n"; 420 } 421} 422 423hash_save_array_words(\%ignore_type, \@ignore); 424hash_save_array_words(\%use_type, \@use); 425 426my $dbg_values = 0; 427my $dbg_possible = 0; 428my $dbg_type = 0; 429my $dbg_attr = 0; 430for my $key (keys %debug) { 431 ## no critic 432 eval "\${dbg_$key} = '$debug{$key}';"; 433 die "$@" if ($@); 434} 435 436my $rpt_cleaners = 0; 437 438if ($terse) { 439 $emacs = 1; 440 $quiet++; 441} 442 443if ($tree) { 444 if (defined $root) { 445 if (!top_of_kernel_tree($root)) { 446 die "$P: $root: --root does not point at a valid tree\n"; 447 } 448 } else { 449 if (top_of_kernel_tree('.')) { 450 $root = '.'; 451 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 452 top_of_kernel_tree($1)) { 453 $root = $1; 454 } 455 } 456 457 if (!defined $root) { 458 print "Must be run from the top-level dir. of a kernel tree\n"; 459 exit(2); 460 } 461} 462 463my $emitted_corrupt = 0; 464 465our $Ident = qr{ 466 [A-Za-z_][A-Za-z\d_]* 467 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 468 }x; 469our $Storage = qr{extern|static|asmlinkage}; 470our $Sparse = qr{ 471 __user| 472 __kernel| 473 __force| 474 __iomem| 475 __must_check| 476 __kprobes| 477 __ref| 478 __refconst| 479 __refdata| 480 __rcu| 481 __private 482 }x; 483our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 484our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 485our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 486our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 487our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 488 489# Notes to $Attribute: 490# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 491our $Attribute = qr{ 492 const| 493 volatile| 494 __percpu| 495 __nocast| 496 __safe| 497 __bitwise| 498 __packed__| 499 __packed2__| 500 __naked| 501 __maybe_unused| 502 __always_unused| 503 __noreturn| 504 __used| 505 __cold| 506 __pure| 507 __noclone| 508 __deprecated| 509 __read_mostly| 510 __ro_after_init| 511 __kprobes| 512 $InitAttribute| 513 ____cacheline_aligned| 514 ____cacheline_aligned_in_smp| 515 ____cacheline_internodealigned_in_smp| 516 __weak| 517 __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) 518 }x; 519our $Modifier; 520our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 521our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 522our $Lval = qr{$Ident(?:$Member)*}; 523 524our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 525our $Binary = qr{(?i)0b[01]+$Int_type?}; 526our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 527our $Int = qr{[0-9]+$Int_type?}; 528our $Octal = qr{0[0-7]+$Int_type?}; 529our $String = qr{(?:\b[Lu])?"[X\t]*"}; 530our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 531our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 532our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 533our $Float = qr{$Float_hex|$Float_dec|$Float_int}; 534our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 535our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 536our $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 537our $Arithmetic = qr{\+|-|\*|\/|%}; 538our $Operators = qr{ 539 <=|>=|==|!=| 540 =>|->|<<|>>|<|>|!|~| 541 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 542 }x; 543 544our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 545 546our $BasicType; 547our $NonptrType; 548our $NonptrTypeMisordered; 549our $NonptrTypeWithAttr; 550our $Type; 551our $TypeMisordered; 552our $Declare; 553our $DeclareMisordered; 554 555our $NON_ASCII_UTF8 = qr{ 556 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 557 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 558 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 559 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 560 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 561 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 562 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 563}x; 564 565our $UTF8 = qr{ 566 [\x09\x0A\x0D\x20-\x7E] # ASCII 567 | $NON_ASCII_UTF8 568}x; 569 570our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 571our $typeOtherOSTypedefs = qr{(?x: 572 u_(?:char|short|int|long) | # bsd 573 u(?:nchar|short|int|long) # sysv 574)}; 575our $typeKernelTypedefs = qr{(?x: 576 (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 577 atomic_t 578)}; 579our $typeStdioTypedefs = qr{(?x: 580 FILE 581)}; 582our $typeTypedefs = qr{(?x: 583 $typeC99Typedefs\b| 584 $typeOtherOSTypedefs\b| 585 $typeKernelTypedefs\b| 586 $typeStdioTypedefs\b 587)}; 588 589our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 590 591our $logFunctions = qr{(?x: 592 printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 593 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 594 TP_printk| 595 WARN(?:_RATELIMIT|_ONCE|)| 596 panic| 597 MODULE_[A-Z_]+| 598 seq_vprintf|seq_printf|seq_puts 599)}; 600 601our $allocFunctions = qr{(?x: 602 (?:(?:devm_)? 603 (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? | 604 kstrdup(?:_const)? | 605 kmemdup(?:_nul)?) | 606 (?:\w+)?alloc_skb(?:_ip_align)? | 607 # dev_alloc_skb/netdev_alloc_skb, et al 608 dma_alloc_coherent 609)}; 610 611our $signature_tags = qr{(?xi: 612 Signed-off-by:| 613 Co-developed-by:| 614 Acked-by:| 615 Tested-by:| 616 Reviewed-by:| 617 Reported-by:| 618 Suggested-by:| 619 To:| 620 Cc: 621)}; 622 623our @link_tags = qw(Link Closes); 624 625#Create a search and print patterns for all these strings to be used directly below 626our $link_tags_search = ""; 627our $link_tags_print = ""; 628foreach my $entry (@link_tags) { 629 if ($link_tags_search ne "") { 630 $link_tags_search .= '|'; 631 $link_tags_print .= ' or '; 632 } 633 $entry .= ':'; 634 $link_tags_search .= $entry; 635 $link_tags_print .= "'$entry'"; 636} 637$link_tags_search = "(?:${link_tags_search})"; 638 639our $tracing_logging_tags = qr{(?xi: 640 [=-]*> | 641 <[=-]* | 642 \[ | 643 \] | 644 start | 645 called | 646 entered | 647 entry | 648 enter | 649 in | 650 inside | 651 here | 652 begin | 653 exit | 654 end | 655 done | 656 leave | 657 completed | 658 out | 659 return | 660 [\.\!:\s]* 661)}; 662 663sub edit_distance_min { 664 my (@arr) = @_; 665 my $len = scalar @arr; 666 if ((scalar @arr) < 1) { 667 # if underflow, return 668 return; 669 } 670 my $min = $arr[0]; 671 for my $i (0 .. ($len-1)) { 672 if ($arr[$i] < $min) { 673 $min = $arr[$i]; 674 } 675 } 676 return $min; 677} 678 679sub get_edit_distance { 680 my ($str1, $str2) = @_; 681 $str1 = lc($str1); 682 $str2 = lc($str2); 683 $str1 =~ s/-//g; 684 $str2 =~ s/-//g; 685 my $len1 = length($str1); 686 my $len2 = length($str2); 687 # two dimensional array storing minimum edit distance 688 my @distance; 689 for my $i (0 .. $len1) { 690 for my $j (0 .. $len2) { 691 if ($i == 0) { 692 $distance[$i][$j] = $j; 693 } elsif ($j == 0) { 694 $distance[$i][$j] = $i; 695 } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) { 696 $distance[$i][$j] = $distance[$i - 1][$j - 1]; 697 } else { 698 my $dist1 = $distance[$i][$j - 1]; #insert distance 699 my $dist2 = $distance[$i - 1][$j]; # remove 700 my $dist3 = $distance[$i - 1][$j - 1]; #replace 701 $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3); 702 } 703 } 704 } 705 return $distance[$len1][$len2]; 706} 707 708sub find_standard_signature { 709 my ($sign_off) = @_; 710 my @standard_signature_tags = ( 711 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:', 712 'Reviewed-by:', 'Reported-by:', 'Suggested-by:' 713 ); 714 foreach my $signature (@standard_signature_tags) { 715 return $signature if (get_edit_distance($sign_off, $signature) <= 2); 716 } 717 718 return ""; 719} 720 721our $obsolete_archives = qr{(?xi: 722 \Qfreedesktop.org/archives/dri-devel\E | 723 \Qlists.infradead.org\E | 724 \Qlkml.org\E | 725 \Qmail-archive.com\E | 726 \Qmailman.alsa-project.org/pipermail\E | 727 \Qmarc.info\E | 728 \Qozlabs.org/pipermail\E | 729 \Qspinics.net\E 730)}; 731 732our @typeListMisordered = ( 733 qr{char\s+(?:un)?signed}, 734 qr{int\s+(?:(?:un)?signed\s+)?short\s}, 735 qr{int\s+short(?:\s+(?:un)?signed)}, 736 qr{short\s+int(?:\s+(?:un)?signed)}, 737 qr{(?:un)?signed\s+int\s+short}, 738 qr{short\s+(?:un)?signed}, 739 qr{long\s+int\s+(?:un)?signed}, 740 qr{int\s+long\s+(?:un)?signed}, 741 qr{long\s+(?:un)?signed\s+int}, 742 qr{int\s+(?:un)?signed\s+long}, 743 qr{int\s+(?:un)?signed}, 744 qr{int\s+long\s+long\s+(?:un)?signed}, 745 qr{long\s+long\s+int\s+(?:un)?signed}, 746 qr{long\s+long\s+(?:un)?signed\s+int}, 747 qr{long\s+long\s+(?:un)?signed}, 748 qr{long\s+(?:un)?signed}, 749); 750 751our @typeList = ( 752 qr{void}, 753 qr{(?:(?:un)?signed\s+)?char}, 754 qr{(?:(?:un)?signed\s+)?short\s+int}, 755 qr{(?:(?:un)?signed\s+)?short}, 756 qr{(?:(?:un)?signed\s+)?int}, 757 qr{(?:(?:un)?signed\s+)?long\s+int}, 758 qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 759 qr{(?:(?:un)?signed\s+)?long\s+long}, 760 qr{(?:(?:un)?signed\s+)?long}, 761 qr{(?:un)?signed}, 762 qr{float}, 763 qr{double}, 764 qr{bool}, 765 qr{struct\s+$Ident}, 766 qr{union\s+$Ident}, 767 qr{enum\s+$Ident}, 768 qr{${Ident}_t}, 769 qr{${Ident}_handler}, 770 qr{${Ident}_handler_fn}, 771 @typeListMisordered, 772); 773 774our $C90_int_types = qr{(?x: 775 long\s+long\s+int\s+(?:un)?signed| 776 long\s+long\s+(?:un)?signed\s+int| 777 long\s+long\s+(?:un)?signed| 778 (?:(?:un)?signed\s+)?long\s+long\s+int| 779 (?:(?:un)?signed\s+)?long\s+long| 780 int\s+long\s+long\s+(?:un)?signed| 781 int\s+(?:(?:un)?signed\s+)?long\s+long| 782 783 long\s+int\s+(?:un)?signed| 784 long\s+(?:un)?signed\s+int| 785 long\s+(?:un)?signed| 786 (?:(?:un)?signed\s+)?long\s+int| 787 (?:(?:un)?signed\s+)?long| 788 int\s+long\s+(?:un)?signed| 789 int\s+(?:(?:un)?signed\s+)?long| 790 791 int\s+(?:un)?signed| 792 (?:(?:un)?signed\s+)?int 793)}; 794 795our @typeListFile = (); 796our @typeListWithAttr = ( 797 @typeList, 798 qr{struct\s+$InitAttribute\s+$Ident}, 799 qr{union\s+$InitAttribute\s+$Ident}, 800); 801 802our @modifierList = ( 803 qr{fastcall}, 804); 805our @modifierListFile = (); 806 807our @mode_permission_funcs = ( 808 ["module_param", 3], 809 ["module_param_(?:array|named|string)", 4], 810 ["module_param_array_named", 5], 811 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 812 ["proc_create(?:_data|)", 2], 813 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 814 ["IIO_DEV_ATTR_[A-Z_]+", 1], 815 ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 816 ["SENSOR_TEMPLATE(?:_2|)", 3], 817 ["__ATTR", 2], 818); 819 820my $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; 821 822#Create a search pattern for all these functions to speed up a loop below 823our $mode_perms_search = ""; 824foreach my $entry (@mode_permission_funcs) { 825 $mode_perms_search .= '|' if ($mode_perms_search ne ""); 826 $mode_perms_search .= $entry->[0]; 827} 828$mode_perms_search = "(?:${mode_perms_search})"; 829 830our %deprecated_apis = ( 831 "synchronize_rcu_bh" => "synchronize_rcu", 832 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 833 "call_rcu_bh" => "call_rcu", 834 "rcu_barrier_bh" => "rcu_barrier", 835 "synchronize_sched" => "synchronize_rcu", 836 "synchronize_sched_expedited" => "synchronize_rcu_expedited", 837 "call_rcu_sched" => "call_rcu", 838 "rcu_barrier_sched" => "rcu_barrier", 839 "get_state_synchronize_sched" => "get_state_synchronize_rcu", 840 "cond_synchronize_sched" => "cond_synchronize_rcu", 841 "kmap" => "kmap_local_page", 842 "kunmap" => "kunmap_local", 843 "kmap_atomic" => "kmap_local_page", 844 "kunmap_atomic" => "kunmap_local", 845); 846 847#Create a search pattern for all these strings to speed up a loop below 848our $deprecated_apis_search = ""; 849foreach my $entry (keys %deprecated_apis) { 850 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 851 $deprecated_apis_search .= $entry; 852} 853$deprecated_apis_search = "(?:${deprecated_apis_search})"; 854 855our $mode_perms_world_writable = qr{ 856 S_IWUGO | 857 S_IWOTH | 858 S_IRWXUGO | 859 S_IALLUGO | 860 0[0-7][0-7][2367] 861}x; 862 863our %mode_permission_string_types = ( 864 "S_IRWXU" => 0700, 865 "S_IRUSR" => 0400, 866 "S_IWUSR" => 0200, 867 "S_IXUSR" => 0100, 868 "S_IRWXG" => 0070, 869 "S_IRGRP" => 0040, 870 "S_IWGRP" => 0020, 871 "S_IXGRP" => 0010, 872 "S_IRWXO" => 0007, 873 "S_IROTH" => 0004, 874 "S_IWOTH" => 0002, 875 "S_IXOTH" => 0001, 876 "S_IRWXUGO" => 0777, 877 "S_IRUGO" => 0444, 878 "S_IWUGO" => 0222, 879 "S_IXUGO" => 0111, 880); 881 882#Create a search pattern for all these strings to speed up a loop below 883our $mode_perms_string_search = ""; 884foreach my $entry (keys %mode_permission_string_types) { 885 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 886 $mode_perms_string_search .= $entry; 887} 888our $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 889our $multi_mode_perms_string_search = qr{ 890 ${single_mode_perms_string_search} 891 (?:\s*\|\s*${single_mode_perms_string_search})* 892}x; 893 894sub perms_to_octal { 895 my ($string) = @_; 896 897 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 898 899 my $val = ""; 900 my $oval = ""; 901 my $to = 0; 902 my $curpos = 0; 903 my $lastpos = 0; 904 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 905 $curpos = pos($string); 906 my $match = $2; 907 my $omatch = $1; 908 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 909 $lastpos = $curpos; 910 $to |= $mode_permission_string_types{$match}; 911 $val .= '\s*\|\s*' if ($val ne ""); 912 $val .= $match; 913 $oval .= $omatch; 914 } 915 $oval =~ s/^\s*\|\s*//; 916 $oval =~ s/\s*\|\s*$//; 917 return sprintf("%04o", $to); 918} 919 920our $allowed_asm_includes = qr{(?x: 921 irq| 922 memory| 923 time| 924 reboot 925)}; 926# memory.h: ARM has a custom one 927 928# Load common spelling mistakes and build regular expression list. 929my $misspellings; 930my %spelling_fix; 931 932if (open(my $spelling, '<', $spelling_file)) { 933 while (<$spelling>) { 934 my $line = $_; 935 936 $line =~ s/\s*\n?$//g; 937 $line =~ s/^\s*//g; 938 939 next if ($line =~ m/^\s*#/); 940 next if ($line =~ m/^\s*$/); 941 942 my ($suspect, $fix) = split(/\|\|/, $line); 943 944 $spelling_fix{$suspect} = $fix; 945 } 946 close($spelling); 947} else { 948 warn "No typos will be found - file '$spelling_file': $!\n"; 949} 950 951if ($codespell) { 952 if (open(my $spelling, '<', $codespellfile)) { 953 while (<$spelling>) { 954 my $line = $_; 955 956 $line =~ s/\s*\n?$//g; 957 $line =~ s/^\s*//g; 958 959 next if ($line =~ m/^\s*#/); 960 next if ($line =~ m/^\s*$/); 961 next if ($line =~ m/, disabled/i); 962 963 $line =~ s/,.*$//; 964 965 my ($suspect, $fix) = split(/->/, $line); 966 967 $spelling_fix{$suspect} = $fix; 968 } 969 close($spelling); 970 } else { 971 warn "No codespell typos will be found - file '$codespellfile': $!\n"; 972 } 973} 974 975$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 976 977sub read_words { 978 my ($wordsRef, $file) = @_; 979 980 if (open(my $words, '<', $file)) { 981 while (<$words>) { 982 my $line = $_; 983 984 $line =~ s/\s*\n?$//g; 985 $line =~ s/^\s*//g; 986 987 next if ($line =~ m/^\s*#/); 988 next if ($line =~ m/^\s*$/); 989 if ($line =~ /\s/) { 990 print("$file: '$line' invalid - ignored\n"); 991 next; 992 } 993 994 $$wordsRef .= '|' if (defined $$wordsRef); 995 $$wordsRef .= $line; 996 } 997 close($file); 998 return 1; 999 } 1000 1001 return 0; 1002} 1003 1004my $const_structs; 1005if (show_type("CONST_STRUCT")) { 1006 read_words(\$const_structs, $conststructsfile) 1007 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 1008} 1009 1010if (defined($typedefsfile)) { 1011 my $typeOtherTypedefs; 1012 read_words(\$typeOtherTypedefs, $typedefsfile) 1013 or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 1014 $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); 1015} 1016 1017sub build_types { 1018 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 1019 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 1020 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 1021 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 1022 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 1023 $BasicType = qr{ 1024 (?:$typeTypedefs\b)| 1025 (?:${all}\b) 1026 }x; 1027 $NonptrType = qr{ 1028 (?:$Modifier\s+|const\s+)* 1029 (?: 1030 (?:typeof|__typeof__)\s*\([^\)]*\)| 1031 (?:$typeTypedefs\b)| 1032 (?:${all}\b) 1033 ) 1034 (?:\s+$Modifier|\s+const)* 1035 }x; 1036 $NonptrTypeMisordered = qr{ 1037 (?:$Modifier\s+|const\s+)* 1038 (?: 1039 (?:${Misordered}\b) 1040 ) 1041 (?:\s+$Modifier|\s+const)* 1042 }x; 1043 $NonptrTypeWithAttr = qr{ 1044 (?:$Modifier\s+|const\s+)* 1045 (?: 1046 (?:typeof|__typeof__)\s*\([^\)]*\)| 1047 (?:$typeTypedefs\b)| 1048 (?:${allWithAttr}\b) 1049 ) 1050 (?:\s+$Modifier|\s+const)* 1051 }x; 1052 $Type = qr{ 1053 $NonptrType 1054 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 1055 (?:\s+$Inline|\s+$Modifier)* 1056 }x; 1057 $TypeMisordered = qr{ 1058 $NonptrTypeMisordered 1059 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 1060 (?:\s+$Inline|\s+$Modifier)* 1061 }x; 1062 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 1063 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 1064} 1065build_types(); 1066 1067our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 1068 1069# Using $balanced_parens, $LvalOrFunc, or $FuncArg 1070# requires at least perl version v5.10.0 1071# Any use must be runtime checked with $^V 1072 1073our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 1074our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 1075our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 1076 1077our $declaration_macros = qr{(?x: 1078 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 1079 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 1080 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(| 1081 (?:$Storage\s+)?(?:XA_STATE|XA_STATE_ORDER)\s*\( 1082)}; 1083 1084our %allow_repeated_words = ( 1085 add => '', 1086 added => '', 1087 bad => '', 1088 be => '', 1089); 1090 1091sub deparenthesize { 1092 my ($string) = @_; 1093 return "" if (!defined($string)); 1094 1095 while ($string =~ /^\s*\(.*\)\s*$/) { 1096 $string =~ s@^\s*\(\s*@@; 1097 $string =~ s@\s*\)\s*$@@; 1098 } 1099 1100 $string =~ s@\s+@ @g; 1101 1102 return $string; 1103} 1104 1105sub seed_camelcase_file { 1106 my ($file) = @_; 1107 1108 return if (!(-f $file)); 1109 1110 local $/; 1111 1112 open(my $include_file, '<', "$file") 1113 or warn "$P: Can't read '$file' $!\n"; 1114 my $text = <$include_file>; 1115 close($include_file); 1116 1117 my @lines = split('\n', $text); 1118 1119 foreach my $line (@lines) { 1120 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 1121 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 1122 $camelcase{$1} = 1; 1123 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 1124 $camelcase{$1} = 1; 1125 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 1126 $camelcase{$1} = 1; 1127 } 1128 } 1129} 1130 1131our %maintained_status = (); 1132 1133sub is_maintained_obsolete { 1134 my ($filename) = @_; 1135 1136 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 1137 1138 if (!exists($maintained_status{$filename})) { 1139 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 1140 } 1141 1142 return $maintained_status{$filename} =~ /obsolete/i; 1143} 1144 1145sub is_SPDX_License_valid { 1146 my ($license) = @_; 1147 1148 return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); 1149 1150 my $root_path = abs_path($root); 1151 my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`; 1152 return 0 if ($status ne ""); 1153 return 1; 1154} 1155 1156my $camelcase_seeded = 0; 1157sub seed_camelcase_includes { 1158 return if ($camelcase_seeded); 1159 1160 my $files; 1161 my $camelcase_cache = ""; 1162 my @include_files = (); 1163 1164 $camelcase_seeded = 1; 1165 1166 if (-e "$gitroot") { 1167 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 1168 chomp $git_last_include_commit; 1169 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 1170 } else { 1171 my $last_mod_date = 0; 1172 $files = `find $root/include -name "*.h"`; 1173 @include_files = split('\n', $files); 1174 foreach my $file (@include_files) { 1175 my $date = POSIX::strftime("%Y%m%d%H%M", 1176 localtime((stat $file)[9])); 1177 $last_mod_date = $date if ($last_mod_date < $date); 1178 } 1179 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 1180 } 1181 1182 if ($camelcase_cache ne "" && -f $camelcase_cache) { 1183 open(my $camelcase_file, '<', "$camelcase_cache") 1184 or warn "$P: Can't read '$camelcase_cache' $!\n"; 1185 while (<$camelcase_file>) { 1186 chomp; 1187 $camelcase{$_} = 1; 1188 } 1189 close($camelcase_file); 1190 1191 return; 1192 } 1193 1194 if (-e "$gitroot") { 1195 $files = `${git_command} ls-files "include/*.h"`; 1196 @include_files = split('\n', $files); 1197 } 1198 1199 foreach my $file (@include_files) { 1200 seed_camelcase_file($file); 1201 } 1202 1203 if ($camelcase_cache ne "") { 1204 unlink glob ".checkpatch-camelcase.*"; 1205 open(my $camelcase_file, '>', "$camelcase_cache") 1206 or warn "$P: Can't write '$camelcase_cache' $!\n"; 1207 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 1208 print $camelcase_file ("$_\n"); 1209 } 1210 close($camelcase_file); 1211 } 1212} 1213 1214sub git_is_single_file { 1215 my ($filename) = @_; 1216 1217 return 0 if ((which("git") eq "") || !(-e "$gitroot")); 1218 1219 my $output = `${git_command} ls-files -- $filename 2>/dev/null`; 1220 my $count = $output =~ tr/\n//; 1221 return $count eq 1 && $output =~ m{^${filename}$}; 1222} 1223 1224sub git_commit_info { 1225 my ($commit, $id, $desc) = @_; 1226 1227 return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); 1228 1229 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 1230 $output =~ s/^\s*//gm; 1231 my @lines = split("\n", $output); 1232 1233 return ($id, $desc) if ($#lines < 0); 1234 1235 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 1236# Maybe one day convert this block of bash into something that returns 1237# all matching commit ids, but it's very slow... 1238# 1239# echo "checking commits $1..." 1240# git rev-list --remotes | grep -i "^$1" | 1241# while read line ; do 1242# git log --format='%H %s' -1 $line | 1243# echo "commit $(cut -c 1-12,41-)" 1244# done 1245 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ || 1246 $lines[0] =~ /^fatal: bad object $commit/) { 1247 $id = undef; 1248 } else { 1249 $id = substr($lines[0], 0, 12); 1250 $desc = substr($lines[0], 41); 1251 } 1252 1253 return ($id, $desc); 1254} 1255 1256$chk_signoff = 0 if ($file); 1257 1258my @rawlines = (); 1259my @lines = (); 1260my @fixed = (); 1261my @fixed_inserted = (); 1262my @fixed_deleted = (); 1263my $fixlinenr = -1; 1264 1265# If input is git commits, extract all commits from the commit expressions. 1266# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 1267die "$P: No git repository found\n" if ($git && !-e "$gitroot"); 1268 1269if ($git) { 1270 my @commits = (); 1271 foreach my $commit_expr (@ARGV) { 1272 my $git_range; 1273 if ($commit_expr =~ m/^(.*)-(\d+)$/) { 1274 $git_range = "-$2 $1"; 1275 } elsif ($commit_expr =~ m/\.\./) { 1276 $git_range = "$commit_expr"; 1277 } else { 1278 $git_range = "-1 $commit_expr"; 1279 } 1280 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 1281 foreach my $line (split(/\n/, $lines)) { 1282 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 1283 next if (!defined($1) || !defined($2)); 1284 my $sha1 = $1; 1285 my $subject = $2; 1286 unshift(@commits, $sha1); 1287 $git_commits{$sha1} = $subject; 1288 } 1289 } 1290 die "$P: no git commits after extraction!\n" if (@commits == 0); 1291 @ARGV = @commits; 1292} 1293 1294my $vname; 1295$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 1296for my $filename (@ARGV) { 1297 my $FILE; 1298 my $is_git_file = git_is_single_file($filename); 1299 my $oldfile = $file; 1300 $file = 1 if ($is_git_file); 1301 if ($git) { 1302 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 1303 die "$P: $filename: git format-patch failed - $!\n"; 1304 } elsif ($file) { 1305 open($FILE, '-|', "diff -u /dev/null $filename") || 1306 die "$P: $filename: diff failed - $!\n"; 1307 } elsif ($filename eq '-') { 1308 open($FILE, '<&STDIN'); 1309 } else { 1310 open($FILE, '<', "$filename") || 1311 die "$P: $filename: open failed - $!\n"; 1312 } 1313 if ($filename eq '-') { 1314 $vname = 'Your patch'; 1315 } elsif ($git) { 1316 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1317 } else { 1318 $vname = $filename; 1319 } 1320 while (<$FILE>) { 1321 chomp; 1322 push(@rawlines, $_); 1323 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); 1324 } 1325 close($FILE); 1326 1327 if ($#ARGV > 0 && $quiet == 0) { 1328 print '-' x length($vname) . "\n"; 1329 print "$vname\n"; 1330 print '-' x length($vname) . "\n"; 1331 } 1332 1333 if (!process($filename)) { 1334 $exit = 1; 1335 } 1336 @rawlines = (); 1337 @lines = (); 1338 @fixed = (); 1339 @fixed_inserted = (); 1340 @fixed_deleted = (); 1341 $fixlinenr = -1; 1342 @modifierListFile = (); 1343 @typeListFile = (); 1344 build_types(); 1345 $file = $oldfile if ($is_git_file); 1346} 1347 1348if (!$quiet) { 1349 hash_show_words(\%use_type, "Used"); 1350 hash_show_words(\%ignore_type, "Ignored"); 1351 1352 if (!$perl_version_ok) { 1353 print << "EOM" 1354 1355NOTE: perl $^V is not modern enough to detect all possible issues. 1356 An upgrade to at least perl $minimum_perl_version is suggested. 1357EOM 1358 } 1359 if ($exit) { 1360 print << "EOM" 1361 1362NOTE: If any of the errors are false positives, please report 1363 them to the maintainer, see CHECKPATCH in MAINTAINERS. 1364EOM 1365 } 1366} 1367 1368exit($exit); 1369 1370sub top_of_kernel_tree { 1371 my ($root) = @_; 1372 1373 my @tree_check = ( 1374 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 1375 "README", "Documentation", "arch", "include", "drivers", 1376 "fs", "init", "ipc", "kernel", "lib", "scripts", 1377 ); 1378 1379 foreach my $check (@tree_check) { 1380 if (! -e $root . '/' . $check) { 1381 return 0; 1382 } 1383 } 1384 return 1; 1385} 1386 1387sub parse_email { 1388 my ($formatted_email) = @_; 1389 1390 my $name = ""; 1391 my $quoted = ""; 1392 my $name_comment = ""; 1393 my $address = ""; 1394 my $comment = ""; 1395 1396 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 1397 $name = $1; 1398 $address = $2; 1399 $comment = $3 if defined $3; 1400 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 1401 $address = $1; 1402 $comment = $2 if defined $2; 1403 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 1404 $address = $1; 1405 $comment = $2 if defined $2; 1406 $formatted_email =~ s/\Q$address\E.*$//; 1407 $name = $formatted_email; 1408 $name = trim($name); 1409 $name =~ s/^\"|\"$//g; 1410 # If there's a name left after stripping spaces and 1411 # leading quotes, and the address doesn't have both 1412 # leading and trailing angle brackets, the address 1413 # is invalid. ie: 1414 # "joe smith joe@smith.com" bad 1415 # "joe smith <joe@smith.com" bad 1416 if ($name ne "" && $address !~ /^<[^>]+>$/) { 1417 $name = ""; 1418 $address = ""; 1419 $comment = ""; 1420 } 1421 } 1422 1423 # Extract comments from names excluding quoted parts 1424 # "John D. (Doe)" - Do not extract 1425 if ($name =~ s/\"(.+)\"//) { 1426 $quoted = $1; 1427 } 1428 while ($name =~ s/\s*($balanced_parens)\s*/ /) { 1429 $name_comment .= trim($1); 1430 } 1431 $name =~ s/^[ \"]+|[ \"]+$//g; 1432 $name = trim("$quoted $name"); 1433 1434 $address = trim($address); 1435 $address =~ s/^\<|\>$//g; 1436 $comment = trim($comment); 1437 1438 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 1439 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 1440 $name = "\"$name\""; 1441 } 1442 1443 return ($name, $name_comment, $address, $comment); 1444} 1445 1446sub format_email { 1447 my ($name, $name_comment, $address, $comment) = @_; 1448 1449 my $formatted_email; 1450 1451 $name =~ s/^[ \"]+|[ \"]+$//g; 1452 $address = trim($address); 1453 $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes 1454 1455 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 1456 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 1457 $name = "\"$name\""; 1458 } 1459 1460 $name_comment = trim($name_comment); 1461 $name_comment = " $name_comment" if ($name_comment ne ""); 1462 $comment = trim($comment); 1463 $comment = " $comment" if ($comment ne ""); 1464 1465 if ("$name" eq "") { 1466 $formatted_email = "$address"; 1467 } else { 1468 $formatted_email = "$name$name_comment <$address>"; 1469 } 1470 $formatted_email .= "$comment"; 1471 return $formatted_email; 1472} 1473 1474sub reformat_email { 1475 my ($email) = @_; 1476 1477 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 1478 return format_email($email_name, $name_comment, $email_address, $comment); 1479} 1480 1481sub same_email_addresses { 1482 my ($email1, $email2) = @_; 1483 1484 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1485 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1486 1487 return $email1_name eq $email2_name && 1488 $email1_address eq $email2_address && 1489 $name1_comment eq $name2_comment && 1490 $comment1 eq $comment2; 1491} 1492 1493sub which { 1494 my ($bin) = @_; 1495 1496 foreach my $path (split(/:/, $ENV{PATH})) { 1497 if (-e "$path/$bin") { 1498 return "$path/$bin"; 1499 } 1500 } 1501 1502 return ""; 1503} 1504 1505sub which_conf { 1506 my ($conf) = @_; 1507 1508 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1509 if (-e "$path/$conf") { 1510 return "$path/$conf"; 1511 } 1512 } 1513 1514 return ""; 1515} 1516 1517sub expand_tabs { 1518 my ($str) = @_; 1519 1520 my $res = ''; 1521 my $n = 0; 1522 for my $c (split(//, $str)) { 1523 if ($c eq "\t") { 1524 $res .= ' '; 1525 $n++; 1526 for (; ($n % $tabsize) != 0; $n++) { 1527 $res .= ' '; 1528 } 1529 next; 1530 } 1531 $res .= $c; 1532 $n++; 1533 } 1534 1535 return $res; 1536} 1537sub copy_spacing { 1538 (my $res = shift) =~ tr/\t/ /c; 1539 return $res; 1540} 1541 1542sub line_stats { 1543 my ($line) = @_; 1544 1545 # Drop the diff line leader and expand tabs 1546 $line =~ s/^.//; 1547 $line = expand_tabs($line); 1548 1549 # Pick the indent from the front of the line. 1550 my ($white) = ($line =~ /^(\s*)/); 1551 1552 return (length($line), length($white)); 1553} 1554 1555my $sanitise_quote = ''; 1556 1557sub sanitise_line_reset { 1558 my ($in_comment) = @_; 1559 1560 if ($in_comment) { 1561 $sanitise_quote = '*/'; 1562 } else { 1563 $sanitise_quote = ''; 1564 } 1565} 1566sub sanitise_line { 1567 my ($line) = @_; 1568 1569 my $res = ''; 1570 my $l = ''; 1571 1572 my $qlen = 0; 1573 my $off = 0; 1574 my $c; 1575 1576 # Always copy over the diff marker. 1577 $res = substr($line, 0, 1); 1578 1579 for ($off = 1; $off < length($line); $off++) { 1580 $c = substr($line, $off, 1); 1581 1582 # Comments we are whacking completely including the begin 1583 # and end, all to $;. 1584 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1585 $sanitise_quote = '*/'; 1586 1587 substr($res, $off, 2, "$;$;"); 1588 $off++; 1589 next; 1590 } 1591 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1592 $sanitise_quote = ''; 1593 substr($res, $off, 2, "$;$;"); 1594 $off++; 1595 next; 1596 } 1597 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1598 $sanitise_quote = '//'; 1599 1600 substr($res, $off, 2, $sanitise_quote); 1601 $off++; 1602 next; 1603 } 1604 1605 # A \ in a string means ignore the next character. 1606 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1607 $c eq "\\") { 1608 substr($res, $off, 2, 'XX'); 1609 $off++; 1610 next; 1611 } 1612 # Regular quotes. 1613 if ($c eq "'" || $c eq '"') { 1614 if ($sanitise_quote eq '') { 1615 $sanitise_quote = $c; 1616 1617 substr($res, $off, 1, $c); 1618 next; 1619 } elsif ($sanitise_quote eq $c) { 1620 $sanitise_quote = ''; 1621 } 1622 } 1623 1624 #print "c<$c> SQ<$sanitise_quote>\n"; 1625 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1626 substr($res, $off, 1, $;); 1627 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1628 substr($res, $off, 1, $;); 1629 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1630 substr($res, $off, 1, 'X'); 1631 } else { 1632 substr($res, $off, 1, $c); 1633 } 1634 } 1635 1636 if ($sanitise_quote eq '//') { 1637 $sanitise_quote = ''; 1638 } 1639 1640 # The pathname on a #include may be surrounded by '<' and '>'. 1641 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1642 my $clean = 'X' x length($1); 1643 $res =~ s@\<.*\>@<$clean>@; 1644 1645 # The whole of a #error is a string. 1646 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1647 my $clean = 'X' x length($1); 1648 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1649 } 1650 1651 if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1652 my $match = $1; 1653 $res =~ s/\Q$match\E/"$;" x length($match)/e; 1654 } 1655 1656 return $res; 1657} 1658 1659sub get_quoted_string { 1660 my ($line, $rawline) = @_; 1661 1662 return "" if (!defined($line) || !defined($rawline)); 1663 return "" if ($line !~ m/($String)/g); 1664 return substr($rawline, $-[0], $+[0] - $-[0]); 1665} 1666 1667sub ctx_statement_block { 1668 my ($linenr, $remain, $off) = @_; 1669 my $line = $linenr - 1; 1670 my $blk = ''; 1671 my $soff = $off; 1672 my $coff = $off - 1; 1673 my $coff_set = 0; 1674 1675 my $loff = 0; 1676 1677 my $type = ''; 1678 my $level = 0; 1679 my @stack = (); 1680 my $p; 1681 my $c; 1682 my $len = 0; 1683 1684 my $remainder; 1685 while (1) { 1686 @stack = (['', 0]) if ($#stack == -1); 1687 1688 #warn "CSB: blk<$blk> remain<$remain>\n"; 1689 # If we are about to drop off the end, pull in more 1690 # context. 1691 if ($off >= $len) { 1692 for (; $remain > 0; $line++) { 1693 last if (!defined $lines[$line]); 1694 next if ($lines[$line] =~ /^-/); 1695 $remain--; 1696 $loff = $len; 1697 $blk .= $lines[$line] . "\n"; 1698 $len = length($blk); 1699 $line++; 1700 last; 1701 } 1702 # Bail if there is no further context. 1703 #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 1704 if ($off >= $len) { 1705 last; 1706 } 1707 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1708 $level++; 1709 $type = '#'; 1710 } 1711 } 1712 $p = $c; 1713 $c = substr($blk, $off, 1); 1714 $remainder = substr($blk, $off); 1715 1716 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 1717 1718 # Handle nested #if/#else. 1719 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 1720 push(@stack, [ $type, $level ]); 1721 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 1722 ($type, $level) = @{$stack[$#stack - 1]}; 1723 } elsif ($remainder =~ /^#\s*endif\b/) { 1724 ($type, $level) = @{pop(@stack)}; 1725 } 1726 1727 # Statement ends at the ';' or a close '}' at the 1728 # outermost level. 1729 if ($level == 0 && $c eq ';') { 1730 last; 1731 } 1732 1733 # An else is really a conditional as long as its not else if 1734 if ($level == 0 && $coff_set == 0 && 1735 (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1736 $remainder =~ /^(else)(?:\s|{)/ && 1737 $remainder !~ /^else\s+if\b/) { 1738 $coff = $off + length($1) - 1; 1739 $coff_set = 1; 1740 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1741 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 1742 } 1743 1744 if (($type eq '' || $type eq '(') && $c eq '(') { 1745 $level++; 1746 $type = '('; 1747 } 1748 if ($type eq '(' && $c eq ')') { 1749 $level--; 1750 $type = ($level != 0)? '(' : ''; 1751 1752 if ($level == 0 && $coff < $soff) { 1753 $coff = $off; 1754 $coff_set = 1; 1755 #warn "CSB: mark coff<$coff>\n"; 1756 } 1757 } 1758 if (($type eq '' || $type eq '{') && $c eq '{') { 1759 $level++; 1760 $type = '{'; 1761 } 1762 if ($type eq '{' && $c eq '}') { 1763 $level--; 1764 $type = ($level != 0)? '{' : ''; 1765 1766 if ($level == 0) { 1767 if (substr($blk, $off + 1, 1) eq ';') { 1768 $off++; 1769 } 1770 last; 1771 } 1772 } 1773 # Preprocessor commands end at the newline unless escaped. 1774 if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1775 $level--; 1776 $type = ''; 1777 $off++; 1778 last; 1779 } 1780 $off++; 1781 } 1782 # We are truly at the end, so shuffle to the next line. 1783 if ($off == $len) { 1784 $loff = $len + 1; 1785 $line++; 1786 $remain--; 1787 } 1788 1789 my $statement = substr($blk, $soff, $off - $soff + 1); 1790 my $condition = substr($blk, $soff, $coff - $soff + 1); 1791 1792 #warn "STATEMENT<$statement>\n"; 1793 #warn "CONDITION<$condition>\n"; 1794 1795 #print "coff<$coff> soff<$off> loff<$loff>\n"; 1796 1797 return ($statement, $condition, 1798 $line, $remain + 1, $off - $loff + 1, $level); 1799} 1800 1801sub statement_lines { 1802 my ($stmt) = @_; 1803 1804 # Strip the diff line prefixes and rip blank lines at start and end. 1805 $stmt =~ s/(^|\n)./$1/g; 1806 $stmt =~ s/^\s*//; 1807 $stmt =~ s/\s*$//; 1808 1809 my @stmt_lines = ($stmt =~ /\n/g); 1810 1811 return $#stmt_lines + 2; 1812} 1813 1814sub statement_rawlines { 1815 my ($stmt) = @_; 1816 1817 my @stmt_lines = ($stmt =~ /\n/g); 1818 1819 return $#stmt_lines + 2; 1820} 1821 1822sub statement_block_size { 1823 my ($stmt) = @_; 1824 1825 $stmt =~ s/(^|\n)./$1/g; 1826 $stmt =~ s/^\s*{//; 1827 $stmt =~ s/}\s*$//; 1828 $stmt =~ s/^\s*//; 1829 $stmt =~ s/\s*$//; 1830 1831 my @stmt_lines = ($stmt =~ /\n/g); 1832 my @stmt_statements = ($stmt =~ /;/g); 1833 1834 my $stmt_lines = $#stmt_lines + 2; 1835 my $stmt_statements = $#stmt_statements + 1; 1836 1837 if ($stmt_lines > $stmt_statements) { 1838 return $stmt_lines; 1839 } else { 1840 return $stmt_statements; 1841 } 1842} 1843 1844sub ctx_statement_full { 1845 my ($linenr, $remain, $off) = @_; 1846 my ($statement, $condition, $level); 1847 1848 my (@chunks); 1849 1850 # Grab the first conditional/block pair. 1851 ($statement, $condition, $linenr, $remain, $off, $level) = 1852 ctx_statement_block($linenr, $remain, $off); 1853 #print "F: c<$condition> s<$statement> remain<$remain>\n"; 1854 push(@chunks, [ $condition, $statement ]); 1855 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1856 return ($level, $linenr, @chunks); 1857 } 1858 1859 # Pull in the following conditional/block pairs and see if they 1860 # could continue the statement. 1861 for (;;) { 1862 ($statement, $condition, $linenr, $remain, $off, $level) = 1863 ctx_statement_block($linenr, $remain, $off); 1864 #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1865 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1866 #print "C: push\n"; 1867 push(@chunks, [ $condition, $statement ]); 1868 } 1869 1870 return ($level, $linenr, @chunks); 1871} 1872 1873sub ctx_block_get { 1874 my ($linenr, $remain, $outer, $open, $close, $off) = @_; 1875 my $line; 1876 my $start = $linenr - 1; 1877 my $blk = ''; 1878 my @o; 1879 my @c; 1880 my @res = (); 1881 1882 my $level = 0; 1883 my @stack = ($level); 1884 for ($line = $start; $remain > 0; $line++) { 1885 next if ($rawlines[$line] =~ /^-/); 1886 $remain--; 1887 1888 $blk .= $rawlines[$line]; 1889 1890 # Handle nested #if/#else. 1891 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 1892 push(@stack, $level); 1893 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 1894 $level = $stack[$#stack - 1]; 1895 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 1896 $level = pop(@stack); 1897 } 1898 1899 foreach my $c (split(//, $lines[$line])) { 1900 ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1901 if ($off > 0) { 1902 $off--; 1903 next; 1904 } 1905 1906 if ($c eq $close && $level > 0) { 1907 $level--; 1908 last if ($level == 0); 1909 } elsif ($c eq $open) { 1910 $level++; 1911 } 1912 } 1913 1914 if (!$outer || $level <= 1) { 1915 push(@res, $rawlines[$line]); 1916 } 1917 1918 last if ($level == 0); 1919 } 1920 1921 return ($level, @res); 1922} 1923sub ctx_block_outer { 1924 my ($linenr, $remain) = @_; 1925 1926 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1927 return @r; 1928} 1929sub ctx_block { 1930 my ($linenr, $remain) = @_; 1931 1932 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1933 return @r; 1934} 1935sub ctx_statement { 1936 my ($linenr, $remain, $off) = @_; 1937 1938 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1939 return @r; 1940} 1941sub ctx_block_level { 1942 my ($linenr, $remain) = @_; 1943 1944 return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1945} 1946sub ctx_statement_level { 1947 my ($linenr, $remain, $off) = @_; 1948 1949 return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1950} 1951 1952sub ctx_locate_comment { 1953 my ($first_line, $end_line) = @_; 1954 1955 # If c99 comment on the current line, or the line before or after 1956 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); 1957 return $current_comment if (defined $current_comment); 1958 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); 1959 return $current_comment if (defined $current_comment); 1960 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); 1961 return $current_comment if (defined $current_comment); 1962 1963 # Catch a comment on the end of the line itself. 1964 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 1965 return $current_comment if (defined $current_comment); 1966 1967 # Look through the context and try and figure out if there is a 1968 # comment. 1969 my $in_comment = 0; 1970 $current_comment = ''; 1971 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 1972 my $line = $rawlines[$linenr - 1]; 1973 #warn " $line\n"; 1974 if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 1975 $in_comment = 1; 1976 } 1977 if ($line =~ m@/\*@) { 1978 $in_comment = 1; 1979 } 1980 if (!$in_comment && $current_comment ne '') { 1981 $current_comment = ''; 1982 } 1983 $current_comment .= $line . "\n" if ($in_comment); 1984 if ($line =~ m@\*/@) { 1985 $in_comment = 0; 1986 } 1987 } 1988 1989 chomp($current_comment); 1990 return($current_comment); 1991} 1992sub ctx_has_comment { 1993 my ($first_line, $end_line) = @_; 1994 my $cmt = ctx_locate_comment($first_line, $end_line); 1995 1996 ##print "LINE: $rawlines[$end_line - 1 ]\n"; 1997 ##print "CMMT: $cmt\n"; 1998 1999 return ($cmt ne ''); 2000} 2001 2002sub raw_line { 2003 my ($linenr, $cnt) = @_; 2004 2005 my $offset = $linenr - 1; 2006 $cnt++; 2007 2008 my $line; 2009 while ($cnt) { 2010 $line = $rawlines[$offset++]; 2011 next if (defined($line) && $line =~ /^-/); 2012 $cnt--; 2013 } 2014 2015 return $line; 2016} 2017 2018sub get_stat_real { 2019 my ($linenr, $lc) = @_; 2020 2021 my $stat_real = raw_line($linenr, 0); 2022 for (my $count = $linenr + 1; $count <= $lc; $count++) { 2023 $stat_real = $stat_real . "\n" . raw_line($count, 0); 2024 } 2025 2026 return $stat_real; 2027} 2028 2029sub get_stat_here { 2030 my ($linenr, $cnt, $here) = @_; 2031 2032 my $herectx = $here . "\n"; 2033 for (my $n = 0; $n < $cnt; $n++) { 2034 $herectx .= raw_line($linenr, $n) . "\n"; 2035 } 2036 2037 return $herectx; 2038} 2039 2040sub cat_vet { 2041 my ($vet) = @_; 2042 my ($res, $coded); 2043 2044 $res = ''; 2045 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 2046 $res .= $1; 2047 if ($2 ne '') { 2048 $coded = sprintf("^%c", unpack('C', $2) + 64); 2049 $res .= $coded; 2050 } 2051 } 2052 $res =~ s/$/\$/; 2053 2054 return $res; 2055} 2056 2057my $av_preprocessor = 0; 2058my $av_pending; 2059my @av_paren_type; 2060my $av_pend_colon; 2061 2062sub annotate_reset { 2063 $av_preprocessor = 0; 2064 $av_pending = '_'; 2065 @av_paren_type = ('E'); 2066 $av_pend_colon = 'O'; 2067} 2068 2069sub annotate_values { 2070 my ($stream, $type) = @_; 2071 2072 my $res; 2073 my $var = '_' x length($stream); 2074 my $cur = $stream; 2075 2076 print "$stream\n" if ($dbg_values > 1); 2077 2078 while (length($cur)) { 2079 @av_paren_type = ('E') if ($#av_paren_type < 0); 2080 print " <" . join('', @av_paren_type) . 2081 "> <$type> <$av_pending>" if ($dbg_values > 1); 2082 if ($cur =~ /^(\s+)/o) { 2083 print "WS($1)\n" if ($dbg_values > 1); 2084 if ($1 =~ /\n/ && $av_preprocessor) { 2085 $type = pop(@av_paren_type); 2086 $av_preprocessor = 0; 2087 } 2088 2089 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 2090 print "CAST($1)\n" if ($dbg_values > 1); 2091 push(@av_paren_type, $type); 2092 $type = 'c'; 2093 2094 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 2095 print "DECLARE($1)\n" if ($dbg_values > 1); 2096 $type = 'T'; 2097 2098 } elsif ($cur =~ /^($Modifier)\s*/) { 2099 print "MODIFIER($1)\n" if ($dbg_values > 1); 2100 $type = 'T'; 2101 2102 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 2103 print "DEFINE($1,$2)\n" if ($dbg_values > 1); 2104 $av_preprocessor = 1; 2105 push(@av_paren_type, $type); 2106 if ($2 ne '') { 2107 $av_pending = 'N'; 2108 } 2109 $type = 'E'; 2110 2111 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 2112 print "UNDEF($1)\n" if ($dbg_values > 1); 2113 $av_preprocessor = 1; 2114 push(@av_paren_type, $type); 2115 2116 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 2117 print "PRE_START($1)\n" if ($dbg_values > 1); 2118 $av_preprocessor = 1; 2119 2120 push(@av_paren_type, $type); 2121 push(@av_paren_type, $type); 2122 $type = 'E'; 2123 2124 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 2125 print "PRE_RESTART($1)\n" if ($dbg_values > 1); 2126 $av_preprocessor = 1; 2127 2128 push(@av_paren_type, $av_paren_type[$#av_paren_type]); 2129 2130 $type = 'E'; 2131 2132 } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 2133 print "PRE_END($1)\n" if ($dbg_values > 1); 2134 2135 $av_preprocessor = 1; 2136 2137 # Assume all arms of the conditional end as this 2138 # one does, and continue as if the #endif was not here. 2139 pop(@av_paren_type); 2140 push(@av_paren_type, $type); 2141 $type = 'E'; 2142 2143 } elsif ($cur =~ /^(\\\n)/o) { 2144 print "PRECONT($1)\n" if ($dbg_values > 1); 2145 2146 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 2147 print "ATTR($1)\n" if ($dbg_values > 1); 2148 $av_pending = $type; 2149 $type = 'N'; 2150 2151 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 2152 print "SIZEOF($1)\n" if ($dbg_values > 1); 2153 if (defined $2) { 2154 $av_pending = 'V'; 2155 } 2156 $type = 'N'; 2157 2158 } elsif ($cur =~ /^(if|while|for)\b/o) { 2159 print "COND($1)\n" if ($dbg_values > 1); 2160 $av_pending = 'E'; 2161 $type = 'N'; 2162 2163 } elsif ($cur =~/^(case)/o) { 2164 print "CASE($1)\n" if ($dbg_values > 1); 2165 $av_pend_colon = 'C'; 2166 $type = 'N'; 2167 2168 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 2169 print "KEYWORD($1)\n" if ($dbg_values > 1); 2170 $type = 'N'; 2171 2172 } elsif ($cur =~ /^(\()/o) { 2173 print "PAREN('$1')\n" if ($dbg_values > 1); 2174 push(@av_paren_type, $av_pending); 2175 $av_pending = '_'; 2176 $type = 'N'; 2177 2178 } elsif ($cur =~ /^(\))/o) { 2179 my $new_type = pop(@av_paren_type); 2180 if ($new_type ne '_') { 2181 $type = $new_type; 2182 print "PAREN('$1') -> $type\n" 2183 if ($dbg_values > 1); 2184 } else { 2185 print "PAREN('$1')\n" if ($dbg_values > 1); 2186 } 2187 2188 } elsif ($cur =~ /^($Ident)\s*\(/o) { 2189 print "FUNC($1)\n" if ($dbg_values > 1); 2190 $type = 'V'; 2191 $av_pending = 'V'; 2192 2193 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 2194 if (defined $2 && $type eq 'C' || $type eq 'T') { 2195 $av_pend_colon = 'B'; 2196 } elsif ($type eq 'E') { 2197 $av_pend_colon = 'L'; 2198 } 2199 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 2200 $type = 'V'; 2201 2202 } elsif ($cur =~ /^($Ident|$Constant)/o) { 2203 print "IDENT($1)\n" if ($dbg_values > 1); 2204 $type = 'V'; 2205 2206 } elsif ($cur =~ /^($Assignment)/o) { 2207 print "ASSIGN($1)\n" if ($dbg_values > 1); 2208 $type = 'N'; 2209 2210 } elsif ($cur =~/^(;|{|})/) { 2211 print "END($1)\n" if ($dbg_values > 1); 2212 $type = 'E'; 2213 $av_pend_colon = 'O'; 2214 2215 } elsif ($cur =~/^(,)/) { 2216 print "COMMA($1)\n" if ($dbg_values > 1); 2217 $type = 'C'; 2218 2219 } elsif ($cur =~ /^(\?)/o) { 2220 print "QUESTION($1)\n" if ($dbg_values > 1); 2221 $type = 'N'; 2222 2223 } elsif ($cur =~ /^(:)/o) { 2224 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 2225 2226 substr($var, length($res), 1, $av_pend_colon); 2227 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 2228 $type = 'E'; 2229 } else { 2230 $type = 'N'; 2231 } 2232 $av_pend_colon = 'O'; 2233 2234 } elsif ($cur =~ /^(\[)/o) { 2235 print "CLOSE($1)\n" if ($dbg_values > 1); 2236 $type = 'N'; 2237 2238 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 2239 my $variant; 2240 2241 print "OPV($1)\n" if ($dbg_values > 1); 2242 if ($type eq 'V') { 2243 $variant = 'B'; 2244 } else { 2245 $variant = 'U'; 2246 } 2247 2248 substr($var, length($res), 1, $variant); 2249 $type = 'N'; 2250 2251 } elsif ($cur =~ /^($Operators)/o) { 2252 print "OP($1)\n" if ($dbg_values > 1); 2253 if ($1 ne '++' && $1 ne '--') { 2254 $type = 'N'; 2255 } 2256 2257 } elsif ($cur =~ /(^.)/o) { 2258 print "C($1)\n" if ($dbg_values > 1); 2259 } 2260 if (defined $1) { 2261 $cur = substr($cur, length($1)); 2262 $res .= $type x length($1); 2263 } 2264 } 2265 2266 return ($res, $var); 2267} 2268 2269sub possible { 2270 my ($possible, $line) = @_; 2271 my $notPermitted = qr{(?: 2272 ^(?: 2273 $Modifier| 2274 $Storage| 2275 $Type| 2276 DEFINE_\S+ 2277 )$| 2278 ^(?: 2279 goto| 2280 return| 2281 case| 2282 else| 2283 asm|__asm__| 2284 do| 2285 \#| 2286 \#\#| 2287 )(?:\s|$)| 2288 ^(?:typedef|struct|enum)\b 2289 )}x; 2290 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 2291 if ($possible !~ $notPermitted) { 2292 # Check for modifiers. 2293 $possible =~ s/\s*$Storage\s*//g; 2294 $possible =~ s/\s*$Sparse\s*//g; 2295 if ($possible =~ /^\s*$/) { 2296 2297 } elsif ($possible =~ /\s/) { 2298 $possible =~ s/\s*$Type\s*//g; 2299 for my $modifier (split(' ', $possible)) { 2300 if ($modifier !~ $notPermitted) { 2301 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2302 push(@modifierListFile, $modifier); 2303 } 2304 } 2305 2306 } else { 2307 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2308 push(@typeListFile, $possible); 2309 } 2310 build_types(); 2311 } else { 2312 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 2313 } 2314} 2315 2316my $prefix = ''; 2317 2318sub show_type { 2319 my ($type) = @_; 2320 2321 $type =~ tr/[a-z]/[A-Z]/; 2322 2323 return defined $use_type{$type} if (scalar keys %use_type > 0); 2324 2325 return !defined $ignore_type{$type}; 2326} 2327 2328sub report { 2329 my ($level, $type, $msg) = @_; 2330 2331 if (!show_type($type) || 2332 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2333 return 0; 2334 } 2335 my $output = ''; 2336 if ($color) { 2337 if ($level eq 'ERROR') { 2338 $output .= RED; 2339 } elsif ($level eq 'WARNING') { 2340 $output .= YELLOW; 2341 } else { 2342 $output .= GREEN; 2343 } 2344 } 2345 $output .= $prefix . $level . ':'; 2346 if ($show_types) { 2347 $output .= BLUE if ($color); 2348 $output .= "$type:"; 2349 } 2350 $output .= RESET if ($color); 2351 $output .= ' ' . $msg . "\n"; 2352 2353 if ($showfile) { 2354 my @lines = split("\n", $output, -1); 2355 splice(@lines, 1, 1); 2356 $output = join("\n", @lines); 2357 } 2358 2359 if ($terse) { 2360 $output = (split('\n', $output))[0] . "\n"; 2361 } 2362 2363 if ($verbose && exists($verbose_messages{$type}) && 2364 !exists($verbose_emitted{$type})) { 2365 $output .= $verbose_messages{$type} . "\n\n"; 2366 $verbose_emitted{$type} = 1; 2367 } 2368 2369 push(our @report, $output); 2370 2371 return 1; 2372} 2373 2374sub report_dump { 2375 our @report; 2376} 2377 2378sub fixup_current_range { 2379 my ($lineRef, $offset, $length) = @_; 2380 2381 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2382 my $o = $1; 2383 my $l = $2; 2384 my $no = $o + $offset; 2385 my $nl = $l + $length; 2386 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2387 } 2388} 2389 2390sub fix_inserted_deleted_lines { 2391 my ($linesRef, $insertedRef, $deletedRef) = @_; 2392 2393 my $range_last_linenr = 0; 2394 my $delta_offset = 0; 2395 2396 my $old_linenr = 0; 2397 my $new_linenr = 0; 2398 2399 my $next_insert = 0; 2400 my $next_delete = 0; 2401 2402 my @lines = (); 2403 2404 my $inserted = @{$insertedRef}[$next_insert++]; 2405 my $deleted = @{$deletedRef}[$next_delete++]; 2406 2407 foreach my $old_line (@{$linesRef}) { 2408 my $save_line = 1; 2409 my $line = $old_line; #don't modify the array 2410 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2411 $delta_offset = 0; 2412 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2413 $range_last_linenr = $new_linenr; 2414 fixup_current_range(\$line, $delta_offset, 0); 2415 } 2416 2417 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2418 $deleted = @{$deletedRef}[$next_delete++]; 2419 $save_line = 0; 2420 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2421 } 2422 2423 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2424 push(@lines, ${$inserted}{'LINE'}); 2425 $inserted = @{$insertedRef}[$next_insert++]; 2426 $new_linenr++; 2427 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2428 } 2429 2430 if ($save_line) { 2431 push(@lines, $line); 2432 $new_linenr++; 2433 } 2434 2435 $old_linenr++; 2436 } 2437 2438 return @lines; 2439} 2440 2441sub fix_insert_line { 2442 my ($linenr, $line) = @_; 2443 2444 my $inserted = { 2445 LINENR => $linenr, 2446 LINE => $line, 2447 }; 2448 push(@fixed_inserted, $inserted); 2449} 2450 2451sub fix_delete_line { 2452 my ($linenr, $line) = @_; 2453 2454 my $deleted = { 2455 LINENR => $linenr, 2456 LINE => $line, 2457 }; 2458 2459 push(@fixed_deleted, $deleted); 2460} 2461 2462sub ERROR { 2463 my ($type, $msg) = @_; 2464 2465 if (report("ERROR", $type, $msg)) { 2466 our $clean = 0; 2467 our $cnt_error++; 2468 return 1; 2469 } 2470 return 0; 2471} 2472sub WARN { 2473 my ($type, $msg) = @_; 2474 2475 if (report("WARNING", $type, $msg)) { 2476 our $clean = 0; 2477 our $cnt_warn++; 2478 return 1; 2479 } 2480 return 0; 2481} 2482sub CHK { 2483 my ($type, $msg) = @_; 2484 2485 if ($check && report("CHECK", $type, $msg)) { 2486 our $clean = 0; 2487 our $cnt_chk++; 2488 return 1; 2489 } 2490 return 0; 2491} 2492 2493sub check_absolute_file { 2494 my ($absolute, $herecurr) = @_; 2495 my $file = $absolute; 2496 2497 ##print "absolute<$absolute>\n"; 2498 2499 # See if any suffix of this path is a path within the tree. 2500 while ($file =~ s@^[^/]*/@@) { 2501 if (-f "$root/$file") { 2502 ##print "file<$file>\n"; 2503 last; 2504 } 2505 } 2506 if (! -f _) { 2507 return 0; 2508 } 2509 2510 # It is, so see if the prefix is acceptable. 2511 my $prefix = $absolute; 2512 substr($prefix, -length($file)) = ''; 2513 2514 ##print "prefix<$prefix>\n"; 2515 if ($prefix ne ".../") { 2516 WARN("USE_RELATIVE_PATH", 2517 "use relative pathname instead of absolute in changelog text\n" . $herecurr); 2518 } 2519} 2520 2521sub trim { 2522 my ($string) = @_; 2523 2524 $string =~ s/^\s+|\s+$//g; 2525 2526 return $string; 2527} 2528 2529sub ltrim { 2530 my ($string) = @_; 2531 2532 $string =~ s/^\s+//; 2533 2534 return $string; 2535} 2536 2537sub rtrim { 2538 my ($string) = @_; 2539 2540 $string =~ s/\s+$//; 2541 2542 return $string; 2543} 2544 2545sub string_find_replace { 2546 my ($string, $find, $replace) = @_; 2547 2548 $string =~ s/$find/$replace/g; 2549 2550 return $string; 2551} 2552 2553sub tabify { 2554 my ($leading) = @_; 2555 2556 my $source_indent = $tabsize; 2557 my $max_spaces_before_tab = $source_indent - 1; 2558 my $spaces_to_tab = " " x $source_indent; 2559 2560 #convert leading spaces to tabs 2561 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 2562 #Remove spaces before a tab 2563 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 2564 2565 return "$leading"; 2566} 2567 2568sub pos_last_openparen { 2569 my ($line) = @_; 2570 2571 my $pos = 0; 2572 2573 my $opens = $line =~ tr/\(/\(/; 2574 my $closes = $line =~ tr/\)/\)/; 2575 2576 my $last_openparen = 0; 2577 2578 if (($opens == 0) || ($closes >= $opens)) { 2579 return -1; 2580 } 2581 2582 my $len = length($line); 2583 2584 for ($pos = 0; $pos < $len; $pos++) { 2585 my $string = substr($line, $pos); 2586 if ($string =~ /^($FuncArg|$balanced_parens)/) { 2587 $pos += length($1) - 1; 2588 } elsif (substr($line, $pos, 1) eq '(') { 2589 $last_openparen = $pos; 2590 } elsif (index($string, '(') == -1) { 2591 last; 2592 } 2593 } 2594 2595 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2596} 2597 2598sub get_raw_comment { 2599 my ($line, $rawline) = @_; 2600 my $comment = ''; 2601 2602 for my $i (0 .. (length($line) - 1)) { 2603 if (substr($line, $i, 1) eq "$;") { 2604 $comment .= substr($rawline, $i, 1); 2605 } 2606 } 2607 2608 return $comment; 2609} 2610 2611sub exclude_global_initialisers { 2612 my ($realfile) = @_; 2613 2614 # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c). 2615 return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ || 2616 $realfile =~ m@^samples/bpf/.*_kern\.c$@ || 2617 $realfile =~ m@/bpf/.*\.bpf\.c$@; 2618} 2619 2620sub process { 2621 my $filename = shift; 2622 2623 my $linenr=0; 2624 my $prevline=""; 2625 my $prevrawline=""; 2626 my $stashline=""; 2627 my $stashrawline=""; 2628 2629 my $length; 2630 my $indent; 2631 my $previndent=0; 2632 my $stashindent=0; 2633 2634 our $clean = 1; 2635 my $signoff = 0; 2636 my $author = ''; 2637 my $authorsignoff = 0; 2638 my $author_sob = ''; 2639 my $is_patch = 0; 2640 my $is_binding_patch = -1; 2641 my $in_header_lines = $file ? 0 : 1; 2642 my $in_commit_log = 0; #Scanning lines before patch 2643 my $has_patch_separator = 0; #Found a --- line 2644 my $has_commit_log = 0; #Encountered lines before patch 2645 my $commit_log_lines = 0; #Number of commit log lines 2646 my $commit_log_possible_stack_dump = 0; 2647 my $commit_log_long_line = 0; 2648 my $commit_log_has_diff = 0; 2649 my $reported_maintainer_file = 0; 2650 my $non_utf8_charset = 0; 2651 2652 my $last_git_commit_id_linenr = -1; 2653 2654 my $last_blank_line = 0; 2655 my $last_coalesced_string_linenr = -1; 2656 2657 our @report = (); 2658 our $cnt_lines = 0; 2659 our $cnt_error = 0; 2660 our $cnt_warn = 0; 2661 our $cnt_chk = 0; 2662 2663 # Trace the real file/line as we go. 2664 my $realfile = ''; 2665 my $realline = 0; 2666 my $realcnt = 0; 2667 my $here = ''; 2668 my $context_function; #undef'd unless there's a known function 2669 my $in_comment = 0; 2670 my $comment_edge = 0; 2671 my $first_line = 0; 2672 my $p1_prefix = ''; 2673 2674 my $prev_values = 'E'; 2675 2676 # suppression flags 2677 my %suppress_ifbraces; 2678 my %suppress_whiletrailers; 2679 my %suppress_export; 2680 my $suppress_statement = 0; 2681 2682 my %signatures = (); 2683 2684 # Pre-scan the patch sanitizing the lines. 2685 # Pre-scan the patch looking for any __setup documentation. 2686 # 2687 my @setup_docs = (); 2688 my $setup_docs = 0; 2689 2690 my $camelcase_file_seeded = 0; 2691 2692 my $checklicenseline = 1; 2693 2694 sanitise_line_reset(); 2695 my $line; 2696 foreach my $rawline (@rawlines) { 2697 $linenr++; 2698 $line = $rawline; 2699 2700 push(@fixed, $rawline) if ($fix); 2701 2702 if ($rawline=~/^\+\+\+\s+(\S+)/) { 2703 $setup_docs = 0; 2704 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { 2705 $setup_docs = 1; 2706 } 2707 #next; 2708 } 2709 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2710 $realline=$1-1; 2711 if (defined $2) { 2712 $realcnt=$3+1; 2713 } else { 2714 $realcnt=1+1; 2715 } 2716 $in_comment = 0; 2717 2718 # Guestimate if this is a continuing comment. Run 2719 # the context looking for a comment "edge". If this 2720 # edge is a close comment then we must be in a comment 2721 # at context start. 2722 my $edge; 2723 my $cnt = $realcnt; 2724 for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 2725 next if (defined $rawlines[$ln - 1] && 2726 $rawlines[$ln - 1] =~ /^-/); 2727 $cnt--; 2728 #print "RAW<$rawlines[$ln - 1]>\n"; 2729 last if (!defined $rawlines[$ln - 1]); 2730 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2731 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2732 ($edge) = $1; 2733 last; 2734 } 2735 } 2736 if (defined $edge && $edge eq '*/') { 2737 $in_comment = 1; 2738 } 2739 2740 # Guestimate if this is a continuing comment. If this 2741 # is the start of a diff block and this line starts 2742 # ' *' then it is very likely a comment. 2743 if (!defined $edge && 2744 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2745 { 2746 $in_comment = 1; 2747 } 2748 2749 ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2750 sanitise_line_reset($in_comment); 2751 2752 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2753 # Standardise the strings and chars within the input to 2754 # simplify matching -- only bother with positive lines. 2755 $line = sanitise_line($rawline); 2756 } 2757 push(@lines, $line); 2758 2759 if ($realcnt > 1) { 2760 $realcnt-- if ($line =~ /^(?:\+| |$)/); 2761 } else { 2762 $realcnt = 0; 2763 } 2764 2765 #print "==>$rawline\n"; 2766 #print "-->$line\n"; 2767 2768 if ($setup_docs && $line =~ /^\+/) { 2769 push(@setup_docs, $line); 2770 } 2771 } 2772 2773 $prefix = ''; 2774 2775 $realcnt = 0; 2776 $linenr = 0; 2777 $fixlinenr = -1; 2778 foreach my $line (@lines) { 2779 $linenr++; 2780 $fixlinenr++; 2781 my $sline = $line; #copy of $line 2782 $sline =~ s/$;/ /g; #with comments as spaces 2783 2784 my $rawline = $rawlines[$linenr - 1]; 2785 my $raw_comment = get_raw_comment($line, $rawline); 2786 2787# check if it's a mode change, rename or start of a patch 2788 if (!$in_commit_log && 2789 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 2790 ($line =~ /^rename (?:from|to) \S+\s*$/ || 2791 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 2792 $is_patch = 1; 2793 } 2794 2795#extract the line range in the file after the patch is applied 2796 if (!$in_commit_log && 2797 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 2798 my $context = $4; 2799 $is_patch = 1; 2800 $first_line = $linenr + 1; 2801 $realline=$1-1; 2802 if (defined $2) { 2803 $realcnt=$3+1; 2804 } else { 2805 $realcnt=1+1; 2806 } 2807 annotate_reset(); 2808 $prev_values = 'E'; 2809 2810 %suppress_ifbraces = (); 2811 %suppress_whiletrailers = (); 2812 %suppress_export = (); 2813 $suppress_statement = 0; 2814 if ($context =~ /\b(\w+)\s*\(/) { 2815 $context_function = $1; 2816 } else { 2817 undef $context_function; 2818 } 2819 next; 2820 2821# track the line number as we move through the hunk, note that 2822# new versions of GNU diff omit the leading space on completely 2823# blank context lines so we need to count that too. 2824 } elsif ($line =~ /^( |\+|$)/) { 2825 $realline++; 2826 $realcnt-- if ($realcnt != 0); 2827 2828 # Measure the line length and indent. 2829 ($length, $indent) = line_stats($rawline); 2830 2831 # Track the previous line. 2832 ($prevline, $stashline) = ($stashline, $line); 2833 ($previndent, $stashindent) = ($stashindent, $indent); 2834 ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2835 2836 #warn "line<$line>\n"; 2837 2838 } elsif ($realcnt == 1) { 2839 $realcnt--; 2840 } 2841 2842 my $hunk_line = ($realcnt != 0); 2843 2844 $here = "#$linenr: " if (!$file); 2845 $here = "#$realline: " if ($file); 2846 2847 my $found_file = 0; 2848 # extract the filename as it passes 2849 if ($line =~ /^diff --git.*?(\S+)$/) { 2850 $realfile = $1; 2851 $realfile =~ s@^([^/]*)/@@ if (!$file); 2852 $in_commit_log = 0; 2853 $found_file = 1; 2854 } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2855 $realfile = $1; 2856 $realfile =~ s@^([^/]*)/@@ if (!$file); 2857 $in_commit_log = 0; 2858 2859 $p1_prefix = $1; 2860 if (!$file && $tree && $p1_prefix ne '' && 2861 -e "$root/$p1_prefix") { 2862 WARN("PATCH_PREFIX", 2863 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 2864 } 2865 2866 if ($realfile =~ m@^include/asm/@) { 2867 ERROR("MODIFIED_INCLUDE_ASM", 2868 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2869 } 2870 $found_file = 1; 2871 } 2872 2873#make up the handle for any error we report on this line 2874 if ($showfile) { 2875 $prefix = "$realfile:$realline: " 2876 } elsif ($emacs) { 2877 if ($file) { 2878 $prefix = "$filename:$realline: "; 2879 } else { 2880 $prefix = "$filename:$linenr: "; 2881 } 2882 } 2883 2884 if ($found_file) { 2885 if (is_maintained_obsolete($realfile)) { 2886 WARN("OBSOLETE", 2887 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 2888 } 2889 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 2890 $check = 1; 2891 } else { 2892 $check = $check_orig; 2893 } 2894 $checklicenseline = 1; 2895 2896 if ($realfile !~ /^MAINTAINERS/) { 2897 my $last_binding_patch = $is_binding_patch; 2898 2899 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2900 2901 if (($last_binding_patch != -1) && 2902 ($last_binding_patch ^ $is_binding_patch)) { 2903 WARN("DT_SPLIT_BINDING_PATCH", 2904 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); 2905 } 2906 } 2907 2908 next; 2909 } 2910 2911 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 2912 2913 my $hereline = "$here\n$rawline\n"; 2914 my $herecurr = "$here\n$rawline\n"; 2915 my $hereprev = "$here\n$prevrawline\n$rawline\n"; 2916 2917 $cnt_lines++ if ($realcnt != 0); 2918 2919# Verify the existence of a commit log if appropriate 2920# 2 is used because a $signature is counted in $commit_log_lines 2921 if ($in_commit_log) { 2922 if ($line !~ /^\s*$/) { 2923 $commit_log_lines++; #could be a $signature 2924 } 2925 } elsif ($has_commit_log && $commit_log_lines < 2) { 2926 WARN("COMMIT_MESSAGE", 2927 "Missing commit description - Add an appropriate one\n"); 2928 $commit_log_lines = 2; #warn only once 2929 } 2930 2931# Check if the commit log has what seems like a diff which can confuse patch 2932 if ($in_commit_log && !$commit_log_has_diff && 2933 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && 2934 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || 2935 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2936 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2937 ERROR("DIFF_IN_COMMIT_MSG", 2938 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2939 $commit_log_has_diff = 1; 2940 } 2941 2942# Check for incorrect file permissions 2943 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 2944 my $permhere = $here . "FILE: $realfile\n"; 2945 if ($realfile !~ m@scripts/@ && 2946 $realfile !~ /\.(py|pl|awk|sh)$/) { 2947 ERROR("EXECUTE_PERMISSIONS", 2948 "do not set execute permissions for source files\n" . $permhere); 2949 } 2950 } 2951 2952# Check the patch for a From: 2953 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2954 $author = $1; 2955 my $curline = $linenr; 2956 while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { 2957 $author .= $1; 2958 } 2959 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2960 $author =~ s/"//g; 2961 $author = reformat_email($author); 2962 } 2963 2964# Check the patch for a signoff: 2965 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 2966 $signoff++; 2967 $in_commit_log = 0; 2968 if ($author ne '' && $authorsignoff != 1) { 2969 if (same_email_addresses($1, $author)) { 2970 $authorsignoff = 1; 2971 } else { 2972 my $ctx = $1; 2973 my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); 2974 my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); 2975 2976 if (lc $email_address eq lc $author_address && $email_name eq $author_name) { 2977 $author_sob = $ctx; 2978 $authorsignoff = 2; 2979 } elsif (lc $email_address eq lc $author_address) { 2980 $author_sob = $ctx; 2981 $authorsignoff = 3; 2982 } elsif ($email_name eq $author_name) { 2983 $author_sob = $ctx; 2984 $authorsignoff = 4; 2985 2986 my $address1 = $email_address; 2987 my $address2 = $author_address; 2988 2989 if ($address1 =~ /(\S+)\+\S+(\@.*)/) { 2990 $address1 = "$1$2"; 2991 } 2992 if ($address2 =~ /(\S+)\+\S+(\@.*)/) { 2993 $address2 = "$1$2"; 2994 } 2995 if ($address1 eq $address2) { 2996 $authorsignoff = 5; 2997 } 2998 } 2999 } 3000 } 3001 } 3002 3003# Check for patch separator 3004 if ($line =~ /^---$/) { 3005 $has_patch_separator = 1; 3006 $in_commit_log = 0; 3007 } 3008 3009# Check if MAINTAINERS is being updated. If so, there's probably no need to 3010# emit the "does MAINTAINERS need updating?" message on file add/move/delete 3011 if ($line =~ /^\s*MAINTAINERS\s*\|/) { 3012 $reported_maintainer_file = 1; 3013 } 3014 3015# Check signature styles 3016 if (!$in_header_lines && 3017 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 3018 my $space_before = $1; 3019 my $sign_off = $2; 3020 my $space_after = $3; 3021 my $email = $4; 3022 my $ucfirst_sign_off = ucfirst(lc($sign_off)); 3023 3024 if ($sign_off !~ /$signature_tags/) { 3025 my $suggested_signature = find_standard_signature($sign_off); 3026 if ($suggested_signature eq "") { 3027 WARN("BAD_SIGN_OFF", 3028 "Non-standard signature: $sign_off\n" . $herecurr); 3029 } else { 3030 if (WARN("BAD_SIGN_OFF", 3031 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) && 3032 $fix) { 3033 $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/; 3034 } 3035 } 3036 } 3037 if (defined $space_before && $space_before ne "") { 3038 if (WARN("BAD_SIGN_OFF", 3039 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 3040 $fix) { 3041 $fixed[$fixlinenr] = 3042 "$ucfirst_sign_off $email"; 3043 } 3044 } 3045 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 3046 if (WARN("BAD_SIGN_OFF", 3047 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 3048 $fix) { 3049 $fixed[$fixlinenr] = 3050 "$ucfirst_sign_off $email"; 3051 } 3052 3053 } 3054 if (!defined $space_after || $space_after ne " ") { 3055 if (WARN("BAD_SIGN_OFF", 3056 "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 3057 $fix) { 3058 $fixed[$fixlinenr] = 3059 "$ucfirst_sign_off $email"; 3060 } 3061 } 3062 3063 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 3064 my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); 3065 if ($suggested_email eq "") { 3066 ERROR("BAD_SIGN_OFF", 3067 "Unrecognized email address: '$email'\n" . $herecurr); 3068 } else { 3069 my $dequoted = $suggested_email; 3070 $dequoted =~ s/^"//; 3071 $dequoted =~ s/" </ </; 3072 # Don't force email to have quotes 3073 # Allow just an angle bracketed address 3074 if (!same_email_addresses($email, $suggested_email)) { 3075 if (WARN("BAD_SIGN_OFF", 3076 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) && 3077 $fix) { 3078 $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/; 3079 } 3080 } 3081 3082 # Address part shouldn't have comments 3083 my $stripped_address = $email_address; 3084 $stripped_address =~ s/\([^\(\)]*\)//g; 3085 if ($email_address ne $stripped_address) { 3086 if (WARN("BAD_SIGN_OFF", 3087 "address part of email should not have comments: '$email_address'\n" . $herecurr) && 3088 $fix) { 3089 $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/; 3090 } 3091 } 3092 3093 # Only one name comment should be allowed 3094 my $comment_count = () = $name_comment =~ /\([^\)]+\)/g; 3095 if ($comment_count > 1) { 3096 WARN("BAD_SIGN_OFF", 3097 "Use a single name comment in email: '$email'\n" . $herecurr); 3098 } 3099 3100 3101 # stable@vger.kernel.org or stable@kernel.org shouldn't 3102 # have an email name. In addition comments should strictly 3103 # begin with a # 3104 if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) { 3105 if (($comment ne "" && $comment !~ /^#.+/) || 3106 ($email_name ne "")) { 3107 my $cur_name = $email_name; 3108 my $new_comment = $comment; 3109 $cur_name =~ s/[a-zA-Z\s\-\"]+//g; 3110 3111 # Remove brackets enclosing comment text 3112 # and # from start of comments to get comment text 3113 $new_comment =~ s/^\((.*)\)$/$1/; 3114 $new_comment =~ s/^\[(.*)\]$/$1/; 3115 $new_comment =~ s/^[\s\#]+|\s+$//g; 3116 3117 $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment); 3118 $new_comment = " # $new_comment" if ($new_comment ne ""); 3119 my $new_email = "$email_address$new_comment"; 3120 3121 if (WARN("BAD_STABLE_ADDRESS_STYLE", 3122 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) && 3123 $fix) { 3124 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 3125 } 3126 } 3127 } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) { 3128 my $new_comment = $comment; 3129 3130 # Extract comment text from within brackets or 3131 # c89 style /*...*/ comments 3132 $new_comment =~ s/^\[(.*)\]$/$1/; 3133 $new_comment =~ s/^\/\*(.*)\*\/$/$1/; 3134 3135 $new_comment = trim($new_comment); 3136 $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo 3137 $new_comment = "($new_comment)" if ($new_comment ne ""); 3138 my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment); 3139 3140 if (WARN("BAD_SIGN_OFF", 3141 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) && 3142 $fix) { 3143 $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 3144 } 3145 } 3146 } 3147 3148# Check for duplicate signatures 3149 my $sig_nospace = $line; 3150 $sig_nospace =~ s/\s//g; 3151 $sig_nospace = lc($sig_nospace); 3152 if (defined $signatures{$sig_nospace}) { 3153 WARN("BAD_SIGN_OFF", 3154 "Duplicate signature\n" . $herecurr); 3155 } else { 3156 $signatures{$sig_nospace} = 1; 3157 } 3158 3159# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 3160 if ($sign_off =~ /^co-developed-by:$/i) { 3161 if ($email eq $author) { 3162 WARN("BAD_SIGN_OFF", 3163 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . $herecurr); 3164 } 3165 if (!defined $lines[$linenr]) { 3166 WARN("BAD_SIGN_OFF", 3167 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr); 3168 } elsif ($rawlines[$linenr] !~ /^signed-off-by:\s*(.*)/i) { 3169 WARN("BAD_SIGN_OFF", 3170 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr . $rawlines[$linenr] . "\n"); 3171 } elsif ($1 ne $email) { 3172 WARN("BAD_SIGN_OFF", 3173 "Co-developed-by and Signed-off-by: name/email do not match\n" . $herecurr . $rawlines[$linenr] . "\n"); 3174 } 3175 } 3176 3177# check if Reported-by: is followed by a Closes: tag 3178 if ($sign_off =~ /^reported(?:|-and-tested)-by:$/i) { 3179 if (!defined $lines[$linenr]) { 3180 WARN("BAD_REPORTED_BY_LINK", 3181 "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . "\n"); 3182 } elsif ($rawlines[$linenr] !~ /^closes:\s*/i) { 3183 WARN("BAD_REPORTED_BY_LINK", 3184 "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . $rawlines[$linenr] . "\n"); 3185 } 3186 } 3187 } 3188 3189 3190# Check Fixes: styles is correct 3191 if (!$in_header_lines && 3192 $line =~ /^\s*fixes:?\s*(?:commit\s*)?[0-9a-f]{5,}\b/i) { 3193 my $orig_commit = ""; 3194 my $id = "0123456789ab"; 3195 my $title = "commit title"; 3196 my $tag_case = 1; 3197 my $tag_space = 1; 3198 my $id_length = 1; 3199 my $id_case = 1; 3200 my $title_has_quotes = 0; 3201 3202 if ($line =~ /(\s*fixes:?)\s+([0-9a-f]{5,})\s+($balanced_parens)/i) { 3203 my $tag = $1; 3204 $orig_commit = $2; 3205 $title = $3; 3206 3207 $tag_case = 0 if $tag eq "Fixes:"; 3208 $tag_space = 0 if ($line =~ /^fixes:? [0-9a-f]{5,} ($balanced_parens)/i); 3209 3210 $id_length = 0 if ($orig_commit =~ /^[0-9a-f]{12}$/i); 3211 $id_case = 0 if ($orig_commit !~ /[A-F]/); 3212 3213 # Always strip leading/trailing parens then double quotes if existing 3214 $title = substr($title, 1, -1); 3215 if ($title =~ /^".*"$/) { 3216 $title = substr($title, 1, -1); 3217 $title_has_quotes = 1; 3218 } 3219 } 3220 3221 my ($cid, $ctitle) = git_commit_info($orig_commit, $id, 3222 $title); 3223 3224 if ($ctitle ne $title || $tag_case || $tag_space || 3225 $id_length || $id_case || !$title_has_quotes) { 3226 if (WARN("BAD_FIXES_TAG", 3227 "Please use correct Fixes: style 'Fixes: <12 chars of sha1> (\"<title line>\")' - ie: 'Fixes: $cid (\"$ctitle\")'\n" . $herecurr) && 3228 $fix) { 3229 $fixed[$fixlinenr] = "Fixes: $cid (\"$ctitle\")"; 3230 } 3231 } 3232 } 3233 3234# Check email subject for common tools that don't need to be mentioned 3235 if ($in_header_lines && 3236 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 3237 WARN("EMAIL_SUBJECT", 3238 "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 3239 } 3240 3241# Check for Gerrit Change-Ids not in any patch context 3242 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 3243 if (ERROR("GERRIT_CHANGE_ID", 3244 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) && 3245 $fix) { 3246 fix_delete_line($fixlinenr, $rawline); 3247 } 3248 } 3249 3250# Check if the commit log is in a possible stack dump 3251 if ($in_commit_log && !$commit_log_possible_stack_dump && 3252 ($line =~ /^\s*(?:WARNING:|BUG:)/ || 3253 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 3254 # timestamp 3255 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 3256 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 3257 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 3258 # stack dump address styles 3259 $commit_log_possible_stack_dump = 1; 3260 } 3261 3262# Check for line lengths > 75 in commit log, warn once 3263 if ($in_commit_log && !$commit_log_long_line && 3264 length($line) > 75 && 3265 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 3266 # file delta changes 3267 $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\.\-\+]+:/ || 3268 # filename then : 3269 $line =~ /^\s*(?:Fixes:|$link_tags_search|$signature_tags)/i || 3270 # A Fixes:, link or signature tag line 3271 $commit_log_possible_stack_dump)) { 3272 WARN("COMMIT_LOG_LONG_LINE", 3273 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 3274 $commit_log_long_line = 1; 3275 } 3276 3277# Reset possible stack dump if a blank line is found 3278 if ($in_commit_log && $commit_log_possible_stack_dump && 3279 $line =~ /^\s*$/) { 3280 $commit_log_possible_stack_dump = 0; 3281 } 3282 3283# Check for odd tags before a URI/URL 3284 if ($in_commit_log && 3285 $line =~ /^\s*(\w+:)\s*http/ && $1 !~ /^$link_tags_search$/) { 3286 if ($1 =~ /^v(?:ersion)?\d+/i) { 3287 WARN("COMMIT_LOG_VERSIONING", 3288 "Patch version information should be after the --- line\n" . $herecurr); 3289 } else { 3290 WARN("COMMIT_LOG_USE_LINK", 3291 "Unknown link reference '$1', use $link_tags_print instead\n" . $herecurr); 3292 } 3293 } 3294 3295# Check for misuse of the link tags 3296 if ($in_commit_log && 3297 $line =~ /^\s*(\w+:)\s*(\S+)/) { 3298 my $tag = $1; 3299 my $value = $2; 3300 if ($tag =~ /^$link_tags_search$/ && $value !~ m{^https?://}) { 3301 WARN("COMMIT_LOG_WRONG_LINK", 3302 "'$tag' should be followed by a public http(s) link\n" . $herecurr); 3303 } 3304 } 3305 3306# Check for lines starting with a # 3307 if ($in_commit_log && $line =~ /^#/) { 3308 if (WARN("COMMIT_COMMENT_SYMBOL", 3309 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) && 3310 $fix) { 3311 $fixed[$fixlinenr] =~ s/^/ /; 3312 } 3313 } 3314 3315# Check for git id commit length and improperly formed commit descriptions 3316# A correctly formed commit description is: 3317# commit <SHA-1 hash length 12+ chars> ("Complete commit subject") 3318# with the commit subject '("' prefix and '")' suffix 3319# This is a fairly compilicated block as it tests for what appears to be 3320# bare SHA-1 hash with minimum length of 5. It also avoids several types of 3321# possible SHA-1 matches. 3322# A commit match can span multiple lines so this block attempts to find a 3323# complete typical commit on a maximum of 3 lines 3324 if ($perl_version_ok && 3325 $in_commit_log && !$commit_log_possible_stack_dump && 3326 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 3327 $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 3328 (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 3329 ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) || 3330 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 3331 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 3332 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 3333 my $init_char = "c"; 3334 my $orig_commit = ""; 3335 my $short = 1; 3336 my $long = 0; 3337 my $case = 1; 3338 my $space = 1; 3339 my $id = '0123456789ab'; 3340 my $orig_desc = "commit description"; 3341 my $description = ""; 3342 my $herectx = $herecurr; 3343 my $has_parens = 0; 3344 my $has_quotes = 0; 3345 3346 my $input = $line; 3347 if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) { 3348 for (my $n = 0; $n < 2; $n++) { 3349 if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) { 3350 $orig_desc = $1; 3351 $has_parens = 1; 3352 # Always strip leading/trailing parens then double quotes if existing 3353 $orig_desc = substr($orig_desc, 1, -1); 3354 if ($orig_desc =~ /^".*"$/) { 3355 $orig_desc = substr($orig_desc, 1, -1); 3356 $has_quotes = 1; 3357 } 3358 last; 3359 } 3360 last if ($#lines < $linenr + $n); 3361 $input .= " " . trim($rawlines[$linenr + $n]); 3362 $herectx .= "$rawlines[$linenr + $n]\n"; 3363 } 3364 $herectx = $herecurr if (!$has_parens); 3365 } 3366 3367 if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 3368 $init_char = $1; 3369 $orig_commit = lc($2); 3370 $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i); 3371 $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i); 3372 $space = 0 if ($input =~ /\bcommit [0-9a-f]/i); 3373 $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 3374 } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) { 3375 $orig_commit = lc($1); 3376 } 3377 3378 ($id, $description) = git_commit_info($orig_commit, 3379 $id, $orig_desc); 3380 3381 if (defined($id) && 3382 ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) && 3383 $last_git_commit_id_linenr != $linenr - 1) { 3384 ERROR("GIT_COMMIT_ID", 3385 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx); 3386 } 3387 #don't report the next line if this line ends in commit and the sha1 hash is the next line 3388 $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i); 3389 } 3390 3391# Check for mailing list archives other than lore.kernel.org 3392 if ($rawline =~ m{http.*\b$obsolete_archives}) { 3393 WARN("PREFER_LORE_ARCHIVE", 3394 "Use lore.kernel.org archive links when possible - see https://lore.kernel.org/lists.html\n" . $herecurr); 3395 } 3396 3397# Check for added, moved or deleted files 3398 if (!$reported_maintainer_file && !$in_commit_log && 3399 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 3400 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 3401 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 3402 (defined($1) || defined($2))))) { 3403 $is_patch = 1; 3404 $reported_maintainer_file = 1; 3405 WARN("FILE_PATH_CHANGES", 3406 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 3407 } 3408 3409# Check for adding new DT bindings not in schema format 3410 if (!$in_commit_log && 3411 ($line =~ /^new file mode\s*\d+\s*$/) && 3412 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 3413 WARN("DT_SCHEMA_BINDING_PATCH", 3414 "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n"); 3415 } 3416 3417# Check for wrappage within a valid hunk of the file 3418 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 3419 ERROR("CORRUPTED_PATCH", 3420 "patch seems to be corrupt (line wrapped?)\n" . 3421 $herecurr) if (!$emitted_corrupt++); 3422 } 3423 3424# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 3425 if (($realfile =~ /^$/ || $line =~ /^\+/) && 3426 $rawline !~ m/^$UTF8*$/) { 3427 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 3428 3429 my $blank = copy_spacing($rawline); 3430 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 3431 my $hereptr = "$hereline$ptr\n"; 3432 3433 CHK("INVALID_UTF8", 3434 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 3435 } 3436 3437# Check if it's the start of a commit log 3438# (not a header line and we haven't seen the patch filename) 3439 if ($in_header_lines && $realfile =~ /^$/ && 3440 !($rawline =~ /^\s+(?:\S|$)/ || 3441 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 3442 $in_header_lines = 0; 3443 $in_commit_log = 1; 3444 $has_commit_log = 1; 3445 } 3446 3447# Check if there is UTF-8 in a commit log when a mail header has explicitly 3448# declined it, i.e defined some charset where it is missing. 3449 if ($in_header_lines && 3450 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 3451 $1 !~ /utf-8/i) { 3452 $non_utf8_charset = 1; 3453 } 3454 3455 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 3456 $rawline =~ /$NON_ASCII_UTF8/) { 3457 WARN("UTF8_BEFORE_PATCH", 3458 "8-bit UTF-8 used in possible commit log\n" . $herecurr); 3459 } 3460 3461# Check for absolute kernel paths in commit message 3462 if ($tree && $in_commit_log) { 3463 while ($line =~ m{(?:^|\s)(/\S*)}g) { 3464 my $file = $1; 3465 3466 if ($file =~ m{^(.*?)(?::\d+)+:?$} && 3467 check_absolute_file($1, $herecurr)) { 3468 # 3469 } else { 3470 check_absolute_file($file, $herecurr); 3471 } 3472 } 3473 } 3474 3475# Check for various typo / spelling mistakes 3476 if (defined($misspellings) && 3477 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 3478 while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) { 3479 my $typo = $1; 3480 my $blank = copy_spacing($rawline); 3481 my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo); 3482 my $hereptr = "$hereline$ptr\n"; 3483 my $typo_fix = $spelling_fix{lc($typo)}; 3484 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 3485 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 3486 my $msg_level = \&WARN; 3487 $msg_level = \&CHK if ($file); 3488 if (&{$msg_level}("TYPO_SPELLING", 3489 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) && 3490 $fix) { 3491 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 3492 } 3493 } 3494 } 3495 3496# check for invalid commit id 3497 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 3498 my $id; 3499 my $description; 3500 ($id, $description) = git_commit_info($2, undef, undef); 3501 if (!defined($id)) { 3502 WARN("UNKNOWN_COMMIT_ID", 3503 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 3504 } 3505 } 3506 3507# check for repeated words separated by a single space 3508# avoid false positive from list command eg, '-rw-r--r-- 1 root root' 3509 if (($rawline =~ /^\+/ || $in_commit_log) && 3510 $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) { 3511 pos($rawline) = 1 if (!$in_commit_log); 3512 while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { 3513 3514 my $first = $1; 3515 my $second = $2; 3516 my $start_pos = $-[1]; 3517 my $end_pos = $+[2]; 3518 if ($first =~ /(?:struct|union|enum)/) { 3519 pos($rawline) += length($first) + length($second) + 1; 3520 next; 3521 } 3522 3523 next if (lc($first) ne lc($second)); 3524 next if ($first eq 'long'); 3525 3526 # check for character before and after the word matches 3527 my $start_char = ''; 3528 my $end_char = ''; 3529 $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1)); 3530 $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline)); 3531 3532 next if ($start_char =~ /^\S$/); 3533 next if (index(" \t.,;?!", $end_char) == -1); 3534 3535 # avoid repeating hex occurrences like 'ff ff fe 09 ...' 3536 if ($first =~ /\b[0-9a-f]{2,}\b/i) { 3537 next if (!exists($allow_repeated_words{lc($first)})); 3538 } 3539 3540 if (WARN("REPEATED_WORD", 3541 "Possible repeated word: '$first'\n" . $herecurr) && 3542 $fix) { 3543 $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; 3544 } 3545 } 3546 3547 # if it's a repeated word on consecutive lines in a comment block 3548 if ($prevline =~ /$;+\s*$/ && 3549 $prevrawline =~ /($word_pattern)\s*$/) { 3550 my $last_word = $1; 3551 if ($rawline =~ /^\+\s*\*\s*$last_word /) { 3552 if (WARN("REPEATED_WORD", 3553 "Possible repeated word: '$last_word'\n" . $hereprev) && 3554 $fix) { 3555 $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; 3556 } 3557 } 3558 } 3559 } 3560 3561# ignore non-hunk lines and lines being removed 3562 next if (!$hunk_line || $line =~ /^-/); 3563 3564#trailing whitespace 3565 if ($line =~ /^\+.*\015/) { 3566 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3567 if (ERROR("DOS_LINE_ENDINGS", 3568 "DOS line endings\n" . $herevet) && 3569 $fix) { 3570 $fixed[$fixlinenr] =~ s/[\s\015]+$//; 3571 } 3572 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 3573 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3574 if (ERROR("TRAILING_WHITESPACE", 3575 "trailing whitespace\n" . $herevet) && 3576 $fix) { 3577 $fixed[$fixlinenr] =~ s/\s+$//; 3578 } 3579 3580 $rpt_cleaners = 1; 3581 } 3582 3583# Check for FSF mailing addresses. 3584 if ($rawline =~ /\bwrite to the Free/i || 3585 $rawline =~ /\b675\s+Mass\s+Ave/i || 3586 $rawline =~ /\b59\s+Temple\s+Pl/i || 3587 $rawline =~ /\b51\s+Franklin\s+St/i) { 3588 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3589 my $msg_level = \&ERROR; 3590 $msg_level = \&CHK if ($file); 3591 &{$msg_level}("FSF_MAILING_ADDRESS", 3592 "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) 3593 } 3594 3595# check for Kconfig help text having a real description 3596# Only applies when adding the entry originally, after that we do not have 3597# sufficient context to determine whether it is indeed long enough. 3598 if ($realfile =~ /Kconfig/ && 3599 # 'choice' is usually the last thing on the line (though 3600 # Kconfig supports named choices), so use a word boundary 3601 # (\b) rather than a whitespace character (\s) 3602 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 3603 my $ln = $linenr; 3604 my $needs_help = 0; 3605 my $has_help = 0; 3606 my $help_length = 0; 3607 while (defined $lines[$ln]) { 3608 my $f = $lines[$ln++]; 3609 3610 next if ($f =~ /^-/); 3611 last if ($f !~ /^[\+ ]/); # !patch context 3612 3613 if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3614 $needs_help = 1; 3615 next; 3616 } 3617 if ($f =~ /^\+\s*help\s*$/) { 3618 $has_help = 1; 3619 next; 3620 } 3621 3622 $f =~ s/^.//; # strip patch context [+ ] 3623 $f =~ s/#.*//; # strip # directives 3624 $f =~ s/^\s+//; # strip leading blanks 3625 next if ($f =~ /^$/); # skip blank lines 3626 3627 # At the end of this Kconfig block: 3628 # This only checks context lines in the patch 3629 # and so hopefully shouldn't trigger false 3630 # positives, even though some of these are 3631 # common words in help texts 3632 if ($f =~ /^(?:config|menuconfig|choice|endchoice| 3633 if|endif|menu|endmenu|source)\b/x) { 3634 last; 3635 } 3636 $help_length++ if ($has_help); 3637 } 3638 if ($needs_help && 3639 $help_length < $min_conf_desc_length) { 3640 my $stat_real = get_stat_real($linenr, $ln - 1); 3641 WARN("CONFIG_DESCRIPTION", 3642 "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n"); 3643 } 3644 } 3645 3646# check MAINTAINERS entries 3647 if ($realfile =~ /^MAINTAINERS$/) { 3648# check MAINTAINERS entries for the right form 3649 if ($rawline =~ /^\+[A-Z]:/ && 3650 $rawline !~ /^\+[A-Z]:\t\S/) { 3651 if (WARN("MAINTAINERS_STYLE", 3652 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3653 $fix) { 3654 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3655 } 3656 } 3657# check MAINTAINERS entries for the right ordering too 3658 my $preferred_order = 'MRLSWQBCPTFXNK'; 3659 if ($rawline =~ /^\+[A-Z]:/ && 3660 $prevrawline =~ /^[\+ ][A-Z]:/) { 3661 $rawline =~ /^\+([A-Z]):\s*(.*)/; 3662 my $cur = $1; 3663 my $curval = $2; 3664 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; 3665 my $prev = $1; 3666 my $prevval = $2; 3667 my $curindex = index($preferred_order, $cur); 3668 my $previndex = index($preferred_order, $prev); 3669 if ($curindex < 0) { 3670 WARN("MAINTAINERS_STYLE", 3671 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); 3672 } else { 3673 if ($previndex >= 0 && $curindex < $previndex) { 3674 WARN("MAINTAINERS_STYLE", 3675 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); 3676 } elsif ((($prev eq 'F' && $cur eq 'F') || 3677 ($prev eq 'X' && $cur eq 'X')) && 3678 ($prevval cmp $curval) > 0) { 3679 WARN("MAINTAINERS_STYLE", 3680 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); 3681 } 3682 } 3683 } 3684 } 3685 3686 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3687 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3688 my $flag = $1; 3689 my $replacement = { 3690 'EXTRA_AFLAGS' => 'asflags-y', 3691 'EXTRA_CFLAGS' => 'ccflags-y', 3692 'EXTRA_CPPFLAGS' => 'cppflags-y', 3693 'EXTRA_LDFLAGS' => 'ldflags-y', 3694 }; 3695 3696 WARN("DEPRECATED_VARIABLE", 3697 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3698 } 3699 3700# check for DT compatible documentation 3701 if (defined $root && 3702 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 3703 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 3704 3705 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3706 3707 my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3708 my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3709 3710 foreach my $compat (@compats) { 3711 my $compat2 = $compat; 3712 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3713 my $compat3 = $compat; 3714 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3715 `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3716 if ( $? >> 8 ) { 3717 WARN("UNDOCUMENTED_DT_STRING", 3718 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3719 } 3720 3721 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 3722 my $vendor = $1; 3723 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3724 if ( $? >> 8 ) { 3725 WARN("UNDOCUMENTED_DT_STRING", 3726 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3727 } 3728 } 3729 } 3730 3731# check for using SPDX license tag at beginning of files 3732 if ($realline == $checklicenseline) { 3733 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 3734 $checklicenseline = 2; 3735 } elsif ($rawline =~ /^\+/) { 3736 my $comment = ""; 3737 if ($realfile =~ /\.(h|s|S)$/) { 3738 $comment = '/*'; 3739 } elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) { 3740 $comment = '//'; 3741 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 3742 $comment = '#'; 3743 } elsif ($realfile =~ /\.rst$/) { 3744 $comment = '..'; 3745 } 3746 3747# check SPDX comment style for .[chsS] files 3748 if ($realfile =~ /\.[chsS]$/ && 3749 $rawline =~ /SPDX-License-Identifier:/ && 3750 $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3751 WARN("SPDX_LICENSE_TAG", 3752 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3753 } 3754 3755 if ($comment !~ /^$/ && 3756 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 3757 WARN("SPDX_LICENSE_TAG", 3758 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 3759 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 3760 my $spdx_license = $1; 3761 if (!is_SPDX_License_valid($spdx_license)) { 3762 WARN("SPDX_LICENSE_TAG", 3763 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 3764 } 3765 if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 3766 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 3767 my $msg_level = \&WARN; 3768 $msg_level = \&CHK if ($file); 3769 if (&{$msg_level}("SPDX_LICENSE_TAG", 3770 3771 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 3772 $fix) { 3773 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 3774 } 3775 } 3776 } 3777 } 3778 } 3779 3780# check for embedded filenames 3781 if ($rawline =~ /^\+.*\b\Q$realfile\E\b/) { 3782 WARN("EMBEDDED_FILENAME", 3783 "It's generally not useful to have the filename in the file\n" . $herecurr); 3784 } 3785 3786# check we are in a valid source file if not then ignore this hunk 3787 next if ($realfile !~ /\.(h|c|rs|s|S|sh|dtsi|dts)$/); 3788 3789# check for using SPDX-License-Identifier on the wrong line number 3790 if ($realline != $checklicenseline && 3791 $rawline =~ /\bSPDX-License-Identifier:/ && 3792 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3793 WARN("SPDX_LICENSE_TAG", 3794 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3795 } 3796 3797# line length limit (with some exclusions) 3798# 3799# There are a few types of lines that may extend beyond $max_line_length: 3800# logging functions like pr_info that end in a string 3801# lines with a single string 3802# #defines that are a single string 3803# lines with an RFC3986 like URL 3804# 3805# There are 3 different line length message types: 3806# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 3807# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 3808# LONG_LINE all other lines longer than $max_line_length 3809# 3810# if LONG_LINE is ignored, the other 2 types are also ignored 3811# 3812 3813 if ($line =~ /^\+/ && $length > $max_line_length) { 3814 my $msg_type = "LONG_LINE"; 3815 3816 # Check the allowed long line types first 3817 3818 # logging functions that end in a string that starts 3819 # before $max_line_length 3820 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 3821 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 3822 $msg_type = ""; 3823 3824 # lines with only strings (w/ possible termination) 3825 # #defines with only strings 3826 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 3827 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 3828 $msg_type = ""; 3829 3830 # More special cases 3831 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3832 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3833 $msg_type = ""; 3834 3835 # URL ($rawline is used in case the URL is in a comment) 3836 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 3837 $msg_type = ""; 3838 3839 # Otherwise set the alternate message types 3840 3841 # a comment starts before $max_line_length 3842 } elsif ($line =~ /($;[\s$;]*)$/ && 3843 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 3844 $msg_type = "LONG_LINE_COMMENT" 3845 3846 # a quoted string starts before $max_line_length 3847 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 3848 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 3849 $msg_type = "LONG_LINE_STRING" 3850 } 3851 3852 if ($msg_type ne "" && 3853 (show_type("LONG_LINE") || show_type($msg_type))) { 3854 my $msg_level = \&WARN; 3855 $msg_level = \&CHK if ($file); 3856 &{$msg_level}($msg_type, 3857 "line length of $length exceeds $max_line_length columns\n" . $herecurr); 3858 } 3859 } 3860 3861# check for adding lines without a newline. 3862 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3863 if (WARN("MISSING_EOF_NEWLINE", 3864 "adding a line without newline at end of file\n" . $herecurr) && 3865 $fix) { 3866 fix_delete_line($fixlinenr+1, "No newline at end of file"); 3867 } 3868 } 3869 3870# check for .L prefix local symbols in .S files 3871 if ($realfile =~ /\.S$/ && 3872 $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) { 3873 WARN("AVOID_L_PREFIX", 3874 "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/core-api/asm-annotations.rst\n" . $herecurr); 3875 } 3876 3877# check we are in a valid source file C or perl if not then ignore this hunk 3878 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 3879 3880# at the beginning of a line any tabs must come first and anything 3881# more than $tabsize must use tabs. 3882 if ($rawline =~ /^\+\s* \t\s*\S/ || 3883 $rawline =~ /^\+\s* \s*/) { 3884 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3885 $rpt_cleaners = 1; 3886 if (ERROR("CODE_INDENT", 3887 "code indent should use tabs where possible\n" . $herevet) && 3888 $fix) { 3889 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 3890 } 3891 } 3892 3893# check for space before tabs. 3894 if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 3895 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3896 if (WARN("SPACE_BEFORE_TAB", 3897 "please, no space before tabs\n" . $herevet) && 3898 $fix) { 3899 while ($fixed[$fixlinenr] =~ 3900 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3901 while ($fixed[$fixlinenr] =~ 3902 s/(^\+.*) +\t/$1\t/) {} 3903 } 3904 } 3905 3906# check for assignments on the start of a line 3907 if ($sline =~ /^\+\s+($Assignment)[^=]/) { 3908 my $operator = $1; 3909 if (CHK("ASSIGNMENT_CONTINUATIONS", 3910 "Assignment operator '$1' should be on the previous line\n" . $hereprev) && 3911 $fix && $prevrawline =~ /^\+/) { 3912 # add assignment operator to the previous line, remove from current line 3913 $fixed[$fixlinenr - 1] .= " $operator"; 3914 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 3915 } 3916 } 3917 3918# check for && or || at the start of a line 3919 if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3920 my $operator = $1; 3921 if (CHK("LOGICAL_CONTINUATIONS", 3922 "Logical continuations should be on the previous line\n" . $hereprev) && 3923 $fix && $prevrawline =~ /^\+/) { 3924 # insert logical operator at last non-comment, non-whitepsace char on previous line 3925 $prevline =~ /[\s$;]*$/; 3926 my $line_end = substr($prevrawline, $-[0]); 3927 $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/; 3928 $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 3929 } 3930 } 3931 3932# check indentation starts on a tab stop 3933 if ($perl_version_ok && 3934 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3935 my $indent = length($1); 3936 if ($indent % $tabsize) { 3937 if (WARN("TABSTOP", 3938 "Statements should start on a tabstop\n" . $herecurr) && 3939 $fix) { 3940 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3941 } 3942 } 3943 } 3944 3945# check multi-line statement indentation matches previous line 3946 if ($perl_version_ok && 3947 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3948 $prevline =~ /^\+(\t*)(.*)$/; 3949 my $oldindent = $1; 3950 my $rest = $2; 3951 3952 my $pos = pos_last_openparen($rest); 3953 if ($pos >= 0) { 3954 $line =~ /^(\+| )([ \t]*)/; 3955 my $newindent = $2; 3956 3957 my $goodtabindent = $oldindent . 3958 "\t" x ($pos / $tabsize) . 3959 " " x ($pos % $tabsize); 3960 my $goodspaceindent = $oldindent . " " x $pos; 3961 3962 if ($newindent ne $goodtabindent && 3963 $newindent ne $goodspaceindent) { 3964 3965 if (CHK("PARENTHESIS_ALIGNMENT", 3966 "Alignment should match open parenthesis\n" . $hereprev) && 3967 $fix && $line =~ /^\+/) { 3968 $fixed[$fixlinenr] =~ 3969 s/^\+[ \t]*/\+$goodtabindent/; 3970 } 3971 } 3972 } 3973 } 3974 3975# check for space after cast like "(int) foo" or "(struct foo) bar" 3976# avoid checking a few false positives: 3977# "sizeof(<type>)" or "__alignof__(<type>)" 3978# function pointer declarations like "(*foo)(int) = bar;" 3979# structure definitions like "(struct foo) { 0 };" 3980# multiline macros that define functions 3981# known attributes or the __attribute__ keyword 3982 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 3983 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 3984 if (CHK("SPACING", 3985 "No space is necessary after a cast\n" . $herecurr) && 3986 $fix) { 3987 $fixed[$fixlinenr] =~ 3988 s/(\(\s*$Type\s*\))[ \t]+/$1/; 3989 } 3990 } 3991 3992# Block comment styles 3993# Networking with an initial /* 3994 if ($realfile =~ m@^(drivers/net/|net/)@ && 3995 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 3996 $rawline =~ /^\+[ \t]*\*/ && 3997 $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier 3998 WARN("NETWORKING_BLOCK_COMMENT_STYLE", 3999 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 4000 } 4001 4002# Block comments use * on subsequent lines 4003 if ($prevline =~ /$;[ \t]*$/ && #ends in comment 4004 $prevrawline =~ /^\+.*?\/\*/ && #starting /* 4005 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 4006 $rawline =~ /^\+/ && #line is new 4007 $rawline !~ /^\+[ \t]*\*/) { #no leading * 4008 WARN("BLOCK_COMMENT_STYLE", 4009 "Block comments use * on subsequent lines\n" . $hereprev); 4010 } 4011 4012# Block comments use */ on trailing lines 4013 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 4014 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 4015 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 4016 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 4017 WARN("BLOCK_COMMENT_STYLE", 4018 "Block comments use a trailing */ on a separate line\n" . $herecurr); 4019 } 4020 4021# Block comment * alignment 4022 if ($prevline =~ /$;[ \t]*$/ && #ends in comment 4023 $line =~ /^\+[ \t]*$;/ && #leading comment 4024 $rawline =~ /^\+[ \t]*\*/ && #leading * 4025 (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 4026 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 4027 $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 4028 my $oldindent; 4029 $prevrawline =~ m@^\+([ \t]*/?)\*@; 4030 if (defined($1)) { 4031 $oldindent = expand_tabs($1); 4032 } else { 4033 $prevrawline =~ m@^\+(.*/?)\*@; 4034 $oldindent = expand_tabs($1); 4035 } 4036 $rawline =~ m@^\+([ \t]*)\*@; 4037 my $newindent = $1; 4038 $newindent = expand_tabs($newindent); 4039 if (length($oldindent) ne length($newindent)) { 4040 WARN("BLOCK_COMMENT_STYLE", 4041 "Block comments should align the * on each line\n" . $hereprev); 4042 } 4043 } 4044 4045# check for missing blank lines after struct/union declarations 4046# with exceptions for various attributes and macros 4047 if ($prevline =~ /^[\+ ]};?\s*$/ && 4048 $line =~ /^\+/ && 4049 !($line =~ /^\+\s*$/ || 4050 $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param)/ || 4051 $line =~ /^\+\s*MODULE_/i || 4052 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 4053 $line =~ /^\+[a-z_]*init/ || 4054 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 4055 $line =~ /^\+\s*DECLARE/ || 4056 $line =~ /^\+\s*builtin_[\w_]*driver/ || 4057 $line =~ /^\+\s*__setup/)) { 4058 if (CHK("LINE_SPACING", 4059 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 4060 $fix) { 4061 fix_insert_line($fixlinenr, "\+"); 4062 } 4063 } 4064 4065# check for multiple consecutive blank lines 4066 if ($prevline =~ /^[\+ ]\s*$/ && 4067 $line =~ /^\+\s*$/ && 4068 $last_blank_line != ($linenr - 1)) { 4069 if (CHK("LINE_SPACING", 4070 "Please don't use multiple blank lines\n" . $hereprev) && 4071 $fix) { 4072 fix_delete_line($fixlinenr, $rawline); 4073 } 4074 4075 $last_blank_line = $linenr; 4076 } 4077 4078# check for missing blank lines after declarations 4079# (declarations must have the same indentation and not be at the start of line) 4080 if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) { 4081 # use temporaries 4082 my $sl = $sline; 4083 my $pl = $prevline; 4084 # remove $Attribute/$Sparse uses to simplify comparisons 4085 $sl =~ s/\b(?:$Attribute|$Sparse)\b//g; 4086 $pl =~ s/\b(?:$Attribute|$Sparse)\b//g; 4087 if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 4088 # function pointer declarations 4089 $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 4090 # foo bar; where foo is some local typedef or #define 4091 $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 4092 # known declaration macros 4093 $pl =~ /^\+\s+$declaration_macros/) && 4094 # for "else if" which can look like "$Ident $Ident" 4095 !($pl =~ /^\+\s+$c90_Keywords\b/ || 4096 # other possible extensions of declaration lines 4097 $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 4098 # not starting a section or a macro "\" extended line 4099 $pl =~ /(?:\{\s*|\\)$/) && 4100 # looks like a declaration 4101 !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 4102 # function pointer declarations 4103 $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 4104 # foo bar; where foo is some local typedef or #define 4105 $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 4106 # known declaration macros 4107 $sl =~ /^\+\s+$declaration_macros/ || 4108 # start of struct or union or enum 4109 $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 4110 # start or end of block or continuation of declaration 4111 $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 4112 # bitfield continuation 4113 $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 4114 # other possible extensions of declaration lines 4115 $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) { 4116 if (WARN("LINE_SPACING", 4117 "Missing a blank line after declarations\n" . $hereprev) && 4118 $fix) { 4119 fix_insert_line($fixlinenr, "\+"); 4120 } 4121 } 4122 } 4123 4124# check for spaces at the beginning of a line. 4125# Exceptions: 4126# 1) within comments 4127# 2) indented preprocessor commands 4128# 3) hanging labels 4129 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 4130 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 4131 if (WARN("LEADING_SPACE", 4132 "please, no spaces at the start of a line\n" . $herevet) && 4133 $fix) { 4134 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 4135 } 4136 } 4137 4138# check we are in a valid C source file if not then ignore this hunk 4139 next if ($realfile !~ /\.(h|c)$/); 4140 4141# check for unusual line ending [ or ( 4142 if ($line =~ /^\+.*([\[\(])\s*$/) { 4143 CHK("OPEN_ENDED_LINE", 4144 "Lines should not end with a '$1'\n" . $herecurr); 4145 } 4146 4147# check if this appears to be the start function declaration, save the name 4148 if ($sline =~ /^\+\{\s*$/ && 4149 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 4150 $context_function = $1; 4151 } 4152 4153# check if this appears to be the end of function declaration 4154 if ($sline =~ /^\+\}\s*$/) { 4155 undef $context_function; 4156 } 4157 4158# check indentation of any line with a bare else 4159# (but not if it is a multiple line "if (foo) return bar; else return baz;") 4160# if the previous line is a break or return and is indented 1 tab more... 4161 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 4162 my $tabs = length($1) + 1; 4163 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 4164 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 4165 defined $lines[$linenr] && 4166 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 4167 WARN("UNNECESSARY_ELSE", 4168 "else is not generally useful after a break or return\n" . $hereprev); 4169 } 4170 } 4171 4172# check indentation of a line with a break; 4173# if the previous line is a goto, return or break 4174# and is indented the same # of tabs 4175 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 4176 my $tabs = $1; 4177 if ($prevline =~ /^\+$tabs(goto|return|break)\b/) { 4178 if (WARN("UNNECESSARY_BREAK", 4179 "break is not useful after a $1\n" . $hereprev) && 4180 $fix) { 4181 fix_delete_line($fixlinenr, $rawline); 4182 } 4183 } 4184 } 4185 4186# check for RCS/CVS revision markers 4187 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 4188 WARN("CVS_KEYWORD", 4189 "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 4190 } 4191 4192# check for old HOTPLUG __dev<foo> section markings 4193 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 4194 WARN("HOTPLUG_SECTION", 4195 "Using $1 is unnecessary\n" . $herecurr); 4196 } 4197 4198# Check for potential 'bare' types 4199 my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 4200 $realline_next); 4201#print "LINE<$line>\n"; 4202 if ($linenr > $suppress_statement && 4203 $realcnt && $sline =~ /.\s*\S/) { 4204 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 4205 ctx_statement_block($linenr, $realcnt, 0); 4206 $stat =~ s/\n./\n /g; 4207 $cond =~ s/\n./\n /g; 4208 4209#print "linenr<$linenr> <$stat>\n"; 4210 # If this statement has no statement boundaries within 4211 # it there is no point in retrying a statement scan 4212 # until we hit end of it. 4213 my $frag = $stat; $frag =~ s/;+\s*$//; 4214 if ($frag !~ /(?:{|;)/) { 4215#print "skip<$line_nr_next>\n"; 4216 $suppress_statement = $line_nr_next; 4217 } 4218 4219 # Find the real next line. 4220 $realline_next = $line_nr_next; 4221 if (defined $realline_next && 4222 (!defined $lines[$realline_next - 1] || 4223 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 4224 $realline_next++; 4225 } 4226 4227 my $s = $stat; 4228 $s =~ s/{.*$//s; 4229 4230 # Ignore goto labels. 4231 if ($s =~ /$Ident:\*$/s) { 4232 4233 # Ignore functions being called 4234 } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 4235 4236 } elsif ($s =~ /^.\s*else\b/s) { 4237 4238 # declarations always start with types 4239 } 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) { 4240 my $type = $1; 4241 $type =~ s/\s+/ /g; 4242 possible($type, "A:" . $s); 4243 4244 # definitions in global scope can only start with types 4245 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 4246 possible($1, "B:" . $s); 4247 } 4248 4249 # any (foo ... *) is a pointer cast, and foo is a type 4250 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 4251 possible($1, "C:" . $s); 4252 } 4253 4254 # Check for any sort of function declaration. 4255 # int foo(something bar, other baz); 4256 # void (*store_gdt)(x86_descr_ptr *); 4257 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 4258 my ($name_len) = length($1); 4259 4260 my $ctx = $s; 4261 substr($ctx, 0, $name_len + 1, ''); 4262 $ctx =~ s/\)[^\)]*$//; 4263 4264 for my $arg (split(/\s*,\s*/, $ctx)) { 4265 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 4266 4267 possible($1, "D:" . $s); 4268 } 4269 } 4270 } 4271 4272 } 4273 4274# 4275# Checks which may be anchored in the context. 4276# 4277 4278# Check for switch () and associated case and default 4279# statements should be at the same indent. 4280 if ($line=~/\bswitch\s*\(.*\)/) { 4281 my $err = ''; 4282 my $sep = ''; 4283 my @ctx = ctx_block_outer($linenr, $realcnt); 4284 shift(@ctx); 4285 for my $ctx (@ctx) { 4286 my ($clen, $cindent) = line_stats($ctx); 4287 if ($ctx =~ /^\+\s*(case\s+|default:)/ && 4288 $indent != $cindent) { 4289 $err .= "$sep$ctx\n"; 4290 $sep = ''; 4291 } else { 4292 $sep = "[...]\n"; 4293 } 4294 } 4295 if ($err ne '') { 4296 ERROR("SWITCH_CASE_INDENT_LEVEL", 4297 "switch and case should be at the same indent\n$hereline$err"); 4298 } 4299 } 4300 4301# if/while/etc brace do not go on next line, unless defining a do while loop, 4302# or if that brace on the next line is for something else 4303 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 4304 my $pre_ctx = "$1$2"; 4305 4306 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 4307 4308 if ($line =~ /^\+\t{6,}/) { 4309 WARN("DEEP_INDENTATION", 4310 "Too many leading tabs - consider code refactoring\n" . $herecurr); 4311 } 4312 4313 my $ctx_cnt = $realcnt - $#ctx - 1; 4314 my $ctx = join("\n", @ctx); 4315 4316 my $ctx_ln = $linenr; 4317 my $ctx_skip = $realcnt; 4318 4319 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 4320 defined $lines[$ctx_ln - 1] && 4321 $lines[$ctx_ln - 1] =~ /^-/)) { 4322 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 4323 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 4324 $ctx_ln++; 4325 } 4326 4327 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 4328 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 4329 4330 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 4331 ERROR("OPEN_BRACE", 4332 "that open brace { should be on the previous line\n" . 4333 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 4334 } 4335 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 4336 $ctx =~ /\)\s*\;\s*$/ && 4337 defined $lines[$ctx_ln - 1]) 4338 { 4339 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 4340 if ($nindent > $indent) { 4341 WARN("TRAILING_SEMICOLON", 4342 "trailing semicolon indicates no statements, indent implies otherwise\n" . 4343 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 4344 } 4345 } 4346 } 4347 4348# Check relative indent for conditionals and blocks. 4349 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 4350 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 4351 ctx_statement_block($linenr, $realcnt, 0) 4352 if (!defined $stat); 4353 my ($s, $c) = ($stat, $cond); 4354 4355 substr($s, 0, length($c), ''); 4356 4357 # remove inline comments 4358 $s =~ s/$;/ /g; 4359 $c =~ s/$;/ /g; 4360 4361 # Find out how long the conditional actually is. 4362 my @newlines = ($c =~ /\n/gs); 4363 my $cond_lines = 1 + $#newlines; 4364 4365 # Make sure we remove the line prefixes as we have 4366 # none on the first line, and are going to readd them 4367 # where necessary. 4368 $s =~ s/\n./\n/gs; 4369 while ($s =~ /\n\s+\\\n/) { 4370 $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 4371 } 4372 4373 # We want to check the first line inside the block 4374 # starting at the end of the conditional, so remove: 4375 # 1) any blank line termination 4376 # 2) any opening brace { on end of the line 4377 # 3) any do (...) { 4378 my $continuation = 0; 4379 my $check = 0; 4380 $s =~ s/^.*\bdo\b//; 4381 $s =~ s/^\s*{//; 4382 if ($s =~ s/^\s*\\//) { 4383 $continuation = 1; 4384 } 4385 if ($s =~ s/^\s*?\n//) { 4386 $check = 1; 4387 $cond_lines++; 4388 } 4389 4390 # Also ignore a loop construct at the end of a 4391 # preprocessor statement. 4392 if (($prevline =~ /^.\s*#\s*define\s/ || 4393 $prevline =~ /\\\s*$/) && $continuation == 0) { 4394 $check = 0; 4395 } 4396 4397 my $cond_ptr = -1; 4398 $continuation = 0; 4399 while ($cond_ptr != $cond_lines) { 4400 $cond_ptr = $cond_lines; 4401 4402 # If we see an #else/#elif then the code 4403 # is not linear. 4404 if ($s =~ /^\s*\#\s*(?:else|elif)/) { 4405 $check = 0; 4406 } 4407 4408 # Ignore: 4409 # 1) blank lines, they should be at 0, 4410 # 2) preprocessor lines, and 4411 # 3) labels. 4412 if ($continuation || 4413 $s =~ /^\s*?\n/ || 4414 $s =~ /^\s*#\s*?/ || 4415 $s =~ /^\s*$Ident\s*:/) { 4416 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 4417 if ($s =~ s/^.*?\n//) { 4418 $cond_lines++; 4419 } 4420 } 4421 } 4422 4423 my (undef, $sindent) = line_stats("+" . $s); 4424 my $stat_real = raw_line($linenr, $cond_lines); 4425 4426 # Check if either of these lines are modified, else 4427 # this is not this patch's fault. 4428 if (!defined($stat_real) || 4429 $stat !~ /^\+/ && $stat_real !~ /^\+/) { 4430 $check = 0; 4431 } 4432 if (defined($stat_real) && $cond_lines > 1) { 4433 $stat_real = "[...]\n$stat_real"; 4434 } 4435 4436 #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"; 4437 4438 if ($check && $s ne '' && 4439 (($sindent % $tabsize) != 0 || 4440 ($sindent < $indent) || 4441 ($sindent == $indent && 4442 ($s !~ /^\s*(?:\}|\{|else\b)/)) || 4443 ($sindent > $indent + $tabsize))) { 4444 WARN("SUSPECT_CODE_INDENT", 4445 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 4446 } 4447 } 4448 4449 # Track the 'values' across context and added lines. 4450 my $opline = $line; $opline =~ s/^./ /; 4451 my ($curr_values, $curr_vars) = 4452 annotate_values($opline . "\n", $prev_values); 4453 $curr_values = $prev_values . $curr_values; 4454 if ($dbg_values) { 4455 my $outline = $opline; $outline =~ s/\t/ /g; 4456 print "$linenr > .$outline\n"; 4457 print "$linenr > $curr_values\n"; 4458 print "$linenr > $curr_vars\n"; 4459 } 4460 $prev_values = substr($curr_values, -1); 4461 4462#ignore lines not being added 4463 next if ($line =~ /^[^\+]/); 4464 4465# check for self assignments used to avoid compiler warnings 4466# e.g.: int foo = foo, *bar = NULL; 4467# struct foo bar = *(&(bar)); 4468 if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { 4469 my $var = $1; 4470 if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { 4471 WARN("SELF_ASSIGNMENT", 4472 "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); 4473 } 4474 } 4475 4476# check for dereferences that span multiple lines 4477 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 4478 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 4479 $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 4480 my $ref = $1; 4481 $line =~ /^.\s*($Lval)/; 4482 $ref .= $1; 4483 $ref =~ s/\s//g; 4484 WARN("MULTILINE_DEREFERENCE", 4485 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 4486 } 4487 4488# check for declarations of signed or unsigned without int 4489 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 4490 my $type = $1; 4491 my $var = $2; 4492 $var = "" if (!defined $var); 4493 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 4494 my $sign = $1; 4495 my $pointer = $2; 4496 4497 $pointer = "" if (!defined $pointer); 4498 4499 if (WARN("UNSPECIFIED_INT", 4500 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 4501 $fix) { 4502 my $decl = trim($sign) . " int "; 4503 my $comp_pointer = $pointer; 4504 $comp_pointer =~ s/\s//g; 4505 $decl .= $comp_pointer; 4506 $decl = rtrim($decl) if ($var eq ""); 4507 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 4508 } 4509 } 4510 } 4511 4512# TEST: allow direct testing of the type matcher. 4513 if ($dbg_type) { 4514 if ($line =~ /^.\s*$Declare\s*$/) { 4515 ERROR("TEST_TYPE", 4516 "TEST: is type\n" . $herecurr); 4517 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 4518 ERROR("TEST_NOT_TYPE", 4519 "TEST: is not type ($1 is)\n". $herecurr); 4520 } 4521 next; 4522 } 4523# TEST: allow direct testing of the attribute matcher. 4524 if ($dbg_attr) { 4525 if ($line =~ /^.\s*$Modifier\s*$/) { 4526 ERROR("TEST_ATTR", 4527 "TEST: is attr\n" . $herecurr); 4528 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 4529 ERROR("TEST_NOT_ATTR", 4530 "TEST: is not attr ($1 is)\n". $herecurr); 4531 } 4532 next; 4533 } 4534 4535# check for initialisation to aggregates open brace on the next line 4536 if ($line =~ /^.\s*{/ && 4537 $prevline =~ /(?:^|[^=])=\s*$/) { 4538 if (ERROR("OPEN_BRACE", 4539 "that open brace { should be on the previous line\n" . $hereprev) && 4540 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 4541 fix_delete_line($fixlinenr - 1, $prevrawline); 4542 fix_delete_line($fixlinenr, $rawline); 4543 my $fixedline = $prevrawline; 4544 $fixedline =~ s/\s*=\s*$/ = {/; 4545 fix_insert_line($fixlinenr, $fixedline); 4546 $fixedline = $line; 4547 $fixedline =~ s/^(.\s*)\{\s*/$1/; 4548 fix_insert_line($fixlinenr, $fixedline); 4549 } 4550 } 4551 4552# 4553# Checks which are anchored on the added line. 4554# 4555 4556# check for malformed paths in #include statements (uses RAW line) 4557 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 4558 my $path = $1; 4559 if ($path =~ m{//}) { 4560 ERROR("MALFORMED_INCLUDE", 4561 "malformed #include filename\n" . $herecurr); 4562 } 4563 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 4564 ERROR("UAPI_INCLUDE", 4565 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 4566 } 4567 } 4568 4569# no C99 // comments 4570 if ($line =~ m{//}) { 4571 if (ERROR("C99_COMMENTS", 4572 "do not use C99 // comments\n" . $herecurr) && 4573 $fix) { 4574 my $line = $fixed[$fixlinenr]; 4575 if ($line =~ /\/\/(.*)$/) { 4576 my $comment = trim($1); 4577 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 4578 } 4579 } 4580 } 4581 # Remove C99 comments. 4582 $line =~ s@//.*@@; 4583 $opline =~ s@//.*@@; 4584 4585# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 4586# the whole statement. 4587#print "APW <$lines[$realline_next - 1]>\n"; 4588 if (defined $realline_next && 4589 exists $lines[$realline_next - 1] && 4590 !defined $suppress_export{$realline_next} && 4591 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) { 4592 # Handle definitions which produce identifiers with 4593 # a prefix: 4594 # XXX(foo); 4595 # EXPORT_SYMBOL(something_foo); 4596 my $name = $1; 4597 $name =~ s/^\s*($Ident).*/$1/; 4598 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 4599 $name =~ /^${Ident}_$2/) { 4600#print "FOO C name<$name>\n"; 4601 $suppress_export{$realline_next} = 1; 4602 4603 } elsif ($stat !~ /(?: 4604 \n.}\s*$| 4605 ^.DEFINE_$Ident\(\Q$name\E\)| 4606 ^.DECLARE_$Ident\(\Q$name\E\)| 4607 ^.LIST_HEAD\(\Q$name\E\)| 4608 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 4609 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 4610 )/x) { 4611#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 4612 $suppress_export{$realline_next} = 2; 4613 } else { 4614 $suppress_export{$realline_next} = 1; 4615 } 4616 } 4617 if (!defined $suppress_export{$linenr} && 4618 $prevline =~ /^.\s*$/ && 4619 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) { 4620#print "FOO B <$lines[$linenr - 1]>\n"; 4621 $suppress_export{$linenr} = 2; 4622 } 4623 if (defined $suppress_export{$linenr} && 4624 $suppress_export{$linenr} == 2) { 4625 WARN("EXPORT_SYMBOL", 4626 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 4627 } 4628 4629# check for global initialisers. 4630 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ && 4631 !exclude_global_initialisers($realfile)) { 4632 if (ERROR("GLOBAL_INITIALISERS", 4633 "do not initialise globals to $1\n" . $herecurr) && 4634 $fix) { 4635 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 4636 } 4637 } 4638# check for static initialisers. 4639 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 4640 if (ERROR("INITIALISED_STATIC", 4641 "do not initialise statics to $1\n" . 4642 $herecurr) && 4643 $fix) { 4644 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 4645 } 4646 } 4647 4648# check for misordered declarations of char/short/int/long with signed/unsigned 4649 while ($sline =~ m{(\b$TypeMisordered\b)}g) { 4650 my $tmp = trim($1); 4651 WARN("MISORDERED_TYPE", 4652 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 4653 } 4654 4655# check for unnecessary <signed> int declarations of short/long/long long 4656 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 4657 my $type = trim($1); 4658 next if ($type !~ /\bint\b/); 4659 next if ($type !~ /\b(?:short|long\s+long|long)\b/); 4660 my $new_type = $type; 4661 $new_type =~ s/\b\s*int\s*\b/ /; 4662 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 4663 $new_type =~ s/^const\s+//; 4664 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 4665 $new_type = "const $new_type" if ($type =~ /^const\b/); 4666 $new_type =~ s/\s+/ /g; 4667 $new_type = trim($new_type); 4668 if (WARN("UNNECESSARY_INT", 4669 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 4670 $fix) { 4671 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 4672 } 4673 } 4674 4675# check for static const char * arrays. 4676 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 4677 WARN("STATIC_CONST_CHAR_ARRAY", 4678 "static const char * array should probably be static const char * const\n" . 4679 $herecurr); 4680 } 4681 4682# check for initialized const char arrays that should be static const 4683 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 4684 if (WARN("STATIC_CONST_CHAR_ARRAY", 4685 "const array should probably be static const\n" . $herecurr) && 4686 $fix) { 4687 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 4688 } 4689 } 4690 4691# check for static char foo[] = "bar" declarations. 4692 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4693 WARN("STATIC_CONST_CHAR_ARRAY", 4694 "static char array declaration should probably be static const char\n" . 4695 $herecurr); 4696 } 4697 4698# check for const <foo> const where <foo> is not a pointer or array type 4699 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4700 my $found = $1; 4701 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4702 WARN("CONST_CONST", 4703 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4704 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4705 WARN("CONST_CONST", 4706 "'const $found const' should probably be 'const $found'\n" . $herecurr); 4707 } 4708 } 4709 4710# check for const static or static <non ptr type> const declarations 4711# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const' 4712 if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ || 4713 $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) { 4714 if (WARN("STATIC_CONST", 4715 "Move const after static - use 'static const $1'\n" . $herecurr) && 4716 $fix) { 4717 $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/; 4718 $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/; 4719 } 4720 } 4721 4722# check for non-global char *foo[] = {"bar", ...} declarations. 4723 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 4724 WARN("STATIC_CONST_CHAR_ARRAY", 4725 "char * array declaration might be better as static const\n" . 4726 $herecurr); 4727 } 4728 4729# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4730 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4731 my $array = $1; 4732 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { 4733 my $array_div = $1; 4734 if (WARN("ARRAY_SIZE", 4735 "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4736 $fix) { 4737 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4738 } 4739 } 4740 } 4741 4742# check for function declarations without arguments like "int foo()" 4743 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { 4744 if (ERROR("FUNCTION_WITHOUT_ARGS", 4745 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4746 $fix) { 4747 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4748 } 4749 } 4750 4751# check for new typedefs, only function parameters and sparse annotations 4752# make sense. 4753 if ($line =~ /\btypedef\s/ && 4754 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4755 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 4756 $line !~ /\b$typeTypedefs\b/ && 4757 $line !~ /\b__bitwise\b/) { 4758 WARN("NEW_TYPEDEFS", 4759 "do not add new typedefs\n" . $herecurr); 4760 } 4761 4762# * goes on variable not on type 4763 # (char*[ const]) 4764 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4765 #print "AA<$1>\n"; 4766 my ($ident, $from, $to) = ($1, $2, $2); 4767 4768 # Should start with a space. 4769 $to =~ s/^(\S)/ $1/; 4770 # Should not end with a space. 4771 $to =~ s/\s+$//; 4772 # '*'s should not have spaces between. 4773 while ($to =~ s/\*\s+\*/\*\*/) { 4774 } 4775 4776## print "1: from<$from> to<$to> ident<$ident>\n"; 4777 if ($from ne $to) { 4778 if (ERROR("POINTER_LOCATION", 4779 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 4780 $fix) { 4781 my $sub_from = $ident; 4782 my $sub_to = $ident; 4783 $sub_to =~ s/\Q$from\E/$to/; 4784 $fixed[$fixlinenr] =~ 4785 s@\Q$sub_from\E@$sub_to@; 4786 } 4787 } 4788 } 4789 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4790 #print "BB<$1>\n"; 4791 my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4792 4793 # Should start with a space. 4794 $to =~ s/^(\S)/ $1/; 4795 # Should not end with a space. 4796 $to =~ s/\s+$//; 4797 # '*'s should not have spaces between. 4798 while ($to =~ s/\*\s+\*/\*\*/) { 4799 } 4800 # Modifiers should have spaces. 4801 $to =~ s/(\b$Modifier$)/$1 /; 4802 4803## print "2: from<$from> to<$to> ident<$ident>\n"; 4804 if ($from ne $to && $ident !~ /^$Modifier$/) { 4805 if (ERROR("POINTER_LOCATION", 4806 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 4807 $fix) { 4808 4809 my $sub_from = $match; 4810 my $sub_to = $match; 4811 $sub_to =~ s/\Q$from\E/$to/; 4812 $fixed[$fixlinenr] =~ 4813 s@\Q$sub_from\E@$sub_to@; 4814 } 4815 } 4816 } 4817 4818# do not use BUG() or variants 4819 if ($line =~ /\b(?!AA_|BUILD_|DCCP_|IDA_|KVM_|RWLOCK_|snd_|SPIN_)(?:[a-zA-Z_]*_)?BUG(?:_ON)?(?:_[A-Z_]+)?\s*\(/) { 4820 my $msg_level = \&WARN; 4821 $msg_level = \&CHK if ($file); 4822 &{$msg_level}("AVOID_BUG", 4823 "Do not crash the kernel unless it is absolutely unavoidable--use WARN_ON_ONCE() plus recovery code (if feasible) instead of BUG() or variants\n" . $herecurr); 4824 } 4825 4826# avoid LINUX_VERSION_CODE 4827 if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4828 WARN("LINUX_VERSION_CODE", 4829 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 4830 } 4831 4832# check for uses of printk_ratelimit 4833 if ($line =~ /\bprintk_ratelimit\s*\(/) { 4834 WARN("PRINTK_RATELIMITED", 4835 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 4836 } 4837 4838# printk should use KERN_* levels 4839 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4840 WARN("PRINTK_WITHOUT_KERN_LEVEL", 4841 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 4842 } 4843 4844# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL> 4845 if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) { 4846 my $printk = $1; 4847 my $modifier = $2; 4848 my $orig = $3; 4849 $modifier = "" if (!defined($modifier)); 4850 my $level = lc($orig); 4851 $level = "warn" if ($level eq "warning"); 4852 my $level2 = $level; 4853 $level2 = "dbg" if ($level eq "debug"); 4854 $level .= $modifier; 4855 $level2 .= $modifier; 4856 WARN("PREFER_PR_LEVEL", 4857 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr); 4858 } 4859 4860# prefer dev_<level> to dev_printk(KERN_<LEVEL> 4861 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4862 my $orig = $1; 4863 my $level = lc($orig); 4864 $level = "warn" if ($level eq "warning"); 4865 $level = "dbg" if ($level eq "debug"); 4866 WARN("PREFER_DEV_LEVEL", 4867 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4868 } 4869 4870# trace_printk should not be used in production code. 4871 if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { 4872 WARN("TRACE_PRINTK", 4873 "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); 4874 } 4875 4876# ENOSYS means "bad syscall nr" and nothing else. This will have a small 4877# number of false positives, but assembly files are not checked, so at 4878# least the arch entry code will not trigger this warning. 4879 if ($line =~ /\bENOSYS\b/) { 4880 WARN("ENOSYS", 4881 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 4882 } 4883 4884# ENOTSUPP is not a standard error code and should be avoided in new patches. 4885# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 4886# Similarly to ENOSYS warning a small number of false positives is expected. 4887 if (!$file && $line =~ /\bENOTSUPP\b/) { 4888 if (WARN("ENOTSUPP", 4889 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 4890 $fix) { 4891 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 4892 } 4893 } 4894 4895# function brace can't be on same line, except for #defines of do while, 4896# or if closed on same line 4897 if ($perl_version_ok && 4898 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 4899 $sline !~ /\#\s*define\b.*do\s*\{/ && 4900 $sline !~ /}/) { 4901 if (ERROR("OPEN_BRACE", 4902 "open brace '{' following function definitions go on the next line\n" . $herecurr) && 4903 $fix) { 4904 fix_delete_line($fixlinenr, $rawline); 4905 my $fixed_line = $rawline; 4906 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/; 4907 my $line1 = $1; 4908 my $line2 = $2; 4909 fix_insert_line($fixlinenr, ltrim($line1)); 4910 fix_insert_line($fixlinenr, "\+{"); 4911 if ($line2 !~ /^\s*$/) { 4912 fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 4913 } 4914 } 4915 } 4916 4917# open braces for enum, union and struct go on the same line. 4918 if ($line =~ /^.\s*{/ && 4919 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 4920 if (ERROR("OPEN_BRACE", 4921 "open brace '{' following $1 go on the same line\n" . $hereprev) && 4922 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 4923 fix_delete_line($fixlinenr - 1, $prevrawline); 4924 fix_delete_line($fixlinenr, $rawline); 4925 my $fixedline = rtrim($prevrawline) . " {"; 4926 fix_insert_line($fixlinenr, $fixedline); 4927 $fixedline = $rawline; 4928 $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 4929 if ($fixedline !~ /^\+\s*$/) { 4930 fix_insert_line($fixlinenr, $fixedline); 4931 } 4932 } 4933 } 4934 4935# missing space after union, struct or enum definition 4936 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 4937 if (WARN("SPACING", 4938 "missing space after $1 definition\n" . $herecurr) && 4939 $fix) { 4940 $fixed[$fixlinenr] =~ 4941 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 4942 } 4943 } 4944 4945# Function pointer declarations 4946# check spacing between type, funcptr, and args 4947# canonical declaration is "type (*funcptr)(args...)" 4948 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 4949 my $declare = $1; 4950 my $pre_pointer_space = $2; 4951 my $post_pointer_space = $3; 4952 my $funcname = $4; 4953 my $post_funcname_space = $5; 4954 my $pre_args_space = $6; 4955 4956# the $Declare variable will capture all spaces after the type 4957# so check it for a missing trailing missing space but pointer return types 4958# don't need a space so don't warn for those. 4959 my $post_declare_space = ""; 4960 if ($declare =~ /(\s+)$/) { 4961 $post_declare_space = $1; 4962 $declare = rtrim($declare); 4963 } 4964 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 4965 WARN("SPACING", 4966 "missing space after return type\n" . $herecurr); 4967 $post_declare_space = " "; 4968 } 4969 4970# unnecessary space "type (*funcptr)(args...)" 4971# This test is not currently implemented because these declarations are 4972# equivalent to 4973# int foo(int bar, ...) 4974# and this is form shouldn't/doesn't generate a checkpatch warning. 4975# 4976# elsif ($declare =~ /\s{2,}$/) { 4977# WARN("SPACING", 4978# "Multiple spaces after return type\n" . $herecurr); 4979# } 4980 4981# unnecessary space "type ( *funcptr)(args...)" 4982 if (defined $pre_pointer_space && 4983 $pre_pointer_space =~ /^\s/) { 4984 WARN("SPACING", 4985 "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 4986 } 4987 4988# unnecessary space "type (* funcptr)(args...)" 4989 if (defined $post_pointer_space && 4990 $post_pointer_space =~ /^\s/) { 4991 WARN("SPACING", 4992 "Unnecessary space before function pointer name\n" . $herecurr); 4993 } 4994 4995# unnecessary space "type (*funcptr )(args...)" 4996 if (defined $post_funcname_space && 4997 $post_funcname_space =~ /^\s/) { 4998 WARN("SPACING", 4999 "Unnecessary space after function pointer name\n" . $herecurr); 5000 } 5001 5002# unnecessary space "type (*funcptr) (args...)" 5003 if (defined $pre_args_space && 5004 $pre_args_space =~ /^\s/) { 5005 WARN("SPACING", 5006 "Unnecessary space before function pointer arguments\n" . $herecurr); 5007 } 5008 5009 if (show_type("SPACING") && $fix) { 5010 $fixed[$fixlinenr] =~ 5011 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 5012 } 5013 } 5014 5015# check for spacing round square brackets; allowed: 5016# 1. with a type on the left -- int [] a; 5017# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 5018# 3. inside a curly brace -- = { [0...10] = 5 } 5019 while ($line =~ /(.*?\s)\[/g) { 5020 my ($where, $prefix) = ($-[1], $1); 5021 if ($prefix !~ /$Type\s+$/ && 5022 ($where != 0 || $prefix !~ /^.\s+$/) && 5023 $prefix !~ /[{,:]\s+$/) { 5024 if (ERROR("BRACKET_SPACE", 5025 "space prohibited before open square bracket '['\n" . $herecurr) && 5026 $fix) { 5027 $fixed[$fixlinenr] =~ 5028 s/^(\+.*?)\s+\[/$1\[/; 5029 } 5030 } 5031 } 5032 5033# check for spaces between functions and their parentheses. 5034 while ($line =~ /($Ident)\s+\(/g) { 5035 my $name = $1; 5036 my $ctx_before = substr($line, 0, $-[1]); 5037 my $ctx = "$ctx_before$name"; 5038 5039 # Ignore those directives where spaces _are_ permitted. 5040 if ($name =~ /^(?: 5041 if|for|while|switch|return|case| 5042 volatile|__volatile__| 5043 __attribute__|format|__extension__| 5044 asm|__asm__)$/x) 5045 { 5046 # cpp #define statements have non-optional spaces, ie 5047 # if there is a space between the name and the open 5048 # parenthesis it is simply not a parameter group. 5049 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 5050 5051 # cpp #elif statement condition may start with a ( 5052 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 5053 5054 # If this whole things ends with a type its most 5055 # likely a typedef for a function. 5056 } elsif ($ctx =~ /$Type$/) { 5057 5058 } else { 5059 if (WARN("SPACING", 5060 "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 5061 $fix) { 5062 $fixed[$fixlinenr] =~ 5063 s/\b$name\s+\(/$name\(/; 5064 } 5065 } 5066 } 5067 5068# Check operator spacing. 5069 if (!($line=~/\#\s*include/)) { 5070 my $fixed_line = ""; 5071 my $line_fixed = 0; 5072 5073 my $ops = qr{ 5074 <<=|>>=|<=|>=|==|!=| 5075 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 5076 =>|->|<<|>>|<|>|=|!|~| 5077 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 5078 \?:|\?|: 5079 }x; 5080 my @elements = split(/($ops|;)/, $opline); 5081 5082## print("element count: <" . $#elements . ">\n"); 5083## foreach my $el (@elements) { 5084## print("el: <$el>\n"); 5085## } 5086 5087 my @fix_elements = (); 5088 my $off = 0; 5089 5090 foreach my $el (@elements) { 5091 push(@fix_elements, substr($rawline, $off, length($el))); 5092 $off += length($el); 5093 } 5094 5095 $off = 0; 5096 5097 my $blank = copy_spacing($opline); 5098 my $last_after = -1; 5099 5100 for (my $n = 0; $n < $#elements; $n += 2) { 5101 5102 my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 5103 5104## print("n: <$n> good: <$good>\n"); 5105 5106 $off += length($elements[$n]); 5107 5108 # Pick up the preceding and succeeding characters. 5109 my $ca = substr($opline, 0, $off); 5110 my $cc = ''; 5111 if (length($opline) >= ($off + length($elements[$n + 1]))) { 5112 $cc = substr($opline, $off + length($elements[$n + 1])); 5113 } 5114 my $cb = "$ca$;$cc"; 5115 5116 my $a = ''; 5117 $a = 'V' if ($elements[$n] ne ''); 5118 $a = 'W' if ($elements[$n] =~ /\s$/); 5119 $a = 'C' if ($elements[$n] =~ /$;$/); 5120 $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 5121 $a = 'O' if ($elements[$n] eq ''); 5122 $a = 'E' if ($ca =~ /^\s*$/); 5123 5124 my $op = $elements[$n + 1]; 5125 5126 my $c = ''; 5127 if (defined $elements[$n + 2]) { 5128 $c = 'V' if ($elements[$n + 2] ne ''); 5129 $c = 'W' if ($elements[$n + 2] =~ /^\s/); 5130 $c = 'C' if ($elements[$n + 2] =~ /^$;/); 5131 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 5132 $c = 'O' if ($elements[$n + 2] eq ''); 5133 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 5134 } else { 5135 $c = 'E'; 5136 } 5137 5138 my $ctx = "${a}x${c}"; 5139 5140 my $at = "(ctx:$ctx)"; 5141 5142 my $ptr = substr($blank, 0, $off) . "^"; 5143 my $hereptr = "$hereline$ptr\n"; 5144 5145 # Pull out the value of this operator. 5146 my $op_type = substr($curr_values, $off + 1, 1); 5147 5148 # Get the full operator variant. 5149 my $opv = $op . substr($curr_vars, $off, 1); 5150 5151 # Ignore operators passed as parameters. 5152 if ($op_type ne 'V' && 5153 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 5154 5155# # Ignore comments 5156# } elsif ($op =~ /^$;+$/) { 5157 5158 # ; should have either the end of line or a space or \ after it 5159 } elsif ($op eq ';') { 5160 if ($ctx !~ /.x[WEBC]/ && 5161 $cc !~ /^\\/ && $cc !~ /^;/) { 5162 if (ERROR("SPACING", 5163 "space required after that '$op' $at\n" . $hereptr)) { 5164 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 5165 $line_fixed = 1; 5166 } 5167 } 5168 5169 # // is a comment 5170 } elsif ($op eq '//') { 5171 5172 # : when part of a bitfield 5173 } elsif ($opv eq ':B') { 5174 # skip the bitfield test for now 5175 5176 # No spaces for: 5177 # -> 5178 } elsif ($op eq '->') { 5179 if ($ctx =~ /Wx.|.xW/) { 5180 if (ERROR("SPACING", 5181 "spaces prohibited around that '$op' $at\n" . $hereptr)) { 5182 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 5183 if (defined $fix_elements[$n + 2]) { 5184 $fix_elements[$n + 2] =~ s/^\s+//; 5185 } 5186 $line_fixed = 1; 5187 } 5188 } 5189 5190 # , must not have a space before and must have a space on the right. 5191 } elsif ($op eq ',') { 5192 my $rtrim_before = 0; 5193 my $space_after = 0; 5194 if ($ctx =~ /Wx./) { 5195 if (ERROR("SPACING", 5196 "space prohibited before that '$op' $at\n" . $hereptr)) { 5197 $line_fixed = 1; 5198 $rtrim_before = 1; 5199 } 5200 } 5201 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 5202 if (ERROR("SPACING", 5203 "space required after that '$op' $at\n" . $hereptr)) { 5204 $line_fixed = 1; 5205 $last_after = $n; 5206 $space_after = 1; 5207 } 5208 } 5209 if ($rtrim_before || $space_after) { 5210 if ($rtrim_before) { 5211 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 5212 } else { 5213 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 5214 } 5215 if ($space_after) { 5216 $good .= " "; 5217 } 5218 } 5219 5220 # '*' as part of a type definition -- reported already. 5221 } elsif ($opv eq '*_') { 5222 #warn "'*' is part of type\n"; 5223 5224 # unary operators should have a space before and 5225 # none after. May be left adjacent to another 5226 # unary operator, or a cast 5227 } elsif ($op eq '!' || $op eq '~' || 5228 $opv eq '*U' || $opv eq '-U' || 5229 $opv eq '&U' || $opv eq '&&U') { 5230 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 5231 if (ERROR("SPACING", 5232 "space required before that '$op' $at\n" . $hereptr)) { 5233 if ($n != $last_after + 2) { 5234 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 5235 $line_fixed = 1; 5236 } 5237 } 5238 } 5239 if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 5240 # A unary '*' may be const 5241 5242 } elsif ($ctx =~ /.xW/) { 5243 if (ERROR("SPACING", 5244 "space prohibited after that '$op' $at\n" . $hereptr)) { 5245 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 5246 if (defined $fix_elements[$n + 2]) { 5247 $fix_elements[$n + 2] =~ s/^\s+//; 5248 } 5249 $line_fixed = 1; 5250 } 5251 } 5252 5253 # unary ++ and unary -- are allowed no space on one side. 5254 } elsif ($op eq '++' or $op eq '--') { 5255 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 5256 if (ERROR("SPACING", 5257 "space required one side of that '$op' $at\n" . $hereptr)) { 5258 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 5259 $line_fixed = 1; 5260 } 5261 } 5262 if ($ctx =~ /Wx[BE]/ || 5263 ($ctx =~ /Wx./ && $cc =~ /^;/)) { 5264 if (ERROR("SPACING", 5265 "space prohibited before that '$op' $at\n" . $hereptr)) { 5266 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 5267 $line_fixed = 1; 5268 } 5269 } 5270 if ($ctx =~ /ExW/) { 5271 if (ERROR("SPACING", 5272 "space prohibited after that '$op' $at\n" . $hereptr)) { 5273 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 5274 if (defined $fix_elements[$n + 2]) { 5275 $fix_elements[$n + 2] =~ s/^\s+//; 5276 } 5277 $line_fixed = 1; 5278 } 5279 } 5280 5281 # << and >> may either have or not have spaces both sides 5282 } elsif ($op eq '<<' or $op eq '>>' or 5283 $op eq '&' or $op eq '^' or $op eq '|' or 5284 $op eq '+' or $op eq '-' or 5285 $op eq '*' or $op eq '/' or 5286 $op eq '%') 5287 { 5288 if ($check) { 5289 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 5290 if (CHK("SPACING", 5291 "spaces preferred around that '$op' $at\n" . $hereptr)) { 5292 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5293 $fix_elements[$n + 2] =~ s/^\s+//; 5294 $line_fixed = 1; 5295 } 5296 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 5297 if (CHK("SPACING", 5298 "space preferred before that '$op' $at\n" . $hereptr)) { 5299 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 5300 $line_fixed = 1; 5301 } 5302 } 5303 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 5304 if (ERROR("SPACING", 5305 "need consistent spacing around '$op' $at\n" . $hereptr)) { 5306 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5307 if (defined $fix_elements[$n + 2]) { 5308 $fix_elements[$n + 2] =~ s/^\s+//; 5309 } 5310 $line_fixed = 1; 5311 } 5312 } 5313 5314 # A colon needs no spaces before when it is 5315 # terminating a case value or a label. 5316 } elsif ($opv eq ':C' || $opv eq ':L') { 5317 if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) { 5318 if (ERROR("SPACING", 5319 "space prohibited before that '$op' $at\n" . $hereptr)) { 5320 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 5321 $line_fixed = 1; 5322 } 5323 } 5324 5325 # All the others need spaces both sides. 5326 } elsif ($ctx !~ /[EWC]x[CWE]/) { 5327 my $ok = 0; 5328 5329 # Ignore email addresses <foo@bar> 5330 if (($op eq '<' && 5331 $cc =~ /^\S+\@\S+>/) || 5332 ($op eq '>' && 5333 $ca =~ /<\S+\@\S+$/)) 5334 { 5335 $ok = 1; 5336 } 5337 5338 # for asm volatile statements 5339 # ignore a colon with another 5340 # colon immediately before or after 5341 if (($op eq ':') && 5342 ($ca =~ /:$/ || $cc =~ /^:/)) { 5343 $ok = 1; 5344 } 5345 5346 # messages are ERROR, but ?: are CHK 5347 if ($ok == 0) { 5348 my $msg_level = \&ERROR; 5349 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 5350 5351 if (&{$msg_level}("SPACING", 5352 "spaces required around that '$op' $at\n" . $hereptr)) { 5353 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5354 if (defined $fix_elements[$n + 2]) { 5355 $fix_elements[$n + 2] =~ s/^\s+//; 5356 } 5357 $line_fixed = 1; 5358 } 5359 } 5360 } 5361 $off += length($elements[$n + 1]); 5362 5363## print("n: <$n> GOOD: <$good>\n"); 5364 5365 $fixed_line = $fixed_line . $good; 5366 } 5367 5368 if (($#elements % 2) == 0) { 5369 $fixed_line = $fixed_line . $fix_elements[$#elements]; 5370 } 5371 5372 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 5373 $fixed[$fixlinenr] = $fixed_line; 5374 } 5375 5376 5377 } 5378 5379# check for whitespace before a non-naked semicolon 5380 if ($line =~ /^\+.*\S\s+;\s*$/) { 5381 if (WARN("SPACING", 5382 "space prohibited before semicolon\n" . $herecurr) && 5383 $fix) { 5384 1 while $fixed[$fixlinenr] =~ 5385 s/^(\+.*\S)\s+;/$1;/; 5386 } 5387 } 5388 5389# check for multiple assignments 5390 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 5391 CHK("MULTIPLE_ASSIGNMENTS", 5392 "multiple assignments should be avoided\n" . $herecurr); 5393 } 5394 5395## # check for multiple declarations, allowing for a function declaration 5396## # continuation. 5397## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 5398## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 5399## 5400## # Remove any bracketed sections to ensure we do not 5401## # falsely report the parameters of functions. 5402## my $ln = $line; 5403## while ($ln =~ s/\([^\(\)]*\)//g) { 5404## } 5405## if ($ln =~ /,/) { 5406## WARN("MULTIPLE_DECLARATION", 5407## "declaring multiple variables together should be avoided\n" . $herecurr); 5408## } 5409## } 5410 5411#need space before brace following if, while, etc 5412 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 5413 $line =~ /\b(?:else|do)\{/) { 5414 if (ERROR("SPACING", 5415 "space required before the open brace '{'\n" . $herecurr) && 5416 $fix) { 5417 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 5418 } 5419 } 5420 5421## # check for blank lines before declarations 5422## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 5423## $prevrawline =~ /^.\s*$/) { 5424## WARN("SPACING", 5425## "No blank lines before declarations\n" . $hereprev); 5426## } 5427## 5428 5429# closing brace should have a space following it when it has anything 5430# on the line 5431 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 5432 if (ERROR("SPACING", 5433 "space required after that close brace '}'\n" . $herecurr) && 5434 $fix) { 5435 $fixed[$fixlinenr] =~ 5436 s/}((?!(?:,|;|\)))\S)/} $1/; 5437 } 5438 } 5439 5440# check spacing on square brackets 5441 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 5442 if (ERROR("SPACING", 5443 "space prohibited after that open square bracket '['\n" . $herecurr) && 5444 $fix) { 5445 $fixed[$fixlinenr] =~ 5446 s/\[\s+/\[/; 5447 } 5448 } 5449 if ($line =~ /\s\]/) { 5450 if (ERROR("SPACING", 5451 "space prohibited before that close square bracket ']'\n" . $herecurr) && 5452 $fix) { 5453 $fixed[$fixlinenr] =~ 5454 s/\s+\]/\]/; 5455 } 5456 } 5457 5458# check spacing on parentheses 5459 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 5460 $line !~ /for\s*\(\s+;/) { 5461 if (ERROR("SPACING", 5462 "space prohibited after that open parenthesis '('\n" . $herecurr) && 5463 $fix) { 5464 $fixed[$fixlinenr] =~ 5465 s/\(\s+/\(/; 5466 } 5467 } 5468 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 5469 $line !~ /for\s*\(.*;\s+\)/ && 5470 $line !~ /:\s+\)/) { 5471 if (ERROR("SPACING", 5472 "space prohibited before that close parenthesis ')'\n" . $herecurr) && 5473 $fix) { 5474 $fixed[$fixlinenr] =~ 5475 s/\s+\)/\)/; 5476 } 5477 } 5478 5479# check unnecessary parentheses around addressof/dereference single $Lvals 5480# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 5481 5482 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 5483 my $var = $1; 5484 if (CHK("UNNECESSARY_PARENTHESES", 5485 "Unnecessary parentheses around $var\n" . $herecurr) && 5486 $fix) { 5487 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 5488 } 5489 } 5490 5491# check for unnecessary parentheses around function pointer uses 5492# ie: (foo->bar)(); should be foo->bar(); 5493# but not "if (foo->bar) (" to avoid some false positives 5494 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 5495 my $var = $2; 5496 if (CHK("UNNECESSARY_PARENTHESES", 5497 "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 5498 $fix) { 5499 my $var2 = deparenthesize($var); 5500 $var2 =~ s/\s//g; 5501 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 5502 } 5503 } 5504 5505# check for unnecessary parentheses around comparisons in if uses 5506# when !drivers/staging or command-line uses --strict 5507 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 5508 $perl_version_ok && defined($stat) && 5509 $stat =~ /(^.\s*if\s*($balanced_parens))/) { 5510 my $if_stat = $1; 5511 my $test = substr($2, 1, -1); 5512 my $herectx; 5513 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 5514 my $match = $1; 5515 # avoid parentheses around potential macro args 5516 next if ($match =~ /^\s*\w+\s*$/); 5517 if (!defined($herectx)) { 5518 $herectx = $here . "\n"; 5519 my $cnt = statement_rawlines($if_stat); 5520 for (my $n = 0; $n < $cnt; $n++) { 5521 my $rl = raw_line($linenr, $n); 5522 $herectx .= $rl . "\n"; 5523 last if $rl =~ /^[ \+].*\{/; 5524 } 5525 } 5526 CHK("UNNECESSARY_PARENTHESES", 5527 "Unnecessary parentheses around '$match'\n" . $herectx); 5528 } 5529 } 5530 5531# check that goto labels aren't indented (allow a single space indentation) 5532# and ignore bitfield definitions like foo:1 5533# Strictly, labels can have whitespace after the identifier and before the : 5534# but this is not allowed here as many ?: uses would appear to be labels 5535 if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ && 5536 $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ && 5537 $sline !~ /^.\s+default:/) { 5538 if (WARN("INDENTED_LABEL", 5539 "labels should not be indented\n" . $herecurr) && 5540 $fix) { 5541 $fixed[$fixlinenr] =~ 5542 s/^(.)\s+/$1/; 5543 } 5544 } 5545 5546# check if a statement with a comma should be two statements like: 5547# foo = bar(), /* comma should be semicolon */ 5548# bar = baz(); 5549 if (defined($stat) && 5550 $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { 5551 my $cnt = statement_rawlines($stat); 5552 my $herectx = get_stat_here($linenr, $cnt, $here); 5553 WARN("SUSPECT_COMMA_SEMICOLON", 5554 "Possible comma where semicolon could be used\n" . $herectx); 5555 } 5556 5557# return is not a function 5558 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 5559 my $spacing = $1; 5560 if ($perl_version_ok && 5561 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 5562 my $value = $1; 5563 $value = deparenthesize($value); 5564 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 5565 ERROR("RETURN_PARENTHESES", 5566 "return is not a function, parentheses are not required\n" . $herecurr); 5567 } 5568 } elsif ($spacing !~ /\s+/) { 5569 ERROR("SPACING", 5570 "space required before the open parenthesis '('\n" . $herecurr); 5571 } 5572 } 5573 5574# unnecessary return in a void function 5575# at end-of-function, with the previous line a single leading tab, then return; 5576# and the line before that not a goto label target like "out:" 5577 if ($sline =~ /^[ \+]}\s*$/ && 5578 $prevline =~ /^\+\treturn\s*;\s*$/ && 5579 $linenr >= 3 && 5580 $lines[$linenr - 3] =~ /^[ +]/ && 5581 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 5582 WARN("RETURN_VOID", 5583 "void function return statements are not generally useful\n" . $hereprev); 5584 } 5585 5586# if statements using unnecessary parentheses - ie: if ((foo == bar)) 5587 if ($perl_version_ok && 5588 $line =~ /\bif\s*((?:\(\s*){2,})/) { 5589 my $openparens = $1; 5590 my $count = $openparens =~ tr@\(@\(@; 5591 my $msg = ""; 5592 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 5593 my $comp = $4; #Not $1 because of $LvalOrFunc 5594 $msg = " - maybe == should be = ?" if ($comp eq "=="); 5595 WARN("UNNECESSARY_PARENTHESES", 5596 "Unnecessary parentheses$msg\n" . $herecurr); 5597 } 5598 } 5599 5600# comparisons with a constant or upper case identifier on the left 5601# avoid cases like "foo + BAR < baz" 5602# only fix matches surrounded by parentheses to avoid incorrect 5603# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 5604 if ($perl_version_ok && 5605 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 5606 my $lead = $1; 5607 my $const = $2; 5608 my $comp = $3; 5609 my $to = $4; 5610 my $newcomp = $comp; 5611 if ($lead !~ /(?:$Operators|\.)\s*$/ && 5612 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 5613 WARN("CONSTANT_COMPARISON", 5614 "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 5615 $fix) { 5616 if ($comp eq "<") { 5617 $newcomp = ">"; 5618 } elsif ($comp eq "<=") { 5619 $newcomp = ">="; 5620 } elsif ($comp eq ">") { 5621 $newcomp = "<"; 5622 } elsif ($comp eq ">=") { 5623 $newcomp = "<="; 5624 } 5625 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 5626 } 5627 } 5628 5629# Return of what appears to be an errno should normally be negative 5630 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 5631 my $name = $1; 5632 if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) { 5633 WARN("USE_NEGATIVE_ERRNO", 5634 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 5635 } 5636 } 5637 5638# Need a space before open parenthesis after if, while etc 5639 if ($line =~ /\b(if|while|for|switch)\(/) { 5640 if (ERROR("SPACING", 5641 "space required before the open parenthesis '('\n" . $herecurr) && 5642 $fix) { 5643 $fixed[$fixlinenr] =~ 5644 s/\b(if|while|for|switch)\(/$1 \(/; 5645 } 5646 } 5647 5648# Check for illegal assignment in if conditional -- and check for trailing 5649# statements after the conditional. 5650 if ($line =~ /do\s*(?!{)/) { 5651 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 5652 ctx_statement_block($linenr, $realcnt, 0) 5653 if (!defined $stat); 5654 my ($stat_next) = ctx_statement_block($line_nr_next, 5655 $remain_next, $off_next); 5656 $stat_next =~ s/\n./\n /g; 5657 ##print "stat<$stat> stat_next<$stat_next>\n"; 5658 5659 if ($stat_next =~ /^\s*while\b/) { 5660 # If the statement carries leading newlines, 5661 # then count those as offsets. 5662 my ($whitespace) = 5663 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 5664 my $offset = 5665 statement_rawlines($whitespace) - 1; 5666 5667 $suppress_whiletrailers{$line_nr_next + 5668 $offset} = 1; 5669 } 5670 } 5671 if (!defined $suppress_whiletrailers{$linenr} && 5672 defined($stat) && defined($cond) && 5673 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 5674 my ($s, $c) = ($stat, $cond); 5675 my $fixed_assign_in_if = 0; 5676 5677 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 5678 if (ERROR("ASSIGN_IN_IF", 5679 "do not use assignment in if condition\n" . $herecurr) && 5680 $fix && $perl_version_ok) { 5681 if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { 5682 my $space = $1; 5683 my $not = $2; 5684 my $statement = $3; 5685 my $assigned = $4; 5686 my $test = $8; 5687 my $against = $9; 5688 my $brace = $15; 5689 fix_delete_line($fixlinenr, $rawline); 5690 fix_insert_line($fixlinenr, "$space$statement;"); 5691 my $newline = "${space}if ("; 5692 $newline .= '!' if defined($not); 5693 $newline .= '(' if (defined $not && defined($test) && defined($against)); 5694 $newline .= "$assigned"; 5695 $newline .= " $test $against" if (defined($test) && defined($against)); 5696 $newline .= ')' if (defined $not && defined($test) && defined($against)); 5697 $newline .= ')'; 5698 $newline .= " {" if (defined($brace)); 5699 fix_insert_line($fixlinenr + 1, $newline); 5700 $fixed_assign_in_if = 1; 5701 } 5702 } 5703 } 5704 5705 # Find out what is on the end of the line after the 5706 # conditional. 5707 substr($s, 0, length($c), ''); 5708 $s =~ s/\n.*//g; 5709 $s =~ s/$;//g; # Remove any comments 5710 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 5711 $c !~ /}\s*while\s*/) 5712 { 5713 # Find out how long the conditional actually is. 5714 my @newlines = ($c =~ /\n/gs); 5715 my $cond_lines = 1 + $#newlines; 5716 my $stat_real = ''; 5717 5718 $stat_real = raw_line($linenr, $cond_lines) 5719 . "\n" if ($cond_lines); 5720 if (defined($stat_real) && $cond_lines > 1) { 5721 $stat_real = "[...]\n$stat_real"; 5722 } 5723 5724 if (ERROR("TRAILING_STATEMENTS", 5725 "trailing statements should be on next line\n" . $herecurr . $stat_real) && 5726 !$fixed_assign_in_if && 5727 $cond_lines == 0 && 5728 $fix && $perl_version_ok && 5729 $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) { 5730 my $indent = $1; 5731 my $test = $2; 5732 my $rest = rtrim($4); 5733 if ($rest =~ /;$/) { 5734 $fixed[$fixlinenr] = "\+$indent$test"; 5735 fix_insert_line($fixlinenr + 1, "$indent\t$rest"); 5736 } 5737 } 5738 } 5739 } 5740 5741# Check for bitwise tests written as boolean 5742 if ($line =~ / 5743 (?: 5744 (?:\[|\(|\&\&|\|\|) 5745 \s*0[xX][0-9]+\s* 5746 (?:\&\&|\|\|) 5747 | 5748 (?:\&\&|\|\|) 5749 \s*0[xX][0-9]+\s* 5750 (?:\&\&|\|\||\)|\]) 5751 )/x) 5752 { 5753 WARN("HEXADECIMAL_BOOLEAN_TEST", 5754 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 5755 } 5756 5757# if and else should not have general statements after it 5758 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 5759 my $s = $1; 5760 $s =~ s/$;//g; # Remove any comments 5761 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5762 ERROR("TRAILING_STATEMENTS", 5763 "trailing statements should be on next line\n" . $herecurr); 5764 } 5765 } 5766# if should not continue a brace 5767 if ($line =~ /}\s*if\b/) { 5768 ERROR("TRAILING_STATEMENTS", 5769 "trailing statements should be on next line (or did you mean 'else if'?)\n" . 5770 $herecurr); 5771 } 5772# case and default should not have general statements after them 5773 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5774 $line !~ /\G(?: 5775 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5776 \s*return\s+ 5777 )/xg) 5778 { 5779 ERROR("TRAILING_STATEMENTS", 5780 "trailing statements should be on next line\n" . $herecurr); 5781 } 5782 5783 # Check for }<nl>else {, these must be at the same 5784 # indent level to be relevant to each other. 5785 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 5786 $previndent == $indent) { 5787 if (ERROR("ELSE_AFTER_BRACE", 5788 "else should follow close brace '}'\n" . $hereprev) && 5789 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 5790 fix_delete_line($fixlinenr - 1, $prevrawline); 5791 fix_delete_line($fixlinenr, $rawline); 5792 my $fixedline = $prevrawline; 5793 $fixedline =~ s/}\s*$//; 5794 if ($fixedline !~ /^\+\s*$/) { 5795 fix_insert_line($fixlinenr, $fixedline); 5796 } 5797 $fixedline = $rawline; 5798 $fixedline =~ s/^(.\s*)else/$1} else/; 5799 fix_insert_line($fixlinenr, $fixedline); 5800 } 5801 } 5802 5803 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5804 $previndent == $indent) { 5805 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5806 5807 # Find out what is on the end of the line after the 5808 # conditional. 5809 substr($s, 0, length($c), ''); 5810 $s =~ s/\n.*//g; 5811 5812 if ($s =~ /^\s*;/) { 5813 if (ERROR("WHILE_AFTER_BRACE", 5814 "while should follow close brace '}'\n" . $hereprev) && 5815 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 5816 fix_delete_line($fixlinenr - 1, $prevrawline); 5817 fix_delete_line($fixlinenr, $rawline); 5818 my $fixedline = $prevrawline; 5819 my $trailing = $rawline; 5820 $trailing =~ s/^\+//; 5821 $trailing = trim($trailing); 5822 $fixedline =~ s/}\s*$/} $trailing/; 5823 fix_insert_line($fixlinenr, $fixedline); 5824 } 5825 } 5826 } 5827 5828#Specific variable tests 5829 while ($line =~ m{($Constant|$Lval)}g) { 5830 my $var = $1; 5831 5832#CamelCase 5833 if ($var !~ /^$Constant$/ && 5834 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 5835#Ignore some autogenerated defines and enum values 5836 $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ && 5837#Ignore Page<foo> variants 5838 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5839#Ignore ETHTOOL_LINK_MODE_<foo> variants 5840 $var !~ /^ETHTOOL_LINK_MODE_/ && 5841#Ignore SI style variants like nS, mV and dB 5842#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5843 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5844#Ignore some three character SI units explicitly, like MiB and KHz 5845 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 5846 while ($var =~ m{\b($Ident)}g) { 5847 my $word = $1; 5848 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5849 if ($check) { 5850 seed_camelcase_includes(); 5851 if (!$file && !$camelcase_file_seeded) { 5852 seed_camelcase_file($realfile); 5853 $camelcase_file_seeded = 1; 5854 } 5855 } 5856 if (!defined $camelcase{$word}) { 5857 $camelcase{$word} = 1; 5858 CHK("CAMELCASE", 5859 "Avoid CamelCase: <$word>\n" . $herecurr); 5860 } 5861 } 5862 } 5863 } 5864 5865#no spaces allowed after \ in define 5866 if ($line =~ /\#\s*define.*\\\s+$/) { 5867 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5868 "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5869 $fix) { 5870 $fixed[$fixlinenr] =~ s/\s+$//; 5871 } 5872 } 5873 5874# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 5875# itself <asm/foo.h> (uses RAW line) 5876 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5877 my $file = "$1.h"; 5878 my $checkfile = "include/linux/$file"; 5879 if (-f "$root/$checkfile" && 5880 $realfile ne $checkfile && 5881 $1 !~ /$allowed_asm_includes/) 5882 { 5883 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 5884 if ($asminclude > 0) { 5885 if ($realfile =~ m{^arch/}) { 5886 CHK("ARCH_INCLUDE_LINUX", 5887 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5888 } else { 5889 WARN("INCLUDE_LINUX", 5890 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5891 } 5892 } 5893 } 5894 } 5895 5896# multi-statement macros should be enclosed in a do while loop, grab the 5897# first statement and ensure its the whole macro if its not enclosed 5898# in a known good container 5899 if ($realfile !~ m@/vmlinux.lds.h$@ && 5900 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5901 my $ln = $linenr; 5902 my $cnt = $realcnt; 5903 my ($off, $dstat, $dcond, $rest); 5904 my $ctx = ''; 5905 my $has_flow_statement = 0; 5906 my $has_arg_concat = 0; 5907 ($dstat, $dcond, $ln, $cnt, $off) = 5908 ctx_statement_block($linenr, $realcnt, 0); 5909 $ctx = $dstat; 5910 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5911 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5912 5913 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 5914 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 5915 5916 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5917 my $define_args = $1; 5918 my $define_stmt = $dstat; 5919 my @def_args = (); 5920 5921 if (defined $define_args && $define_args ne "") { 5922 $define_args = substr($define_args, 1, length($define_args) - 2); 5923 $define_args =~ s/\s*//g; 5924 $define_args =~ s/\\\+?//g; 5925 @def_args = split(",", $define_args); 5926 } 5927 5928 $dstat =~ s/$;//g; 5929 $dstat =~ s/\\\n.//g; 5930 $dstat =~ s/^\s*//s; 5931 $dstat =~ s/\s*$//s; 5932 5933 # Flatten any parentheses and braces 5934 while ($dstat =~ s/\([^\(\)]*\)/1u/ || 5935 $dstat =~ s/\{[^\{\}]*\}/1u/ || 5936 $dstat =~ s/.\[[^\[\]]*\]/1u/) 5937 { 5938 } 5939 5940 # Flatten any obvious string concatenation. 5941 while ($dstat =~ s/($String)\s*$Ident/$1/ || 5942 $dstat =~ s/$Ident\s*($String)/$1/) 5943 { 5944 } 5945 5946 # Make asm volatile uses seem like a generic function 5947 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 5948 5949 my $exceptions = qr{ 5950 $Declare| 5951 module_param_named| 5952 MODULE_PARM_DESC| 5953 DECLARE_PER_CPU| 5954 DEFINE_PER_CPU| 5955 __typeof__\(| 5956 union| 5957 struct| 5958 \.$Ident\s*=\s*| 5959 ^\"|\"$| 5960 ^\[ 5961 }x; 5962 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5963 5964 $ctx =~ s/\n*$//; 5965 my $stmt_cnt = statement_rawlines($ctx); 5966 my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5967 5968 if ($dstat ne '' && 5969 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5970 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 5971 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5972 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5973 $dstat !~ /$exceptions/ && 5974 $dstat !~ /^\.$Ident\s*=/ && # .foo = 5975 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 5976 $dstat !~ /^case\b/ && # case ... 5977 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5978 $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} 5979 $dstat !~ /^for\s*$Constant$/ && # for (...) 5980 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5981 $dstat !~ /^do\s*{/ && # do {... 5982 $dstat !~ /^\(\{/ && # ({... 5983 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5984 { 5985 if ($dstat =~ /^\s*if\b/) { 5986 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5987 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5988 } elsif ($dstat =~ /;/) { 5989 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5990 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5991 } else { 5992 ERROR("COMPLEX_MACRO", 5993 "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5994 } 5995 5996 } 5997 5998 # Make $define_stmt single line, comment-free, etc 5999 my @stmt_array = split('\n', $define_stmt); 6000 my $first = 1; 6001 $define_stmt = ""; 6002 foreach my $l (@stmt_array) { 6003 $l =~ s/\\$//; 6004 if ($first) { 6005 $define_stmt = $l; 6006 $first = 0; 6007 } elsif ($l =~ /^[\+ ]/) { 6008 $define_stmt .= substr($l, 1); 6009 } 6010 } 6011 $define_stmt =~ s/$;//g; 6012 $define_stmt =~ s/\s+/ /g; 6013 $define_stmt = trim($define_stmt); 6014 6015# check if any macro arguments are reused (ignore '...' and 'type') 6016 foreach my $arg (@def_args) { 6017 next if ($arg =~ /\.\.\./); 6018 next if ($arg =~ /^type$/i); 6019 my $tmp_stmt = $define_stmt; 6020 $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 6021 $tmp_stmt =~ s/\#+\s*$arg\b//g; 6022 $tmp_stmt =~ s/\b$arg\s*\#\#//g; 6023 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 6024 if ($use_cnt > 1) { 6025 CHK("MACRO_ARG_REUSE", 6026 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 6027 } 6028# check if any macro arguments may have other precedence issues 6029 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 6030 ((defined($1) && $1 ne ',') || 6031 (defined($2) && $2 ne ','))) { 6032 CHK("MACRO_ARG_PRECEDENCE", 6033 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 6034 } 6035 } 6036 6037# check for macros with flow control, but without ## concatenation 6038# ## concatenation is commonly a macro that defines a function so ignore those 6039 if ($has_flow_statement && !$has_arg_concat) { 6040 my $cnt = statement_rawlines($ctx); 6041 my $herectx = get_stat_here($linenr, $cnt, $here); 6042 6043 WARN("MACRO_WITH_FLOW_CONTROL", 6044 "Macros with flow control statements should be avoided\n" . "$herectx"); 6045 } 6046 6047# check for line continuations outside of #defines, preprocessor #, and asm 6048 6049 } else { 6050 if ($prevline !~ /^..*\\$/ && 6051 $line !~ /^\+\s*\#.*\\$/ && # preprocessor 6052 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 6053 $line =~ /^\+.*\\$/) { 6054 WARN("LINE_CONTINUATIONS", 6055 "Avoid unnecessary line continuations\n" . $herecurr); 6056 } 6057 } 6058 6059# do {} while (0) macro tests: 6060# single-statement macros do not need to be enclosed in do while (0) loop, 6061# macro should not end with a semicolon 6062 if ($perl_version_ok && 6063 $realfile !~ m@/vmlinux.lds.h$@ && 6064 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 6065 my $ln = $linenr; 6066 my $cnt = $realcnt; 6067 my ($off, $dstat, $dcond, $rest); 6068 my $ctx = ''; 6069 ($dstat, $dcond, $ln, $cnt, $off) = 6070 ctx_statement_block($linenr, $realcnt, 0); 6071 $ctx = $dstat; 6072 6073 $dstat =~ s/\\\n.//g; 6074 $dstat =~ s/$;/ /g; 6075 6076 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 6077 my $stmts = $2; 6078 my $semis = $3; 6079 6080 $ctx =~ s/\n*$//; 6081 my $cnt = statement_rawlines($ctx); 6082 my $herectx = get_stat_here($linenr, $cnt, $here); 6083 6084 if (($stmts =~ tr/;/;/) == 1 && 6085 $stmts !~ /^\s*(if|while|for|switch)\b/) { 6086 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 6087 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 6088 } 6089 if (defined $semis && $semis ne "") { 6090 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 6091 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 6092 } 6093 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 6094 $ctx =~ s/\n*$//; 6095 my $cnt = statement_rawlines($ctx); 6096 my $herectx = get_stat_here($linenr, $cnt, $here); 6097 6098 WARN("TRAILING_SEMICOLON", 6099 "macros should not use a trailing semicolon\n" . "$herectx"); 6100 } 6101 } 6102 6103# check for redundant bracing round if etc 6104 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 6105 my ($level, $endln, @chunks) = 6106 ctx_statement_full($linenr, $realcnt, 1); 6107 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 6108 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 6109 if ($#chunks > 0 && $level == 0) { 6110 my @allowed = (); 6111 my $allow = 0; 6112 my $seen = 0; 6113 my $herectx = $here . "\n"; 6114 my $ln = $linenr - 1; 6115 for my $chunk (@chunks) { 6116 my ($cond, $block) = @{$chunk}; 6117 6118 # If the condition carries leading newlines, then count those as offsets. 6119 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 6120 my $offset = statement_rawlines($whitespace) - 1; 6121 6122 $allowed[$allow] = 0; 6123 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 6124 6125 # We have looked at and allowed this specific line. 6126 $suppress_ifbraces{$ln + $offset} = 1; 6127 6128 $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 6129 $ln += statement_rawlines($block) - 1; 6130 6131 substr($block, 0, length($cond), ''); 6132 6133 $seen++ if ($block =~ /^\s*{/); 6134 6135 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 6136 if (statement_lines($cond) > 1) { 6137 #print "APW: ALLOWED: cond<$cond>\n"; 6138 $allowed[$allow] = 1; 6139 } 6140 if ($block =~/\b(?:if|for|while)\b/) { 6141 #print "APW: ALLOWED: block<$block>\n"; 6142 $allowed[$allow] = 1; 6143 } 6144 if (statement_block_size($block) > 1) { 6145 #print "APW: ALLOWED: lines block<$block>\n"; 6146 $allowed[$allow] = 1; 6147 } 6148 $allow++; 6149 } 6150 if ($seen) { 6151 my $sum_allowed = 0; 6152 foreach (@allowed) { 6153 $sum_allowed += $_; 6154 } 6155 if ($sum_allowed == 0) { 6156 WARN("BRACES", 6157 "braces {} are not necessary for any arm of this statement\n" . $herectx); 6158 } elsif ($sum_allowed != $allow && 6159 $seen != $allow) { 6160 CHK("BRACES", 6161 "braces {} should be used on all arms of this statement\n" . $herectx); 6162 } 6163 } 6164 } 6165 } 6166 if (!defined $suppress_ifbraces{$linenr - 1} && 6167 $line =~ /\b(if|while|for|else)\b/) { 6168 my $allowed = 0; 6169 6170 # Check the pre-context. 6171 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 6172 #print "APW: ALLOWED: pre<$1>\n"; 6173 $allowed = 1; 6174 } 6175 6176 my ($level, $endln, @chunks) = 6177 ctx_statement_full($linenr, $realcnt, $-[0]); 6178 6179 # Check the condition. 6180 my ($cond, $block) = @{$chunks[0]}; 6181 #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 6182 if (defined $cond) { 6183 substr($block, 0, length($cond), ''); 6184 } 6185 if (statement_lines($cond) > 1) { 6186 #print "APW: ALLOWED: cond<$cond>\n"; 6187 $allowed = 1; 6188 } 6189 if ($block =~/\b(?:if|for|while)\b/) { 6190 #print "APW: ALLOWED: block<$block>\n"; 6191 $allowed = 1; 6192 } 6193 if (statement_block_size($block) > 1) { 6194 #print "APW: ALLOWED: lines block<$block>\n"; 6195 $allowed = 1; 6196 } 6197 # Check the post-context. 6198 if (defined $chunks[1]) { 6199 my ($cond, $block) = @{$chunks[1]}; 6200 if (defined $cond) { 6201 substr($block, 0, length($cond), ''); 6202 } 6203 if ($block =~ /^\s*\{/) { 6204 #print "APW: ALLOWED: chunk-1 block<$block>\n"; 6205 $allowed = 1; 6206 } 6207 } 6208 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 6209 my $cnt = statement_rawlines($block); 6210 my $herectx = get_stat_here($linenr, $cnt, $here); 6211 6212 WARN("BRACES", 6213 "braces {} are not necessary for single statement blocks\n" . $herectx); 6214 } 6215 } 6216 6217# check for single line unbalanced braces 6218 if ($sline =~ /^.\s*\}\s*else\s*$/ || 6219 $sline =~ /^.\s*else\s*\{\s*$/) { 6220 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 6221 } 6222 6223# check for unnecessary blank lines around braces 6224 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 6225 if (CHK("BRACES", 6226 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 6227 $fix && $prevrawline =~ /^\+/) { 6228 fix_delete_line($fixlinenr - 1, $prevrawline); 6229 } 6230 } 6231 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 6232 if (CHK("BRACES", 6233 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 6234 $fix) { 6235 fix_delete_line($fixlinenr, $rawline); 6236 } 6237 } 6238 6239# no volatiles please 6240 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 6241 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 6242 WARN("VOLATILE", 6243 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 6244 } 6245 6246# Check for user-visible strings broken across lines, which breaks the ability 6247# to grep for the string. Make exceptions when the previous string ends in a 6248# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 6249# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 6250 if ($line =~ /^\+\s*$String/ && 6251 $prevline =~ /"\s*$/ && 6252 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 6253 if (WARN("SPLIT_STRING", 6254 "quoted string split across lines\n" . $hereprev) && 6255 $fix && 6256 $prevrawline =~ /^\+.*"\s*$/ && 6257 $last_coalesced_string_linenr != $linenr - 1) { 6258 my $extracted_string = get_quoted_string($line, $rawline); 6259 my $comma_close = ""; 6260 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 6261 $comma_close = $1; 6262 } 6263 6264 fix_delete_line($fixlinenr - 1, $prevrawline); 6265 fix_delete_line($fixlinenr, $rawline); 6266 my $fixedline = $prevrawline; 6267 $fixedline =~ s/"\s*$//; 6268 $fixedline .= substr($extracted_string, 1) . trim($comma_close); 6269 fix_insert_line($fixlinenr - 1, $fixedline); 6270 $fixedline = $rawline; 6271 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 6272 if ($fixedline !~ /\+\s*$/) { 6273 fix_insert_line($fixlinenr, $fixedline); 6274 } 6275 $last_coalesced_string_linenr = $linenr; 6276 } 6277 } 6278 6279# check for missing a space in a string concatenation 6280 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 6281 WARN('MISSING_SPACE', 6282 "break quoted strings at a space character\n" . $hereprev); 6283 } 6284 6285# check for an embedded function name in a string when the function is known 6286# This does not work very well for -f --file checking as it depends on patch 6287# context providing the function name or a single line form for in-file 6288# function declarations 6289 if ($line =~ /^\+.*$String/ && 6290 defined($context_function) && 6291 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 6292 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 6293 WARN("EMBEDDED_FUNCTION_NAME", 6294 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 6295 } 6296 6297# check for unnecessary function tracing like uses 6298# This does not use $logFunctions because there are many instances like 6299# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions 6300 if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) { 6301 if (WARN("TRACING_LOGGING", 6302 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) && 6303 $fix) { 6304 fix_delete_line($fixlinenr, $rawline); 6305 } 6306 } 6307 6308# check for spaces before a quoted newline 6309 if ($rawline =~ /^.*\".*\s\\n/) { 6310 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 6311 "unnecessary whitespace before a quoted newline\n" . $herecurr) && 6312 $fix) { 6313 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 6314 } 6315 6316 } 6317 6318# concatenated string without spaces between elements 6319 if ($line =~ /$String[A-Z_]/ || 6320 ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) { 6321 if (CHK("CONCATENATED_STRING", 6322 "Concatenated strings should use spaces between elements\n" . $herecurr) && 6323 $fix) { 6324 while ($line =~ /($String)/g) { 6325 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 6326 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 6327 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 6328 } 6329 } 6330 } 6331 6332# uncoalesced string fragments 6333 if ($line =~ /$String\s*[Lu]?"/) { 6334 if (WARN("STRING_FRAGMENTS", 6335 "Consecutive strings are generally better as a single string\n" . $herecurr) && 6336 $fix) { 6337 while ($line =~ /($String)(?=\s*")/g) { 6338 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 6339 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 6340 } 6341 } 6342 } 6343 6344# check for non-standard and hex prefixed decimal printf formats 6345 my $show_L = 1; #don't show the same defect twice 6346 my $show_Z = 1; 6347 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 6348 my $string = substr($rawline, $-[1], $+[1] - $-[1]); 6349 $string =~ s/%%/__/g; 6350 # check for %L 6351 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 6352 WARN("PRINTF_L", 6353 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 6354 $show_L = 0; 6355 } 6356 # check for %Z 6357 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 6358 WARN("PRINTF_Z", 6359 "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 6360 $show_Z = 0; 6361 } 6362 # check for 0x<decimal> 6363 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 6364 ERROR("PRINTF_0XDECIMAL", 6365 "Prefixing 0x with decimal output is defective\n" . $herecurr); 6366 } 6367 } 6368 6369# check for line continuations in quoted strings with odd counts of " 6370 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 6371 WARN("LINE_CONTINUATIONS", 6372 "Avoid line continuations in quoted strings\n" . $herecurr); 6373 } 6374 6375# warn about #if 0 6376 if ($line =~ /^.\s*\#\s*if\s+0\b/) { 6377 WARN("IF_0", 6378 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 6379 } 6380 6381# warn about #if 1 6382 if ($line =~ /^.\s*\#\s*if\s+1\b/) { 6383 WARN("IF_1", 6384 "Consider removing the #if 1 and its #endif\n" . $herecurr); 6385 } 6386 6387# check for needless "if (<foo>) fn(<foo>)" uses 6388 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 6389 my $tested = quotemeta($1); 6390 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 6391 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 6392 my $func = $1; 6393 if (WARN('NEEDLESS_IF', 6394 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 6395 $fix) { 6396 my $do_fix = 1; 6397 my $leading_tabs = ""; 6398 my $new_leading_tabs = ""; 6399 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 6400 $leading_tabs = $1; 6401 } else { 6402 $do_fix = 0; 6403 } 6404 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 6405 $new_leading_tabs = $1; 6406 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 6407 $do_fix = 0; 6408 } 6409 } else { 6410 $do_fix = 0; 6411 } 6412 if ($do_fix) { 6413 fix_delete_line($fixlinenr - 1, $prevrawline); 6414 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 6415 } 6416 } 6417 } 6418 } 6419 6420# check for unnecessary "Out of Memory" messages 6421 if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 6422 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 6423 (defined $1 || defined $3) && 6424 $linenr > 3) { 6425 my $testval = $2; 6426 my $testline = $lines[$linenr - 3]; 6427 6428 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 6429# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 6430 6431 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 6432 $s !~ /\b__GFP_NOWARN\b/ ) { 6433 WARN("OOM_MESSAGE", 6434 "Possible unnecessary 'out of memory' message\n" . $hereprev); 6435 } 6436 } 6437 6438# check for logging functions with KERN_<LEVEL> 6439 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 6440 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 6441 my $level = $1; 6442 if (WARN("UNNECESSARY_KERN_LEVEL", 6443 "Possible unnecessary $level\n" . $herecurr) && 6444 $fix) { 6445 $fixed[$fixlinenr] =~ s/\s*$level\s*//; 6446 } 6447 } 6448 6449# check for logging continuations 6450 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 6451 WARN("LOGGING_CONTINUATION", 6452 "Avoid logging continuation uses where feasible\n" . $herecurr); 6453 } 6454 6455# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions 6456 if (defined $stat && 6457 $line =~ /\b$logFunctions\s*\(/ && 6458 index($stat, '"') >= 0) { 6459 my $lc = $stat =~ tr@\n@@; 6460 $lc = $lc + $linenr; 6461 my $stat_real = get_stat_real($linenr, $lc); 6462 pos($stat_real) = index($stat_real, '"'); 6463 while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { 6464 my $pspec = $1; 6465 my $h = $2; 6466 my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; 6467 if (WARN("UNNECESSARY_MODIFIER", 6468 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && 6469 $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { 6470 my $nspec = $pspec; 6471 $nspec =~ s/h//g; 6472 $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; 6473 } 6474 } 6475 } 6476 6477# check for mask then right shift without a parentheses 6478 if ($perl_version_ok && 6479 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 6480 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 6481 WARN("MASK_THEN_SHIFT", 6482 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 6483 } 6484 6485# check for pointer comparisons to NULL 6486 if ($perl_version_ok) { 6487 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 6488 my $val = $1; 6489 my $equal = "!"; 6490 $equal = "" if ($4 eq "!="); 6491 if (CHK("COMPARISON_TO_NULL", 6492 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 6493 $fix) { 6494 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 6495 } 6496 } 6497 } 6498 6499# check for bad placement of section $InitAttribute (e.g.: __initdata) 6500 if ($line =~ /(\b$InitAttribute\b)/) { 6501 my $attr = $1; 6502 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 6503 my $ptr = $1; 6504 my $var = $2; 6505 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 6506 ERROR("MISPLACED_INIT", 6507 "$attr should be placed after $var\n" . $herecurr)) || 6508 ($ptr !~ /\b(union|struct)\s+$attr\b/ && 6509 WARN("MISPLACED_INIT", 6510 "$attr should be placed after $var\n" . $herecurr))) && 6511 $fix) { 6512 $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; 6513 } 6514 } 6515 } 6516 6517# check for $InitAttributeData (ie: __initdata) with const 6518 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 6519 my $attr = $1; 6520 $attr =~ /($InitAttributePrefix)(.*)/; 6521 my $attr_prefix = $1; 6522 my $attr_type = $2; 6523 if (ERROR("INIT_ATTRIBUTE", 6524 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 6525 $fix) { 6526 $fixed[$fixlinenr] =~ 6527 s/$InitAttributeData/${attr_prefix}initconst/; 6528 } 6529 } 6530 6531# check for $InitAttributeConst (ie: __initconst) without const 6532 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 6533 my $attr = $1; 6534 if (ERROR("INIT_ATTRIBUTE", 6535 "Use of $attr requires a separate use of const\n" . $herecurr) && 6536 $fix) { 6537 my $lead = $fixed[$fixlinenr] =~ 6538 /(^\+\s*(?:static\s+))/; 6539 $lead = rtrim($1); 6540 $lead = "$lead " if ($lead !~ /^\+$/); 6541 $lead = "${lead}const "; 6542 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 6543 } 6544 } 6545 6546# check for __read_mostly with const non-pointer (should just be const) 6547 if ($line =~ /\b__read_mostly\b/ && 6548 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 6549 if (ERROR("CONST_READ_MOSTLY", 6550 "Invalid use of __read_mostly with const type\n" . $herecurr) && 6551 $fix) { 6552 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 6553 } 6554 } 6555 6556# don't use __constant_<foo> functions outside of include/uapi/ 6557 if ($realfile !~ m@^include/uapi/@ && 6558 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 6559 my $constant_func = $1; 6560 my $func = $constant_func; 6561 $func =~ s/^__constant_//; 6562 if (WARN("CONSTANT_CONVERSION", 6563 "$constant_func should be $func\n" . $herecurr) && 6564 $fix) { 6565 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 6566 } 6567 } 6568 6569# prefer usleep_range over udelay 6570 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 6571 my $delay = $1; 6572 # ignore udelay's < 10, however 6573 if (! ($delay < 10) ) { 6574 CHK("USLEEP_RANGE", 6575 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 6576 } 6577 if ($delay > 2000) { 6578 WARN("LONG_UDELAY", 6579 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 6580 } 6581 } 6582 6583# warn about unexpectedly long msleep's 6584 if ($line =~ /\bmsleep\s*\((\d+)\);/) { 6585 if ($1 < 20) { 6586 WARN("MSLEEP", 6587 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 6588 } 6589 } 6590 6591# check for comparisons of jiffies 6592 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 6593 WARN("JIFFIES_COMPARISON", 6594 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 6595 } 6596 6597# check for comparisons of get_jiffies_64() 6598 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 6599 WARN("JIFFIES_COMPARISON", 6600 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 6601 } 6602 6603# warn about #ifdefs in C files 6604# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 6605# print "#ifdef in C files should be avoided\n"; 6606# print "$herecurr"; 6607# $clean = 0; 6608# } 6609 6610# warn about spacing in #ifdefs 6611 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 6612 if (ERROR("SPACING", 6613 "exactly one space required after that #$1\n" . $herecurr) && 6614 $fix) { 6615 $fixed[$fixlinenr] =~ 6616 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 6617 } 6618 6619 } 6620 6621# check for spinlock_t definitions without a comment. 6622 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 6623 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 6624 my $which = $1; 6625 if (!ctx_has_comment($first_line, $linenr)) { 6626 CHK("UNCOMMENTED_DEFINITION", 6627 "$1 definition without comment\n" . $herecurr); 6628 } 6629 } 6630# check for memory barriers without a comment. 6631 6632 my $barriers = qr{ 6633 mb| 6634 rmb| 6635 wmb 6636 }x; 6637 my $barrier_stems = qr{ 6638 mb__before_atomic| 6639 mb__after_atomic| 6640 store_release| 6641 load_acquire| 6642 store_mb| 6643 (?:$barriers) 6644 }x; 6645 my $all_barriers = qr{ 6646 (?:$barriers)| 6647 smp_(?:$barrier_stems)| 6648 virt_(?:$barrier_stems) 6649 }x; 6650 6651 if ($line =~ /\b(?:$all_barriers)\s*\(/) { 6652 if (!ctx_has_comment($first_line, $linenr)) { 6653 WARN("MEMORY_BARRIER", 6654 "memory barrier without comment\n" . $herecurr); 6655 } 6656 } 6657 6658 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 6659 6660 if ($realfile !~ m@^include/asm-generic/@ && 6661 $realfile !~ m@/barrier\.h$@ && 6662 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 6663 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 6664 WARN("MEMORY_BARRIER", 6665 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 6666 } 6667 6668# check for waitqueue_active without a comment. 6669 if ($line =~ /\bwaitqueue_active\s*\(/) { 6670 if (!ctx_has_comment($first_line, $linenr)) { 6671 WARN("WAITQUEUE_ACTIVE", 6672 "waitqueue_active without comment\n" . $herecurr); 6673 } 6674 } 6675 6676# check for data_race without a comment. 6677 if ($line =~ /\bdata_race\s*\(/) { 6678 if (!ctx_has_comment($first_line, $linenr)) { 6679 WARN("DATA_RACE", 6680 "data_race without comment\n" . $herecurr); 6681 } 6682 } 6683 6684# check of hardware specific defines 6685 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 6686 CHK("ARCH_DEFINES", 6687 "architecture specific defines should be avoided\n" . $herecurr); 6688 } 6689 6690# check that the storage class is not after a type 6691 if ($line =~ /\b($Type)\s+($Storage)\b/) { 6692 WARN("STORAGE_CLASS", 6693 "storage class '$2' should be located before type '$1'\n" . $herecurr); 6694 } 6695# Check that the storage class is at the beginning of a declaration 6696 if ($line =~ /\b$Storage\b/ && 6697 $line !~ /^.\s*$Storage/ && 6698 $line =~ /^.\s*(.+?)\$Storage\s/ && 6699 $1 !~ /[\,\)]\s*$/) { 6700 WARN("STORAGE_CLASS", 6701 "storage class should be at the beginning of the declaration\n" . $herecurr); 6702 } 6703 6704# check the location of the inline attribute, that it is between 6705# storage class and type. 6706 if ($line =~ /\b$Type\s+$Inline\b/ || 6707 $line =~ /\b$Inline\s+$Storage\b/) { 6708 ERROR("INLINE_LOCATION", 6709 "inline keyword should sit between storage class and type\n" . $herecurr); 6710 } 6711 6712# Check for __inline__ and __inline, prefer inline 6713 if ($realfile !~ m@\binclude/uapi/@ && 6714 $line =~ /\b(__inline__|__inline)\b/) { 6715 if (WARN("INLINE", 6716 "plain inline is preferred over $1\n" . $herecurr) && 6717 $fix) { 6718 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 6719 6720 } 6721 } 6722 6723# Check for compiler attributes 6724 if ($realfile !~ m@\binclude/uapi/@ && 6725 $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) { 6726 my $attr = $1; 6727 $attr =~ s/\s*\(\s*(.*)\)\s*/$1/; 6728 6729 my %attr_list = ( 6730 "alias" => "__alias", 6731 "aligned" => "__aligned", 6732 "always_inline" => "__always_inline", 6733 "assume_aligned" => "__assume_aligned", 6734 "cold" => "__cold", 6735 "const" => "__attribute_const__", 6736 "copy" => "__copy", 6737 "designated_init" => "__designated_init", 6738 "externally_visible" => "__visible", 6739 "format" => "printf|scanf", 6740 "gnu_inline" => "__gnu_inline", 6741 "malloc" => "__malloc", 6742 "mode" => "__mode", 6743 "no_caller_saved_registers" => "__no_caller_saved_registers", 6744 "noclone" => "__noclone", 6745 "noinline" => "noinline", 6746 "nonstring" => "__nonstring", 6747 "noreturn" => "__noreturn", 6748 "packed" => "__packed", 6749 "pure" => "__pure", 6750 "section" => "__section", 6751 "used" => "__used", 6752 "weak" => "__weak" 6753 ); 6754 6755 while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) { 6756 my $orig_attr = $1; 6757 my $params = ''; 6758 $params = $2 if defined($2); 6759 my $curr_attr = $orig_attr; 6760 $curr_attr =~ s/^[\s_]+|[\s_]+$//g; 6761 if (exists($attr_list{$curr_attr})) { 6762 my $new = $attr_list{$curr_attr}; 6763 if ($curr_attr eq "format" && $params) { 6764 $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/; 6765 $new = "__$1\($2"; 6766 } else { 6767 $new = "$new$params"; 6768 } 6769 if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 6770 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) && 6771 $fix) { 6772 my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?'; 6773 $fixed[$fixlinenr] =~ s/$remove//; 6774 $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/; 6775 $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/; 6776 $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//; 6777 } 6778 } 6779 } 6780 6781 # Check for __attribute__ unused, prefer __always_unused or __maybe_unused 6782 if ($attr =~ /^_*unused/) { 6783 WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 6784 "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr); 6785 } 6786 } 6787 6788# Check for __attribute__ weak, or __weak declarations (may have link issues) 6789 if ($perl_version_ok && 6790 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 6791 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6792 $line =~ /\b__weak\b/)) { 6793 ERROR("WEAK_DECLARATION", 6794 "Using weak declarations can have unintended link defects\n" . $herecurr); 6795 } 6796 6797# check for c99 types like uint8_t used outside of uapi/ and tools/ 6798 if ($realfile !~ m@\binclude/uapi/@ && 6799 $realfile !~ m@\btools/@ && 6800 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6801 my $type = $1; 6802 if ($type =~ /\b($typeC99Typedefs)\b/) { 6803 $type = $1; 6804 my $kernel_type = 'u'; 6805 $kernel_type = 's' if ($type =~ /^_*[si]/); 6806 $type =~ /(\d+)/; 6807 $kernel_type .= $1; 6808 if (CHK("PREFER_KERNEL_TYPES", 6809 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6810 $fix) { 6811 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6812 } 6813 } 6814 } 6815 6816# check for cast of C90 native int or longer types constants 6817 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6818 my $cast = $1; 6819 my $const = $2; 6820 my $suffix = ""; 6821 my $newconst = $const; 6822 $newconst =~ s/${Int_type}$//; 6823 $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6824 if ($cast =~ /\blong\s+long\b/) { 6825 $suffix .= 'LL'; 6826 } elsif ($cast =~ /\blong\b/) { 6827 $suffix .= 'L'; 6828 } 6829 if (WARN("TYPECAST_INT_CONSTANT", 6830 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) && 6831 $fix) { 6832 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6833 } 6834 } 6835 6836# check for sizeof(&) 6837 if ($line =~ /\bsizeof\s*\(\s*\&/) { 6838 WARN("SIZEOF_ADDRESS", 6839 "sizeof(& should be avoided\n" . $herecurr); 6840 } 6841 6842# check for sizeof without parenthesis 6843 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6844 if (WARN("SIZEOF_PARENTHESIS", 6845 "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6846 $fix) { 6847 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6848 } 6849 } 6850 6851# check for struct spinlock declarations 6852 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 6853 WARN("USE_SPINLOCK_T", 6854 "struct spinlock should be spinlock_t\n" . $herecurr); 6855 } 6856 6857# check for seq_printf uses that could be seq_puts 6858 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6859 my $fmt = get_quoted_string($line, $rawline); 6860 $fmt =~ s/%%//g; 6861 if ($fmt !~ /%/) { 6862 if (WARN("PREFER_SEQ_PUTS", 6863 "Prefer seq_puts to seq_printf\n" . $herecurr) && 6864 $fix) { 6865 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6866 } 6867 } 6868 } 6869 6870# check for vsprintf extension %p<foo> misuses 6871 if ($perl_version_ok && 6872 defined $stat && 6873 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 6874 $1 !~ /^_*volatile_*$/) { 6875 my $stat_real; 6876 6877 my $lc = $stat =~ tr@\n@@; 6878 $lc = $lc + $linenr; 6879 for (my $count = $linenr; $count <= $lc; $count++) { 6880 my $specifier; 6881 my $extension; 6882 my $qualifier; 6883 my $bad_specifier = ""; 6884 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 6885 $fmt =~ s/%%//g; 6886 6887 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6888 $specifier = $1; 6889 $extension = $2; 6890 $qualifier = $3; 6891 if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 6892 ($extension eq "f" && 6893 defined $qualifier && $qualifier !~ /^w/) || 6894 ($extension eq "4" && 6895 defined $qualifier && $qualifier !~ /^cc/)) { 6896 $bad_specifier = $specifier; 6897 last; 6898 } 6899 if ($extension eq "x" && !defined($stat_real)) { 6900 if (!defined($stat_real)) { 6901 $stat_real = get_stat_real($linenr, $lc); 6902 } 6903 WARN("VSPRINTF_SPECIFIER_PX", 6904 "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n"); 6905 } 6906 } 6907 if ($bad_specifier ne "") { 6908 my $stat_real = get_stat_real($linenr, $lc); 6909 my $msg_level = \&WARN; 6910 my $ext_type = "Invalid"; 6911 my $use = ""; 6912 if ($bad_specifier =~ /p[Ff]/) { 6913 $use = " - use %pS instead"; 6914 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 6915 } elsif ($bad_specifier =~ /pA/) { 6916 $use = " - '%pA' is only intended to be used from Rust code"; 6917 $msg_level = \&ERROR; 6918 } 6919 6920 &{$msg_level}("VSPRINTF_POINTER_EXTENSION", 6921 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6922 } 6923 } 6924 } 6925 6926# Check for misused memsets 6927 if ($perl_version_ok && 6928 defined $stat && 6929 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6930 6931 my $ms_addr = $2; 6932 my $ms_val = $7; 6933 my $ms_size = $12; 6934 6935 if ($ms_size =~ /^(0x|)0$/i) { 6936 ERROR("MEMSET", 6937 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6938 } elsif ($ms_size =~ /^(0x|)1$/i) { 6939 WARN("MEMSET", 6940 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6941 } 6942 } 6943 6944# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 6945# if ($perl_version_ok && 6946# defined $stat && 6947# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6948# if (WARN("PREFER_ETHER_ADDR_COPY", 6949# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6950# $fix) { 6951# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6952# } 6953# } 6954 6955# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 6956# if ($perl_version_ok && 6957# defined $stat && 6958# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6959# WARN("PREFER_ETHER_ADDR_EQUAL", 6960# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6961# } 6962 6963# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 6964# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 6965# if ($perl_version_ok && 6966# defined $stat && 6967# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6968# 6969# my $ms_val = $7; 6970# 6971# if ($ms_val =~ /^(?:0x|)0+$/i) { 6972# if (WARN("PREFER_ETH_ZERO_ADDR", 6973# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6974# $fix) { 6975# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6976# } 6977# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6978# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6979# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6980# $fix) { 6981# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6982# } 6983# } 6984# } 6985 6986# strlcpy uses that should likely be strscpy 6987 if ($line =~ /\bstrlcpy\s*\(/) { 6988 WARN("STRLCPY", 6989 "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr); 6990 } 6991 6992# typecasts on min/max could be min_t/max_t 6993 if ($perl_version_ok && 6994 defined $stat && 6995 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6996 if (defined $2 || defined $7) { 6997 my $call = $1; 6998 my $cast1 = deparenthesize($2); 6999 my $arg1 = $3; 7000 my $cast2 = deparenthesize($7); 7001 my $arg2 = $8; 7002 my $cast; 7003 7004 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 7005 $cast = "$cast1 or $cast2"; 7006 } elsif ($cast1 ne "") { 7007 $cast = $cast1; 7008 } else { 7009 $cast = $cast2; 7010 } 7011 WARN("MINMAX", 7012 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 7013 } 7014 } 7015 7016# check usleep_range arguments 7017 if ($perl_version_ok && 7018 defined $stat && 7019 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 7020 my $min = $1; 7021 my $max = $7; 7022 if ($min eq $max) { 7023 WARN("USLEEP_RANGE", 7024 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 7025 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 7026 $min > $max) { 7027 WARN("USLEEP_RANGE", 7028 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 7029 } 7030 } 7031 7032# check for naked sscanf 7033 if ($perl_version_ok && 7034 defined $stat && 7035 $line =~ /\bsscanf\b/ && 7036 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 7037 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 7038 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 7039 my $lc = $stat =~ tr@\n@@; 7040 $lc = $lc + $linenr; 7041 my $stat_real = get_stat_real($linenr, $lc); 7042 WARN("NAKED_SSCANF", 7043 "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 7044 } 7045 7046# check for simple sscanf that should be kstrto<foo> 7047 if ($perl_version_ok && 7048 defined $stat && 7049 $line =~ /\bsscanf\b/) { 7050 my $lc = $stat =~ tr@\n@@; 7051 $lc = $lc + $linenr; 7052 my $stat_real = get_stat_real($linenr, $lc); 7053 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 7054 my $format = $6; 7055 my $count = $format =~ tr@%@%@; 7056 if ($count == 1 && 7057 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 7058 WARN("SSCANF_TO_KSTRTO", 7059 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 7060 } 7061 } 7062 } 7063 7064# check for new externs in .h files. 7065 if ($realfile =~ /\.h$/ && 7066 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 7067 if (CHK("AVOID_EXTERNS", 7068 "extern prototypes should be avoided in .h files\n" . $herecurr) && 7069 $fix) { 7070 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 7071 } 7072 } 7073 7074# check for new externs in .c files. 7075 if ($realfile =~ /\.c$/ && defined $stat && 7076 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 7077 { 7078 my $function_name = $1; 7079 my $paren_space = $2; 7080 7081 my $s = $stat; 7082 if (defined $cond) { 7083 substr($s, 0, length($cond), ''); 7084 } 7085 if ($s =~ /^\s*;/) 7086 { 7087 WARN("AVOID_EXTERNS", 7088 "externs should be avoided in .c files\n" . $herecurr); 7089 } 7090 7091 if ($paren_space =~ /\n/) { 7092 WARN("FUNCTION_ARGUMENTS", 7093 "arguments for function declarations should follow identifier\n" . $herecurr); 7094 } 7095 7096 } elsif ($realfile =~ /\.c$/ && defined $stat && 7097 $stat =~ /^.\s*extern\s+/) 7098 { 7099 WARN("AVOID_EXTERNS", 7100 "externs should be avoided in .c files\n" . $herecurr); 7101 } 7102 7103# check for function declarations that have arguments without identifier names 7104 if (defined $stat && 7105 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 7106 $1 ne "void") { 7107 my $args = trim($1); 7108 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 7109 my $arg = trim($1); 7110 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 7111 WARN("FUNCTION_ARGUMENTS", 7112 "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 7113 } 7114 } 7115 } 7116 7117# check for function definitions 7118 if ($perl_version_ok && 7119 defined $stat && 7120 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 7121 $context_function = $1; 7122 7123# check for multiline function definition with misplaced open brace 7124 my $ok = 0; 7125 my $cnt = statement_rawlines($stat); 7126 my $herectx = $here . "\n"; 7127 for (my $n = 0; $n < $cnt; $n++) { 7128 my $rl = raw_line($linenr, $n); 7129 $herectx .= $rl . "\n"; 7130 $ok = 1 if ($rl =~ /^[ \+]\{/); 7131 $ok = 1 if ($rl =~ /\{/ && $n == 0); 7132 last if $rl =~ /^[ \+].*\{/; 7133 } 7134 if (!$ok) { 7135 ERROR("OPEN_BRACE", 7136 "open brace '{' following function definitions go on the next line\n" . $herectx); 7137 } 7138 } 7139 7140# checks for new __setup's 7141 if ($rawline =~ /\b__setup\("([^"]*)"/) { 7142 my $name = $1; 7143 7144 if (!grep(/$name/, @setup_docs)) { 7145 CHK("UNDOCUMENTED_SETUP", 7146 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); 7147 } 7148 } 7149 7150# check for pointless casting of alloc functions 7151 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 7152 WARN("UNNECESSARY_CASTS", 7153 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 7154 } 7155 7156# alloc style 7157# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 7158 if ($perl_version_ok && 7159 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 7160 CHK("ALLOC_SIZEOF_STRUCT", 7161 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 7162 } 7163 7164# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc 7165 if ($perl_version_ok && 7166 defined $stat && 7167 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 7168 my $oldfunc = $3; 7169 my $a1 = $4; 7170 my $a2 = $10; 7171 my $newfunc = "kmalloc_array"; 7172 $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc"); 7173 $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc"); 7174 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 7175 my $r1 = $a1; 7176 my $r2 = $a2; 7177 if ($a1 =~ /^sizeof\s*\S/) { 7178 $r1 = $a2; 7179 $r2 = $a1; 7180 } 7181 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 7182 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 7183 my $cnt = statement_rawlines($stat); 7184 my $herectx = get_stat_here($linenr, $cnt, $here); 7185 7186 if (WARN("ALLOC_WITH_MULTIPLY", 7187 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 7188 $cnt == 1 && 7189 $fix) { 7190 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; 7191 } 7192 } 7193 } 7194 7195# check for krealloc arg reuse 7196 if ($perl_version_ok && 7197 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 7198 $1 eq $3) { 7199 WARN("KREALLOC_ARG_REUSE", 7200 "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 7201 } 7202 7203# check for alloc argument mismatch 7204 if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) { 7205 WARN("ALLOC_ARRAY_ARGS", 7206 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 7207 } 7208 7209# check for multiple semicolons 7210 if ($line =~ /;\s*;\s*$/) { 7211 if (WARN("ONE_SEMICOLON", 7212 "Statements terminations use 1 semicolon\n" . $herecurr) && 7213 $fix) { 7214 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 7215 } 7216 } 7217 7218# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 7219 if ($realfile !~ m@^include/uapi/@ && 7220 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 7221 my $ull = ""; 7222 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 7223 if (CHK("BIT_MACRO", 7224 "Prefer using the BIT$ull macro\n" . $herecurr) && 7225 $fix) { 7226 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 7227 } 7228 } 7229 7230# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) 7231 if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { 7232 WARN("IS_ENABLED_CONFIG", 7233 "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); 7234 } 7235 7236# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 7237 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { 7238 my $config = $1; 7239 if (WARN("PREFER_IS_ENABLED", 7240 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && 7241 $fix) { 7242 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 7243 } 7244 } 7245 7246# check for /* fallthrough */ like comment, prefer fallthrough; 7247 my @fallthroughs = ( 7248 'fallthrough', 7249 '@fallthrough@', 7250 'lint -fallthrough[ \t]*', 7251 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 7252 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 7253 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 7254 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 7255 ); 7256 if ($raw_comment ne '') { 7257 foreach my $ft (@fallthroughs) { 7258 if ($raw_comment =~ /$ft/) { 7259 my $msg_level = \&WARN; 7260 $msg_level = \&CHK if ($file); 7261 &{$msg_level}("PREFER_FALLTHROUGH", 7262 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 7263 last; 7264 } 7265 } 7266 } 7267 7268# check for switch/default statements without a break; 7269 if ($perl_version_ok && 7270 defined $stat && 7271 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 7272 my $cnt = statement_rawlines($stat); 7273 my $herectx = get_stat_here($linenr, $cnt, $here); 7274 7275 WARN("DEFAULT_NO_BREAK", 7276 "switch default: should use break\n" . $herectx); 7277 } 7278 7279# check for gcc specific __FUNCTION__ 7280 if ($line =~ /\b__FUNCTION__\b/) { 7281 if (WARN("USE_FUNC", 7282 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 7283 $fix) { 7284 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 7285 } 7286 } 7287 7288# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 7289 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 7290 ERROR("DATE_TIME", 7291 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 7292 } 7293 7294# check for use of yield() 7295 if ($line =~ /\byield\s*\(\s*\)/) { 7296 WARN("YIELD", 7297 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 7298 } 7299 7300# check for comparisons against true and false 7301 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 7302 my $lead = $1; 7303 my $arg = $2; 7304 my $test = $3; 7305 my $otype = $4; 7306 my $trail = $5; 7307 my $op = "!"; 7308 7309 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 7310 7311 my $type = lc($otype); 7312 if ($type =~ /^(?:true|false)$/) { 7313 if (("$test" eq "==" && "$type" eq "true") || 7314 ("$test" eq "!=" && "$type" eq "false")) { 7315 $op = ""; 7316 } 7317 7318 CHK("BOOL_COMPARISON", 7319 "Using comparison to $otype is error prone\n" . $herecurr); 7320 7321## maybe suggesting a correct construct would better 7322## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 7323 7324 } 7325 } 7326 7327# check for semaphores initialized locked 7328 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 7329 WARN("CONSIDER_COMPLETION", 7330 "consider using a completion\n" . $herecurr); 7331 } 7332 7333# recommend kstrto* over simple_strto* and strict_strto* 7334 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 7335 WARN("CONSIDER_KSTRTO", 7336 "$1 is obsolete, use k$3 instead\n" . $herecurr); 7337 } 7338 7339# check for __initcall(), use device_initcall() explicitly or more appropriate function please 7340 if ($line =~ /^.\s*__initcall\s*\(/) { 7341 WARN("USE_DEVICE_INITCALL", 7342 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 7343 } 7344 7345# check for spin_is_locked(), suggest lockdep instead 7346 if ($line =~ /\bspin_is_locked\(/) { 7347 WARN("USE_LOCKDEP", 7348 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 7349 } 7350 7351# check for deprecated apis 7352 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 7353 my $deprecated_api = $1; 7354 my $new_api = $deprecated_apis{$deprecated_api}; 7355 WARN("DEPRECATED_API", 7356 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 7357 } 7358 7359# check for various structs that are normally const (ops, kgdb, device_tree) 7360# and avoid what seem like struct definitions 'struct foo {' 7361 if (defined($const_structs) && 7362 $line !~ /\bconst\b/ && 7363 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 7364 WARN("CONST_STRUCT", 7365 "struct $1 should normally be const\n" . $herecurr); 7366 } 7367 7368# use of NR_CPUS is usually wrong 7369# ignore definitions of NR_CPUS and usage to define arrays as likely right 7370# ignore designated initializers using NR_CPUS 7371 if ($line =~ /\bNR_CPUS\b/ && 7372 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 7373 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 7374 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 7375 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 7376 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ && 7377 $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/) 7378 { 7379 WARN("NR_CPUS", 7380 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 7381 } 7382 7383# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 7384 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 7385 ERROR("DEFINE_ARCH_HAS", 7386 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 7387 } 7388 7389# likely/unlikely comparisons similar to "(likely(foo) > 0)" 7390 if ($perl_version_ok && 7391 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 7392 WARN("LIKELY_MISUSE", 7393 "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 7394 } 7395 7396# return sysfs_emit(foo, fmt, ...) fmt without newline 7397 if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ && 7398 substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) { 7399 my $offset = $+[6] - 1; 7400 if (WARN("SYSFS_EMIT", 7401 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) && 7402 $fix) { 7403 substr($fixed[$fixlinenr], $offset, 0) = '\\n'; 7404 } 7405 } 7406 7407# nested likely/unlikely calls 7408 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 7409 WARN("LIKELY_MISUSE", 7410 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 7411 } 7412 7413# whine mightly about in_atomic 7414 if ($line =~ /\bin_atomic\s*\(/) { 7415 if ($realfile =~ m@^drivers/@) { 7416 ERROR("IN_ATOMIC", 7417 "do not use in_atomic in drivers\n" . $herecurr); 7418 } elsif ($realfile !~ m@^kernel/@) { 7419 WARN("IN_ATOMIC", 7420 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 7421 } 7422 } 7423 7424# check for lockdep_set_novalidate_class 7425 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 7426 $line =~ /__lockdep_no_validate__\s*\)/ ) { 7427 if ($realfile !~ m@^kernel/lockdep@ && 7428 $realfile !~ m@^include/linux/lockdep@ && 7429 $realfile !~ m@^drivers/base/core@) { 7430 ERROR("LOCKDEP", 7431 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 7432 } 7433 } 7434 7435 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 7436 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 7437 WARN("EXPORTED_WORLD_WRITABLE", 7438 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 7439 } 7440 7441# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 7442# and whether or not function naming is typical and if 7443# DEVICE_ATTR permissions uses are unusual too 7444 if ($perl_version_ok && 7445 defined $stat && 7446 $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) { 7447 my $var = $1; 7448 my $perms = $2; 7449 my $show = $3; 7450 my $store = $4; 7451 my $octal_perms = perms_to_octal($perms); 7452 if ($show =~ /^${var}_show$/ && 7453 $store =~ /^${var}_store$/ && 7454 $octal_perms eq "0644") { 7455 if (WARN("DEVICE_ATTR_RW", 7456 "Use DEVICE_ATTR_RW\n" . $herecurr) && 7457 $fix) { 7458 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/; 7459 } 7460 } elsif ($show =~ /^${var}_show$/ && 7461 $store =~ /^NULL$/ && 7462 $octal_perms eq "0444") { 7463 if (WARN("DEVICE_ATTR_RO", 7464 "Use DEVICE_ATTR_RO\n" . $herecurr) && 7465 $fix) { 7466 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/; 7467 } 7468 } elsif ($show =~ /^NULL$/ && 7469 $store =~ /^${var}_store$/ && 7470 $octal_perms eq "0200") { 7471 if (WARN("DEVICE_ATTR_WO", 7472 "Use DEVICE_ATTR_WO\n" . $herecurr) && 7473 $fix) { 7474 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/; 7475 } 7476 } elsif ($octal_perms eq "0644" || 7477 $octal_perms eq "0444" || 7478 $octal_perms eq "0200") { 7479 my $newshow = "$show"; 7480 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 7481 my $newstore = $store; 7482 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 7483 my $rename = ""; 7484 if ($show ne $newshow) { 7485 $rename .= " '$show' to '$newshow'"; 7486 } 7487 if ($store ne $newstore) { 7488 $rename .= " '$store' to '$newstore'"; 7489 } 7490 WARN("DEVICE_ATTR_FUNCTIONS", 7491 "Consider renaming function(s)$rename\n" . $herecurr); 7492 } else { 7493 WARN("DEVICE_ATTR_PERMS", 7494 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 7495 } 7496 } 7497 7498# Mode permission misuses where it seems decimal should be octal 7499# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 7500# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 7501# specific definition of not visible in sysfs. 7502# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 7503# use the default permissions 7504 if ($perl_version_ok && 7505 defined $stat && 7506 $line =~ /$mode_perms_search/) { 7507 foreach my $entry (@mode_permission_funcs) { 7508 my $func = $entry->[0]; 7509 my $arg_pos = $entry->[1]; 7510 7511 my $lc = $stat =~ tr@\n@@; 7512 $lc = $lc + $linenr; 7513 my $stat_real = get_stat_real($linenr, $lc); 7514 7515 my $skip_args = ""; 7516 if ($arg_pos > 1) { 7517 $arg_pos--; 7518 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 7519 } 7520 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 7521 if ($stat =~ /$test/) { 7522 my $val = $1; 7523 $val = $6 if ($skip_args ne ""); 7524 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 7525 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 7526 ($val =~ /^$Octal$/ && length($val) ne 4))) { 7527 ERROR("NON_OCTAL_PERMISSIONS", 7528 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 7529 } 7530 if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 7531 ERROR("EXPORTED_WORLD_WRITABLE", 7532 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 7533 } 7534 } 7535 } 7536 } 7537 7538# check for uses of S_<PERMS> that could be octal for readability 7539 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 7540 my $oval = $1; 7541 my $octal = perms_to_octal($oval); 7542 if (WARN("SYMBOLIC_PERMS", 7543 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 7544 $fix) { 7545 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 7546 } 7547 } 7548 7549# validate content of MODULE_LICENSE against list from include/linux/module.h 7550 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 7551 my $extracted_string = get_quoted_string($line, $rawline); 7552 my $valid_licenses = qr{ 7553 GPL| 7554 GPL\ v2| 7555 GPL\ and\ additional\ rights| 7556 Dual\ BSD/GPL| 7557 Dual\ MIT/GPL| 7558 Dual\ MPL/GPL| 7559 Proprietary 7560 }x; 7561 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 7562 WARN("MODULE_LICENSE", 7563 "unknown module license " . $extracted_string . "\n" . $herecurr); 7564 } 7565 if (!$file && $extracted_string eq '"GPL v2"') { 7566 if (WARN("MODULE_LICENSE", 7567 "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) && 7568 $fix) { 7569 $fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/; 7570 } 7571 } 7572 } 7573 7574# check for sysctl duplicate constants 7575 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 7576 WARN("DUPLICATED_SYSCTL_CONST", 7577 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 7578 } 7579 } 7580 7581 # If we have no input at all, then there is nothing to report on 7582 # so just keep quiet. 7583 if ($#rawlines == -1) { 7584 exit(0); 7585 } 7586 7587 # In mailback mode only produce a report in the negative, for 7588 # things that appear to be patches. 7589 if ($mailback && ($clean == 1 || !$is_patch)) { 7590 exit(0); 7591 } 7592 7593 # This is not a patch, and we are in 'no-patch' mode so 7594 # just keep quiet. 7595 if (!$chk_patch && !$is_patch) { 7596 exit(0); 7597 } 7598 7599 if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 7600 ERROR("NOT_UNIFIED_DIFF", 7601 "Does not appear to be a unified-diff format patch\n"); 7602 } 7603 if ($is_patch && $has_commit_log && $chk_signoff) { 7604 if ($signoff == 0) { 7605 ERROR("MISSING_SIGN_OFF", 7606 "Missing Signed-off-by: line(s)\n"); 7607 } elsif ($authorsignoff != 1) { 7608 # authorsignoff values: 7609 # 0 -> missing sign off 7610 # 1 -> sign off identical 7611 # 2 -> names and addresses match, comments mismatch 7612 # 3 -> addresses match, names different 7613 # 4 -> names match, addresses different 7614 # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match 7615 7616 my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; 7617 7618 if ($authorsignoff == 0) { 7619 ERROR("NO_AUTHOR_SIGN_OFF", 7620 "Missing Signed-off-by: line by nominal patch author '$author'\n"); 7621 } elsif ($authorsignoff == 2) { 7622 CHK("FROM_SIGN_OFF_MISMATCH", 7623 "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); 7624 } elsif ($authorsignoff == 3) { 7625 WARN("FROM_SIGN_OFF_MISMATCH", 7626 "From:/Signed-off-by: email name mismatch: $sob_msg\n"); 7627 } elsif ($authorsignoff == 4) { 7628 WARN("FROM_SIGN_OFF_MISMATCH", 7629 "From:/Signed-off-by: email address mismatch: $sob_msg\n"); 7630 } elsif ($authorsignoff == 5) { 7631 WARN("FROM_SIGN_OFF_MISMATCH", 7632 "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); 7633 } 7634 } 7635 } 7636 7637 print report_dump(); 7638 if ($summary && !($clean == 1 && $quiet == 1)) { 7639 print "$filename " if ($summary_file); 7640 print "total: $cnt_error errors, $cnt_warn warnings, " . 7641 (($check)? "$cnt_chk checks, " : "") . 7642 "$cnt_lines lines checked\n"; 7643 } 7644 7645 if ($quiet == 0) { 7646 # If there were any defects found and not already fixing them 7647 if (!$clean and !$fix) { 7648 print << "EOM" 7649 7650NOTE: For some of the reported defects, checkpatch may be able to 7651 mechanically convert to the typical style using --fix or --fix-inplace. 7652EOM 7653 } 7654 # If there were whitespace errors which cleanpatch can fix 7655 # then suggest that. 7656 if ($rpt_cleaners) { 7657 $rpt_cleaners = 0; 7658 print << "EOM" 7659 7660NOTE: Whitespace errors detected. 7661 You may wish to use scripts/cleanpatch or scripts/cleanfile 7662EOM 7663 } 7664 } 7665 7666 if ($clean == 0 && $fix && 7667 ("@rawlines" ne "@fixed" || 7668 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 7669 my $newfile = $filename; 7670 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 7671 my $linecount = 0; 7672 my $f; 7673 7674 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 7675 7676 open($f, '>', $newfile) 7677 or die "$P: Can't open $newfile for write\n"; 7678 foreach my $fixed_line (@fixed) { 7679 $linecount++; 7680 if ($file) { 7681 if ($linecount > 3) { 7682 $fixed_line =~ s/^\+//; 7683 print $f $fixed_line . "\n"; 7684 } 7685 } else { 7686 print $f $fixed_line . "\n"; 7687 } 7688 } 7689 close($f); 7690 7691 if (!$quiet) { 7692 print << "EOM"; 7693 7694Wrote EXPERIMENTAL --fix correction(s) to '$newfile' 7695 7696Do _NOT_ trust the results written to this file. 7697Do _NOT_ submit these changes without inspecting them for correctness. 7698 7699This EXPERIMENTAL file is simply a convenience to help rewrite patches. 7700No warranties, expressed or implied... 7701EOM 7702 } 7703 } 7704 7705 if ($quiet == 0) { 7706 print "\n"; 7707 if ($clean == 1) { 7708 print "$vname has no obvious style problems and is ready for submission.\n"; 7709 } else { 7710 print "$vname has style problems, please review.\n"; 7711 } 7712 } 7713 return $clean; 7714} 7715