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