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