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