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