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