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