1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl 2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0 3882ea1d6SJoe Perches# 4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit) 500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit) 62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) 7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]> 8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]> 90a920b5bSAndy Whitcroft 100a920b5bSAndy Whitcroftuse strict; 11cb77f0d6SKamil Rytarowskiuse warnings; 12c707a81dSJoe Perchesuse POSIX; 1336061e38SJoe Perchesuse File::Basename; 1436061e38SJoe Perchesuse Cwd 'abs_path'; 1557230297SJoe Perchesuse Term::ANSIColor qw(:constants); 16cd261496SGeert Uytterhoevenuse Encode qw(decode encode); 170a920b5bSAndy Whitcroft 180a920b5bSAndy Whitcroftmy $P = $0; 1936061e38SJoe Perchesmy $D = dirname(abs_path($P)); 200a920b5bSAndy Whitcroft 21000d1cc1SJoe Perchesmy $V = '0.32'; 220a920b5bSAndy Whitcroft 230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 240a920b5bSAndy Whitcroft 250a920b5bSAndy Whitcroftmy $quiet = 0; 2652178ce0SDwaipayan Raymy $verbose = 0; 2752178ce0SDwaipayan Raymy %verbose_messages = (); 2852178ce0SDwaipayan Raymy %verbose_emitted = (); 290a920b5bSAndy Whitcroftmy $tree = 1; 300a920b5bSAndy Whitcroftmy $chk_signoff = 1; 310a920b5bSAndy Whitcroftmy $chk_patch = 1; 32773647a0SAndy Whitcroftmy $tst_only; 336c72ffaaSAndy Whitcroftmy $emacs = 0; 348905a67cSAndy Whitcroftmy $terse = 0; 3534d8815fSJoe Perchesmy $showfile = 0; 366c72ffaaSAndy Whitcroftmy $file = 0; 374a593c34SDu, Changbinmy $git = 0; 380dea9f1eSJoe Perchesmy %git_commits = (); 396c72ffaaSAndy Whitcroftmy $check = 0; 402ac73b4fSJoe Perchesmy $check_orig = 0; 418905a67cSAndy Whitcroftmy $summary = 1; 428905a67cSAndy Whitcroftmy $mailback = 0; 4313214adfSAndy Whitcroftmy $summary_file = 0; 44000d1cc1SJoe Perchesmy $show_types = 0; 453beb42ecSJoe Perchesmy $list_types = 0; 463705ce5bSJoe Perchesmy $fix = 0; 479624b8d6SJoe Perchesmy $fix_inplace = 0; 486c72ffaaSAndy Whitcroftmy $root; 490f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'}; 500f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot); 51c2fdda0dSAndy Whitcroftmy %debug; 523445686aSJoe Perchesmy %camelcase = (); 5391bfe484SJoe Perchesmy %use_type = (); 5491bfe484SJoe Perchesmy @use = (); 5591bfe484SJoe Perchesmy %ignore_type = (); 56000d1cc1SJoe Perchesmy @ignore = (); 5777f5b10aSHannes Edermy $help = 0; 58000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 59bdc48fa1SJoe Perchesmy $max_line_length = 100; 60d62a201fSDave Hansenmy $ignore_perl_version = 0; 61d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 6256193274SVadim Bendeburymy $min_conf_desc_length = 4; 6366b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 64ebfd7d62SJoe Perchesmy $codespell = 0; 65f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 660ee3e7b8SPeter Ujfalusimy $user_codespellfile = ""; 67bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch"; 6852178ce0SDwaipayan Raymy $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst"; 69ced69da1SQuentin Monnetmy $typedefsfile; 70737c0767SJohn Brooksmy $color = "auto"; 7198005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE 72dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE 73dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git'; 74713a09deSAntonio Borneomy $tabsize = 8; 753e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_"; 7677f5b10aSHannes Eder 7777f5b10aSHannes Edersub help { 7877f5b10aSHannes Eder my ($exitcode) = @_; 7977f5b10aSHannes Eder 8077f5b10aSHannes Eder print << "EOM"; 8177f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 8277f5b10aSHannes EderVersion: $V 8377f5b10aSHannes Eder 8477f5b10aSHannes EderOptions: 8577f5b10aSHannes Eder -q, --quiet quiet 8652178ce0SDwaipayan Ray -v, --verbose verbose mode 8777f5b10aSHannes Eder --no-tree run without a kernel tree 8877f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 8977f5b10aSHannes Eder --patch treat FILE as patchfile (default) 9077f5b10aSHannes Eder --emacs emacs compile window format 9177f5b10aSHannes Eder --terse one line per report 9234d8815fSJoe Perches --showfile emit diffed file position, not input file position 934a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 944a593c34SDu, Changbin single git commit with: 954a593c34SDu, Changbin <rev> 964a593c34SDu, Changbin <rev>^ 974a593c34SDu, Changbin <rev>~n 984a593c34SDu, Changbin multiple git commits with: 994a593c34SDu, Changbin <rev1>..<rev2> 1004a593c34SDu, Changbin <rev1>...<rev2> 1014a593c34SDu, Changbin <rev>-<count> 1024a593c34SDu, Changbin git merges are ignored 10377f5b10aSHannes Eder -f, --file treat FILE as regular source file 10477f5b10aSHannes Eder --subjective, --strict enable more subjective tests 1053beb42ecSJoe Perches --list-types list the possible message types 10691bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 107000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 1083beb42ecSJoe Perches --show-types show the specific message type in the output 109bdc48fa1SJoe Perches --max-line-length=n set the maximum line length, (default $max_line_length) 110bdc48fa1SJoe Perches if exceeded, warn on patches 111bdc48fa1SJoe Perches requires --strict for use with --file 11256193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 113bdc48fa1SJoe Perches --tab-size=n set the number of spaces for tab (default $tabsize) 11477f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 11577f5b10aSHannes Eder --no-summary suppress the per-file summary 11677f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 11777f5b10aSHannes Eder --summary-file include the filename in summary 11877f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 11977f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 12077f5b10aSHannes Eder is all off) 12177f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 12277f5b10aSHannes Eder literally 1233705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1243705ce5bSJoe Perches If correctable single-line errors exist, create 1253705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1263705ce5bSJoe Perches with potential errors corrected to the preferred 1273705ce5bSJoe Perches checkpatch style 1289624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1299624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1309624b8d6SJoe Perches file. It's your fault if there's no backup or git 131d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 132d62a201fSDave Hansen runtime errors. 133ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 1340ee3e7b8SPeter Ujfalusi (default:$codespellfile) 135ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 13675ad8c57SJerome Forissier --typedefsfile Read additional types from this file 137737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 138737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 1393e89ad85SJerome Forissier --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default 1403e89ad85SJerome Forissier ${CONFIG_}) 14177f5b10aSHannes Eder -h, --help, --version display this help and exit 14277f5b10aSHannes Eder 14377f5b10aSHannes EderWhen FILE is - read standard input. 14477f5b10aSHannes EderEOM 14577f5b10aSHannes Eder 14677f5b10aSHannes Eder exit($exitcode); 14777f5b10aSHannes Eder} 14877f5b10aSHannes Eder 1493beb42ecSJoe Perchessub uniq { 1503beb42ecSJoe Perches my %seen; 1513beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1523beb42ecSJoe Perches} 1533beb42ecSJoe Perches 1543beb42ecSJoe Perchessub list_types { 1553beb42ecSJoe Perches my ($exitcode) = @_; 1563beb42ecSJoe Perches 1573beb42ecSJoe Perches my $count = 0; 1583beb42ecSJoe Perches 1593beb42ecSJoe Perches local $/ = undef; 1603beb42ecSJoe Perches 1613beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1623beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1633beb42ecSJoe Perches 1643beb42ecSJoe Perches my $text = <$script>; 1653beb42ecSJoe Perches close($script); 1663beb42ecSJoe Perches 16752178ce0SDwaipayan Ray my %types = (); 1680547fa58SJean Delvare # Also catch when type or level is passed through a variable 16952178ce0SDwaipayan Ray while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 17052178ce0SDwaipayan Ray if (defined($1)) { 17152178ce0SDwaipayan Ray if (exists($types{$2})) { 17252178ce0SDwaipayan Ray $types{$2} .= ",$1" if ($types{$2} ne $1); 17352178ce0SDwaipayan Ray } else { 17452178ce0SDwaipayan Ray $types{$2} = $1; 1753beb42ecSJoe Perches } 17652178ce0SDwaipayan Ray } else { 17752178ce0SDwaipayan Ray $types{$2} = "UNDETERMINED"; 17852178ce0SDwaipayan Ray } 17952178ce0SDwaipayan Ray } 18052178ce0SDwaipayan Ray 1813beb42ecSJoe Perches print("#\tMessage type\n\n"); 18252178ce0SDwaipayan Ray if ($color) { 18352178ce0SDwaipayan Ray print(" ( Color coding: "); 18452178ce0SDwaipayan Ray print(RED . "ERROR" . RESET); 18552178ce0SDwaipayan Ray print(" | "); 18652178ce0SDwaipayan Ray print(YELLOW . "WARNING" . RESET); 18752178ce0SDwaipayan Ray print(" | "); 18852178ce0SDwaipayan Ray print(GREEN . "CHECK" . RESET); 18952178ce0SDwaipayan Ray print(" | "); 19052178ce0SDwaipayan Ray print("Multiple levels / Undetermined"); 19152178ce0SDwaipayan Ray print(" )\n\n"); 19252178ce0SDwaipayan Ray } 19352178ce0SDwaipayan Ray 19452178ce0SDwaipayan Ray foreach my $type (sort keys %types) { 19552178ce0SDwaipayan Ray my $orig_type = $type; 19652178ce0SDwaipayan Ray if ($color) { 19752178ce0SDwaipayan Ray my $level = $types{$type}; 19852178ce0SDwaipayan Ray if ($level eq "ERROR") { 19952178ce0SDwaipayan Ray $type = RED . $type . RESET; 20052178ce0SDwaipayan Ray } elsif ($level eq "WARN") { 20152178ce0SDwaipayan Ray $type = YELLOW . $type . RESET; 20252178ce0SDwaipayan Ray } elsif ($level eq "CHK") { 20352178ce0SDwaipayan Ray $type = GREEN . $type . RESET; 20452178ce0SDwaipayan Ray } 20552178ce0SDwaipayan Ray } 2063beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 20752178ce0SDwaipayan Ray if ($verbose && exists($verbose_messages{$orig_type})) { 20852178ce0SDwaipayan Ray my $message = $verbose_messages{$orig_type}; 20952178ce0SDwaipayan Ray $message =~ s/\n/\n\t/g; 21052178ce0SDwaipayan Ray print("\t" . $message . "\n\n"); 21152178ce0SDwaipayan Ray } 2123beb42ecSJoe Perches } 2133beb42ecSJoe Perches 2143beb42ecSJoe Perches exit($exitcode); 2153beb42ecSJoe Perches} 2163beb42ecSJoe Perches 217000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 218000d1cc1SJoe Perchesif (-f $conf) { 219000d1cc1SJoe Perches my @conf_args; 220000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 221000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 222000d1cc1SJoe Perches 223000d1cc1SJoe Perches while (<$conffile>) { 224000d1cc1SJoe Perches my $line = $_; 225000d1cc1SJoe Perches 226000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 227000d1cc1SJoe Perches $line =~ s/^\s*//g; 228000d1cc1SJoe Perches $line =~ s/\s+/ /g; 229000d1cc1SJoe Perches 230000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 231000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 232000d1cc1SJoe Perches 233000d1cc1SJoe Perches my @words = split(" ", $line); 234000d1cc1SJoe Perches foreach my $word (@words) { 235000d1cc1SJoe Perches last if ($word =~ m/^#/); 236000d1cc1SJoe Perches push (@conf_args, $word); 237000d1cc1SJoe Perches } 238000d1cc1SJoe Perches } 239000d1cc1SJoe Perches close($conffile); 240000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 241000d1cc1SJoe Perches} 242000d1cc1SJoe Perches 24352178ce0SDwaipayan Raysub load_docs { 24452178ce0SDwaipayan Ray open(my $docs, '<', "$docsfile") 24552178ce0SDwaipayan Ray or warn "$P: Can't read the documentation file $docsfile $!\n"; 24652178ce0SDwaipayan Ray 24752178ce0SDwaipayan Ray my $type = ''; 24852178ce0SDwaipayan Ray my $desc = ''; 24952178ce0SDwaipayan Ray my $in_desc = 0; 25052178ce0SDwaipayan Ray 25152178ce0SDwaipayan Ray while (<$docs>) { 25252178ce0SDwaipayan Ray chomp; 25352178ce0SDwaipayan Ray my $line = $_; 25452178ce0SDwaipayan Ray $line =~ s/\s+$//; 25552178ce0SDwaipayan Ray 25652178ce0SDwaipayan Ray if ($line =~ /^\s*\*\*(.+)\*\*$/) { 25752178ce0SDwaipayan Ray if ($desc ne '') { 25852178ce0SDwaipayan Ray $verbose_messages{$type} = trim($desc); 25952178ce0SDwaipayan Ray } 26052178ce0SDwaipayan Ray $type = $1; 26152178ce0SDwaipayan Ray $desc = ''; 26252178ce0SDwaipayan Ray $in_desc = 1; 26352178ce0SDwaipayan Ray } elsif ($in_desc) { 26452178ce0SDwaipayan Ray if ($line =~ /^(?:\s{4,}|$)/) { 26552178ce0SDwaipayan Ray $line =~ s/^\s{4}//; 26652178ce0SDwaipayan Ray $desc .= $line; 26752178ce0SDwaipayan Ray $desc .= "\n"; 26852178ce0SDwaipayan Ray } else { 26952178ce0SDwaipayan Ray $verbose_messages{$type} = trim($desc); 27052178ce0SDwaipayan Ray $type = ''; 27152178ce0SDwaipayan Ray $desc = ''; 27252178ce0SDwaipayan Ray $in_desc = 0; 27352178ce0SDwaipayan Ray } 27452178ce0SDwaipayan Ray } 27552178ce0SDwaipayan Ray } 27652178ce0SDwaipayan Ray 27752178ce0SDwaipayan Ray if ($desc ne '') { 27852178ce0SDwaipayan Ray $verbose_messages{$type} = trim($desc); 27952178ce0SDwaipayan Ray } 28052178ce0SDwaipayan Ray close($docs); 28152178ce0SDwaipayan Ray} 28252178ce0SDwaipayan Ray 283737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 284737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 285737c0767SJohn Brooksforeach (@ARGV) { 286737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 287737c0767SJohn Brooks $_ = "--color=$color"; 288737c0767SJohn Brooks } 289737c0767SJohn Brooks} 290737c0767SJohn Brooks 2910a920b5bSAndy WhitcroftGetOptions( 2926c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 29352178ce0SDwaipayan Ray 'v|verbose!' => \$verbose, 2940a920b5bSAndy Whitcroft 'tree!' => \$tree, 2950a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2960a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2976c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2988905a67cSAndy Whitcroft 'terse!' => \$terse, 29934d8815fSJoe Perches 'showfile!' => \$showfile, 30077f5b10aSHannes Eder 'f|file!' => \$file, 3014a593c34SDu, Changbin 'g|git!' => \$git, 3026c72ffaaSAndy Whitcroft 'subjective!' => \$check, 3036c72ffaaSAndy Whitcroft 'strict!' => \$check, 304000d1cc1SJoe Perches 'ignore=s' => \@ignore, 30591bfe484SJoe Perches 'types=s' => \@use, 306000d1cc1SJoe Perches 'show-types!' => \$show_types, 3073beb42ecSJoe Perches 'list-types!' => \$list_types, 3086cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 30956193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 310713a09deSAntonio Borneo 'tab-size=i' => \$tabsize, 3116c72ffaaSAndy Whitcroft 'root=s' => \$root, 3128905a67cSAndy Whitcroft 'summary!' => \$summary, 3138905a67cSAndy Whitcroft 'mailback!' => \$mailback, 31413214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 3153705ce5bSJoe Perches 'fix!' => \$fix, 3169624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 317d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 318c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 319773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 320ebfd7d62SJoe Perches 'codespell!' => \$codespell, 3210ee3e7b8SPeter Ujfalusi 'codespellfile=s' => \$user_codespellfile, 32275ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 323737c0767SJohn Brooks 'color=s' => \$color, 324737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 325737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 3263e89ad85SJerome Forissier 'kconfig-prefix=s' => \${CONFIG_}, 32777f5b10aSHannes Eder 'h|help' => \$help, 32877f5b10aSHannes Eder 'version' => \$help 3290ee3e7b8SPeter Ujfalusi) or $help = 2; 33077f5b10aSHannes Eder 3310ee3e7b8SPeter Ujfalusiif ($user_codespellfile) { 3320ee3e7b8SPeter Ujfalusi # Use the user provided codespell file unconditionally 3330ee3e7b8SPeter Ujfalusi $codespellfile = $user_codespellfile; 3340ee3e7b8SPeter Ujfalusi} elsif (!(-f $codespellfile)) { 3350ee3e7b8SPeter Ujfalusi # If /usr/share/codespell/dictionary.txt is not present, try to find it 3360ee3e7b8SPeter Ujfalusi # under codespell's install directory: <codespell_root>/data/dictionary.txt 337c882c6b1SSagar Patel if (($codespell || $help) && which("python3") ne "") { 3380ee3e7b8SPeter Ujfalusi my $python_codespell_dict = << "EOF"; 3390ee3e7b8SPeter Ujfalusi 3400ee3e7b8SPeter Ujfalusiimport os.path as op 3410ee3e7b8SPeter Ujfalusiimport codespell_lib 3420ee3e7b8SPeter Ujfalusicodespell_dir = op.dirname(codespell_lib.__file__) 3430ee3e7b8SPeter Ujfalusicodespell_file = op.join(codespell_dir, 'data', 'dictionary.txt') 3440ee3e7b8SPeter Ujfalusiprint(codespell_file, end='') 3450ee3e7b8SPeter UjfalusiEOF 3460ee3e7b8SPeter Ujfalusi 347c882c6b1SSagar Patel my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`; 3480ee3e7b8SPeter Ujfalusi $codespellfile = $codespell_dict if (-f $codespell_dict); 3490ee3e7b8SPeter Ujfalusi } 3500ee3e7b8SPeter Ujfalusi} 3510ee3e7b8SPeter Ujfalusi 3520ee3e7b8SPeter Ujfalusi# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0 3530ee3e7b8SPeter Ujfalusi# $help is 2 if invalid option is passed - exitcode: 1 3540ee3e7b8SPeter Ujfalusihelp($help - 1) if ($help); 3550a920b5bSAndy Whitcroft 35652178ce0SDwaipayan Raydie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); 35752178ce0SDwaipayan Raydie "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse); 35852178ce0SDwaipayan Ray 35952178ce0SDwaipayan Rayif ($color =~ /^[01]$/) { 36052178ce0SDwaipayan Ray $color = !$color; 36152178ce0SDwaipayan Ray} elsif ($color =~ /^always$/i) { 36252178ce0SDwaipayan Ray $color = 1; 36352178ce0SDwaipayan Ray} elsif ($color =~ /^never$/i) { 36452178ce0SDwaipayan Ray $color = 0; 36552178ce0SDwaipayan Ray} elsif ($color =~ /^auto$/i) { 36652178ce0SDwaipayan Ray $color = (-t STDOUT); 36752178ce0SDwaipayan Ray} else { 36852178ce0SDwaipayan Ray die "$P: Invalid color mode: $color\n"; 36952178ce0SDwaipayan Ray} 37052178ce0SDwaipayan Ray 37152178ce0SDwaipayan Rayload_docs() if ($verbose); 3723beb42ecSJoe Percheslist_types(0) if ($list_types); 3733beb42ecSJoe Perches 3749624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 3752ac73b4fSJoe Perches$check_orig = $check; 3769624b8d6SJoe Perches 3770a920b5bSAndy Whitcroftmy $exit = 0; 3780a920b5bSAndy Whitcroft 3795b57980dSJoe Perchesmy $perl_version_ok = 1; 380d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 3815b57980dSJoe Perches $perl_version_ok = 0; 382d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 3835b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 384d62a201fSDave Hansen} 385d62a201fSDave Hansen 38645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 3870a920b5bSAndy Whitcroftif ($#ARGV < 0) { 38845107ff6SAllen Hubbe push(@ARGV, '-'); 3890a920b5bSAndy Whitcroft} 3900a920b5bSAndy Whitcroft 391713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1 39232f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); 393713a09deSAntonio Borneo 39491bfe484SJoe Perchessub hash_save_array_words { 39591bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 39691bfe484SJoe Perches 39791bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 39891bfe484SJoe Perches foreach my $word (@array) { 399000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 400000d1cc1SJoe Perches $word =~ s/^\s*//g; 401000d1cc1SJoe Perches $word =~ s/\s+/ /g; 402000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 403000d1cc1SJoe Perches 404000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 405000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 406000d1cc1SJoe Perches 40791bfe484SJoe Perches $hashRef->{$word}++; 408000d1cc1SJoe Perches } 40991bfe484SJoe Perches} 41091bfe484SJoe Perches 41191bfe484SJoe Perchessub hash_show_words { 41291bfe484SJoe Perches my ($hashRef, $prefix) = @_; 41391bfe484SJoe Perches 4143c816e49SJoe Perches if (keys %$hashRef) { 415d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 41658cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 41791bfe484SJoe Perches print " $word"; 41891bfe484SJoe Perches } 419d8469f16SJoe Perches print "\n"; 42091bfe484SJoe Perches } 42191bfe484SJoe Perches} 42291bfe484SJoe Perches 42391bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 42491bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 425000d1cc1SJoe Perches 426c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 427c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 4287429c690SAndy Whitcroftmy $dbg_type = 0; 429a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 430c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 43121caa13cSAndy Whitcroft ## no critic 43221caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 43321caa13cSAndy Whitcroft die "$@" if ($@); 434c2fdda0dSAndy Whitcroft} 435c2fdda0dSAndy Whitcroft 436d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 437d2c0a235SAndy Whitcroft 4388905a67cSAndy Whitcroftif ($terse) { 4398905a67cSAndy Whitcroft $emacs = 1; 4408905a67cSAndy Whitcroft $quiet++; 4418905a67cSAndy Whitcroft} 4428905a67cSAndy Whitcroft 4436c72ffaaSAndy Whitcroftif ($tree) { 4446c72ffaaSAndy Whitcroft if (defined $root) { 4456c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 4466c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 4476c72ffaaSAndy Whitcroft } 4486c72ffaaSAndy Whitcroft } else { 4496c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 4506c72ffaaSAndy Whitcroft $root = '.'; 4516c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 4526c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 4536c72ffaaSAndy Whitcroft $root = $1; 4546c72ffaaSAndy Whitcroft } 4556c72ffaaSAndy Whitcroft } 4566c72ffaaSAndy Whitcroft 4576c72ffaaSAndy Whitcroft if (!defined $root) { 4580a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 4590a920b5bSAndy Whitcroft exit(2); 4600a920b5bSAndy Whitcroft } 4616c72ffaaSAndy Whitcroft} 4626c72ffaaSAndy Whitcroft 4636c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 4646c72ffaaSAndy Whitcroft 4652ceb532bSAndy Whitcroftour $Ident = qr{ 4662ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 4672ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 4682ceb532bSAndy Whitcroft }x; 4696c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 4706c72ffaaSAndy Whitcroftour $Sparse = qr{ 4716c72ffaaSAndy Whitcroft __user| 4726c72ffaaSAndy Whitcroft __kernel| 4736c72ffaaSAndy Whitcroft __force| 4746c72ffaaSAndy Whitcroft __iomem| 4756c72ffaaSAndy Whitcroft __must_check| 476417495edSAndy Whitcroft __kprobes| 477165e72a6SSven Eckelmann __ref| 47833aa4597SGeert Uytterhoeven __refconst| 47933aa4597SGeert Uytterhoeven __refdata| 480ad315455SBoqun Feng __rcu| 481ad315455SBoqun Feng __private 4826c72ffaaSAndy Whitcroft }x; 483e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 484e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 485e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 486e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 487e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 4888716de38SJoe Perches 48952131292SWolfram Sang# Notes to $Attribute: 49052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 4916c72ffaaSAndy Whitcroftour $Attribute = qr{ 4926c72ffaaSAndy Whitcroft const| 493b5e8736aSJoe Perches volatile| 49403f1df7dSJoe Perches __percpu| 49503f1df7dSJoe Perches __nocast| 49603f1df7dSJoe Perches __safe| 49746d832f5SMichael S. Tsirkin __bitwise| 49803f1df7dSJoe Perches __packed__| 49903f1df7dSJoe Perches __packed2__| 50003f1df7dSJoe Perches __naked| 50103f1df7dSJoe Perches __maybe_unused| 50203f1df7dSJoe Perches __always_unused| 50303f1df7dSJoe Perches __noreturn| 50403f1df7dSJoe Perches __used| 50503f1df7dSJoe Perches __cold| 506e23ef1f3SJoe Perches __pure| 50703f1df7dSJoe Perches __noclone| 50803f1df7dSJoe Perches __deprecated| 5096c72ffaaSAndy Whitcroft __read_mostly| 510c5967e98SJoe Perches __ro_after_init| 5116c72ffaaSAndy Whitcroft __kprobes| 5128716de38SJoe Perches $InitAttribute| 51324e1d81aSAndy Whitcroft ____cacheline_aligned| 51424e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 5155fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 51686cffecdSKees Cook __weak| 51786cffecdSKees Cook __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) 5186c72ffaaSAndy Whitcroft }x; 519c45dcabdSAndy Whitcroftour $Modifier; 52091cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 5216c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 5226c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 5236c72ffaaSAndy Whitcroft 52495e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 52595e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 52695e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 52795e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 5282435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 529d2af5aa6SJoe Perchesour $String = qr{(?:\b[Lu])?"[X\t]*"}; 530326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 531326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 532326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 53374349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 5342435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 535326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 536447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 53723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 5386c72ffaaSAndy Whitcroftour $Operators = qr{ 5396c72ffaaSAndy Whitcroft <=|>=|==|!=| 5406c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 54123f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 5426c72ffaaSAndy Whitcroft }x; 5436c72ffaaSAndy Whitcroft 54491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 54591cb5195SJoe Perches 546ab7e23f3SJoe Perchesour $BasicType; 5478905a67cSAndy Whitcroftour $NonptrType; 5481813087dSJoe Perchesour $NonptrTypeMisordered; 5498716de38SJoe Perchesour $NonptrTypeWithAttr; 5508905a67cSAndy Whitcroftour $Type; 5511813087dSJoe Perchesour $TypeMisordered; 5528905a67cSAndy Whitcroftour $Declare; 5531813087dSJoe Perchesour $DeclareMisordered; 5548905a67cSAndy Whitcroft 55515662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 55615662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 557171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 558171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 559171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 560171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 561171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 562171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 563171ae1a4SAndy Whitcroft}x; 564171ae1a4SAndy Whitcroft 56515662b3eSJoe Perchesour $UTF8 = qr{ 56615662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 56715662b3eSJoe Perches | $NON_ASCII_UTF8 56815662b3eSJoe Perches}x; 56915662b3eSJoe Perches 570e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 571021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 572021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 573021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 574021158b4SJoe Perches)}; 575e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 576fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 5778ed22cadSAndy Whitcroft atomic_t 5788ed22cadSAndy Whitcroft)}; 5798ea0114eSMickaël Salaünour $typeStdioTypedefs = qr{(?x: 5808ea0114eSMickaël Salaün FILE 5818ea0114eSMickaël Salaün)}; 582e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 583e6176fa4SJoe Perches $typeC99Typedefs\b| 584e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 5858ea0114eSMickaël Salaün $typeKernelTypedefs\b| 5868ea0114eSMickaël Salaün $typeStdioTypedefs\b 587e6176fa4SJoe Perches)}; 5888ed22cadSAndy Whitcroft 5896d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 5906d32f7a3SJoe Perches 591691e669bSJoe Perchesour $logFunctions = qr{(?x: 592758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 5937d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 59487bd499aSJoe Perches TP_printk| 5956e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 596b0531722SJoe Perches panic| 59706668727SJoe Perches MODULE_[A-Z_]+| 59806668727SJoe Perches seq_vprintf|seq_printf|seq_puts 599691e669bSJoe Perches)}; 600691e669bSJoe Perches 601e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 602e29a70f1SJoe Perches (?:(?:devm_)? 60358f02267SJoe Perches (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? | 604e29a70f1SJoe Perches kstrdup(?:_const)? | 605e29a70f1SJoe Perches kmemdup(?:_nul)?) | 606461e1565SChristophe JAILLET (?:\w+)?alloc_skb(?:_ip_align)? | 607e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 608e29a70f1SJoe Perches dma_alloc_coherent 609e29a70f1SJoe Perches)}; 610e29a70f1SJoe Perches 61120112475SJoe Perchesour $signature_tags = qr{(?xi: 61220112475SJoe Perches Signed-off-by:| 613d499480cSJorge Ramirez-Ortiz Co-developed-by:| 61420112475SJoe Perches Acked-by:| 61520112475SJoe Perches Tested-by:| 61620112475SJoe Perches Reviewed-by:| 61720112475SJoe Perches Reported-by:| 6188543ae12SMugunthan V N Suggested-by:| 61920112475SJoe Perches To:| 62020112475SJoe Perches Cc: 62120112475SJoe Perches)}; 62220112475SJoe Perches 623adb2da82SJoe Perchesour $tracing_logging_tags = qr{(?xi: 624adb2da82SJoe Perches [=-]*> | 625adb2da82SJoe Perches <[=-]* | 626adb2da82SJoe Perches \[ | 627adb2da82SJoe Perches \] | 628adb2da82SJoe Perches start | 629adb2da82SJoe Perches called | 630adb2da82SJoe Perches entered | 631adb2da82SJoe Perches entry | 632adb2da82SJoe Perches enter | 633adb2da82SJoe Perches in | 634adb2da82SJoe Perches inside | 635adb2da82SJoe Perches here | 636adb2da82SJoe Perches begin | 637adb2da82SJoe Perches exit | 638adb2da82SJoe Perches end | 639adb2da82SJoe Perches done | 640adb2da82SJoe Perches leave | 641adb2da82SJoe Perches completed | 642adb2da82SJoe Perches out | 643adb2da82SJoe Perches return | 644adb2da82SJoe Perches [\.\!:\s]* 645adb2da82SJoe Perches)}; 646adb2da82SJoe Perches 647831242abSAditya Srivastavasub edit_distance_min { 648831242abSAditya Srivastava my (@arr) = @_; 649831242abSAditya Srivastava my $len = scalar @arr; 650831242abSAditya Srivastava if ((scalar @arr) < 1) { 651831242abSAditya Srivastava # if underflow, return 652831242abSAditya Srivastava return; 653831242abSAditya Srivastava } 654831242abSAditya Srivastava my $min = $arr[0]; 655831242abSAditya Srivastava for my $i (0 .. ($len-1)) { 656831242abSAditya Srivastava if ($arr[$i] < $min) { 657831242abSAditya Srivastava $min = $arr[$i]; 658831242abSAditya Srivastava } 659831242abSAditya Srivastava } 660831242abSAditya Srivastava return $min; 661831242abSAditya Srivastava} 662831242abSAditya Srivastava 663831242abSAditya Srivastavasub get_edit_distance { 664831242abSAditya Srivastava my ($str1, $str2) = @_; 665831242abSAditya Srivastava $str1 = lc($str1); 666831242abSAditya Srivastava $str2 = lc($str2); 667831242abSAditya Srivastava $str1 =~ s/-//g; 668831242abSAditya Srivastava $str2 =~ s/-//g; 669831242abSAditya Srivastava my $len1 = length($str1); 670831242abSAditya Srivastava my $len2 = length($str2); 671831242abSAditya Srivastava # two dimensional array storing minimum edit distance 672831242abSAditya Srivastava my @distance; 673831242abSAditya Srivastava for my $i (0 .. $len1) { 674831242abSAditya Srivastava for my $j (0 .. $len2) { 675831242abSAditya Srivastava if ($i == 0) { 676831242abSAditya Srivastava $distance[$i][$j] = $j; 677831242abSAditya Srivastava } elsif ($j == 0) { 678831242abSAditya Srivastava $distance[$i][$j] = $i; 679831242abSAditya Srivastava } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) { 680831242abSAditya Srivastava $distance[$i][$j] = $distance[$i - 1][$j - 1]; 681831242abSAditya Srivastava } else { 682831242abSAditya Srivastava my $dist1 = $distance[$i][$j - 1]; #insert distance 683831242abSAditya Srivastava my $dist2 = $distance[$i - 1][$j]; # remove 684831242abSAditya Srivastava my $dist3 = $distance[$i - 1][$j - 1]; #replace 685831242abSAditya Srivastava $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3); 686831242abSAditya Srivastava } 687831242abSAditya Srivastava } 688831242abSAditya Srivastava } 689831242abSAditya Srivastava return $distance[$len1][$len2]; 690831242abSAditya Srivastava} 691831242abSAditya Srivastava 692831242abSAditya Srivastavasub find_standard_signature { 693831242abSAditya Srivastava my ($sign_off) = @_; 694831242abSAditya Srivastava my @standard_signature_tags = ( 695831242abSAditya Srivastava 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:', 696831242abSAditya Srivastava 'Reviewed-by:', 'Reported-by:', 'Suggested-by:' 697831242abSAditya Srivastava ); 698831242abSAditya Srivastava foreach my $signature (@standard_signature_tags) { 699831242abSAditya Srivastava return $signature if (get_edit_distance($sign_off, $signature) <= 2); 700831242abSAditya Srivastava } 701831242abSAditya Srivastava 702831242abSAditya Srivastava return ""; 703831242abSAditya Srivastava} 704831242abSAditya Srivastava 7059b71f79fSBjorn Helgaasour $obsolete_archives = qr{(?xi: 7069b71f79fSBjorn Helgaas \Qfreedesktop.org/archives/dri-devel\E | 7079b71f79fSBjorn Helgaas \Qlists.infradead.org\E | 7089b71f79fSBjorn Helgaas \Qlkml.org\E | 7099b71f79fSBjorn Helgaas \Qmail-archive.com\E | 7109b71f79fSBjorn Helgaas \Qmailman.alsa-project.org/pipermail\E | 7119b71f79fSBjorn Helgaas \Qmarc.info\E | 7129b71f79fSBjorn Helgaas \Qozlabs.org/pipermail\E | 7139b71f79fSBjorn Helgaas \Qspinics.net\E 7149b71f79fSBjorn Helgaas)}; 7159b71f79fSBjorn Helgaas 7161813087dSJoe Perchesour @typeListMisordered = ( 7171813087dSJoe Perches qr{char\s+(?:un)?signed}, 7181813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 7191813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 7201813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 7211813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 7221813087dSJoe Perches qr{short\s+(?:un)?signed}, 7231813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 7241813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 7251813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 7261813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 7271813087dSJoe Perches qr{int\s+(?:un)?signed}, 7281813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 7291813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 7301813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 7311813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 7321813087dSJoe Perches qr{long\s+(?:un)?signed}, 7331813087dSJoe Perches); 7341813087dSJoe Perches 7358905a67cSAndy Whitcroftour @typeList = ( 7368905a67cSAndy Whitcroft qr{void}, 7370c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 7380c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 7390c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 7400c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 7410c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 7420c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 7430c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 7440c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 7450c773d9dSJoe Perches qr{(?:un)?signed}, 7468905a67cSAndy Whitcroft qr{float}, 7478905a67cSAndy Whitcroft qr{double}, 7488905a67cSAndy Whitcroft qr{bool}, 7498905a67cSAndy Whitcroft qr{struct\s+$Ident}, 7508905a67cSAndy Whitcroft qr{union\s+$Ident}, 7518905a67cSAndy Whitcroft qr{enum\s+$Ident}, 7528905a67cSAndy Whitcroft qr{${Ident}_t}, 7538905a67cSAndy Whitcroft qr{${Ident}_handler}, 7548905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 7551813087dSJoe Perches @typeListMisordered, 7568905a67cSAndy Whitcroft); 757938224b5SJoe Perches 758938224b5SJoe Perchesour $C90_int_types = qr{(?x: 759938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 760938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 761938224b5SJoe Perches long\s+long\s+(?:un)?signed| 762938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 763938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 764938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 765938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 766938224b5SJoe Perches 767938224b5SJoe Perches long\s+int\s+(?:un)?signed| 768938224b5SJoe Perches long\s+(?:un)?signed\s+int| 769938224b5SJoe Perches long\s+(?:un)?signed| 770938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 771938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 772938224b5SJoe Perches int\s+long\s+(?:un)?signed| 773938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 774938224b5SJoe Perches 775938224b5SJoe Perches int\s+(?:un)?signed| 776938224b5SJoe Perches (?:(?:un)?signed\s+)?int 777938224b5SJoe Perches)}; 778938224b5SJoe Perches 779485ff23eSAlex Dowadour @typeListFile = (); 7808716de38SJoe Perchesour @typeListWithAttr = ( 7818716de38SJoe Perches @typeList, 7828716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 7838716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 7848716de38SJoe Perches); 7858716de38SJoe Perches 786c45dcabdSAndy Whitcroftour @modifierList = ( 787c45dcabdSAndy Whitcroft qr{fastcall}, 788c45dcabdSAndy Whitcroft); 789485ff23eSAlex Dowadour @modifierListFile = (); 7908905a67cSAndy Whitcroft 7912435880fSJoe Perchesour @mode_permission_funcs = ( 7922435880fSJoe Perches ["module_param", 3], 7932435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 7942435880fSJoe Perches ["module_param_array_named", 5], 7952435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 7962435880fSJoe Perches ["proc_create(?:_data|)", 2], 797459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 798459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 799459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 800459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 801459cf0aeSJoe Perches ["__ATTR", 2], 8022435880fSJoe Perches); 8032435880fSJoe Perches 8041a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; 8051a3dcf2eSJoe Perches 806515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 807515a235eSJoe Perchesour $mode_perms_search = ""; 808515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 809515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 810515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 811515a235eSJoe Perches} 81200180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 813515a235eSJoe Perches 8149189c7e7SJoe Perchesour %deprecated_apis = ( 8159189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 8169189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 8179189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 8189189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 8199189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 8209189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 8219189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 8229189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 8239189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 8249189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 825defdaff1SIra Weiny "kmap" => "kmap_local_page", 826*a3ea42ffSIra Weiny "kunmap" => "kunmap_local", 827defdaff1SIra Weiny "kmap_atomic" => "kmap_local_page", 828*a3ea42ffSIra Weiny "kunmap_atomic" => "kunmap_local", 8299189c7e7SJoe Perches); 8309189c7e7SJoe Perches 8319189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 8329189c7e7SJoe Perchesour $deprecated_apis_search = ""; 8339189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 8349189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 8359189c7e7SJoe Perches $deprecated_apis_search .= $entry; 8369189c7e7SJoe Perches} 8379189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 8389189c7e7SJoe Perches 839b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 840b392c64fSJoe Perches S_IWUGO | 841b392c64fSJoe Perches S_IWOTH | 842b392c64fSJoe Perches S_IRWXUGO | 843b392c64fSJoe Perches S_IALLUGO | 844b392c64fSJoe Perches 0[0-7][0-7][2367] 845b392c64fSJoe Perches}x; 846b392c64fSJoe Perches 847f90774e1SJoe Perchesour %mode_permission_string_types = ( 848f90774e1SJoe Perches "S_IRWXU" => 0700, 849f90774e1SJoe Perches "S_IRUSR" => 0400, 850f90774e1SJoe Perches "S_IWUSR" => 0200, 851f90774e1SJoe Perches "S_IXUSR" => 0100, 852f90774e1SJoe Perches "S_IRWXG" => 0070, 853f90774e1SJoe Perches "S_IRGRP" => 0040, 854f90774e1SJoe Perches "S_IWGRP" => 0020, 855f90774e1SJoe Perches "S_IXGRP" => 0010, 856f90774e1SJoe Perches "S_IRWXO" => 0007, 857f90774e1SJoe Perches "S_IROTH" => 0004, 858f90774e1SJoe Perches "S_IWOTH" => 0002, 859f90774e1SJoe Perches "S_IXOTH" => 0001, 860f90774e1SJoe Perches "S_IRWXUGO" => 0777, 861f90774e1SJoe Perches "S_IRUGO" => 0444, 862f90774e1SJoe Perches "S_IWUGO" => 0222, 863f90774e1SJoe Perches "S_IXUGO" => 0111, 864f90774e1SJoe Perches); 865f90774e1SJoe Perches 866f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 867f90774e1SJoe Perchesour $mode_perms_string_search = ""; 868f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 869f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 870f90774e1SJoe Perches $mode_perms_string_search .= $entry; 871f90774e1SJoe Perches} 87200180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 87300180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 87400180468SJoe Perches ${single_mode_perms_string_search} 87500180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 87600180468SJoe Perches}x; 87700180468SJoe Perches 87800180468SJoe Perchessub perms_to_octal { 87900180468SJoe Perches my ($string) = @_; 88000180468SJoe Perches 88100180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 88200180468SJoe Perches 88300180468SJoe Perches my $val = ""; 88400180468SJoe Perches my $oval = ""; 88500180468SJoe Perches my $to = 0; 88600180468SJoe Perches my $curpos = 0; 88700180468SJoe Perches my $lastpos = 0; 88800180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 88900180468SJoe Perches $curpos = pos($string); 89000180468SJoe Perches my $match = $2; 89100180468SJoe Perches my $omatch = $1; 89200180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 89300180468SJoe Perches $lastpos = $curpos; 89400180468SJoe Perches $to |= $mode_permission_string_types{$match}; 89500180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 89600180468SJoe Perches $val .= $match; 89700180468SJoe Perches $oval .= $omatch; 89800180468SJoe Perches } 89900180468SJoe Perches $oval =~ s/^\s*\|\s*//; 90000180468SJoe Perches $oval =~ s/\s*\|\s*$//; 90100180468SJoe Perches return sprintf("%04o", $to); 90200180468SJoe Perches} 903f90774e1SJoe Perches 9047840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 9057840a94cSWolfram Sang irq| 906cdcee686SSergey Ryazanov memory| 907cdcee686SSergey Ryazanov time| 908cdcee686SSergey Ryazanov reboot 9097840a94cSWolfram Sang)}; 9107840a94cSWolfram Sang# memory.h: ARM has a custom one 9117840a94cSWolfram Sang 91266b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 91366b47b4aSKees Cookmy $misspellings; 91466b47b4aSKees Cookmy %spelling_fix; 91536061e38SJoe Perches 91636061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 91766b47b4aSKees Cook while (<$spelling>) { 91866b47b4aSKees Cook my $line = $_; 91966b47b4aSKees Cook 92066b47b4aSKees Cook $line =~ s/\s*\n?$//g; 92166b47b4aSKees Cook $line =~ s/^\s*//g; 92266b47b4aSKees Cook 92366b47b4aSKees Cook next if ($line =~ m/^\s*#/); 92466b47b4aSKees Cook next if ($line =~ m/^\s*$/); 92566b47b4aSKees Cook 92666b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 92766b47b4aSKees Cook 92866b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 92966b47b4aSKees Cook } 93066b47b4aSKees Cook close($spelling); 93136061e38SJoe Perches} else { 93236061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 93336061e38SJoe Perches} 93466b47b4aSKees Cook 935ebfd7d62SJoe Perchesif ($codespell) { 936ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 937ebfd7d62SJoe Perches while (<$spelling>) { 938ebfd7d62SJoe Perches my $line = $_; 939ebfd7d62SJoe Perches 940ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 941ebfd7d62SJoe Perches $line =~ s/^\s*//g; 942ebfd7d62SJoe Perches 943ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 944ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 945ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 946ebfd7d62SJoe Perches 947ebfd7d62SJoe Perches $line =~ s/,.*$//; 948ebfd7d62SJoe Perches 949ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 950ebfd7d62SJoe Perches 951ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 952ebfd7d62SJoe Perches } 953ebfd7d62SJoe Perches close($spelling); 954ebfd7d62SJoe Perches } else { 955ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 956ebfd7d62SJoe Perches } 957ebfd7d62SJoe Perches} 958ebfd7d62SJoe Perches 959ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 960ebfd7d62SJoe Perches 96175ad8c57SJerome Forissiersub read_words { 96275ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 96375ad8c57SJerome Forissier 96475ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 96575ad8c57SJerome Forissier while (<$words>) { 966bf1fa1daSJoe Perches my $line = $_; 967bf1fa1daSJoe Perches 968bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 969bf1fa1daSJoe Perches $line =~ s/^\s*//g; 970bf1fa1daSJoe Perches 971bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 972bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 973bf1fa1daSJoe Perches if ($line =~ /\s/) { 97475ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 975bf1fa1daSJoe Perches next; 976bf1fa1daSJoe Perches } 977bf1fa1daSJoe Perches 978ced69da1SQuentin Monnet $$wordsRef .= '|' if (defined $$wordsRef); 97975ad8c57SJerome Forissier $$wordsRef .= $line; 980bf1fa1daSJoe Perches } 98175ad8c57SJerome Forissier close($file); 98275ad8c57SJerome Forissier return 1; 983bf1fa1daSJoe Perches } 984bf1fa1daSJoe Perches 98575ad8c57SJerome Forissier return 0; 98675ad8c57SJerome Forissier} 98775ad8c57SJerome Forissier 988ced69da1SQuentin Monnetmy $const_structs; 989ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) { 99075ad8c57SJerome Forissier read_words(\$const_structs, $conststructsfile) 99175ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 992ced69da1SQuentin Monnet} 99375ad8c57SJerome Forissier 994ced69da1SQuentin Monnetif (defined($typedefsfile)) { 995ced69da1SQuentin Monnet my $typeOtherTypedefs; 99675ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 99775ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 998ced69da1SQuentin Monnet $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); 99975ad8c57SJerome Forissier} 100075ad8c57SJerome Forissier 10018905a67cSAndy Whitcroftsub build_types { 1002485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 1003485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 10041813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 10058716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 1006c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 1007ab7e23f3SJoe Perches $BasicType = qr{ 1008ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 1009ab7e23f3SJoe Perches (?:${all}\b) 1010ab7e23f3SJoe Perches }x; 10118905a67cSAndy Whitcroft $NonptrType = qr{ 1012d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 1013cf655043SAndy Whitcroft (?: 10146b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 10158ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 1016c45dcabdSAndy Whitcroft (?:${all}\b) 1017cf655043SAndy Whitcroft ) 1018c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 10198905a67cSAndy Whitcroft }x; 10201813087dSJoe Perches $NonptrTypeMisordered = qr{ 10211813087dSJoe Perches (?:$Modifier\s+|const\s+)* 10221813087dSJoe Perches (?: 10231813087dSJoe Perches (?:${Misordered}\b) 10241813087dSJoe Perches ) 10251813087dSJoe Perches (?:\s+$Modifier|\s+const)* 10261813087dSJoe Perches }x; 10278716de38SJoe Perches $NonptrTypeWithAttr = qr{ 10288716de38SJoe Perches (?:$Modifier\s+|const\s+)* 10298716de38SJoe Perches (?: 10308716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 10318716de38SJoe Perches (?:$typeTypedefs\b)| 10328716de38SJoe Perches (?:${allWithAttr}\b) 10338716de38SJoe Perches ) 10348716de38SJoe Perches (?:\s+$Modifier|\s+const)* 10358716de38SJoe Perches }x; 10368905a67cSAndy Whitcroft $Type = qr{ 1037c45dcabdSAndy Whitcroft $NonptrType 10387b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 1039c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 10408905a67cSAndy Whitcroft }x; 10411813087dSJoe Perches $TypeMisordered = qr{ 10421813087dSJoe Perches $NonptrTypeMisordered 10437b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 10441813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 10451813087dSJoe Perches }x; 104691cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 10471813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 10488905a67cSAndy Whitcroft} 10498905a67cSAndy Whitcroftbuild_types(); 10506c72ffaaSAndy Whitcroft 10517d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 1052d1fe9c09SJoe Perches 1053d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 1054d1fe9c09SJoe Perches# requires at least perl version v5.10.0 1055d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 1056d1fe9c09SJoe Perches 1057d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 10582435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 1059c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 10607d2367afSJoe Perches 1061f8422308SJoe Perchesour $declaration_macros = qr{(?x: 10623e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 1063fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 1064dcea7964SJoe Perches (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(| 1065dcea7964SJoe Perches (?:$Storage\s+)?(?:XA_STATE|XA_STATE_ORDER)\s*\( 1066f8422308SJoe Perches)}; 1067f8422308SJoe Perches 10688d0325ccSAditya Srivastavaour %allow_repeated_words = ( 10698d0325ccSAditya Srivastava add => '', 10708d0325ccSAditya Srivastava added => '', 10718d0325ccSAditya Srivastava bad => '', 10728d0325ccSAditya Srivastava be => '', 10738d0325ccSAditya Srivastava); 10748d0325ccSAditya Srivastava 10757d2367afSJoe Perchessub deparenthesize { 10767d2367afSJoe Perches my ($string) = @_; 10777d2367afSJoe Perches return "" if (!defined($string)); 10785b9553abSJoe Perches 10795b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 10805b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 10815b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 10825b9553abSJoe Perches } 10835b9553abSJoe Perches 10847d2367afSJoe Perches $string =~ s@\s+@ @g; 10855b9553abSJoe Perches 10867d2367afSJoe Perches return $string; 10877d2367afSJoe Perches} 10887d2367afSJoe Perches 10893445686aSJoe Perchessub seed_camelcase_file { 10903445686aSJoe Perches my ($file) = @_; 10913445686aSJoe Perches 10923445686aSJoe Perches return if (!(-f $file)); 10933445686aSJoe Perches 10943445686aSJoe Perches local $/; 10953445686aSJoe Perches 10963445686aSJoe Perches open(my $include_file, '<', "$file") 10973445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 10983445686aSJoe Perches my $text = <$include_file>; 10993445686aSJoe Perches close($include_file); 11003445686aSJoe Perches 11013445686aSJoe Perches my @lines = split('\n', $text); 11023445686aSJoe Perches 11033445686aSJoe Perches foreach my $line (@lines) { 11043445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 11053445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 11063445686aSJoe Perches $camelcase{$1} = 1; 110711ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 110811ea516aSJoe Perches $camelcase{$1} = 1; 110911ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 11103445686aSJoe Perches $camelcase{$1} = 1; 11113445686aSJoe Perches } 11123445686aSJoe Perches } 11133445686aSJoe Perches} 11143445686aSJoe Perches 1115cd28b119SJoe Perchesour %maintained_status = (); 1116cd28b119SJoe Perches 111785b0ee18SJoe Perchessub is_maintained_obsolete { 111885b0ee18SJoe Perches my ($filename) = @_; 111985b0ee18SJoe Perches 1120f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 112185b0ee18SJoe Perches 1122cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 1123cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 1124cd28b119SJoe Perches } 112585b0ee18SJoe Perches 1126cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 112785b0ee18SJoe Perches} 112885b0ee18SJoe Perches 11293b6e8ac9SJoe Perchessub is_SPDX_License_valid { 11303b6e8ac9SJoe Perches my ($license) = @_; 11313b6e8ac9SJoe Perches 1132f9363b31SGuenter Roeck return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); 11333b6e8ac9SJoe Perches 113456294112SJoe Perches my $root_path = abs_path($root); 1135f9363b31SGuenter Roeck my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`; 11363b6e8ac9SJoe Perches return 0 if ($status ne ""); 11373b6e8ac9SJoe Perches return 1; 11383b6e8ac9SJoe Perches} 11393b6e8ac9SJoe Perches 11403445686aSJoe Perchesmy $camelcase_seeded = 0; 11413445686aSJoe Perchessub seed_camelcase_includes { 11423445686aSJoe Perches return if ($camelcase_seeded); 11433445686aSJoe Perches 11443445686aSJoe Perches my $files; 1145c707a81dSJoe Perches my $camelcase_cache = ""; 1146c707a81dSJoe Perches my @include_files = (); 1147c707a81dSJoe Perches 1148c707a81dSJoe Perches $camelcase_seeded = 1; 1149351b2a1fSJoe Perches 11500f7f635bSJoe Perches if (-e "$gitroot") { 1151dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 1152351b2a1fSJoe Perches chomp $git_last_include_commit; 1153c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 1154c707a81dSJoe Perches } else { 1155c707a81dSJoe Perches my $last_mod_date = 0; 1156c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 1157c707a81dSJoe Perches @include_files = split('\n', $files); 1158c707a81dSJoe Perches foreach my $file (@include_files) { 1159c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 1160c707a81dSJoe Perches localtime((stat $file)[9])); 1161c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 1162c707a81dSJoe Perches } 1163c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 1164c707a81dSJoe Perches } 1165c707a81dSJoe Perches 1166c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 1167c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 1168c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 1169351b2a1fSJoe Perches while (<$camelcase_file>) { 1170351b2a1fSJoe Perches chomp; 1171351b2a1fSJoe Perches $camelcase{$_} = 1; 1172351b2a1fSJoe Perches } 1173351b2a1fSJoe Perches close($camelcase_file); 1174351b2a1fSJoe Perches 1175351b2a1fSJoe Perches return; 1176351b2a1fSJoe Perches } 1177c707a81dSJoe Perches 11780f7f635bSJoe Perches if (-e "$gitroot") { 1179dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 1180c707a81dSJoe Perches @include_files = split('\n', $files); 11813445686aSJoe Perches } 1182c707a81dSJoe Perches 11833445686aSJoe Perches foreach my $file (@include_files) { 11843445686aSJoe Perches seed_camelcase_file($file); 11853445686aSJoe Perches } 1186351b2a1fSJoe Perches 1187c707a81dSJoe Perches if ($camelcase_cache ne "") { 1188351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 1189c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 1190c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 1191351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 1192351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 1193351b2a1fSJoe Perches } 1194351b2a1fSJoe Perches close($camelcase_file); 1195351b2a1fSJoe Perches } 11963445686aSJoe Perches} 11973445686aSJoe Perches 1198f5f61325SJoe Perchessub git_is_single_file { 1199f5f61325SJoe Perches my ($filename) = @_; 1200f5f61325SJoe Perches 1201f5f61325SJoe Perches return 0 if ((which("git") eq "") || !(-e "$gitroot")); 1202f5f61325SJoe Perches 1203f5f61325SJoe Perches my $output = `${git_command} ls-files -- $filename 2>/dev/null`; 1204f5f61325SJoe Perches my $count = $output =~ tr/\n//; 1205f5f61325SJoe Perches return $count eq 1 && $output =~ m{^${filename}$}; 1206f5f61325SJoe Perches} 1207f5f61325SJoe Perches 1208d311cd44SJoe Perchessub git_commit_info { 1209d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 1210d311cd44SJoe Perches 12110f7f635bSJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); 1212d311cd44SJoe Perches 1213dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 1214d311cd44SJoe Perches $output =~ s/^\s*//gm; 1215d311cd44SJoe Perches my @lines = split("\n", $output); 1216d311cd44SJoe Perches 12170d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 12180d7835fcSJoe Perches 12195a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 1220d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 1221d311cd44SJoe Perches# all matching commit ids, but it's very slow... 1222d311cd44SJoe Perches# 1223d311cd44SJoe Perches# echo "checking commits $1..." 1224d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 1225d311cd44SJoe Perches# while read line ; do 1226d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 1227d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 1228d311cd44SJoe Perches# done 12294ce9f970SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ || 12304ce9f970SJoe Perches $lines[0] =~ /^fatal: bad object $commit/) { 1231948b133aSHeinrich Schuchardt $id = undef; 1232d311cd44SJoe Perches } else { 1233d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 1234d311cd44SJoe Perches $desc = substr($lines[0], 41); 1235d311cd44SJoe Perches } 1236d311cd44SJoe Perches 1237d311cd44SJoe Perches return ($id, $desc); 1238d311cd44SJoe Perches} 1239d311cd44SJoe Perches 12406c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 12410a920b5bSAndy Whitcroft 124200df344fSAndy Whitcroftmy @rawlines = (); 1243c2fdda0dSAndy Whitcroftmy @lines = (); 12443705ce5bSJoe Perchesmy @fixed = (); 1245d752fcc8SJoe Perchesmy @fixed_inserted = (); 1246d752fcc8SJoe Perchesmy @fixed_deleted = (); 1247194f66fcSJoe Perchesmy $fixlinenr = -1; 1248194f66fcSJoe Perches 12494a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 12504a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 12510f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot"); 12524a593c34SDu, Changbin 12534a593c34SDu, Changbinif ($git) { 12544a593c34SDu, Changbin my @commits = (); 12550dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 12564a593c34SDu, Changbin my $git_range; 125728898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 125828898fd1SJoe Perches $git_range = "-$2 $1"; 12594a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 12604a593c34SDu, Changbin $git_range = "$commit_expr"; 12614a593c34SDu, Changbin } else { 12620dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 12630dea9f1eSJoe Perches } 1264dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 12650dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 126628898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 126728898fd1SJoe Perches next if (!defined($1) || !defined($2)); 12680dea9f1eSJoe Perches my $sha1 = $1; 12690dea9f1eSJoe Perches my $subject = $2; 12700dea9f1eSJoe Perches unshift(@commits, $sha1); 12710dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 12724a593c34SDu, Changbin } 12734a593c34SDu, Changbin } 12744a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 12754a593c34SDu, Changbin @ARGV = @commits; 12764a593c34SDu, Changbin} 12774a593c34SDu, Changbin 1278c2fdda0dSAndy Whitcroftmy $vname; 127998005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 12806c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 128121caa13cSAndy Whitcroft my $FILE; 1282f5f61325SJoe Perches my $is_git_file = git_is_single_file($filename); 1283f5f61325SJoe Perches my $oldfile = $file; 1284f5f61325SJoe Perches $file = 1 if ($is_git_file); 12854a593c34SDu, Changbin if ($git) { 12864a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 12874a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 12884a593c34SDu, Changbin } elsif ($file) { 128921caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 12906c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 129121caa13cSAndy Whitcroft } elsif ($filename eq '-') { 129221caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 12936c72ffaaSAndy Whitcroft } else { 129421caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 12956c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 12966c72ffaaSAndy Whitcroft } 1297c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1298c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 12994a593c34SDu, Changbin } elsif ($git) { 13000dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1301c2fdda0dSAndy Whitcroft } else { 1302c2fdda0dSAndy Whitcroft $vname = $filename; 1303c2fdda0dSAndy Whitcroft } 130421caa13cSAndy Whitcroft while (<$FILE>) { 13050a920b5bSAndy Whitcroft chomp; 130600df344fSAndy Whitcroft push(@rawlines, $_); 1307c7f574d0SGeert Uytterhoeven $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); 13086c72ffaaSAndy Whitcroft } 130921caa13cSAndy Whitcroft close($FILE); 1310d8469f16SJoe Perches 1311d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1312d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1313d8469f16SJoe Perches print "$vname\n"; 1314d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1315d8469f16SJoe Perches } 1316d8469f16SJoe Perches 1317c2fdda0dSAndy Whitcroft if (!process($filename)) { 13180a920b5bSAndy Whitcroft $exit = 1; 13190a920b5bSAndy Whitcroft } 132000df344fSAndy Whitcroft @rawlines = (); 132113214adfSAndy Whitcroft @lines = (); 13223705ce5bSJoe Perches @fixed = (); 1323d752fcc8SJoe Perches @fixed_inserted = (); 1324d752fcc8SJoe Perches @fixed_deleted = (); 1325194f66fcSJoe Perches $fixlinenr = -1; 1326485ff23eSAlex Dowad @modifierListFile = (); 1327485ff23eSAlex Dowad @typeListFile = (); 1328485ff23eSAlex Dowad build_types(); 1329f5f61325SJoe Perches $file = $oldfile if ($is_git_file); 13300a920b5bSAndy Whitcroft} 13310a920b5bSAndy Whitcroft 1332d8469f16SJoe Perchesif (!$quiet) { 13333c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 13343c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 13353c816e49SJoe Perches 13365b57980dSJoe Perches if (!$perl_version_ok) { 1337d8469f16SJoe Perches print << "EOM" 1338d8469f16SJoe Perches 1339d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 13405b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1341d8469f16SJoe PerchesEOM 1342d8469f16SJoe Perches } 1343d8469f16SJoe Perches if ($exit) { 1344d8469f16SJoe Perches print << "EOM" 1345d8469f16SJoe Perches 1346d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1347d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1348d8469f16SJoe PerchesEOM 1349d8469f16SJoe Perches } 1350d8469f16SJoe Perches} 1351d8469f16SJoe Perches 13520a920b5bSAndy Whitcroftexit($exit); 13530a920b5bSAndy Whitcroft 13540a920b5bSAndy Whitcroftsub top_of_kernel_tree { 13556c72ffaaSAndy Whitcroft my ($root) = @_; 13566c72ffaaSAndy Whitcroft 13576c72ffaaSAndy Whitcroft my @tree_check = ( 13586c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 13596c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 13606c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 13616c72ffaaSAndy Whitcroft ); 13626c72ffaaSAndy Whitcroft 13636c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 13646c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 13650a920b5bSAndy Whitcroft return 0; 13660a920b5bSAndy Whitcroft } 13676c72ffaaSAndy Whitcroft } 13686c72ffaaSAndy Whitcroft return 1; 13696c72ffaaSAndy Whitcroft} 13700a920b5bSAndy Whitcroft 137120112475SJoe Perchessub parse_email { 137220112475SJoe Perches my ($formatted_email) = @_; 137320112475SJoe Perches 137420112475SJoe Perches my $name = ""; 1375fccaebf0SDwaipayan Ray my $quoted = ""; 1376dfa05c28SJoe Perches my $name_comment = ""; 137720112475SJoe Perches my $address = ""; 137820112475SJoe Perches my $comment = ""; 137920112475SJoe Perches 138020112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 138120112475SJoe Perches $name = $1; 138220112475SJoe Perches $address = $2; 138320112475SJoe Perches $comment = $3 if defined $3; 138420112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 138520112475SJoe Perches $address = $1; 138620112475SJoe Perches $comment = $2 if defined $2; 138720112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 138820112475SJoe Perches $address = $1; 138920112475SJoe Perches $comment = $2 if defined $2; 139085e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 139120112475SJoe Perches $name = $formatted_email; 13923705ce5bSJoe Perches $name = trim($name); 139320112475SJoe Perches $name =~ s/^\"|\"$//g; 139420112475SJoe Perches # If there's a name left after stripping spaces and 139520112475SJoe Perches # leading quotes, and the address doesn't have both 139620112475SJoe Perches # leading and trailing angle brackets, the address 139720112475SJoe Perches # is invalid. ie: 139820112475SJoe Perches # "joe smith [email protected]" bad 139920112475SJoe Perches # "joe smith <[email protected]" bad 140020112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 140120112475SJoe Perches $name = ""; 140220112475SJoe Perches $address = ""; 140320112475SJoe Perches $comment = ""; 140420112475SJoe Perches } 140520112475SJoe Perches } 140620112475SJoe Perches 1407fccaebf0SDwaipayan Ray # Extract comments from names excluding quoted parts 1408fccaebf0SDwaipayan Ray # "John D. (Doe)" - Do not extract 1409fccaebf0SDwaipayan Ray if ($name =~ s/\"(.+)\"//) { 1410fccaebf0SDwaipayan Ray $quoted = $1; 1411dfa05c28SJoe Perches } 1412fccaebf0SDwaipayan Ray while ($name =~ s/\s*($balanced_parens)\s*/ /) { 1413fccaebf0SDwaipayan Ray $name_comment .= trim($1); 1414fccaebf0SDwaipayan Ray } 1415fccaebf0SDwaipayan Ray $name =~ s/^[ \"]+|[ \"]+$//g; 1416fccaebf0SDwaipayan Ray $name = trim("$quoted $name"); 1417fccaebf0SDwaipayan Ray 14183705ce5bSJoe Perches $address = trim($address); 141920112475SJoe Perches $address =~ s/^\<|\>$//g; 1420fccaebf0SDwaipayan Ray $comment = trim($comment); 142120112475SJoe Perches 142220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 142320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 142420112475SJoe Perches $name = "\"$name\""; 142520112475SJoe Perches } 142620112475SJoe Perches 1427dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 142820112475SJoe Perches} 142920112475SJoe Perches 143020112475SJoe Perchessub format_email { 143148ca2d8aSDwaipayan Ray my ($name, $name_comment, $address, $comment) = @_; 143220112475SJoe Perches 143320112475SJoe Perches my $formatted_email; 143420112475SJoe Perches 1435fccaebf0SDwaipayan Ray $name =~ s/^[ \"]+|[ \"]+$//g; 14363705ce5bSJoe Perches $address = trim($address); 1437fccaebf0SDwaipayan Ray $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes 143820112475SJoe Perches 143920112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 144020112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 144120112475SJoe Perches $name = "\"$name\""; 144220112475SJoe Perches } 144320112475SJoe Perches 1444fccaebf0SDwaipayan Ray $name_comment = trim($name_comment); 1445fccaebf0SDwaipayan Ray $name_comment = " $name_comment" if ($name_comment ne ""); 1446fccaebf0SDwaipayan Ray $comment = trim($comment); 1447fccaebf0SDwaipayan Ray $comment = " $comment" if ($comment ne ""); 1448fccaebf0SDwaipayan Ray 144920112475SJoe Perches if ("$name" eq "") { 145020112475SJoe Perches $formatted_email = "$address"; 145120112475SJoe Perches } else { 145248ca2d8aSDwaipayan Ray $formatted_email = "$name$name_comment <$address>"; 145320112475SJoe Perches } 145448ca2d8aSDwaipayan Ray $formatted_email .= "$comment"; 145520112475SJoe Perches return $formatted_email; 145620112475SJoe Perches} 145720112475SJoe Perches 1458dfa05c28SJoe Perchessub reformat_email { 1459dfa05c28SJoe Perches my ($email) = @_; 1460dfa05c28SJoe Perches 1461dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 146248ca2d8aSDwaipayan Ray return format_email($email_name, $name_comment, $email_address, $comment); 1463dfa05c28SJoe Perches} 1464dfa05c28SJoe Perches 1465dfa05c28SJoe Perchessub same_email_addresses { 1466fccaebf0SDwaipayan Ray my ($email1, $email2) = @_; 1467dfa05c28SJoe Perches 1468dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1469dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1470dfa05c28SJoe Perches 147148ca2d8aSDwaipayan Ray return $email1_name eq $email2_name && 147248ca2d8aSDwaipayan Ray $email1_address eq $email2_address && 147348ca2d8aSDwaipayan Ray $name1_comment eq $name2_comment && 147448ca2d8aSDwaipayan Ray $comment1 eq $comment2; 147548ca2d8aSDwaipayan Ray} 1476dfa05c28SJoe Perches 1477d311cd44SJoe Perchessub which { 1478d311cd44SJoe Perches my ($bin) = @_; 1479d311cd44SJoe Perches 1480d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1481d311cd44SJoe Perches if (-e "$path/$bin") { 1482d311cd44SJoe Perches return "$path/$bin"; 1483d311cd44SJoe Perches } 1484d311cd44SJoe Perches } 1485d311cd44SJoe Perches 1486d311cd44SJoe Perches return ""; 1487d311cd44SJoe Perches} 1488d311cd44SJoe Perches 1489000d1cc1SJoe Perchessub which_conf { 1490000d1cc1SJoe Perches my ($conf) = @_; 1491000d1cc1SJoe Perches 1492000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1493000d1cc1SJoe Perches if (-e "$path/$conf") { 1494000d1cc1SJoe Perches return "$path/$conf"; 1495000d1cc1SJoe Perches } 1496000d1cc1SJoe Perches } 1497000d1cc1SJoe Perches 1498000d1cc1SJoe Perches return ""; 1499000d1cc1SJoe Perches} 1500000d1cc1SJoe Perches 15010a920b5bSAndy Whitcroftsub expand_tabs { 15020a920b5bSAndy Whitcroft my ($str) = @_; 15030a920b5bSAndy Whitcroft 15040a920b5bSAndy Whitcroft my $res = ''; 15050a920b5bSAndy Whitcroft my $n = 0; 15060a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 15070a920b5bSAndy Whitcroft if ($c eq "\t") { 15080a920b5bSAndy Whitcroft $res .= ' '; 15090a920b5bSAndy Whitcroft $n++; 1510713a09deSAntonio Borneo for (; ($n % $tabsize) != 0; $n++) { 15110a920b5bSAndy Whitcroft $res .= ' '; 15120a920b5bSAndy Whitcroft } 15130a920b5bSAndy Whitcroft next; 15140a920b5bSAndy Whitcroft } 15150a920b5bSAndy Whitcroft $res .= $c; 15160a920b5bSAndy Whitcroft $n++; 15170a920b5bSAndy Whitcroft } 15180a920b5bSAndy Whitcroft 15190a920b5bSAndy Whitcroft return $res; 15200a920b5bSAndy Whitcroft} 15216c72ffaaSAndy Whitcroftsub copy_spacing { 1522773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 15236c72ffaaSAndy Whitcroft return $res; 15246c72ffaaSAndy Whitcroft} 15250a920b5bSAndy Whitcroft 15264a0df2efSAndy Whitcroftsub line_stats { 15274a0df2efSAndy Whitcroft my ($line) = @_; 15284a0df2efSAndy Whitcroft 15294a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 15304a0df2efSAndy Whitcroft $line =~ s/^.//; 15314a0df2efSAndy Whitcroft $line = expand_tabs($line); 15324a0df2efSAndy Whitcroft 15334a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 15344a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 15354a0df2efSAndy Whitcroft 15364a0df2efSAndy Whitcroft return (length($line), length($white)); 15374a0df2efSAndy Whitcroft} 15384a0df2efSAndy Whitcroft 1539773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1540773647a0SAndy Whitcroft 1541773647a0SAndy Whitcroftsub sanitise_line_reset { 1542773647a0SAndy Whitcroft my ($in_comment) = @_; 1543773647a0SAndy Whitcroft 1544773647a0SAndy Whitcroft if ($in_comment) { 1545773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1546773647a0SAndy Whitcroft } else { 1547773647a0SAndy Whitcroft $sanitise_quote = ''; 1548773647a0SAndy Whitcroft } 1549773647a0SAndy Whitcroft} 155000df344fSAndy Whitcroftsub sanitise_line { 155100df344fSAndy Whitcroft my ($line) = @_; 155200df344fSAndy Whitcroft 155300df344fSAndy Whitcroft my $res = ''; 155400df344fSAndy Whitcroft my $l = ''; 155500df344fSAndy Whitcroft 1556c2fdda0dSAndy Whitcroft my $qlen = 0; 1557773647a0SAndy Whitcroft my $off = 0; 1558773647a0SAndy Whitcroft my $c; 155900df344fSAndy Whitcroft 1560773647a0SAndy Whitcroft # Always copy over the diff marker. 1561773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1562773647a0SAndy Whitcroft 1563773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1564773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1565773647a0SAndy Whitcroft 15668d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1567773647a0SAndy Whitcroft # and end, all to $;. 1568773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1569773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1570773647a0SAndy Whitcroft 1571773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1572773647a0SAndy Whitcroft $off++; 157300df344fSAndy Whitcroft next; 1574773647a0SAndy Whitcroft } 157581bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1576773647a0SAndy Whitcroft $sanitise_quote = ''; 1577773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1578773647a0SAndy Whitcroft $off++; 1579773647a0SAndy Whitcroft next; 1580773647a0SAndy Whitcroft } 1581113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1582113f04a8SDaniel Walker $sanitise_quote = '//'; 1583113f04a8SDaniel Walker 1584113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1585113f04a8SDaniel Walker $off++; 1586113f04a8SDaniel Walker next; 1587113f04a8SDaniel Walker } 1588773647a0SAndy Whitcroft 1589773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1590773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1591773647a0SAndy Whitcroft $c eq "\\") { 1592773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1593773647a0SAndy Whitcroft $off++; 1594773647a0SAndy Whitcroft next; 1595773647a0SAndy Whitcroft } 1596773647a0SAndy Whitcroft # Regular quotes. 1597773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1598773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1599773647a0SAndy Whitcroft $sanitise_quote = $c; 1600773647a0SAndy Whitcroft 1601773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1602773647a0SAndy Whitcroft next; 1603773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1604773647a0SAndy Whitcroft $sanitise_quote = ''; 160500df344fSAndy Whitcroft } 160600df344fSAndy Whitcroft } 1607773647a0SAndy Whitcroft 1608fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1609773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1610773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1611113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1612113f04a8SDaniel Walker substr($res, $off, 1, $;); 1613773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1614773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 161500df344fSAndy Whitcroft } else { 1616773647a0SAndy Whitcroft substr($res, $off, 1, $c); 161700df344fSAndy Whitcroft } 1618c2fdda0dSAndy Whitcroft } 1619c2fdda0dSAndy Whitcroft 1620113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1621113f04a8SDaniel Walker $sanitise_quote = ''; 1622113f04a8SDaniel Walker } 1623113f04a8SDaniel Walker 1624c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1625c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1626c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1627c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1628c2fdda0dSAndy Whitcroft 1629c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1630c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1631c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1632c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1633c2fdda0dSAndy Whitcroft } 1634c2fdda0dSAndy Whitcroft 1635dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1636dadf680dSJoe Perches my $match = $1; 1637dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1638dadf680dSJoe Perches } 1639dadf680dSJoe Perches 164000df344fSAndy Whitcroft return $res; 164100df344fSAndy Whitcroft} 164200df344fSAndy Whitcroft 1643a6962d72SJoe Perchessub get_quoted_string { 1644a6962d72SJoe Perches my ($line, $rawline) = @_; 1645a6962d72SJoe Perches 1646478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 164733acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1648a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1649a6962d72SJoe Perches} 1650a6962d72SJoe Perches 16518905a67cSAndy Whitcroftsub ctx_statement_block { 16528905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 16538905a67cSAndy Whitcroft my $line = $linenr - 1; 16548905a67cSAndy Whitcroft my $blk = ''; 16558905a67cSAndy Whitcroft my $soff = $off; 16568905a67cSAndy Whitcroft my $coff = $off - 1; 1657773647a0SAndy Whitcroft my $coff_set = 0; 16588905a67cSAndy Whitcroft 165913214adfSAndy Whitcroft my $loff = 0; 166013214adfSAndy Whitcroft 16618905a67cSAndy Whitcroft my $type = ''; 16628905a67cSAndy Whitcroft my $level = 0; 1663a2750645SAndy Whitcroft my @stack = (); 1664cf655043SAndy Whitcroft my $p; 16658905a67cSAndy Whitcroft my $c; 16668905a67cSAndy Whitcroft my $len = 0; 166713214adfSAndy Whitcroft 166813214adfSAndy Whitcroft my $remainder; 16698905a67cSAndy Whitcroft while (1) { 1670a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1671a2750645SAndy Whitcroft 1672773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 16738905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 16748905a67cSAndy Whitcroft # context. 16758905a67cSAndy Whitcroft if ($off >= $len) { 16768905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1677dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1678c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 16798905a67cSAndy Whitcroft $remain--; 168013214adfSAndy Whitcroft $loff = $len; 1681c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 16828905a67cSAndy Whitcroft $len = length($blk); 16838905a67cSAndy Whitcroft $line++; 16848905a67cSAndy Whitcroft last; 16858905a67cSAndy Whitcroft } 16868905a67cSAndy Whitcroft # Bail if there is no further context. 16878905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 168813214adfSAndy Whitcroft if ($off >= $len) { 16898905a67cSAndy Whitcroft last; 16908905a67cSAndy Whitcroft } 1691f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1692f74bd194SAndy Whitcroft $level++; 1693f74bd194SAndy Whitcroft $type = '#'; 1694f74bd194SAndy Whitcroft } 16958905a67cSAndy Whitcroft } 1696cf655043SAndy Whitcroft $p = $c; 16978905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 169813214adfSAndy Whitcroft $remainder = substr($blk, $off); 16998905a67cSAndy Whitcroft 1700773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 17014635f4fbSAndy Whitcroft 17024635f4fbSAndy Whitcroft # Handle nested #if/#else. 17034635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 17044635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 17054635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 17064635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 17074635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 17084635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 17094635f4fbSAndy Whitcroft } 17104635f4fbSAndy Whitcroft 17118905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 17128905a67cSAndy Whitcroft # outermost level. 17138905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 17148905a67cSAndy Whitcroft last; 17158905a67cSAndy Whitcroft } 17168905a67cSAndy Whitcroft 171713214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1718773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1719773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1720773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1721773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1722773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1723773647a0SAndy Whitcroft $coff_set = 1; 1724773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1725773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 172613214adfSAndy Whitcroft } 172713214adfSAndy Whitcroft 17288905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 17298905a67cSAndy Whitcroft $level++; 17308905a67cSAndy Whitcroft $type = '('; 17318905a67cSAndy Whitcroft } 17328905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 17338905a67cSAndy Whitcroft $level--; 17348905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 17358905a67cSAndy Whitcroft 17368905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 17378905a67cSAndy Whitcroft $coff = $off; 1738773647a0SAndy Whitcroft $coff_set = 1; 1739773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 17408905a67cSAndy Whitcroft } 17418905a67cSAndy Whitcroft } 17428905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 17438905a67cSAndy Whitcroft $level++; 17448905a67cSAndy Whitcroft $type = '{'; 17458905a67cSAndy Whitcroft } 17468905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 17478905a67cSAndy Whitcroft $level--; 17488905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 17498905a67cSAndy Whitcroft 17508905a67cSAndy Whitcroft if ($level == 0) { 1751b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1752b998e001SPatrick Pannuto $off++; 1753b998e001SPatrick Pannuto } 17548905a67cSAndy Whitcroft last; 17558905a67cSAndy Whitcroft } 17568905a67cSAndy Whitcroft } 1757f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1758f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1759f74bd194SAndy Whitcroft $level--; 1760f74bd194SAndy Whitcroft $type = ''; 1761f74bd194SAndy Whitcroft $off++; 1762f74bd194SAndy Whitcroft last; 1763f74bd194SAndy Whitcroft } 17648905a67cSAndy Whitcroft $off++; 17658905a67cSAndy Whitcroft } 1766a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 176713214adfSAndy Whitcroft if ($off == $len) { 1768a3bb97a7SAndy Whitcroft $loff = $len + 1; 176913214adfSAndy Whitcroft $line++; 177013214adfSAndy Whitcroft $remain--; 177113214adfSAndy Whitcroft } 17728905a67cSAndy Whitcroft 17738905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 17748905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 17758905a67cSAndy Whitcroft 17768905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 17778905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 17788905a67cSAndy Whitcroft 1779773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 178013214adfSAndy Whitcroft 178113214adfSAndy Whitcroft return ($statement, $condition, 178213214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 178313214adfSAndy Whitcroft} 178413214adfSAndy Whitcroft 1785cf655043SAndy Whitcroftsub statement_lines { 1786cf655043SAndy Whitcroft my ($stmt) = @_; 1787cf655043SAndy Whitcroft 1788cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1789cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1790cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1791cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1792cf655043SAndy Whitcroft 1793cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1794cf655043SAndy Whitcroft 1795cf655043SAndy Whitcroft return $#stmt_lines + 2; 1796cf655043SAndy Whitcroft} 1797cf655043SAndy Whitcroft 1798cf655043SAndy Whitcroftsub statement_rawlines { 1799cf655043SAndy Whitcroft my ($stmt) = @_; 1800cf655043SAndy Whitcroft 1801cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1802cf655043SAndy Whitcroft 1803cf655043SAndy Whitcroft return $#stmt_lines + 2; 1804cf655043SAndy Whitcroft} 1805cf655043SAndy Whitcroft 1806cf655043SAndy Whitcroftsub statement_block_size { 1807cf655043SAndy Whitcroft my ($stmt) = @_; 1808cf655043SAndy Whitcroft 1809cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1810cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1811cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1812cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1813cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1814cf655043SAndy Whitcroft 1815cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1816cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1817cf655043SAndy Whitcroft 1818cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1819cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1820cf655043SAndy Whitcroft 1821cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1822cf655043SAndy Whitcroft return $stmt_lines; 1823cf655043SAndy Whitcroft } else { 1824cf655043SAndy Whitcroft return $stmt_statements; 1825cf655043SAndy Whitcroft } 1826cf655043SAndy Whitcroft} 1827cf655043SAndy Whitcroft 182813214adfSAndy Whitcroftsub ctx_statement_full { 182913214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 183013214adfSAndy Whitcroft my ($statement, $condition, $level); 183113214adfSAndy Whitcroft 183213214adfSAndy Whitcroft my (@chunks); 183313214adfSAndy Whitcroft 1834cf655043SAndy Whitcroft # Grab the first conditional/block pair. 183513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 183613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1837773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 183813214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1839cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1840cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1841cf655043SAndy Whitcroft } 1842cf655043SAndy Whitcroft 1843cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1844cf655043SAndy Whitcroft # could continue the statement. 1845cf655043SAndy Whitcroft for (;;) { 184613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 184713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1848cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1849773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1850cf655043SAndy Whitcroft #print "C: push\n"; 1851cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 185213214adfSAndy Whitcroft } 185313214adfSAndy Whitcroft 185413214adfSAndy Whitcroft return ($level, $linenr, @chunks); 18558905a67cSAndy Whitcroft} 18568905a67cSAndy Whitcroft 18574a0df2efSAndy Whitcroftsub ctx_block_get { 1858f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 18594a0df2efSAndy Whitcroft my $line; 18604a0df2efSAndy Whitcroft my $start = $linenr - 1; 18614a0df2efSAndy Whitcroft my $blk = ''; 18624a0df2efSAndy Whitcroft my @o; 18634a0df2efSAndy Whitcroft my @c; 18644a0df2efSAndy Whitcroft my @res = (); 18654a0df2efSAndy Whitcroft 1866f0a594c1SAndy Whitcroft my $level = 0; 18674635f4fbSAndy Whitcroft my @stack = ($level); 186800df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 186900df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 187000df344fSAndy Whitcroft $remain--; 187100df344fSAndy Whitcroft 187200df344fSAndy Whitcroft $blk .= $rawlines[$line]; 18734635f4fbSAndy Whitcroft 18744635f4fbSAndy Whitcroft # Handle nested #if/#else. 187501464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 18764635f4fbSAndy Whitcroft push(@stack, $level); 187701464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 18784635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 187901464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 18804635f4fbSAndy Whitcroft $level = pop(@stack); 18814635f4fbSAndy Whitcroft } 18824635f4fbSAndy Whitcroft 188301464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1884f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1885f0a594c1SAndy Whitcroft if ($off > 0) { 1886f0a594c1SAndy Whitcroft $off--; 1887f0a594c1SAndy Whitcroft next; 1888f0a594c1SAndy Whitcroft } 18894a0df2efSAndy Whitcroft 1890f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1891f0a594c1SAndy Whitcroft $level--; 1892f0a594c1SAndy Whitcroft last if ($level == 0); 1893f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1894f0a594c1SAndy Whitcroft $level++; 1895f0a594c1SAndy Whitcroft } 1896f0a594c1SAndy Whitcroft } 18974a0df2efSAndy Whitcroft 1898f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 189900df344fSAndy Whitcroft push(@res, $rawlines[$line]); 19004a0df2efSAndy Whitcroft } 19014a0df2efSAndy Whitcroft 1902f0a594c1SAndy Whitcroft last if ($level == 0); 19034a0df2efSAndy Whitcroft } 19044a0df2efSAndy Whitcroft 1905f0a594c1SAndy Whitcroft return ($level, @res); 19064a0df2efSAndy Whitcroft} 19074a0df2efSAndy Whitcroftsub ctx_block_outer { 19084a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 19094a0df2efSAndy Whitcroft 1910f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1911f0a594c1SAndy Whitcroft return @r; 19124a0df2efSAndy Whitcroft} 19134a0df2efSAndy Whitcroftsub ctx_block { 19144a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 19154a0df2efSAndy Whitcroft 1916f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1917f0a594c1SAndy Whitcroft return @r; 1918653d4876SAndy Whitcroft} 1919653d4876SAndy Whitcroftsub ctx_statement { 1920f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1921f0a594c1SAndy Whitcroft 1922f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1923f0a594c1SAndy Whitcroft return @r; 1924f0a594c1SAndy Whitcroft} 1925f0a594c1SAndy Whitcroftsub ctx_block_level { 1926653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1927653d4876SAndy Whitcroft 1928f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 19294a0df2efSAndy Whitcroft} 19309c0ca6f9SAndy Whitcroftsub ctx_statement_level { 19319c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 19329c0ca6f9SAndy Whitcroft 19339c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 19349c0ca6f9SAndy Whitcroft} 19354a0df2efSAndy Whitcroft 19364a0df2efSAndy Whitcroftsub ctx_locate_comment { 19374a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 19384a0df2efSAndy Whitcroft 1939a55ee0ccSJoe Perches # If c99 comment on the current line, or the line before or after 1940a55ee0ccSJoe Perches my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); 1941a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1942a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); 1943a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1944a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); 1945a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1946a55ee0ccSJoe Perches 19474a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1948a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 19494a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 19504a0df2efSAndy Whitcroft 19514a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 19524a0df2efSAndy Whitcroft # comment. 19534a0df2efSAndy Whitcroft my $in_comment = 0; 19544a0df2efSAndy Whitcroft $current_comment = ''; 19554a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 195600df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 195700df344fSAndy Whitcroft #warn " $line\n"; 19584a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 19594a0df2efSAndy Whitcroft $in_comment = 1; 19604a0df2efSAndy Whitcroft } 19614a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 19624a0df2efSAndy Whitcroft $in_comment = 1; 19634a0df2efSAndy Whitcroft } 19644a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 19654a0df2efSAndy Whitcroft $current_comment = ''; 19664a0df2efSAndy Whitcroft } 19674a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 19684a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 19694a0df2efSAndy Whitcroft $in_comment = 0; 19704a0df2efSAndy Whitcroft } 19714a0df2efSAndy Whitcroft } 19724a0df2efSAndy Whitcroft 19734a0df2efSAndy Whitcroft chomp($current_comment); 19744a0df2efSAndy Whitcroft return($current_comment); 19754a0df2efSAndy Whitcroft} 19764a0df2efSAndy Whitcroftsub ctx_has_comment { 19774a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 19784a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 19794a0df2efSAndy Whitcroft 198000df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 19814a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 19824a0df2efSAndy Whitcroft 19834a0df2efSAndy Whitcroft return ($cmt ne ''); 19844a0df2efSAndy Whitcroft} 19854a0df2efSAndy Whitcroft 19864d001e4dSAndy Whitcroftsub raw_line { 19874d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 19884d001e4dSAndy Whitcroft 19894d001e4dSAndy Whitcroft my $offset = $linenr - 1; 19904d001e4dSAndy Whitcroft $cnt++; 19914d001e4dSAndy Whitcroft 19924d001e4dSAndy Whitcroft my $line; 19934d001e4dSAndy Whitcroft while ($cnt) { 19944d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 19954d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 19964d001e4dSAndy Whitcroft $cnt--; 19974d001e4dSAndy Whitcroft } 19984d001e4dSAndy Whitcroft 19994d001e4dSAndy Whitcroft return $line; 20004d001e4dSAndy Whitcroft} 20014d001e4dSAndy Whitcroft 20022a9f9d85STobin C. Hardingsub get_stat_real { 20032a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 20042a9f9d85STobin C. Harding 20052a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 20062a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 20072a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 20082a9f9d85STobin C. Harding } 20092a9f9d85STobin C. Harding 20102a9f9d85STobin C. Harding return $stat_real; 20112a9f9d85STobin C. Harding} 20122a9f9d85STobin C. Harding 2013e3d95a2aSTobin C. Hardingsub get_stat_here { 2014e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 2015e3d95a2aSTobin C. Harding 2016e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 2017e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 2018e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 2019e3d95a2aSTobin C. Harding } 2020e3d95a2aSTobin C. Harding 2021e3d95a2aSTobin C. Harding return $herectx; 2022e3d95a2aSTobin C. Harding} 2023e3d95a2aSTobin C. Harding 20240a920b5bSAndy Whitcroftsub cat_vet { 20250a920b5bSAndy Whitcroft my ($vet) = @_; 20269c0ca6f9SAndy Whitcroft my ($res, $coded); 20270a920b5bSAndy Whitcroft 20289c0ca6f9SAndy Whitcroft $res = ''; 20296c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 20306c72ffaaSAndy Whitcroft $res .= $1; 20316c72ffaaSAndy Whitcroft if ($2 ne '') { 20329c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 20336c72ffaaSAndy Whitcroft $res .= $coded; 20346c72ffaaSAndy Whitcroft } 20359c0ca6f9SAndy Whitcroft } 20369c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 20370a920b5bSAndy Whitcroft 20389c0ca6f9SAndy Whitcroft return $res; 20390a920b5bSAndy Whitcroft} 20400a920b5bSAndy Whitcroft 2041c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 2042cf655043SAndy Whitcroftmy $av_pending; 2043c2fdda0dSAndy Whitcroftmy @av_paren_type; 20441f65f947SAndy Whitcroftmy $av_pend_colon; 2045c2fdda0dSAndy Whitcroft 2046c2fdda0dSAndy Whitcroftsub annotate_reset { 2047c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 2048cf655043SAndy Whitcroft $av_pending = '_'; 2049cf655043SAndy Whitcroft @av_paren_type = ('E'); 20501f65f947SAndy Whitcroft $av_pend_colon = 'O'; 2051c2fdda0dSAndy Whitcroft} 2052c2fdda0dSAndy Whitcroft 20536c72ffaaSAndy Whitcroftsub annotate_values { 20546c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 20556c72ffaaSAndy Whitcroft 20566c72ffaaSAndy Whitcroft my $res; 20571f65f947SAndy Whitcroft my $var = '_' x length($stream); 20586c72ffaaSAndy Whitcroft my $cur = $stream; 20596c72ffaaSAndy Whitcroft 2060c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 20616c72ffaaSAndy Whitcroft 20626c72ffaaSAndy Whitcroft while (length($cur)) { 2063773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 2064cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 2065171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 20666c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 2067c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 2068c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 2069cf655043SAndy Whitcroft $type = pop(@av_paren_type); 2070c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 20716c72ffaaSAndy Whitcroft } 20726c72ffaaSAndy Whitcroft 2073c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 20749446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 20759446ef56SAndy Whitcroft push(@av_paren_type, $type); 2076addcdceaSAndy Whitcroft $type = 'c'; 20779446ef56SAndy Whitcroft 2078e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 2079c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 20806c72ffaaSAndy Whitcroft $type = 'T'; 20816c72ffaaSAndy Whitcroft 2082389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 2083389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 2084389a2fe5SAndy Whitcroft $type = 'T'; 2085389a2fe5SAndy Whitcroft 2086c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 2087171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 2088c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 2089171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 2090171ae1a4SAndy Whitcroft if ($2 ne '') { 2091cf655043SAndy Whitcroft $av_pending = 'N'; 2092171ae1a4SAndy Whitcroft } 2093171ae1a4SAndy Whitcroft $type = 'E'; 2094171ae1a4SAndy Whitcroft 2095c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 2096171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 2097171ae1a4SAndy Whitcroft $av_preprocessor = 1; 2098171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 20996c72ffaaSAndy Whitcroft 2100c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 2101cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 2102c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 2103cf655043SAndy Whitcroft 2104cf655043SAndy Whitcroft push(@av_paren_type, $type); 2105cf655043SAndy Whitcroft push(@av_paren_type, $type); 2106171ae1a4SAndy Whitcroft $type = 'E'; 2107cf655043SAndy Whitcroft 2108c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 2109cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 2110cf655043SAndy Whitcroft $av_preprocessor = 1; 2111cf655043SAndy Whitcroft 2112cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 2113cf655043SAndy Whitcroft 2114171ae1a4SAndy Whitcroft $type = 'E'; 2115cf655043SAndy Whitcroft 2116c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 2117cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 2118cf655043SAndy Whitcroft 2119cf655043SAndy Whitcroft $av_preprocessor = 1; 2120cf655043SAndy Whitcroft 2121cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 2122cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 2123cf655043SAndy Whitcroft pop(@av_paren_type); 2124cf655043SAndy Whitcroft push(@av_paren_type, $type); 2125171ae1a4SAndy Whitcroft $type = 'E'; 21266c72ffaaSAndy Whitcroft 21276c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 2128c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 21296c72ffaaSAndy Whitcroft 2130171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 2131171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 2132171ae1a4SAndy Whitcroft $av_pending = $type; 2133171ae1a4SAndy Whitcroft $type = 'N'; 2134171ae1a4SAndy Whitcroft 21356c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 2136c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 21376c72ffaaSAndy Whitcroft if (defined $2) { 2138cf655043SAndy Whitcroft $av_pending = 'V'; 21396c72ffaaSAndy Whitcroft } 21406c72ffaaSAndy Whitcroft $type = 'N'; 21416c72ffaaSAndy Whitcroft 214214b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 2143c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 214414b111c1SAndy Whitcroft $av_pending = 'E'; 21456c72ffaaSAndy Whitcroft $type = 'N'; 21466c72ffaaSAndy Whitcroft 21471f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 21481f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 21491f65f947SAndy Whitcroft $av_pend_colon = 'C'; 21501f65f947SAndy Whitcroft $type = 'N'; 21511f65f947SAndy Whitcroft 215214b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 2153c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 21546c72ffaaSAndy Whitcroft $type = 'N'; 21556c72ffaaSAndy Whitcroft 21566c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 2157c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 2158cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 2159cf655043SAndy Whitcroft $av_pending = '_'; 21606c72ffaaSAndy Whitcroft $type = 'N'; 21616c72ffaaSAndy Whitcroft 21626c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 2163cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 2164cf655043SAndy Whitcroft if ($new_type ne '_') { 2165cf655043SAndy Whitcroft $type = $new_type; 2166c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 2167c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 21686c72ffaaSAndy Whitcroft } else { 2169c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 21706c72ffaaSAndy Whitcroft } 21716c72ffaaSAndy Whitcroft 2172c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 2173c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 2174c8cb2ca3SAndy Whitcroft $type = 'V'; 2175cf655043SAndy Whitcroft $av_pending = 'V'; 21766c72ffaaSAndy Whitcroft 21778e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 21788e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 21791f65f947SAndy Whitcroft $av_pend_colon = 'B'; 21808e761b04SAndy Whitcroft } elsif ($type eq 'E') { 21818e761b04SAndy Whitcroft $av_pend_colon = 'L'; 21821f65f947SAndy Whitcroft } 21831f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 21841f65f947SAndy Whitcroft $type = 'V'; 21851f65f947SAndy Whitcroft 21866c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 2187c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 21886c72ffaaSAndy Whitcroft $type = 'V'; 21896c72ffaaSAndy Whitcroft 21906c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 2191c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 21926c72ffaaSAndy Whitcroft $type = 'N'; 21936c72ffaaSAndy Whitcroft 2194cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 2195c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 219613214adfSAndy Whitcroft $type = 'E'; 21971f65f947SAndy Whitcroft $av_pend_colon = 'O'; 219813214adfSAndy Whitcroft 21998e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 22008e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 22018e761b04SAndy Whitcroft $type = 'C'; 22028e761b04SAndy Whitcroft 22031f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 22041f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 22051f65f947SAndy Whitcroft $type = 'N'; 22061f65f947SAndy Whitcroft 22071f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 22081f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 22091f65f947SAndy Whitcroft 22101f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 22111f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 22121f65f947SAndy Whitcroft $type = 'E'; 22131f65f947SAndy Whitcroft } else { 22141f65f947SAndy Whitcroft $type = 'N'; 22151f65f947SAndy Whitcroft } 22161f65f947SAndy Whitcroft $av_pend_colon = 'O'; 22171f65f947SAndy Whitcroft 22188e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 221913214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 22206c72ffaaSAndy Whitcroft $type = 'N'; 22216c72ffaaSAndy Whitcroft 22220d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 222374048ed8SAndy Whitcroft my $variant; 222474048ed8SAndy Whitcroft 222574048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 222674048ed8SAndy Whitcroft if ($type eq 'V') { 222774048ed8SAndy Whitcroft $variant = 'B'; 222874048ed8SAndy Whitcroft } else { 222974048ed8SAndy Whitcroft $variant = 'U'; 223074048ed8SAndy Whitcroft } 223174048ed8SAndy Whitcroft 223274048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 223374048ed8SAndy Whitcroft $type = 'N'; 223474048ed8SAndy Whitcroft 22356c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 2236c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 22376c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 22386c72ffaaSAndy Whitcroft $type = 'N'; 22396c72ffaaSAndy Whitcroft } 22406c72ffaaSAndy Whitcroft 22416c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 2242c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 22436c72ffaaSAndy Whitcroft } 22446c72ffaaSAndy Whitcroft if (defined $1) { 22456c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 22466c72ffaaSAndy Whitcroft $res .= $type x length($1); 22476c72ffaaSAndy Whitcroft } 22486c72ffaaSAndy Whitcroft } 22496c72ffaaSAndy Whitcroft 22501f65f947SAndy Whitcroft return ($res, $var); 22516c72ffaaSAndy Whitcroft} 22526c72ffaaSAndy Whitcroft 22538905a67cSAndy Whitcroftsub possible { 225413214adfSAndy Whitcroft my ($possible, $line) = @_; 22559a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 22560776e594SAndy Whitcroft ^(?: 22570776e594SAndy Whitcroft $Modifier| 22580776e594SAndy Whitcroft $Storage| 22590776e594SAndy Whitcroft $Type| 22609a974fdbSAndy Whitcroft DEFINE_\S+ 22619a974fdbSAndy Whitcroft )$| 22629a974fdbSAndy Whitcroft ^(?: 22630776e594SAndy Whitcroft goto| 22640776e594SAndy Whitcroft return| 22650776e594SAndy Whitcroft case| 22660776e594SAndy Whitcroft else| 22670776e594SAndy Whitcroft asm|__asm__| 226889a88353SAndy Whitcroft do| 226989a88353SAndy Whitcroft \#| 227089a88353SAndy Whitcroft \#\#| 22719a974fdbSAndy Whitcroft )(?:\s|$)| 22720776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 22739a974fdbSAndy Whitcroft )}x; 22749a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 22759a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2276c45dcabdSAndy Whitcroft # Check for modifiers. 2277c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2278c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2279c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2280c45dcabdSAndy Whitcroft 2281c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2282c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2283d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 22849a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2285d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2286485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2287d2506586SAndy Whitcroft } 22889a974fdbSAndy Whitcroft } 2289c45dcabdSAndy Whitcroft 2290c45dcabdSAndy Whitcroft } else { 229113214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2292485ff23eSAlex Dowad push(@typeListFile, $possible); 2293c45dcabdSAndy Whitcroft } 22948905a67cSAndy Whitcroft build_types(); 22950776e594SAndy Whitcroft } else { 22960776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 22978905a67cSAndy Whitcroft } 22988905a67cSAndy Whitcroft} 22998905a67cSAndy Whitcroft 23006c72ffaaSAndy Whitcroftmy $prefix = ''; 23016c72ffaaSAndy Whitcroft 2302000d1cc1SJoe Perchessub show_type { 2303cbec18afSJoe Perches my ($type) = @_; 230491bfe484SJoe Perches 2305522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2306522b837cSAlexey Dobriyan 2307cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2308cbec18afSJoe Perches 2309cbec18afSJoe Perches return !defined $ignore_type{$type}; 2310000d1cc1SJoe Perches} 2311000d1cc1SJoe Perches 2312f0a594c1SAndy Whitcroftsub report { 2313cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2314cbec18afSJoe Perches 2315cbec18afSJoe Perches if (!show_type($type) || 2316cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2317773647a0SAndy Whitcroft return 0; 2318773647a0SAndy Whitcroft } 231957230297SJoe Perches my $output = ''; 2320737c0767SJohn Brooks if ($color) { 232157230297SJoe Perches if ($level eq 'ERROR') { 232257230297SJoe Perches $output .= RED; 232357230297SJoe Perches } elsif ($level eq 'WARNING') { 232457230297SJoe Perches $output .= YELLOW; 2325000d1cc1SJoe Perches } else { 232657230297SJoe Perches $output .= GREEN; 2327000d1cc1SJoe Perches } 232857230297SJoe Perches } 232957230297SJoe Perches $output .= $prefix . $level . ':'; 233057230297SJoe Perches if ($show_types) { 2331737c0767SJohn Brooks $output .= BLUE if ($color); 233257230297SJoe Perches $output .= "$type:"; 233357230297SJoe Perches } 2334737c0767SJohn Brooks $output .= RESET if ($color); 233557230297SJoe Perches $output .= ' ' . $msg . "\n"; 233634d8815fSJoe Perches 233734d8815fSJoe Perches if ($showfile) { 233834d8815fSJoe Perches my @lines = split("\n", $output, -1); 233934d8815fSJoe Perches splice(@lines, 1, 1); 234034d8815fSJoe Perches $output = join("\n", @lines); 234134d8815fSJoe Perches } 234252178ce0SDwaipayan Ray 234352178ce0SDwaipayan Ray if ($terse) { 234452178ce0SDwaipayan Ray $output = (split('\n', $output))[0] . "\n"; 234552178ce0SDwaipayan Ray } 234652178ce0SDwaipayan Ray 234752178ce0SDwaipayan Ray if ($verbose && exists($verbose_messages{$type}) && 234852178ce0SDwaipayan Ray !exists($verbose_emitted{$type})) { 234952178ce0SDwaipayan Ray $output .= $verbose_messages{$type} . "\n\n"; 235052178ce0SDwaipayan Ray $verbose_emitted{$type} = 1; 235152178ce0SDwaipayan Ray } 23528905a67cSAndy Whitcroft 235357230297SJoe Perches push(our @report, $output); 2354773647a0SAndy Whitcroft 2355773647a0SAndy Whitcroft return 1; 2356f0a594c1SAndy Whitcroft} 2357cbec18afSJoe Perches 2358f0a594c1SAndy Whitcroftsub report_dump { 235913214adfSAndy Whitcroft our @report; 2360f0a594c1SAndy Whitcroft} 2361000d1cc1SJoe Perches 2362d752fcc8SJoe Perchessub fixup_current_range { 2363d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2364d752fcc8SJoe Perches 2365d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2366d752fcc8SJoe Perches my $o = $1; 2367d752fcc8SJoe Perches my $l = $2; 2368d752fcc8SJoe Perches my $no = $o + $offset; 2369d752fcc8SJoe Perches my $nl = $l + $length; 2370d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2371d752fcc8SJoe Perches } 2372d752fcc8SJoe Perches} 2373d752fcc8SJoe Perches 2374d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2375d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2376d752fcc8SJoe Perches 2377d752fcc8SJoe Perches my $range_last_linenr = 0; 2378d752fcc8SJoe Perches my $delta_offset = 0; 2379d752fcc8SJoe Perches 2380d752fcc8SJoe Perches my $old_linenr = 0; 2381d752fcc8SJoe Perches my $new_linenr = 0; 2382d752fcc8SJoe Perches 2383d752fcc8SJoe Perches my $next_insert = 0; 2384d752fcc8SJoe Perches my $next_delete = 0; 2385d752fcc8SJoe Perches 2386d752fcc8SJoe Perches my @lines = (); 2387d752fcc8SJoe Perches 2388d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2389d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2390d752fcc8SJoe Perches 2391d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2392d752fcc8SJoe Perches my $save_line = 1; 2393d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2394323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2395d752fcc8SJoe Perches $delta_offset = 0; 2396d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2397d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2398d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2399d752fcc8SJoe Perches } 2400d752fcc8SJoe Perches 2401d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2402d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2403d752fcc8SJoe Perches $save_line = 0; 2404d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2405d752fcc8SJoe Perches } 2406d752fcc8SJoe Perches 2407d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2408d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2409d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2410d752fcc8SJoe Perches $new_linenr++; 2411d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2412d752fcc8SJoe Perches } 2413d752fcc8SJoe Perches 2414d752fcc8SJoe Perches if ($save_line) { 2415d752fcc8SJoe Perches push(@lines, $line); 2416d752fcc8SJoe Perches $new_linenr++; 2417d752fcc8SJoe Perches } 2418d752fcc8SJoe Perches 2419d752fcc8SJoe Perches $old_linenr++; 2420d752fcc8SJoe Perches } 2421d752fcc8SJoe Perches 2422d752fcc8SJoe Perches return @lines; 2423d752fcc8SJoe Perches} 2424d752fcc8SJoe Perches 2425f2d7e4d4SJoe Perchessub fix_insert_line { 2426f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2427f2d7e4d4SJoe Perches 2428f2d7e4d4SJoe Perches my $inserted = { 2429f2d7e4d4SJoe Perches LINENR => $linenr, 2430f2d7e4d4SJoe Perches LINE => $line, 2431f2d7e4d4SJoe Perches }; 2432f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2433f2d7e4d4SJoe Perches} 2434f2d7e4d4SJoe Perches 2435f2d7e4d4SJoe Perchessub fix_delete_line { 2436f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2437f2d7e4d4SJoe Perches 2438f2d7e4d4SJoe Perches my $deleted = { 2439f2d7e4d4SJoe Perches LINENR => $linenr, 2440f2d7e4d4SJoe Perches LINE => $line, 2441f2d7e4d4SJoe Perches }; 2442f2d7e4d4SJoe Perches 2443f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2444f2d7e4d4SJoe Perches} 2445f2d7e4d4SJoe Perches 2446de7d4f0eSAndy Whitcroftsub ERROR { 2447cbec18afSJoe Perches my ($type, $msg) = @_; 2448cbec18afSJoe Perches 2449cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2450de7d4f0eSAndy Whitcroft our $clean = 0; 24516c72ffaaSAndy Whitcroft our $cnt_error++; 24523705ce5bSJoe Perches return 1; 2453de7d4f0eSAndy Whitcroft } 24543705ce5bSJoe Perches return 0; 2455773647a0SAndy Whitcroft} 2456de7d4f0eSAndy Whitcroftsub WARN { 2457cbec18afSJoe Perches my ($type, $msg) = @_; 2458cbec18afSJoe Perches 2459cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2460de7d4f0eSAndy Whitcroft our $clean = 0; 24616c72ffaaSAndy Whitcroft our $cnt_warn++; 24623705ce5bSJoe Perches return 1; 2463de7d4f0eSAndy Whitcroft } 24643705ce5bSJoe Perches return 0; 2465773647a0SAndy Whitcroft} 2466de7d4f0eSAndy Whitcroftsub CHK { 2467cbec18afSJoe Perches my ($type, $msg) = @_; 2468cbec18afSJoe Perches 2469cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2470de7d4f0eSAndy Whitcroft our $clean = 0; 24716c72ffaaSAndy Whitcroft our $cnt_chk++; 24723705ce5bSJoe Perches return 1; 24736c72ffaaSAndy Whitcroft } 24743705ce5bSJoe Perches return 0; 2475de7d4f0eSAndy Whitcroft} 2476de7d4f0eSAndy Whitcroft 24776ecd9674SAndy Whitcroftsub check_absolute_file { 24786ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 24796ecd9674SAndy Whitcroft my $file = $absolute; 24806ecd9674SAndy Whitcroft 24816ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 24826ecd9674SAndy Whitcroft 24836ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 24846ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 24856ecd9674SAndy Whitcroft if (-f "$root/$file") { 24866ecd9674SAndy Whitcroft ##print "file<$file>\n"; 24876ecd9674SAndy Whitcroft last; 24886ecd9674SAndy Whitcroft } 24896ecd9674SAndy Whitcroft } 24906ecd9674SAndy Whitcroft if (! -f _) { 24916ecd9674SAndy Whitcroft return 0; 24926ecd9674SAndy Whitcroft } 24936ecd9674SAndy Whitcroft 24946ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 24956ecd9674SAndy Whitcroft my $prefix = $absolute; 24966ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 24976ecd9674SAndy Whitcroft 24986ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 24996ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2500000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2501000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 25026ecd9674SAndy Whitcroft } 25036ecd9674SAndy Whitcroft} 25046ecd9674SAndy Whitcroft 25053705ce5bSJoe Perchessub trim { 25063705ce5bSJoe Perches my ($string) = @_; 25073705ce5bSJoe Perches 2508b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2509b34c648bSJoe Perches 2510b34c648bSJoe Perches return $string; 2511b34c648bSJoe Perches} 2512b34c648bSJoe Perches 2513b34c648bSJoe Perchessub ltrim { 2514b34c648bSJoe Perches my ($string) = @_; 2515b34c648bSJoe Perches 2516b34c648bSJoe Perches $string =~ s/^\s+//; 2517b34c648bSJoe Perches 2518b34c648bSJoe Perches return $string; 2519b34c648bSJoe Perches} 2520b34c648bSJoe Perches 2521b34c648bSJoe Perchessub rtrim { 2522b34c648bSJoe Perches my ($string) = @_; 2523b34c648bSJoe Perches 2524b34c648bSJoe Perches $string =~ s/\s+$//; 25253705ce5bSJoe Perches 25263705ce5bSJoe Perches return $string; 25273705ce5bSJoe Perches} 25283705ce5bSJoe Perches 252952ea8506SJoe Perchessub string_find_replace { 253052ea8506SJoe Perches my ($string, $find, $replace) = @_; 253152ea8506SJoe Perches 253252ea8506SJoe Perches $string =~ s/$find/$replace/g; 253352ea8506SJoe Perches 253452ea8506SJoe Perches return $string; 253552ea8506SJoe Perches} 253652ea8506SJoe Perches 25373705ce5bSJoe Perchessub tabify { 25383705ce5bSJoe Perches my ($leading) = @_; 25393705ce5bSJoe Perches 2540713a09deSAntonio Borneo my $source_indent = $tabsize; 25413705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 25423705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 25433705ce5bSJoe Perches 25443705ce5bSJoe Perches #convert leading spaces to tabs 25453705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 25463705ce5bSJoe Perches #Remove spaces before a tab 25473705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 25483705ce5bSJoe Perches 25493705ce5bSJoe Perches return "$leading"; 25503705ce5bSJoe Perches} 25513705ce5bSJoe Perches 2552d1fe9c09SJoe Perchessub pos_last_openparen { 2553d1fe9c09SJoe Perches my ($line) = @_; 2554d1fe9c09SJoe Perches 2555d1fe9c09SJoe Perches my $pos = 0; 2556d1fe9c09SJoe Perches 2557d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2558d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2559d1fe9c09SJoe Perches 2560d1fe9c09SJoe Perches my $last_openparen = 0; 2561d1fe9c09SJoe Perches 2562d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2563d1fe9c09SJoe Perches return -1; 2564d1fe9c09SJoe Perches } 2565d1fe9c09SJoe Perches 2566d1fe9c09SJoe Perches my $len = length($line); 2567d1fe9c09SJoe Perches 2568d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2569d1fe9c09SJoe Perches my $string = substr($line, $pos); 2570d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2571d1fe9c09SJoe Perches $pos += length($1) - 1; 2572d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2573d1fe9c09SJoe Perches $last_openparen = $pos; 2574d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2575d1fe9c09SJoe Perches last; 2576d1fe9c09SJoe Perches } 2577d1fe9c09SJoe Perches } 2578d1fe9c09SJoe Perches 257991cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2580d1fe9c09SJoe Perches} 2581d1fe9c09SJoe Perches 2582f36d3eb8SJoe Perchessub get_raw_comment { 2583f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2584f36d3eb8SJoe Perches my $comment = ''; 2585f36d3eb8SJoe Perches 2586f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2587f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2588f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2589f36d3eb8SJoe Perches } 2590f36d3eb8SJoe Perches } 2591f36d3eb8SJoe Perches 2592f36d3eb8SJoe Perches return $comment; 2593f36d3eb8SJoe Perches} 2594f36d3eb8SJoe Perches 25955b8f82e1SSong Liusub exclude_global_initialisers { 25965b8f82e1SSong Liu my ($realfile) = @_; 25975b8f82e1SSong Liu 25985b8f82e1SSong Liu # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c). 25995b8f82e1SSong Liu return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ || 26005b8f82e1SSong Liu $realfile =~ m@^samples/bpf/.*_kern\.c$@ || 26015b8f82e1SSong Liu $realfile =~ m@/bpf/.*\.bpf\.c$@; 26025b8f82e1SSong Liu} 26035b8f82e1SSong Liu 26040a920b5bSAndy Whitcroftsub process { 26050a920b5bSAndy Whitcroft my $filename = shift; 26060a920b5bSAndy Whitcroft 26070a920b5bSAndy Whitcroft my $linenr=0; 26080a920b5bSAndy Whitcroft my $prevline=""; 2609c2fdda0dSAndy Whitcroft my $prevrawline=""; 26100a920b5bSAndy Whitcroft my $stashline=""; 2611c2fdda0dSAndy Whitcroft my $stashrawline=""; 26120a920b5bSAndy Whitcroft 26134a0df2efSAndy Whitcroft my $length; 26140a920b5bSAndy Whitcroft my $indent; 26150a920b5bSAndy Whitcroft my $previndent=0; 26160a920b5bSAndy Whitcroft my $stashindent=0; 26170a920b5bSAndy Whitcroft 2618de7d4f0eSAndy Whitcroft our $clean = 1; 26190a920b5bSAndy Whitcroft my $signoff = 0; 2620cd261496SGeert Uytterhoeven my $author = ''; 2621cd261496SGeert Uytterhoeven my $authorsignoff = 0; 262248ca2d8aSDwaipayan Ray my $author_sob = ''; 26230a920b5bSAndy Whitcroft my $is_patch = 0; 2624133712a2SRob Herring my $is_binding_patch = -1; 262529ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 262615662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 262744d303ebSJoe Perches my $has_patch_separator = 0; #Found a --- line 2628ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2629490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2630bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 26312a076f40SJoe Perches my $commit_log_long_line = 0; 2632e518e9a5SJoe Perches my $commit_log_has_diff = 0; 263313f1937eSJoe Perches my $reported_maintainer_file = 0; 2634fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2635fa64205dSPasi Savanainen 26364ce9f970SJoe Perches my $last_git_commit_id_linenr = -1; 26374ce9f970SJoe Perches 2638365dd4eaSJoe Perches my $last_blank_line = 0; 26395e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2640365dd4eaSJoe Perches 264113214adfSAndy Whitcroft our @report = (); 26426c72ffaaSAndy Whitcroft our $cnt_lines = 0; 26436c72ffaaSAndy Whitcroft our $cnt_error = 0; 26446c72ffaaSAndy Whitcroft our $cnt_warn = 0; 26456c72ffaaSAndy Whitcroft our $cnt_chk = 0; 26466c72ffaaSAndy Whitcroft 26470a920b5bSAndy Whitcroft # Trace the real file/line as we go. 26480a920b5bSAndy Whitcroft my $realfile = ''; 26490a920b5bSAndy Whitcroft my $realline = 0; 26500a920b5bSAndy Whitcroft my $realcnt = 0; 26510a920b5bSAndy Whitcroft my $here = ''; 265277cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 26530a920b5bSAndy Whitcroft my $in_comment = 0; 2654c2fdda0dSAndy Whitcroft my $comment_edge = 0; 26550a920b5bSAndy Whitcroft my $first_line = 0; 26561e855726SWolfram Sang my $p1_prefix = ''; 26570a920b5bSAndy Whitcroft 265813214adfSAndy Whitcroft my $prev_values = 'E'; 265913214adfSAndy Whitcroft 266013214adfSAndy Whitcroft # suppression flags 2661773647a0SAndy Whitcroft my %suppress_ifbraces; 2662170d3a22SAndy Whitcroft my %suppress_whiletrailers; 26632b474a1aSAndy Whitcroft my %suppress_export; 26643e469cdcSAndy Whitcroft my $suppress_statement = 0; 2665653d4876SAndy Whitcroft 26667e51f197SJoe Perches my %signatures = (); 2667323c1260SJoe Perches 2668c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2669de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2670c2fdda0dSAndy Whitcroft # 2671de7d4f0eSAndy Whitcroft my @setup_docs = (); 2672de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2673773647a0SAndy Whitcroft 2674d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2675d8b07710SJoe Perches 26769f3a8992SRob Herring my $checklicenseline = 1; 26779f3a8992SRob Herring 2678773647a0SAndy Whitcroft sanitise_line_reset(); 2679c2fdda0dSAndy Whitcroft my $line; 2680c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2681773647a0SAndy Whitcroft $linenr++; 2682773647a0SAndy Whitcroft $line = $rawline; 2683c2fdda0dSAndy Whitcroft 26843705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 26853705ce5bSJoe Perches 2686773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2687de7d4f0eSAndy Whitcroft $setup_docs = 0; 26882581ac7cSTim Froidcoeur if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { 2689de7d4f0eSAndy Whitcroft $setup_docs = 1; 2690de7d4f0eSAndy Whitcroft } 2691773647a0SAndy Whitcroft #next; 2692de7d4f0eSAndy Whitcroft } 269374fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2694773647a0SAndy Whitcroft $realline=$1-1; 2695773647a0SAndy Whitcroft if (defined $2) { 2696773647a0SAndy Whitcroft $realcnt=$3+1; 2697773647a0SAndy Whitcroft } else { 2698773647a0SAndy Whitcroft $realcnt=1+1; 2699773647a0SAndy Whitcroft } 2700c45dcabdSAndy Whitcroft $in_comment = 0; 2701773647a0SAndy Whitcroft 2702773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2703773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2704773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2705773647a0SAndy Whitcroft # at context start. 2706773647a0SAndy Whitcroft my $edge; 270701fa9147SAndy Whitcroft my $cnt = $realcnt; 270801fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 270901fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 271001fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 271101fa9147SAndy Whitcroft $cnt--; 271201fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2713721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2714fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2715fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2716fae17daeSAndy Whitcroft ($edge) = $1; 2717fae17daeSAndy Whitcroft last; 2718fae17daeSAndy Whitcroft } 2719773647a0SAndy Whitcroft } 2720773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2721773647a0SAndy Whitcroft $in_comment = 1; 2722773647a0SAndy Whitcroft } 2723773647a0SAndy Whitcroft 2724773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2725773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2726773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2727773647a0SAndy Whitcroft if (!defined $edge && 272883242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2729773647a0SAndy Whitcroft { 2730773647a0SAndy Whitcroft $in_comment = 1; 2731773647a0SAndy Whitcroft } 2732773647a0SAndy Whitcroft 2733773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2734773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2735773647a0SAndy Whitcroft 2736171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2737773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2738171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2739773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2740773647a0SAndy Whitcroft } 2741773647a0SAndy Whitcroft push(@lines, $line); 2742773647a0SAndy Whitcroft 2743773647a0SAndy Whitcroft if ($realcnt > 1) { 2744773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2745773647a0SAndy Whitcroft } else { 2746773647a0SAndy Whitcroft $realcnt = 0; 2747773647a0SAndy Whitcroft } 2748773647a0SAndy Whitcroft 2749773647a0SAndy Whitcroft #print "==>$rawline\n"; 2750773647a0SAndy Whitcroft #print "-->$line\n"; 2751de7d4f0eSAndy Whitcroft 2752de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2753de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2754de7d4f0eSAndy Whitcroft } 2755de7d4f0eSAndy Whitcroft } 2756de7d4f0eSAndy Whitcroft 27576c72ffaaSAndy Whitcroft $prefix = ''; 27586c72ffaaSAndy Whitcroft 2759773647a0SAndy Whitcroft $realcnt = 0; 2760773647a0SAndy Whitcroft $linenr = 0; 2761194f66fcSJoe Perches $fixlinenr = -1; 27620a920b5bSAndy Whitcroft foreach my $line (@lines) { 27630a920b5bSAndy Whitcroft $linenr++; 2764194f66fcSJoe Perches $fixlinenr++; 27651b5539b1SJoe Perches my $sline = $line; #copy of $line 27661b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 27670a920b5bSAndy Whitcroft 2768c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2769f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 27706c72ffaaSAndy Whitcroft 277112c253abSJoe Perches# check if it's a mode change, rename or start of a patch 277212c253abSJoe Perches if (!$in_commit_log && 277312c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 277412c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 277512c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 277612c253abSJoe Perches $is_patch = 1; 277712c253abSJoe Perches } 277812c253abSJoe Perches 27790a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2780e518e9a5SJoe Perches if (!$in_commit_log && 278174fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 278274fd4f34SJoe Perches my $context = $4; 27830a920b5bSAndy Whitcroft $is_patch = 1; 27844a0df2efSAndy Whitcroft $first_line = $linenr + 1; 27850a920b5bSAndy Whitcroft $realline=$1-1; 27860a920b5bSAndy Whitcroft if (defined $2) { 27870a920b5bSAndy Whitcroft $realcnt=$3+1; 27880a920b5bSAndy Whitcroft } else { 27890a920b5bSAndy Whitcroft $realcnt=1+1; 27900a920b5bSAndy Whitcroft } 2791c2fdda0dSAndy Whitcroft annotate_reset(); 279213214adfSAndy Whitcroft $prev_values = 'E'; 279313214adfSAndy Whitcroft 2794773647a0SAndy Whitcroft %suppress_ifbraces = (); 2795170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 27962b474a1aSAndy Whitcroft %suppress_export = (); 27973e469cdcSAndy Whitcroft $suppress_statement = 0; 279874fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 279974fd4f34SJoe Perches $context_function = $1; 280074fd4f34SJoe Perches } else { 280174fd4f34SJoe Perches undef $context_function; 280274fd4f34SJoe Perches } 28030a920b5bSAndy Whitcroft next; 28040a920b5bSAndy Whitcroft 28054a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 28064a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 28074a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2808773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 28090a920b5bSAndy Whitcroft $realline++; 2810d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 28110a920b5bSAndy Whitcroft 28124a0df2efSAndy Whitcroft # Measure the line length and indent. 2813c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 28140a920b5bSAndy Whitcroft 28150a920b5bSAndy Whitcroft # Track the previous line. 28160a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 28170a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2818c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2819c2fdda0dSAndy Whitcroft 2820773647a0SAndy Whitcroft #warn "line<$line>\n"; 28216c72ffaaSAndy Whitcroft 2822d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2823d8aaf121SAndy Whitcroft $realcnt--; 28240a920b5bSAndy Whitcroft } 28250a920b5bSAndy Whitcroft 2826cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2827cc77cdcaSAndy Whitcroft 28286c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 28296c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2830773647a0SAndy Whitcroft 28312ac73b4fSJoe Perches my $found_file = 0; 2832773647a0SAndy Whitcroft # extract the filename as it passes 28333bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 28343bf9a009SRabin Vincent $realfile = $1; 28352b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2836270c49a0SJoe Perches $in_commit_log = 0; 28372ac73b4fSJoe Perches $found_file = 1; 28383bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2839773647a0SAndy Whitcroft $realfile = $1; 28402b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2841270c49a0SJoe Perches $in_commit_log = 0; 28421e855726SWolfram Sang 28431e855726SWolfram Sang $p1_prefix = $1; 2844e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2845e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2846000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2847000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 28481e855726SWolfram Sang } 2849773647a0SAndy Whitcroft 2850c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2851000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2852000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2853773647a0SAndy Whitcroft } 28542ac73b4fSJoe Perches $found_file = 1; 28552ac73b4fSJoe Perches } 28562ac73b4fSJoe Perches 285734d8815fSJoe Perches#make up the handle for any error we report on this line 285834d8815fSJoe Perches if ($showfile) { 285934d8815fSJoe Perches $prefix = "$realfile:$realline: " 286034d8815fSJoe Perches } elsif ($emacs) { 28617d3a9f67SJoe Perches if ($file) { 28627d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 28637d3a9f67SJoe Perches } else { 286434d8815fSJoe Perches $prefix = "$filename:$linenr: "; 286534d8815fSJoe Perches } 28667d3a9f67SJoe Perches } 286734d8815fSJoe Perches 28682ac73b4fSJoe Perches if ($found_file) { 286985b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 287085b0ee18SJoe Perches WARN("OBSOLETE", 287185b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 287285b0ee18SJoe Perches } 28737bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 28742ac73b4fSJoe Perches $check = 1; 28752ac73b4fSJoe Perches } else { 28762ac73b4fSJoe Perches $check = $check_orig; 28772ac73b4fSJoe Perches } 28789f3a8992SRob Herring $checklicenseline = 1; 2879133712a2SRob Herring 2880133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2881133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2882133712a2SRob Herring 2883133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2884133712a2SRob Herring 2885133712a2SRob Herring if (($last_binding_patch != -1) && 2886133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2887133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2888858e6845SMauro Carvalho Chehab "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); 2889133712a2SRob Herring } 2890133712a2SRob Herring } 2891133712a2SRob Herring 2892773647a0SAndy Whitcroft next; 2893773647a0SAndy Whitcroft } 2894773647a0SAndy Whitcroft 2895389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 28960a920b5bSAndy Whitcroft 2897c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2898c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2899c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 29000a920b5bSAndy Whitcroft 29016c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 29026c72ffaaSAndy Whitcroft 2903490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2904490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2905490b292cSJoe Perches if ($in_commit_log) { 2906490b292cSJoe Perches if ($line !~ /^\s*$/) { 2907490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2908490b292cSJoe Perches } 2909490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2910490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2911490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2912490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2913490b292cSJoe Perches } 2914490b292cSJoe Perches 2915e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2916e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 291713e45417SMrinal Pandey (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && 291813e45417SMrinal Pandey $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || 2919e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2920e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2921e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2922e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2923e518e9a5SJoe Perches $commit_log_has_diff = 1; 2924e518e9a5SJoe Perches } 2925e518e9a5SJoe Perches 29263bf9a009SRabin Vincent# Check for incorrect file permissions 29273bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 29283bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 292904db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 293004db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2931000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2932000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 29333bf9a009SRabin Vincent } 29343bf9a009SRabin Vincent } 29353bf9a009SRabin Vincent 2936cd261496SGeert Uytterhoeven# Check the patch for a From: 2937cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2938cd261496SGeert Uytterhoeven $author = $1; 2939e7f929f3SDwaipayan Ray my $curline = $linenr; 2940e7f929f3SDwaipayan Ray while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { 2941e7f929f3SDwaipayan Ray $author .= $1; 2942e7f929f3SDwaipayan Ray } 2943cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2944cd261496SGeert Uytterhoeven $author =~ s/"//g; 2945dfa05c28SJoe Perches $author = reformat_email($author); 2946cd261496SGeert Uytterhoeven } 2947cd261496SGeert Uytterhoeven 294820112475SJoe Perches# Check the patch for a signoff: 2949dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 29504a0df2efSAndy Whitcroft $signoff++; 295115662b3eSJoe Perches $in_commit_log = 0; 295248ca2d8aSDwaipayan Ray if ($author ne '' && $authorsignoff != 1) { 2953fccaebf0SDwaipayan Ray if (same_email_addresses($1, $author)) { 2954cd261496SGeert Uytterhoeven $authorsignoff = 1; 295548ca2d8aSDwaipayan Ray } else { 295648ca2d8aSDwaipayan Ray my $ctx = $1; 295748ca2d8aSDwaipayan Ray my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); 295848ca2d8aSDwaipayan Ray my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); 295948ca2d8aSDwaipayan Ray 2960046fc741SMimi Zohar if (lc $email_address eq lc $author_address && $email_name eq $author_name) { 296148ca2d8aSDwaipayan Ray $author_sob = $ctx; 296248ca2d8aSDwaipayan Ray $authorsignoff = 2; 2963046fc741SMimi Zohar } elsif (lc $email_address eq lc $author_address) { 296448ca2d8aSDwaipayan Ray $author_sob = $ctx; 296548ca2d8aSDwaipayan Ray $authorsignoff = 3; 296648ca2d8aSDwaipayan Ray } elsif ($email_name eq $author_name) { 296748ca2d8aSDwaipayan Ray $author_sob = $ctx; 296848ca2d8aSDwaipayan Ray $authorsignoff = 4; 296948ca2d8aSDwaipayan Ray 297048ca2d8aSDwaipayan Ray my $address1 = $email_address; 297148ca2d8aSDwaipayan Ray my $address2 = $author_address; 297248ca2d8aSDwaipayan Ray 297348ca2d8aSDwaipayan Ray if ($address1 =~ /(\S+)\+\S+(\@.*)/) { 297448ca2d8aSDwaipayan Ray $address1 = "$1$2"; 297548ca2d8aSDwaipayan Ray } 297648ca2d8aSDwaipayan Ray if ($address2 =~ /(\S+)\+\S+(\@.*)/) { 297748ca2d8aSDwaipayan Ray $address2 = "$1$2"; 297848ca2d8aSDwaipayan Ray } 297948ca2d8aSDwaipayan Ray if ($address1 eq $address2) { 298048ca2d8aSDwaipayan Ray $authorsignoff = 5; 298148ca2d8aSDwaipayan Ray } 298248ca2d8aSDwaipayan Ray } 2983cd261496SGeert Uytterhoeven } 2984cd261496SGeert Uytterhoeven } 29850a920b5bSAndy Whitcroft } 298620112475SJoe Perches 298744d303ebSJoe Perches# Check for patch separator 298844d303ebSJoe Perches if ($line =~ /^---$/) { 298944d303ebSJoe Perches $has_patch_separator = 1; 299044d303ebSJoe Perches $in_commit_log = 0; 299144d303ebSJoe Perches } 299244d303ebSJoe Perches 2993e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2994e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2995e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2996e0d975b1SJoe Perches $reported_maintainer_file = 1; 2997e0d975b1SJoe Perches } 2998e0d975b1SJoe Perches 299920112475SJoe Perches# Check signature styles 3000270c49a0SJoe Perches if (!$in_header_lines && 3001ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 300220112475SJoe Perches my $space_before = $1; 300320112475SJoe Perches my $sign_off = $2; 300420112475SJoe Perches my $space_after = $3; 300520112475SJoe Perches my $email = $4; 300620112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 300720112475SJoe Perches 3008ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 3009831242abSAditya Srivastava my $suggested_signature = find_standard_signature($sign_off); 3010831242abSAditya Srivastava if ($suggested_signature eq "") { 3011ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 3012ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 3013831242abSAditya Srivastava } else { 3014831242abSAditya Srivastava if (WARN("BAD_SIGN_OFF", 3015831242abSAditya Srivastava "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) && 3016831242abSAditya Srivastava $fix) { 3017831242abSAditya Srivastava $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/; 3018831242abSAditya Srivastava } 3019831242abSAditya Srivastava } 3020ce0338dfSJoe Perches } 302120112475SJoe Perches if (defined $space_before && $space_before ne "") { 30223705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 30233705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 30243705ce5bSJoe Perches $fix) { 3025194f66fcSJoe Perches $fixed[$fixlinenr] = 30263705ce5bSJoe Perches "$ucfirst_sign_off $email"; 30273705ce5bSJoe Perches } 302820112475SJoe Perches } 302920112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 30303705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 30313705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 30323705ce5bSJoe Perches $fix) { 3033194f66fcSJoe Perches $fixed[$fixlinenr] = 30343705ce5bSJoe Perches "$ucfirst_sign_off $email"; 30353705ce5bSJoe Perches } 30363705ce5bSJoe Perches 303720112475SJoe Perches } 303820112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 30393705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 30403705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 30413705ce5bSJoe Perches $fix) { 3042194f66fcSJoe Perches $fixed[$fixlinenr] = 30433705ce5bSJoe Perches "$ucfirst_sign_off $email"; 30443705ce5bSJoe Perches } 304520112475SJoe Perches } 304620112475SJoe Perches 3047dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 304848ca2d8aSDwaipayan Ray my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); 304920112475SJoe Perches if ($suggested_email eq "") { 3050000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 3051000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 305220112475SJoe Perches } else { 305320112475SJoe Perches my $dequoted = $suggested_email; 305420112475SJoe Perches $dequoted =~ s/^"//; 305520112475SJoe Perches $dequoted =~ s/" </ </; 305620112475SJoe Perches # Don't force email to have quotes 305720112475SJoe Perches # Allow just an angle bracketed address 3058fccaebf0SDwaipayan Ray if (!same_email_addresses($email, $suggested_email)) { 3059fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 3060fccaebf0SDwaipayan Ray "email address '$email' might be better as '$suggested_email'\n" . $herecurr) && 3061fccaebf0SDwaipayan Ray $fix) { 3062fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/; 3063fccaebf0SDwaipayan Ray } 3064fccaebf0SDwaipayan Ray } 3065fccaebf0SDwaipayan Ray 3066fccaebf0SDwaipayan Ray # Address part shouldn't have comments 3067fccaebf0SDwaipayan Ray my $stripped_address = $email_address; 3068fccaebf0SDwaipayan Ray $stripped_address =~ s/\([^\(\)]*\)//g; 3069fccaebf0SDwaipayan Ray if ($email_address ne $stripped_address) { 3070fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 3071fccaebf0SDwaipayan Ray "address part of email should not have comments: '$email_address'\n" . $herecurr) && 3072fccaebf0SDwaipayan Ray $fix) { 3073fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/; 3074fccaebf0SDwaipayan Ray } 3075fccaebf0SDwaipayan Ray } 3076fccaebf0SDwaipayan Ray 3077fccaebf0SDwaipayan Ray # Only one name comment should be allowed 3078fccaebf0SDwaipayan Ray my $comment_count = () = $name_comment =~ /\([^\)]+\)/g; 3079fccaebf0SDwaipayan Ray if ($comment_count > 1) { 3080000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 3081fccaebf0SDwaipayan Ray "Use a single name comment in email: '$email'\n" . $herecurr); 3082fccaebf0SDwaipayan Ray } 3083fccaebf0SDwaipayan Ray 3084fccaebf0SDwaipayan Ray 3085fccaebf0SDwaipayan Ray # [email protected] or [email protected] shouldn't 3086e73d2715SDwaipayan Ray # have an email name. In addition comments should strictly 3087fccaebf0SDwaipayan Ray # begin with a # 3088fccaebf0SDwaipayan Ray if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) { 3089fccaebf0SDwaipayan Ray if (($comment ne "" && $comment !~ /^#.+/) || 3090fccaebf0SDwaipayan Ray ($email_name ne "")) { 3091fccaebf0SDwaipayan Ray my $cur_name = $email_name; 3092fccaebf0SDwaipayan Ray my $new_comment = $comment; 3093fccaebf0SDwaipayan Ray $cur_name =~ s/[a-zA-Z\s\-\"]+//g; 3094fccaebf0SDwaipayan Ray 3095fccaebf0SDwaipayan Ray # Remove brackets enclosing comment text 3096fccaebf0SDwaipayan Ray # and # from start of comments to get comment text 3097fccaebf0SDwaipayan Ray $new_comment =~ s/^\((.*)\)$/$1/; 3098fccaebf0SDwaipayan Ray $new_comment =~ s/^\[(.*)\]$/$1/; 3099fccaebf0SDwaipayan Ray $new_comment =~ s/^[\s\#]+|\s+$//g; 3100fccaebf0SDwaipayan Ray 3101fccaebf0SDwaipayan Ray $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment); 3102fccaebf0SDwaipayan Ray $new_comment = " # $new_comment" if ($new_comment ne ""); 3103fccaebf0SDwaipayan Ray my $new_email = "$email_address$new_comment"; 3104fccaebf0SDwaipayan Ray 3105fccaebf0SDwaipayan Ray if (WARN("BAD_STABLE_ADDRESS_STYLE", 3106fccaebf0SDwaipayan Ray "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) && 3107fccaebf0SDwaipayan Ray $fix) { 3108fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 3109fccaebf0SDwaipayan Ray } 3110fccaebf0SDwaipayan Ray } 3111fccaebf0SDwaipayan Ray } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) { 3112fccaebf0SDwaipayan Ray my $new_comment = $comment; 3113fccaebf0SDwaipayan Ray 3114fccaebf0SDwaipayan Ray # Extract comment text from within brackets or 3115fccaebf0SDwaipayan Ray # c89 style /*...*/ comments 3116fccaebf0SDwaipayan Ray $new_comment =~ s/^\[(.*)\]$/$1/; 3117fccaebf0SDwaipayan Ray $new_comment =~ s/^\/\*(.*)\*\/$/$1/; 3118fccaebf0SDwaipayan Ray 3119fccaebf0SDwaipayan Ray $new_comment = trim($new_comment); 3120fccaebf0SDwaipayan Ray $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo 3121fccaebf0SDwaipayan Ray $new_comment = "($new_comment)" if ($new_comment ne ""); 3122fccaebf0SDwaipayan Ray my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment); 3123fccaebf0SDwaipayan Ray 3124fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 3125fccaebf0SDwaipayan Ray "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) && 3126fccaebf0SDwaipayan Ray $fix) { 3127fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 3128fccaebf0SDwaipayan Ray } 312920112475SJoe Perches } 31300a920b5bSAndy Whitcroft } 31317e51f197SJoe Perches 31327e51f197SJoe Perches# Check for duplicate signatures 31337e51f197SJoe Perches my $sig_nospace = $line; 31347e51f197SJoe Perches $sig_nospace =~ s/\s//g; 31357e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 31367e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 31377e51f197SJoe Perches WARN("BAD_SIGN_OFF", 31387e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 31397e51f197SJoe Perches } else { 31407e51f197SJoe Perches $signatures{$sig_nospace} = 1; 31417e51f197SJoe Perches } 31426c5d24eeSSean Christopherson 31436c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 31446c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 31456c5d24eeSSean Christopherson if ($email eq $author) { 31466c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 31476c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 31486c5d24eeSSean Christopherson } 31496c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 31506c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 31516c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 31526c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 31536c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 31546c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 31556c5d24eeSSean Christopherson } elsif ($1 ne $email) { 31566c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 31576c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 31586c5d24eeSSean Christopherson } 31596c5d24eeSSean Christopherson } 31600a920b5bSAndy Whitcroft } 31610a920b5bSAndy Whitcroft 3162bd17e036SNiklas Söderlund# Check Fixes: styles is correct 3163bd17e036SNiklas Söderlund if (!$in_header_lines && 3164bd17e036SNiklas Söderlund $line =~ /^\s*fixes:?\s*(?:commit\s*)?[0-9a-f]{5,}\b/i) { 3165bd17e036SNiklas Söderlund my $orig_commit = ""; 3166bd17e036SNiklas Söderlund my $id = "0123456789ab"; 3167bd17e036SNiklas Söderlund my $title = "commit title"; 3168bd17e036SNiklas Söderlund my $tag_case = 1; 3169bd17e036SNiklas Söderlund my $tag_space = 1; 3170bd17e036SNiklas Söderlund my $id_length = 1; 3171bd17e036SNiklas Söderlund my $id_case = 1; 3172bd17e036SNiklas Söderlund my $title_has_quotes = 0; 3173bd17e036SNiklas Söderlund 3174bd17e036SNiklas Söderlund if ($line =~ /(\s*fixes:?)\s+([0-9a-f]{5,})\s+($balanced_parens)/i) { 3175bd17e036SNiklas Söderlund my $tag = $1; 3176bd17e036SNiklas Söderlund $orig_commit = $2; 3177bd17e036SNiklas Söderlund $title = $3; 3178bd17e036SNiklas Söderlund 3179bd17e036SNiklas Söderlund $tag_case = 0 if $tag eq "Fixes:"; 3180bd17e036SNiklas Söderlund $tag_space = 0 if ($line =~ /^fixes:? [0-9a-f]{5,} ($balanced_parens)/i); 3181bd17e036SNiklas Söderlund 3182bd17e036SNiklas Söderlund $id_length = 0 if ($orig_commit =~ /^[0-9a-f]{12}$/i); 3183bd17e036SNiklas Söderlund $id_case = 0 if ($orig_commit !~ /[A-F]/); 3184bd17e036SNiklas Söderlund 3185bd17e036SNiklas Söderlund # Always strip leading/trailing parens then double quotes if existing 3186bd17e036SNiklas Söderlund $title = substr($title, 1, -1); 3187bd17e036SNiklas Söderlund if ($title =~ /^".*"$/) { 3188bd17e036SNiklas Söderlund $title = substr($title, 1, -1); 3189bd17e036SNiklas Söderlund $title_has_quotes = 1; 3190bd17e036SNiklas Söderlund } 3191bd17e036SNiklas Söderlund } 3192bd17e036SNiklas Söderlund 3193bd17e036SNiklas Söderlund my ($cid, $ctitle) = git_commit_info($orig_commit, $id, 3194bd17e036SNiklas Söderlund $title); 3195bd17e036SNiklas Söderlund 3196bd17e036SNiklas Söderlund if ($ctitle ne $title || $tag_case || $tag_space || 3197bd17e036SNiklas Söderlund $id_length || $id_case || !$title_has_quotes) { 3198bd17e036SNiklas Söderlund if (WARN("BAD_FIXES_TAG", 3199bd17e036SNiklas Söderlund "Please use correct Fixes: style 'Fixes: <12 chars of sha1> (\"<title line>\")' - ie: 'Fixes: $cid (\"$ctitle\")'\n" . $herecurr) && 3200bd17e036SNiklas Söderlund $fix) { 3201bd17e036SNiklas Söderlund $fixed[$fixlinenr] = "Fixes: $cid (\"$ctitle\")"; 3202bd17e036SNiklas Söderlund } 3203bd17e036SNiklas Söderlund } 3204bd17e036SNiklas Söderlund } 3205bd17e036SNiklas Söderlund 3206a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 3207a2fe16b9SJoe Perches if ($in_header_lines && 3208a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 3209a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 3210a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 3211a2fe16b9SJoe Perches } 3212a2fe16b9SJoe Perches 321344d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context 321444d303ebSJoe Perches if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 32157580c5b9SAditya Srivastava if (ERROR("GERRIT_CHANGE_ID", 32167580c5b9SAditya Srivastava "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) && 32177580c5b9SAditya Srivastava $fix) { 32187580c5b9SAditya Srivastava fix_delete_line($fixlinenr, $rawline); 32197580c5b9SAditya Srivastava } 32207ebd05efSChristopher Covington } 32217ebd05efSChristopher Covington 3222369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 3223369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 3224369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 3225369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 3226369c8dd3SJoe Perches # timestamp 3227634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 3228634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 3229634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 3230634cffccSJoe Perches # stack dump address styles 3231369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 3232369c8dd3SJoe Perches } 3233369c8dd3SJoe Perches 32342a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 32352a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 3236bf4daf12SJoe Perches length($line) > 75 && 3237bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 3238bf4daf12SJoe Perches # file delta changes 323936f8b348SJerome Forissier $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\.\-\+]+:/ || 3240bf4daf12SJoe Perches # filename then : 324127b379afSAditya Srivastava $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i || 324227b379afSAditya Srivastava # A Fixes: or Link: line or signature tag line 3243bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 32442a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 32452a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 32462a076f40SJoe Perches $commit_log_long_line = 1; 32472a076f40SJoe Perches } 32482a076f40SJoe Perches 3249bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 3250bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 3251bf4daf12SJoe Perches $line =~ /^\s*$/) { 3252bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 3253bf4daf12SJoe Perches } 3254bf4daf12SJoe Perches 3255084a617aSDwaipayan Ray# Check for lines starting with a # 3256084a617aSDwaipayan Ray if ($in_commit_log && $line =~ /^#/) { 3257084a617aSDwaipayan Ray if (WARN("COMMIT_COMMENT_SYMBOL", 3258084a617aSDwaipayan Ray "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) && 3259084a617aSDwaipayan Ray $fix) { 3260084a617aSDwaipayan Ray $fixed[$fixlinenr] =~ s/^/ /; 3261084a617aSDwaipayan Ray } 3262084a617aSDwaipayan Ray } 3263084a617aSDwaipayan Ray 32640d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 32654ce9f970SJoe Perches# A correctly formed commit description is: 32664ce9f970SJoe Perches# commit <SHA-1 hash length 12+ chars> ("Complete commit subject") 32674ce9f970SJoe Perches# with the commit subject '("' prefix and '")' suffix 32684ce9f970SJoe Perches# This is a fairly compilicated block as it tests for what appears to be 32694ce9f970SJoe Perches# bare SHA-1 hash with minimum length of 5. It also avoids several types of 32704ce9f970SJoe Perches# possible SHA-1 matches. 32714ce9f970SJoe Perches# A commit match can span multiple lines so this block attempts to find a 32724ce9f970SJoe Perches# complete typical commit on a maximum of 3 lines 32734ce9f970SJoe Perches if ($perl_version_ok && 32744ce9f970SJoe Perches $in_commit_log && !$commit_log_possible_stack_dump && 3275a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 3276e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 32774ce9f970SJoe Perches (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 32784ce9f970SJoe Perches ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) || 3279aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 3280369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 3281bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 3282fe043ea1SJoe Perches my $init_char = "c"; 3283fe043ea1SJoe Perches my $orig_commit = ""; 32840d7835fcSJoe Perches my $short = 1; 32850d7835fcSJoe Perches my $long = 0; 32860d7835fcSJoe Perches my $case = 1; 32870d7835fcSJoe Perches my $space = 1; 32880d7835fcSJoe Perches my $id = '0123456789ab'; 32890d7835fcSJoe Perches my $orig_desc = "commit description"; 32900d7835fcSJoe Perches my $description = ""; 32914ce9f970SJoe Perches my $herectx = $herecurr; 32924ce9f970SJoe Perches my $has_parens = 0; 32934ce9f970SJoe Perches my $has_quotes = 0; 32940d7835fcSJoe Perches 32954ce9f970SJoe Perches my $input = $line; 32964ce9f970SJoe Perches if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) { 32974ce9f970SJoe Perches for (my $n = 0; $n < 2; $n++) { 32984ce9f970SJoe Perches if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) { 32994ce9f970SJoe Perches $orig_desc = $1; 33004ce9f970SJoe Perches $has_parens = 1; 33014ce9f970SJoe Perches # Always strip leading/trailing parens then double quotes if existing 33024ce9f970SJoe Perches $orig_desc = substr($orig_desc, 1, -1); 33034ce9f970SJoe Perches if ($orig_desc =~ /^".*"$/) { 33044ce9f970SJoe Perches $orig_desc = substr($orig_desc, 1, -1); 33054ce9f970SJoe Perches $has_quotes = 1; 33064ce9f970SJoe Perches } 33074ce9f970SJoe Perches last; 33084ce9f970SJoe Perches } 33094ce9f970SJoe Perches last if ($#lines < $linenr + $n); 33104ce9f970SJoe Perches $input .= " " . trim($rawlines[$linenr + $n]); 33114ce9f970SJoe Perches $herectx .= "$rawlines[$linenr + $n]\n"; 33124ce9f970SJoe Perches } 33134ce9f970SJoe Perches $herectx = $herecurr if (!$has_parens); 3314fe043ea1SJoe Perches } 3315fe043ea1SJoe Perches 33164ce9f970SJoe Perches if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 33174ce9f970SJoe Perches $init_char = $1; 33184ce9f970SJoe Perches $orig_commit = lc($2); 33194ce9f970SJoe Perches $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i); 33204ce9f970SJoe Perches $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i); 33214ce9f970SJoe Perches $space = 0 if ($input =~ /\bcommit [0-9a-f]/i); 33224ce9f970SJoe Perches $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 33234ce9f970SJoe Perches } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) { 33244ce9f970SJoe Perches $orig_commit = lc($1); 33250d7835fcSJoe Perches } 33260d7835fcSJoe Perches 33270d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 33280d7835fcSJoe Perches $id, $orig_desc); 33290d7835fcSJoe Perches 3330948b133aSHeinrich Schuchardt if (defined($id) && 33314ce9f970SJoe Perches ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) && 33324ce9f970SJoe Perches $last_git_commit_id_linenr != $linenr - 1) { 3333d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 33344ce9f970SJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx); 33350d7835fcSJoe Perches } 33364ce9f970SJoe Perches #don't report the next line if this line ends in commit and the sha1 hash is the next line 33374ce9f970SJoe Perches $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i); 3338d311cd44SJoe Perches } 3339d311cd44SJoe Perches 33409b71f79fSBjorn Helgaas# Check for mailing list archives other than lore.kernel.org 33419b71f79fSBjorn Helgaas if ($rawline =~ m{http.*\b$obsolete_archives}) { 33429b71f79fSBjorn Helgaas WARN("PREFER_LORE_ARCHIVE", 33439b71f79fSBjorn Helgaas "Use lore.kernel.org archive links when possible - see https://lore.kernel.org/lists.html\n" . $herecurr); 33449b71f79fSBjorn Helgaas } 33459b71f79fSBjorn Helgaas 334613f1937eSJoe Perches# Check for added, moved or deleted files 334713f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 334813f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 334913f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 335013f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 335113f1937eSJoe Perches (defined($1) || defined($2))))) { 3352a82603a8SAndrew Jeffery $is_patch = 1; 335313f1937eSJoe Perches $reported_maintainer_file = 1; 335413f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 335513f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 335613f1937eSJoe Perches } 335713f1937eSJoe Perches 3358e400edb1SRob Herring# Check for adding new DT bindings not in schema format 3359e400edb1SRob Herring if (!$in_commit_log && 3360e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 3361e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 3362e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 336356ddc4cdSMauro Carvalho Chehab "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n"); 3364e400edb1SRob Herring } 3365e400edb1SRob Herring 336600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 33678905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 3368000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 3369000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 33706c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 3371de7d4f0eSAndy Whitcroft } 3372de7d4f0eSAndy Whitcroft 3373de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 3374de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 3375171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 3376171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 3377171ae1a4SAndy Whitcroft 3378171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 3379171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 3380171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 3381171ae1a4SAndy Whitcroft 338234d99219SJoe Perches CHK("INVALID_UTF8", 3383000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 338400df344fSAndy Whitcroft } 33850a920b5bSAndy Whitcroft 338615662b3eSJoe Perches# Check if it's the start of a commit log 338715662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 338815662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 3389eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 3390eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 339115662b3eSJoe Perches $in_header_lines = 0; 339215662b3eSJoe Perches $in_commit_log = 1; 3393ed43c4e5SAllen Hubbe $has_commit_log = 1; 339415662b3eSJoe Perches } 339515662b3eSJoe Perches 3396fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 3397fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 3398fa64205dSPasi Savanainen if ($in_header_lines && 3399fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 3400fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 3401fa64205dSPasi Savanainen $non_utf8_charset = 1; 3402fa64205dSPasi Savanainen } 3403fa64205dSPasi Savanainen 3404fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 340515662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 3406fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 340715662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 340815662b3eSJoe Perches } 340915662b3eSJoe Perches 3410d6430f71SJoe Perches# Check for absolute kernel paths in commit message 3411d6430f71SJoe Perches if ($tree && $in_commit_log) { 3412d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 3413d6430f71SJoe Perches my $file = $1; 3414d6430f71SJoe Perches 3415d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 3416d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 3417d6430f71SJoe Perches # 3418d6430f71SJoe Perches } else { 3419d6430f71SJoe Perches check_absolute_file($file, $herecurr); 3420d6430f71SJoe Perches } 3421d6430f71SJoe Perches } 3422d6430f71SJoe Perches } 3423d6430f71SJoe Perches 342466b47b4aSKees Cook# Check for various typo / spelling mistakes 342566d7a382SJoe Perches if (defined($misspellings) && 342666d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 34277da07c31SDwaipayan Ray while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) { 342866b47b4aSKees Cook my $typo = $1; 34297da07c31SDwaipayan Ray my $blank = copy_spacing($rawline); 34307da07c31SDwaipayan Ray my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo); 34317da07c31SDwaipayan Ray my $hereptr = "$hereline$ptr\n"; 343266b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 343366b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 343466b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 34350675a8fbSJean Delvare my $msg_level = \&WARN; 34360675a8fbSJean Delvare $msg_level = \&CHK if ($file); 34370675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 34387da07c31SDwaipayan Ray "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) && 343966b47b4aSKees Cook $fix) { 344066b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 344166b47b4aSKees Cook } 344266b47b4aSKees Cook } 344366b47b4aSKees Cook } 344466b47b4aSKees Cook 3445a8dd86bfSMatteo Croce# check for invalid commit id 3446a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 3447a8dd86bfSMatteo Croce my $id; 3448a8dd86bfSMatteo Croce my $description; 3449a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 3450a8dd86bfSMatteo Croce if (!defined($id)) { 3451a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 3452a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 3453a8dd86bfSMatteo Croce } 3454a8dd86bfSMatteo Croce } 3455a8dd86bfSMatteo Croce 3456310cd06bSJoe Perches# check for repeated words separated by a single space 34578d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root' 34588d0325ccSAditya Srivastava if (($rawline =~ /^\+/ || $in_commit_log) && 34598d0325ccSAditya Srivastava $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) { 34601db81a68SDwaipayan Ray pos($rawline) = 1 if (!$in_commit_log); 3461310cd06bSJoe Perches while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { 3462310cd06bSJoe Perches 3463310cd06bSJoe Perches my $first = $1; 3464310cd06bSJoe Perches my $second = $2; 34651db81a68SDwaipayan Ray my $start_pos = $-[1]; 34661db81a68SDwaipayan Ray my $end_pos = $+[2]; 3467310cd06bSJoe Perches if ($first =~ /(?:struct|union|enum)/) { 3468310cd06bSJoe Perches pos($rawline) += length($first) + length($second) + 1; 3469310cd06bSJoe Perches next; 3470310cd06bSJoe Perches } 3471310cd06bSJoe Perches 34721db81a68SDwaipayan Ray next if (lc($first) ne lc($second)); 3473310cd06bSJoe Perches next if ($first eq 'long'); 3474310cd06bSJoe Perches 34751db81a68SDwaipayan Ray # check for character before and after the word matches 34761db81a68SDwaipayan Ray my $start_char = ''; 34771db81a68SDwaipayan Ray my $end_char = ''; 34781db81a68SDwaipayan Ray $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1)); 34791db81a68SDwaipayan Ray $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline)); 34801db81a68SDwaipayan Ray 34811db81a68SDwaipayan Ray next if ($start_char =~ /^\S$/); 34821db81a68SDwaipayan Ray next if (index(" \t.,;?!", $end_char) == -1); 34831db81a68SDwaipayan Ray 34848d0325ccSAditya Srivastava # avoid repeating hex occurrences like 'ff ff fe 09 ...' 34858d0325ccSAditya Srivastava if ($first =~ /\b[0-9a-f]{2,}\b/i) { 34868d0325ccSAditya Srivastava next if (!exists($allow_repeated_words{lc($first)})); 34878d0325ccSAditya Srivastava } 34888d0325ccSAditya Srivastava 3489310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3490310cd06bSJoe Perches "Possible repeated word: '$first'\n" . $herecurr) && 3491310cd06bSJoe Perches $fix) { 3492310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; 3493310cd06bSJoe Perches } 3494310cd06bSJoe Perches } 3495310cd06bSJoe Perches 3496310cd06bSJoe Perches # if it's a repeated word on consecutive lines in a comment block 3497310cd06bSJoe Perches if ($prevline =~ /$;+\s*$/ && 3498310cd06bSJoe Perches $prevrawline =~ /($word_pattern)\s*$/) { 3499310cd06bSJoe Perches my $last_word = $1; 3500310cd06bSJoe Perches if ($rawline =~ /^\+\s*\*\s*$last_word /) { 3501310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3502310cd06bSJoe Perches "Possible repeated word: '$last_word'\n" . $hereprev) && 3503310cd06bSJoe Perches $fix) { 3504310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; 3505310cd06bSJoe Perches } 3506310cd06bSJoe Perches } 3507310cd06bSJoe Perches } 3508310cd06bSJoe Perches } 3509310cd06bSJoe Perches 351030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 351130670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 351200df344fSAndy Whitcroft 35130a920b5bSAndy Whitcroft#trailing whitespace 35149c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 3515c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3516d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 3517d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 3518d5e616fcSJoe Perches $fix) { 3519194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 3520d5e616fcSJoe Perches } 3521c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 3522c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 35233705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 35243705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 35253705ce5bSJoe Perches $fix) { 3526194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 35273705ce5bSJoe Perches } 35283705ce5bSJoe Perches 3529d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 35300a920b5bSAndy Whitcroft } 35315368df20SAndy Whitcroft 35324783f894SJosh Triplett# Check for FSF mailing addresses. 3533109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 35341bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 35353e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 35363e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 35374783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 35380675a8fbSJean Delvare my $msg_level = \&ERROR; 35390675a8fbSJean Delvare $msg_level = \&CHK if ($file); 35400675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 35414783f894SJosh Triplett "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) 35424783f894SJosh Triplett } 35434783f894SJosh Triplett 35443354957aSAndi Kleen# check for Kconfig help text having a real description 35459fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 35469fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 35473354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 3548678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3549678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3550678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3551678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 3552b8709bceSJoe Perches my $ln = $linenr; 3553b8709bceSJoe Perches my $needs_help = 0; 3554b8709bceSJoe Perches my $has_help = 0; 3555b8709bceSJoe Perches my $help_length = 0; 3556b8709bceSJoe Perches while (defined $lines[$ln]) { 3557b8709bceSJoe Perches my $f = $lines[$ln++]; 35589fe287d7SAndy Whitcroft 35599fe287d7SAndy Whitcroft next if ($f =~ /^-/); 3560b8709bceSJoe Perches last if ($f !~ /^[\+ ]/); # !patch context 3561a1385803SAndy Whitcroft 3562b8709bceSJoe Perches if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3563b8709bceSJoe Perches $needs_help = 1; 3564b8709bceSJoe Perches next; 3565b8709bceSJoe Perches } 3566b8709bceSJoe Perches if ($f =~ /^\+\s*help\s*$/) { 3567b8709bceSJoe Perches $has_help = 1; 3568b8709bceSJoe Perches next; 3569a1385803SAndy Whitcroft } 3570a1385803SAndy Whitcroft 3571b8709bceSJoe Perches $f =~ s/^.//; # strip patch context [+ ] 3572b8709bceSJoe Perches $f =~ s/#.*//; # strip # directives 3573b8709bceSJoe Perches $f =~ s/^\s+//; # strip leading blanks 3574b8709bceSJoe Perches next if ($f =~ /^$/); # skip blank lines 3575678ae162SUlf Magnusson 3576b8709bceSJoe Perches # At the end of this Kconfig block: 3577678ae162SUlf Magnusson # This only checks context lines in the patch 3578678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3579678ae162SUlf Magnusson # positives, even though some of these are 3580678ae162SUlf Magnusson # common words in help texts 3581b8709bceSJoe Perches if ($f =~ /^(?:config|menuconfig|choice|endchoice| 3582678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 35839fe287d7SAndy Whitcroft last; 35849fe287d7SAndy Whitcroft } 3585b8709bceSJoe Perches $help_length++ if ($has_help); 35863354957aSAndi Kleen } 3587b8709bceSJoe Perches if ($needs_help && 3588b8709bceSJoe Perches $help_length < $min_conf_desc_length) { 3589b8709bceSJoe Perches my $stat_real = get_stat_real($linenr, $ln - 1); 3590000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 3591b8709bceSJoe Perches "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n"); 359256193274SVadim Bendebury } 35933354957aSAndi Kleen } 35943354957aSAndi Kleen 35957ccf41a8SJoe Perches# check MAINTAINERS entries 35967ccf41a8SJoe Perches if ($realfile =~ /^MAINTAINERS$/) { 35977ccf41a8SJoe Perches# check MAINTAINERS entries for the right form 35987ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 3599628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3600628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3601628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3602628f91a2SJoe Perches $fix) { 3603628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3604628f91a2SJoe Perches } 3605628f91a2SJoe Perches } 36067ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too 36077ccf41a8SJoe Perches my $preferred_order = 'MRLSWQBCPTFXNK'; 36087ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 36097ccf41a8SJoe Perches $prevrawline =~ /^[\+ ][A-Z]:/) { 36107ccf41a8SJoe Perches $rawline =~ /^\+([A-Z]):\s*(.*)/; 36117ccf41a8SJoe Perches my $cur = $1; 36127ccf41a8SJoe Perches my $curval = $2; 36137ccf41a8SJoe Perches $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; 36147ccf41a8SJoe Perches my $prev = $1; 36157ccf41a8SJoe Perches my $prevval = $2; 36167ccf41a8SJoe Perches my $curindex = index($preferred_order, $cur); 36177ccf41a8SJoe Perches my $previndex = index($preferred_order, $prev); 36187ccf41a8SJoe Perches if ($curindex < 0) { 36197ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 36207ccf41a8SJoe Perches "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); 36217ccf41a8SJoe Perches } else { 36227ccf41a8SJoe Perches if ($previndex >= 0 && $curindex < $previndex) { 36237ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 36247ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); 36257ccf41a8SJoe Perches } elsif ((($prev eq 'F' && $cur eq 'F') || 36267ccf41a8SJoe Perches ($prev eq 'X' && $cur eq 'X')) && 36277ccf41a8SJoe Perches ($prevval cmp $curval) > 0) { 36287ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 36297ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); 36307ccf41a8SJoe Perches } 36317ccf41a8SJoe Perches } 36327ccf41a8SJoe Perches } 36337ccf41a8SJoe Perches } 3634628f91a2SJoe Perches 3635c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3636c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3637c68e5878SArnaud Lacombe my $flag = $1; 3638c68e5878SArnaud Lacombe my $replacement = { 3639c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3640c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3641c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3642c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3643c68e5878SArnaud Lacombe }; 3644c68e5878SArnaud Lacombe 3645c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3646c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3647c68e5878SArnaud Lacombe } 3648c68e5878SArnaud Lacombe 3649bff5da43SRob Herring# check for DT compatible documentation 36507dd05b38SFlorian Vaussard if (defined $root && 36517dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 36527dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 36537dd05b38SFlorian Vaussard 3654bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3655bff5da43SRob Herring 3656cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3657852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3658cc93319bSFlorian Vaussard 3659bff5da43SRob Herring foreach my $compat (@compats) { 3660bff5da43SRob Herring my $compat2 = $compat; 3661185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3662185d566bSRob Herring my $compat3 = $compat; 3663185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3664185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3665bff5da43SRob Herring if ( $? >> 8 ) { 3666bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3667bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3668bff5da43SRob Herring } 3669bff5da43SRob Herring 36704fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 36714fbf32a6SFlorian Vaussard my $vendor = $1; 3672852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3673bff5da43SRob Herring if ( $? >> 8 ) { 3674bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3675cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3676bff5da43SRob Herring } 3677bff5da43SRob Herring } 3678bff5da43SRob Herring } 3679bff5da43SRob Herring 36809f3a8992SRob Herring# check for using SPDX license tag at beginning of files 36819f3a8992SRob Herring if ($realline == $checklicenseline) { 36829f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 36839f3a8992SRob Herring $checklicenseline = 2; 36849f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 36859f3a8992SRob Herring my $comment = ""; 36869f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 36879f3a8992SRob Herring $comment = '/*'; 3688d1d84b5fSMiguel Ojeda } elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) { 36899f3a8992SRob Herring $comment = '//'; 3690c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 36919f3a8992SRob Herring $comment = '#'; 36929f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 36939f3a8992SRob Herring $comment = '..'; 36949f3a8992SRob Herring } 36959f3a8992SRob Herring 3696fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3697fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3698fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3699ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3700fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3701fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3702fdf13693SJoe Perches } 3703fdf13693SJoe Perches 37049f3a8992SRob Herring if ($comment !~ /^$/ && 3705ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 37069f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 37079f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 37083b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 37093b6e8ac9SJoe Perches my $spdx_license = $1; 37103b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 37113b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 37123b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 37133b6e8ac9SJoe Perches } 371450c92900SLubomir Rintel if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 371550c92900SLubomir Rintel not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 371650c92900SLubomir Rintel my $msg_level = \&WARN; 371750c92900SLubomir Rintel $msg_level = \&CHK if ($file); 371850c92900SLubomir Rintel if (&{$msg_level}("SPDX_LICENSE_TAG", 371950c92900SLubomir Rintel 372050c92900SLubomir Rintel "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 372150c92900SLubomir Rintel $fix) { 372250c92900SLubomir Rintel $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 372350c92900SLubomir Rintel } 372450c92900SLubomir Rintel } 37259f3a8992SRob Herring } 37269f3a8992SRob Herring } 37279f3a8992SRob Herring } 37289f3a8992SRob Herring 3729a0154cdbSJoe Perches# check for embedded filenames 3730a0154cdbSJoe Perches if ($rawline =~ /^\+.*\Q$realfile\E/) { 3731a0154cdbSJoe Perches WARN("EMBEDDED_FILENAME", 3732a0154cdbSJoe Perches "It's generally not useful to have the filename in the file\n" . $herecurr); 3733a0154cdbSJoe Perches } 3734a0154cdbSJoe Perches 37355368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3736d1d84b5fSMiguel Ojeda next if ($realfile !~ /\.(h|c|rs|s|S|sh|dtsi|dts)$/); 37375368df20SAndy Whitcroft 3738a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3739a8da38a9SJoe Perches if ($realline != $checklicenseline && 3740a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3741a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3742a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3743a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3744a8da38a9SJoe Perches } 3745a8da38a9SJoe Perches 374647e0c88bSJoe Perches# line length limit (with some exclusions) 374747e0c88bSJoe Perches# 374847e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 374947e0c88bSJoe Perches# logging functions like pr_info that end in a string 375047e0c88bSJoe Perches# lines with a single string 375147e0c88bSJoe Perches# #defines that are a single string 37522e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 375347e0c88bSJoe Perches# 375447e0c88bSJoe Perches# There are 3 different line length message types: 3755ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 375647e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 375747e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 375847e0c88bSJoe Perches# 375947e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 376047e0c88bSJoe Perches# 376147e0c88bSJoe Perches 3762b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 376347e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 376447e0c88bSJoe Perches 376547e0c88bSJoe Perches # Check the allowed long line types first 376647e0c88bSJoe Perches 376747e0c88bSJoe Perches # logging functions that end in a string that starts 376847e0c88bSJoe Perches # before $max_line_length 376947e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 377047e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 377147e0c88bSJoe Perches $msg_type = ""; 377247e0c88bSJoe Perches 377347e0c88bSJoe Perches # lines with only strings (w/ possible termination) 377447e0c88bSJoe Perches # #defines with only strings 377547e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 377647e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 377747e0c88bSJoe Perches $msg_type = ""; 377847e0c88bSJoe Perches 3779cc147506SJoe Perches # More special cases 3780cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3781cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3782d560a5f8SJoe Perches $msg_type = ""; 3783d560a5f8SJoe Perches 37842e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 37852e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 37862e4bbbc5SAndreas Brauchli $msg_type = ""; 37872e4bbbc5SAndreas Brauchli 378847e0c88bSJoe Perches # Otherwise set the alternate message types 378947e0c88bSJoe Perches 379047e0c88bSJoe Perches # a comment starts before $max_line_length 379147e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 379247e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 379347e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 379447e0c88bSJoe Perches 379547e0c88bSJoe Perches # a quoted string starts before $max_line_length 379647e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 379747e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 379847e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 379947e0c88bSJoe Perches } 380047e0c88bSJoe Perches 380147e0c88bSJoe Perches if ($msg_type ne "" && 380247e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 3803bdc48fa1SJoe Perches my $msg_level = \&WARN; 3804bdc48fa1SJoe Perches $msg_level = \&CHK if ($file); 3805bdc48fa1SJoe Perches &{$msg_level}($msg_type, 3806bdc48fa1SJoe Perches "line length of $length exceeds $max_line_length columns\n" . $herecurr); 38070a920b5bSAndy Whitcroft } 380847e0c88bSJoe Perches } 38090a920b5bSAndy Whitcroft 38108905a67cSAndy Whitcroft# check for adding lines without a newline. 38118905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 381247ca69b8STom Rix if (WARN("MISSING_EOF_NEWLINE", 381347ca69b8STom Rix "adding a line without newline at end of file\n" . $herecurr) && 381447ca69b8STom Rix $fix) { 381547ca69b8STom Rix fix_delete_line($fixlinenr+1, "No newline at end of file"); 381647ca69b8STom Rix } 38178905a67cSAndy Whitcroft } 38188905a67cSAndy Whitcroft 3819de93245cSAditya Srivastava# check for .L prefix local symbols in .S files 3820de93245cSAditya Srivastava if ($realfile =~ /\.S$/ && 3821de93245cSAditya Srivastava $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) { 3822de93245cSAditya Srivastava WARN("AVOID_L_PREFIX", 3823f4bf1cd4SJonathan Corbet "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/core-api/asm-annotations.rst\n" . $herecurr); 3824de93245cSAditya Srivastava } 3825de93245cSAditya Srivastava 3826b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3827de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 38280a920b5bSAndy Whitcroft 38290a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3830713a09deSAntonio Borneo# more than $tabsize must use tabs. 3831c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3832c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3833c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3834d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 38353705ce5bSJoe Perches if (ERROR("CODE_INDENT", 38363705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 38373705ce5bSJoe Perches $fix) { 3838194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 38393705ce5bSJoe Perches } 38400a920b5bSAndy Whitcroft } 38410a920b5bSAndy Whitcroft 384208e44365SAlberto Panizzo# check for space before tabs. 384308e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 384408e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 38453705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 38463705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 38473705ce5bSJoe Perches $fix) { 3848194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3849713a09deSAntonio Borneo s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3850194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3851c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 38523705ce5bSJoe Perches } 385308e44365SAlberto Panizzo } 385408e44365SAlberto Panizzo 38556a487211SJoe Perches# check for assignments on the start of a line 38566a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 3857da7355abSAditya Srivastava my $operator = $1; 3858da7355abSAditya Srivastava if (CHK("ASSIGNMENT_CONTINUATIONS", 3859da7355abSAditya Srivastava "Assignment operator '$1' should be on the previous line\n" . $hereprev) && 3860da7355abSAditya Srivastava $fix && $prevrawline =~ /^\+/) { 3861da7355abSAditya Srivastava # add assignment operator to the previous line, remove from current line 3862da7355abSAditya Srivastava $fixed[$fixlinenr - 1] .= " $operator"; 3863da7355abSAditya Srivastava $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 3864da7355abSAditya Srivastava } 38656a487211SJoe Perches } 38666a487211SJoe Perches 3867d1fe9c09SJoe Perches# check for && or || at the start of a line 3868d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 38698e08f076SAditya Srivastava my $operator = $1; 38708e08f076SAditya Srivastava if (CHK("LOGICAL_CONTINUATIONS", 38718e08f076SAditya Srivastava "Logical continuations should be on the previous line\n" . $hereprev) && 38728e08f076SAditya Srivastava $fix && $prevrawline =~ /^\+/) { 38738e08f076SAditya Srivastava # insert logical operator at last non-comment, non-whitepsace char on previous line 38748e08f076SAditya Srivastava $prevline =~ /[\s$;]*$/; 38758e08f076SAditya Srivastava my $line_end = substr($prevrawline, $-[0]); 38768e08f076SAditya Srivastava $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/; 38778e08f076SAditya Srivastava $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 38788e08f076SAditya Srivastava } 3879d1fe9c09SJoe Perches } 3880d1fe9c09SJoe Perches 3881a91e8994SJoe Perches# check indentation starts on a tab stop 38825b57980dSJoe Perches if ($perl_version_ok && 3883bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3884a91e8994SJoe Perches my $indent = length($1); 3885713a09deSAntonio Borneo if ($indent % $tabsize) { 3886a91e8994SJoe Perches if (WARN("TABSTOP", 3887a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3888a91e8994SJoe Perches $fix) { 3889713a09deSAntonio Borneo $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3890a91e8994SJoe Perches } 3891a91e8994SJoe Perches } 3892a91e8994SJoe Perches } 3893a91e8994SJoe Perches 3894d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 38955b57980dSJoe Perches if ($perl_version_ok && 3896fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3897d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3898d1fe9c09SJoe Perches my $oldindent = $1; 3899d1fe9c09SJoe Perches my $rest = $2; 3900d1fe9c09SJoe Perches 3901d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3902d1fe9c09SJoe Perches if ($pos >= 0) { 3903b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3904b34a26f3SJoe Perches my $newindent = $2; 3905d1fe9c09SJoe Perches 3906d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3907713a09deSAntonio Borneo "\t" x ($pos / $tabsize) . 3908713a09deSAntonio Borneo " " x ($pos % $tabsize); 3909d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3910d1fe9c09SJoe Perches 3911d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3912d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 39133705ce5bSJoe Perches 39143705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 39153705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 39163705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3917194f66fcSJoe Perches $fixed[$fixlinenr] =~ 39183705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 39193705ce5bSJoe Perches } 3920d1fe9c09SJoe Perches } 3921d1fe9c09SJoe Perches } 3922d1fe9c09SJoe Perches } 3923d1fe9c09SJoe Perches 39246ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 39256ab3a970SJoe Perches# avoid checking a few false positives: 39266ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 39276ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 39286ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 39296ab3a970SJoe Perches# multiline macros that define functions 39306ab3a970SJoe Perches# known attributes or the __attribute__ keyword 39316ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 39326ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 39333705ce5bSJoe Perches if (CHK("SPACING", 3934f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 39353705ce5bSJoe Perches $fix) { 3936194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3937f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 39383705ce5bSJoe Perches } 3939aad4f614SJoe Perches } 3940aad4f614SJoe Perches 394186406b1cSJoe Perches# Block comment styles 394286406b1cSJoe Perches# Networking with an initial /* 394305880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3944fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 394585ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 3946c70735c2SŁukasz Stelmach $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier 394705880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 394805880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 394905880600SJoe Perches } 395005880600SJoe Perches 395186406b1cSJoe Perches# Block comments use * on subsequent lines 395286406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 395386406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3954a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 395561135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3956a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 395786406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 395886406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3959a605e32eSJoe Perches } 3960a605e32eSJoe Perches 396186406b1cSJoe Perches# Block comments use */ on trailing lines 396286406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3963c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3964c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3965c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 396686406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 396786406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 396805880600SJoe Perches } 396905880600SJoe Perches 397008eb9b80SJoe Perches# Block comment * alignment 397108eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3972af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3973af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3974af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 397508eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3976af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3977af207524SJoe Perches my $oldindent; 397808eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3979af207524SJoe Perches if (defined($1)) { 3980af207524SJoe Perches $oldindent = expand_tabs($1); 3981af207524SJoe Perches } else { 3982af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3983af207524SJoe Perches $oldindent = expand_tabs($1); 3984af207524SJoe Perches } 398508eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 398608eb9b80SJoe Perches my $newindent = $1; 398708eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3988af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 398908eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 399008eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 399108eb9b80SJoe Perches } 399208eb9b80SJoe Perches } 399308eb9b80SJoe Perches 39947f619191SJoe Perches# check for missing blank lines after struct/union declarations 39957f619191SJoe Perches# with exceptions for various attributes and macros 39967f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 39977f619191SJoe Perches $line =~ /^\+/ && 39987f619191SJoe Perches !($line =~ /^\+\s*$/ || 399905dc40e6SJoe Perches $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param)/ || 40007f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 40017f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 40027f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 40037f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 40047f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 40050bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 40067f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 4007d752fcc8SJoe Perches if (CHK("LINE_SPACING", 4008d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 4009d752fcc8SJoe Perches $fix) { 4010f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 4011d752fcc8SJoe Perches } 40127f619191SJoe Perches } 40137f619191SJoe Perches 4014365dd4eaSJoe Perches# check for multiple consecutive blank lines 4015365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 4016365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 4017365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 4018d752fcc8SJoe Perches if (CHK("LINE_SPACING", 4019d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 4020d752fcc8SJoe Perches $fix) { 4021f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 4022d752fcc8SJoe Perches } 4023d752fcc8SJoe Perches 4024365dd4eaSJoe Perches $last_blank_line = $linenr; 4025365dd4eaSJoe Perches } 4026365dd4eaSJoe Perches 40273b617e3bSJoe Perches# check for missing blank lines after declarations 4028b5e8736aSJoe Perches# (declarations must have the same indentation and not be at the start of line) 4029b5e8736aSJoe Perches if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) { 4030b5e8736aSJoe Perches # use temporaries 4031b5e8736aSJoe Perches my $sl = $sline; 4032b5e8736aSJoe Perches my $pl = $prevline; 4033b5e8736aSJoe Perches # remove $Attribute/$Sparse uses to simplify comparisons 4034b5e8736aSJoe Perches $sl =~ s/\b(?:$Attribute|$Sparse)\b//g; 4035b5e8736aSJoe Perches $pl =~ s/\b(?:$Attribute|$Sparse)\b//g; 4036b5e8736aSJoe Perches if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 40375a4e1fd3SJoe Perches # function pointer declarations 4038b5e8736aSJoe Perches $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 40393f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 4040b5e8736aSJoe Perches $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 40413f7bac03SJoe Perches # known declaration macros 4042b5e8736aSJoe Perches $pl =~ /^\+\s+$declaration_macros/) && 40433f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 4044b5e8736aSJoe Perches !($pl =~ /^\+\s+$c90_Keywords\b/ || 40453f7bac03SJoe Perches # other possible extensions of declaration lines 4046b5e8736aSJoe Perches $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 40473f7bac03SJoe Perches # not starting a section or a macro "\" extended line 4048b5e8736aSJoe Perches $pl =~ /(?:\{\s*|\\)$/) && 40493f7bac03SJoe Perches # looks like a declaration 4050b5e8736aSJoe Perches !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 40515a4e1fd3SJoe Perches # function pointer declarations 4052b5e8736aSJoe Perches $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 40533f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 4054b5e8736aSJoe Perches $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 40553f7bac03SJoe Perches # known declaration macros 4056b5e8736aSJoe Perches $sl =~ /^\+\s+$declaration_macros/ || 40573f7bac03SJoe Perches # start of struct or union or enum 4058b5e8736aSJoe Perches $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 40593f7bac03SJoe Perches # start or end of block or continuation of declaration 4060b5e8736aSJoe Perches $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 40613f7bac03SJoe Perches # bitfield continuation 4062b5e8736aSJoe Perches $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 40633f7bac03SJoe Perches # other possible extensions of declaration lines 4064b5e8736aSJoe Perches $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) { 4065d752fcc8SJoe Perches if (WARN("LINE_SPACING", 4066d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 4067d752fcc8SJoe Perches $fix) { 4068f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 4069d752fcc8SJoe Perches } 40703b617e3bSJoe Perches } 4071b5e8736aSJoe Perches } 40723b617e3bSJoe Perches 40735f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 40746b4c5bebSAndy Whitcroft# Exceptions: 40756b4c5bebSAndy Whitcroft# 1) within comments 40766b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 40776b4c5bebSAndy Whitcroft# 3) hanging labels 40783705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 40795f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 40803705ce5bSJoe Perches if (WARN("LEADING_SPACE", 40813705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 40823705ce5bSJoe Perches $fix) { 4083194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 40843705ce5bSJoe Perches } 40855f7ddae6SRaffaele Recalcati } 40865f7ddae6SRaffaele Recalcati 4087b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 4088b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 4089b9ea10d6SAndy Whitcroft 40905751a24eSJoe Perches# check for unusual line ending [ or ( 40915751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 40925751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 40935751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 40945751a24eSJoe Perches } 40955751a24eSJoe Perches 40964dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 40974dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 40984dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 40994dbed76fSJoe Perches $context_function = $1; 41004dbed76fSJoe Perches } 41014dbed76fSJoe Perches 41024dbed76fSJoe Perches# check if this appears to be the end of function declaration 41034dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 41044dbed76fSJoe Perches undef $context_function; 41054dbed76fSJoe Perches } 41064dbed76fSJoe Perches 4107032a4c0fSJoe Perches# check indentation of any line with a bare else 4108840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 4109032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 4110032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 4111032a4c0fSJoe Perches my $tabs = length($1) + 1; 4112840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 4113840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 4114840080a0SJoe Perches defined $lines[$linenr] && 4115840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 4116032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 4117032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 4118032a4c0fSJoe Perches } 4119032a4c0fSJoe Perches } 4120032a4c0fSJoe Perches 4121c00df19aSJoe Perches# check indentation of a line with a break; 4122dc58bc55SJoe Perches# if the previous line is a goto, return or break 4123dc58bc55SJoe Perches# and is indented the same # of tabs 4124c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 4125c00df19aSJoe Perches my $tabs = $1; 4126dc58bc55SJoe Perches if ($prevline =~ /^\+$tabs(goto|return|break)\b/) { 4127dc58bc55SJoe Perches if (WARN("UNNECESSARY_BREAK", 4128dc58bc55SJoe Perches "break is not useful after a $1\n" . $hereprev) && 4129dc58bc55SJoe Perches $fix) { 4130dc58bc55SJoe Perches fix_delete_line($fixlinenr, $rawline); 4131dc58bc55SJoe Perches } 4132c00df19aSJoe Perches } 4133c00df19aSJoe Perches } 4134c00df19aSJoe Perches 4135c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 4136cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 4137000d1cc1SJoe Perches WARN("CVS_KEYWORD", 4138000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 4139c2fdda0dSAndy Whitcroft } 414022f2a2efSAndy Whitcroft 414156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 414256e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 414356e77d70SJoe Perches WARN("HOTPLUG_SECTION", 414456e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 414556e77d70SJoe Perches } 414656e77d70SJoe Perches 41479c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 41482b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 41492b474a1aSAndy Whitcroft $realline_next); 41503e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 4151ca819864SJoe Perches if ($linenr > $suppress_statement && 41521b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 4153170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 4154f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4155171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 4156171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 4157171ae1a4SAndy Whitcroft 41583e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 41593e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 41603e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 41613e469cdcSAndy Whitcroft # until we hit end of it. 41623e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 41633e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 41643e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 41653e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 41663e469cdcSAndy Whitcroft } 4167f74bd194SAndy Whitcroft 41682b474a1aSAndy Whitcroft # Find the real next line. 41692b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 41702b474a1aSAndy Whitcroft if (defined $realline_next && 41712b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 41722b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 41732b474a1aSAndy Whitcroft $realline_next++; 41742b474a1aSAndy Whitcroft } 41752b474a1aSAndy Whitcroft 4176171ae1a4SAndy Whitcroft my $s = $stat; 4177171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 4178cf655043SAndy Whitcroft 4179c2fdda0dSAndy Whitcroft # Ignore goto labels. 4180171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 4181c2fdda0dSAndy Whitcroft 4182c2fdda0dSAndy Whitcroft # Ignore functions being called 4183171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 4184c2fdda0dSAndy Whitcroft 4185463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 4186463f2864SAndy Whitcroft 4187c45dcabdSAndy Whitcroft # declarations always start with types 4188d2506586SAndy Whitcroft } 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) { 4189c45dcabdSAndy Whitcroft my $type = $1; 4190c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 4191c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 4192c45dcabdSAndy Whitcroft 41936c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 4194a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 4195c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 4196c2fdda0dSAndy Whitcroft } 41978905a67cSAndy Whitcroft 41986c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 419965863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 4200c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 42019c0ca6f9SAndy Whitcroft } 42028905a67cSAndy Whitcroft 42038905a67cSAndy Whitcroft # Check for any sort of function declaration. 42048905a67cSAndy Whitcroft # int foo(something bar, other baz); 42058905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 4206171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 42078905a67cSAndy Whitcroft my ($name_len) = length($1); 42088905a67cSAndy Whitcroft 4209cf655043SAndy Whitcroft my $ctx = $s; 4210773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 42118905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 4212cf655043SAndy Whitcroft 42138905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 4214c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 42158905a67cSAndy Whitcroft 4216c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 42178905a67cSAndy Whitcroft } 42188905a67cSAndy Whitcroft } 42198905a67cSAndy Whitcroft } 42208905a67cSAndy Whitcroft 42219c0ca6f9SAndy Whitcroft } 42229c0ca6f9SAndy Whitcroft 422300df344fSAndy Whitcroft# 422400df344fSAndy Whitcroft# Checks which may be anchored in the context. 422500df344fSAndy Whitcroft# 422600df344fSAndy Whitcroft 422700df344fSAndy Whitcroft# Check for switch () and associated case and default 422800df344fSAndy Whitcroft# statements should be at the same indent. 422900df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 423000df344fSAndy Whitcroft my $err = ''; 423100df344fSAndy Whitcroft my $sep = ''; 423200df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 423300df344fSAndy Whitcroft shift(@ctx); 423400df344fSAndy Whitcroft for my $ctx (@ctx) { 423500df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 423600df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 423700df344fSAndy Whitcroft $indent != $cindent) { 423800df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 423900df344fSAndy Whitcroft $sep = ''; 424000df344fSAndy Whitcroft } else { 424100df344fSAndy Whitcroft $sep = "[...]\n"; 424200df344fSAndy Whitcroft } 424300df344fSAndy Whitcroft } 424400df344fSAndy Whitcroft if ($err ne '') { 4245000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 4246000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 4247de7d4f0eSAndy Whitcroft } 4248de7d4f0eSAndy Whitcroft } 4249de7d4f0eSAndy Whitcroft 4250de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 4251de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 42520fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 4253773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 4254773647a0SAndy Whitcroft 42559c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 42568eef05ddSJoe Perches 42578eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 42588eef05ddSJoe Perches WARN("DEEP_INDENTATION", 42598eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 42608eef05ddSJoe Perches } 42618eef05ddSJoe Perches 4262de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 4263de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 4264de7d4f0eSAndy Whitcroft 4265548596d5SAndy Whitcroft my $ctx_ln = $linenr; 4266548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 4267de7d4f0eSAndy Whitcroft 4268548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 4269548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 4270548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 4271548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 4272548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 4273773647a0SAndy Whitcroft $ctx_ln++; 4274773647a0SAndy Whitcroft } 4275548596d5SAndy Whitcroft 427653210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 427753210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 4278773647a0SAndy Whitcroft 4279773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 4280000d1cc1SJoe Perches ERROR("OPEN_BRACE", 4281000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 428201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 428300df344fSAndy Whitcroft } 4284773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 4285773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 4286773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 4287773647a0SAndy Whitcroft { 42889c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 42899c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 4290000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 4291000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 429201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 42939c0ca6f9SAndy Whitcroft } 42949c0ca6f9SAndy Whitcroft } 429500df344fSAndy Whitcroft } 429600df344fSAndy Whitcroft 42974d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 4298f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 42993e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 43003e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 43013e469cdcSAndy Whitcroft if (!defined $stat); 43024d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 43034d001e4dSAndy Whitcroft 43044d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 43054d001e4dSAndy Whitcroft 43069f5af480SJoe Perches # remove inline comments 43079f5af480SJoe Perches $s =~ s/$;/ /g; 43089f5af480SJoe Perches $c =~ s/$;/ /g; 43094d001e4dSAndy Whitcroft 43104d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 43116f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 43126f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 43134d001e4dSAndy Whitcroft 43149f5af480SJoe Perches # Make sure we remove the line prefixes as we have 43159f5af480SJoe Perches # none on the first line, and are going to readd them 43169f5af480SJoe Perches # where necessary. 43179f5af480SJoe Perches $s =~ s/\n./\n/gs; 43189f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 43199f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 43209f5af480SJoe Perches } 43219f5af480SJoe Perches 43224d001e4dSAndy Whitcroft # We want to check the first line inside the block 43234d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 43244d001e4dSAndy Whitcroft # 1) any blank line termination 43254d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 43264d001e4dSAndy Whitcroft # 3) any do (...) { 43274d001e4dSAndy Whitcroft my $continuation = 0; 43284d001e4dSAndy Whitcroft my $check = 0; 43294d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 43304d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 43314d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 43324d001e4dSAndy Whitcroft $continuation = 1; 43334d001e4dSAndy Whitcroft } 43349bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 43354d001e4dSAndy Whitcroft $check = 1; 43364d001e4dSAndy Whitcroft $cond_lines++; 43374d001e4dSAndy Whitcroft } 43384d001e4dSAndy Whitcroft 43394d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 43404d001e4dSAndy Whitcroft # preprocessor statement. 43414d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 43424d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 43434d001e4dSAndy Whitcroft $check = 0; 43444d001e4dSAndy Whitcroft } 43454d001e4dSAndy Whitcroft 43469bd49efeSAndy Whitcroft my $cond_ptr = -1; 4347740504c6SAndy Whitcroft $continuation = 0; 43489bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 43499bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 43504d001e4dSAndy Whitcroft 4351f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 4352f16fa28fSAndy Whitcroft # is not linear. 4353f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 4354f16fa28fSAndy Whitcroft $check = 0; 4355f16fa28fSAndy Whitcroft } 4356f16fa28fSAndy Whitcroft 43579bd49efeSAndy Whitcroft # Ignore: 43589bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 43599bd49efeSAndy Whitcroft # 2) preprocessor lines, and 43609bd49efeSAndy Whitcroft # 3) labels. 4361740504c6SAndy Whitcroft if ($continuation || 4362740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 43639bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 43649bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 4365740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 436630dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 43679bd49efeSAndy Whitcroft $cond_lines++; 43689bd49efeSAndy Whitcroft } 43694d001e4dSAndy Whitcroft } 437030dad6ebSAndy Whitcroft } 43714d001e4dSAndy Whitcroft 43724d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 43734d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 43744d001e4dSAndy Whitcroft 43754d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 43764d001e4dSAndy Whitcroft # this is not this patch's fault. 43774d001e4dSAndy Whitcroft if (!defined($stat_real) || 43784d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 43794d001e4dSAndy Whitcroft $check = 0; 43804d001e4dSAndy Whitcroft } 43814d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 43824d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 43834d001e4dSAndy Whitcroft } 43844d001e4dSAndy Whitcroft 43859bd49efeSAndy Whitcroft #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"; 43864d001e4dSAndy Whitcroft 43879f5af480SJoe Perches if ($check && $s ne '' && 4388713a09deSAntonio Borneo (($sindent % $tabsize) != 0 || 43899f5af480SJoe Perches ($sindent < $indent) || 4390f6950a73SJoe Perches ($sindent == $indent && 4391f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 4392713a09deSAntonio Borneo ($sindent > $indent + $tabsize))) { 4393000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 4394000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 43954d001e4dSAndy Whitcroft } 43964d001e4dSAndy Whitcroft } 43974d001e4dSAndy Whitcroft 43986c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 43996c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 44001f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 44011f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 44026c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 4403c2fdda0dSAndy Whitcroft if ($dbg_values) { 4404c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 4405cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 4406cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 44071f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 4408c2fdda0dSAndy Whitcroft } 44096c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 44106c72ffaaSAndy Whitcroft 441100df344fSAndy Whitcroft#ignore lines not being added 44123705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 441300df344fSAndy Whitcroft 441499ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings 441599ca38c2SJoe Perches# e.g.: int foo = foo, *bar = NULL; 441699ca38c2SJoe Perches# struct foo bar = *(&(bar)); 441799ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { 441899ca38c2SJoe Perches my $var = $1; 441999ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { 442099ca38c2SJoe Perches WARN("SELF_ASSIGNMENT", 442199ca38c2SJoe Perches "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); 442299ca38c2SJoe Perches } 442399ca38c2SJoe Perches } 442499ca38c2SJoe Perches 442511ca40a0SJoe Perches# check for dereferences that span multiple lines 442611ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 442711ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 442811ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 442911ca40a0SJoe Perches my $ref = $1; 443011ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 443111ca40a0SJoe Perches $ref .= $1; 443211ca40a0SJoe Perches $ref =~ s/\s//g; 443311ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 443411ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 443511ca40a0SJoe Perches } 443611ca40a0SJoe Perches 4437a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 4438c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 4439a1ce18e4SJoe Perches my $type = $1; 4440a1ce18e4SJoe Perches my $var = $2; 4441207a8e84SJoe Perches $var = "" if (!defined $var); 4442207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 4443a1ce18e4SJoe Perches my $sign = $1; 4444a1ce18e4SJoe Perches my $pointer = $2; 4445a1ce18e4SJoe Perches 4446a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 4447a1ce18e4SJoe Perches 4448a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 4449a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 4450a1ce18e4SJoe Perches $fix) { 4451a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 4452207a8e84SJoe Perches my $comp_pointer = $pointer; 4453207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 4454207a8e84SJoe Perches $decl .= $comp_pointer; 4455207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 4456207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 4457a1ce18e4SJoe Perches } 4458a1ce18e4SJoe Perches } 4459a1ce18e4SJoe Perches } 4460a1ce18e4SJoe Perches 4461653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 44627429c690SAndy Whitcroft if ($dbg_type) { 44637429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 4464000d1cc1SJoe Perches ERROR("TEST_TYPE", 4465000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 44667429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 4467000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 4468000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 44697429c690SAndy Whitcroft } 4470653d4876SAndy Whitcroft next; 4471653d4876SAndy Whitcroft } 4472a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 4473a1ef277eSAndy Whitcroft if ($dbg_attr) { 44749360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 4475000d1cc1SJoe Perches ERROR("TEST_ATTR", 4476000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 44779360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 4478000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 4479000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 4480a1ef277eSAndy Whitcroft } 4481a1ef277eSAndy Whitcroft next; 4482a1ef277eSAndy Whitcroft } 4483653d4876SAndy Whitcroft 4484f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 448599423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 448699423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 4487d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 4488d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 4489f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 4490f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4491f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 4492d752fcc8SJoe Perches my $fixedline = $prevrawline; 4493d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 4494f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4495d752fcc8SJoe Perches $fixedline = $line; 44968d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 4497f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4498d752fcc8SJoe Perches } 4499f0a594c1SAndy Whitcroft } 4500f0a594c1SAndy Whitcroft 450100df344fSAndy Whitcroft# 450200df344fSAndy Whitcroft# Checks which are anchored on the added line. 450300df344fSAndy Whitcroft# 450400df344fSAndy Whitcroft 4505653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 4506c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 4507653d4876SAndy Whitcroft my $path = $1; 4508653d4876SAndy Whitcroft if ($path =~ m{//}) { 4509000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 4510495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 4511495e9d84SJoe Perches } 4512495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 4513495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 4514495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 4515653d4876SAndy Whitcroft } 4516653d4876SAndy Whitcroft } 4517653d4876SAndy Whitcroft 451800df344fSAndy Whitcroft# no C99 // comments 451900df344fSAndy Whitcroft if ($line =~ m{//}) { 45203705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 45213705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 45223705ce5bSJoe Perches $fix) { 4523194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 45243705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 45253705ce5bSJoe Perches my $comment = trim($1); 4526194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 45273705ce5bSJoe Perches } 45283705ce5bSJoe Perches } 452900df344fSAndy Whitcroft } 453000df344fSAndy Whitcroft # Remove C99 comments. 45310a920b5bSAndy Whitcroft $line =~ s@//.*@@; 45326c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 45330a920b5bSAndy Whitcroft 45342b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 45352b474a1aSAndy Whitcroft# the whole statement. 45362b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 45372b474a1aSAndy Whitcroft if (defined $realline_next && 45382b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 45392b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 454036794822SChristoph Hellwig ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) { 45413cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 45423cbf62dfSAndy Whitcroft # a prefix: 45433cbf62dfSAndy Whitcroft # XXX(foo); 45443cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 4545653d4876SAndy Whitcroft my $name = $1; 454670a11659SJoe Perches $name =~ s/^\s*($Ident).*/$1/; 454787a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 45483cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 45493cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 45503cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 45513cbf62dfSAndy Whitcroft 45523cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 45532b474a1aSAndy Whitcroft \n.}\s*$| 455448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 455548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 455648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 45572b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 45582b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 455948012058SAndy Whitcroft )/x) { 45602b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 45612b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 45622b474a1aSAndy Whitcroft } else { 45632b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 45640a920b5bSAndy Whitcroft } 45650a920b5bSAndy Whitcroft } 45662b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 45672b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 456836794822SChristoph Hellwig ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) { 45692b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 45702b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 45712b474a1aSAndy Whitcroft } 45722b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 45732b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 4574000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 4575000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 45762b474a1aSAndy Whitcroft } 45770a920b5bSAndy Whitcroft 45785150bda4SJoe Eloff# check for global initialisers. 45795b8f82e1SSong Liu if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ && 45805b8f82e1SSong Liu !exclude_global_initialisers($realfile)) { 4581d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 45826d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 4583d5e616fcSJoe Perches $fix) { 45846d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 4585d5e616fcSJoe Perches } 4586f0a594c1SAndy Whitcroft } 45870a920b5bSAndy Whitcroft# check for static initialisers. 45886d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 4589d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 45906d32f7a3SJoe Perches "do not initialise statics to $1\n" . 4591d5e616fcSJoe Perches $herecurr) && 4592d5e616fcSJoe Perches $fix) { 45936d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 4594d5e616fcSJoe Perches } 45950a920b5bSAndy Whitcroft } 45960a920b5bSAndy Whitcroft 45971813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 45981813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 45991813087dSJoe Perches my $tmp = trim($1); 46001813087dSJoe Perches WARN("MISORDERED_TYPE", 46011813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 46021813087dSJoe Perches } 46031813087dSJoe Perches 4604809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 4605809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 4606809e082eSJoe Perches my $type = trim($1); 4607809e082eSJoe Perches next if ($type !~ /\bint\b/); 4608809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 4609809e082eSJoe Perches my $new_type = $type; 4610809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 4611809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 4612809e082eSJoe Perches $new_type =~ s/^const\s+//; 4613809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 4614809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 4615809e082eSJoe Perches $new_type =~ s/\s+/ /g; 4616809e082eSJoe Perches $new_type = trim($new_type); 4617809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 4618809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 4619809e082eSJoe Perches $fix) { 4620809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 4621809e082eSJoe Perches } 4622809e082eSJoe Perches } 4623809e082eSJoe Perches 4624cb710ecaSJoe Perches# check for static const char * arrays. 4625cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 4626000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4627000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 4628cb710ecaSJoe Perches $herecurr); 4629cb710ecaSJoe Perches } 4630cb710ecaSJoe Perches 463177b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 463277b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 463377b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 463477b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 463577b8c0a8SJoe Perches $fix) { 463677b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 463777b8c0a8SJoe Perches } 463877b8c0a8SJoe Perches } 463977b8c0a8SJoe Perches 4640cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 4641cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4642000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4643000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4644cb710ecaSJoe Perches $herecurr); 4645cb710ecaSJoe Perches } 4646cb710ecaSJoe Perches 4647ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4648ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4649ab7e23f3SJoe Perches my $found = $1; 4650ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4651ab7e23f3SJoe Perches WARN("CONST_CONST", 4652ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4653ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4654ab7e23f3SJoe Perches WARN("CONST_CONST", 4655ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4656ab7e23f3SJoe Perches } 4657ab7e23f3SJoe Perches } 4658ab7e23f3SJoe Perches 465973169765SJoe Perches# check for const static or static <non ptr type> const declarations 466073169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const' 466173169765SJoe Perches if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ || 466273169765SJoe Perches $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) { 466373169765SJoe Perches if (WARN("STATIC_CONST", 466473169765SJoe Perches "Move const after static - use 'static const $1'\n" . $herecurr) && 466573169765SJoe Perches $fix) { 466673169765SJoe Perches $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/; 466773169765SJoe Perches $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/; 466873169765SJoe Perches } 466973169765SJoe Perches } 467073169765SJoe Perches 46719b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 46729b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 46739b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 46749b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 46759b0fa60dSJoe Perches $herecurr); 46769b0fa60dSJoe Perches } 46779b0fa60dSJoe Perches 4678b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4679b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4680b598b670SJoe Perches my $array = $1; 4681b598b670SJoe Perches if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { 4682b598b670SJoe Perches my $array_div = $1; 4683b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4684b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4685b598b670SJoe Perches $fix) { 4686b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4687b598b670SJoe Perches } 4688b598b670SJoe Perches } 4689b598b670SJoe Perches } 4690b598b670SJoe Perches 4691b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 469216b7f3c8SJoe Perches if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { 4693b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4694b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4695b36190c5SJoe Perches $fix) { 4696194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4697b36190c5SJoe Perches } 4698b36190c5SJoe Perches } 4699b36190c5SJoe Perches 4700653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4701653d4876SAndy Whitcroft# make sense. 4702653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 47038054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4704c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 47058ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 470646d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4707000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4708000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 47090a920b5bSAndy Whitcroft } 47100a920b5bSAndy Whitcroft 47110a920b5bSAndy Whitcroft# * goes on variable not on type 471265863862SAndy Whitcroft # (char*[ const]) 4713bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4714bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 47153705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4716d8aaf121SAndy Whitcroft 471765863862SAndy Whitcroft # Should start with a space. 471865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 471965863862SAndy Whitcroft # Should not end with a space. 472065863862SAndy Whitcroft $to =~ s/\s+$//; 472165863862SAndy Whitcroft # '*'s should not have spaces between. 4722f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 472365863862SAndy Whitcroft } 4724d8aaf121SAndy Whitcroft 47253705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 472665863862SAndy Whitcroft if ($from ne $to) { 47273705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 47283705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 47293705ce5bSJoe Perches $fix) { 47303705ce5bSJoe Perches my $sub_from = $ident; 47313705ce5bSJoe Perches my $sub_to = $ident; 47323705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4733194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47343705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 47353705ce5bSJoe Perches } 473665863862SAndy Whitcroft } 4737bfcb2cc7SAndy Whitcroft } 4738bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4739bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 47403705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4741d8aaf121SAndy Whitcroft 474265863862SAndy Whitcroft # Should start with a space. 474365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 474465863862SAndy Whitcroft # Should not end with a space. 474565863862SAndy Whitcroft $to =~ s/\s+$//; 474665863862SAndy Whitcroft # '*'s should not have spaces between. 4747f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 474865863862SAndy Whitcroft } 474965863862SAndy Whitcroft # Modifiers should have spaces. 475065863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 475165863862SAndy Whitcroft 47523705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4753667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 47543705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 47553705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 47563705ce5bSJoe Perches $fix) { 47573705ce5bSJoe Perches 47583705ce5bSJoe Perches my $sub_from = $match; 47593705ce5bSJoe Perches my $sub_to = $match; 47603705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4761194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47623705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 47633705ce5bSJoe Perches } 476465863862SAndy Whitcroft } 47650a920b5bSAndy Whitcroft } 47660a920b5bSAndy Whitcroft 476769d517e6SDavid Hildenbrand# do not use BUG() or variants 476869d517e6SDavid Hildenbrand if ($line =~ /\b(?!AA_|BUILD_|DCCP_|IDA_|KVM_|RWLOCK_|snd_|SPIN_)(?:[a-zA-Z_]*_)?BUG(?:_ON)?(?:_[A-Z_]+)?\s*\(/) { 47690675a8fbSJean Delvare my $msg_level = \&WARN; 47700675a8fbSJean Delvare $msg_level = \&CHK if ($file); 47710675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 477269d517e6SDavid Hildenbrand "Do not crash the kernel unless it is absolutely unavoidable--use WARN_ON_ONCE() plus recovery code (if feasible) instead of BUG() or variants\n" . $herecurr); 47739d3e3c70SJoe Perches } 47740a920b5bSAndy Whitcroft 47759d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 47768905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4777000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4778000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 47798905a67cSAndy Whitcroft } 47808905a67cSAndy Whitcroft 478117441227SJoe Perches# check for uses of printk_ratelimit 478217441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4783000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4784000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 478517441227SJoe Perches } 478617441227SJoe Perches 4787eeef5733SJoe Perches# printk should use KERN_* levels 4788eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4789000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4790eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 479100df344fSAndy Whitcroft } 47920a920b5bSAndy Whitcroft 4793f5eea3b0SJoe Perches# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL> 4794f5eea3b0SJoe Perches if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) { 4795f5eea3b0SJoe Perches my $printk = $1; 4796f5eea3b0SJoe Perches my $modifier = $2; 4797f5eea3b0SJoe Perches my $orig = $3; 4798f5eea3b0SJoe Perches $modifier = "" if (!defined($modifier)); 4799243f3803SJoe Perches my $level = lc($orig); 4800243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 48018f26b837SJoe Perches my $level2 = $level; 48028f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4803f5eea3b0SJoe Perches $level .= $modifier; 4804f5eea3b0SJoe Perches $level2 .= $modifier; 4805243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4806f5eea3b0SJoe Perches "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr); 4807243f3803SJoe Perches } 4808243f3803SJoe Perches 4809f5eea3b0SJoe Perches# prefer dev_<level> to dev_printk(KERN_<LEVEL> 4810dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4811dc139313SJoe Perches my $orig = $1; 4812dc139313SJoe Perches my $level = lc($orig); 4813dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4814dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4815dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4816dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4817dc139313SJoe Perches } 4818dc139313SJoe Perches 48198020b253SNicolas Boichat# trace_printk should not be used in production code. 48208020b253SNicolas Boichat if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { 48218020b253SNicolas Boichat WARN("TRACE_PRINTK", 48228020b253SNicolas Boichat "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); 48238020b253SNicolas Boichat } 48248020b253SNicolas Boichat 482591c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 482691c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 482791c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 482891c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 482991c9afafSAndy Lutomirski WARN("ENOSYS", 483091c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 483191c9afafSAndy Lutomirski } 483291c9afafSAndy Lutomirski 48336b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches. 48346b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 48356b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected. 48366b9ea5ffSJakub Kicinski if (!$file && $line =~ /\bENOTSUPP\b/) { 48376b9ea5ffSJakub Kicinski if (WARN("ENOTSUPP", 48386b9ea5ffSJakub Kicinski "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 48396b9ea5ffSJakub Kicinski $fix) { 48406b9ea5ffSJakub Kicinski $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 48416b9ea5ffSJakub Kicinski } 48426b9ea5ffSJakub Kicinski } 48436b9ea5ffSJakub Kicinski 4844653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4845653d4876SAndy Whitcroft# or if closed on same line 48465b57980dSJoe Perches if ($perl_version_ok && 48472d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 48482d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 48492d453e3bSJoe Perches $sline !~ /}/) { 48508d182478SJoe Perches if (ERROR("OPEN_BRACE", 48512d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 48528d182478SJoe Perches $fix) { 48538d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 48548d182478SJoe Perches my $fixed_line = $rawline; 485503f49351SDwaipayan Ray $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/; 48568d182478SJoe Perches my $line1 = $1; 48578d182478SJoe Perches my $line2 = $2; 48588d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 48598d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 48608d182478SJoe Perches if ($line2 !~ /^\s*$/) { 48618d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 48628d182478SJoe Perches } 48638d182478SJoe Perches } 48640a920b5bSAndy Whitcroft } 4865653d4876SAndy Whitcroft 48668905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 48678905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 48688905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 48698d182478SJoe Perches if (ERROR("OPEN_BRACE", 48708d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 48718d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 48728d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 48738d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 48748d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 48758d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48768d182478SJoe Perches $fixedline = $rawline; 48778d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 48788d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 48798d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48808d182478SJoe Perches } 48818d182478SJoe Perches } 48828905a67cSAndy Whitcroft } 48838905a67cSAndy Whitcroft 48840c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 48853705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 48863705ce5bSJoe Perches if (WARN("SPACING", 48873705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 48883705ce5bSJoe Perches $fix) { 4889194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48903705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 48913705ce5bSJoe Perches } 48920c73b4ebSAndy Whitcroft } 48930c73b4ebSAndy Whitcroft 489431070b5dSJoe Perches# Function pointer declarations 489531070b5dSJoe Perches# check spacing between type, funcptr, and args 489631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 489791f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 489831070b5dSJoe Perches my $declare = $1; 489931070b5dSJoe Perches my $pre_pointer_space = $2; 490031070b5dSJoe Perches my $post_pointer_space = $3; 490131070b5dSJoe Perches my $funcname = $4; 490231070b5dSJoe Perches my $post_funcname_space = $5; 490331070b5dSJoe Perches my $pre_args_space = $6; 490431070b5dSJoe Perches 490591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 490691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 490791f72e9cSJoe Perches# don't need a space so don't warn for those. 490891f72e9cSJoe Perches my $post_declare_space = ""; 490991f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 491091f72e9cSJoe Perches $post_declare_space = $1; 491191f72e9cSJoe Perches $declare = rtrim($declare); 491291f72e9cSJoe Perches } 491391f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 491431070b5dSJoe Perches WARN("SPACING", 491531070b5dSJoe Perches "missing space after return type\n" . $herecurr); 491691f72e9cSJoe Perches $post_declare_space = " "; 491731070b5dSJoe Perches } 491831070b5dSJoe Perches 491931070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 492091f72e9cSJoe Perches# This test is not currently implemented because these declarations are 492191f72e9cSJoe Perches# equivalent to 492291f72e9cSJoe Perches# int foo(int bar, ...) 492391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 492491f72e9cSJoe Perches# 492591f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 492691f72e9cSJoe Perches# WARN("SPACING", 492791f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 492891f72e9cSJoe Perches# } 492931070b5dSJoe Perches 493031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 493131070b5dSJoe Perches if (defined $pre_pointer_space && 493231070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 493331070b5dSJoe Perches WARN("SPACING", 493431070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 493531070b5dSJoe Perches } 493631070b5dSJoe Perches 493731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 493831070b5dSJoe Perches if (defined $post_pointer_space && 493931070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 494031070b5dSJoe Perches WARN("SPACING", 494131070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 494231070b5dSJoe Perches } 494331070b5dSJoe Perches 494431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 494531070b5dSJoe Perches if (defined $post_funcname_space && 494631070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 494731070b5dSJoe Perches WARN("SPACING", 494831070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 494931070b5dSJoe Perches } 495031070b5dSJoe Perches 495131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 495231070b5dSJoe Perches if (defined $pre_args_space && 495331070b5dSJoe Perches $pre_args_space =~ /^\s/) { 495431070b5dSJoe Perches WARN("SPACING", 495531070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 495631070b5dSJoe Perches } 495731070b5dSJoe Perches 495831070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4959194f66fcSJoe Perches $fixed[$fixlinenr] =~ 496091f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 496131070b5dSJoe Perches } 496231070b5dSJoe Perches } 496331070b5dSJoe Perches 49648d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 49658d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4966fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4967fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 49688d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 49698d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 49708d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4971fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 497238dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 49733705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 49743705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 49753705ce5bSJoe Perches $fix) { 4976194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49773705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 49783705ce5bSJoe Perches } 49798d31cfceSAndy Whitcroft } 49808d31cfceSAndy Whitcroft } 49818d31cfceSAndy Whitcroft 4982f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 49836c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4984c2fdda0dSAndy Whitcroft my $name = $1; 4985773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4986773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4987c2fdda0dSAndy Whitcroft 4988c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4989773647a0SAndy Whitcroft if ($name =~ /^(?: 4990773647a0SAndy Whitcroft if|for|while|switch|return|case| 4991773647a0SAndy Whitcroft volatile|__volatile__| 4992773647a0SAndy Whitcroft __attribute__|format|__extension__| 4993773647a0SAndy Whitcroft asm|__asm__)$/x) 4994773647a0SAndy Whitcroft { 4995c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4996c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4997c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4998c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4999773647a0SAndy Whitcroft 5000773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 5001c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 5002c2fdda0dSAndy Whitcroft 5003c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 5004c2fdda0dSAndy Whitcroft # likely a typedef for a function. 5005773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 5006c2fdda0dSAndy Whitcroft 5007c2fdda0dSAndy Whitcroft } else { 50083705ce5bSJoe Perches if (WARN("SPACING", 50093705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 50103705ce5bSJoe Perches $fix) { 5011194f66fcSJoe Perches $fixed[$fixlinenr] =~ 50123705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 50133705ce5bSJoe Perches } 5014f0a594c1SAndy Whitcroft } 50156c72ffaaSAndy Whitcroft } 50169a4cad4eSEric Nelson 5017653d4876SAndy Whitcroft# Check operator spacing. 50180a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 50193705ce5bSJoe Perches my $fixed_line = ""; 50203705ce5bSJoe Perches my $line_fixed = 0; 50213705ce5bSJoe Perches 50229c0ca6f9SAndy Whitcroft my $ops = qr{ 50239c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 50249c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 50259c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 50261f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 502784731623SJoe Perches \?:|\?|: 50289c0ca6f9SAndy Whitcroft }x; 5029cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 50303705ce5bSJoe Perches 50313705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 50323705ce5bSJoe Perches## foreach my $el (@elements) { 50333705ce5bSJoe Perches## print("el: <$el>\n"); 50343705ce5bSJoe Perches## } 50353705ce5bSJoe Perches 50363705ce5bSJoe Perches my @fix_elements = (); 503700df344fSAndy Whitcroft my $off = 0; 50386c72ffaaSAndy Whitcroft 50393705ce5bSJoe Perches foreach my $el (@elements) { 50403705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 50413705ce5bSJoe Perches $off += length($el); 50423705ce5bSJoe Perches } 50433705ce5bSJoe Perches 50443705ce5bSJoe Perches $off = 0; 50453705ce5bSJoe Perches 50466c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 5047b34c648bSJoe Perches my $last_after = -1; 50486c72ffaaSAndy Whitcroft 50490a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 50503705ce5bSJoe Perches 50513705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 50523705ce5bSJoe Perches 50533705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 50543705ce5bSJoe Perches 50554a0df2efSAndy Whitcroft $off += length($elements[$n]); 50564a0df2efSAndy Whitcroft 505725985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 5058773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 5059773647a0SAndy Whitcroft my $cc = ''; 5060773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 5061773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 5062773647a0SAndy Whitcroft } 5063773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 5064773647a0SAndy Whitcroft 50654a0df2efSAndy Whitcroft my $a = ''; 50664a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 50674a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 5068cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 50694a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 50704a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 5071773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 50724a0df2efSAndy Whitcroft 50730a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 50744a0df2efSAndy Whitcroft 50754a0df2efSAndy Whitcroft my $c = ''; 50760a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 50774a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 50784a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 5079cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 50804a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 50814a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 50828b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 50834a0df2efSAndy Whitcroft } else { 50844a0df2efSAndy Whitcroft $c = 'E'; 50850a920b5bSAndy Whitcroft } 50860a920b5bSAndy Whitcroft 50874a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 50884a0df2efSAndy Whitcroft 50894a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 50904a0df2efSAndy Whitcroft 50916c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 5092de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 50930a920b5bSAndy Whitcroft 509474048ed8SAndy Whitcroft # Pull out the value of this operator. 50956c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 50960a920b5bSAndy Whitcroft 50971f65f947SAndy Whitcroft # Get the full operator variant. 50981f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 50991f65f947SAndy Whitcroft 510013214adfSAndy Whitcroft # Ignore operators passed as parameters. 510113214adfSAndy Whitcroft if ($op_type ne 'V' && 5102d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 510313214adfSAndy Whitcroft 5104cf655043SAndy Whitcroft# # Ignore comments 5105cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 510613214adfSAndy Whitcroft 5107d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 510813214adfSAndy Whitcroft } elsif ($op eq ';') { 5109cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 5110cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 51113705ce5bSJoe Perches if (ERROR("SPACING", 51123705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 5113b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 51143705ce5bSJoe Perches $line_fixed = 1; 51153705ce5bSJoe Perches } 5116d8aaf121SAndy Whitcroft } 5117d8aaf121SAndy Whitcroft 5118d8aaf121SAndy Whitcroft # // is a comment 5119d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 51200a920b5bSAndy Whitcroft 5121b00e4814SJoe Perches # : when part of a bitfield 5122b00e4814SJoe Perches } elsif ($opv eq ':B') { 5123b00e4814SJoe Perches # skip the bitfield test for now 5124b00e4814SJoe Perches 51251f65f947SAndy Whitcroft # No spaces for: 51261f65f947SAndy Whitcroft # -> 5127b00e4814SJoe Perches } elsif ($op eq '->') { 51284a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 51293705ce5bSJoe Perches if (ERROR("SPACING", 51303705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 5131b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 51323705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 51333705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 51343705ce5bSJoe Perches } 5135b34c648bSJoe Perches $line_fixed = 1; 51363705ce5bSJoe Perches } 51370a920b5bSAndy Whitcroft } 51380a920b5bSAndy Whitcroft 51392381097bSJoe Perches # , must not have a space before and must have a space on the right. 51400a920b5bSAndy Whitcroft } elsif ($op eq ',') { 51412381097bSJoe Perches my $rtrim_before = 0; 51422381097bSJoe Perches my $space_after = 0; 51432381097bSJoe Perches if ($ctx =~ /Wx./) { 51442381097bSJoe Perches if (ERROR("SPACING", 51452381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 51462381097bSJoe Perches $line_fixed = 1; 51472381097bSJoe Perches $rtrim_before = 1; 51482381097bSJoe Perches } 51492381097bSJoe Perches } 5150cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 51513705ce5bSJoe Perches if (ERROR("SPACING", 51523705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 51533705ce5bSJoe Perches $line_fixed = 1; 5154b34c648bSJoe Perches $last_after = $n; 51552381097bSJoe Perches $space_after = 1; 51562381097bSJoe Perches } 51572381097bSJoe Perches } 51582381097bSJoe Perches if ($rtrim_before || $space_after) { 51592381097bSJoe Perches if ($rtrim_before) { 51602381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 51612381097bSJoe Perches } else { 51622381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 51632381097bSJoe Perches } 51642381097bSJoe Perches if ($space_after) { 51652381097bSJoe Perches $good .= " "; 51663705ce5bSJoe Perches } 51670a920b5bSAndy Whitcroft } 51680a920b5bSAndy Whitcroft 51699c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 517074048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 51719c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 51729c0ca6f9SAndy Whitcroft 51739c0ca6f9SAndy Whitcroft # unary operators should have a space before and 51749c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 51759c0ca6f9SAndy Whitcroft # unary operator, or a cast 51769c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 517774048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 51780d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 5179cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 51803705ce5bSJoe Perches if (ERROR("SPACING", 51813705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 5182b34c648bSJoe Perches if ($n != $last_after + 2) { 5183b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 51843705ce5bSJoe Perches $line_fixed = 1; 51853705ce5bSJoe Perches } 51860a920b5bSAndy Whitcroft } 5187b34c648bSJoe Perches } 5188a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 5189171ae1a4SAndy Whitcroft # A unary '*' may be const 5190171ae1a4SAndy Whitcroft 5191171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 51923705ce5bSJoe Perches if (ERROR("SPACING", 51933705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 5194b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 51953705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 51963705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 51973705ce5bSJoe Perches } 5198b34c648bSJoe Perches $line_fixed = 1; 51993705ce5bSJoe Perches } 52000a920b5bSAndy Whitcroft } 52010a920b5bSAndy Whitcroft 52020a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 52030a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 5204773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 52053705ce5bSJoe Perches if (ERROR("SPACING", 52063705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 5207b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 52083705ce5bSJoe Perches $line_fixed = 1; 52093705ce5bSJoe Perches } 52100a920b5bSAndy Whitcroft } 5211773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 5212773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 52133705ce5bSJoe Perches if (ERROR("SPACING", 52143705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 5215b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 52163705ce5bSJoe Perches $line_fixed = 1; 52173705ce5bSJoe Perches } 5218653d4876SAndy Whitcroft } 5219773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 52203705ce5bSJoe Perches if (ERROR("SPACING", 52213705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 5222b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 52233705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 52243705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5225773647a0SAndy Whitcroft } 5226b34c648bSJoe Perches $line_fixed = 1; 52273705ce5bSJoe Perches } 52283705ce5bSJoe Perches } 52290a920b5bSAndy Whitcroft 52300a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 52319c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 52329c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 52339c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 5234c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 5235c2fdda0dSAndy Whitcroft $op eq '%') 52360a920b5bSAndy Whitcroft { 5237d2e025f3SJoe Perches if ($check) { 5238d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 5239d2e025f3SJoe Perches if (CHK("SPACING", 5240d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 5241d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5242d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5243d2e025f3SJoe Perches $line_fixed = 1; 5244d2e025f3SJoe Perches } 5245d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 5246d2e025f3SJoe Perches if (CHK("SPACING", 5247d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 5248d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 5249d2e025f3SJoe Perches $line_fixed = 1; 5250d2e025f3SJoe Perches } 5251d2e025f3SJoe Perches } 5252d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 52533705ce5bSJoe Perches if (ERROR("SPACING", 52543705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 5255b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5256b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 5257b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5258b34c648bSJoe Perches } 52593705ce5bSJoe Perches $line_fixed = 1; 52603705ce5bSJoe Perches } 52610a920b5bSAndy Whitcroft } 52620a920b5bSAndy Whitcroft 52631f65f947SAndy Whitcroft # A colon needs no spaces before when it is 52641f65f947SAndy Whitcroft # terminating a case value or a label. 52651f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 5266263afd39SChris Down if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) { 52673705ce5bSJoe Perches if (ERROR("SPACING", 52683705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 5269b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 52703705ce5bSJoe Perches $line_fixed = 1; 52713705ce5bSJoe Perches } 52721f65f947SAndy Whitcroft } 52731f65f947SAndy Whitcroft 52740a920b5bSAndy Whitcroft # All the others need spaces both sides. 5275cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 52761f65f947SAndy Whitcroft my $ok = 0; 52771f65f947SAndy Whitcroft 527822f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 52791f65f947SAndy Whitcroft if (($op eq '<' && 52801f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 52811f65f947SAndy Whitcroft ($op eq '>' && 52821f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 52831f65f947SAndy Whitcroft { 52841f65f947SAndy Whitcroft $ok = 1; 52851f65f947SAndy Whitcroft } 52861f65f947SAndy Whitcroft 5287e0df7e1fSJoe Perches # for asm volatile statements 5288e0df7e1fSJoe Perches # ignore a colon with another 5289e0df7e1fSJoe Perches # colon immediately before or after 5290e0df7e1fSJoe Perches if (($op eq ':') && 5291e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 5292e0df7e1fSJoe Perches $ok = 1; 5293e0df7e1fSJoe Perches } 5294e0df7e1fSJoe Perches 529584731623SJoe Perches # messages are ERROR, but ?: are CHK 52961f65f947SAndy Whitcroft if ($ok == 0) { 52970675a8fbSJean Delvare my $msg_level = \&ERROR; 52980675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 529984731623SJoe Perches 53000675a8fbSJean Delvare if (&{$msg_level}("SPACING", 53013705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 5302b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5303b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 5304b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5305b34c648bSJoe Perches } 53063705ce5bSJoe Perches $line_fixed = 1; 53073705ce5bSJoe Perches } 53080a920b5bSAndy Whitcroft } 530922f2a2efSAndy Whitcroft } 53104a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 53113705ce5bSJoe Perches 53123705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 53133705ce5bSJoe Perches 53143705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 53150a920b5bSAndy Whitcroft } 53163705ce5bSJoe Perches 53173705ce5bSJoe Perches if (($#elements % 2) == 0) { 53183705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 53193705ce5bSJoe Perches } 53203705ce5bSJoe Perches 5321194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 5322194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 53233705ce5bSJoe Perches } 53243705ce5bSJoe Perches 53253705ce5bSJoe Perches 53260a920b5bSAndy Whitcroft } 53270a920b5bSAndy Whitcroft 5328786b6326SJoe Perches# check for whitespace before a non-naked semicolon 5329d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 5330786b6326SJoe Perches if (WARN("SPACING", 5331786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 5332786b6326SJoe Perches $fix) { 5333194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 5334786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 5335786b6326SJoe Perches } 5336786b6326SJoe Perches } 5337786b6326SJoe Perches 5338f0a594c1SAndy Whitcroft# check for multiple assignments 5339f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 5340000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 5341000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 5342f0a594c1SAndy Whitcroft } 5343f0a594c1SAndy Whitcroft 534422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 534522f2a2efSAndy Whitcroft## # continuation. 534622f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 534722f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 534822f2a2efSAndy Whitcroft## 534922f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 5350e73d2715SDwaipayan Ray## # falsely report the parameters of functions. 535122f2a2efSAndy Whitcroft## my $ln = $line; 535222f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 535322f2a2efSAndy Whitcroft## } 535422f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 5355000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 5356000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 535722f2a2efSAndy Whitcroft## } 535822f2a2efSAndy Whitcroft## } 5359f0a594c1SAndy Whitcroft 53600a920b5bSAndy Whitcroft#need space before brace following if, while, etc 53616b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 53626ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 53633705ce5bSJoe Perches if (ERROR("SPACING", 53643705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 53653705ce5bSJoe Perches $fix) { 53666ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 53673705ce5bSJoe Perches } 5368de7d4f0eSAndy Whitcroft } 5369de7d4f0eSAndy Whitcroft 5370c4a62ef9SJoe Perches## # check for blank lines before declarations 5371c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 5372c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 5373c4a62ef9SJoe Perches## WARN("SPACING", 5374c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 5375c4a62ef9SJoe Perches## } 5376c4a62ef9SJoe Perches## 5377c4a62ef9SJoe Perches 5378de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 5379de7d4f0eSAndy Whitcroft# on the line 538094fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 5381d5e616fcSJoe Perches if (ERROR("SPACING", 5382d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 5383d5e616fcSJoe Perches $fix) { 5384194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5385d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 5386d5e616fcSJoe Perches } 53870a920b5bSAndy Whitcroft } 53880a920b5bSAndy Whitcroft 538922f2a2efSAndy Whitcroft# check spacing on square brackets 539022f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 53913705ce5bSJoe Perches if (ERROR("SPACING", 53923705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 53933705ce5bSJoe Perches $fix) { 5394194f66fcSJoe Perches $fixed[$fixlinenr] =~ 53953705ce5bSJoe Perches s/\[\s+/\[/; 53963705ce5bSJoe Perches } 539722f2a2efSAndy Whitcroft } 539822f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 53993705ce5bSJoe Perches if (ERROR("SPACING", 54003705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 54013705ce5bSJoe Perches $fix) { 5402194f66fcSJoe Perches $fixed[$fixlinenr] =~ 54033705ce5bSJoe Perches s/\s+\]/\]/; 54043705ce5bSJoe Perches } 540522f2a2efSAndy Whitcroft } 540622f2a2efSAndy Whitcroft 5407c45dcabdSAndy Whitcroft# check spacing on parentheses 54089c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 54099c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 54103705ce5bSJoe Perches if (ERROR("SPACING", 54113705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 54123705ce5bSJoe Perches $fix) { 5413194f66fcSJoe Perches $fixed[$fixlinenr] =~ 54143705ce5bSJoe Perches s/\(\s+/\(/; 54153705ce5bSJoe Perches } 541622f2a2efSAndy Whitcroft } 541713214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 5418c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 5419c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 54203705ce5bSJoe Perches if (ERROR("SPACING", 54213705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 54223705ce5bSJoe Perches $fix) { 5423194f66fcSJoe Perches $fixed[$fixlinenr] =~ 54243705ce5bSJoe Perches s/\s+\)/\)/; 54253705ce5bSJoe Perches } 542622f2a2efSAndy Whitcroft } 542722f2a2efSAndy Whitcroft 5428e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 5429e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 5430e2826fd0SJoe Perches 5431e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 5432ea4acbb1SJoe Perches my $var = $1; 5433ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5434ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 5435ea4acbb1SJoe Perches $fix) { 5436ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 5437ea4acbb1SJoe Perches } 5438ea4acbb1SJoe Perches } 5439ea4acbb1SJoe Perches 5440ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 5441ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 5442ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 5443ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 5444ea4acbb1SJoe Perches my $var = $2; 5445ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5446ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 5447ea4acbb1SJoe Perches $fix) { 5448ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 5449ea4acbb1SJoe Perches $var2 =~ s/\s//g; 5450ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 5451ea4acbb1SJoe Perches } 5452e2826fd0SJoe Perches } 5453e2826fd0SJoe Perches 545463b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 5455a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 5456a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 54575b57980dSJoe Perches $perl_version_ok && defined($stat) && 545863b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 545963b7c73eSJoe Perches my $if_stat = $1; 546063b7c73eSJoe Perches my $test = substr($2, 1, -1); 546163b7c73eSJoe Perches my $herectx; 546263b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 546363b7c73eSJoe Perches my $match = $1; 546463b7c73eSJoe Perches # avoid parentheses around potential macro args 546563b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 546663b7c73eSJoe Perches if (!defined($herectx)) { 546763b7c73eSJoe Perches $herectx = $here . "\n"; 546863b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 546963b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 547063b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 547163b7c73eSJoe Perches $herectx .= $rl . "\n"; 547263b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 547363b7c73eSJoe Perches } 547463b7c73eSJoe Perches } 547563b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 547663b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 547763b7c73eSJoe Perches } 547863b7c73eSJoe Perches } 547963b7c73eSJoe Perches 548069078651SJoe Perches# check that goto labels aren't indented (allow a single space indentation) 548169078651SJoe Perches# and ignore bitfield definitions like foo:1 548269078651SJoe Perches# Strictly, labels can have whitespace after the identifier and before the : 548369078651SJoe Perches# but this is not allowed here as many ?: uses would appear to be labels 548469078651SJoe Perches if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ && 548569078651SJoe Perches $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ && 548669078651SJoe Perches $sline !~ /^.\s+default:/) { 54873705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 54883705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 54893705ce5bSJoe Perches $fix) { 5490194f66fcSJoe Perches $fixed[$fixlinenr] =~ 54913705ce5bSJoe Perches s/^(.)\s+/$1/; 54923705ce5bSJoe Perches } 54930a920b5bSAndy Whitcroft } 54940a920b5bSAndy Whitcroft 549540873abaSJoe Perches# check if a statement with a comma should be two statements like: 549640873abaSJoe Perches# foo = bar(), /* comma should be semicolon */ 549740873abaSJoe Perches# bar = baz(); 549840873abaSJoe Perches if (defined($stat) && 549940873abaSJoe Perches $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { 550040873abaSJoe Perches my $cnt = statement_rawlines($stat); 550140873abaSJoe Perches my $herectx = get_stat_here($linenr, $cnt, $here); 550240873abaSJoe Perches WARN("SUSPECT_COMMA_SEMICOLON", 550340873abaSJoe Perches "Possible comma where semicolon could be used\n" . $herectx); 550440873abaSJoe Perches } 550540873abaSJoe Perches 55065b9553abSJoe Perches# return is not a function 5507507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 5508c45dcabdSAndy Whitcroft my $spacing = $1; 55095b57980dSJoe Perches if ($perl_version_ok && 55105b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 55115b9553abSJoe Perches my $value = $1; 55125b9553abSJoe Perches $value = deparenthesize($value); 55135b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 5514000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 5515000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 55165b9553abSJoe Perches } 5517c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 5518000d1cc1SJoe Perches ERROR("SPACING", 5519000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 5520c45dcabdSAndy Whitcroft } 5521c45dcabdSAndy Whitcroft } 5522507e5141SJoe Perches 5523b43ae21bSJoe Perches# unnecessary return in a void function 5524b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 5525b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 5526b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 5527b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 5528b43ae21bSJoe Perches $linenr >= 3 && 5529b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 5530b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 55319819cf25SJoe Perches WARN("RETURN_VOID", 5532b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 55339819cf25SJoe Perches } 55349819cf25SJoe Perches 5535189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 55365b57980dSJoe Perches if ($perl_version_ok && 5537189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 5538189248d8SJoe Perches my $openparens = $1; 5539189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 5540189248d8SJoe Perches my $msg = ""; 5541189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 5542189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 5543189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 5544189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 5545189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 5546189248d8SJoe Perches } 5547189248d8SJoe Perches } 5548189248d8SJoe Perches 5549c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 5550c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 5551c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 5552c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 55535b57980dSJoe Perches if ($perl_version_ok && 5554c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 5555c5595fa2SJoe Perches my $lead = $1; 5556c5595fa2SJoe Perches my $const = $2; 5557c5595fa2SJoe Perches my $comp = $3; 5558c5595fa2SJoe Perches my $to = $4; 5559c5595fa2SJoe Perches my $newcomp = $comp; 5560f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 5561c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 5562c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 5563c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 5564c5595fa2SJoe Perches $fix) { 5565c5595fa2SJoe Perches if ($comp eq "<") { 5566c5595fa2SJoe Perches $newcomp = ">"; 5567c5595fa2SJoe Perches } elsif ($comp eq "<=") { 5568c5595fa2SJoe Perches $newcomp = ">="; 5569c5595fa2SJoe Perches } elsif ($comp eq ">") { 5570c5595fa2SJoe Perches $newcomp = "<"; 5571c5595fa2SJoe Perches } elsif ($comp eq ">=") { 5572c5595fa2SJoe Perches $newcomp = "<="; 5573c5595fa2SJoe Perches } 5574c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 5575c5595fa2SJoe Perches } 5576c5595fa2SJoe Perches } 5577c5595fa2SJoe Perches 5578f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 5579f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 558053a3c448SAndy Whitcroft my $name = $1; 558146b85bf9SGuenter Roeck if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) { 5582000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 5583f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 558453a3c448SAndy Whitcroft } 558553a3c448SAndy Whitcroft } 5586c45dcabdSAndy Whitcroft 55870a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 55884a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 55893705ce5bSJoe Perches if (ERROR("SPACING", 55903705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 55913705ce5bSJoe Perches $fix) { 5592194f66fcSJoe Perches $fixed[$fixlinenr] =~ 55933705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 55943705ce5bSJoe Perches } 55950a920b5bSAndy Whitcroft } 55960a920b5bSAndy Whitcroft 5597f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 5598f5fe35ddSAndy Whitcroft# statements after the conditional. 5599170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 56003e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 56013e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 56023e469cdcSAndy Whitcroft if (!defined $stat); 5603170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 5604170d3a22SAndy Whitcroft $remain_next, $off_next); 5605170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 5606170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 5607170d3a22SAndy Whitcroft 5608170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 5609170d3a22SAndy Whitcroft # If the statement carries leading newlines, 5610170d3a22SAndy Whitcroft # then count those as offsets. 5611170d3a22SAndy Whitcroft my ($whitespace) = 5612170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 5613170d3a22SAndy Whitcroft my $offset = 5614170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 5615170d3a22SAndy Whitcroft 5616170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 5617170d3a22SAndy Whitcroft $offset} = 1; 5618170d3a22SAndy Whitcroft } 5619170d3a22SAndy Whitcroft } 5620170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 5621c11230f4SJoe Perches defined($stat) && defined($cond) && 5622170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 5623171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 5624481efd7bSJoe Perches my $fixed_assign_in_if = 0; 56258905a67cSAndy Whitcroft 5626b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 562765b64b3bSJoe Perches if (ERROR("ASSIGN_IN_IF", 562865b64b3bSJoe Perches "do not use assignment in if condition\n" . $herecurr) && 562965b64b3bSJoe Perches $fix && $perl_version_ok) { 563065b64b3bSJoe Perches if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { 563165b64b3bSJoe Perches my $space = $1; 563265b64b3bSJoe Perches my $not = $2; 563365b64b3bSJoe Perches my $statement = $3; 563465b64b3bSJoe Perches my $assigned = $4; 563565b64b3bSJoe Perches my $test = $8; 563665b64b3bSJoe Perches my $against = $9; 563765b64b3bSJoe Perches my $brace = $15; 563865b64b3bSJoe Perches fix_delete_line($fixlinenr, $rawline); 563965b64b3bSJoe Perches fix_insert_line($fixlinenr, "$space$statement;"); 564065b64b3bSJoe Perches my $newline = "${space}if ("; 564165b64b3bSJoe Perches $newline .= '!' if defined($not); 564265b64b3bSJoe Perches $newline .= '(' if (defined $not && defined($test) && defined($against)); 564365b64b3bSJoe Perches $newline .= "$assigned"; 564465b64b3bSJoe Perches $newline .= " $test $against" if (defined($test) && defined($against)); 564565b64b3bSJoe Perches $newline .= ')' if (defined $not && defined($test) && defined($against)); 564665b64b3bSJoe Perches $newline .= ')'; 564765b64b3bSJoe Perches $newline .= " {" if (defined($brace)); 564865b64b3bSJoe Perches fix_insert_line($fixlinenr + 1, $newline); 5649481efd7bSJoe Perches $fixed_assign_in_if = 1; 565065b64b3bSJoe Perches } 565165b64b3bSJoe Perches } 56528905a67cSAndy Whitcroft } 56538905a67cSAndy Whitcroft 56548905a67cSAndy Whitcroft # Find out what is on the end of the line after the 56558905a67cSAndy Whitcroft # conditional. 5656773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 56578905a67cSAndy Whitcroft $s =~ s/\n.*//g; 565813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 565953210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 566053210168SAndy Whitcroft $c !~ /}\s*while\s*/) 5661773647a0SAndy Whitcroft { 5662bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 5663bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 5664bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 566542bdf74cSHidetoshi Seto my $stat_real = ''; 5666bb44ad39SAndy Whitcroft 566742bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 566842bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 5669bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 5670bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 5671bb44ad39SAndy Whitcroft } 5672bb44ad39SAndy Whitcroft 5673481efd7bSJoe Perches if (ERROR("TRAILING_STATEMENTS", 5674481efd7bSJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real) && 5675481efd7bSJoe Perches !$fixed_assign_in_if && 5676481efd7bSJoe Perches $cond_lines == 0 && 5677481efd7bSJoe Perches $fix && $perl_version_ok && 5678481efd7bSJoe Perches $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) { 5679481efd7bSJoe Perches my $indent = $1; 5680481efd7bSJoe Perches my $test = $2; 5681481efd7bSJoe Perches my $rest = rtrim($4); 5682481efd7bSJoe Perches if ($rest =~ /;$/) { 5683481efd7bSJoe Perches $fixed[$fixlinenr] = "\+$indent$test"; 5684481efd7bSJoe Perches fix_insert_line($fixlinenr + 1, "$indent\t$rest"); 5685481efd7bSJoe Perches } 5686481efd7bSJoe Perches } 56878905a67cSAndy Whitcroft } 56888905a67cSAndy Whitcroft } 56898905a67cSAndy Whitcroft 569013214adfSAndy Whitcroft# Check for bitwise tests written as boolean 569113214adfSAndy Whitcroft if ($line =~ / 569213214adfSAndy Whitcroft (?: 569313214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 569413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 569513214adfSAndy Whitcroft (?:\&\&|\|\|) 569613214adfSAndy Whitcroft | 569713214adfSAndy Whitcroft (?:\&\&|\|\|) 569813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 569913214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 570013214adfSAndy Whitcroft )/x) 570113214adfSAndy Whitcroft { 5702000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 5703000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 570413214adfSAndy Whitcroft } 570513214adfSAndy Whitcroft 57068905a67cSAndy Whitcroft# if and else should not have general statements after it 570713214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 570813214adfSAndy Whitcroft my $s = $1; 570913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 571013214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5711000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5712000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 57130a920b5bSAndy Whitcroft } 571413214adfSAndy Whitcroft } 571539667782SAndy Whitcroft# if should not continue a brace 571639667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5717000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5718048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 571939667782SAndy Whitcroft $herecurr); 572039667782SAndy Whitcroft } 5721a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5722a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5723a1080bf8SAndy Whitcroft $line !~ /\G(?: 57243fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5725a1080bf8SAndy Whitcroft \s*return\s+ 5726a1080bf8SAndy Whitcroft )/xg) 5727a1080bf8SAndy Whitcroft { 5728000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5729000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5730a1080bf8SAndy Whitcroft } 57310a920b5bSAndy Whitcroft 57320a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 57330a920b5bSAndy Whitcroft # indent level to be relevant to each other. 57348b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 57350a920b5bSAndy Whitcroft $previndent == $indent) { 57368b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 57378b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 57388b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 57398b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 57408b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 57418b8856f4SJoe Perches my $fixedline = $prevrawline; 57428b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 57438b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 57448b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 57458b8856f4SJoe Perches } 57468b8856f4SJoe Perches $fixedline = $rawline; 57478b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 57488b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 57498b8856f4SJoe Perches } 57500a920b5bSAndy Whitcroft } 57510a920b5bSAndy Whitcroft 57528b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5753c2fdda0dSAndy Whitcroft $previndent == $indent) { 5754c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5755c2fdda0dSAndy Whitcroft 5756c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5757c2fdda0dSAndy Whitcroft # conditional. 5758773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5759c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5760c2fdda0dSAndy Whitcroft 5761c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 57628b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 57638b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 57648b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 57658b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 57668b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 57678b8856f4SJoe Perches my $fixedline = $prevrawline; 57688b8856f4SJoe Perches my $trailing = $rawline; 57698b8856f4SJoe Perches $trailing =~ s/^\+//; 57708b8856f4SJoe Perches $trailing = trim($trailing); 57718b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 57728b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 57738b8856f4SJoe Perches } 5774c2fdda0dSAndy Whitcroft } 5775c2fdda0dSAndy Whitcroft } 5776c2fdda0dSAndy Whitcroft 577795e2c602SJoe Perches#Specific variable tests 5778323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5779323c1260SJoe Perches my $var = $1; 578095e2c602SJoe Perches 578195e2c602SJoe Perches#CamelCase 5782807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5783be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 57844104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values 57854104a206SŁukasz Stelmach $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ && 578622735ce8SJoe Perches#Ignore Page<foo> variants 5787807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5788d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5789d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5790d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5791f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5792f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 5793f858e23aSAntonio Borneo while ($var =~ m{\b($Ident)}g) { 57947e781f67SJoe Perches my $word = $1; 57957e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5796d8b07710SJoe Perches if ($check) { 5797d8b07710SJoe Perches seed_camelcase_includes(); 5798d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5799d8b07710SJoe Perches seed_camelcase_file($realfile); 5800d8b07710SJoe Perches $camelcase_file_seeded = 1; 5801d8b07710SJoe Perches } 5802d8b07710SJoe Perches } 58037e781f67SJoe Perches if (!defined $camelcase{$word}) { 58047e781f67SJoe Perches $camelcase{$word} = 1; 5805be79794bSJoe Perches CHK("CAMELCASE", 58067e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 58077e781f67SJoe Perches } 5808323c1260SJoe Perches } 5809323c1260SJoe Perches } 58103445686aSJoe Perches } 58110a920b5bSAndy Whitcroft 58120a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5813d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5814d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5815d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5816d5e616fcSJoe Perches $fix) { 5817194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5818d5e616fcSJoe Perches } 58190a920b5bSAndy Whitcroft } 58200a920b5bSAndy Whitcroft 58210e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 58220e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5823c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5824e09dec48SAndy Whitcroft my $file = "$1.h"; 5825e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5826e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5827e09dec48SAndy Whitcroft $realfile ne $checkfile && 58287840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5829c45dcabdSAndy Whitcroft { 58300e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 58310e212e0aSFabian Frederick if ($asminclude > 0) { 5832e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5833000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5834000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5835e09dec48SAndy Whitcroft } else { 5836000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5837000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5838e09dec48SAndy Whitcroft } 58390a920b5bSAndy Whitcroft } 58400a920b5bSAndy Whitcroft } 58410e212e0aSFabian Frederick } 58420a920b5bSAndy Whitcroft 5843653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5844653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5845cf655043SAndy Whitcroft# in a known good container 5846b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5847b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5848d8aaf121SAndy Whitcroft my $ln = $linenr; 5849d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5850c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5851c45dcabdSAndy Whitcroft my $ctx = ''; 585208a2843eSJoe Perches my $has_flow_statement = 0; 585308a2843eSJoe Perches my $has_arg_concat = 0; 5854c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5855f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5856f74bd194SAndy Whitcroft $ctx = $dstat; 5857c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5858a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5859c45dcabdSAndy Whitcroft 586008a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 586162e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 586208a2843eSJoe Perches 5863f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5864f59b64bfSJoe Perches my $define_args = $1; 5865f59b64bfSJoe Perches my $define_stmt = $dstat; 5866f59b64bfSJoe Perches my @def_args = (); 5867f59b64bfSJoe Perches 5868f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5869f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5870f59b64bfSJoe Perches $define_args =~ s/\s*//g; 58718c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5872f59b64bfSJoe Perches @def_args = split(",", $define_args); 5873f59b64bfSJoe Perches } 5874f59b64bfSJoe Perches 5875292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5876c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5877c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5878c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5879c45dcabdSAndy Whitcroft 5880c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 58812e44e803SDwaipayan Ray while ($dstat =~ s/\([^\(\)]*\)/1u/ || 58822e44e803SDwaipayan Ray $dstat =~ s/\{[^\{\}]*\}/1u/ || 58832e44e803SDwaipayan Ray $dstat =~ s/.\[[^\[\]]*\]/1u/) 5884bf30d6edSAndy Whitcroft { 5885c45dcabdSAndy Whitcroft } 5886c45dcabdSAndy Whitcroft 5887342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 588833acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 588933acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5890e45bab8eSAndy Whitcroft { 5891e45bab8eSAndy Whitcroft } 5892e45bab8eSAndy Whitcroft 589342e15293SJoe Perches # Make asm volatile uses seem like a generic function 589442e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 589542e15293SJoe Perches 5896c45dcabdSAndy Whitcroft my $exceptions = qr{ 5897c45dcabdSAndy Whitcroft $Declare| 5898c45dcabdSAndy Whitcroft module_param_named| 5899a0a0a7a9SKees Cook MODULE_PARM_DESC| 5900c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5901c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5902383099fdSAndy Whitcroft __typeof__\(| 590322fd2d3eSStefani Seibold union| 590422fd2d3eSStefani Seibold struct| 5905ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 59066b10df42SVladimir Zapolskiy ^\"|\"$| 59076b10df42SVladimir Zapolskiy ^\[ 5908c45dcabdSAndy Whitcroft }x; 59095eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5910f59b64bfSJoe Perches 5911f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5912f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5913e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5914f59b64bfSJoe Perches 5915f74bd194SAndy Whitcroft if ($dstat ne '' && 5916f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5917f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 59183cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5919356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5920f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5921f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5922e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 5923dc3f4deeSStanislaw Gruszka $dstat !~ /^case\b/ && # case ... 592472f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 59252e44e803SDwaipayan Ray $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} 5926f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5927f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5928f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 59294e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5930f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5931c45dcabdSAndy Whitcroft { 5932e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5933e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5934e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5935e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5936f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5937f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5938f74bd194SAndy Whitcroft } else { 5939000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5940388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5941d8aaf121SAndy Whitcroft } 5942f59b64bfSJoe Perches 5943f59b64bfSJoe Perches } 59445207649bSJoe Perches 59455207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 59465207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 59475207649bSJoe Perches my $first = 1; 59485207649bSJoe Perches $define_stmt = ""; 59495207649bSJoe Perches foreach my $l (@stmt_array) { 59505207649bSJoe Perches $l =~ s/\\$//; 59515207649bSJoe Perches if ($first) { 59525207649bSJoe Perches $define_stmt = $l; 59535207649bSJoe Perches $first = 0; 59545207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 59555207649bSJoe Perches $define_stmt .= substr($l, 1); 59565207649bSJoe Perches } 59575207649bSJoe Perches } 59585207649bSJoe Perches $define_stmt =~ s/$;//g; 59595207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 59605207649bSJoe Perches $define_stmt = trim($define_stmt); 59615207649bSJoe Perches 5962f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5963f59b64bfSJoe Perches foreach my $arg (@def_args) { 5964f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 59659192d41aSJoe Perches next if ($arg =~ /^type$/i); 59667fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 59677b844345SVincent Mailhol $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 59687fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 59697fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5970d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5971f59b64bfSJoe Perches if ($use_cnt > 1) { 5972f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5973f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5974f59b64bfSJoe Perches } 59759192d41aSJoe Perches# check if any macro arguments may have other precedence issues 59767fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 59779192d41aSJoe Perches ((defined($1) && $1 ne ',') || 59789192d41aSJoe Perches (defined($2) && $2 ne ','))) { 59799192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 59809192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 59819192d41aSJoe Perches } 59820a920b5bSAndy Whitcroft } 59835023d347SJoe Perches 598408a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 598508a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 598608a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 598708a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5988e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 598908a2843eSJoe Perches 599008a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 599108a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 599208a2843eSJoe Perches } 599308a2843eSJoe Perches 5994481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 59955023d347SJoe Perches 59965023d347SJoe Perches } else { 59975023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5998481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5999481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 60005023d347SJoe Perches $line =~ /^\+.*\\$/) { 60015023d347SJoe Perches WARN("LINE_CONTINUATIONS", 60025023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 60035023d347SJoe Perches } 6004653d4876SAndy Whitcroft } 60050a920b5bSAndy Whitcroft 6006b13edf7fSJoe Perches# do {} while (0) macro tests: 6007b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 6008b13edf7fSJoe Perches# macro should not end with a semicolon 60095b57980dSJoe Perches if ($perl_version_ok && 6010b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 6011b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 6012b13edf7fSJoe Perches my $ln = $linenr; 6013b13edf7fSJoe Perches my $cnt = $realcnt; 6014b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 6015b13edf7fSJoe Perches my $ctx = ''; 6016b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 6017b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 6018b13edf7fSJoe Perches $ctx = $dstat; 6019b13edf7fSJoe Perches 6020b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 60211b36b201SJoe Perches $dstat =~ s/$;/ /g; 6022b13edf7fSJoe Perches 6023b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 6024b13edf7fSJoe Perches my $stmts = $2; 6025b13edf7fSJoe Perches my $semis = $3; 6026b13edf7fSJoe Perches 6027b13edf7fSJoe Perches $ctx =~ s/\n*$//; 6028b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 6029e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6030b13edf7fSJoe Perches 6031ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 6032ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 6033b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 6034b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 6035b13edf7fSJoe Perches } 6036b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 6037b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 6038b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 6039b13edf7fSJoe Perches } 6040f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 6041f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 6042f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 6043e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6044f5ef95b1SJoe Perches 6045f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 6046f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 6047b13edf7fSJoe Perches } 6048b13edf7fSJoe Perches } 6049b13edf7fSJoe Perches 6050f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 605113214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 605213214adfSAndy Whitcroft my ($level, $endln, @chunks) = 6053cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 605413214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 6055cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 6056cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 6057aad4f614SJoe Perches my @allowed = (); 6058aad4f614SJoe Perches my $allow = 0; 605913214adfSAndy Whitcroft my $seen = 0; 6060773647a0SAndy Whitcroft my $herectx = $here . "\n"; 6061cf655043SAndy Whitcroft my $ln = $linenr - 1; 606213214adfSAndy Whitcroft for my $chunk (@chunks) { 606313214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 606413214adfSAndy Whitcroft 6065773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 6066773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 6067773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 6068773647a0SAndy Whitcroft 6069aad4f614SJoe Perches $allowed[$allow] = 0; 6070773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 6071773647a0SAndy Whitcroft 6072773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 6073773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 6074773647a0SAndy Whitcroft 6075773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 6076cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 6077cf655043SAndy Whitcroft 6078773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 607913214adfSAndy Whitcroft 608013214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 608113214adfSAndy Whitcroft 6082aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 6083cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 6084cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 6085aad4f614SJoe Perches $allowed[$allow] = 1; 608613214adfSAndy Whitcroft } 608713214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 6088cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 6089aad4f614SJoe Perches $allowed[$allow] = 1; 609013214adfSAndy Whitcroft } 6091cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 6092cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 6093aad4f614SJoe Perches $allowed[$allow] = 1; 609413214adfSAndy Whitcroft } 6095aad4f614SJoe Perches $allow++; 609613214adfSAndy Whitcroft } 6097aad4f614SJoe Perches if ($seen) { 6098aad4f614SJoe Perches my $sum_allowed = 0; 6099aad4f614SJoe Perches foreach (@allowed) { 6100aad4f614SJoe Perches $sum_allowed += $_; 6101aad4f614SJoe Perches } 6102aad4f614SJoe Perches if ($sum_allowed == 0) { 6103000d1cc1SJoe Perches WARN("BRACES", 6104000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 6105aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 6106aad4f614SJoe Perches $seen != $allow) { 6107aad4f614SJoe Perches CHK("BRACES", 6108aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 6109aad4f614SJoe Perches } 611013214adfSAndy Whitcroft } 611113214adfSAndy Whitcroft } 611213214adfSAndy Whitcroft } 6113773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 611413214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 6115cf655043SAndy Whitcroft my $allowed = 0; 6116f0a594c1SAndy Whitcroft 6117cf655043SAndy Whitcroft # Check the pre-context. 6118cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 6119cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 6120cf655043SAndy Whitcroft $allowed = 1; 6121f0a594c1SAndy Whitcroft } 6122773647a0SAndy Whitcroft 6123773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 6124773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 6125773647a0SAndy Whitcroft 6126cf655043SAndy Whitcroft # Check the condition. 6127cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 6128773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 6129cf655043SAndy Whitcroft if (defined $cond) { 6130773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 6131cf655043SAndy Whitcroft } 6132cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 6133cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 6134cf655043SAndy Whitcroft $allowed = 1; 6135cf655043SAndy Whitcroft } 6136cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 6137cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 6138cf655043SAndy Whitcroft $allowed = 1; 6139cf655043SAndy Whitcroft } 6140cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 6141cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 6142cf655043SAndy Whitcroft $allowed = 1; 6143cf655043SAndy Whitcroft } 6144cf655043SAndy Whitcroft # Check the post-context. 6145cf655043SAndy Whitcroft if (defined $chunks[1]) { 6146cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 6147cf655043SAndy Whitcroft if (defined $cond) { 6148773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 6149cf655043SAndy Whitcroft } 6150cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 6151cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 6152cf655043SAndy Whitcroft $allowed = 1; 6153cf655043SAndy Whitcroft } 6154cf655043SAndy Whitcroft } 6155cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 6156f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 6157e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6158cf655043SAndy Whitcroft 6159000d1cc1SJoe Perches WARN("BRACES", 6160000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 6161f0a594c1SAndy Whitcroft } 6162f0a594c1SAndy Whitcroft } 6163f0a594c1SAndy Whitcroft 6164e4c5babdSJoe Perches# check for single line unbalanced braces 616595330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 616695330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 6167e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 6168e4c5babdSJoe Perches } 6169e4c5babdSJoe Perches 61700979ae66SJoe Perches# check for unnecessary blank lines around braces 617177b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 6172f8e58219SJoe Perches if (CHK("BRACES", 6173f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 6174f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 6175f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 6176f8e58219SJoe Perches } 61770979ae66SJoe Perches } 617877b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 6179f8e58219SJoe Perches if (CHK("BRACES", 6180f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 6181f8e58219SJoe Perches $fix) { 6182f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 6183f8e58219SJoe Perches } 61840979ae66SJoe Perches } 61850979ae66SJoe Perches 61864a0df2efSAndy Whitcroft# no volatiles please 61876c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 61886c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 6189000d1cc1SJoe Perches WARN("VOLATILE", 61908c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 61914a0df2efSAndy Whitcroft } 61924a0df2efSAndy Whitcroft 61935e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 61945e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 61955e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 61965e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 619733acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 61985e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 61995e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 62005e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 62015e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 62025e4f6ba5SJoe Perches $fix && 62035e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 62045e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 62055e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 62065e4f6ba5SJoe Perches my $comma_close = ""; 62075e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 62085e4f6ba5SJoe Perches $comma_close = $1; 62095e4f6ba5SJoe Perches } 62105e4f6ba5SJoe Perches 62115e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 62125e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 62135e4f6ba5SJoe Perches my $fixedline = $prevrawline; 62145e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 62155e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 62165e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 62175e4f6ba5SJoe Perches $fixedline = $rawline; 62185e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 62195e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 62205e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 62215e4f6ba5SJoe Perches } 62225e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 62235e4f6ba5SJoe Perches } 62245e4f6ba5SJoe Perches } 62255e4f6ba5SJoe Perches 62265e4f6ba5SJoe Perches# check for missing a space in a string concatenation 62275e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 62285e4f6ba5SJoe Perches WARN('MISSING_SPACE', 62295e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 62305e4f6ba5SJoe Perches } 62315e4f6ba5SJoe Perches 623277cb8546SJoe Perches# check for an embedded function name in a string when the function is known 6233e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 6234e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 6235e4b7d309SJoe Perches# function declarations 623677cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 623777cb8546SJoe Perches defined($context_function) && 6238e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 6239e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 624077cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 6241e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 624277cb8546SJoe Perches } 624377cb8546SJoe Perches 6244adb2da82SJoe Perches# check for unnecessary function tracing like uses 6245adb2da82SJoe Perches# This does not use $logFunctions because there are many instances like 6246adb2da82SJoe Perches# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions 6247adb2da82SJoe Perches if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) { 6248adb2da82SJoe Perches if (WARN("TRACING_LOGGING", 6249adb2da82SJoe Perches "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) && 6250adb2da82SJoe Perches $fix) { 6251adb2da82SJoe Perches fix_delete_line($fixlinenr, $rawline); 6252adb2da82SJoe Perches } 6253adb2da82SJoe Perches } 6254adb2da82SJoe Perches 62555e4f6ba5SJoe Perches# check for spaces before a quoted newline 62565e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 62575e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 62585e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 62595e4f6ba5SJoe Perches $fix) { 62605e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 62615e4f6ba5SJoe Perches } 62625e4f6ba5SJoe Perches 62635e4f6ba5SJoe Perches } 62645e4f6ba5SJoe Perches 6265f17dba4fSJoe Perches# concatenated string without spaces between elements 6266d2af5aa6SJoe Perches if ($line =~ /$String[A-Z_]/ || 6267d2af5aa6SJoe Perches ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) { 626879682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 626979682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 627079682c0cSJoe Perches $fix) { 627179682c0cSJoe Perches while ($line =~ /($String)/g) { 627279682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 627379682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 627479682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 627579682c0cSJoe Perches } 627679682c0cSJoe Perches } 6277f17dba4fSJoe Perches } 6278f17dba4fSJoe Perches 627990ad30e5SJoe Perches# uncoalesced string fragments 6280d2af5aa6SJoe Perches if ($line =~ /$String\s*[Lu]?"/) { 628179682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 628279682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 628379682c0cSJoe Perches $fix) { 628479682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 628579682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 628679682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 628779682c0cSJoe Perches } 628879682c0cSJoe Perches } 628990ad30e5SJoe Perches } 629090ad30e5SJoe Perches 6291522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 6292522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 6293522b837cSAlexey Dobriyan my $show_Z = 1; 62945e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 6295522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 62965e4f6ba5SJoe Perches $string =~ s/%%/__/g; 6297522b837cSAlexey Dobriyan # check for %L 6298522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 62995e4f6ba5SJoe Perches WARN("PRINTF_L", 6300522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 6301522b837cSAlexey Dobriyan $show_L = 0; 63025e4f6ba5SJoe Perches } 6303522b837cSAlexey Dobriyan # check for %Z 6304522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 6305522b837cSAlexey Dobriyan WARN("PRINTF_Z", 6306522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 6307522b837cSAlexey Dobriyan $show_Z = 0; 6308522b837cSAlexey Dobriyan } 6309522b837cSAlexey Dobriyan # check for 0x<decimal> 6310522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 6311522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 63126e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 63136e300757SJoe Perches } 63145e4f6ba5SJoe Perches } 63155e4f6ba5SJoe Perches 63165e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 63173f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 63185e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 63195e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 63205e4f6ba5SJoe Perches } 63215e4f6ba5SJoe Perches 632200df344fSAndy Whitcroft# warn about #if 0 6323c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 632460f89010SPrakruthi Deepak Heragu WARN("IF_0", 632560f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 632660f89010SPrakruthi Deepak Heragu } 632760f89010SPrakruthi Deepak Heragu 632860f89010SPrakruthi Deepak Heragu# warn about #if 1 632960f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 633060f89010SPrakruthi Deepak Heragu WARN("IF_1", 633160f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 63324a0df2efSAndy Whitcroft } 63334a0df2efSAndy Whitcroft 633403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 633503df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 6336100425deSJoe Perches my $tested = quotemeta($1); 6337100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 6338100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 6339100425deSJoe Perches my $func = $1; 6340100425deSJoe Perches if (WARN('NEEDLESS_IF', 6341100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 6342100425deSJoe Perches $fix) { 6343100425deSJoe Perches my $do_fix = 1; 6344100425deSJoe Perches my $leading_tabs = ""; 6345100425deSJoe Perches my $new_leading_tabs = ""; 6346100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 6347100425deSJoe Perches $leading_tabs = $1; 6348100425deSJoe Perches } else { 6349100425deSJoe Perches $do_fix = 0; 6350100425deSJoe Perches } 6351100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 6352100425deSJoe Perches $new_leading_tabs = $1; 6353100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 6354100425deSJoe Perches $do_fix = 0; 6355100425deSJoe Perches } 6356100425deSJoe Perches } else { 6357100425deSJoe Perches $do_fix = 0; 6358100425deSJoe Perches } 6359100425deSJoe Perches if ($do_fix) { 6360100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 6361100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 6362100425deSJoe Perches } 6363100425deSJoe Perches } 63644c432a8fSGreg Kroah-Hartman } 63654c432a8fSGreg Kroah-Hartman } 6366f0a594c1SAndy Whitcroft 6367ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 6368ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 6369ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 6370ebfdc409SJoe Perches (defined $1 || defined $3) && 6371ebfdc409SJoe Perches $linenr > 3) { 6372ebfdc409SJoe Perches my $testval = $2; 6373ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 6374ebfdc409SJoe Perches 6375ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 6376ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 6377ebfdc409SJoe Perches 6378e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 6379e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 6380ebfdc409SJoe Perches WARN("OOM_MESSAGE", 6381ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 6382ebfdc409SJoe Perches } 6383ebfdc409SJoe Perches } 6384ebfdc409SJoe Perches 6385f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 6386dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 6387f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 6388f78d98f6SJoe Perches my $level = $1; 6389f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 6390f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 6391f78d98f6SJoe Perches $fix) { 6392f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 6393f78d98f6SJoe Perches } 6394f78d98f6SJoe Perches } 6395f78d98f6SJoe Perches 639645c55e92SJoe Perches# check for logging continuations 639745c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 639845c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 639945c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 640045c55e92SJoe Perches } 640145c55e92SJoe Perches 640270eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions 640370eb2275SDwaipayan Ray if (defined $stat && 640470eb2275SDwaipayan Ray $line =~ /\b$logFunctions\s*\(/ && 640570eb2275SDwaipayan Ray index($stat, '"') >= 0) { 640670eb2275SDwaipayan Ray my $lc = $stat =~ tr@\n@@; 640770eb2275SDwaipayan Ray $lc = $lc + $linenr; 640870eb2275SDwaipayan Ray my $stat_real = get_stat_real($linenr, $lc); 640970eb2275SDwaipayan Ray pos($stat_real) = index($stat_real, '"'); 641070eb2275SDwaipayan Ray while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { 641170eb2275SDwaipayan Ray my $pspec = $1; 641270eb2275SDwaipayan Ray my $h = $2; 641370eb2275SDwaipayan Ray my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; 641470eb2275SDwaipayan Ray if (WARN("UNNECESSARY_MODIFIER", 641570eb2275SDwaipayan Ray "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && 641670eb2275SDwaipayan Ray $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { 641770eb2275SDwaipayan Ray my $nspec = $pspec; 641870eb2275SDwaipayan Ray $nspec =~ s/h//g; 641970eb2275SDwaipayan Ray $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; 642070eb2275SDwaipayan Ray } 642170eb2275SDwaipayan Ray } 642270eb2275SDwaipayan Ray } 642370eb2275SDwaipayan Ray 6424abb08a53SJoe Perches# check for mask then right shift without a parentheses 64255b57980dSJoe Perches if ($perl_version_ok && 6426abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 6427abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 6428abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 6429abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 6430abb08a53SJoe Perches } 6431abb08a53SJoe Perches 6432b75ac618SJoe Perches# check for pointer comparisons to NULL 64335b57980dSJoe Perches if ($perl_version_ok) { 6434b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 6435b75ac618SJoe Perches my $val = $1; 6436b75ac618SJoe Perches my $equal = "!"; 6437b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 6438b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 6439b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 6440b75ac618SJoe Perches $fix) { 6441b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 6442b75ac618SJoe Perches } 6443b75ac618SJoe Perches } 6444b75ac618SJoe Perches } 6445b75ac618SJoe Perches 64468716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 64478716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 64488716de38SJoe Perches my $attr = $1; 64498716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 64508716de38SJoe Perches my $ptr = $1; 64518716de38SJoe Perches my $var = $2; 64528716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 64538716de38SJoe Perches ERROR("MISPLACED_INIT", 64548716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 64558716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 64568716de38SJoe Perches WARN("MISPLACED_INIT", 64578716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 64588716de38SJoe Perches $fix) { 6459194f66fcSJoe Perches $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; 64608716de38SJoe Perches } 64618716de38SJoe Perches } 64628716de38SJoe Perches } 64638716de38SJoe Perches 6464e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 6465e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 6466e970b884SJoe Perches my $attr = $1; 6467e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 6468e970b884SJoe Perches my $attr_prefix = $1; 6469e970b884SJoe Perches my $attr_type = $2; 6470e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 6471e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 6472e970b884SJoe Perches $fix) { 6473194f66fcSJoe Perches $fixed[$fixlinenr] =~ 6474e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 6475e970b884SJoe Perches } 6476e970b884SJoe Perches } 6477e970b884SJoe Perches 6478e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 6479e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 6480e970b884SJoe Perches my $attr = $1; 6481e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 6482e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 6483e970b884SJoe Perches $fix) { 6484194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 6485e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 6486e970b884SJoe Perches $lead = rtrim($1); 6487e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 6488e970b884SJoe Perches $lead = "${lead}const "; 6489194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 6490e970b884SJoe Perches } 6491e970b884SJoe Perches } 6492e970b884SJoe Perches 6493c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 6494c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 6495c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 6496c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 6497c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 6498c17893c7SJoe Perches $fix) { 6499c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 6500c17893c7SJoe Perches } 6501c17893c7SJoe Perches } 6502c17893c7SJoe Perches 6503fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 6504fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 6505fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 6506fbdb8138SJoe Perches my $constant_func = $1; 6507fbdb8138SJoe Perches my $func = $constant_func; 6508fbdb8138SJoe Perches $func =~ s/^__constant_//; 6509fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 6510fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 6511fbdb8138SJoe Perches $fix) { 6512194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 6513fbdb8138SJoe Perches } 6514fbdb8138SJoe Perches } 6515fbdb8138SJoe Perches 65161a15a250SPatrick Pannuto# prefer usleep_range over udelay 651737581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 651843c1d77cSJoe Perches my $delay = $1; 65191a15a250SPatrick Pannuto # ignore udelay's < 10, however 652043c1d77cSJoe Perches if (! ($delay < 10) ) { 6521000d1cc1SJoe Perches CHK("USLEEP_RANGE", 6522458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 652343c1d77cSJoe Perches } 652443c1d77cSJoe Perches if ($delay > 2000) { 652543c1d77cSJoe Perches WARN("LONG_UDELAY", 652643c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 65271a15a250SPatrick Pannuto } 65281a15a250SPatrick Pannuto } 65291a15a250SPatrick Pannuto 653009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 653109ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 653209ef8725SPatrick Pannuto if ($1 < 20) { 6533000d1cc1SJoe Perches WARN("MSLEEP", 6534458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 653509ef8725SPatrick Pannuto } 653609ef8725SPatrick Pannuto } 653709ef8725SPatrick Pannuto 653836ec1939SJoe Perches# check for comparisons of jiffies 653936ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 654036ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 654136ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 654236ec1939SJoe Perches } 654336ec1939SJoe Perches 65449d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 65459d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 65469d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 65479d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 65489d7a34a5SJoe Perches } 65499d7a34a5SJoe Perches 655000df344fSAndy Whitcroft# warn about #ifdefs in C files 6551c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 655200df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 655300df344fSAndy Whitcroft# print "$herecurr"; 655400df344fSAndy Whitcroft# $clean = 0; 655500df344fSAndy Whitcroft# } 655600df344fSAndy Whitcroft 655722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 6558c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 65593705ce5bSJoe Perches if (ERROR("SPACING", 65603705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 65613705ce5bSJoe Perches $fix) { 6562194f66fcSJoe Perches $fixed[$fixlinenr] =~ 65633705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 65643705ce5bSJoe Perches } 65653705ce5bSJoe Perches 656622f2a2efSAndy Whitcroft } 656722f2a2efSAndy Whitcroft 65684a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 6569171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 6570171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 65714a0df2efSAndy Whitcroft my $which = $1; 65724a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6573000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 6574000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 65754a0df2efSAndy Whitcroft } 65764a0df2efSAndy Whitcroft } 65774a0df2efSAndy Whitcroft# check for memory barriers without a comment. 6578402c2553SMichael S. Tsirkin 6579402c2553SMichael S. Tsirkin my $barriers = qr{ 6580402c2553SMichael S. Tsirkin mb| 6581402c2553SMichael S. Tsirkin rmb| 6582ad83ec6cSWill Deacon wmb 6583402c2553SMichael S. Tsirkin }x; 6584402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 6585402c2553SMichael S. Tsirkin mb__before_atomic| 6586402c2553SMichael S. Tsirkin mb__after_atomic| 6587402c2553SMichael S. Tsirkin store_release| 6588402c2553SMichael S. Tsirkin load_acquire| 6589402c2553SMichael S. Tsirkin store_mb| 6590402c2553SMichael S. Tsirkin (?:$barriers) 6591402c2553SMichael S. Tsirkin }x; 6592402c2553SMichael S. Tsirkin my $all_barriers = qr{ 6593402c2553SMichael S. Tsirkin (?:$barriers)| 659443e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 659543e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 6596402c2553SMichael S. Tsirkin }x; 6597402c2553SMichael S. Tsirkin 6598402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 65994a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6600c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 6601000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 66024a0df2efSAndy Whitcroft } 66034a0df2efSAndy Whitcroft } 66043ad81779SPaul E. McKenney 6605f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 6606f4073b0fSMichael S. Tsirkin 6607f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 6608f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 6609f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 6610f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 6611f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 6612f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 6613f4073b0fSMichael S. Tsirkin } 6614f4073b0fSMichael S. Tsirkin 6615cb426e99SJoe Perches# check for waitqueue_active without a comment. 6616cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 6617cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 6618cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 6619cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 6620cb426e99SJoe Perches } 6621cb426e99SJoe Perches } 66223ad81779SPaul E. McKenney 66235099a722SMarco Elver# check for data_race without a comment. 66245099a722SMarco Elver if ($line =~ /\bdata_race\s*\(/) { 66255099a722SMarco Elver if (!ctx_has_comment($first_line, $linenr)) { 66265099a722SMarco Elver WARN("DATA_RACE", 66275099a722SMarco Elver "data_race without comment\n" . $herecurr); 66285099a722SMarco Elver } 66295099a722SMarco Elver } 66305099a722SMarco Elver 66314a0df2efSAndy Whitcroft# check of hardware specific defines 6632c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 6633000d1cc1SJoe Perches CHK("ARCH_DEFINES", 6634000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 66350a920b5bSAndy Whitcroft } 6636653d4876SAndy Whitcroft 6637596ed45bSJoe Perches# check that the storage class is not after a type 6638596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 6639000d1cc1SJoe Perches WARN("STORAGE_CLASS", 6640596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 6641596ed45bSJoe Perches } 6642596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 6643596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 6644596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 6645596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 6646596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 6647596ed45bSJoe Perches WARN("STORAGE_CLASS", 6648596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 6649d4977c78STobias Klauser } 6650d4977c78STobias Klauser 6651de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 6652de7d4f0eSAndy Whitcroft# storage class and type. 66539c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 66549c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 6655000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 6656000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 6657de7d4f0eSAndy Whitcroft } 6658de7d4f0eSAndy Whitcroft 66598905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 66602b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 66612b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 6662d5e616fcSJoe Perches if (WARN("INLINE", 6663d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 6664d5e616fcSJoe Perches $fix) { 6665194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 6666d5e616fcSJoe Perches 6667d5e616fcSJoe Perches } 66688905a67cSAndy Whitcroft } 66698905a67cSAndy Whitcroft 66707ebe1d17SDwaipayan Ray# Check for compiler attributes 66712b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 66727ebe1d17SDwaipayan Ray $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) { 66737ebe1d17SDwaipayan Ray my $attr = $1; 66747ebe1d17SDwaipayan Ray $attr =~ s/\s*\(\s*(.*)\)\s*/$1/; 66757ebe1d17SDwaipayan Ray 66767ebe1d17SDwaipayan Ray my %attr_list = ( 66770830aab0SJoe Perches "alias" => "__alias", 66787ebe1d17SDwaipayan Ray "aligned" => "__aligned", 66797ebe1d17SDwaipayan Ray "always_inline" => "__always_inline", 66807ebe1d17SDwaipayan Ray "assume_aligned" => "__assume_aligned", 66817ebe1d17SDwaipayan Ray "cold" => "__cold", 66827ebe1d17SDwaipayan Ray "const" => "__attribute_const__", 66837ebe1d17SDwaipayan Ray "copy" => "__copy", 66847ebe1d17SDwaipayan Ray "designated_init" => "__designated_init", 66857ebe1d17SDwaipayan Ray "externally_visible" => "__visible", 66867ebe1d17SDwaipayan Ray "format" => "printf|scanf", 66877ebe1d17SDwaipayan Ray "gnu_inline" => "__gnu_inline", 66887ebe1d17SDwaipayan Ray "malloc" => "__malloc", 66897ebe1d17SDwaipayan Ray "mode" => "__mode", 66907ebe1d17SDwaipayan Ray "no_caller_saved_registers" => "__no_caller_saved_registers", 66917ebe1d17SDwaipayan Ray "noclone" => "__noclone", 66927ebe1d17SDwaipayan Ray "noinline" => "noinline", 66937ebe1d17SDwaipayan Ray "nonstring" => "__nonstring", 66947ebe1d17SDwaipayan Ray "noreturn" => "__noreturn", 66957ebe1d17SDwaipayan Ray "packed" => "__packed", 66967ebe1d17SDwaipayan Ray "pure" => "__pure", 6697339f29d9SJoe Perches "section" => "__section", 66980830aab0SJoe Perches "used" => "__used", 66990830aab0SJoe Perches "weak" => "__weak" 67007ebe1d17SDwaipayan Ray ); 67017ebe1d17SDwaipayan Ray 67027ebe1d17SDwaipayan Ray while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) { 6703339f29d9SJoe Perches my $orig_attr = $1; 67047ebe1d17SDwaipayan Ray my $params = ''; 67057ebe1d17SDwaipayan Ray $params = $2 if defined($2); 6706339f29d9SJoe Perches my $curr_attr = $orig_attr; 67077ebe1d17SDwaipayan Ray $curr_attr =~ s/^[\s_]+|[\s_]+$//g; 67087ebe1d17SDwaipayan Ray if (exists($attr_list{$curr_attr})) { 6709339f29d9SJoe Perches my $new = $attr_list{$curr_attr}; 67107ebe1d17SDwaipayan Ray if ($curr_attr eq "format" && $params) { 67117ebe1d17SDwaipayan Ray $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/; 6712339f29d9SJoe Perches $new = "__$1\($2"; 67137ebe1d17SDwaipayan Ray } else { 6714339f29d9SJoe Perches $new = "$new$params"; 67157ebe1d17SDwaipayan Ray } 67167ebe1d17SDwaipayan Ray if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 6717339f29d9SJoe Perches "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) && 67187ebe1d17SDwaipayan Ray $fix) { 6719339f29d9SJoe Perches my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?'; 6720339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/$remove//; 6721339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/; 6722339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/; 6723339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//; 67247ebe1d17SDwaipayan Ray } 672539b7e287SJoe Perches } 6726462811d9SJoe Perches } 6727462811d9SJoe Perches 67287ebe1d17SDwaipayan Ray # Check for __attribute__ unused, prefer __always_unused or __maybe_unused 67297ebe1d17SDwaipayan Ray if ($attr =~ /^_*unused/) { 67307ebe1d17SDwaipayan Ray WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 67317ebe1d17SDwaipayan Ray "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr); 6732d5e616fcSJoe Perches } 67336061d949SJoe Perches } 67346061d949SJoe Perches 6735619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 67365b57980dSJoe Perches if ($perl_version_ok && 6737619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 6738619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6739619a908aSJoe Perches $line =~ /\b__weak\b/)) { 6740619a908aSJoe Perches ERROR("WEAK_DECLARATION", 6741619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 6742619a908aSJoe Perches } 6743619a908aSJoe Perches 6744fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 6745e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6746fd39f904STomas Winkler $realfile !~ m@\btools/@ && 6747e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6748e6176fa4SJoe Perches my $type = $1; 6749e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 6750e6176fa4SJoe Perches $type = $1; 6751e6176fa4SJoe Perches my $kernel_type = 'u'; 6752e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 6753e6176fa4SJoe Perches $type =~ /(\d+)/; 6754e6176fa4SJoe Perches $kernel_type .= $1; 6755e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 6756e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6757e6176fa4SJoe Perches $fix) { 6758e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6759e6176fa4SJoe Perches } 6760e6176fa4SJoe Perches } 6761e6176fa4SJoe Perches } 6762e6176fa4SJoe Perches 6763938224b5SJoe Perches# check for cast of C90 native int or longer types constants 6764938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6765938224b5SJoe Perches my $cast = $1; 6766938224b5SJoe Perches my $const = $2; 6767938224b5SJoe Perches my $suffix = ""; 6768938224b5SJoe Perches my $newconst = $const; 6769938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 6770938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6771938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6772938224b5SJoe Perches $suffix .= 'LL'; 6773938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6774938224b5SJoe Perches $suffix .= 'L'; 6775938224b5SJoe Perches } 67760972b8bfSJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 67770972b8bfSJoe Perches "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) && 67780972b8bfSJoe Perches $fix) { 6779938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6780938224b5SJoe Perches } 6781938224b5SJoe Perches } 6782938224b5SJoe Perches 67838f53a9b8SJoe Perches# check for sizeof(&) 67848f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6785000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6786000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 67878f53a9b8SJoe Perches } 67888f53a9b8SJoe Perches 678966c80b60SJoe Perches# check for sizeof without parenthesis 679066c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6791d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6792d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6793d5e616fcSJoe Perches $fix) { 6794194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6795d5e616fcSJoe Perches } 679666c80b60SJoe Perches } 679766c80b60SJoe Perches 679888982feaSJoe Perches# check for struct spinlock declarations 679988982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 680088982feaSJoe Perches WARN("USE_SPINLOCK_T", 680188982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 680288982feaSJoe Perches } 680388982feaSJoe Perches 6804a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 680506668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6806a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6807caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6808caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6809d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6810d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6811d5e616fcSJoe Perches $fix) { 6812194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6813d5e616fcSJoe Perches } 6814a6962d72SJoe Perches } 6815a6962d72SJoe Perches } 6816a6962d72SJoe Perches 68170b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 68185b57980dSJoe Perches if ($perl_version_ok && 68190b523769SJoe Perches defined $stat && 68200b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 68210b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6822e3c6bc95STobin C. Harding my $stat_real; 6823e3c6bc95STobin C. Harding 68240b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 68250b523769SJoe Perches $lc = $lc + $linenr; 68260b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6827ffe07513SJoe Perches my $specifier; 6828ffe07513SJoe Perches my $extension; 68293bd32d6aSSakari Ailus my $qualifier; 6830ffe07513SJoe Perches my $bad_specifier = ""; 68310b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 68320b523769SJoe Perches $fmt =~ s/%%//g; 6833e3c6bc95STobin C. Harding 68343bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6835e3c6bc95STobin C. Harding $specifier = $1; 6836e3c6bc95STobin C. Harding $extension = $2; 68373bd32d6aSSakari Ailus $qualifier = $3; 6838af612e43SSakari Ailus if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 68393bd32d6aSSakari Ailus ($extension eq "f" && 6840af612e43SSakari Ailus defined $qualifier && $qualifier !~ /^w/) || 6841af612e43SSakari Ailus ($extension eq "4" && 6842af612e43SSakari Ailus defined $qualifier && $qualifier !~ /^cc/)) { 6843e3c6bc95STobin C. Harding $bad_specifier = $specifier; 68440b523769SJoe Perches last; 68450b523769SJoe Perches } 6846e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6847e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6848e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 68490b523769SJoe Perches } 6850e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6851e3c6bc95STobin C. Harding "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"); 6852e3c6bc95STobin C. Harding } 6853e3c6bc95STobin C. Harding } 6854e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 68552a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6856de48fa1aSMiguel Ojeda my $msg_level = \&WARN; 68571df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 68581df7338aSSergey Senozhatsky my $use = ""; 6859e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 68601df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6861e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 6862de48fa1aSMiguel Ojeda } elsif ($bad_specifier =~ /pA/) { 6863de48fa1aSMiguel Ojeda $use = " - '%pA' is only intended to be used from Rust code"; 6864de48fa1aSMiguel Ojeda $msg_level = \&ERROR; 68651df7338aSSergey Senozhatsky } 68662a9f9d85STobin C. Harding 6867de48fa1aSMiguel Ojeda &{$msg_level}("VSPRINTF_POINTER_EXTENSION", 6868e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6869e3c6bc95STobin C. Harding } 68700b523769SJoe Perches } 68710b523769SJoe Perches } 68720b523769SJoe Perches 6873554e165cSAndy Whitcroft# Check for misused memsets 68745b57980dSJoe Perches if ($perl_version_ok && 6875d1fe9c09SJoe Perches defined $stat && 68769e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6877554e165cSAndy Whitcroft 6878d7c76ba7SJoe Perches my $ms_addr = $2; 6879d1fe9c09SJoe Perches my $ms_val = $7; 6880d1fe9c09SJoe Perches my $ms_size = $12; 6881d7c76ba7SJoe Perches 6882554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6883554e165cSAndy Whitcroft ERROR("MEMSET", 6884d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6885554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6886554e165cSAndy Whitcroft WARN("MEMSET", 6887d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6888d7c76ba7SJoe Perches } 6889d7c76ba7SJoe Perches } 6890d7c76ba7SJoe Perches 689198a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 68925b57980dSJoe Perches# if ($perl_version_ok && 6893f333195dSJoe Perches# defined $stat && 6894f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6895f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6896f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6897f333195dSJoe Perches# $fix) { 6898f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6899f333195dSJoe Perches# } 6900f333195dSJoe Perches# } 690198a9bba5SJoe Perches 6902b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 69035b57980dSJoe Perches# if ($perl_version_ok && 6904f333195dSJoe Perches# defined $stat && 6905f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6906f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6907f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6908f333195dSJoe Perches# } 6909b6117d17SMateusz Kulikowski 69108617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 69118617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 69125b57980dSJoe Perches# if ($perl_version_ok && 6913f333195dSJoe Perches# defined $stat && 6914f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6915f333195dSJoe Perches# 6916f333195dSJoe Perches# my $ms_val = $7; 6917f333195dSJoe Perches# 6918f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6919f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6920f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6921f333195dSJoe Perches# $fix) { 6922f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6923f333195dSJoe Perches# } 6924f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6925f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6926f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6927f333195dSJoe Perches# $fix) { 6928f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6929f333195dSJoe Perches# } 6930f333195dSJoe Perches# } 6931f333195dSJoe Perches# } 69328617cd09SMateusz Kulikowski 69335dbdb2d8SJoe Perches# strlcpy uses that should likely be strscpy 69345dbdb2d8SJoe Perches if ($line =~ /\bstrlcpy\s*\(/) { 69355dbdb2d8SJoe Perches WARN("STRLCPY", 69365dbdb2d8SJoe Perches "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr); 69375dbdb2d8SJoe Perches } 69385dbdb2d8SJoe Perches 6939d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 69405b57980dSJoe Perches if ($perl_version_ok && 6941d1fe9c09SJoe Perches defined $stat && 6942d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6943d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6944d7c76ba7SJoe Perches my $call = $1; 6945d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6946d7c76ba7SJoe Perches my $arg1 = $3; 6947d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6948d1fe9c09SJoe Perches my $arg2 = $8; 6949d7c76ba7SJoe Perches my $cast; 6950d7c76ba7SJoe Perches 6951d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6952d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6953d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6954d7c76ba7SJoe Perches $cast = $cast1; 6955d7c76ba7SJoe Perches } else { 6956d7c76ba7SJoe Perches $cast = $cast2; 6957d7c76ba7SJoe Perches } 6958d7c76ba7SJoe Perches WARN("MINMAX", 6959d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6960554e165cSAndy Whitcroft } 6961554e165cSAndy Whitcroft } 6962554e165cSAndy Whitcroft 69634a273195SJoe Perches# check usleep_range arguments 69645b57980dSJoe Perches if ($perl_version_ok && 69654a273195SJoe Perches defined $stat && 69664a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 69674a273195SJoe Perches my $min = $1; 69684a273195SJoe Perches my $max = $7; 69694a273195SJoe Perches if ($min eq $max) { 69704a273195SJoe Perches WARN("USLEEP_RANGE", 6971458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 69724a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 69734a273195SJoe Perches $min > $max) { 69744a273195SJoe Perches WARN("USLEEP_RANGE", 6975458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 69764a273195SJoe Perches } 69774a273195SJoe Perches } 69784a273195SJoe Perches 6979823b794cSJoe Perches# check for naked sscanf 69805b57980dSJoe Perches if ($perl_version_ok && 6981823b794cSJoe Perches defined $stat && 69826c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6983823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6984823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6985823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6986823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6987823b794cSJoe Perches $lc = $lc + $linenr; 69882a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6989823b794cSJoe Perches WARN("NAKED_SSCANF", 6990823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6991823b794cSJoe Perches } 6992823b794cSJoe Perches 6993afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 69945b57980dSJoe Perches if ($perl_version_ok && 6995afc819abSJoe Perches defined $stat && 6996afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6997afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6998afc819abSJoe Perches $lc = $lc + $linenr; 69992a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 7000afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 7001afc819abSJoe Perches my $format = $6; 7002afc819abSJoe Perches my $count = $format =~ tr@%@%@; 7003afc819abSJoe Perches if ($count == 1 && 7004afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 7005afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 7006afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 7007afc819abSJoe Perches } 7008afc819abSJoe Perches } 7009afc819abSJoe Perches } 7010afc819abSJoe Perches 701170dc8a48SJoe Perches# check for new externs in .h files. 701270dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 701370dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 7014d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 701570dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 701670dc8a48SJoe Perches $fix) { 7017194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 701870dc8a48SJoe Perches } 701970dc8a48SJoe Perches } 702070dc8a48SJoe Perches 7021de7d4f0eSAndy Whitcroft# check for new externs in .c files. 7022171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 7023c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 7024171ae1a4SAndy Whitcroft { 7025c45dcabdSAndy Whitcroft my $function_name = $1; 7026c45dcabdSAndy Whitcroft my $paren_space = $2; 7027171ae1a4SAndy Whitcroft 7028171ae1a4SAndy Whitcroft my $s = $stat; 7029171ae1a4SAndy Whitcroft if (defined $cond) { 7030171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 7031171ae1a4SAndy Whitcroft } 7032d8b44b58SKees Cook if ($s =~ /^\s*;/) 7033c45dcabdSAndy Whitcroft { 7034000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 7035000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 7036de7d4f0eSAndy Whitcroft } 7037de7d4f0eSAndy Whitcroft 7038171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 7039000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 7040000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 7041171ae1a4SAndy Whitcroft } 70429c9ba34eSAndy Whitcroft 70439c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 70449c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 70459c9ba34eSAndy Whitcroft { 7046000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 7047000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 7048171ae1a4SAndy Whitcroft } 7049171ae1a4SAndy Whitcroft 7050a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 7051a0ad7596SJoe Perches if (defined $stat && 7052d8b44b58SKees Cook $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 7053d8b44b58SKees Cook $1 ne "void") { 7054d8b44b58SKees Cook my $args = trim($1); 7055ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 7056ca0d8929SJoe Perches my $arg = trim($1); 7057d8b44b58SKees Cook if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 7058ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 7059ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 7060ca0d8929SJoe Perches } 7061ca0d8929SJoe Perches } 7062ca0d8929SJoe Perches } 7063ca0d8929SJoe Perches 7064a0ad7596SJoe Perches# check for function definitions 70655b57980dSJoe Perches if ($perl_version_ok && 7066a0ad7596SJoe Perches defined $stat && 7067a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 7068a0ad7596SJoe Perches $context_function = $1; 7069a0ad7596SJoe Perches 7070a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 7071a0ad7596SJoe Perches my $ok = 0; 7072a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 7073a0ad7596SJoe Perches my $herectx = $here . "\n"; 7074a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 7075a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 7076a0ad7596SJoe Perches $herectx .= $rl . "\n"; 7077a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 7078a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 7079a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 7080a0ad7596SJoe Perches } 7081a0ad7596SJoe Perches if (!$ok) { 7082a0ad7596SJoe Perches ERROR("OPEN_BRACE", 7083a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 7084a0ad7596SJoe Perches } 7085a0ad7596SJoe Perches } 7086a0ad7596SJoe Perches 7087de7d4f0eSAndy Whitcroft# checks for new __setup's 7088de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 7089de7d4f0eSAndy Whitcroft my $name = $1; 7090de7d4f0eSAndy Whitcroft 7091de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 7092000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 70932581ac7cSTim Froidcoeur "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); 7094de7d4f0eSAndy Whitcroft } 7095653d4876SAndy Whitcroft } 70969c0ca6f9SAndy Whitcroft 7097e29a70f1SJoe Perches# check for pointless casting of alloc functions 7098e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 7099000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 7100000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 71019c0ca6f9SAndy Whitcroft } 710213214adfSAndy Whitcroft 7103a640d25cSJoe Perches# alloc style 7104a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 71055b57980dSJoe Perches if ($perl_version_ok && 7106e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 7107a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 7108a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 7109a640d25cSJoe Perches } 7110a640d25cSJoe Perches 711173f1d07eSGustavo A. R. Silva# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc 71125b57980dSJoe Perches if ($perl_version_ok && 71131b4a2ed4SJoe Perches defined $stat && 711473f1d07eSGustavo A. R. Silva $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 711560a55369SJoe Perches my $oldfunc = $3; 711660a55369SJoe Perches my $a1 = $4; 711760a55369SJoe Perches my $a2 = $10; 711860a55369SJoe Perches my $newfunc = "kmalloc_array"; 711973f1d07eSGustavo A. R. Silva $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc"); 712073f1d07eSGustavo A. R. Silva $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc"); 712160a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 712260a55369SJoe Perches my $r1 = $a1; 712360a55369SJoe Perches my $r2 = $a2; 712460a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 712560a55369SJoe Perches $r1 = $a2; 712660a55369SJoe Perches $r2 = $a1; 712760a55369SJoe Perches } 7128e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 7129e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 71301b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 7131e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 7132e3d95a2aSTobin C. Harding 7133e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 71341b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 71351b4a2ed4SJoe Perches $cnt == 1 && 7136e367455aSJoe Perches $fix) { 713773f1d07eSGustavo A. R. Silva $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; 713860a55369SJoe Perches } 713960a55369SJoe Perches } 714060a55369SJoe Perches } 714160a55369SJoe Perches 7142972fdea2SJoe Perches# check for krealloc arg reuse 71435b57980dSJoe Perches if ($perl_version_ok && 71444cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 71454cab63ceSJoe Perches $1 eq $3) { 7146972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 7147972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 7148972fdea2SJoe Perches } 7149972fdea2SJoe Perches 71505ce59ae0SJoe Perches# check for alloc argument mismatch 71513965292aSLiao Chang if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) { 71525ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 71535ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 71545ce59ae0SJoe Perches } 71555ce59ae0SJoe Perches 7156caf2a54fSJoe Perches# check for multiple semicolons 7157caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 7158d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 7159d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 7160d5e616fcSJoe Perches $fix) { 7161194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 7162d5e616fcSJoe Perches } 7163d1e2ad07SJoe Perches } 7164d1e2ad07SJoe Perches 7165cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 7166cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 7167cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 71680ab90191SJoe Perches my $ull = ""; 71690ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 71700ab90191SJoe Perches if (CHK("BIT_MACRO", 71710ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 71720ab90191SJoe Perches $fix) { 71730ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 71740ab90191SJoe Perches } 71750ab90191SJoe Perches } 71760ab90191SJoe Perches 717750161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) 71783e89ad85SJerome Forissier if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { 717950161266SJoe Perches WARN("IS_ENABLED_CONFIG", 71803e89ad85SJerome Forissier "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); 718150161266SJoe Perches } 718250161266SJoe Perches 71832d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 71843e89ad85SJerome Forissier if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { 71852d632745SJoe Perches my $config = $1; 71862d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 71873e89ad85SJerome Forissier "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && 71882d632745SJoe Perches $fix) { 71892d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 71902d632745SJoe Perches } 71912d632745SJoe Perches } 71922d632745SJoe Perches 7193f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 7194f36d3eb8SJoe Perches my @fallthroughs = ( 7195f36d3eb8SJoe Perches 'fallthrough', 7196f36d3eb8SJoe Perches '@fallthrough@', 7197f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 7198f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 7199f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 7200f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 7201f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 7202f36d3eb8SJoe Perches ); 7203f36d3eb8SJoe Perches if ($raw_comment ne '') { 7204f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 7205f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 7206f36d3eb8SJoe Perches my $msg_level = \&WARN; 7207f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 7208f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 7209f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 7210f36d3eb8SJoe Perches last; 7211f36d3eb8SJoe Perches } 7212f36d3eb8SJoe Perches } 7213f36d3eb8SJoe Perches } 7214f36d3eb8SJoe Perches 7215d1e2ad07SJoe Perches# check for switch/default statements without a break; 72165b57980dSJoe Perches if ($perl_version_ok && 7217d1e2ad07SJoe Perches defined $stat && 7218d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 7219d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 7220e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 7221e3d95a2aSTobin C. Harding 7222d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 7223d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 7224caf2a54fSJoe Perches } 7225caf2a54fSJoe Perches 722613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 7227d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 7228d5e616fcSJoe Perches if (WARN("USE_FUNC", 7229d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 7230d5e616fcSJoe Perches $fix) { 7231194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 7232d5e616fcSJoe Perches } 723313214adfSAndy Whitcroft } 7234773647a0SAndy Whitcroft 723562ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 723662ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 723762ec818fSJoe Perches ERROR("DATE_TIME", 723862ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 723962ec818fSJoe Perches } 724062ec818fSJoe Perches 72412c92488aSJoe Perches# check for use of yield() 72422c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 72432c92488aSJoe Perches WARN("YIELD", 72442c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 72452c92488aSJoe Perches } 72462c92488aSJoe Perches 7247179f8f40SJoe Perches# check for comparisons against true and false 7248179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 7249179f8f40SJoe Perches my $lead = $1; 7250179f8f40SJoe Perches my $arg = $2; 7251179f8f40SJoe Perches my $test = $3; 7252179f8f40SJoe Perches my $otype = $4; 7253179f8f40SJoe Perches my $trail = $5; 7254179f8f40SJoe Perches my $op = "!"; 7255179f8f40SJoe Perches 7256179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 7257179f8f40SJoe Perches 7258179f8f40SJoe Perches my $type = lc($otype); 7259179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 7260179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 7261179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 7262179f8f40SJoe Perches $op = ""; 7263179f8f40SJoe Perches } 7264179f8f40SJoe Perches 7265179f8f40SJoe Perches CHK("BOOL_COMPARISON", 7266179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 7267179f8f40SJoe Perches 7268179f8f40SJoe Perches## maybe suggesting a correct construct would better 7269179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 7270179f8f40SJoe Perches 7271179f8f40SJoe Perches } 7272179f8f40SJoe Perches } 7273179f8f40SJoe Perches 72744882720bSThomas Gleixner# check for semaphores initialized locked 72754882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 7276000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 7277000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 7278773647a0SAndy Whitcroft } 72796712d858SJoe Perches 728067d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 728167d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 7282000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 728367d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 7284773647a0SAndy Whitcroft } 72856712d858SJoe Perches 7286ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 7287f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 7288000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 7289ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 7290f3db6639SMichael Ellerman } 72916712d858SJoe Perches 72923d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 72933d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 72943d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 72953d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 72963d709ab5SPaul E. McKenney } 72973d709ab5SPaul E. McKenney 72989189c7e7SJoe Perches# check for deprecated apis 72999189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 73009189c7e7SJoe Perches my $deprecated_api = $1; 73019189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 73029189c7e7SJoe Perches WARN("DEPRECATED_API", 73039189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 73049189c7e7SJoe Perches } 73059189c7e7SJoe Perches 73060f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 7307d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 7308ced69da1SQuentin Monnet if (defined($const_structs) && 7309ced69da1SQuentin Monnet $line !~ /\bconst\b/ && 7310d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 7311000d1cc1SJoe Perches WARN("CONST_STRUCT", 7312d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 73132b6db5cbSAndy Whitcroft } 7314773647a0SAndy Whitcroft 7315773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 7316773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 731735cdcbfcSPeng Wang# ignore designated initializers using NR_CPUS 7318773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 7319c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 7320c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 7321171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 7322171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 732335cdcbfcSPeng Wang $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ && 732435cdcbfcSPeng Wang $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/) 7325773647a0SAndy Whitcroft { 7326000d1cc1SJoe Perches WARN("NR_CPUS", 7327000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 7328773647a0SAndy Whitcroft } 73299c9ba34eSAndy Whitcroft 733052ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 733152ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 733252ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 733352ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 733452ea8506SJoe Perches } 733552ea8506SJoe Perches 7336acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 73375b57980dSJoe Perches if ($perl_version_ok && 7338acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 7339acd9362cSJoe Perches WARN("LIKELY_MISUSE", 7340acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 7341acd9362cSJoe Perches } 7342acd9362cSJoe Perches 7343fbe74541SJoe Perches# return sysfs_emit(foo, fmt, ...) fmt without newline 7344fbe74541SJoe Perches if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ && 7345fbe74541SJoe Perches substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) { 7346fbe74541SJoe Perches my $offset = $+[6] - 1; 7347fbe74541SJoe Perches if (WARN("SYSFS_EMIT", 7348fbe74541SJoe Perches "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) && 7349fbe74541SJoe Perches $fix) { 7350fbe74541SJoe Perches substr($fixed[$fixlinenr], $offset, 0) = '\\n'; 7351fbe74541SJoe Perches } 7352fbe74541SJoe Perches } 7353fbe74541SJoe Perches 7354de3f186fSDenis Efremov# nested likely/unlikely calls 7355de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 7356de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 7357de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 7358de3f186fSDenis Efremov } 7359de3f186fSDenis Efremov 7360691d77b6SAndy Whitcroft# whine mightly about in_atomic 7361691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 7362691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 7363000d1cc1SJoe Perches ERROR("IN_ATOMIC", 7364000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 7365f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 7366000d1cc1SJoe Perches WARN("IN_ATOMIC", 7367000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 7368691d77b6SAndy Whitcroft } 7369691d77b6SAndy Whitcroft } 73701704f47bSPeter Zijlstra 73711704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 73721704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 73731704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 73741704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 73751704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 73761704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 7377000d1cc1SJoe Perches ERROR("LOCKDEP", 7378000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 73791704f47bSPeter Zijlstra } 73801704f47bSPeter Zijlstra } 738188f8831cSDave Jones 7382b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 7383b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 7384000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 7385000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 738688f8831cSDave Jones } 73872435880fSJoe Perches 738800180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 738900180468SJoe Perches# and whether or not function naming is typical and if 739000180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 73915b57980dSJoe Perches if ($perl_version_ok && 739200180468SJoe Perches defined $stat && 739300180468SJoe Perches $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*\)/) { 739400180468SJoe Perches my $var = $1; 739500180468SJoe Perches my $perms = $2; 739600180468SJoe Perches my $show = $3; 739700180468SJoe Perches my $store = $4; 739800180468SJoe Perches my $octal_perms = perms_to_octal($perms); 739900180468SJoe Perches if ($show =~ /^${var}_show$/ && 740000180468SJoe Perches $store =~ /^${var}_store$/ && 740100180468SJoe Perches $octal_perms eq "0644") { 740200180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 740300180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 740400180468SJoe Perches $fix) { 740500180468SJoe Perches $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/; 740600180468SJoe Perches } 740700180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 740800180468SJoe Perches $store =~ /^NULL$/ && 740900180468SJoe Perches $octal_perms eq "0444") { 741000180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 741100180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 741200180468SJoe Perches $fix) { 741300180468SJoe Perches $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/; 741400180468SJoe Perches } 741500180468SJoe Perches } elsif ($show =~ /^NULL$/ && 741600180468SJoe Perches $store =~ /^${var}_store$/ && 741700180468SJoe Perches $octal_perms eq "0200") { 741800180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 741900180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 742000180468SJoe Perches $fix) { 742100180468SJoe Perches $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/; 742200180468SJoe Perches } 742300180468SJoe Perches } elsif ($octal_perms eq "0644" || 742400180468SJoe Perches $octal_perms eq "0444" || 742500180468SJoe Perches $octal_perms eq "0200") { 742600180468SJoe Perches my $newshow = "$show"; 742700180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 742800180468SJoe Perches my $newstore = $store; 742900180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 743000180468SJoe Perches my $rename = ""; 743100180468SJoe Perches if ($show ne $newshow) { 743200180468SJoe Perches $rename .= " '$show' to '$newshow'"; 743300180468SJoe Perches } 743400180468SJoe Perches if ($store ne $newstore) { 743500180468SJoe Perches $rename .= " '$store' to '$newstore'"; 743600180468SJoe Perches } 743700180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 743800180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 743900180468SJoe Perches } else { 744000180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 744100180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 744200180468SJoe Perches } 744300180468SJoe Perches } 744400180468SJoe Perches 7445515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 7446515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 744773121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 744873121534SJoe Perches# specific definition of not visible in sysfs. 744973121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 745073121534SJoe Perches# use the default permissions 74515b57980dSJoe Perches if ($perl_version_ok && 7452459cf0aeSJoe Perches defined $stat && 7453515a235eSJoe Perches $line =~ /$mode_perms_search/) { 74542435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 74552435880fSJoe Perches my $func = $entry->[0]; 74562435880fSJoe Perches my $arg_pos = $entry->[1]; 74572435880fSJoe Perches 7458459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 7459459cf0aeSJoe Perches $lc = $lc + $linenr; 74602a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 7461459cf0aeSJoe Perches 74622435880fSJoe Perches my $skip_args = ""; 74632435880fSJoe Perches if ($arg_pos > 1) { 74642435880fSJoe Perches $arg_pos--; 74652435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 74662435880fSJoe Perches } 7467f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 7468459cf0aeSJoe Perches if ($stat =~ /$test/) { 74692435880fSJoe Perches my $val = $1; 74702435880fSJoe Perches $val = $6 if ($skip_args ne ""); 747173121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 747273121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 747373121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 74742435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 7475459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 7476f90774e1SJoe Perches } 7477f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 7478c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 7479459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 74802435880fSJoe Perches } 7481459cf0aeSJoe Perches } 7482459cf0aeSJoe Perches } 7483459cf0aeSJoe Perches } 7484459cf0aeSJoe Perches 7485459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 7486bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 748700180468SJoe Perches my $oval = $1; 748800180468SJoe Perches my $octal = perms_to_octal($oval); 7489f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 7490459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 7491f90774e1SJoe Perches $fix) { 749200180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 74932435880fSJoe Perches } 749413214adfSAndy Whitcroft } 74955a6d20ceSBjorn Andersson 74965a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 74975a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 74985a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 74995a6d20ceSBjorn Andersson my $valid_licenses = qr{ 75005a6d20ceSBjorn Andersson GPL| 75015a6d20ceSBjorn Andersson GPL\ v2| 75025a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 75035a6d20ceSBjorn Andersson Dual\ BSD/GPL| 75045a6d20ceSBjorn Andersson Dual\ MIT/GPL| 75055a6d20ceSBjorn Andersson Dual\ MPL/GPL| 75065a6d20ceSBjorn Andersson Proprietary 75075a6d20ceSBjorn Andersson }x; 75085a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 75095a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 75105a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 75115a6d20ceSBjorn Andersson } 75126e8f42dcSJoe Perches if (!$file && $extracted_string eq '"GPL v2"') { 75136e8f42dcSJoe Perches if (WARN("MODULE_LICENSE", 75146e8f42dcSJoe Perches "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) && 75156e8f42dcSJoe Perches $fix) { 75166e8f42dcSJoe Perches $fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/; 75176e8f42dcSJoe Perches } 75186e8f42dcSJoe Perches } 75195a6d20ceSBjorn Andersson } 75206a8d76cbSMatteo Croce 75216a8d76cbSMatteo Croce# check for sysctl duplicate constants 75226a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 75236a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 75246a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 75256a8d76cbSMatteo Croce } 7526515a235eSJoe Perches } 752713214adfSAndy Whitcroft 752813214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 752913214adfSAndy Whitcroft # so just keep quiet. 753013214adfSAndy Whitcroft if ($#rawlines == -1) { 753113214adfSAndy Whitcroft exit(0); 75320a920b5bSAndy Whitcroft } 75330a920b5bSAndy Whitcroft 75348905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 75358905a67cSAndy Whitcroft # things that appear to be patches. 75368905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 75378905a67cSAndy Whitcroft exit(0); 75388905a67cSAndy Whitcroft } 75398905a67cSAndy Whitcroft 7540e73d2715SDwaipayan Ray # This is not a patch, and we are in 'no-patch' mode so 75418905a67cSAndy Whitcroft # just keep quiet. 75428905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 75438905a67cSAndy Whitcroft exit(0); 75448905a67cSAndy Whitcroft } 75458905a67cSAndy Whitcroft 7546a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 7547000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 7548000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 75490a920b5bSAndy Whitcroft } 7550cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 7551cd261496SGeert Uytterhoeven if ($signoff == 0) { 7552000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 7553000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 755448ca2d8aSDwaipayan Ray } elsif ($authorsignoff != 1) { 755548ca2d8aSDwaipayan Ray # authorsignoff values: 755648ca2d8aSDwaipayan Ray # 0 -> missing sign off 755748ca2d8aSDwaipayan Ray # 1 -> sign off identical 755848ca2d8aSDwaipayan Ray # 2 -> names and addresses match, comments mismatch 755948ca2d8aSDwaipayan Ray # 3 -> addresses match, names different 756048ca2d8aSDwaipayan Ray # 4 -> names match, addresses different 756148ca2d8aSDwaipayan Ray # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match 756248ca2d8aSDwaipayan Ray 756348ca2d8aSDwaipayan Ray my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; 756448ca2d8aSDwaipayan Ray 756548ca2d8aSDwaipayan Ray if ($authorsignoff == 0) { 756648ca2d8aSDwaipayan Ray ERROR("NO_AUTHOR_SIGN_OFF", 7567cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 756848ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 2) { 756948ca2d8aSDwaipayan Ray CHK("FROM_SIGN_OFF_MISMATCH", 757048ca2d8aSDwaipayan Ray "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); 757148ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 3) { 757248ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 757348ca2d8aSDwaipayan Ray "From:/Signed-off-by: email name mismatch: $sob_msg\n"); 757448ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 4) { 757548ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 757648ca2d8aSDwaipayan Ray "From:/Signed-off-by: email address mismatch: $sob_msg\n"); 757748ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 5) { 757848ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 757948ca2d8aSDwaipayan Ray "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); 758048ca2d8aSDwaipayan Ray } 7581cd261496SGeert Uytterhoeven } 75820a920b5bSAndy Whitcroft } 75830a920b5bSAndy Whitcroft 7584f0a594c1SAndy Whitcroft print report_dump(); 758513214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 758613214adfSAndy Whitcroft print "$filename " if ($summary_file); 75876c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 75886c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 75896c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 75906c72ffaaSAndy Whitcroft } 75918905a67cSAndy Whitcroft 7592d2c0a235SAndy Whitcroft if ($quiet == 0) { 7593ef212196SJoe Perches # If there were any defects found and not already fixing them 7594ef212196SJoe Perches if (!$clean and !$fix) { 7595ef212196SJoe Perches print << "EOM" 7596ef212196SJoe Perches 7597ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 7598ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 7599ef212196SJoe PerchesEOM 7600ef212196SJoe Perches } 7601d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 7602d2c0a235SAndy Whitcroft # then suggest that. 7603d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 7604b0781216SMike Frysinger $rpt_cleaners = 0; 7605d8469f16SJoe Perches print << "EOM" 7606d8469f16SJoe Perches 7607d8469f16SJoe PerchesNOTE: Whitespace errors detected. 7608d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 7609d8469f16SJoe PerchesEOM 7610d2c0a235SAndy Whitcroft } 7611d2c0a235SAndy Whitcroft } 7612d2c0a235SAndy Whitcroft 7613d752fcc8SJoe Perches if ($clean == 0 && $fix && 7614d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 7615d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 76169624b8d6SJoe Perches my $newfile = $filename; 76179624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 76183705ce5bSJoe Perches my $linecount = 0; 76193705ce5bSJoe Perches my $f; 76203705ce5bSJoe Perches 7621d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 7622d752fcc8SJoe Perches 76233705ce5bSJoe Perches open($f, '>', $newfile) 76243705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 76253705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 76263705ce5bSJoe Perches $linecount++; 76273705ce5bSJoe Perches if ($file) { 76283705ce5bSJoe Perches if ($linecount > 3) { 76293705ce5bSJoe Perches $fixed_line =~ s/^\+//; 76303705ce5bSJoe Perches print $f $fixed_line . "\n"; 76313705ce5bSJoe Perches } 76323705ce5bSJoe Perches } else { 76333705ce5bSJoe Perches print $f $fixed_line . "\n"; 76343705ce5bSJoe Perches } 76353705ce5bSJoe Perches } 76363705ce5bSJoe Perches close($f); 76373705ce5bSJoe Perches 76383705ce5bSJoe Perches if (!$quiet) { 76393705ce5bSJoe Perches print << "EOM"; 7640d8469f16SJoe Perches 76413705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 76423705ce5bSJoe Perches 76433705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 76443705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 76453705ce5bSJoe Perches 76463705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 76473705ce5bSJoe PerchesNo warranties, expressed or implied... 76483705ce5bSJoe PerchesEOM 76493705ce5bSJoe Perches } 76503705ce5bSJoe Perches } 76513705ce5bSJoe Perches 7652d8469f16SJoe Perches if ($quiet == 0) { 7653d8469f16SJoe Perches print "\n"; 7654d8469f16SJoe Perches if ($clean == 1) { 7655d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 7656d8469f16SJoe Perches } else { 7657d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 76580a920b5bSAndy Whitcroft } 76590a920b5bSAndy Whitcroft } 76600a920b5bSAndy Whitcroft return $clean; 76610a920b5bSAndy Whitcroft} 7662