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