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