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