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