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; 260a920b5bSAndy Whitcroftmy $tree = 1; 270a920b5bSAndy Whitcroftmy $chk_signoff = 1; 280a920b5bSAndy Whitcroftmy $chk_patch = 1; 29773647a0SAndy Whitcroftmy $tst_only; 306c72ffaaSAndy Whitcroftmy $emacs = 0; 318905a67cSAndy Whitcroftmy $terse = 0; 3234d8815fSJoe Perchesmy $showfile = 0; 336c72ffaaSAndy Whitcroftmy $file = 0; 344a593c34SDu, Changbinmy $git = 0; 350dea9f1eSJoe Perchesmy %git_commits = (); 366c72ffaaSAndy Whitcroftmy $check = 0; 372ac73b4fSJoe Perchesmy $check_orig = 0; 388905a67cSAndy Whitcroftmy $summary = 1; 398905a67cSAndy Whitcroftmy $mailback = 0; 4013214adfSAndy Whitcroftmy $summary_file = 0; 41000d1cc1SJoe Perchesmy $show_types = 0; 423beb42ecSJoe Perchesmy $list_types = 0; 433705ce5bSJoe Perchesmy $fix = 0; 449624b8d6SJoe Perchesmy $fix_inplace = 0; 456c72ffaaSAndy Whitcroftmy $root; 460f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'}; 470f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot); 48c2fdda0dSAndy Whitcroftmy %debug; 493445686aSJoe Perchesmy %camelcase = (); 5091bfe484SJoe Perchesmy %use_type = (); 5191bfe484SJoe Perchesmy @use = (); 5291bfe484SJoe Perchesmy %ignore_type = (); 53000d1cc1SJoe Perchesmy @ignore = (); 5477f5b10aSHannes Edermy $help = 0; 55000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 56bdc48fa1SJoe Perchesmy $max_line_length = 100; 57d62a201fSDave Hansenmy $ignore_perl_version = 0; 58d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5956193274SVadim Bendeburymy $min_conf_desc_length = 4; 6066b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 61ebfd7d62SJoe Perchesmy $codespell = 0; 62f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 63bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch"; 64ced69da1SQuentin Monnetmy $typedefsfile; 65737c0767SJohn Brooksmy $color = "auto"; 6698005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE 67dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE 68dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git'; 69713a09deSAntonio Borneomy $tabsize = 8; 703e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_"; 7177f5b10aSHannes Eder 7277f5b10aSHannes Edersub help { 7377f5b10aSHannes Eder my ($exitcode) = @_; 7477f5b10aSHannes Eder 7577f5b10aSHannes Eder print << "EOM"; 7677f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 7777f5b10aSHannes EderVersion: $V 7877f5b10aSHannes Eder 7977f5b10aSHannes EderOptions: 8077f5b10aSHannes Eder -q, --quiet quiet 8177f5b10aSHannes Eder --no-tree run without a kernel tree 8277f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 8377f5b10aSHannes Eder --patch treat FILE as patchfile (default) 8477f5b10aSHannes Eder --emacs emacs compile window format 8577f5b10aSHannes Eder --terse one line per report 8634d8815fSJoe Perches --showfile emit diffed file position, not input file position 874a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 884a593c34SDu, Changbin single git commit with: 894a593c34SDu, Changbin <rev> 904a593c34SDu, Changbin <rev>^ 914a593c34SDu, Changbin <rev>~n 924a593c34SDu, Changbin multiple git commits with: 934a593c34SDu, Changbin <rev1>..<rev2> 944a593c34SDu, Changbin <rev1>...<rev2> 954a593c34SDu, Changbin <rev>-<count> 964a593c34SDu, Changbin git merges are ignored 9777f5b10aSHannes Eder -f, --file treat FILE as regular source file 9877f5b10aSHannes Eder --subjective, --strict enable more subjective tests 993beb42ecSJoe Perches --list-types list the possible message types 10091bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 101000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 1023beb42ecSJoe Perches --show-types show the specific message type in the output 103bdc48fa1SJoe Perches --max-line-length=n set the maximum line length, (default $max_line_length) 104bdc48fa1SJoe Perches if exceeded, warn on patches 105bdc48fa1SJoe Perches requires --strict for use with --file 10656193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 107bdc48fa1SJoe Perches --tab-size=n set the number of spaces for tab (default $tabsize) 10877f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10977f5b10aSHannes Eder --no-summary suppress the per-file summary 11077f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 11177f5b10aSHannes Eder --summary-file include the filename in summary 11277f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 11377f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 11477f5b10aSHannes Eder is all off) 11577f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 11677f5b10aSHannes Eder literally 1173705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1183705ce5bSJoe Perches If correctable single-line errors exist, create 1193705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1203705ce5bSJoe Perches with potential errors corrected to the preferred 1213705ce5bSJoe Perches checkpatch style 1229624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1239624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1249624b8d6SJoe Perches file. It's your fault if there's no backup or git 125d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 126d62a201fSDave Hansen runtime errors. 127ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 128f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 129ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 13075ad8c57SJerome Forissier --typedefsfile Read additional types from this file 131737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 132737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 1333e89ad85SJerome Forissier --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default 1343e89ad85SJerome Forissier ${CONFIG_}) 13577f5b10aSHannes Eder -h, --help, --version display this help and exit 13677f5b10aSHannes Eder 13777f5b10aSHannes EderWhen FILE is - read standard input. 13877f5b10aSHannes EderEOM 13977f5b10aSHannes Eder 14077f5b10aSHannes Eder exit($exitcode); 14177f5b10aSHannes Eder} 14277f5b10aSHannes Eder 1433beb42ecSJoe Perchessub uniq { 1443beb42ecSJoe Perches my %seen; 1453beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1463beb42ecSJoe Perches} 1473beb42ecSJoe Perches 1483beb42ecSJoe Perchessub list_types { 1493beb42ecSJoe Perches my ($exitcode) = @_; 1503beb42ecSJoe Perches 1513beb42ecSJoe Perches my $count = 0; 1523beb42ecSJoe Perches 1533beb42ecSJoe Perches local $/ = undef; 1543beb42ecSJoe Perches 1553beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1563beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1573beb42ecSJoe Perches 1583beb42ecSJoe Perches my $text = <$script>; 1593beb42ecSJoe Perches close($script); 1603beb42ecSJoe Perches 1613beb42ecSJoe Perches my @types = (); 1620547fa58SJean Delvare # Also catch when type or level is passed through a variable 1630547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1643beb42ecSJoe Perches push (@types, $_); 1653beb42ecSJoe Perches } 1663beb42ecSJoe Perches @types = sort(uniq(@types)); 1673beb42ecSJoe Perches print("#\tMessage type\n\n"); 1683beb42ecSJoe Perches foreach my $type (@types) { 1693beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1703beb42ecSJoe Perches } 1713beb42ecSJoe Perches 1723beb42ecSJoe Perches exit($exitcode); 1733beb42ecSJoe Perches} 1743beb42ecSJoe Perches 175000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 176000d1cc1SJoe Perchesif (-f $conf) { 177000d1cc1SJoe Perches my @conf_args; 178000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 179000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 180000d1cc1SJoe Perches 181000d1cc1SJoe Perches while (<$conffile>) { 182000d1cc1SJoe Perches my $line = $_; 183000d1cc1SJoe Perches 184000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 185000d1cc1SJoe Perches $line =~ s/^\s*//g; 186000d1cc1SJoe Perches $line =~ s/\s+/ /g; 187000d1cc1SJoe Perches 188000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 189000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 190000d1cc1SJoe Perches 191000d1cc1SJoe Perches my @words = split(" ", $line); 192000d1cc1SJoe Perches foreach my $word (@words) { 193000d1cc1SJoe Perches last if ($word =~ m/^#/); 194000d1cc1SJoe Perches push (@conf_args, $word); 195000d1cc1SJoe Perches } 196000d1cc1SJoe Perches } 197000d1cc1SJoe Perches close($conffile); 198000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 199000d1cc1SJoe Perches} 200000d1cc1SJoe Perches 201737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 202737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 203737c0767SJohn Brooksforeach (@ARGV) { 204737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 205737c0767SJohn Brooks $_ = "--color=$color"; 206737c0767SJohn Brooks } 207737c0767SJohn Brooks} 208737c0767SJohn Brooks 2090a920b5bSAndy WhitcroftGetOptions( 2106c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2110a920b5bSAndy Whitcroft 'tree!' => \$tree, 2120a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2130a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2146c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2158905a67cSAndy Whitcroft 'terse!' => \$terse, 21634d8815fSJoe Perches 'showfile!' => \$showfile, 21777f5b10aSHannes Eder 'f|file!' => \$file, 2184a593c34SDu, Changbin 'g|git!' => \$git, 2196c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2206c72ffaaSAndy Whitcroft 'strict!' => \$check, 221000d1cc1SJoe Perches 'ignore=s' => \@ignore, 22291bfe484SJoe Perches 'types=s' => \@use, 223000d1cc1SJoe Perches 'show-types!' => \$show_types, 2243beb42ecSJoe Perches 'list-types!' => \$list_types, 2256cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 22656193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 227713a09deSAntonio Borneo 'tab-size=i' => \$tabsize, 2286c72ffaaSAndy Whitcroft 'root=s' => \$root, 2298905a67cSAndy Whitcroft 'summary!' => \$summary, 2308905a67cSAndy Whitcroft 'mailback!' => \$mailback, 23113214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2323705ce5bSJoe Perches 'fix!' => \$fix, 2339624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 234d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 235c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 236773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 237ebfd7d62SJoe Perches 'codespell!' => \$codespell, 238ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 23975ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 240737c0767SJohn Brooks 'color=s' => \$color, 241737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 242737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 2433e89ad85SJerome Forissier 'kconfig-prefix=s' => \${CONFIG_}, 24477f5b10aSHannes Eder 'h|help' => \$help, 24577f5b10aSHannes Eder 'version' => \$help 24677f5b10aSHannes Eder) or help(1); 24777f5b10aSHannes Eder 24877f5b10aSHannes Ederhelp(0) if ($help); 2490a920b5bSAndy Whitcroft 2503beb42ecSJoe Percheslist_types(0) if ($list_types); 2513beb42ecSJoe Perches 2529624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2532ac73b4fSJoe Perches$check_orig = $check; 2549624b8d6SJoe Perches 25532f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); 25632f30ca9SJoe Perches 2570a920b5bSAndy Whitcroftmy $exit = 0; 2580a920b5bSAndy Whitcroft 2595b57980dSJoe Perchesmy $perl_version_ok = 1; 260d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2615b57980dSJoe Perches $perl_version_ok = 0; 262d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2635b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 264d62a201fSDave Hansen} 265d62a201fSDave Hansen 26645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2670a920b5bSAndy Whitcroftif ($#ARGV < 0) { 26845107ff6SAllen Hubbe push(@ARGV, '-'); 2690a920b5bSAndy Whitcroft} 2700a920b5bSAndy Whitcroft 271737c0767SJohn Brooksif ($color =~ /^[01]$/) { 272737c0767SJohn Brooks $color = !$color; 273737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 274737c0767SJohn Brooks $color = 1; 275737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 276737c0767SJohn Brooks $color = 0; 277737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 278737c0767SJohn Brooks $color = (-t STDOUT); 279737c0767SJohn Brooks} else { 28032f30ca9SJoe Perches die "$P: Invalid color mode: $color\n"; 281737c0767SJohn Brooks} 282737c0767SJohn Brooks 283713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1 28432f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); 285713a09deSAntonio Borneo 28691bfe484SJoe Perchessub hash_save_array_words { 28791bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 28891bfe484SJoe Perches 28991bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 29091bfe484SJoe Perches foreach my $word (@array) { 291000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 292000d1cc1SJoe Perches $word =~ s/^\s*//g; 293000d1cc1SJoe Perches $word =~ s/\s+/ /g; 294000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 295000d1cc1SJoe Perches 296000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 297000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 298000d1cc1SJoe Perches 29991bfe484SJoe Perches $hashRef->{$word}++; 300000d1cc1SJoe Perches } 30191bfe484SJoe Perches} 30291bfe484SJoe Perches 30391bfe484SJoe Perchessub hash_show_words { 30491bfe484SJoe Perches my ($hashRef, $prefix) = @_; 30591bfe484SJoe Perches 3063c816e49SJoe Perches if (keys %$hashRef) { 307d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 30858cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 30991bfe484SJoe Perches print " $word"; 31091bfe484SJoe Perches } 311d8469f16SJoe Perches print "\n"; 31291bfe484SJoe Perches } 31391bfe484SJoe Perches} 31491bfe484SJoe Perches 31591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 31691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 317000d1cc1SJoe Perches 318c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 319c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3207429c690SAndy Whitcroftmy $dbg_type = 0; 321a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 322c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 32321caa13cSAndy Whitcroft ## no critic 32421caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 32521caa13cSAndy Whitcroft die "$@" if ($@); 326c2fdda0dSAndy Whitcroft} 327c2fdda0dSAndy Whitcroft 328d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 329d2c0a235SAndy Whitcroft 3308905a67cSAndy Whitcroftif ($terse) { 3318905a67cSAndy Whitcroft $emacs = 1; 3328905a67cSAndy Whitcroft $quiet++; 3338905a67cSAndy Whitcroft} 3348905a67cSAndy Whitcroft 3356c72ffaaSAndy Whitcroftif ($tree) { 3366c72ffaaSAndy Whitcroft if (defined $root) { 3376c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3386c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3396c72ffaaSAndy Whitcroft } 3406c72ffaaSAndy Whitcroft } else { 3416c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3426c72ffaaSAndy Whitcroft $root = '.'; 3436c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3446c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3456c72ffaaSAndy Whitcroft $root = $1; 3466c72ffaaSAndy Whitcroft } 3476c72ffaaSAndy Whitcroft } 3486c72ffaaSAndy Whitcroft 3496c72ffaaSAndy Whitcroft if (!defined $root) { 3500a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3510a920b5bSAndy Whitcroft exit(2); 3520a920b5bSAndy Whitcroft } 3536c72ffaaSAndy Whitcroft} 3546c72ffaaSAndy Whitcroft 3556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3566c72ffaaSAndy Whitcroft 3572ceb532bSAndy Whitcroftour $Ident = qr{ 3582ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3592ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3602ceb532bSAndy Whitcroft }x; 3616c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3626c72ffaaSAndy Whitcroftour $Sparse = qr{ 3636c72ffaaSAndy Whitcroft __user| 3646c72ffaaSAndy Whitcroft __kernel| 3656c72ffaaSAndy Whitcroft __force| 3666c72ffaaSAndy Whitcroft __iomem| 3676c72ffaaSAndy Whitcroft __must_check| 368417495edSAndy Whitcroft __kprobes| 369165e72a6SSven Eckelmann __ref| 37033aa4597SGeert Uytterhoeven __refconst| 37133aa4597SGeert Uytterhoeven __refdata| 372ad315455SBoqun Feng __rcu| 373ad315455SBoqun Feng __private 3746c72ffaaSAndy Whitcroft }x; 375e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 376e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 377e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 378e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 379e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3808716de38SJoe Perches 38152131292SWolfram Sang# Notes to $Attribute: 38252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3836c72ffaaSAndy Whitcroftour $Attribute = qr{ 3846c72ffaaSAndy Whitcroft const| 38503f1df7dSJoe Perches __percpu| 38603f1df7dSJoe Perches __nocast| 38703f1df7dSJoe Perches __safe| 38846d832f5SMichael S. Tsirkin __bitwise| 38903f1df7dSJoe Perches __packed__| 39003f1df7dSJoe Perches __packed2__| 39103f1df7dSJoe Perches __naked| 39203f1df7dSJoe Perches __maybe_unused| 39303f1df7dSJoe Perches __always_unused| 39403f1df7dSJoe Perches __noreturn| 39503f1df7dSJoe Perches __used| 39603f1df7dSJoe Perches __cold| 397e23ef1f3SJoe Perches __pure| 39803f1df7dSJoe Perches __noclone| 39903f1df7dSJoe Perches __deprecated| 4006c72ffaaSAndy Whitcroft __read_mostly| 401c5967e98SJoe Perches __ro_after_init| 4026c72ffaaSAndy Whitcroft __kprobes| 4038716de38SJoe Perches $InitAttribute| 40424e1d81aSAndy Whitcroft ____cacheline_aligned| 40524e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 4065fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 4075fe3af11SAndy Whitcroft __weak 4086c72ffaaSAndy Whitcroft }x; 409c45dcabdSAndy Whitcroftour $Modifier; 41091cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 4116c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 4126c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 4136c72ffaaSAndy Whitcroft 41495e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 41595e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 41695e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 41795e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4182435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 419c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 420326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 421326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 422326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 42374349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4242435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 425326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 426447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 42723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4286c72ffaaSAndy Whitcroftour $Operators = qr{ 4296c72ffaaSAndy Whitcroft <=|>=|==|!=| 4306c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 43123f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4326c72ffaaSAndy Whitcroft }x; 4336c72ffaaSAndy Whitcroft 43491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 43591cb5195SJoe Perches 436ab7e23f3SJoe Perchesour $BasicType; 4378905a67cSAndy Whitcroftour $NonptrType; 4381813087dSJoe Perchesour $NonptrTypeMisordered; 4398716de38SJoe Perchesour $NonptrTypeWithAttr; 4408905a67cSAndy Whitcroftour $Type; 4411813087dSJoe Perchesour $TypeMisordered; 4428905a67cSAndy Whitcroftour $Declare; 4431813087dSJoe Perchesour $DeclareMisordered; 4448905a67cSAndy Whitcroft 44515662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 44615662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 447171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 448171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 449171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 450171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 451171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 452171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 453171ae1a4SAndy Whitcroft}x; 454171ae1a4SAndy Whitcroft 45515662b3eSJoe Perchesour $UTF8 = qr{ 45615662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 45715662b3eSJoe Perches | $NON_ASCII_UTF8 45815662b3eSJoe Perches}x; 45915662b3eSJoe Perches 460e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 461021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 462021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 463021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 464021158b4SJoe Perches)}; 465e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 466fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4678ed22cadSAndy Whitcroft atomic_t 4688ed22cadSAndy Whitcroft)}; 469e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 470e6176fa4SJoe Perches $typeC99Typedefs\b| 471e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 472e6176fa4SJoe Perches $typeKernelTypedefs\b 473e6176fa4SJoe Perches)}; 4748ed22cadSAndy Whitcroft 4756d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4766d32f7a3SJoe Perches 477691e669bSJoe Perchesour $logFunctions = qr{(?x: 478758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4797d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 48087bd499aSJoe Perches TP_printk| 4816e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 482b0531722SJoe Perches panic| 48306668727SJoe Perches MODULE_[A-Z_]+| 48406668727SJoe Perches seq_vprintf|seq_printf|seq_puts 485691e669bSJoe Perches)}; 486691e669bSJoe Perches 487e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 488e29a70f1SJoe Perches (?:(?:devm_)? 489e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 490e29a70f1SJoe Perches kstrdup(?:_const)? | 491e29a70f1SJoe Perches kmemdup(?:_nul)?) | 492461e1565SChristophe JAILLET (?:\w+)?alloc_skb(?:_ip_align)? | 493e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 494e29a70f1SJoe Perches dma_alloc_coherent 495e29a70f1SJoe Perches)}; 496e29a70f1SJoe Perches 49720112475SJoe Perchesour $signature_tags = qr{(?xi: 49820112475SJoe Perches Signed-off-by:| 499d499480cSJorge Ramirez-Ortiz Co-developed-by:| 50020112475SJoe Perches Acked-by:| 50120112475SJoe Perches Tested-by:| 50220112475SJoe Perches Reviewed-by:| 50320112475SJoe Perches Reported-by:| 5048543ae12SMugunthan V N Suggested-by:| 50520112475SJoe Perches To:| 50620112475SJoe Perches Cc: 50720112475SJoe Perches)}; 50820112475SJoe Perches 5091813087dSJoe Perchesour @typeListMisordered = ( 5101813087dSJoe Perches qr{char\s+(?:un)?signed}, 5111813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 5121813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 5131813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 5141813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 5151813087dSJoe Perches qr{short\s+(?:un)?signed}, 5161813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5171813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5181813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5191813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5201813087dSJoe Perches qr{int\s+(?:un)?signed}, 5211813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5221813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5231813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5241813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5251813087dSJoe Perches qr{long\s+(?:un)?signed}, 5261813087dSJoe Perches); 5271813087dSJoe Perches 5288905a67cSAndy Whitcroftour @typeList = ( 5298905a67cSAndy Whitcroft qr{void}, 5300c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5310c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5320c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5330c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5340c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5350c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5360c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5370c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5380c773d9dSJoe Perches qr{(?:un)?signed}, 5398905a67cSAndy Whitcroft qr{float}, 5408905a67cSAndy Whitcroft qr{double}, 5418905a67cSAndy Whitcroft qr{bool}, 5428905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5438905a67cSAndy Whitcroft qr{union\s+$Ident}, 5448905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5458905a67cSAndy Whitcroft qr{${Ident}_t}, 5468905a67cSAndy Whitcroft qr{${Ident}_handler}, 5478905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5481813087dSJoe Perches @typeListMisordered, 5498905a67cSAndy Whitcroft); 550938224b5SJoe Perches 551938224b5SJoe Perchesour $C90_int_types = qr{(?x: 552938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 553938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 554938224b5SJoe Perches long\s+long\s+(?:un)?signed| 555938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 556938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 557938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 558938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 559938224b5SJoe Perches 560938224b5SJoe Perches long\s+int\s+(?:un)?signed| 561938224b5SJoe Perches long\s+(?:un)?signed\s+int| 562938224b5SJoe Perches long\s+(?:un)?signed| 563938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 564938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 565938224b5SJoe Perches int\s+long\s+(?:un)?signed| 566938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 567938224b5SJoe Perches 568938224b5SJoe Perches int\s+(?:un)?signed| 569938224b5SJoe Perches (?:(?:un)?signed\s+)?int 570938224b5SJoe Perches)}; 571938224b5SJoe Perches 572485ff23eSAlex Dowadour @typeListFile = (); 5738716de38SJoe Perchesour @typeListWithAttr = ( 5748716de38SJoe Perches @typeList, 5758716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5768716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5778716de38SJoe Perches); 5788716de38SJoe Perches 579c45dcabdSAndy Whitcroftour @modifierList = ( 580c45dcabdSAndy Whitcroft qr{fastcall}, 581c45dcabdSAndy Whitcroft); 582485ff23eSAlex Dowadour @modifierListFile = (); 5838905a67cSAndy Whitcroft 5842435880fSJoe Perchesour @mode_permission_funcs = ( 5852435880fSJoe Perches ["module_param", 3], 5862435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5872435880fSJoe Perches ["module_param_array_named", 5], 5882435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5892435880fSJoe Perches ["proc_create(?:_data|)", 2], 590459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 591459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 592459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 593459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 594459cf0aeSJoe Perches ["__ATTR", 2], 5952435880fSJoe Perches); 5962435880fSJoe Perches 5971a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; 5981a3dcf2eSJoe Perches 599515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 600515a235eSJoe Perchesour $mode_perms_search = ""; 601515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 602515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 603515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 604515a235eSJoe Perches} 60500180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 606515a235eSJoe Perches 6079189c7e7SJoe Perchesour %deprecated_apis = ( 6089189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 6099189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 6109189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 6119189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 6129189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 6139189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 6149189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 6159189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 6169189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 6179189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6189189c7e7SJoe Perches); 6199189c7e7SJoe Perches 6209189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6219189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6229189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6239189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6249189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6259189c7e7SJoe Perches} 6269189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6279189c7e7SJoe Perches 628b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 629b392c64fSJoe Perches S_IWUGO | 630b392c64fSJoe Perches S_IWOTH | 631b392c64fSJoe Perches S_IRWXUGO | 632b392c64fSJoe Perches S_IALLUGO | 633b392c64fSJoe Perches 0[0-7][0-7][2367] 634b392c64fSJoe Perches}x; 635b392c64fSJoe Perches 636f90774e1SJoe Perchesour %mode_permission_string_types = ( 637f90774e1SJoe Perches "S_IRWXU" => 0700, 638f90774e1SJoe Perches "S_IRUSR" => 0400, 639f90774e1SJoe Perches "S_IWUSR" => 0200, 640f90774e1SJoe Perches "S_IXUSR" => 0100, 641f90774e1SJoe Perches "S_IRWXG" => 0070, 642f90774e1SJoe Perches "S_IRGRP" => 0040, 643f90774e1SJoe Perches "S_IWGRP" => 0020, 644f90774e1SJoe Perches "S_IXGRP" => 0010, 645f90774e1SJoe Perches "S_IRWXO" => 0007, 646f90774e1SJoe Perches "S_IROTH" => 0004, 647f90774e1SJoe Perches "S_IWOTH" => 0002, 648f90774e1SJoe Perches "S_IXOTH" => 0001, 649f90774e1SJoe Perches "S_IRWXUGO" => 0777, 650f90774e1SJoe Perches "S_IRUGO" => 0444, 651f90774e1SJoe Perches "S_IWUGO" => 0222, 652f90774e1SJoe Perches "S_IXUGO" => 0111, 653f90774e1SJoe Perches); 654f90774e1SJoe Perches 655f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 656f90774e1SJoe Perchesour $mode_perms_string_search = ""; 657f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 658f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 659f90774e1SJoe Perches $mode_perms_string_search .= $entry; 660f90774e1SJoe Perches} 66100180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 66200180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 66300180468SJoe Perches ${single_mode_perms_string_search} 66400180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 66500180468SJoe Perches}x; 66600180468SJoe Perches 66700180468SJoe Perchessub perms_to_octal { 66800180468SJoe Perches my ($string) = @_; 66900180468SJoe Perches 67000180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 67100180468SJoe Perches 67200180468SJoe Perches my $val = ""; 67300180468SJoe Perches my $oval = ""; 67400180468SJoe Perches my $to = 0; 67500180468SJoe Perches my $curpos = 0; 67600180468SJoe Perches my $lastpos = 0; 67700180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 67800180468SJoe Perches $curpos = pos($string); 67900180468SJoe Perches my $match = $2; 68000180468SJoe Perches my $omatch = $1; 68100180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 68200180468SJoe Perches $lastpos = $curpos; 68300180468SJoe Perches $to |= $mode_permission_string_types{$match}; 68400180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 68500180468SJoe Perches $val .= $match; 68600180468SJoe Perches $oval .= $omatch; 68700180468SJoe Perches } 68800180468SJoe Perches $oval =~ s/^\s*\|\s*//; 68900180468SJoe Perches $oval =~ s/\s*\|\s*$//; 69000180468SJoe Perches return sprintf("%04o", $to); 69100180468SJoe Perches} 692f90774e1SJoe Perches 6937840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6947840a94cSWolfram Sang irq| 695cdcee686SSergey Ryazanov memory| 696cdcee686SSergey Ryazanov time| 697cdcee686SSergey Ryazanov reboot 6987840a94cSWolfram Sang)}; 6997840a94cSWolfram Sang# memory.h: ARM has a custom one 7007840a94cSWolfram Sang 70166b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 70266b47b4aSKees Cookmy $misspellings; 70366b47b4aSKees Cookmy %spelling_fix; 70436061e38SJoe Perches 70536061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 70666b47b4aSKees Cook while (<$spelling>) { 70766b47b4aSKees Cook my $line = $_; 70866b47b4aSKees Cook 70966b47b4aSKees Cook $line =~ s/\s*\n?$//g; 71066b47b4aSKees Cook $line =~ s/^\s*//g; 71166b47b4aSKees Cook 71266b47b4aSKees Cook next if ($line =~ m/^\s*#/); 71366b47b4aSKees Cook next if ($line =~ m/^\s*$/); 71466b47b4aSKees Cook 71566b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 71666b47b4aSKees Cook 71766b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 71866b47b4aSKees Cook } 71966b47b4aSKees Cook close($spelling); 72036061e38SJoe Perches} else { 72136061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 72236061e38SJoe Perches} 72366b47b4aSKees Cook 724ebfd7d62SJoe Perchesif ($codespell) { 725ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 726ebfd7d62SJoe Perches while (<$spelling>) { 727ebfd7d62SJoe Perches my $line = $_; 728ebfd7d62SJoe Perches 729ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 730ebfd7d62SJoe Perches $line =~ s/^\s*//g; 731ebfd7d62SJoe Perches 732ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 733ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 734ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 735ebfd7d62SJoe Perches 736ebfd7d62SJoe Perches $line =~ s/,.*$//; 737ebfd7d62SJoe Perches 738ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 739ebfd7d62SJoe Perches 740ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 741ebfd7d62SJoe Perches } 742ebfd7d62SJoe Perches close($spelling); 743ebfd7d62SJoe Perches } else { 744ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 745ebfd7d62SJoe Perches } 746ebfd7d62SJoe Perches} 747ebfd7d62SJoe Perches 748ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 749ebfd7d62SJoe Perches 75075ad8c57SJerome Forissiersub read_words { 75175ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 75275ad8c57SJerome Forissier 75375ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 75475ad8c57SJerome Forissier while (<$words>) { 755bf1fa1daSJoe Perches my $line = $_; 756bf1fa1daSJoe Perches 757bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 758bf1fa1daSJoe Perches $line =~ s/^\s*//g; 759bf1fa1daSJoe Perches 760bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 761bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 762bf1fa1daSJoe Perches if ($line =~ /\s/) { 76375ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 764bf1fa1daSJoe Perches next; 765bf1fa1daSJoe Perches } 766bf1fa1daSJoe Perches 767ced69da1SQuentin Monnet $$wordsRef .= '|' if (defined $$wordsRef); 76875ad8c57SJerome Forissier $$wordsRef .= $line; 769bf1fa1daSJoe Perches } 77075ad8c57SJerome Forissier close($file); 77175ad8c57SJerome Forissier return 1; 772bf1fa1daSJoe Perches } 773bf1fa1daSJoe Perches 77475ad8c57SJerome Forissier return 0; 77575ad8c57SJerome Forissier} 77675ad8c57SJerome Forissier 777ced69da1SQuentin Monnetmy $const_structs; 778ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) { 77975ad8c57SJerome Forissier read_words(\$const_structs, $conststructsfile) 78075ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 781ced69da1SQuentin Monnet} 78275ad8c57SJerome Forissier 783ced69da1SQuentin Monnetif (defined($typedefsfile)) { 784ced69da1SQuentin Monnet my $typeOtherTypedefs; 78575ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 78675ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 787ced69da1SQuentin Monnet $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); 78875ad8c57SJerome Forissier} 78975ad8c57SJerome Forissier 7908905a67cSAndy Whitcroftsub build_types { 791485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 792485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7931813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7948716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 795c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 796ab7e23f3SJoe Perches $BasicType = qr{ 797ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 798ab7e23f3SJoe Perches (?:${all}\b) 799ab7e23f3SJoe Perches }x; 8008905a67cSAndy Whitcroft $NonptrType = qr{ 801d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 802cf655043SAndy Whitcroft (?: 8036b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 8048ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 805c45dcabdSAndy Whitcroft (?:${all}\b) 806cf655043SAndy Whitcroft ) 807c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 8088905a67cSAndy Whitcroft }x; 8091813087dSJoe Perches $NonptrTypeMisordered = qr{ 8101813087dSJoe Perches (?:$Modifier\s+|const\s+)* 8111813087dSJoe Perches (?: 8121813087dSJoe Perches (?:${Misordered}\b) 8131813087dSJoe Perches ) 8141813087dSJoe Perches (?:\s+$Modifier|\s+const)* 8151813087dSJoe Perches }x; 8168716de38SJoe Perches $NonptrTypeWithAttr = qr{ 8178716de38SJoe Perches (?:$Modifier\s+|const\s+)* 8188716de38SJoe Perches (?: 8198716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8208716de38SJoe Perches (?:$typeTypedefs\b)| 8218716de38SJoe Perches (?:${allWithAttr}\b) 8228716de38SJoe Perches ) 8238716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8248716de38SJoe Perches }x; 8258905a67cSAndy Whitcroft $Type = qr{ 826c45dcabdSAndy Whitcroft $NonptrType 8277b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 828c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8298905a67cSAndy Whitcroft }x; 8301813087dSJoe Perches $TypeMisordered = qr{ 8311813087dSJoe Perches $NonptrTypeMisordered 8327b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 8331813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8341813087dSJoe Perches }x; 83591cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8361813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8378905a67cSAndy Whitcroft} 8388905a67cSAndy Whitcroftbuild_types(); 8396c72ffaaSAndy Whitcroft 8407d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 841d1fe9c09SJoe Perches 842d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 843d1fe9c09SJoe Perches# requires at least perl version v5.10.0 844d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 845d1fe9c09SJoe Perches 846d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 8472435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 848c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 8497d2367afSJoe Perches 850f8422308SJoe Perchesour $declaration_macros = qr{(?x: 8513e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 852fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 8533d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 854f8422308SJoe Perches)}; 855f8422308SJoe Perches 8568d0325ccSAditya Srivastavaour %allow_repeated_words = ( 8578d0325ccSAditya Srivastava add => '', 8588d0325ccSAditya Srivastava added => '', 8598d0325ccSAditya Srivastava bad => '', 8608d0325ccSAditya Srivastava be => '', 8618d0325ccSAditya Srivastava); 8628d0325ccSAditya Srivastava 8637d2367afSJoe Perchessub deparenthesize { 8647d2367afSJoe Perches my ($string) = @_; 8657d2367afSJoe Perches return "" if (!defined($string)); 8665b9553abSJoe Perches 8675b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8685b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8695b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8705b9553abSJoe Perches } 8715b9553abSJoe Perches 8727d2367afSJoe Perches $string =~ s@\s+@ @g; 8735b9553abSJoe Perches 8747d2367afSJoe Perches return $string; 8757d2367afSJoe Perches} 8767d2367afSJoe Perches 8773445686aSJoe Perchessub seed_camelcase_file { 8783445686aSJoe Perches my ($file) = @_; 8793445686aSJoe Perches 8803445686aSJoe Perches return if (!(-f $file)); 8813445686aSJoe Perches 8823445686aSJoe Perches local $/; 8833445686aSJoe Perches 8843445686aSJoe Perches open(my $include_file, '<', "$file") 8853445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8863445686aSJoe Perches my $text = <$include_file>; 8873445686aSJoe Perches close($include_file); 8883445686aSJoe Perches 8893445686aSJoe Perches my @lines = split('\n', $text); 8903445686aSJoe Perches 8913445686aSJoe Perches foreach my $line (@lines) { 8923445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8933445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8943445686aSJoe Perches $camelcase{$1} = 1; 89511ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 89611ea516aSJoe Perches $camelcase{$1} = 1; 89711ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8983445686aSJoe Perches $camelcase{$1} = 1; 8993445686aSJoe Perches } 9003445686aSJoe Perches } 9013445686aSJoe Perches} 9023445686aSJoe Perches 903cd28b119SJoe Perchesour %maintained_status = (); 904cd28b119SJoe Perches 90585b0ee18SJoe Perchessub is_maintained_obsolete { 90685b0ee18SJoe Perches my ($filename) = @_; 90785b0ee18SJoe Perches 908f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 90985b0ee18SJoe Perches 910cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 911cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 912cd28b119SJoe Perches } 91385b0ee18SJoe Perches 914cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 91585b0ee18SJoe Perches} 91685b0ee18SJoe Perches 9173b6e8ac9SJoe Perchessub is_SPDX_License_valid { 9183b6e8ac9SJoe Perches my ($license) = @_; 9193b6e8ac9SJoe Perches 9200f7f635bSJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); 9213b6e8ac9SJoe Perches 92256294112SJoe Perches my $root_path = abs_path($root); 92356294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 9243b6e8ac9SJoe Perches return 0 if ($status ne ""); 9253b6e8ac9SJoe Perches return 1; 9263b6e8ac9SJoe Perches} 9273b6e8ac9SJoe Perches 9283445686aSJoe Perchesmy $camelcase_seeded = 0; 9293445686aSJoe Perchessub seed_camelcase_includes { 9303445686aSJoe Perches return if ($camelcase_seeded); 9313445686aSJoe Perches 9323445686aSJoe Perches my $files; 933c707a81dSJoe Perches my $camelcase_cache = ""; 934c707a81dSJoe Perches my @include_files = (); 935c707a81dSJoe Perches 936c707a81dSJoe Perches $camelcase_seeded = 1; 937351b2a1fSJoe Perches 9380f7f635bSJoe Perches if (-e "$gitroot") { 939dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 940351b2a1fSJoe Perches chomp $git_last_include_commit; 941c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 942c707a81dSJoe Perches } else { 943c707a81dSJoe Perches my $last_mod_date = 0; 944c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 945c707a81dSJoe Perches @include_files = split('\n', $files); 946c707a81dSJoe Perches foreach my $file (@include_files) { 947c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 948c707a81dSJoe Perches localtime((stat $file)[9])); 949c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 950c707a81dSJoe Perches } 951c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 952c707a81dSJoe Perches } 953c707a81dSJoe Perches 954c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 955c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 956c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 957351b2a1fSJoe Perches while (<$camelcase_file>) { 958351b2a1fSJoe Perches chomp; 959351b2a1fSJoe Perches $camelcase{$_} = 1; 960351b2a1fSJoe Perches } 961351b2a1fSJoe Perches close($camelcase_file); 962351b2a1fSJoe Perches 963351b2a1fSJoe Perches return; 964351b2a1fSJoe Perches } 965c707a81dSJoe Perches 9660f7f635bSJoe Perches if (-e "$gitroot") { 967dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 968c707a81dSJoe Perches @include_files = split('\n', $files); 9693445686aSJoe Perches } 970c707a81dSJoe Perches 9713445686aSJoe Perches foreach my $file (@include_files) { 9723445686aSJoe Perches seed_camelcase_file($file); 9733445686aSJoe Perches } 974351b2a1fSJoe Perches 975c707a81dSJoe Perches if ($camelcase_cache ne "") { 976351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 977c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 978c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 979351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 980351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 981351b2a1fSJoe Perches } 982351b2a1fSJoe Perches close($camelcase_file); 983351b2a1fSJoe Perches } 9843445686aSJoe Perches} 9853445686aSJoe Perches 986f5f61325SJoe Perchessub git_is_single_file { 987f5f61325SJoe Perches my ($filename) = @_; 988f5f61325SJoe Perches 989f5f61325SJoe Perches return 0 if ((which("git") eq "") || !(-e "$gitroot")); 990f5f61325SJoe Perches 991f5f61325SJoe Perches my $output = `${git_command} ls-files -- $filename 2>/dev/null`; 992f5f61325SJoe Perches my $count = $output =~ tr/\n//; 993f5f61325SJoe Perches return $count eq 1 && $output =~ m{^${filename}$}; 994f5f61325SJoe Perches} 995f5f61325SJoe Perches 996d311cd44SJoe Perchessub git_commit_info { 997d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 998d311cd44SJoe Perches 9990f7f635bSJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); 1000d311cd44SJoe Perches 1001dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 1002d311cd44SJoe Perches $output =~ s/^\s*//gm; 1003d311cd44SJoe Perches my @lines = split("\n", $output); 1004d311cd44SJoe Perches 10050d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 10060d7835fcSJoe Perches 10075a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 1008d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 1009d311cd44SJoe Perches# all matching commit ids, but it's very slow... 1010d311cd44SJoe Perches# 1011d311cd44SJoe Perches# echo "checking commits $1..." 1012d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 1013d311cd44SJoe Perches# while read line ; do 1014d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 1015d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 1016d311cd44SJoe Perches# done 1017d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 1018948b133aSHeinrich Schuchardt $id = undef; 1019d311cd44SJoe Perches } else { 1020d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 1021d311cd44SJoe Perches $desc = substr($lines[0], 41); 1022d311cd44SJoe Perches } 1023d311cd44SJoe Perches 1024d311cd44SJoe Perches return ($id, $desc); 1025d311cd44SJoe Perches} 1026d311cd44SJoe Perches 10276c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 10280a920b5bSAndy Whitcroft 102900df344fSAndy Whitcroftmy @rawlines = (); 1030c2fdda0dSAndy Whitcroftmy @lines = (); 10313705ce5bSJoe Perchesmy @fixed = (); 1032d752fcc8SJoe Perchesmy @fixed_inserted = (); 1033d752fcc8SJoe Perchesmy @fixed_deleted = (); 1034194f66fcSJoe Perchesmy $fixlinenr = -1; 1035194f66fcSJoe Perches 10364a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10374a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 10380f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot"); 10394a593c34SDu, Changbin 10404a593c34SDu, Changbinif ($git) { 10414a593c34SDu, Changbin my @commits = (); 10420dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 10434a593c34SDu, Changbin my $git_range; 104428898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 104528898fd1SJoe Perches $git_range = "-$2 $1"; 10464a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 10474a593c34SDu, Changbin $git_range = "$commit_expr"; 10484a593c34SDu, Changbin } else { 10490dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 10500dea9f1eSJoe Perches } 1051dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 10520dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 105328898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 105428898fd1SJoe Perches next if (!defined($1) || !defined($2)); 10550dea9f1eSJoe Perches my $sha1 = $1; 10560dea9f1eSJoe Perches my $subject = $2; 10570dea9f1eSJoe Perches unshift(@commits, $sha1); 10580dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 10594a593c34SDu, Changbin } 10604a593c34SDu, Changbin } 10614a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 10624a593c34SDu, Changbin @ARGV = @commits; 10634a593c34SDu, Changbin} 10644a593c34SDu, Changbin 1065c2fdda0dSAndy Whitcroftmy $vname; 106698005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 10676c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 106821caa13cSAndy Whitcroft my $FILE; 1069f5f61325SJoe Perches my $is_git_file = git_is_single_file($filename); 1070f5f61325SJoe Perches my $oldfile = $file; 1071f5f61325SJoe Perches $file = 1 if ($is_git_file); 10724a593c34SDu, Changbin if ($git) { 10734a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 10744a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 10754a593c34SDu, Changbin } elsif ($file) { 107621caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 10776c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 107821caa13cSAndy Whitcroft } elsif ($filename eq '-') { 107921caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 10806c72ffaaSAndy Whitcroft } else { 108121caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 10826c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 10836c72ffaaSAndy Whitcroft } 1084c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1085c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 10864a593c34SDu, Changbin } elsif ($git) { 10870dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1088c2fdda0dSAndy Whitcroft } else { 1089c2fdda0dSAndy Whitcroft $vname = $filename; 1090c2fdda0dSAndy Whitcroft } 109121caa13cSAndy Whitcroft while (<$FILE>) { 10920a920b5bSAndy Whitcroft chomp; 109300df344fSAndy Whitcroft push(@rawlines, $_); 1094c7f574d0SGeert Uytterhoeven $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); 10956c72ffaaSAndy Whitcroft } 109621caa13cSAndy Whitcroft close($FILE); 1097d8469f16SJoe Perches 1098d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1099d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1100d8469f16SJoe Perches print "$vname\n"; 1101d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1102d8469f16SJoe Perches } 1103d8469f16SJoe Perches 1104c2fdda0dSAndy Whitcroft if (!process($filename)) { 11050a920b5bSAndy Whitcroft $exit = 1; 11060a920b5bSAndy Whitcroft } 110700df344fSAndy Whitcroft @rawlines = (); 110813214adfSAndy Whitcroft @lines = (); 11093705ce5bSJoe Perches @fixed = (); 1110d752fcc8SJoe Perches @fixed_inserted = (); 1111d752fcc8SJoe Perches @fixed_deleted = (); 1112194f66fcSJoe Perches $fixlinenr = -1; 1113485ff23eSAlex Dowad @modifierListFile = (); 1114485ff23eSAlex Dowad @typeListFile = (); 1115485ff23eSAlex Dowad build_types(); 1116f5f61325SJoe Perches $file = $oldfile if ($is_git_file); 11170a920b5bSAndy Whitcroft} 11180a920b5bSAndy Whitcroft 1119d8469f16SJoe Perchesif (!$quiet) { 11203c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 11213c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 11223c816e49SJoe Perches 11235b57980dSJoe Perches if (!$perl_version_ok) { 1124d8469f16SJoe Perches print << "EOM" 1125d8469f16SJoe Perches 1126d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 11275b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1128d8469f16SJoe PerchesEOM 1129d8469f16SJoe Perches } 1130d8469f16SJoe Perches if ($exit) { 1131d8469f16SJoe Perches print << "EOM" 1132d8469f16SJoe Perches 1133d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1134d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1135d8469f16SJoe PerchesEOM 1136d8469f16SJoe Perches } 1137d8469f16SJoe Perches} 1138d8469f16SJoe Perches 11390a920b5bSAndy Whitcroftexit($exit); 11400a920b5bSAndy Whitcroft 11410a920b5bSAndy Whitcroftsub top_of_kernel_tree { 11426c72ffaaSAndy Whitcroft my ($root) = @_; 11436c72ffaaSAndy Whitcroft 11446c72ffaaSAndy Whitcroft my @tree_check = ( 11456c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 11466c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 11476c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 11486c72ffaaSAndy Whitcroft ); 11496c72ffaaSAndy Whitcroft 11506c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 11516c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 11520a920b5bSAndy Whitcroft return 0; 11530a920b5bSAndy Whitcroft } 11546c72ffaaSAndy Whitcroft } 11556c72ffaaSAndy Whitcroft return 1; 11566c72ffaaSAndy Whitcroft} 11570a920b5bSAndy Whitcroft 115820112475SJoe Perchessub parse_email { 115920112475SJoe Perches my ($formatted_email) = @_; 116020112475SJoe Perches 116120112475SJoe Perches my $name = ""; 1162dfa05c28SJoe Perches my $name_comment = ""; 116320112475SJoe Perches my $address = ""; 116420112475SJoe Perches my $comment = ""; 116520112475SJoe Perches 116620112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 116720112475SJoe Perches $name = $1; 116820112475SJoe Perches $address = $2; 116920112475SJoe Perches $comment = $3 if defined $3; 117020112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 117120112475SJoe Perches $address = $1; 117220112475SJoe Perches $comment = $2 if defined $2; 117320112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 117420112475SJoe Perches $address = $1; 117520112475SJoe Perches $comment = $2 if defined $2; 117685e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 117720112475SJoe Perches $name = $formatted_email; 11783705ce5bSJoe Perches $name = trim($name); 117920112475SJoe Perches $name =~ s/^\"|\"$//g; 118020112475SJoe Perches # If there's a name left after stripping spaces and 118120112475SJoe Perches # leading quotes, and the address doesn't have both 118220112475SJoe Perches # leading and trailing angle brackets, the address 118320112475SJoe Perches # is invalid. ie: 118420112475SJoe Perches # "joe smith [email protected]" bad 118520112475SJoe Perches # "joe smith <[email protected]" bad 118620112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 118720112475SJoe Perches $name = ""; 118820112475SJoe Perches $address = ""; 118920112475SJoe Perches $comment = ""; 119020112475SJoe Perches } 119120112475SJoe Perches } 119220112475SJoe Perches 119348ca2d8aSDwaipayan Ray $comment = trim($comment); 11943705ce5bSJoe Perches $name = trim($name); 119520112475SJoe Perches $name =~ s/^\"|\"$//g; 119648ca2d8aSDwaipayan Ray if ($name =~ s/(\s*\([^\)]+\))\s*//) { 1197dfa05c28SJoe Perches $name_comment = trim($1); 1198dfa05c28SJoe Perches } 11993705ce5bSJoe Perches $address = trim($address); 120020112475SJoe Perches $address =~ s/^\<|\>$//g; 120120112475SJoe Perches 120220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 120320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 120420112475SJoe Perches $name = "\"$name\""; 120520112475SJoe Perches } 120620112475SJoe Perches 1207dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 120820112475SJoe Perches} 120920112475SJoe Perches 121020112475SJoe Perchessub format_email { 121148ca2d8aSDwaipayan Ray my ($name, $name_comment, $address, $comment) = @_; 121220112475SJoe Perches 121320112475SJoe Perches my $formatted_email; 121420112475SJoe Perches 121548ca2d8aSDwaipayan Ray $name_comment = trim($name_comment); 121648ca2d8aSDwaipayan Ray $comment = trim($comment); 12173705ce5bSJoe Perches $name = trim($name); 121820112475SJoe Perches $name =~ s/^\"|\"$//g; 12193705ce5bSJoe Perches $address = trim($address); 122020112475SJoe Perches 122120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 122220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 122320112475SJoe Perches $name = "\"$name\""; 122420112475SJoe Perches } 122520112475SJoe Perches 122620112475SJoe Perches if ("$name" eq "") { 122720112475SJoe Perches $formatted_email = "$address"; 122820112475SJoe Perches } else { 122948ca2d8aSDwaipayan Ray $formatted_email = "$name$name_comment <$address>"; 123020112475SJoe Perches } 123148ca2d8aSDwaipayan Ray $formatted_email .= "$comment"; 123220112475SJoe Perches return $formatted_email; 123320112475SJoe Perches} 123420112475SJoe Perches 1235dfa05c28SJoe Perchessub reformat_email { 1236dfa05c28SJoe Perches my ($email) = @_; 1237dfa05c28SJoe Perches 1238dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 123948ca2d8aSDwaipayan Ray return format_email($email_name, $name_comment, $email_address, $comment); 1240dfa05c28SJoe Perches} 1241dfa05c28SJoe Perches 1242dfa05c28SJoe Perchessub same_email_addresses { 124348ca2d8aSDwaipayan Ray my ($email1, $email2, $match_comment) = @_; 1244dfa05c28SJoe Perches 1245dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1246dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1247dfa05c28SJoe Perches 124848ca2d8aSDwaipayan Ray if ($match_comment != 1) { 1249dfa05c28SJoe Perches return $email1_name eq $email2_name && 1250dfa05c28SJoe Perches $email1_address eq $email2_address; 1251dfa05c28SJoe Perches } 125248ca2d8aSDwaipayan Ray return $email1_name eq $email2_name && 125348ca2d8aSDwaipayan Ray $email1_address eq $email2_address && 125448ca2d8aSDwaipayan Ray $name1_comment eq $name2_comment && 125548ca2d8aSDwaipayan Ray $comment1 eq $comment2; 125648ca2d8aSDwaipayan Ray} 1257dfa05c28SJoe Perches 1258d311cd44SJoe Perchessub which { 1259d311cd44SJoe Perches my ($bin) = @_; 1260d311cd44SJoe Perches 1261d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1262d311cd44SJoe Perches if (-e "$path/$bin") { 1263d311cd44SJoe Perches return "$path/$bin"; 1264d311cd44SJoe Perches } 1265d311cd44SJoe Perches } 1266d311cd44SJoe Perches 1267d311cd44SJoe Perches return ""; 1268d311cd44SJoe Perches} 1269d311cd44SJoe Perches 1270000d1cc1SJoe Perchessub which_conf { 1271000d1cc1SJoe Perches my ($conf) = @_; 1272000d1cc1SJoe Perches 1273000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1274000d1cc1SJoe Perches if (-e "$path/$conf") { 1275000d1cc1SJoe Perches return "$path/$conf"; 1276000d1cc1SJoe Perches } 1277000d1cc1SJoe Perches } 1278000d1cc1SJoe Perches 1279000d1cc1SJoe Perches return ""; 1280000d1cc1SJoe Perches} 1281000d1cc1SJoe Perches 12820a920b5bSAndy Whitcroftsub expand_tabs { 12830a920b5bSAndy Whitcroft my ($str) = @_; 12840a920b5bSAndy Whitcroft 12850a920b5bSAndy Whitcroft my $res = ''; 12860a920b5bSAndy Whitcroft my $n = 0; 12870a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 12880a920b5bSAndy Whitcroft if ($c eq "\t") { 12890a920b5bSAndy Whitcroft $res .= ' '; 12900a920b5bSAndy Whitcroft $n++; 1291713a09deSAntonio Borneo for (; ($n % $tabsize) != 0; $n++) { 12920a920b5bSAndy Whitcroft $res .= ' '; 12930a920b5bSAndy Whitcroft } 12940a920b5bSAndy Whitcroft next; 12950a920b5bSAndy Whitcroft } 12960a920b5bSAndy Whitcroft $res .= $c; 12970a920b5bSAndy Whitcroft $n++; 12980a920b5bSAndy Whitcroft } 12990a920b5bSAndy Whitcroft 13000a920b5bSAndy Whitcroft return $res; 13010a920b5bSAndy Whitcroft} 13026c72ffaaSAndy Whitcroftsub copy_spacing { 1303773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 13046c72ffaaSAndy Whitcroft return $res; 13056c72ffaaSAndy Whitcroft} 13060a920b5bSAndy Whitcroft 13074a0df2efSAndy Whitcroftsub line_stats { 13084a0df2efSAndy Whitcroft my ($line) = @_; 13094a0df2efSAndy Whitcroft 13104a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 13114a0df2efSAndy Whitcroft $line =~ s/^.//; 13124a0df2efSAndy Whitcroft $line = expand_tabs($line); 13134a0df2efSAndy Whitcroft 13144a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 13154a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 13164a0df2efSAndy Whitcroft 13174a0df2efSAndy Whitcroft return (length($line), length($white)); 13184a0df2efSAndy Whitcroft} 13194a0df2efSAndy Whitcroft 1320773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1321773647a0SAndy Whitcroft 1322773647a0SAndy Whitcroftsub sanitise_line_reset { 1323773647a0SAndy Whitcroft my ($in_comment) = @_; 1324773647a0SAndy Whitcroft 1325773647a0SAndy Whitcroft if ($in_comment) { 1326773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1327773647a0SAndy Whitcroft } else { 1328773647a0SAndy Whitcroft $sanitise_quote = ''; 1329773647a0SAndy Whitcroft } 1330773647a0SAndy Whitcroft} 133100df344fSAndy Whitcroftsub sanitise_line { 133200df344fSAndy Whitcroft my ($line) = @_; 133300df344fSAndy Whitcroft 133400df344fSAndy Whitcroft my $res = ''; 133500df344fSAndy Whitcroft my $l = ''; 133600df344fSAndy Whitcroft 1337c2fdda0dSAndy Whitcroft my $qlen = 0; 1338773647a0SAndy Whitcroft my $off = 0; 1339773647a0SAndy Whitcroft my $c; 134000df344fSAndy Whitcroft 1341773647a0SAndy Whitcroft # Always copy over the diff marker. 1342773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1343773647a0SAndy Whitcroft 1344773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1345773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1346773647a0SAndy Whitcroft 13478d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1348773647a0SAndy Whitcroft # and end, all to $;. 1349773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1350773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1351773647a0SAndy Whitcroft 1352773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1353773647a0SAndy Whitcroft $off++; 135400df344fSAndy Whitcroft next; 1355773647a0SAndy Whitcroft } 135681bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1357773647a0SAndy Whitcroft $sanitise_quote = ''; 1358773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1359773647a0SAndy Whitcroft $off++; 1360773647a0SAndy Whitcroft next; 1361773647a0SAndy Whitcroft } 1362113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1363113f04a8SDaniel Walker $sanitise_quote = '//'; 1364113f04a8SDaniel Walker 1365113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1366113f04a8SDaniel Walker $off++; 1367113f04a8SDaniel Walker next; 1368113f04a8SDaniel Walker } 1369773647a0SAndy Whitcroft 1370773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1371773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1372773647a0SAndy Whitcroft $c eq "\\") { 1373773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1374773647a0SAndy Whitcroft $off++; 1375773647a0SAndy Whitcroft next; 1376773647a0SAndy Whitcroft } 1377773647a0SAndy Whitcroft # Regular quotes. 1378773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1379773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1380773647a0SAndy Whitcroft $sanitise_quote = $c; 1381773647a0SAndy Whitcroft 1382773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1383773647a0SAndy Whitcroft next; 1384773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1385773647a0SAndy Whitcroft $sanitise_quote = ''; 138600df344fSAndy Whitcroft } 138700df344fSAndy Whitcroft } 1388773647a0SAndy Whitcroft 1389fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1390773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1391773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1392113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1393113f04a8SDaniel Walker substr($res, $off, 1, $;); 1394773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1395773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 139600df344fSAndy Whitcroft } else { 1397773647a0SAndy Whitcroft substr($res, $off, 1, $c); 139800df344fSAndy Whitcroft } 1399c2fdda0dSAndy Whitcroft } 1400c2fdda0dSAndy Whitcroft 1401113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1402113f04a8SDaniel Walker $sanitise_quote = ''; 1403113f04a8SDaniel Walker } 1404113f04a8SDaniel Walker 1405c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1406c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1407c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1408c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1409c2fdda0dSAndy Whitcroft 1410c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1411c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1412c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1413c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1414c2fdda0dSAndy Whitcroft } 1415c2fdda0dSAndy Whitcroft 1416dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1417dadf680dSJoe Perches my $match = $1; 1418dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1419dadf680dSJoe Perches } 1420dadf680dSJoe Perches 142100df344fSAndy Whitcroft return $res; 142200df344fSAndy Whitcroft} 142300df344fSAndy Whitcroft 1424a6962d72SJoe Perchessub get_quoted_string { 1425a6962d72SJoe Perches my ($line, $rawline) = @_; 1426a6962d72SJoe Perches 1427478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 142833acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1429a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1430a6962d72SJoe Perches} 1431a6962d72SJoe Perches 14328905a67cSAndy Whitcroftsub ctx_statement_block { 14338905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 14348905a67cSAndy Whitcroft my $line = $linenr - 1; 14358905a67cSAndy Whitcroft my $blk = ''; 14368905a67cSAndy Whitcroft my $soff = $off; 14378905a67cSAndy Whitcroft my $coff = $off - 1; 1438773647a0SAndy Whitcroft my $coff_set = 0; 14398905a67cSAndy Whitcroft 144013214adfSAndy Whitcroft my $loff = 0; 144113214adfSAndy Whitcroft 14428905a67cSAndy Whitcroft my $type = ''; 14438905a67cSAndy Whitcroft my $level = 0; 1444a2750645SAndy Whitcroft my @stack = (); 1445cf655043SAndy Whitcroft my $p; 14468905a67cSAndy Whitcroft my $c; 14478905a67cSAndy Whitcroft my $len = 0; 144813214adfSAndy Whitcroft 144913214adfSAndy Whitcroft my $remainder; 14508905a67cSAndy Whitcroft while (1) { 1451a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1452a2750645SAndy Whitcroft 1453773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 14548905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 14558905a67cSAndy Whitcroft # context. 14568905a67cSAndy Whitcroft if ($off >= $len) { 14578905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1458dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1459c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 14608905a67cSAndy Whitcroft $remain--; 146113214adfSAndy Whitcroft $loff = $len; 1462c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 14638905a67cSAndy Whitcroft $len = length($blk); 14648905a67cSAndy Whitcroft $line++; 14658905a67cSAndy Whitcroft last; 14668905a67cSAndy Whitcroft } 14678905a67cSAndy Whitcroft # Bail if there is no further context. 14688905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 146913214adfSAndy Whitcroft if ($off >= $len) { 14708905a67cSAndy Whitcroft last; 14718905a67cSAndy Whitcroft } 1472f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1473f74bd194SAndy Whitcroft $level++; 1474f74bd194SAndy Whitcroft $type = '#'; 1475f74bd194SAndy Whitcroft } 14768905a67cSAndy Whitcroft } 1477cf655043SAndy Whitcroft $p = $c; 14788905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 147913214adfSAndy Whitcroft $remainder = substr($blk, $off); 14808905a67cSAndy Whitcroft 1481773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 14824635f4fbSAndy Whitcroft 14834635f4fbSAndy Whitcroft # Handle nested #if/#else. 14844635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 14854635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 14864635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 14874635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 14884635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 14894635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 14904635f4fbSAndy Whitcroft } 14914635f4fbSAndy Whitcroft 14928905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 14938905a67cSAndy Whitcroft # outermost level. 14948905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 14958905a67cSAndy Whitcroft last; 14968905a67cSAndy Whitcroft } 14978905a67cSAndy Whitcroft 149813214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1499773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1500773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1501773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1502773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1503773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1504773647a0SAndy Whitcroft $coff_set = 1; 1505773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1506773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 150713214adfSAndy Whitcroft } 150813214adfSAndy Whitcroft 15098905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 15108905a67cSAndy Whitcroft $level++; 15118905a67cSAndy Whitcroft $type = '('; 15128905a67cSAndy Whitcroft } 15138905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 15148905a67cSAndy Whitcroft $level--; 15158905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 15168905a67cSAndy Whitcroft 15178905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 15188905a67cSAndy Whitcroft $coff = $off; 1519773647a0SAndy Whitcroft $coff_set = 1; 1520773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 15218905a67cSAndy Whitcroft } 15228905a67cSAndy Whitcroft } 15238905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 15248905a67cSAndy Whitcroft $level++; 15258905a67cSAndy Whitcroft $type = '{'; 15268905a67cSAndy Whitcroft } 15278905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 15288905a67cSAndy Whitcroft $level--; 15298905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 15308905a67cSAndy Whitcroft 15318905a67cSAndy Whitcroft if ($level == 0) { 1532b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1533b998e001SPatrick Pannuto $off++; 1534b998e001SPatrick Pannuto } 15358905a67cSAndy Whitcroft last; 15368905a67cSAndy Whitcroft } 15378905a67cSAndy Whitcroft } 1538f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1539f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1540f74bd194SAndy Whitcroft $level--; 1541f74bd194SAndy Whitcroft $type = ''; 1542f74bd194SAndy Whitcroft $off++; 1543f74bd194SAndy Whitcroft last; 1544f74bd194SAndy Whitcroft } 15458905a67cSAndy Whitcroft $off++; 15468905a67cSAndy Whitcroft } 1547a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 154813214adfSAndy Whitcroft if ($off == $len) { 1549a3bb97a7SAndy Whitcroft $loff = $len + 1; 155013214adfSAndy Whitcroft $line++; 155113214adfSAndy Whitcroft $remain--; 155213214adfSAndy Whitcroft } 15538905a67cSAndy Whitcroft 15548905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 15558905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 15568905a67cSAndy Whitcroft 15578905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 15588905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 15598905a67cSAndy Whitcroft 1560773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 156113214adfSAndy Whitcroft 156213214adfSAndy Whitcroft return ($statement, $condition, 156313214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 156413214adfSAndy Whitcroft} 156513214adfSAndy Whitcroft 1566cf655043SAndy Whitcroftsub statement_lines { 1567cf655043SAndy Whitcroft my ($stmt) = @_; 1568cf655043SAndy Whitcroft 1569cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1570cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1571cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1572cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1573cf655043SAndy Whitcroft 1574cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1575cf655043SAndy Whitcroft 1576cf655043SAndy Whitcroft return $#stmt_lines + 2; 1577cf655043SAndy Whitcroft} 1578cf655043SAndy Whitcroft 1579cf655043SAndy Whitcroftsub statement_rawlines { 1580cf655043SAndy Whitcroft my ($stmt) = @_; 1581cf655043SAndy Whitcroft 1582cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1583cf655043SAndy Whitcroft 1584cf655043SAndy Whitcroft return $#stmt_lines + 2; 1585cf655043SAndy Whitcroft} 1586cf655043SAndy Whitcroft 1587cf655043SAndy Whitcroftsub statement_block_size { 1588cf655043SAndy Whitcroft my ($stmt) = @_; 1589cf655043SAndy Whitcroft 1590cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1591cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1592cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1593cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1594cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1595cf655043SAndy Whitcroft 1596cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1597cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1598cf655043SAndy Whitcroft 1599cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1600cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1601cf655043SAndy Whitcroft 1602cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1603cf655043SAndy Whitcroft return $stmt_lines; 1604cf655043SAndy Whitcroft } else { 1605cf655043SAndy Whitcroft return $stmt_statements; 1606cf655043SAndy Whitcroft } 1607cf655043SAndy Whitcroft} 1608cf655043SAndy Whitcroft 160913214adfSAndy Whitcroftsub ctx_statement_full { 161013214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 161113214adfSAndy Whitcroft my ($statement, $condition, $level); 161213214adfSAndy Whitcroft 161313214adfSAndy Whitcroft my (@chunks); 161413214adfSAndy Whitcroft 1615cf655043SAndy Whitcroft # Grab the first conditional/block pair. 161613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 161713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1618773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 161913214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1620cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1621cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1622cf655043SAndy Whitcroft } 1623cf655043SAndy Whitcroft 1624cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1625cf655043SAndy Whitcroft # could continue the statement. 1626cf655043SAndy Whitcroft for (;;) { 162713214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 162813214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1629cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1630773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1631cf655043SAndy Whitcroft #print "C: push\n"; 1632cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 163313214adfSAndy Whitcroft } 163413214adfSAndy Whitcroft 163513214adfSAndy Whitcroft return ($level, $linenr, @chunks); 16368905a67cSAndy Whitcroft} 16378905a67cSAndy Whitcroft 16384a0df2efSAndy Whitcroftsub ctx_block_get { 1639f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 16404a0df2efSAndy Whitcroft my $line; 16414a0df2efSAndy Whitcroft my $start = $linenr - 1; 16424a0df2efSAndy Whitcroft my $blk = ''; 16434a0df2efSAndy Whitcroft my @o; 16444a0df2efSAndy Whitcroft my @c; 16454a0df2efSAndy Whitcroft my @res = (); 16464a0df2efSAndy Whitcroft 1647f0a594c1SAndy Whitcroft my $level = 0; 16484635f4fbSAndy Whitcroft my @stack = ($level); 164900df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 165000df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 165100df344fSAndy Whitcroft $remain--; 165200df344fSAndy Whitcroft 165300df344fSAndy Whitcroft $blk .= $rawlines[$line]; 16544635f4fbSAndy Whitcroft 16554635f4fbSAndy Whitcroft # Handle nested #if/#else. 165601464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 16574635f4fbSAndy Whitcroft push(@stack, $level); 165801464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 16594635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 166001464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 16614635f4fbSAndy Whitcroft $level = pop(@stack); 16624635f4fbSAndy Whitcroft } 16634635f4fbSAndy Whitcroft 166401464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1665f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1666f0a594c1SAndy Whitcroft if ($off > 0) { 1667f0a594c1SAndy Whitcroft $off--; 1668f0a594c1SAndy Whitcroft next; 1669f0a594c1SAndy Whitcroft } 16704a0df2efSAndy Whitcroft 1671f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1672f0a594c1SAndy Whitcroft $level--; 1673f0a594c1SAndy Whitcroft last if ($level == 0); 1674f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1675f0a594c1SAndy Whitcroft $level++; 1676f0a594c1SAndy Whitcroft } 1677f0a594c1SAndy Whitcroft } 16784a0df2efSAndy Whitcroft 1679f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 168000df344fSAndy Whitcroft push(@res, $rawlines[$line]); 16814a0df2efSAndy Whitcroft } 16824a0df2efSAndy Whitcroft 1683f0a594c1SAndy Whitcroft last if ($level == 0); 16844a0df2efSAndy Whitcroft } 16854a0df2efSAndy Whitcroft 1686f0a594c1SAndy Whitcroft return ($level, @res); 16874a0df2efSAndy Whitcroft} 16884a0df2efSAndy Whitcroftsub ctx_block_outer { 16894a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16904a0df2efSAndy Whitcroft 1691f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1692f0a594c1SAndy Whitcroft return @r; 16934a0df2efSAndy Whitcroft} 16944a0df2efSAndy Whitcroftsub ctx_block { 16954a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16964a0df2efSAndy Whitcroft 1697f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1698f0a594c1SAndy Whitcroft return @r; 1699653d4876SAndy Whitcroft} 1700653d4876SAndy Whitcroftsub ctx_statement { 1701f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1702f0a594c1SAndy Whitcroft 1703f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1704f0a594c1SAndy Whitcroft return @r; 1705f0a594c1SAndy Whitcroft} 1706f0a594c1SAndy Whitcroftsub ctx_block_level { 1707653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1708653d4876SAndy Whitcroft 1709f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 17104a0df2efSAndy Whitcroft} 17119c0ca6f9SAndy Whitcroftsub ctx_statement_level { 17129c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 17139c0ca6f9SAndy Whitcroft 17149c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 17159c0ca6f9SAndy Whitcroft} 17164a0df2efSAndy Whitcroft 17174a0df2efSAndy Whitcroftsub ctx_locate_comment { 17184a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17194a0df2efSAndy Whitcroft 1720a55ee0ccSJoe Perches # If c99 comment on the current line, or the line before or after 1721a55ee0ccSJoe Perches my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); 1722a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1723a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); 1724a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1725a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); 1726a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1727a55ee0ccSJoe Perches 17284a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1729a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 17304a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 17314a0df2efSAndy Whitcroft 17324a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 17334a0df2efSAndy Whitcroft # comment. 17344a0df2efSAndy Whitcroft my $in_comment = 0; 17354a0df2efSAndy Whitcroft $current_comment = ''; 17364a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 173700df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 173800df344fSAndy Whitcroft #warn " $line\n"; 17394a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 17404a0df2efSAndy Whitcroft $in_comment = 1; 17414a0df2efSAndy Whitcroft } 17424a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 17434a0df2efSAndy Whitcroft $in_comment = 1; 17444a0df2efSAndy Whitcroft } 17454a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 17464a0df2efSAndy Whitcroft $current_comment = ''; 17474a0df2efSAndy Whitcroft } 17484a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 17494a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 17504a0df2efSAndy Whitcroft $in_comment = 0; 17514a0df2efSAndy Whitcroft } 17524a0df2efSAndy Whitcroft } 17534a0df2efSAndy Whitcroft 17544a0df2efSAndy Whitcroft chomp($current_comment); 17554a0df2efSAndy Whitcroft return($current_comment); 17564a0df2efSAndy Whitcroft} 17574a0df2efSAndy Whitcroftsub ctx_has_comment { 17584a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17594a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 17604a0df2efSAndy Whitcroft 176100df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 17624a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 17634a0df2efSAndy Whitcroft 17644a0df2efSAndy Whitcroft return ($cmt ne ''); 17654a0df2efSAndy Whitcroft} 17664a0df2efSAndy Whitcroft 17674d001e4dSAndy Whitcroftsub raw_line { 17684d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 17694d001e4dSAndy Whitcroft 17704d001e4dSAndy Whitcroft my $offset = $linenr - 1; 17714d001e4dSAndy Whitcroft $cnt++; 17724d001e4dSAndy Whitcroft 17734d001e4dSAndy Whitcroft my $line; 17744d001e4dSAndy Whitcroft while ($cnt) { 17754d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 17764d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 17774d001e4dSAndy Whitcroft $cnt--; 17784d001e4dSAndy Whitcroft } 17794d001e4dSAndy Whitcroft 17804d001e4dSAndy Whitcroft return $line; 17814d001e4dSAndy Whitcroft} 17824d001e4dSAndy Whitcroft 17832a9f9d85STobin C. Hardingsub get_stat_real { 17842a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 17852a9f9d85STobin C. Harding 17862a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 17872a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 17882a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 17892a9f9d85STobin C. Harding } 17902a9f9d85STobin C. Harding 17912a9f9d85STobin C. Harding return $stat_real; 17922a9f9d85STobin C. Harding} 17932a9f9d85STobin C. Harding 1794e3d95a2aSTobin C. Hardingsub get_stat_here { 1795e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1796e3d95a2aSTobin C. Harding 1797e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1798e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1799e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1800e3d95a2aSTobin C. Harding } 1801e3d95a2aSTobin C. Harding 1802e3d95a2aSTobin C. Harding return $herectx; 1803e3d95a2aSTobin C. Harding} 1804e3d95a2aSTobin C. Harding 18050a920b5bSAndy Whitcroftsub cat_vet { 18060a920b5bSAndy Whitcroft my ($vet) = @_; 18079c0ca6f9SAndy Whitcroft my ($res, $coded); 18080a920b5bSAndy Whitcroft 18099c0ca6f9SAndy Whitcroft $res = ''; 18106c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 18116c72ffaaSAndy Whitcroft $res .= $1; 18126c72ffaaSAndy Whitcroft if ($2 ne '') { 18139c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 18146c72ffaaSAndy Whitcroft $res .= $coded; 18156c72ffaaSAndy Whitcroft } 18169c0ca6f9SAndy Whitcroft } 18179c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 18180a920b5bSAndy Whitcroft 18199c0ca6f9SAndy Whitcroft return $res; 18200a920b5bSAndy Whitcroft} 18210a920b5bSAndy Whitcroft 1822c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1823cf655043SAndy Whitcroftmy $av_pending; 1824c2fdda0dSAndy Whitcroftmy @av_paren_type; 18251f65f947SAndy Whitcroftmy $av_pend_colon; 1826c2fdda0dSAndy Whitcroft 1827c2fdda0dSAndy Whitcroftsub annotate_reset { 1828c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1829cf655043SAndy Whitcroft $av_pending = '_'; 1830cf655043SAndy Whitcroft @av_paren_type = ('E'); 18311f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1832c2fdda0dSAndy Whitcroft} 1833c2fdda0dSAndy Whitcroft 18346c72ffaaSAndy Whitcroftsub annotate_values { 18356c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 18366c72ffaaSAndy Whitcroft 18376c72ffaaSAndy Whitcroft my $res; 18381f65f947SAndy Whitcroft my $var = '_' x length($stream); 18396c72ffaaSAndy Whitcroft my $cur = $stream; 18406c72ffaaSAndy Whitcroft 1841c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 18426c72ffaaSAndy Whitcroft 18436c72ffaaSAndy Whitcroft while (length($cur)) { 1844773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1845cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1846171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 18476c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1848c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1849c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1850cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1851c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 18526c72ffaaSAndy Whitcroft } 18536c72ffaaSAndy Whitcroft 1854c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 18559446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 18569446ef56SAndy Whitcroft push(@av_paren_type, $type); 1857addcdceaSAndy Whitcroft $type = 'c'; 18589446ef56SAndy Whitcroft 1859e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1860c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 18616c72ffaaSAndy Whitcroft $type = 'T'; 18626c72ffaaSAndy Whitcroft 1863389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1864389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1865389a2fe5SAndy Whitcroft $type = 'T'; 1866389a2fe5SAndy Whitcroft 1867c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1868171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1869c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1870171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1871171ae1a4SAndy Whitcroft if ($2 ne '') { 1872cf655043SAndy Whitcroft $av_pending = 'N'; 1873171ae1a4SAndy Whitcroft } 1874171ae1a4SAndy Whitcroft $type = 'E'; 1875171ae1a4SAndy Whitcroft 1876c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1877171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1878171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1879171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 18806c72ffaaSAndy Whitcroft 1881c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1882cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1883c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1884cf655043SAndy Whitcroft 1885cf655043SAndy Whitcroft push(@av_paren_type, $type); 1886cf655043SAndy Whitcroft push(@av_paren_type, $type); 1887171ae1a4SAndy Whitcroft $type = 'E'; 1888cf655043SAndy Whitcroft 1889c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1890cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1891cf655043SAndy Whitcroft $av_preprocessor = 1; 1892cf655043SAndy Whitcroft 1893cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1894cf655043SAndy Whitcroft 1895171ae1a4SAndy Whitcroft $type = 'E'; 1896cf655043SAndy Whitcroft 1897c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1898cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1899cf655043SAndy Whitcroft 1900cf655043SAndy Whitcroft $av_preprocessor = 1; 1901cf655043SAndy Whitcroft 1902cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1903cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1904cf655043SAndy Whitcroft pop(@av_paren_type); 1905cf655043SAndy Whitcroft push(@av_paren_type, $type); 1906171ae1a4SAndy Whitcroft $type = 'E'; 19076c72ffaaSAndy Whitcroft 19086c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1909c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 19106c72ffaaSAndy Whitcroft 1911171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1912171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1913171ae1a4SAndy Whitcroft $av_pending = $type; 1914171ae1a4SAndy Whitcroft $type = 'N'; 1915171ae1a4SAndy Whitcroft 19166c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1917c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 19186c72ffaaSAndy Whitcroft if (defined $2) { 1919cf655043SAndy Whitcroft $av_pending = 'V'; 19206c72ffaaSAndy Whitcroft } 19216c72ffaaSAndy Whitcroft $type = 'N'; 19226c72ffaaSAndy Whitcroft 192314b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1924c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 192514b111c1SAndy Whitcroft $av_pending = 'E'; 19266c72ffaaSAndy Whitcroft $type = 'N'; 19276c72ffaaSAndy Whitcroft 19281f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 19291f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 19301f65f947SAndy Whitcroft $av_pend_colon = 'C'; 19311f65f947SAndy Whitcroft $type = 'N'; 19321f65f947SAndy Whitcroft 193314b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1934c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 19356c72ffaaSAndy Whitcroft $type = 'N'; 19366c72ffaaSAndy Whitcroft 19376c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1938c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1939cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1940cf655043SAndy Whitcroft $av_pending = '_'; 19416c72ffaaSAndy Whitcroft $type = 'N'; 19426c72ffaaSAndy Whitcroft 19436c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1944cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1945cf655043SAndy Whitcroft if ($new_type ne '_') { 1946cf655043SAndy Whitcroft $type = $new_type; 1947c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1948c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 19496c72ffaaSAndy Whitcroft } else { 1950c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 19516c72ffaaSAndy Whitcroft } 19526c72ffaaSAndy Whitcroft 1953c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1954c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1955c8cb2ca3SAndy Whitcroft $type = 'V'; 1956cf655043SAndy Whitcroft $av_pending = 'V'; 19576c72ffaaSAndy Whitcroft 19588e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 19598e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 19601f65f947SAndy Whitcroft $av_pend_colon = 'B'; 19618e761b04SAndy Whitcroft } elsif ($type eq 'E') { 19628e761b04SAndy Whitcroft $av_pend_colon = 'L'; 19631f65f947SAndy Whitcroft } 19641f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 19651f65f947SAndy Whitcroft $type = 'V'; 19661f65f947SAndy Whitcroft 19676c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1968c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 19696c72ffaaSAndy Whitcroft $type = 'V'; 19706c72ffaaSAndy Whitcroft 19716c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1972c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 19736c72ffaaSAndy Whitcroft $type = 'N'; 19746c72ffaaSAndy Whitcroft 1975cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1976c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 197713214adfSAndy Whitcroft $type = 'E'; 19781f65f947SAndy Whitcroft $av_pend_colon = 'O'; 197913214adfSAndy Whitcroft 19808e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 19818e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 19828e761b04SAndy Whitcroft $type = 'C'; 19838e761b04SAndy Whitcroft 19841f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 19851f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 19861f65f947SAndy Whitcroft $type = 'N'; 19871f65f947SAndy Whitcroft 19881f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 19891f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 19901f65f947SAndy Whitcroft 19911f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 19921f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 19931f65f947SAndy Whitcroft $type = 'E'; 19941f65f947SAndy Whitcroft } else { 19951f65f947SAndy Whitcroft $type = 'N'; 19961f65f947SAndy Whitcroft } 19971f65f947SAndy Whitcroft $av_pend_colon = 'O'; 19981f65f947SAndy Whitcroft 19998e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 200013214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 20016c72ffaaSAndy Whitcroft $type = 'N'; 20026c72ffaaSAndy Whitcroft 20030d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 200474048ed8SAndy Whitcroft my $variant; 200574048ed8SAndy Whitcroft 200674048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 200774048ed8SAndy Whitcroft if ($type eq 'V') { 200874048ed8SAndy Whitcroft $variant = 'B'; 200974048ed8SAndy Whitcroft } else { 201074048ed8SAndy Whitcroft $variant = 'U'; 201174048ed8SAndy Whitcroft } 201274048ed8SAndy Whitcroft 201374048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 201474048ed8SAndy Whitcroft $type = 'N'; 201574048ed8SAndy Whitcroft 20166c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 2017c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 20186c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 20196c72ffaaSAndy Whitcroft $type = 'N'; 20206c72ffaaSAndy Whitcroft } 20216c72ffaaSAndy Whitcroft 20226c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 2023c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 20246c72ffaaSAndy Whitcroft } 20256c72ffaaSAndy Whitcroft if (defined $1) { 20266c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 20276c72ffaaSAndy Whitcroft $res .= $type x length($1); 20286c72ffaaSAndy Whitcroft } 20296c72ffaaSAndy Whitcroft } 20306c72ffaaSAndy Whitcroft 20311f65f947SAndy Whitcroft return ($res, $var); 20326c72ffaaSAndy Whitcroft} 20336c72ffaaSAndy Whitcroft 20348905a67cSAndy Whitcroftsub possible { 203513214adfSAndy Whitcroft my ($possible, $line) = @_; 20369a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 20370776e594SAndy Whitcroft ^(?: 20380776e594SAndy Whitcroft $Modifier| 20390776e594SAndy Whitcroft $Storage| 20400776e594SAndy Whitcroft $Type| 20419a974fdbSAndy Whitcroft DEFINE_\S+ 20429a974fdbSAndy Whitcroft )$| 20439a974fdbSAndy Whitcroft ^(?: 20440776e594SAndy Whitcroft goto| 20450776e594SAndy Whitcroft return| 20460776e594SAndy Whitcroft case| 20470776e594SAndy Whitcroft else| 20480776e594SAndy Whitcroft asm|__asm__| 204989a88353SAndy Whitcroft do| 205089a88353SAndy Whitcroft \#| 205189a88353SAndy Whitcroft \#\#| 20529a974fdbSAndy Whitcroft )(?:\s|$)| 20530776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 20549a974fdbSAndy Whitcroft )}x; 20559a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 20569a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2057c45dcabdSAndy Whitcroft # Check for modifiers. 2058c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2059c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2060c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2061c45dcabdSAndy Whitcroft 2062c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2063c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2064d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 20659a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2066d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2067485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2068d2506586SAndy Whitcroft } 20699a974fdbSAndy Whitcroft } 2070c45dcabdSAndy Whitcroft 2071c45dcabdSAndy Whitcroft } else { 207213214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2073485ff23eSAlex Dowad push(@typeListFile, $possible); 2074c45dcabdSAndy Whitcroft } 20758905a67cSAndy Whitcroft build_types(); 20760776e594SAndy Whitcroft } else { 20770776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 20788905a67cSAndy Whitcroft } 20798905a67cSAndy Whitcroft} 20808905a67cSAndy Whitcroft 20816c72ffaaSAndy Whitcroftmy $prefix = ''; 20826c72ffaaSAndy Whitcroft 2083000d1cc1SJoe Perchessub show_type { 2084cbec18afSJoe Perches my ($type) = @_; 208591bfe484SJoe Perches 2086522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2087522b837cSAlexey Dobriyan 2088cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2089cbec18afSJoe Perches 2090cbec18afSJoe Perches return !defined $ignore_type{$type}; 2091000d1cc1SJoe Perches} 2092000d1cc1SJoe Perches 2093f0a594c1SAndy Whitcroftsub report { 2094cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2095cbec18afSJoe Perches 2096cbec18afSJoe Perches if (!show_type($type) || 2097cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2098773647a0SAndy Whitcroft return 0; 2099773647a0SAndy Whitcroft } 210057230297SJoe Perches my $output = ''; 2101737c0767SJohn Brooks if ($color) { 210257230297SJoe Perches if ($level eq 'ERROR') { 210357230297SJoe Perches $output .= RED; 210457230297SJoe Perches } elsif ($level eq 'WARNING') { 210557230297SJoe Perches $output .= YELLOW; 2106000d1cc1SJoe Perches } else { 210757230297SJoe Perches $output .= GREEN; 2108000d1cc1SJoe Perches } 210957230297SJoe Perches } 211057230297SJoe Perches $output .= $prefix . $level . ':'; 211157230297SJoe Perches if ($show_types) { 2112737c0767SJohn Brooks $output .= BLUE if ($color); 211357230297SJoe Perches $output .= "$type:"; 211457230297SJoe Perches } 2115737c0767SJohn Brooks $output .= RESET if ($color); 211657230297SJoe Perches $output .= ' ' . $msg . "\n"; 211734d8815fSJoe Perches 211834d8815fSJoe Perches if ($showfile) { 211934d8815fSJoe Perches my @lines = split("\n", $output, -1); 212034d8815fSJoe Perches splice(@lines, 1, 1); 212134d8815fSJoe Perches $output = join("\n", @lines); 212234d8815fSJoe Perches } 212357230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 21248905a67cSAndy Whitcroft 212557230297SJoe Perches push(our @report, $output); 2126773647a0SAndy Whitcroft 2127773647a0SAndy Whitcroft return 1; 2128f0a594c1SAndy Whitcroft} 2129cbec18afSJoe Perches 2130f0a594c1SAndy Whitcroftsub report_dump { 213113214adfSAndy Whitcroft our @report; 2132f0a594c1SAndy Whitcroft} 2133000d1cc1SJoe Perches 2134d752fcc8SJoe Perchessub fixup_current_range { 2135d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2136d752fcc8SJoe Perches 2137d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2138d752fcc8SJoe Perches my $o = $1; 2139d752fcc8SJoe Perches my $l = $2; 2140d752fcc8SJoe Perches my $no = $o + $offset; 2141d752fcc8SJoe Perches my $nl = $l + $length; 2142d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2143d752fcc8SJoe Perches } 2144d752fcc8SJoe Perches} 2145d752fcc8SJoe Perches 2146d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2147d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2148d752fcc8SJoe Perches 2149d752fcc8SJoe Perches my $range_last_linenr = 0; 2150d752fcc8SJoe Perches my $delta_offset = 0; 2151d752fcc8SJoe Perches 2152d752fcc8SJoe Perches my $old_linenr = 0; 2153d752fcc8SJoe Perches my $new_linenr = 0; 2154d752fcc8SJoe Perches 2155d752fcc8SJoe Perches my $next_insert = 0; 2156d752fcc8SJoe Perches my $next_delete = 0; 2157d752fcc8SJoe Perches 2158d752fcc8SJoe Perches my @lines = (); 2159d752fcc8SJoe Perches 2160d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2161d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2162d752fcc8SJoe Perches 2163d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2164d752fcc8SJoe Perches my $save_line = 1; 2165d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2166323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2167d752fcc8SJoe Perches $delta_offset = 0; 2168d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2169d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2170d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2171d752fcc8SJoe Perches } 2172d752fcc8SJoe Perches 2173d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2174d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2175d752fcc8SJoe Perches $save_line = 0; 2176d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2177d752fcc8SJoe Perches } 2178d752fcc8SJoe Perches 2179d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2180d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2181d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2182d752fcc8SJoe Perches $new_linenr++; 2183d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2184d752fcc8SJoe Perches } 2185d752fcc8SJoe Perches 2186d752fcc8SJoe Perches if ($save_line) { 2187d752fcc8SJoe Perches push(@lines, $line); 2188d752fcc8SJoe Perches $new_linenr++; 2189d752fcc8SJoe Perches } 2190d752fcc8SJoe Perches 2191d752fcc8SJoe Perches $old_linenr++; 2192d752fcc8SJoe Perches } 2193d752fcc8SJoe Perches 2194d752fcc8SJoe Perches return @lines; 2195d752fcc8SJoe Perches} 2196d752fcc8SJoe Perches 2197f2d7e4d4SJoe Perchessub fix_insert_line { 2198f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2199f2d7e4d4SJoe Perches 2200f2d7e4d4SJoe Perches my $inserted = { 2201f2d7e4d4SJoe Perches LINENR => $linenr, 2202f2d7e4d4SJoe Perches LINE => $line, 2203f2d7e4d4SJoe Perches }; 2204f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2205f2d7e4d4SJoe Perches} 2206f2d7e4d4SJoe Perches 2207f2d7e4d4SJoe Perchessub fix_delete_line { 2208f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2209f2d7e4d4SJoe Perches 2210f2d7e4d4SJoe Perches my $deleted = { 2211f2d7e4d4SJoe Perches LINENR => $linenr, 2212f2d7e4d4SJoe Perches LINE => $line, 2213f2d7e4d4SJoe Perches }; 2214f2d7e4d4SJoe Perches 2215f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2216f2d7e4d4SJoe Perches} 2217f2d7e4d4SJoe Perches 2218de7d4f0eSAndy Whitcroftsub ERROR { 2219cbec18afSJoe Perches my ($type, $msg) = @_; 2220cbec18afSJoe Perches 2221cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2222de7d4f0eSAndy Whitcroft our $clean = 0; 22236c72ffaaSAndy Whitcroft our $cnt_error++; 22243705ce5bSJoe Perches return 1; 2225de7d4f0eSAndy Whitcroft } 22263705ce5bSJoe Perches return 0; 2227773647a0SAndy Whitcroft} 2228de7d4f0eSAndy Whitcroftsub WARN { 2229cbec18afSJoe Perches my ($type, $msg) = @_; 2230cbec18afSJoe Perches 2231cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2232de7d4f0eSAndy Whitcroft our $clean = 0; 22336c72ffaaSAndy Whitcroft our $cnt_warn++; 22343705ce5bSJoe Perches return 1; 2235de7d4f0eSAndy Whitcroft } 22363705ce5bSJoe Perches return 0; 2237773647a0SAndy Whitcroft} 2238de7d4f0eSAndy Whitcroftsub CHK { 2239cbec18afSJoe Perches my ($type, $msg) = @_; 2240cbec18afSJoe Perches 2241cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2242de7d4f0eSAndy Whitcroft our $clean = 0; 22436c72ffaaSAndy Whitcroft our $cnt_chk++; 22443705ce5bSJoe Perches return 1; 22456c72ffaaSAndy Whitcroft } 22463705ce5bSJoe Perches return 0; 2247de7d4f0eSAndy Whitcroft} 2248de7d4f0eSAndy Whitcroft 22496ecd9674SAndy Whitcroftsub check_absolute_file { 22506ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 22516ecd9674SAndy Whitcroft my $file = $absolute; 22526ecd9674SAndy Whitcroft 22536ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 22546ecd9674SAndy Whitcroft 22556ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 22566ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 22576ecd9674SAndy Whitcroft if (-f "$root/$file") { 22586ecd9674SAndy Whitcroft ##print "file<$file>\n"; 22596ecd9674SAndy Whitcroft last; 22606ecd9674SAndy Whitcroft } 22616ecd9674SAndy Whitcroft } 22626ecd9674SAndy Whitcroft if (! -f _) { 22636ecd9674SAndy Whitcroft return 0; 22646ecd9674SAndy Whitcroft } 22656ecd9674SAndy Whitcroft 22666ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 22676ecd9674SAndy Whitcroft my $prefix = $absolute; 22686ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 22696ecd9674SAndy Whitcroft 22706ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 22716ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2272000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2273000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 22746ecd9674SAndy Whitcroft } 22756ecd9674SAndy Whitcroft} 22766ecd9674SAndy Whitcroft 22773705ce5bSJoe Perchessub trim { 22783705ce5bSJoe Perches my ($string) = @_; 22793705ce5bSJoe Perches 2280b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2281b34c648bSJoe Perches 2282b34c648bSJoe Perches return $string; 2283b34c648bSJoe Perches} 2284b34c648bSJoe Perches 2285b34c648bSJoe Perchessub ltrim { 2286b34c648bSJoe Perches my ($string) = @_; 2287b34c648bSJoe Perches 2288b34c648bSJoe Perches $string =~ s/^\s+//; 2289b34c648bSJoe Perches 2290b34c648bSJoe Perches return $string; 2291b34c648bSJoe Perches} 2292b34c648bSJoe Perches 2293b34c648bSJoe Perchessub rtrim { 2294b34c648bSJoe Perches my ($string) = @_; 2295b34c648bSJoe Perches 2296b34c648bSJoe Perches $string =~ s/\s+$//; 22973705ce5bSJoe Perches 22983705ce5bSJoe Perches return $string; 22993705ce5bSJoe Perches} 23003705ce5bSJoe Perches 230152ea8506SJoe Perchessub string_find_replace { 230252ea8506SJoe Perches my ($string, $find, $replace) = @_; 230352ea8506SJoe Perches 230452ea8506SJoe Perches $string =~ s/$find/$replace/g; 230552ea8506SJoe Perches 230652ea8506SJoe Perches return $string; 230752ea8506SJoe Perches} 230852ea8506SJoe Perches 23093705ce5bSJoe Perchessub tabify { 23103705ce5bSJoe Perches my ($leading) = @_; 23113705ce5bSJoe Perches 2312713a09deSAntonio Borneo my $source_indent = $tabsize; 23133705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 23143705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 23153705ce5bSJoe Perches 23163705ce5bSJoe Perches #convert leading spaces to tabs 23173705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 23183705ce5bSJoe Perches #Remove spaces before a tab 23193705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 23203705ce5bSJoe Perches 23213705ce5bSJoe Perches return "$leading"; 23223705ce5bSJoe Perches} 23233705ce5bSJoe Perches 2324d1fe9c09SJoe Perchessub pos_last_openparen { 2325d1fe9c09SJoe Perches my ($line) = @_; 2326d1fe9c09SJoe Perches 2327d1fe9c09SJoe Perches my $pos = 0; 2328d1fe9c09SJoe Perches 2329d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2330d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2331d1fe9c09SJoe Perches 2332d1fe9c09SJoe Perches my $last_openparen = 0; 2333d1fe9c09SJoe Perches 2334d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2335d1fe9c09SJoe Perches return -1; 2336d1fe9c09SJoe Perches } 2337d1fe9c09SJoe Perches 2338d1fe9c09SJoe Perches my $len = length($line); 2339d1fe9c09SJoe Perches 2340d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2341d1fe9c09SJoe Perches my $string = substr($line, $pos); 2342d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2343d1fe9c09SJoe Perches $pos += length($1) - 1; 2344d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2345d1fe9c09SJoe Perches $last_openparen = $pos; 2346d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2347d1fe9c09SJoe Perches last; 2348d1fe9c09SJoe Perches } 2349d1fe9c09SJoe Perches } 2350d1fe9c09SJoe Perches 235191cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2352d1fe9c09SJoe Perches} 2353d1fe9c09SJoe Perches 2354f36d3eb8SJoe Perchessub get_raw_comment { 2355f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2356f36d3eb8SJoe Perches my $comment = ''; 2357f36d3eb8SJoe Perches 2358f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2359f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2360f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2361f36d3eb8SJoe Perches } 2362f36d3eb8SJoe Perches } 2363f36d3eb8SJoe Perches 2364f36d3eb8SJoe Perches return $comment; 2365f36d3eb8SJoe Perches} 2366f36d3eb8SJoe Perches 23670a920b5bSAndy Whitcroftsub process { 23680a920b5bSAndy Whitcroft my $filename = shift; 23690a920b5bSAndy Whitcroft 23700a920b5bSAndy Whitcroft my $linenr=0; 23710a920b5bSAndy Whitcroft my $prevline=""; 2372c2fdda0dSAndy Whitcroft my $prevrawline=""; 23730a920b5bSAndy Whitcroft my $stashline=""; 2374c2fdda0dSAndy Whitcroft my $stashrawline=""; 23750a920b5bSAndy Whitcroft 23764a0df2efSAndy Whitcroft my $length; 23770a920b5bSAndy Whitcroft my $indent; 23780a920b5bSAndy Whitcroft my $previndent=0; 23790a920b5bSAndy Whitcroft my $stashindent=0; 23800a920b5bSAndy Whitcroft 2381de7d4f0eSAndy Whitcroft our $clean = 1; 23820a920b5bSAndy Whitcroft my $signoff = 0; 2383cd261496SGeert Uytterhoeven my $author = ''; 2384cd261496SGeert Uytterhoeven my $authorsignoff = 0; 238548ca2d8aSDwaipayan Ray my $author_sob = ''; 23860a920b5bSAndy Whitcroft my $is_patch = 0; 2387133712a2SRob Herring my $is_binding_patch = -1; 238829ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 238915662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 239044d303ebSJoe Perches my $has_patch_separator = 0; #Found a --- line 2391ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2392490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2393bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 23942a076f40SJoe Perches my $commit_log_long_line = 0; 2395e518e9a5SJoe Perches my $commit_log_has_diff = 0; 239613f1937eSJoe Perches my $reported_maintainer_file = 0; 2397fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2398fa64205dSPasi Savanainen 2399365dd4eaSJoe Perches my $last_blank_line = 0; 24005e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2401365dd4eaSJoe Perches 240213214adfSAndy Whitcroft our @report = (); 24036c72ffaaSAndy Whitcroft our $cnt_lines = 0; 24046c72ffaaSAndy Whitcroft our $cnt_error = 0; 24056c72ffaaSAndy Whitcroft our $cnt_warn = 0; 24066c72ffaaSAndy Whitcroft our $cnt_chk = 0; 24076c72ffaaSAndy Whitcroft 24080a920b5bSAndy Whitcroft # Trace the real file/line as we go. 24090a920b5bSAndy Whitcroft my $realfile = ''; 24100a920b5bSAndy Whitcroft my $realline = 0; 24110a920b5bSAndy Whitcroft my $realcnt = 0; 24120a920b5bSAndy Whitcroft my $here = ''; 241377cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 24140a920b5bSAndy Whitcroft my $in_comment = 0; 2415c2fdda0dSAndy Whitcroft my $comment_edge = 0; 24160a920b5bSAndy Whitcroft my $first_line = 0; 24171e855726SWolfram Sang my $p1_prefix = ''; 24180a920b5bSAndy Whitcroft 241913214adfSAndy Whitcroft my $prev_values = 'E'; 242013214adfSAndy Whitcroft 242113214adfSAndy Whitcroft # suppression flags 2422773647a0SAndy Whitcroft my %suppress_ifbraces; 2423170d3a22SAndy Whitcroft my %suppress_whiletrailers; 24242b474a1aSAndy Whitcroft my %suppress_export; 24253e469cdcSAndy Whitcroft my $suppress_statement = 0; 2426653d4876SAndy Whitcroft 24277e51f197SJoe Perches my %signatures = (); 2428323c1260SJoe Perches 2429c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2430de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2431c2fdda0dSAndy Whitcroft # 2432de7d4f0eSAndy Whitcroft my @setup_docs = (); 2433de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2434773647a0SAndy Whitcroft 2435d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2436d8b07710SJoe Perches 24379f3a8992SRob Herring my $checklicenseline = 1; 24389f3a8992SRob Herring 2439773647a0SAndy Whitcroft sanitise_line_reset(); 2440c2fdda0dSAndy Whitcroft my $line; 2441c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2442773647a0SAndy Whitcroft $linenr++; 2443773647a0SAndy Whitcroft $line = $rawline; 2444c2fdda0dSAndy Whitcroft 24453705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 24463705ce5bSJoe Perches 2447773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2448de7d4f0eSAndy Whitcroft $setup_docs = 0; 24492581ac7cSTim Froidcoeur if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { 2450de7d4f0eSAndy Whitcroft $setup_docs = 1; 2451de7d4f0eSAndy Whitcroft } 2452773647a0SAndy Whitcroft #next; 2453de7d4f0eSAndy Whitcroft } 245474fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2455773647a0SAndy Whitcroft $realline=$1-1; 2456773647a0SAndy Whitcroft if (defined $2) { 2457773647a0SAndy Whitcroft $realcnt=$3+1; 2458773647a0SAndy Whitcroft } else { 2459773647a0SAndy Whitcroft $realcnt=1+1; 2460773647a0SAndy Whitcroft } 2461c45dcabdSAndy Whitcroft $in_comment = 0; 2462773647a0SAndy Whitcroft 2463773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2464773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2465773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2466773647a0SAndy Whitcroft # at context start. 2467773647a0SAndy Whitcroft my $edge; 246801fa9147SAndy Whitcroft my $cnt = $realcnt; 246901fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 247001fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 247101fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 247201fa9147SAndy Whitcroft $cnt--; 247301fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2474721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2475fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2476fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2477fae17daeSAndy Whitcroft ($edge) = $1; 2478fae17daeSAndy Whitcroft last; 2479fae17daeSAndy Whitcroft } 2480773647a0SAndy Whitcroft } 2481773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2482773647a0SAndy Whitcroft $in_comment = 1; 2483773647a0SAndy Whitcroft } 2484773647a0SAndy Whitcroft 2485773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2486773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2487773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2488773647a0SAndy Whitcroft if (!defined $edge && 248983242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2490773647a0SAndy Whitcroft { 2491773647a0SAndy Whitcroft $in_comment = 1; 2492773647a0SAndy Whitcroft } 2493773647a0SAndy Whitcroft 2494773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2495773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2496773647a0SAndy Whitcroft 2497171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2498773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2499171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2500773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2501773647a0SAndy Whitcroft } 2502773647a0SAndy Whitcroft push(@lines, $line); 2503773647a0SAndy Whitcroft 2504773647a0SAndy Whitcroft if ($realcnt > 1) { 2505773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2506773647a0SAndy Whitcroft } else { 2507773647a0SAndy Whitcroft $realcnt = 0; 2508773647a0SAndy Whitcroft } 2509773647a0SAndy Whitcroft 2510773647a0SAndy Whitcroft #print "==>$rawline\n"; 2511773647a0SAndy Whitcroft #print "-->$line\n"; 2512de7d4f0eSAndy Whitcroft 2513de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2514de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2515de7d4f0eSAndy Whitcroft } 2516de7d4f0eSAndy Whitcroft } 2517de7d4f0eSAndy Whitcroft 25186c72ffaaSAndy Whitcroft $prefix = ''; 25196c72ffaaSAndy Whitcroft 2520773647a0SAndy Whitcroft $realcnt = 0; 2521773647a0SAndy Whitcroft $linenr = 0; 2522194f66fcSJoe Perches $fixlinenr = -1; 25230a920b5bSAndy Whitcroft foreach my $line (@lines) { 25240a920b5bSAndy Whitcroft $linenr++; 2525194f66fcSJoe Perches $fixlinenr++; 25261b5539b1SJoe Perches my $sline = $line; #copy of $line 25271b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 25280a920b5bSAndy Whitcroft 2529c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2530f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 25316c72ffaaSAndy Whitcroft 253212c253abSJoe Perches# check if it's a mode change, rename or start of a patch 253312c253abSJoe Perches if (!$in_commit_log && 253412c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 253512c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 253612c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 253712c253abSJoe Perches $is_patch = 1; 253812c253abSJoe Perches } 253912c253abSJoe Perches 25400a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2541e518e9a5SJoe Perches if (!$in_commit_log && 254274fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 254374fd4f34SJoe Perches my $context = $4; 25440a920b5bSAndy Whitcroft $is_patch = 1; 25454a0df2efSAndy Whitcroft $first_line = $linenr + 1; 25460a920b5bSAndy Whitcroft $realline=$1-1; 25470a920b5bSAndy Whitcroft if (defined $2) { 25480a920b5bSAndy Whitcroft $realcnt=$3+1; 25490a920b5bSAndy Whitcroft } else { 25500a920b5bSAndy Whitcroft $realcnt=1+1; 25510a920b5bSAndy Whitcroft } 2552c2fdda0dSAndy Whitcroft annotate_reset(); 255313214adfSAndy Whitcroft $prev_values = 'E'; 255413214adfSAndy Whitcroft 2555773647a0SAndy Whitcroft %suppress_ifbraces = (); 2556170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 25572b474a1aSAndy Whitcroft %suppress_export = (); 25583e469cdcSAndy Whitcroft $suppress_statement = 0; 255974fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 256074fd4f34SJoe Perches $context_function = $1; 256174fd4f34SJoe Perches } else { 256274fd4f34SJoe Perches undef $context_function; 256374fd4f34SJoe Perches } 25640a920b5bSAndy Whitcroft next; 25650a920b5bSAndy Whitcroft 25664a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 25674a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 25684a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2569773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 25700a920b5bSAndy Whitcroft $realline++; 2571d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 25720a920b5bSAndy Whitcroft 25734a0df2efSAndy Whitcroft # Measure the line length and indent. 2574c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 25750a920b5bSAndy Whitcroft 25760a920b5bSAndy Whitcroft # Track the previous line. 25770a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 25780a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2579c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2580c2fdda0dSAndy Whitcroft 2581773647a0SAndy Whitcroft #warn "line<$line>\n"; 25826c72ffaaSAndy Whitcroft 2583d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2584d8aaf121SAndy Whitcroft $realcnt--; 25850a920b5bSAndy Whitcroft } 25860a920b5bSAndy Whitcroft 2587cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2588cc77cdcaSAndy Whitcroft 25896c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 25906c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2591773647a0SAndy Whitcroft 25922ac73b4fSJoe Perches my $found_file = 0; 2593773647a0SAndy Whitcroft # extract the filename as it passes 25943bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 25953bf9a009SRabin Vincent $realfile = $1; 25962b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2597270c49a0SJoe Perches $in_commit_log = 0; 25982ac73b4fSJoe Perches $found_file = 1; 25993bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2600773647a0SAndy Whitcroft $realfile = $1; 26012b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2602270c49a0SJoe Perches $in_commit_log = 0; 26031e855726SWolfram Sang 26041e855726SWolfram Sang $p1_prefix = $1; 2605e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2606e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2607000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2608000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 26091e855726SWolfram Sang } 2610773647a0SAndy Whitcroft 2611c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2612000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2613000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2614773647a0SAndy Whitcroft } 26152ac73b4fSJoe Perches $found_file = 1; 26162ac73b4fSJoe Perches } 26172ac73b4fSJoe Perches 261834d8815fSJoe Perches#make up the handle for any error we report on this line 261934d8815fSJoe Perches if ($showfile) { 262034d8815fSJoe Perches $prefix = "$realfile:$realline: " 262134d8815fSJoe Perches } elsif ($emacs) { 26227d3a9f67SJoe Perches if ($file) { 26237d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 26247d3a9f67SJoe Perches } else { 262534d8815fSJoe Perches $prefix = "$filename:$linenr: "; 262634d8815fSJoe Perches } 26277d3a9f67SJoe Perches } 262834d8815fSJoe Perches 26292ac73b4fSJoe Perches if ($found_file) { 263085b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 263185b0ee18SJoe Perches WARN("OBSOLETE", 263285b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 263385b0ee18SJoe Perches } 26347bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 26352ac73b4fSJoe Perches $check = 1; 26362ac73b4fSJoe Perches } else { 26372ac73b4fSJoe Perches $check = $check_orig; 26382ac73b4fSJoe Perches } 26399f3a8992SRob Herring $checklicenseline = 1; 2640133712a2SRob Herring 2641133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2642133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2643133712a2SRob Herring 2644133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2645133712a2SRob Herring 2646133712a2SRob Herring if (($last_binding_patch != -1) && 2647133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2648133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2649858e6845SMauro Carvalho Chehab "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); 2650133712a2SRob Herring } 2651133712a2SRob Herring } 2652133712a2SRob Herring 2653773647a0SAndy Whitcroft next; 2654773647a0SAndy Whitcroft } 2655773647a0SAndy Whitcroft 2656389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 26570a920b5bSAndy Whitcroft 2658c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2659c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2660c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 26610a920b5bSAndy Whitcroft 26626c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 26636c72ffaaSAndy Whitcroft 2664490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2665490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2666490b292cSJoe Perches if ($in_commit_log) { 2667490b292cSJoe Perches if ($line !~ /^\s*$/) { 2668490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2669490b292cSJoe Perches } 2670490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2671490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2672490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2673490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2674490b292cSJoe Perches } 2675490b292cSJoe Perches 2676e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2677e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 267813e45417SMrinal Pandey (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && 267913e45417SMrinal Pandey $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || 2680e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2681e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2682e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2683e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2684e518e9a5SJoe Perches $commit_log_has_diff = 1; 2685e518e9a5SJoe Perches } 2686e518e9a5SJoe Perches 26873bf9a009SRabin Vincent# Check for incorrect file permissions 26883bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 26893bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 269004db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 269104db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2692000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2693000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 26943bf9a009SRabin Vincent } 26953bf9a009SRabin Vincent } 26963bf9a009SRabin Vincent 2697cd261496SGeert Uytterhoeven# Check the patch for a From: 2698cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2699cd261496SGeert Uytterhoeven $author = $1; 2700e7f929f3SDwaipayan Ray my $curline = $linenr; 2701e7f929f3SDwaipayan Ray while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { 2702e7f929f3SDwaipayan Ray $author .= $1; 2703e7f929f3SDwaipayan Ray } 2704cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2705cd261496SGeert Uytterhoeven $author =~ s/"//g; 2706dfa05c28SJoe Perches $author = reformat_email($author); 2707cd261496SGeert Uytterhoeven } 2708cd261496SGeert Uytterhoeven 270920112475SJoe Perches# Check the patch for a signoff: 2710dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 27114a0df2efSAndy Whitcroft $signoff++; 271215662b3eSJoe Perches $in_commit_log = 0; 271348ca2d8aSDwaipayan Ray if ($author ne '' && $authorsignoff != 1) { 271448ca2d8aSDwaipayan Ray if (same_email_addresses($1, $author, 1)) { 2715cd261496SGeert Uytterhoeven $authorsignoff = 1; 271648ca2d8aSDwaipayan Ray } else { 271748ca2d8aSDwaipayan Ray my $ctx = $1; 271848ca2d8aSDwaipayan Ray my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); 271948ca2d8aSDwaipayan Ray my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); 272048ca2d8aSDwaipayan Ray 272148ca2d8aSDwaipayan Ray if ($email_address eq $author_address && $email_name eq $author_name) { 272248ca2d8aSDwaipayan Ray $author_sob = $ctx; 272348ca2d8aSDwaipayan Ray $authorsignoff = 2; 272448ca2d8aSDwaipayan Ray } elsif ($email_address eq $author_address) { 272548ca2d8aSDwaipayan Ray $author_sob = $ctx; 272648ca2d8aSDwaipayan Ray $authorsignoff = 3; 272748ca2d8aSDwaipayan Ray } elsif ($email_name eq $author_name) { 272848ca2d8aSDwaipayan Ray $author_sob = $ctx; 272948ca2d8aSDwaipayan Ray $authorsignoff = 4; 273048ca2d8aSDwaipayan Ray 273148ca2d8aSDwaipayan Ray my $address1 = $email_address; 273248ca2d8aSDwaipayan Ray my $address2 = $author_address; 273348ca2d8aSDwaipayan Ray 273448ca2d8aSDwaipayan Ray if ($address1 =~ /(\S+)\+\S+(\@.*)/) { 273548ca2d8aSDwaipayan Ray $address1 = "$1$2"; 273648ca2d8aSDwaipayan Ray } 273748ca2d8aSDwaipayan Ray if ($address2 =~ /(\S+)\+\S+(\@.*)/) { 273848ca2d8aSDwaipayan Ray $address2 = "$1$2"; 273948ca2d8aSDwaipayan Ray } 274048ca2d8aSDwaipayan Ray if ($address1 eq $address2) { 274148ca2d8aSDwaipayan Ray $authorsignoff = 5; 274248ca2d8aSDwaipayan Ray } 274348ca2d8aSDwaipayan Ray } 2744cd261496SGeert Uytterhoeven } 2745cd261496SGeert Uytterhoeven } 27460a920b5bSAndy Whitcroft } 274720112475SJoe Perches 274844d303ebSJoe Perches# Check for patch separator 274944d303ebSJoe Perches if ($line =~ /^---$/) { 275044d303ebSJoe Perches $has_patch_separator = 1; 275144d303ebSJoe Perches $in_commit_log = 0; 275244d303ebSJoe Perches } 275344d303ebSJoe Perches 2754e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2755e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2756e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2757e0d975b1SJoe Perches $reported_maintainer_file = 1; 2758e0d975b1SJoe Perches } 2759e0d975b1SJoe Perches 276020112475SJoe Perches# Check signature styles 2761270c49a0SJoe Perches if (!$in_header_lines && 2762ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 276320112475SJoe Perches my $space_before = $1; 276420112475SJoe Perches my $sign_off = $2; 276520112475SJoe Perches my $space_after = $3; 276620112475SJoe Perches my $email = $4; 276720112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 276820112475SJoe Perches 2769ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2770ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2771ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2772ce0338dfSJoe Perches } 277320112475SJoe Perches if (defined $space_before && $space_before ne "") { 27743705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27753705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 27763705ce5bSJoe Perches $fix) { 2777194f66fcSJoe Perches $fixed[$fixlinenr] = 27783705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27793705ce5bSJoe Perches } 278020112475SJoe Perches } 278120112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 27823705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27833705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 27843705ce5bSJoe Perches $fix) { 2785194f66fcSJoe Perches $fixed[$fixlinenr] = 27863705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27873705ce5bSJoe Perches } 27883705ce5bSJoe Perches 278920112475SJoe Perches } 279020112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 27913705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27923705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 27933705ce5bSJoe Perches $fix) { 2794194f66fcSJoe Perches $fixed[$fixlinenr] = 27953705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27963705ce5bSJoe Perches } 279720112475SJoe Perches } 279820112475SJoe Perches 2799dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 280048ca2d8aSDwaipayan Ray my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); 280120112475SJoe Perches if ($suggested_email eq "") { 2802000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2803000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 280420112475SJoe Perches } else { 280520112475SJoe Perches my $dequoted = $suggested_email; 280620112475SJoe Perches $dequoted =~ s/^"//; 280720112475SJoe Perches $dequoted =~ s/" </ </; 280820112475SJoe Perches # Don't force email to have quotes 280920112475SJoe Perches # Allow just an angle bracketed address 281048ca2d8aSDwaipayan Ray if (!same_email_addresses($email, $suggested_email, 0)) { 2811000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 281248ca2d8aSDwaipayan Ray "email address '$email' might be better as '$suggested_email'\n" . $herecurr); 281320112475SJoe Perches } 28140a920b5bSAndy Whitcroft } 28157e51f197SJoe Perches 28167e51f197SJoe Perches# Check for duplicate signatures 28177e51f197SJoe Perches my $sig_nospace = $line; 28187e51f197SJoe Perches $sig_nospace =~ s/\s//g; 28197e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 28207e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 28217e51f197SJoe Perches WARN("BAD_SIGN_OFF", 28227e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 28237e51f197SJoe Perches } else { 28247e51f197SJoe Perches $signatures{$sig_nospace} = 1; 28257e51f197SJoe Perches } 28266c5d24eeSSean Christopherson 28276c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 28286c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 28296c5d24eeSSean Christopherson if ($email eq $author) { 28306c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28316c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 28326c5d24eeSSean Christopherson } 28336c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 28346c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28356c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 28366c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 28376c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28386c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 28396c5d24eeSSean Christopherson } elsif ($1 ne $email) { 28406c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28416c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 28426c5d24eeSSean Christopherson } 28436c5d24eeSSean Christopherson } 28440a920b5bSAndy Whitcroft } 28450a920b5bSAndy Whitcroft 2846a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2847a2fe16b9SJoe Perches if ($in_header_lines && 2848a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2849a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2850a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2851a2fe16b9SJoe Perches } 2852a2fe16b9SJoe Perches 285344d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context 285444d303ebSJoe Perches if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 28557ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 285644d303ebSJoe Perches "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr); 28577ebd05efSChristopher Covington } 28587ebd05efSChristopher Covington 2859369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2860369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2861369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2862369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2863369c8dd3SJoe Perches # timestamp 2864634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 2865634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 2866634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 2867634cffccSJoe Perches # stack dump address styles 2868369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2869369c8dd3SJoe Perches } 2870369c8dd3SJoe Perches 28712a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 28722a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2873bf4daf12SJoe Perches length($line) > 75 && 2874bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2875bf4daf12SJoe Perches # file delta changes 2876bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2877bf4daf12SJoe Perches # filename then : 2878bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2879bf4daf12SJoe Perches # A Fixes: or Link: line 2880bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 28812a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 28822a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 28832a076f40SJoe Perches $commit_log_long_line = 1; 28842a076f40SJoe Perches } 28852a076f40SJoe Perches 2886bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2887bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2888bf4daf12SJoe Perches $line =~ /^\s*$/) { 2889bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2890bf4daf12SJoe Perches } 2891bf4daf12SJoe Perches 28920d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2893369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2894a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 2895e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2896fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2897aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2898369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2899bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2900fe043ea1SJoe Perches my $init_char = "c"; 2901fe043ea1SJoe Perches my $orig_commit = ""; 29020d7835fcSJoe Perches my $short = 1; 29030d7835fcSJoe Perches my $long = 0; 29040d7835fcSJoe Perches my $case = 1; 29050d7835fcSJoe Perches my $space = 1; 29060d7835fcSJoe Perches my $hasdesc = 0; 290719c146a6SJoe Perches my $hasparens = 0; 29080d7835fcSJoe Perches my $id = '0123456789ab'; 29090d7835fcSJoe Perches my $orig_desc = "commit description"; 29100d7835fcSJoe Perches my $description = ""; 29110d7835fcSJoe Perches 2912fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2913fe043ea1SJoe Perches $init_char = $1; 2914fe043ea1SJoe Perches $orig_commit = lc($2); 2915fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2916fe043ea1SJoe Perches $orig_commit = lc($1); 2917fe043ea1SJoe Perches } 2918fe043ea1SJoe Perches 29190d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 29200d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 29210d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 29220d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 29230d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 29240d7835fcSJoe Perches $orig_desc = $1; 292519c146a6SJoe Perches $hasparens = 1; 29260d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 29270d7835fcSJoe Perches defined $rawlines[$linenr] && 29280d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 29290d7835fcSJoe Perches $orig_desc = $1; 293019c146a6SJoe Perches $hasparens = 1; 2931b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2932b671fde0SJoe Perches defined $rawlines[$linenr] && 2933b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2934b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2935b671fde0SJoe Perches $orig_desc = $1; 2936b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2937b671fde0SJoe Perches $orig_desc .= " " . $1; 293819c146a6SJoe Perches $hasparens = 1; 29390d7835fcSJoe Perches } 29400d7835fcSJoe Perches 29410d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 29420d7835fcSJoe Perches $id, $orig_desc); 29430d7835fcSJoe Perches 2944948b133aSHeinrich Schuchardt if (defined($id) && 2945948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2946d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 29470d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 29480d7835fcSJoe Perches } 2949d311cd44SJoe Perches } 2950d311cd44SJoe Perches 295113f1937eSJoe Perches# Check for added, moved or deleted files 295213f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 295313f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 295413f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 295513f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 295613f1937eSJoe Perches (defined($1) || defined($2))))) { 2957a82603a8SAndrew Jeffery $is_patch = 1; 295813f1937eSJoe Perches $reported_maintainer_file = 1; 295913f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 296013f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 296113f1937eSJoe Perches } 296213f1937eSJoe Perches 2963e400edb1SRob Herring# Check for adding new DT bindings not in schema format 2964e400edb1SRob Herring if (!$in_commit_log && 2965e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 2966e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 2967e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 2968e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 2969e400edb1SRob Herring } 2970e400edb1SRob Herring 297100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 29728905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2973000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2974000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 29756c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2976de7d4f0eSAndy Whitcroft } 2977de7d4f0eSAndy Whitcroft 2978de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2979de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2980171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2981171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2982171ae1a4SAndy Whitcroft 2983171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2984171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2985171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2986171ae1a4SAndy Whitcroft 298734d99219SJoe Perches CHK("INVALID_UTF8", 2988000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 298900df344fSAndy Whitcroft } 29900a920b5bSAndy Whitcroft 299115662b3eSJoe Perches# Check if it's the start of a commit log 299215662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 299315662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2994eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2995eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 299615662b3eSJoe Perches $in_header_lines = 0; 299715662b3eSJoe Perches $in_commit_log = 1; 2998ed43c4e5SAllen Hubbe $has_commit_log = 1; 299915662b3eSJoe Perches } 300015662b3eSJoe Perches 3001fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 3002fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 3003fa64205dSPasi Savanainen if ($in_header_lines && 3004fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 3005fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 3006fa64205dSPasi Savanainen $non_utf8_charset = 1; 3007fa64205dSPasi Savanainen } 3008fa64205dSPasi Savanainen 3009fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 301015662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 3011fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 301215662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 301315662b3eSJoe Perches } 301415662b3eSJoe Perches 3015d6430f71SJoe Perches# Check for absolute kernel paths in commit message 3016d6430f71SJoe Perches if ($tree && $in_commit_log) { 3017d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 3018d6430f71SJoe Perches my $file = $1; 3019d6430f71SJoe Perches 3020d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 3021d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 3022d6430f71SJoe Perches # 3023d6430f71SJoe Perches } else { 3024d6430f71SJoe Perches check_absolute_file($file, $herecurr); 3025d6430f71SJoe Perches } 3026d6430f71SJoe Perches } 3027d6430f71SJoe Perches } 3028d6430f71SJoe Perches 302966b47b4aSKees Cook# Check for various typo / spelling mistakes 303066d7a382SJoe Perches if (defined($misspellings) && 303166d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 3032ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 303366b47b4aSKees Cook my $typo = $1; 303466b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 303566b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 303666b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 30370675a8fbSJean Delvare my $msg_level = \&WARN; 30380675a8fbSJean Delvare $msg_level = \&CHK if ($file); 30390675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 304066b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 304166b47b4aSKees Cook $fix) { 304266b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 304366b47b4aSKees Cook } 304466b47b4aSKees Cook } 304566b47b4aSKees Cook } 304666b47b4aSKees Cook 3047a8dd86bfSMatteo Croce# check for invalid commit id 3048a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 3049a8dd86bfSMatteo Croce my $id; 3050a8dd86bfSMatteo Croce my $description; 3051a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 3052a8dd86bfSMatteo Croce if (!defined($id)) { 3053a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 3054a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 3055a8dd86bfSMatteo Croce } 3056a8dd86bfSMatteo Croce } 3057a8dd86bfSMatteo Croce 3058310cd06bSJoe Perches# check for repeated words separated by a single space 30598d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root' 30608d0325ccSAditya Srivastava if (($rawline =~ /^\+/ || $in_commit_log) && 30618d0325ccSAditya Srivastava $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) { 30621db81a68SDwaipayan Ray pos($rawline) = 1 if (!$in_commit_log); 3063310cd06bSJoe Perches while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { 3064310cd06bSJoe Perches 3065310cd06bSJoe Perches my $first = $1; 3066310cd06bSJoe Perches my $second = $2; 30671db81a68SDwaipayan Ray my $start_pos = $-[1]; 30681db81a68SDwaipayan Ray my $end_pos = $+[2]; 3069310cd06bSJoe Perches if ($first =~ /(?:struct|union|enum)/) { 3070310cd06bSJoe Perches pos($rawline) += length($first) + length($second) + 1; 3071310cd06bSJoe Perches next; 3072310cd06bSJoe Perches } 3073310cd06bSJoe Perches 30741db81a68SDwaipayan Ray next if (lc($first) ne lc($second)); 3075310cd06bSJoe Perches next if ($first eq 'long'); 3076310cd06bSJoe Perches 30771db81a68SDwaipayan Ray # check for character before and after the word matches 30781db81a68SDwaipayan Ray my $start_char = ''; 30791db81a68SDwaipayan Ray my $end_char = ''; 30801db81a68SDwaipayan Ray $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1)); 30811db81a68SDwaipayan Ray $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline)); 30821db81a68SDwaipayan Ray 30831db81a68SDwaipayan Ray next if ($start_char =~ /^\S$/); 30841db81a68SDwaipayan Ray next if (index(" \t.,;?!", $end_char) == -1); 30851db81a68SDwaipayan Ray 30868d0325ccSAditya Srivastava # avoid repeating hex occurrences like 'ff ff fe 09 ...' 30878d0325ccSAditya Srivastava if ($first =~ /\b[0-9a-f]{2,}\b/i) { 30888d0325ccSAditya Srivastava next if (!exists($allow_repeated_words{lc($first)})); 30898d0325ccSAditya Srivastava } 30908d0325ccSAditya Srivastava 3091310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3092310cd06bSJoe Perches "Possible repeated word: '$first'\n" . $herecurr) && 3093310cd06bSJoe Perches $fix) { 3094310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; 3095310cd06bSJoe Perches } 3096310cd06bSJoe Perches } 3097310cd06bSJoe Perches 3098310cd06bSJoe Perches # if it's a repeated word on consecutive lines in a comment block 3099310cd06bSJoe Perches if ($prevline =~ /$;+\s*$/ && 3100310cd06bSJoe Perches $prevrawline =~ /($word_pattern)\s*$/) { 3101310cd06bSJoe Perches my $last_word = $1; 3102310cd06bSJoe Perches if ($rawline =~ /^\+\s*\*\s*$last_word /) { 3103310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3104310cd06bSJoe Perches "Possible repeated word: '$last_word'\n" . $hereprev) && 3105310cd06bSJoe Perches $fix) { 3106310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; 3107310cd06bSJoe Perches } 3108310cd06bSJoe Perches } 3109310cd06bSJoe Perches } 3110310cd06bSJoe Perches } 3111310cd06bSJoe Perches 311230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 311330670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 311400df344fSAndy Whitcroft 31150a920b5bSAndy Whitcroft#trailing whitespace 31169c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 3117c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3118d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 3119d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 3120d5e616fcSJoe Perches $fix) { 3121194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 3122d5e616fcSJoe Perches } 3123c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 3124c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 31253705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 31263705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 31273705ce5bSJoe Perches $fix) { 3128194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 31293705ce5bSJoe Perches } 31303705ce5bSJoe Perches 3131d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 31320a920b5bSAndy Whitcroft } 31335368df20SAndy Whitcroft 31344783f894SJosh Triplett# Check for FSF mailing addresses. 3135109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 31361bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 31373e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 31383e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 31394783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 31400675a8fbSJean Delvare my $msg_level = \&ERROR; 31410675a8fbSJean Delvare $msg_level = \&CHK if ($file); 31420675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 31434783f894SJosh 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) 31444783f894SJosh Triplett } 31454783f894SJosh Triplett 31463354957aSAndi Kleen# check for Kconfig help text having a real description 31479fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 31489fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 31493354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 3150678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3151678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3152678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3153678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 31543354957aSAndi Kleen my $length = 0; 31559fe287d7SAndy Whitcroft my $cnt = $realcnt; 31569fe287d7SAndy Whitcroft my $ln = $linenr + 1; 31579fe287d7SAndy Whitcroft my $f; 3158a1385803SAndy Whitcroft my $is_start = 0; 31599fe287d7SAndy Whitcroft my $is_end = 0; 3160a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 31619fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 31629fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 31639fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 31649fe287d7SAndy Whitcroft 31659fe287d7SAndy Whitcroft next if ($f =~ /^-/); 31668d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3167a1385803SAndy Whitcroft 316886adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3169a1385803SAndy Whitcroft $is_start = 1; 317022a4ac02SMasahiro Yamada } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 3171a1385803SAndy Whitcroft $length = -1; 3172a1385803SAndy Whitcroft } 3173a1385803SAndy Whitcroft 31749fe287d7SAndy Whitcroft $f =~ s/^.//; 31753354957aSAndi Kleen $f =~ s/#.*//; 31763354957aSAndi Kleen $f =~ s/^\s+//; 31773354957aSAndi Kleen next if ($f =~ /^$/); 3178678ae162SUlf Magnusson 3179678ae162SUlf Magnusson # This only checks context lines in the patch 3180678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3181678ae162SUlf Magnusson # positives, even though some of these are 3182678ae162SUlf Magnusson # common words in help texts 3183678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3184678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 31859fe287d7SAndy Whitcroft $is_end = 1; 31869fe287d7SAndy Whitcroft last; 31879fe287d7SAndy Whitcroft } 31883354957aSAndi Kleen $length++; 31893354957aSAndi Kleen } 319056193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3191000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 319256193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 319356193274SVadim Bendebury } 3194a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 31953354957aSAndi Kleen } 31963354957aSAndi Kleen 31977ccf41a8SJoe Perches# check MAINTAINERS entries 31987ccf41a8SJoe Perches if ($realfile =~ /^MAINTAINERS$/) { 31997ccf41a8SJoe Perches# check MAINTAINERS entries for the right form 32007ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 3201628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3202628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3203628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3204628f91a2SJoe Perches $fix) { 3205628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3206628f91a2SJoe Perches } 3207628f91a2SJoe Perches } 32087ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too 32097ccf41a8SJoe Perches my $preferred_order = 'MRLSWQBCPTFXNK'; 32107ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 32117ccf41a8SJoe Perches $prevrawline =~ /^[\+ ][A-Z]:/) { 32127ccf41a8SJoe Perches $rawline =~ /^\+([A-Z]):\s*(.*)/; 32137ccf41a8SJoe Perches my $cur = $1; 32147ccf41a8SJoe Perches my $curval = $2; 32157ccf41a8SJoe Perches $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; 32167ccf41a8SJoe Perches my $prev = $1; 32177ccf41a8SJoe Perches my $prevval = $2; 32187ccf41a8SJoe Perches my $curindex = index($preferred_order, $cur); 32197ccf41a8SJoe Perches my $previndex = index($preferred_order, $prev); 32207ccf41a8SJoe Perches if ($curindex < 0) { 32217ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 32227ccf41a8SJoe Perches "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); 32237ccf41a8SJoe Perches } else { 32247ccf41a8SJoe Perches if ($previndex >= 0 && $curindex < $previndex) { 32257ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 32267ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); 32277ccf41a8SJoe Perches } elsif ((($prev eq 'F' && $cur eq 'F') || 32287ccf41a8SJoe Perches ($prev eq 'X' && $cur eq 'X')) && 32297ccf41a8SJoe Perches ($prevval cmp $curval) > 0) { 32307ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 32317ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); 32327ccf41a8SJoe Perches } 32337ccf41a8SJoe Perches } 32347ccf41a8SJoe Perches } 32357ccf41a8SJoe Perches } 3236628f91a2SJoe Perches 3237327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3238327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3239327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3240327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3241327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3242327953e9SChristoph Jaeger } 3243327953e9SChristoph Jaeger 3244c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3245c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3246c68e5878SArnaud Lacombe my $flag = $1; 3247c68e5878SArnaud Lacombe my $replacement = { 3248c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3249c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3250c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3251c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3252c68e5878SArnaud Lacombe }; 3253c68e5878SArnaud Lacombe 3254c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3255c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3256c68e5878SArnaud Lacombe } 3257c68e5878SArnaud Lacombe 3258bff5da43SRob Herring# check for DT compatible documentation 32597dd05b38SFlorian Vaussard if (defined $root && 32607dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 32617dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 32627dd05b38SFlorian Vaussard 3263bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3264bff5da43SRob Herring 3265cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3266852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3267cc93319bSFlorian Vaussard 3268bff5da43SRob Herring foreach my $compat (@compats) { 3269bff5da43SRob Herring my $compat2 = $compat; 3270185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3271185d566bSRob Herring my $compat3 = $compat; 3272185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3273185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3274bff5da43SRob Herring if ( $? >> 8 ) { 3275bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3276bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3277bff5da43SRob Herring } 3278bff5da43SRob Herring 32794fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 32804fbf32a6SFlorian Vaussard my $vendor = $1; 3281852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3282bff5da43SRob Herring if ( $? >> 8 ) { 3283bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3284cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3285bff5da43SRob Herring } 3286bff5da43SRob Herring } 3287bff5da43SRob Herring } 3288bff5da43SRob Herring 32899f3a8992SRob Herring# check for using SPDX license tag at beginning of files 32909f3a8992SRob Herring if ($realline == $checklicenseline) { 32919f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 32929f3a8992SRob Herring $checklicenseline = 2; 32939f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 32949f3a8992SRob Herring my $comment = ""; 32959f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 32969f3a8992SRob Herring $comment = '/*'; 32979f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 32989f3a8992SRob Herring $comment = '//'; 3299c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 33009f3a8992SRob Herring $comment = '#'; 33019f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 33029f3a8992SRob Herring $comment = '..'; 33039f3a8992SRob Herring } 33049f3a8992SRob Herring 3305fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3306fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3307fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3308ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3309fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3310fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3311fdf13693SJoe Perches } 3312fdf13693SJoe Perches 33139f3a8992SRob Herring if ($comment !~ /^$/ && 3314ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 33159f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 33169f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 33173b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 33183b6e8ac9SJoe Perches my $spdx_license = $1; 33193b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 33203b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 33213b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 33223b6e8ac9SJoe Perches } 332350c92900SLubomir Rintel if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 332450c92900SLubomir Rintel not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 332550c92900SLubomir Rintel my $msg_level = \&WARN; 332650c92900SLubomir Rintel $msg_level = \&CHK if ($file); 332750c92900SLubomir Rintel if (&{$msg_level}("SPDX_LICENSE_TAG", 332850c92900SLubomir Rintel 332950c92900SLubomir Rintel "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 333050c92900SLubomir Rintel $fix) { 333150c92900SLubomir Rintel $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 333250c92900SLubomir Rintel } 333350c92900SLubomir Rintel } 33349f3a8992SRob Herring } 33359f3a8992SRob Herring } 33369f3a8992SRob Herring } 33379f3a8992SRob Herring 3338a0154cdbSJoe Perches# check for embedded filenames 3339a0154cdbSJoe Perches if ($rawline =~ /^\+.*\Q$realfile\E/) { 3340a0154cdbSJoe Perches WARN("EMBEDDED_FILENAME", 3341a0154cdbSJoe Perches "It's generally not useful to have the filename in the file\n" . $herecurr); 3342a0154cdbSJoe Perches } 3343a0154cdbSJoe Perches 33445368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3345d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 33465368df20SAndy Whitcroft 3347a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3348a8da38a9SJoe Perches if ($realline != $checklicenseline && 3349a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3350a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3351a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3352a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3353a8da38a9SJoe Perches } 3354a8da38a9SJoe Perches 335547e0c88bSJoe Perches# line length limit (with some exclusions) 335647e0c88bSJoe Perches# 335747e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 335847e0c88bSJoe Perches# logging functions like pr_info that end in a string 335947e0c88bSJoe Perches# lines with a single string 336047e0c88bSJoe Perches# #defines that are a single string 33612e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 336247e0c88bSJoe Perches# 336347e0c88bSJoe Perches# There are 3 different line length message types: 3364ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 336547e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 336647e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 336747e0c88bSJoe Perches# 336847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 336947e0c88bSJoe Perches# 337047e0c88bSJoe Perches 3371b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 337247e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 337347e0c88bSJoe Perches 337447e0c88bSJoe Perches # Check the allowed long line types first 337547e0c88bSJoe Perches 337647e0c88bSJoe Perches # logging functions that end in a string that starts 337747e0c88bSJoe Perches # before $max_line_length 337847e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 337947e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 338047e0c88bSJoe Perches $msg_type = ""; 338147e0c88bSJoe Perches 338247e0c88bSJoe Perches # lines with only strings (w/ possible termination) 338347e0c88bSJoe Perches # #defines with only strings 338447e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 338547e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 338647e0c88bSJoe Perches $msg_type = ""; 338747e0c88bSJoe Perches 3388cc147506SJoe Perches # More special cases 3389cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3390cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3391d560a5f8SJoe Perches $msg_type = ""; 3392d560a5f8SJoe Perches 33932e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 33942e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 33952e4bbbc5SAndreas Brauchli $msg_type = ""; 33962e4bbbc5SAndreas Brauchli 339747e0c88bSJoe Perches # Otherwise set the alternate message types 339847e0c88bSJoe Perches 339947e0c88bSJoe Perches # a comment starts before $max_line_length 340047e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 340147e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 340247e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 340347e0c88bSJoe Perches 340447e0c88bSJoe Perches # a quoted string starts before $max_line_length 340547e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 340647e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 340747e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 340847e0c88bSJoe Perches } 340947e0c88bSJoe Perches 341047e0c88bSJoe Perches if ($msg_type ne "" && 341147e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 3412bdc48fa1SJoe Perches my $msg_level = \&WARN; 3413bdc48fa1SJoe Perches $msg_level = \&CHK if ($file); 3414bdc48fa1SJoe Perches &{$msg_level}($msg_type, 3415bdc48fa1SJoe Perches "line length of $length exceeds $max_line_length columns\n" . $herecurr); 34160a920b5bSAndy Whitcroft } 341747e0c88bSJoe Perches } 34180a920b5bSAndy Whitcroft 34198905a67cSAndy Whitcroft# check for adding lines without a newline. 34208905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3421000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3422000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 34238905a67cSAndy Whitcroft } 34248905a67cSAndy Whitcroft 3425b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3426de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 34270a920b5bSAndy Whitcroft 34280a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3429713a09deSAntonio Borneo# more than $tabsize must use tabs. 3430c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3431c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3432c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3433d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 34343705ce5bSJoe Perches if (ERROR("CODE_INDENT", 34353705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 34363705ce5bSJoe Perches $fix) { 3437194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 34383705ce5bSJoe Perches } 34390a920b5bSAndy Whitcroft } 34400a920b5bSAndy Whitcroft 344108e44365SAlberto Panizzo# check for space before tabs. 344208e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 344308e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 34443705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 34453705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 34463705ce5bSJoe Perches $fix) { 3447194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3448713a09deSAntonio Borneo s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3449194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3450c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 34513705ce5bSJoe Perches } 345208e44365SAlberto Panizzo } 345308e44365SAlberto Panizzo 34546a487211SJoe Perches# check for assignments on the start of a line 34556a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 34566a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 34576a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 34586a487211SJoe Perches } 34596a487211SJoe Perches 3460d1fe9c09SJoe Perches# check for && or || at the start of a line 3461d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3462d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3463d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3464d1fe9c09SJoe Perches } 3465d1fe9c09SJoe Perches 3466a91e8994SJoe Perches# check indentation starts on a tab stop 34675b57980dSJoe Perches if ($perl_version_ok && 3468bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3469a91e8994SJoe Perches my $indent = length($1); 3470713a09deSAntonio Borneo if ($indent % $tabsize) { 3471a91e8994SJoe Perches if (WARN("TABSTOP", 3472a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3473a91e8994SJoe Perches $fix) { 3474713a09deSAntonio Borneo $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3475a91e8994SJoe Perches } 3476a91e8994SJoe Perches } 3477a91e8994SJoe Perches } 3478a91e8994SJoe Perches 3479d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 34805b57980dSJoe Perches if ($perl_version_ok && 3481fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3482d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3483d1fe9c09SJoe Perches my $oldindent = $1; 3484d1fe9c09SJoe Perches my $rest = $2; 3485d1fe9c09SJoe Perches 3486d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3487d1fe9c09SJoe Perches if ($pos >= 0) { 3488b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3489b34a26f3SJoe Perches my $newindent = $2; 3490d1fe9c09SJoe Perches 3491d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3492713a09deSAntonio Borneo "\t" x ($pos / $tabsize) . 3493713a09deSAntonio Borneo " " x ($pos % $tabsize); 3494d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3495d1fe9c09SJoe Perches 3496d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3497d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 34983705ce5bSJoe Perches 34993705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 35003705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 35013705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3502194f66fcSJoe Perches $fixed[$fixlinenr] =~ 35033705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 35043705ce5bSJoe Perches } 3505d1fe9c09SJoe Perches } 3506d1fe9c09SJoe Perches } 3507d1fe9c09SJoe Perches } 3508d1fe9c09SJoe Perches 35096ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 35106ab3a970SJoe Perches# avoid checking a few false positives: 35116ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 35126ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 35136ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 35146ab3a970SJoe Perches# multiline macros that define functions 35156ab3a970SJoe Perches# known attributes or the __attribute__ keyword 35166ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 35176ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 35183705ce5bSJoe Perches if (CHK("SPACING", 3519f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 35203705ce5bSJoe Perches $fix) { 3521194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3522f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 35233705ce5bSJoe Perches } 3524aad4f614SJoe Perches } 3525aad4f614SJoe Perches 352686406b1cSJoe Perches# Block comment styles 352786406b1cSJoe Perches# Networking with an initial /* 352805880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3529fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 353085ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 3531c70735c2SŁukasz Stelmach $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier 353205880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 353305880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 353405880600SJoe Perches } 353505880600SJoe Perches 353686406b1cSJoe Perches# Block comments use * on subsequent lines 353786406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 353886406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3539a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 354061135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3541a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 354286406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 354386406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3544a605e32eSJoe Perches } 3545a605e32eSJoe Perches 354686406b1cSJoe Perches# Block comments use */ on trailing lines 354786406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3548c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3549c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3550c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 355186406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 355286406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 355305880600SJoe Perches } 355405880600SJoe Perches 355508eb9b80SJoe Perches# Block comment * alignment 355608eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3557af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3558af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3559af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 356008eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3561af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3562af207524SJoe Perches my $oldindent; 356308eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3564af207524SJoe Perches if (defined($1)) { 3565af207524SJoe Perches $oldindent = expand_tabs($1); 3566af207524SJoe Perches } else { 3567af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3568af207524SJoe Perches $oldindent = expand_tabs($1); 3569af207524SJoe Perches } 357008eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 357108eb9b80SJoe Perches my $newindent = $1; 357208eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3573af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 357408eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 357508eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 357608eb9b80SJoe Perches } 357708eb9b80SJoe Perches } 357808eb9b80SJoe Perches 35797f619191SJoe Perches# check for missing blank lines after struct/union declarations 35807f619191SJoe Perches# with exceptions for various attributes and macros 35817f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 35827f619191SJoe Perches $line =~ /^\+/ && 35837f619191SJoe Perches !($line =~ /^\+\s*$/ || 35847f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 35857f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 35867f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 35877f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 35887f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 35897f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 35900bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 35917f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3592d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3593d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3594d752fcc8SJoe Perches $fix) { 3595f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3596d752fcc8SJoe Perches } 35977f619191SJoe Perches } 35987f619191SJoe Perches 3599365dd4eaSJoe Perches# check for multiple consecutive blank lines 3600365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3601365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3602365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3603d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3604d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3605d752fcc8SJoe Perches $fix) { 3606f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3607d752fcc8SJoe Perches } 3608d752fcc8SJoe Perches 3609365dd4eaSJoe Perches $last_blank_line = $linenr; 3610365dd4eaSJoe Perches } 3611365dd4eaSJoe Perches 36123b617e3bSJoe Perches# check for missing blank lines after declarations 36133f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 36143f7bac03SJoe Perches # actual declarations 36153f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 36165a4e1fd3SJoe Perches # function pointer declarations 36175a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 36183f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 36193f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 36203f7bac03SJoe Perches # known declaration macros 36213f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 36223f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 36233f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 36243f7bac03SJoe Perches # other possible extensions of declaration lines 36253f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 36263f7bac03SJoe Perches # not starting a section or a macro "\" extended line 36273f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 36283f7bac03SJoe Perches # looks like a declaration 36293f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 36305a4e1fd3SJoe Perches # function pointer declarations 36315a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 36323f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 36333f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 36343f7bac03SJoe Perches # known declaration macros 36353f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 36363f7bac03SJoe Perches # start of struct or union or enum 3637328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 36383f7bac03SJoe Perches # start or end of block or continuation of declaration 36393f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 36403f7bac03SJoe Perches # bitfield continuation 36413f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 36423f7bac03SJoe Perches # other possible extensions of declaration lines 36433f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 36443f7bac03SJoe Perches # indentation of previous and current line are the same 36453f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3646d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3647d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3648d752fcc8SJoe Perches $fix) { 3649f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3650d752fcc8SJoe Perches } 36513b617e3bSJoe Perches } 36523b617e3bSJoe Perches 36535f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 36546b4c5bebSAndy Whitcroft# Exceptions: 36556b4c5bebSAndy Whitcroft# 1) within comments 36566b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 36576b4c5bebSAndy Whitcroft# 3) hanging labels 36583705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 36595f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 36603705ce5bSJoe Perches if (WARN("LEADING_SPACE", 36613705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 36623705ce5bSJoe Perches $fix) { 3663194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 36643705ce5bSJoe Perches } 36655f7ddae6SRaffaele Recalcati } 36665f7ddae6SRaffaele Recalcati 3667b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3668b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3669b9ea10d6SAndy Whitcroft 36705751a24eSJoe Perches# check for unusual line ending [ or ( 36715751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 36725751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 36735751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 36745751a24eSJoe Perches } 36755751a24eSJoe Perches 36764dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 36774dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 36784dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 36794dbed76fSJoe Perches $context_function = $1; 36804dbed76fSJoe Perches } 36814dbed76fSJoe Perches 36824dbed76fSJoe Perches# check if this appears to be the end of function declaration 36834dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 36844dbed76fSJoe Perches undef $context_function; 36854dbed76fSJoe Perches } 36864dbed76fSJoe Perches 3687032a4c0fSJoe Perches# check indentation of any line with a bare else 3688840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3689032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3690032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3691032a4c0fSJoe Perches my $tabs = length($1) + 1; 3692840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3693840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3694840080a0SJoe Perches defined $lines[$linenr] && 3695840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3696032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3697032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3698032a4c0fSJoe Perches } 3699032a4c0fSJoe Perches } 3700032a4c0fSJoe Perches 3701c00df19aSJoe Perches# check indentation of a line with a break; 3702*dc58bc55SJoe Perches# if the previous line is a goto, return or break 3703*dc58bc55SJoe Perches# and is indented the same # of tabs 3704c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3705c00df19aSJoe Perches my $tabs = $1; 3706*dc58bc55SJoe Perches if ($prevline =~ /^\+$tabs(goto|return|break)\b/) { 3707*dc58bc55SJoe Perches if (WARN("UNNECESSARY_BREAK", 3708*dc58bc55SJoe Perches "break is not useful after a $1\n" . $hereprev) && 3709*dc58bc55SJoe Perches $fix) { 3710*dc58bc55SJoe Perches fix_delete_line($fixlinenr, $rawline); 3711*dc58bc55SJoe Perches } 3712c00df19aSJoe Perches } 3713c00df19aSJoe Perches } 3714c00df19aSJoe Perches 3715c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3716cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3717000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3718000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3719c2fdda0dSAndy Whitcroft } 372022f2a2efSAndy Whitcroft 372156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 372256e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 372356e77d70SJoe Perches WARN("HOTPLUG_SECTION", 372456e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 372556e77d70SJoe Perches } 372656e77d70SJoe Perches 37279c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 37282b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 37292b474a1aSAndy Whitcroft $realline_next); 37303e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3731ca819864SJoe Perches if ($linenr > $suppress_statement && 37321b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3733170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3734f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3735171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3736171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3737171ae1a4SAndy Whitcroft 37383e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 37393e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 37403e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 37413e469cdcSAndy Whitcroft # until we hit end of it. 37423e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 37433e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 37443e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 37453e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 37463e469cdcSAndy Whitcroft } 3747f74bd194SAndy Whitcroft 37482b474a1aSAndy Whitcroft # Find the real next line. 37492b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 37502b474a1aSAndy Whitcroft if (defined $realline_next && 37512b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 37522b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 37532b474a1aSAndy Whitcroft $realline_next++; 37542b474a1aSAndy Whitcroft } 37552b474a1aSAndy Whitcroft 3756171ae1a4SAndy Whitcroft my $s = $stat; 3757171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3758cf655043SAndy Whitcroft 3759c2fdda0dSAndy Whitcroft # Ignore goto labels. 3760171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3761c2fdda0dSAndy Whitcroft 3762c2fdda0dSAndy Whitcroft # Ignore functions being called 3763171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3764c2fdda0dSAndy Whitcroft 3765463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3766463f2864SAndy Whitcroft 3767c45dcabdSAndy Whitcroft # declarations always start with types 3768d2506586SAndy 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) { 3769c45dcabdSAndy Whitcroft my $type = $1; 3770c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3771c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3772c45dcabdSAndy Whitcroft 37736c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3774a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3775c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3776c2fdda0dSAndy Whitcroft } 37778905a67cSAndy Whitcroft 37786c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 377965863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3780c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 37819c0ca6f9SAndy Whitcroft } 37828905a67cSAndy Whitcroft 37838905a67cSAndy Whitcroft # Check for any sort of function declaration. 37848905a67cSAndy Whitcroft # int foo(something bar, other baz); 37858905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3786171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 37878905a67cSAndy Whitcroft my ($name_len) = length($1); 37888905a67cSAndy Whitcroft 3789cf655043SAndy Whitcroft my $ctx = $s; 3790773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 37918905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3792cf655043SAndy Whitcroft 37938905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3794c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 37958905a67cSAndy Whitcroft 3796c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 37978905a67cSAndy Whitcroft } 37988905a67cSAndy Whitcroft } 37998905a67cSAndy Whitcroft } 38008905a67cSAndy Whitcroft 38019c0ca6f9SAndy Whitcroft } 38029c0ca6f9SAndy Whitcroft 380300df344fSAndy Whitcroft# 380400df344fSAndy Whitcroft# Checks which may be anchored in the context. 380500df344fSAndy Whitcroft# 380600df344fSAndy Whitcroft 380700df344fSAndy Whitcroft# Check for switch () and associated case and default 380800df344fSAndy Whitcroft# statements should be at the same indent. 380900df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 381000df344fSAndy Whitcroft my $err = ''; 381100df344fSAndy Whitcroft my $sep = ''; 381200df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 381300df344fSAndy Whitcroft shift(@ctx); 381400df344fSAndy Whitcroft for my $ctx (@ctx) { 381500df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 381600df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 381700df344fSAndy Whitcroft $indent != $cindent) { 381800df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 381900df344fSAndy Whitcroft $sep = ''; 382000df344fSAndy Whitcroft } else { 382100df344fSAndy Whitcroft $sep = "[...]\n"; 382200df344fSAndy Whitcroft } 382300df344fSAndy Whitcroft } 382400df344fSAndy Whitcroft if ($err ne '') { 3825000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3826000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3827de7d4f0eSAndy Whitcroft } 3828de7d4f0eSAndy Whitcroft } 3829de7d4f0eSAndy Whitcroft 3830de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3831de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 38320fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3833773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3834773647a0SAndy Whitcroft 38359c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 38368eef05ddSJoe Perches 38378eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 38388eef05ddSJoe Perches WARN("DEEP_INDENTATION", 38398eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 38408eef05ddSJoe Perches } 38418eef05ddSJoe Perches 3842de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3843de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3844de7d4f0eSAndy Whitcroft 3845548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3846548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3847de7d4f0eSAndy Whitcroft 3848548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3849548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3850548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3851548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3852548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3853773647a0SAndy Whitcroft $ctx_ln++; 3854773647a0SAndy Whitcroft } 3855548596d5SAndy Whitcroft 385653210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 385753210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3858773647a0SAndy Whitcroft 3859773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3860000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3861000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 386201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 386300df344fSAndy Whitcroft } 3864773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3865773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3866773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3867773647a0SAndy Whitcroft { 38689c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 38699c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3870000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3871000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 387201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 38739c0ca6f9SAndy Whitcroft } 38749c0ca6f9SAndy Whitcroft } 387500df344fSAndy Whitcroft } 387600df344fSAndy Whitcroft 38774d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3878f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 38793e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 38803e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 38813e469cdcSAndy Whitcroft if (!defined $stat); 38824d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 38834d001e4dSAndy Whitcroft 38844d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 38854d001e4dSAndy Whitcroft 38869f5af480SJoe Perches # remove inline comments 38879f5af480SJoe Perches $s =~ s/$;/ /g; 38889f5af480SJoe Perches $c =~ s/$;/ /g; 38894d001e4dSAndy Whitcroft 38904d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 38916f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 38926f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 38934d001e4dSAndy Whitcroft 38949f5af480SJoe Perches # Make sure we remove the line prefixes as we have 38959f5af480SJoe Perches # none on the first line, and are going to readd them 38969f5af480SJoe Perches # where necessary. 38979f5af480SJoe Perches $s =~ s/\n./\n/gs; 38989f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 38999f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 39009f5af480SJoe Perches } 39019f5af480SJoe Perches 39024d001e4dSAndy Whitcroft # We want to check the first line inside the block 39034d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 39044d001e4dSAndy Whitcroft # 1) any blank line termination 39054d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 39064d001e4dSAndy Whitcroft # 3) any do (...) { 39074d001e4dSAndy Whitcroft my $continuation = 0; 39084d001e4dSAndy Whitcroft my $check = 0; 39094d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 39104d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 39114d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 39124d001e4dSAndy Whitcroft $continuation = 1; 39134d001e4dSAndy Whitcroft } 39149bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 39154d001e4dSAndy Whitcroft $check = 1; 39164d001e4dSAndy Whitcroft $cond_lines++; 39174d001e4dSAndy Whitcroft } 39184d001e4dSAndy Whitcroft 39194d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 39204d001e4dSAndy Whitcroft # preprocessor statement. 39214d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 39224d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 39234d001e4dSAndy Whitcroft $check = 0; 39244d001e4dSAndy Whitcroft } 39254d001e4dSAndy Whitcroft 39269bd49efeSAndy Whitcroft my $cond_ptr = -1; 3927740504c6SAndy Whitcroft $continuation = 0; 39289bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 39299bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 39304d001e4dSAndy Whitcroft 3931f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3932f16fa28fSAndy Whitcroft # is not linear. 3933f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3934f16fa28fSAndy Whitcroft $check = 0; 3935f16fa28fSAndy Whitcroft } 3936f16fa28fSAndy Whitcroft 39379bd49efeSAndy Whitcroft # Ignore: 39389bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 39399bd49efeSAndy Whitcroft # 2) preprocessor lines, and 39409bd49efeSAndy Whitcroft # 3) labels. 3941740504c6SAndy Whitcroft if ($continuation || 3942740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 39439bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 39449bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3945740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 394630dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 39479bd49efeSAndy Whitcroft $cond_lines++; 39489bd49efeSAndy Whitcroft } 39494d001e4dSAndy Whitcroft } 395030dad6ebSAndy Whitcroft } 39514d001e4dSAndy Whitcroft 39524d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 39534d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 39544d001e4dSAndy Whitcroft 39554d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 39564d001e4dSAndy Whitcroft # this is not this patch's fault. 39574d001e4dSAndy Whitcroft if (!defined($stat_real) || 39584d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 39594d001e4dSAndy Whitcroft $check = 0; 39604d001e4dSAndy Whitcroft } 39614d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 39624d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 39634d001e4dSAndy Whitcroft } 39644d001e4dSAndy Whitcroft 39659bd49efeSAndy 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"; 39664d001e4dSAndy Whitcroft 39679f5af480SJoe Perches if ($check && $s ne '' && 3968713a09deSAntonio Borneo (($sindent % $tabsize) != 0 || 39699f5af480SJoe Perches ($sindent < $indent) || 3970f6950a73SJoe Perches ($sindent == $indent && 3971f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 3972713a09deSAntonio Borneo ($sindent > $indent + $tabsize))) { 3973000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3974000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 39754d001e4dSAndy Whitcroft } 39764d001e4dSAndy Whitcroft } 39774d001e4dSAndy Whitcroft 39786c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 39796c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 39801f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 39811f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 39826c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3983c2fdda0dSAndy Whitcroft if ($dbg_values) { 3984c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3985cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3986cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 39871f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3988c2fdda0dSAndy Whitcroft } 39896c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 39906c72ffaaSAndy Whitcroft 399100df344fSAndy Whitcroft#ignore lines not being added 39923705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 399300df344fSAndy Whitcroft 399499ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings 399599ca38c2SJoe Perches# e.g.: int foo = foo, *bar = NULL; 399699ca38c2SJoe Perches# struct foo bar = *(&(bar)); 399799ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { 399899ca38c2SJoe Perches my $var = $1; 399999ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { 400099ca38c2SJoe Perches WARN("SELF_ASSIGNMENT", 400199ca38c2SJoe Perches "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); 400299ca38c2SJoe Perches } 400399ca38c2SJoe Perches } 400499ca38c2SJoe Perches 400511ca40a0SJoe Perches# check for dereferences that span multiple lines 400611ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 400711ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 400811ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 400911ca40a0SJoe Perches my $ref = $1; 401011ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 401111ca40a0SJoe Perches $ref .= $1; 401211ca40a0SJoe Perches $ref =~ s/\s//g; 401311ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 401411ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 401511ca40a0SJoe Perches } 401611ca40a0SJoe Perches 4017a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 4018c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 4019a1ce18e4SJoe Perches my $type = $1; 4020a1ce18e4SJoe Perches my $var = $2; 4021207a8e84SJoe Perches $var = "" if (!defined $var); 4022207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 4023a1ce18e4SJoe Perches my $sign = $1; 4024a1ce18e4SJoe Perches my $pointer = $2; 4025a1ce18e4SJoe Perches 4026a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 4027a1ce18e4SJoe Perches 4028a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 4029a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 4030a1ce18e4SJoe Perches $fix) { 4031a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 4032207a8e84SJoe Perches my $comp_pointer = $pointer; 4033207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 4034207a8e84SJoe Perches $decl .= $comp_pointer; 4035207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 4036207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 4037a1ce18e4SJoe Perches } 4038a1ce18e4SJoe Perches } 4039a1ce18e4SJoe Perches } 4040a1ce18e4SJoe Perches 4041653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 40427429c690SAndy Whitcroft if ($dbg_type) { 40437429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 4044000d1cc1SJoe Perches ERROR("TEST_TYPE", 4045000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 40467429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 4047000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 4048000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 40497429c690SAndy Whitcroft } 4050653d4876SAndy Whitcroft next; 4051653d4876SAndy Whitcroft } 4052a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 4053a1ef277eSAndy Whitcroft if ($dbg_attr) { 40549360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 4055000d1cc1SJoe Perches ERROR("TEST_ATTR", 4056000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 40579360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 4058000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 4059000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 4060a1ef277eSAndy Whitcroft } 4061a1ef277eSAndy Whitcroft next; 4062a1ef277eSAndy Whitcroft } 4063653d4876SAndy Whitcroft 4064f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 406599423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 406699423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 4067d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 4068d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 4069f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 4070f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4071f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 4072d752fcc8SJoe Perches my $fixedline = $prevrawline; 4073d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 4074f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4075d752fcc8SJoe Perches $fixedline = $line; 40768d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 4077f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4078d752fcc8SJoe Perches } 4079f0a594c1SAndy Whitcroft } 4080f0a594c1SAndy Whitcroft 408100df344fSAndy Whitcroft# 408200df344fSAndy Whitcroft# Checks which are anchored on the added line. 408300df344fSAndy Whitcroft# 408400df344fSAndy Whitcroft 4085653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 4086c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 4087653d4876SAndy Whitcroft my $path = $1; 4088653d4876SAndy Whitcroft if ($path =~ m{//}) { 4089000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 4090495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 4091495e9d84SJoe Perches } 4092495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 4093495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 4094495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 4095653d4876SAndy Whitcroft } 4096653d4876SAndy Whitcroft } 4097653d4876SAndy Whitcroft 409800df344fSAndy Whitcroft# no C99 // comments 409900df344fSAndy Whitcroft if ($line =~ m{//}) { 41003705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 41013705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 41023705ce5bSJoe Perches $fix) { 4103194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 41043705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 41053705ce5bSJoe Perches my $comment = trim($1); 4106194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 41073705ce5bSJoe Perches } 41083705ce5bSJoe Perches } 410900df344fSAndy Whitcroft } 411000df344fSAndy Whitcroft # Remove C99 comments. 41110a920b5bSAndy Whitcroft $line =~ s@//.*@@; 41126c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 41130a920b5bSAndy Whitcroft 41142b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 41152b474a1aSAndy Whitcroft# the whole statement. 41162b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 41172b474a1aSAndy Whitcroft if (defined $realline_next && 41182b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 41192b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 41202b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 41212b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 41223cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 41233cbf62dfSAndy Whitcroft # a prefix: 41243cbf62dfSAndy Whitcroft # XXX(foo); 41253cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 4126653d4876SAndy Whitcroft my $name = $1; 412787a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 41283cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 41293cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 41303cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 41313cbf62dfSAndy Whitcroft 41323cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 41332b474a1aSAndy Whitcroft \n.}\s*$| 413448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 413548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 413648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 41372b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 41382b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 413948012058SAndy Whitcroft )/x) { 41402b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 41412b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 41422b474a1aSAndy Whitcroft } else { 41432b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 41440a920b5bSAndy Whitcroft } 41450a920b5bSAndy Whitcroft } 41462b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 41472b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 41482b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 41492b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 41502b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 41512b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 41522b474a1aSAndy Whitcroft } 41532b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 41542b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 4155000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 4156000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 41572b474a1aSAndy Whitcroft } 41580a920b5bSAndy Whitcroft 41595150bda4SJoe Eloff# check for global initialisers. 41606d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 4161d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 41626d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 4163d5e616fcSJoe Perches $fix) { 41646d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 4165d5e616fcSJoe Perches } 4166f0a594c1SAndy Whitcroft } 41670a920b5bSAndy Whitcroft# check for static initialisers. 41686d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 4169d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 41706d32f7a3SJoe Perches "do not initialise statics to $1\n" . 4171d5e616fcSJoe Perches $herecurr) && 4172d5e616fcSJoe Perches $fix) { 41736d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 4174d5e616fcSJoe Perches } 41750a920b5bSAndy Whitcroft } 41760a920b5bSAndy Whitcroft 41771813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 41781813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 41791813087dSJoe Perches my $tmp = trim($1); 41801813087dSJoe Perches WARN("MISORDERED_TYPE", 41811813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 41821813087dSJoe Perches } 41831813087dSJoe Perches 4184809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 4185809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 4186809e082eSJoe Perches my $type = trim($1); 4187809e082eSJoe Perches next if ($type !~ /\bint\b/); 4188809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 4189809e082eSJoe Perches my $new_type = $type; 4190809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 4191809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 4192809e082eSJoe Perches $new_type =~ s/^const\s+//; 4193809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 4194809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 4195809e082eSJoe Perches $new_type =~ s/\s+/ /g; 4196809e082eSJoe Perches $new_type = trim($new_type); 4197809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 4198809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 4199809e082eSJoe Perches $fix) { 4200809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 4201809e082eSJoe Perches } 4202809e082eSJoe Perches } 4203809e082eSJoe Perches 4204cb710ecaSJoe Perches# check for static const char * arrays. 4205cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 4206000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4207000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 4208cb710ecaSJoe Perches $herecurr); 4209cb710ecaSJoe Perches } 4210cb710ecaSJoe Perches 421177b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 421277b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 421377b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 421477b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 421577b8c0a8SJoe Perches $fix) { 421677b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 421777b8c0a8SJoe Perches } 421877b8c0a8SJoe Perches } 421977b8c0a8SJoe Perches 4220cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 4221cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4222000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4223000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4224cb710ecaSJoe Perches $herecurr); 4225cb710ecaSJoe Perches } 4226cb710ecaSJoe Perches 4227ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4228ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4229ab7e23f3SJoe Perches my $found = $1; 4230ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4231ab7e23f3SJoe Perches WARN("CONST_CONST", 4232ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4233ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4234ab7e23f3SJoe Perches WARN("CONST_CONST", 4235ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4236ab7e23f3SJoe Perches } 4237ab7e23f3SJoe Perches } 4238ab7e23f3SJoe Perches 423973169765SJoe Perches# check for const static or static <non ptr type> const declarations 424073169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const' 424173169765SJoe Perches if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ || 424273169765SJoe Perches $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) { 424373169765SJoe Perches if (WARN("STATIC_CONST", 424473169765SJoe Perches "Move const after static - use 'static const $1'\n" . $herecurr) && 424573169765SJoe Perches $fix) { 424673169765SJoe Perches $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/; 424773169765SJoe Perches $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/; 424873169765SJoe Perches } 424973169765SJoe Perches } 425073169765SJoe Perches 42519b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 42529b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 42539b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 42549b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 42559b0fa60dSJoe Perches $herecurr); 42569b0fa60dSJoe Perches } 42579b0fa60dSJoe Perches 4258b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4259b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4260b598b670SJoe Perches my $array = $1; 4261b598b670SJoe 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*\))@) { 4262b598b670SJoe Perches my $array_div = $1; 4263b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4264b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4265b598b670SJoe Perches $fix) { 4266b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4267b598b670SJoe Perches } 4268b598b670SJoe Perches } 4269b598b670SJoe Perches } 4270b598b670SJoe Perches 4271b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 427216b7f3c8SJoe Perches if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { 4273b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4274b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4275b36190c5SJoe Perches $fix) { 4276194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4277b36190c5SJoe Perches } 4278b36190c5SJoe Perches } 4279b36190c5SJoe Perches 4280653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4281653d4876SAndy Whitcroft# make sense. 4282653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 42838054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4284c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 42858ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 428646d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4287000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4288000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 42890a920b5bSAndy Whitcroft } 42900a920b5bSAndy Whitcroft 42910a920b5bSAndy Whitcroft# * goes on variable not on type 429265863862SAndy Whitcroft # (char*[ const]) 4293bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4294bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 42953705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4296d8aaf121SAndy Whitcroft 429765863862SAndy Whitcroft # Should start with a space. 429865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 429965863862SAndy Whitcroft # Should not end with a space. 430065863862SAndy Whitcroft $to =~ s/\s+$//; 430165863862SAndy Whitcroft # '*'s should not have spaces between. 4302f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 430365863862SAndy Whitcroft } 4304d8aaf121SAndy Whitcroft 43053705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 430665863862SAndy Whitcroft if ($from ne $to) { 43073705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 43083705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 43093705ce5bSJoe Perches $fix) { 43103705ce5bSJoe Perches my $sub_from = $ident; 43113705ce5bSJoe Perches my $sub_to = $ident; 43123705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4313194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43143705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 43153705ce5bSJoe Perches } 431665863862SAndy Whitcroft } 4317bfcb2cc7SAndy Whitcroft } 4318bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4319bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 43203705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4321d8aaf121SAndy Whitcroft 432265863862SAndy Whitcroft # Should start with a space. 432365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 432465863862SAndy Whitcroft # Should not end with a space. 432565863862SAndy Whitcroft $to =~ s/\s+$//; 432665863862SAndy Whitcroft # '*'s should not have spaces between. 4327f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 432865863862SAndy Whitcroft } 432965863862SAndy Whitcroft # Modifiers should have spaces. 433065863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 433165863862SAndy Whitcroft 43323705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4333667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 43343705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 43353705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 43363705ce5bSJoe Perches $fix) { 43373705ce5bSJoe Perches 43383705ce5bSJoe Perches my $sub_from = $match; 43393705ce5bSJoe Perches my $sub_to = $match; 43403705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4341194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43423705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 43433705ce5bSJoe Perches } 434465863862SAndy Whitcroft } 43450a920b5bSAndy Whitcroft } 43460a920b5bSAndy Whitcroft 43479d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 43489d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 43490675a8fbSJean Delvare my $msg_level = \&WARN; 43500675a8fbSJean Delvare $msg_level = \&CHK if ($file); 43510675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 43529d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 43539d3e3c70SJoe Perches } 43540a920b5bSAndy Whitcroft 43559d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 43568905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4357000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4358000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 43598905a67cSAndy Whitcroft } 43608905a67cSAndy Whitcroft 436117441227SJoe Perches# check for uses of printk_ratelimit 436217441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4363000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4364000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 436517441227SJoe Perches } 436617441227SJoe Perches 4367eeef5733SJoe Perches# printk should use KERN_* levels 4368eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4369000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4370eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 437100df344fSAndy Whitcroft } 43720a920b5bSAndy Whitcroft 4373243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4374243f3803SJoe Perches my $orig = $1; 4375243f3803SJoe Perches my $level = lc($orig); 4376243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 43778f26b837SJoe Perches my $level2 = $level; 43788f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4379243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4380daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4381243f3803SJoe Perches } 4382243f3803SJoe Perches 4383dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4384dc139313SJoe Perches my $orig = $1; 4385dc139313SJoe Perches my $level = lc($orig); 4386dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4387dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4388dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4389dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4390dc139313SJoe Perches } 4391dc139313SJoe Perches 43928020b253SNicolas Boichat# trace_printk should not be used in production code. 43938020b253SNicolas Boichat if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { 43948020b253SNicolas Boichat WARN("TRACE_PRINTK", 43958020b253SNicolas Boichat "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); 43968020b253SNicolas Boichat } 43978020b253SNicolas Boichat 439891c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 439991c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 440091c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 440191c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 440291c9afafSAndy Lutomirski WARN("ENOSYS", 440391c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 440491c9afafSAndy Lutomirski } 440591c9afafSAndy Lutomirski 44066b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches. 44076b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 44086b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected. 44096b9ea5ffSJakub Kicinski if (!$file && $line =~ /\bENOTSUPP\b/) { 44106b9ea5ffSJakub Kicinski if (WARN("ENOTSUPP", 44116b9ea5ffSJakub Kicinski "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 44126b9ea5ffSJakub Kicinski $fix) { 44136b9ea5ffSJakub Kicinski $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 44146b9ea5ffSJakub Kicinski } 44156b9ea5ffSJakub Kicinski } 44166b9ea5ffSJakub Kicinski 4417653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4418653d4876SAndy Whitcroft# or if closed on same line 44195b57980dSJoe Perches if ($perl_version_ok && 44202d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 44212d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 44222d453e3bSJoe Perches $sline !~ /}/) { 44238d182478SJoe Perches if (ERROR("OPEN_BRACE", 44242d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 44258d182478SJoe Perches $fix) { 44268d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 44278d182478SJoe Perches my $fixed_line = $rawline; 44288d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 44298d182478SJoe Perches my $line1 = $1; 44308d182478SJoe Perches my $line2 = $2; 44318d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 44328d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 44338d182478SJoe Perches if ($line2 !~ /^\s*$/) { 44348d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 44358d182478SJoe Perches } 44368d182478SJoe Perches } 44370a920b5bSAndy Whitcroft } 4438653d4876SAndy Whitcroft 44398905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 44408905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 44418905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 44428d182478SJoe Perches if (ERROR("OPEN_BRACE", 44438d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 44448d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 44458d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 44468d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 44478d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 44488d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 44498d182478SJoe Perches $fixedline = $rawline; 44508d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 44518d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 44528d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 44538d182478SJoe Perches } 44548d182478SJoe Perches } 44558905a67cSAndy Whitcroft } 44568905a67cSAndy Whitcroft 44570c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 44583705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 44593705ce5bSJoe Perches if (WARN("SPACING", 44603705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 44613705ce5bSJoe Perches $fix) { 4462194f66fcSJoe Perches $fixed[$fixlinenr] =~ 44633705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 44643705ce5bSJoe Perches } 44650c73b4ebSAndy Whitcroft } 44660c73b4ebSAndy Whitcroft 446731070b5dSJoe Perches# Function pointer declarations 446831070b5dSJoe Perches# check spacing between type, funcptr, and args 446931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 447091f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 447131070b5dSJoe Perches my $declare = $1; 447231070b5dSJoe Perches my $pre_pointer_space = $2; 447331070b5dSJoe Perches my $post_pointer_space = $3; 447431070b5dSJoe Perches my $funcname = $4; 447531070b5dSJoe Perches my $post_funcname_space = $5; 447631070b5dSJoe Perches my $pre_args_space = $6; 447731070b5dSJoe Perches 447891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 447991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 448091f72e9cSJoe Perches# don't need a space so don't warn for those. 448191f72e9cSJoe Perches my $post_declare_space = ""; 448291f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 448391f72e9cSJoe Perches $post_declare_space = $1; 448491f72e9cSJoe Perches $declare = rtrim($declare); 448591f72e9cSJoe Perches } 448691f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 448731070b5dSJoe Perches WARN("SPACING", 448831070b5dSJoe Perches "missing space after return type\n" . $herecurr); 448991f72e9cSJoe Perches $post_declare_space = " "; 449031070b5dSJoe Perches } 449131070b5dSJoe Perches 449231070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 449391f72e9cSJoe Perches# This test is not currently implemented because these declarations are 449491f72e9cSJoe Perches# equivalent to 449591f72e9cSJoe Perches# int foo(int bar, ...) 449691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 449791f72e9cSJoe Perches# 449891f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 449991f72e9cSJoe Perches# WARN("SPACING", 450091f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 450191f72e9cSJoe Perches# } 450231070b5dSJoe Perches 450331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 450431070b5dSJoe Perches if (defined $pre_pointer_space && 450531070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 450631070b5dSJoe Perches WARN("SPACING", 450731070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 450831070b5dSJoe Perches } 450931070b5dSJoe Perches 451031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 451131070b5dSJoe Perches if (defined $post_pointer_space && 451231070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 451331070b5dSJoe Perches WARN("SPACING", 451431070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 451531070b5dSJoe Perches } 451631070b5dSJoe Perches 451731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 451831070b5dSJoe Perches if (defined $post_funcname_space && 451931070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 452031070b5dSJoe Perches WARN("SPACING", 452131070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 452231070b5dSJoe Perches } 452331070b5dSJoe Perches 452431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 452531070b5dSJoe Perches if (defined $pre_args_space && 452631070b5dSJoe Perches $pre_args_space =~ /^\s/) { 452731070b5dSJoe Perches WARN("SPACING", 452831070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 452931070b5dSJoe Perches } 453031070b5dSJoe Perches 453131070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4532194f66fcSJoe Perches $fixed[$fixlinenr] =~ 453391f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 453431070b5dSJoe Perches } 453531070b5dSJoe Perches } 453631070b5dSJoe Perches 45378d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 45388d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4539fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4540fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 45418d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 45428d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 45438d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4544fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 454538dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 45463705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 45473705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 45483705ce5bSJoe Perches $fix) { 4549194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45503705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 45513705ce5bSJoe Perches } 45528d31cfceSAndy Whitcroft } 45538d31cfceSAndy Whitcroft } 45548d31cfceSAndy Whitcroft 4555f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 45566c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4557c2fdda0dSAndy Whitcroft my $name = $1; 4558773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4559773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4560c2fdda0dSAndy Whitcroft 4561c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4562773647a0SAndy Whitcroft if ($name =~ /^(?: 4563773647a0SAndy Whitcroft if|for|while|switch|return|case| 4564773647a0SAndy Whitcroft volatile|__volatile__| 4565773647a0SAndy Whitcroft __attribute__|format|__extension__| 4566773647a0SAndy Whitcroft asm|__asm__)$/x) 4567773647a0SAndy Whitcroft { 4568c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4569c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4570c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4571c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4572773647a0SAndy Whitcroft 4573773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4574c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4575c2fdda0dSAndy Whitcroft 4576c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4577c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4578773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4579c2fdda0dSAndy Whitcroft 4580c2fdda0dSAndy Whitcroft } else { 45813705ce5bSJoe Perches if (WARN("SPACING", 45823705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 45833705ce5bSJoe Perches $fix) { 4584194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45853705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 45863705ce5bSJoe Perches } 4587f0a594c1SAndy Whitcroft } 45886c72ffaaSAndy Whitcroft } 45899a4cad4eSEric Nelson 4590653d4876SAndy Whitcroft# Check operator spacing. 45910a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 45923705ce5bSJoe Perches my $fixed_line = ""; 45933705ce5bSJoe Perches my $line_fixed = 0; 45943705ce5bSJoe Perches 45959c0ca6f9SAndy Whitcroft my $ops = qr{ 45969c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 45979c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 45989c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 45991f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 460084731623SJoe Perches \?:|\?|: 46019c0ca6f9SAndy Whitcroft }x; 4602cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 46033705ce5bSJoe Perches 46043705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 46053705ce5bSJoe Perches## foreach my $el (@elements) { 46063705ce5bSJoe Perches## print("el: <$el>\n"); 46073705ce5bSJoe Perches## } 46083705ce5bSJoe Perches 46093705ce5bSJoe Perches my @fix_elements = (); 461000df344fSAndy Whitcroft my $off = 0; 46116c72ffaaSAndy Whitcroft 46123705ce5bSJoe Perches foreach my $el (@elements) { 46133705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 46143705ce5bSJoe Perches $off += length($el); 46153705ce5bSJoe Perches } 46163705ce5bSJoe Perches 46173705ce5bSJoe Perches $off = 0; 46183705ce5bSJoe Perches 46196c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4620b34c648bSJoe Perches my $last_after = -1; 46216c72ffaaSAndy Whitcroft 46220a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 46233705ce5bSJoe Perches 46243705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 46253705ce5bSJoe Perches 46263705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 46273705ce5bSJoe Perches 46284a0df2efSAndy Whitcroft $off += length($elements[$n]); 46294a0df2efSAndy Whitcroft 463025985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4631773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4632773647a0SAndy Whitcroft my $cc = ''; 4633773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4634773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4635773647a0SAndy Whitcroft } 4636773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4637773647a0SAndy Whitcroft 46384a0df2efSAndy Whitcroft my $a = ''; 46394a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 46404a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4641cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 46424a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 46434a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4644773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 46454a0df2efSAndy Whitcroft 46460a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 46474a0df2efSAndy Whitcroft 46484a0df2efSAndy Whitcroft my $c = ''; 46490a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 46504a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 46514a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4652cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 46534a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 46544a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 46558b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 46564a0df2efSAndy Whitcroft } else { 46574a0df2efSAndy Whitcroft $c = 'E'; 46580a920b5bSAndy Whitcroft } 46590a920b5bSAndy Whitcroft 46604a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 46614a0df2efSAndy Whitcroft 46624a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 46634a0df2efSAndy Whitcroft 46646c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4665de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 46660a920b5bSAndy Whitcroft 466774048ed8SAndy Whitcroft # Pull out the value of this operator. 46686c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 46690a920b5bSAndy Whitcroft 46701f65f947SAndy Whitcroft # Get the full operator variant. 46711f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 46721f65f947SAndy Whitcroft 467313214adfSAndy Whitcroft # Ignore operators passed as parameters. 467413214adfSAndy Whitcroft if ($op_type ne 'V' && 4675d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 467613214adfSAndy Whitcroft 4677cf655043SAndy Whitcroft# # Ignore comments 4678cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 467913214adfSAndy Whitcroft 4680d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 468113214adfSAndy Whitcroft } elsif ($op eq ';') { 4682cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4683cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 46843705ce5bSJoe Perches if (ERROR("SPACING", 46853705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4686b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 46873705ce5bSJoe Perches $line_fixed = 1; 46883705ce5bSJoe Perches } 4689d8aaf121SAndy Whitcroft } 4690d8aaf121SAndy Whitcroft 4691d8aaf121SAndy Whitcroft # // is a comment 4692d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 46930a920b5bSAndy Whitcroft 4694b00e4814SJoe Perches # : when part of a bitfield 4695b00e4814SJoe Perches } elsif ($opv eq ':B') { 4696b00e4814SJoe Perches # skip the bitfield test for now 4697b00e4814SJoe Perches 46981f65f947SAndy Whitcroft # No spaces for: 46991f65f947SAndy Whitcroft # -> 4700b00e4814SJoe Perches } elsif ($op eq '->') { 47014a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 47023705ce5bSJoe Perches if (ERROR("SPACING", 47033705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4704b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 47053705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 47063705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 47073705ce5bSJoe Perches } 4708b34c648bSJoe Perches $line_fixed = 1; 47093705ce5bSJoe Perches } 47100a920b5bSAndy Whitcroft } 47110a920b5bSAndy Whitcroft 47122381097bSJoe Perches # , must not have a space before and must have a space on the right. 47130a920b5bSAndy Whitcroft } elsif ($op eq ',') { 47142381097bSJoe Perches my $rtrim_before = 0; 47152381097bSJoe Perches my $space_after = 0; 47162381097bSJoe Perches if ($ctx =~ /Wx./) { 47172381097bSJoe Perches if (ERROR("SPACING", 47182381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 47192381097bSJoe Perches $line_fixed = 1; 47202381097bSJoe Perches $rtrim_before = 1; 47212381097bSJoe Perches } 47222381097bSJoe Perches } 4723cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 47243705ce5bSJoe Perches if (ERROR("SPACING", 47253705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 47263705ce5bSJoe Perches $line_fixed = 1; 4727b34c648bSJoe Perches $last_after = $n; 47282381097bSJoe Perches $space_after = 1; 47292381097bSJoe Perches } 47302381097bSJoe Perches } 47312381097bSJoe Perches if ($rtrim_before || $space_after) { 47322381097bSJoe Perches if ($rtrim_before) { 47332381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 47342381097bSJoe Perches } else { 47352381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 47362381097bSJoe Perches } 47372381097bSJoe Perches if ($space_after) { 47382381097bSJoe Perches $good .= " "; 47393705ce5bSJoe Perches } 47400a920b5bSAndy Whitcroft } 47410a920b5bSAndy Whitcroft 47429c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 474374048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 47449c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 47459c0ca6f9SAndy Whitcroft 47469c0ca6f9SAndy Whitcroft # unary operators should have a space before and 47479c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 47489c0ca6f9SAndy Whitcroft # unary operator, or a cast 47499c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 475074048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 47510d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4752cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 47533705ce5bSJoe Perches if (ERROR("SPACING", 47543705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4755b34c648bSJoe Perches if ($n != $last_after + 2) { 4756b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 47573705ce5bSJoe Perches $line_fixed = 1; 47583705ce5bSJoe Perches } 47590a920b5bSAndy Whitcroft } 4760b34c648bSJoe Perches } 4761a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4762171ae1a4SAndy Whitcroft # A unary '*' may be const 4763171ae1a4SAndy Whitcroft 4764171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 47653705ce5bSJoe Perches if (ERROR("SPACING", 47663705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4767b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 47683705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 47693705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 47703705ce5bSJoe Perches } 4771b34c648bSJoe Perches $line_fixed = 1; 47723705ce5bSJoe Perches } 47730a920b5bSAndy Whitcroft } 47740a920b5bSAndy Whitcroft 47750a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 47760a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4777773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 47783705ce5bSJoe Perches if (ERROR("SPACING", 47793705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4780b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 47813705ce5bSJoe Perches $line_fixed = 1; 47823705ce5bSJoe Perches } 47830a920b5bSAndy Whitcroft } 4784773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4785773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 47863705ce5bSJoe Perches if (ERROR("SPACING", 47873705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4788b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 47893705ce5bSJoe Perches $line_fixed = 1; 47903705ce5bSJoe Perches } 4791653d4876SAndy Whitcroft } 4792773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 47933705ce5bSJoe Perches if (ERROR("SPACING", 47943705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4795b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 47963705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 47973705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4798773647a0SAndy Whitcroft } 4799b34c648bSJoe Perches $line_fixed = 1; 48003705ce5bSJoe Perches } 48013705ce5bSJoe Perches } 48020a920b5bSAndy Whitcroft 48030a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 48049c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 48059c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 48069c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4807c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4808c2fdda0dSAndy Whitcroft $op eq '%') 48090a920b5bSAndy Whitcroft { 4810d2e025f3SJoe Perches if ($check) { 4811d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4812d2e025f3SJoe Perches if (CHK("SPACING", 4813d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4814d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4815d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4816d2e025f3SJoe Perches $line_fixed = 1; 4817d2e025f3SJoe Perches } 4818d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4819d2e025f3SJoe Perches if (CHK("SPACING", 4820d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4821d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4822d2e025f3SJoe Perches $line_fixed = 1; 4823d2e025f3SJoe Perches } 4824d2e025f3SJoe Perches } 4825d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 48263705ce5bSJoe Perches if (ERROR("SPACING", 48273705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4828b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4829b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4830b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4831b34c648bSJoe Perches } 48323705ce5bSJoe Perches $line_fixed = 1; 48333705ce5bSJoe Perches } 48340a920b5bSAndy Whitcroft } 48350a920b5bSAndy Whitcroft 48361f65f947SAndy Whitcroft # A colon needs no spaces before when it is 48371f65f947SAndy Whitcroft # terminating a case value or a label. 48381f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 48391f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 48403705ce5bSJoe Perches if (ERROR("SPACING", 48413705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4842b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 48433705ce5bSJoe Perches $line_fixed = 1; 48443705ce5bSJoe Perches } 48451f65f947SAndy Whitcroft } 48461f65f947SAndy Whitcroft 48470a920b5bSAndy Whitcroft # All the others need spaces both sides. 4848cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 48491f65f947SAndy Whitcroft my $ok = 0; 48501f65f947SAndy Whitcroft 485122f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 48521f65f947SAndy Whitcroft if (($op eq '<' && 48531f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 48541f65f947SAndy Whitcroft ($op eq '>' && 48551f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 48561f65f947SAndy Whitcroft { 48571f65f947SAndy Whitcroft $ok = 1; 48581f65f947SAndy Whitcroft } 48591f65f947SAndy Whitcroft 4860e0df7e1fSJoe Perches # for asm volatile statements 4861e0df7e1fSJoe Perches # ignore a colon with another 4862e0df7e1fSJoe Perches # colon immediately before or after 4863e0df7e1fSJoe Perches if (($op eq ':') && 4864e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4865e0df7e1fSJoe Perches $ok = 1; 4866e0df7e1fSJoe Perches } 4867e0df7e1fSJoe Perches 486884731623SJoe Perches # messages are ERROR, but ?: are CHK 48691f65f947SAndy Whitcroft if ($ok == 0) { 48700675a8fbSJean Delvare my $msg_level = \&ERROR; 48710675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 487284731623SJoe Perches 48730675a8fbSJean Delvare if (&{$msg_level}("SPACING", 48743705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4875b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4876b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4877b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4878b34c648bSJoe Perches } 48793705ce5bSJoe Perches $line_fixed = 1; 48803705ce5bSJoe Perches } 48810a920b5bSAndy Whitcroft } 488222f2a2efSAndy Whitcroft } 48834a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 48843705ce5bSJoe Perches 48853705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 48863705ce5bSJoe Perches 48873705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 48880a920b5bSAndy Whitcroft } 48893705ce5bSJoe Perches 48903705ce5bSJoe Perches if (($#elements % 2) == 0) { 48913705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 48923705ce5bSJoe Perches } 48933705ce5bSJoe Perches 4894194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4895194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 48963705ce5bSJoe Perches } 48973705ce5bSJoe Perches 48983705ce5bSJoe Perches 48990a920b5bSAndy Whitcroft } 49000a920b5bSAndy Whitcroft 4901786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4902d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4903786b6326SJoe Perches if (WARN("SPACING", 4904786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4905786b6326SJoe Perches $fix) { 4906194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4907786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4908786b6326SJoe Perches } 4909786b6326SJoe Perches } 4910786b6326SJoe Perches 4911f0a594c1SAndy Whitcroft# check for multiple assignments 4912f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4913000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4914000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4915f0a594c1SAndy Whitcroft } 4916f0a594c1SAndy Whitcroft 491722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 491822f2a2efSAndy Whitcroft## # continuation. 491922f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 492022f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 492122f2a2efSAndy Whitcroft## 492222f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 492322f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 492422f2a2efSAndy Whitcroft## my $ln = $line; 492522f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 492622f2a2efSAndy Whitcroft## } 492722f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4928000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4929000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 493022f2a2efSAndy Whitcroft## } 493122f2a2efSAndy Whitcroft## } 4932f0a594c1SAndy Whitcroft 49330a920b5bSAndy Whitcroft#need space before brace following if, while, etc 49346b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 49356ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 49363705ce5bSJoe Perches if (ERROR("SPACING", 49373705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 49383705ce5bSJoe Perches $fix) { 49396ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 49403705ce5bSJoe Perches } 4941de7d4f0eSAndy Whitcroft } 4942de7d4f0eSAndy Whitcroft 4943c4a62ef9SJoe Perches## # check for blank lines before declarations 4944c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4945c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4946c4a62ef9SJoe Perches## WARN("SPACING", 4947c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4948c4a62ef9SJoe Perches## } 4949c4a62ef9SJoe Perches## 4950c4a62ef9SJoe Perches 4951de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4952de7d4f0eSAndy Whitcroft# on the line 495394fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 4954d5e616fcSJoe Perches if (ERROR("SPACING", 4955d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4956d5e616fcSJoe Perches $fix) { 4957194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4958d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4959d5e616fcSJoe Perches } 49600a920b5bSAndy Whitcroft } 49610a920b5bSAndy Whitcroft 496222f2a2efSAndy Whitcroft# check spacing on square brackets 496322f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 49643705ce5bSJoe Perches if (ERROR("SPACING", 49653705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 49663705ce5bSJoe Perches $fix) { 4967194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49683705ce5bSJoe Perches s/\[\s+/\[/; 49693705ce5bSJoe Perches } 497022f2a2efSAndy Whitcroft } 497122f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 49723705ce5bSJoe Perches if (ERROR("SPACING", 49733705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 49743705ce5bSJoe Perches $fix) { 4975194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49763705ce5bSJoe Perches s/\s+\]/\]/; 49773705ce5bSJoe Perches } 497822f2a2efSAndy Whitcroft } 497922f2a2efSAndy Whitcroft 4980c45dcabdSAndy Whitcroft# check spacing on parentheses 49819c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 49829c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 49833705ce5bSJoe Perches if (ERROR("SPACING", 49843705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 49853705ce5bSJoe Perches $fix) { 4986194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49873705ce5bSJoe Perches s/\(\s+/\(/; 49883705ce5bSJoe Perches } 498922f2a2efSAndy Whitcroft } 499013214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4991c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4992c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 49933705ce5bSJoe Perches if (ERROR("SPACING", 49943705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 49953705ce5bSJoe Perches $fix) { 4996194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49973705ce5bSJoe Perches s/\s+\)/\)/; 49983705ce5bSJoe Perches } 499922f2a2efSAndy Whitcroft } 500022f2a2efSAndy Whitcroft 5001e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 5002e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 5003e2826fd0SJoe Perches 5004e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 5005ea4acbb1SJoe Perches my $var = $1; 5006ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5007ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 5008ea4acbb1SJoe Perches $fix) { 5009ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 5010ea4acbb1SJoe Perches } 5011ea4acbb1SJoe Perches } 5012ea4acbb1SJoe Perches 5013ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 5014ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 5015ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 5016ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 5017ea4acbb1SJoe Perches my $var = $2; 5018ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5019ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 5020ea4acbb1SJoe Perches $fix) { 5021ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 5022ea4acbb1SJoe Perches $var2 =~ s/\s//g; 5023ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 5024ea4acbb1SJoe Perches } 5025e2826fd0SJoe Perches } 5026e2826fd0SJoe Perches 502763b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 5028a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 5029a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 50305b57980dSJoe Perches $perl_version_ok && defined($stat) && 503163b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 503263b7c73eSJoe Perches my $if_stat = $1; 503363b7c73eSJoe Perches my $test = substr($2, 1, -1); 503463b7c73eSJoe Perches my $herectx; 503563b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 503663b7c73eSJoe Perches my $match = $1; 503763b7c73eSJoe Perches # avoid parentheses around potential macro args 503863b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 503963b7c73eSJoe Perches if (!defined($herectx)) { 504063b7c73eSJoe Perches $herectx = $here . "\n"; 504163b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 504263b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 504363b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 504463b7c73eSJoe Perches $herectx .= $rl . "\n"; 504563b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 504663b7c73eSJoe Perches } 504763b7c73eSJoe Perches } 504863b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 504963b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 505063b7c73eSJoe Perches } 505163b7c73eSJoe Perches } 505263b7c73eSJoe Perches 50530a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 50544a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 50550a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 50563705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 50573705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 50583705ce5bSJoe Perches $fix) { 5059194f66fcSJoe Perches $fixed[$fixlinenr] =~ 50603705ce5bSJoe Perches s/^(.)\s+/$1/; 50613705ce5bSJoe Perches } 50620a920b5bSAndy Whitcroft } 50630a920b5bSAndy Whitcroft 506440873abaSJoe Perches# check if a statement with a comma should be two statements like: 506540873abaSJoe Perches# foo = bar(), /* comma should be semicolon */ 506640873abaSJoe Perches# bar = baz(); 506740873abaSJoe Perches if (defined($stat) && 506840873abaSJoe Perches $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { 506940873abaSJoe Perches my $cnt = statement_rawlines($stat); 507040873abaSJoe Perches my $herectx = get_stat_here($linenr, $cnt, $here); 507140873abaSJoe Perches WARN("SUSPECT_COMMA_SEMICOLON", 507240873abaSJoe Perches "Possible comma where semicolon could be used\n" . $herectx); 507340873abaSJoe Perches } 507440873abaSJoe Perches 50755b9553abSJoe Perches# return is not a function 5076507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 5077c45dcabdSAndy Whitcroft my $spacing = $1; 50785b57980dSJoe Perches if ($perl_version_ok && 50795b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 50805b9553abSJoe Perches my $value = $1; 50815b9553abSJoe Perches $value = deparenthesize($value); 50825b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 5083000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 5084000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 50855b9553abSJoe Perches } 5086c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 5087000d1cc1SJoe Perches ERROR("SPACING", 5088000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 5089c45dcabdSAndy Whitcroft } 5090c45dcabdSAndy Whitcroft } 5091507e5141SJoe Perches 5092b43ae21bSJoe Perches# unnecessary return in a void function 5093b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 5094b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 5095b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 5096b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 5097b43ae21bSJoe Perches $linenr >= 3 && 5098b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 5099b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 51009819cf25SJoe Perches WARN("RETURN_VOID", 5101b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 51029819cf25SJoe Perches } 51039819cf25SJoe Perches 5104189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 51055b57980dSJoe Perches if ($perl_version_ok && 5106189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 5107189248d8SJoe Perches my $openparens = $1; 5108189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 5109189248d8SJoe Perches my $msg = ""; 5110189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 5111189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 5112189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 5113189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 5114189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 5115189248d8SJoe Perches } 5116189248d8SJoe Perches } 5117189248d8SJoe Perches 5118c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 5119c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 5120c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 5121c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 51225b57980dSJoe Perches if ($perl_version_ok && 5123c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 5124c5595fa2SJoe Perches my $lead = $1; 5125c5595fa2SJoe Perches my $const = $2; 5126c5595fa2SJoe Perches my $comp = $3; 5127c5595fa2SJoe Perches my $to = $4; 5128c5595fa2SJoe Perches my $newcomp = $comp; 5129f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 5130c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 5131c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 5132c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 5133c5595fa2SJoe Perches $fix) { 5134c5595fa2SJoe Perches if ($comp eq "<") { 5135c5595fa2SJoe Perches $newcomp = ">"; 5136c5595fa2SJoe Perches } elsif ($comp eq "<=") { 5137c5595fa2SJoe Perches $newcomp = ">="; 5138c5595fa2SJoe Perches } elsif ($comp eq ">") { 5139c5595fa2SJoe Perches $newcomp = "<"; 5140c5595fa2SJoe Perches } elsif ($comp eq ">=") { 5141c5595fa2SJoe Perches $newcomp = "<="; 5142c5595fa2SJoe Perches } 5143c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 5144c5595fa2SJoe Perches } 5145c5595fa2SJoe Perches } 5146c5595fa2SJoe Perches 5147f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 5148f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 514953a3c448SAndy Whitcroft my $name = $1; 515053a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 5151000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 5152f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 515353a3c448SAndy Whitcroft } 515453a3c448SAndy Whitcroft } 5155c45dcabdSAndy Whitcroft 51560a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 51574a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 51583705ce5bSJoe Perches if (ERROR("SPACING", 51593705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 51603705ce5bSJoe Perches $fix) { 5161194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51623705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 51633705ce5bSJoe Perches } 51640a920b5bSAndy Whitcroft } 51650a920b5bSAndy Whitcroft 5166f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 5167f5fe35ddSAndy Whitcroft# statements after the conditional. 5168170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 51693e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 51703e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 51713e469cdcSAndy Whitcroft if (!defined $stat); 5172170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 5173170d3a22SAndy Whitcroft $remain_next, $off_next); 5174170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 5175170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 5176170d3a22SAndy Whitcroft 5177170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 5178170d3a22SAndy Whitcroft # If the statement carries leading newlines, 5179170d3a22SAndy Whitcroft # then count those as offsets. 5180170d3a22SAndy Whitcroft my ($whitespace) = 5181170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 5182170d3a22SAndy Whitcroft my $offset = 5183170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 5184170d3a22SAndy Whitcroft 5185170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 5186170d3a22SAndy Whitcroft $offset} = 1; 5187170d3a22SAndy Whitcroft } 5188170d3a22SAndy Whitcroft } 5189170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 5190c11230f4SJoe Perches defined($stat) && defined($cond) && 5191170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 5192171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 51938905a67cSAndy Whitcroft 5194b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 519565b64b3bSJoe Perches if (ERROR("ASSIGN_IN_IF", 519665b64b3bSJoe Perches "do not use assignment in if condition\n" . $herecurr) && 519765b64b3bSJoe Perches $fix && $perl_version_ok) { 519865b64b3bSJoe Perches if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { 519965b64b3bSJoe Perches my $space = $1; 520065b64b3bSJoe Perches my $not = $2; 520165b64b3bSJoe Perches my $statement = $3; 520265b64b3bSJoe Perches my $assigned = $4; 520365b64b3bSJoe Perches my $test = $8; 520465b64b3bSJoe Perches my $against = $9; 520565b64b3bSJoe Perches my $brace = $15; 520665b64b3bSJoe Perches fix_delete_line($fixlinenr, $rawline); 520765b64b3bSJoe Perches fix_insert_line($fixlinenr, "$space$statement;"); 520865b64b3bSJoe Perches my $newline = "${space}if ("; 520965b64b3bSJoe Perches $newline .= '!' if defined($not); 521065b64b3bSJoe Perches $newline .= '(' if (defined $not && defined($test) && defined($against)); 521165b64b3bSJoe Perches $newline .= "$assigned"; 521265b64b3bSJoe Perches $newline .= " $test $against" if (defined($test) && defined($against)); 521365b64b3bSJoe Perches $newline .= ')' if (defined $not && defined($test) && defined($against)); 521465b64b3bSJoe Perches $newline .= ')'; 521565b64b3bSJoe Perches $newline .= " {" if (defined($brace)); 521665b64b3bSJoe Perches fix_insert_line($fixlinenr + 1, $newline); 521765b64b3bSJoe Perches } 521865b64b3bSJoe Perches } 52198905a67cSAndy Whitcroft } 52208905a67cSAndy Whitcroft 52218905a67cSAndy Whitcroft # Find out what is on the end of the line after the 52228905a67cSAndy Whitcroft # conditional. 5223773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 52248905a67cSAndy Whitcroft $s =~ s/\n.*//g; 522513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 522653210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 522753210168SAndy Whitcroft $c !~ /}\s*while\s*/) 5228773647a0SAndy Whitcroft { 5229bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 5230bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 5231bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 523242bdf74cSHidetoshi Seto my $stat_real = ''; 5233bb44ad39SAndy Whitcroft 523442bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 523542bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 5236bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 5237bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 5238bb44ad39SAndy Whitcroft } 5239bb44ad39SAndy Whitcroft 5240000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5241000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 52428905a67cSAndy Whitcroft } 52438905a67cSAndy Whitcroft } 52448905a67cSAndy Whitcroft 524513214adfSAndy Whitcroft# Check for bitwise tests written as boolean 524613214adfSAndy Whitcroft if ($line =~ / 524713214adfSAndy Whitcroft (?: 524813214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 524913214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 525013214adfSAndy Whitcroft (?:\&\&|\|\|) 525113214adfSAndy Whitcroft | 525213214adfSAndy Whitcroft (?:\&\&|\|\|) 525313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 525413214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 525513214adfSAndy Whitcroft )/x) 525613214adfSAndy Whitcroft { 5257000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 5258000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 525913214adfSAndy Whitcroft } 526013214adfSAndy Whitcroft 52618905a67cSAndy Whitcroft# if and else should not have general statements after it 526213214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 526313214adfSAndy Whitcroft my $s = $1; 526413214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 526513214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5266000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5267000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 52680a920b5bSAndy Whitcroft } 526913214adfSAndy Whitcroft } 527039667782SAndy Whitcroft# if should not continue a brace 527139667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5272000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5273048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 527439667782SAndy Whitcroft $herecurr); 527539667782SAndy Whitcroft } 5276a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5277a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5278a1080bf8SAndy Whitcroft $line !~ /\G(?: 52793fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5280a1080bf8SAndy Whitcroft \s*return\s+ 5281a1080bf8SAndy Whitcroft )/xg) 5282a1080bf8SAndy Whitcroft { 5283000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5284000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5285a1080bf8SAndy Whitcroft } 52860a920b5bSAndy Whitcroft 52870a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 52880a920b5bSAndy Whitcroft # indent level to be relevant to each other. 52898b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 52900a920b5bSAndy Whitcroft $previndent == $indent) { 52918b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 52928b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 52938b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 52948b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 52958b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 52968b8856f4SJoe Perches my $fixedline = $prevrawline; 52978b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 52988b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 52998b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 53008b8856f4SJoe Perches } 53018b8856f4SJoe Perches $fixedline = $rawline; 53028b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 53038b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 53048b8856f4SJoe Perches } 53050a920b5bSAndy Whitcroft } 53060a920b5bSAndy Whitcroft 53078b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5308c2fdda0dSAndy Whitcroft $previndent == $indent) { 5309c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5310c2fdda0dSAndy Whitcroft 5311c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5312c2fdda0dSAndy Whitcroft # conditional. 5313773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5314c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5315c2fdda0dSAndy Whitcroft 5316c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 53178b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 53188b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 53198b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 53208b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 53218b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 53228b8856f4SJoe Perches my $fixedline = $prevrawline; 53238b8856f4SJoe Perches my $trailing = $rawline; 53248b8856f4SJoe Perches $trailing =~ s/^\+//; 53258b8856f4SJoe Perches $trailing = trim($trailing); 53268b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 53278b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 53288b8856f4SJoe Perches } 5329c2fdda0dSAndy Whitcroft } 5330c2fdda0dSAndy Whitcroft } 5331c2fdda0dSAndy Whitcroft 533295e2c602SJoe Perches#Specific variable tests 5333323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5334323c1260SJoe Perches my $var = $1; 533595e2c602SJoe Perches 533695e2c602SJoe Perches#CamelCase 5337807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5338be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 53394104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values 53404104a206SŁukasz Stelmach $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ && 534122735ce8SJoe Perches#Ignore Page<foo> variants 5342807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5343d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5344d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5345d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5346f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5347f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 53487e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 53497e781f67SJoe Perches my $word = $1; 53507e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5351d8b07710SJoe Perches if ($check) { 5352d8b07710SJoe Perches seed_camelcase_includes(); 5353d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5354d8b07710SJoe Perches seed_camelcase_file($realfile); 5355d8b07710SJoe Perches $camelcase_file_seeded = 1; 5356d8b07710SJoe Perches } 5357d8b07710SJoe Perches } 53587e781f67SJoe Perches if (!defined $camelcase{$word}) { 53597e781f67SJoe Perches $camelcase{$word} = 1; 5360be79794bSJoe Perches CHK("CAMELCASE", 53617e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 53627e781f67SJoe Perches } 5363323c1260SJoe Perches } 5364323c1260SJoe Perches } 53653445686aSJoe Perches } 53660a920b5bSAndy Whitcroft 53670a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5368d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5369d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5370d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5371d5e616fcSJoe Perches $fix) { 5372194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5373d5e616fcSJoe Perches } 53740a920b5bSAndy Whitcroft } 53750a920b5bSAndy Whitcroft 53760e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 53770e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5378c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5379e09dec48SAndy Whitcroft my $file = "$1.h"; 5380e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5381e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5382e09dec48SAndy Whitcroft $realfile ne $checkfile && 53837840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5384c45dcabdSAndy Whitcroft { 53850e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 53860e212e0aSFabian Frederick if ($asminclude > 0) { 5387e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5388000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5389000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5390e09dec48SAndy Whitcroft } else { 5391000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5392000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5393e09dec48SAndy Whitcroft } 53940a920b5bSAndy Whitcroft } 53950a920b5bSAndy Whitcroft } 53960e212e0aSFabian Frederick } 53970a920b5bSAndy Whitcroft 5398653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5399653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5400cf655043SAndy Whitcroft# in a known good container 5401b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5402b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5403d8aaf121SAndy Whitcroft my $ln = $linenr; 5404d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5405c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5406c45dcabdSAndy Whitcroft my $ctx = ''; 540708a2843eSJoe Perches my $has_flow_statement = 0; 540808a2843eSJoe Perches my $has_arg_concat = 0; 5409c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5410f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5411f74bd194SAndy Whitcroft $ctx = $dstat; 5412c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5413a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5414c45dcabdSAndy Whitcroft 541508a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 541662e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 541708a2843eSJoe Perches 5418f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5419f59b64bfSJoe Perches my $define_args = $1; 5420f59b64bfSJoe Perches my $define_stmt = $dstat; 5421f59b64bfSJoe Perches my @def_args = (); 5422f59b64bfSJoe Perches 5423f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5424f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5425f59b64bfSJoe Perches $define_args =~ s/\s*//g; 54268c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5427f59b64bfSJoe Perches @def_args = split(",", $define_args); 5428f59b64bfSJoe Perches } 5429f59b64bfSJoe Perches 5430292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5431c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5432c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5433c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5434c45dcabdSAndy Whitcroft 5435c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 54362e44e803SDwaipayan Ray while ($dstat =~ s/\([^\(\)]*\)/1u/ || 54372e44e803SDwaipayan Ray $dstat =~ s/\{[^\{\}]*\}/1u/ || 54382e44e803SDwaipayan Ray $dstat =~ s/.\[[^\[\]]*\]/1u/) 5439bf30d6edSAndy Whitcroft { 5440c45dcabdSAndy Whitcroft } 5441c45dcabdSAndy Whitcroft 5442342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 544333acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 544433acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5445e45bab8eSAndy Whitcroft { 5446e45bab8eSAndy Whitcroft } 5447e45bab8eSAndy Whitcroft 544842e15293SJoe Perches # Make asm volatile uses seem like a generic function 544942e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 545042e15293SJoe Perches 5451c45dcabdSAndy Whitcroft my $exceptions = qr{ 5452c45dcabdSAndy Whitcroft $Declare| 5453c45dcabdSAndy Whitcroft module_param_named| 5454a0a0a7a9SKees Cook MODULE_PARM_DESC| 5455c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5456c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5457383099fdSAndy Whitcroft __typeof__\(| 545822fd2d3eSStefani Seibold union| 545922fd2d3eSStefani Seibold struct| 5460ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 54616b10df42SVladimir Zapolskiy ^\"|\"$| 54626b10df42SVladimir Zapolskiy ^\[ 5463c45dcabdSAndy Whitcroft }x; 54645eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5465f59b64bfSJoe Perches 5466f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5467f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5468e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5469f59b64bfSJoe Perches 5470f74bd194SAndy Whitcroft if ($dstat ne '' && 5471f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5472f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 54733cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5474356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5475f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5476f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5477e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 547872f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 54792e44e803SDwaipayan Ray $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} 5480f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5481f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5482f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 54834e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5484f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5485c45dcabdSAndy Whitcroft { 5486e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5487e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5488e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5489e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5490f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5491f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5492f74bd194SAndy Whitcroft } else { 5493000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5494388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5495d8aaf121SAndy Whitcroft } 5496f59b64bfSJoe Perches 5497f59b64bfSJoe Perches } 54985207649bSJoe Perches 54995207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 55005207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 55015207649bSJoe Perches my $first = 1; 55025207649bSJoe Perches $define_stmt = ""; 55035207649bSJoe Perches foreach my $l (@stmt_array) { 55045207649bSJoe Perches $l =~ s/\\$//; 55055207649bSJoe Perches if ($first) { 55065207649bSJoe Perches $define_stmt = $l; 55075207649bSJoe Perches $first = 0; 55085207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 55095207649bSJoe Perches $define_stmt .= substr($l, 1); 55105207649bSJoe Perches } 55115207649bSJoe Perches } 55125207649bSJoe Perches $define_stmt =~ s/$;//g; 55135207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 55145207649bSJoe Perches $define_stmt = trim($define_stmt); 55155207649bSJoe Perches 5516f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5517f59b64bfSJoe Perches foreach my $arg (@def_args) { 5518f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 55199192d41aSJoe Perches next if ($arg =~ /^type$/i); 55207fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 55216dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 55227fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 55237fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5524d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5525f59b64bfSJoe Perches if ($use_cnt > 1) { 5526f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5527f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5528f59b64bfSJoe Perches } 55299192d41aSJoe Perches# check if any macro arguments may have other precedence issues 55307fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 55319192d41aSJoe Perches ((defined($1) && $1 ne ',') || 55329192d41aSJoe Perches (defined($2) && $2 ne ','))) { 55339192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 55349192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 55359192d41aSJoe Perches } 55360a920b5bSAndy Whitcroft } 55375023d347SJoe Perches 553808a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 553908a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 554008a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 554108a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5542e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 554308a2843eSJoe Perches 554408a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 554508a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 554608a2843eSJoe Perches } 554708a2843eSJoe Perches 5548481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 55495023d347SJoe Perches 55505023d347SJoe Perches } else { 55515023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5552481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5553481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 55545023d347SJoe Perches $line =~ /^\+.*\\$/) { 55555023d347SJoe Perches WARN("LINE_CONTINUATIONS", 55565023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 55575023d347SJoe Perches } 5558653d4876SAndy Whitcroft } 55590a920b5bSAndy Whitcroft 5560b13edf7fSJoe Perches# do {} while (0) macro tests: 5561b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5562b13edf7fSJoe Perches# macro should not end with a semicolon 55635b57980dSJoe Perches if ($perl_version_ok && 5564b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5565b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5566b13edf7fSJoe Perches my $ln = $linenr; 5567b13edf7fSJoe Perches my $cnt = $realcnt; 5568b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5569b13edf7fSJoe Perches my $ctx = ''; 5570b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5571b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5572b13edf7fSJoe Perches $ctx = $dstat; 5573b13edf7fSJoe Perches 5574b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 55751b36b201SJoe Perches $dstat =~ s/$;/ /g; 5576b13edf7fSJoe Perches 5577b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5578b13edf7fSJoe Perches my $stmts = $2; 5579b13edf7fSJoe Perches my $semis = $3; 5580b13edf7fSJoe Perches 5581b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5582b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5583e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5584b13edf7fSJoe Perches 5585ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5586ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5587b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5588b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5589b13edf7fSJoe Perches } 5590b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5591b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5592b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5593b13edf7fSJoe Perches } 5594f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5595f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5596f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5597e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5598f5ef95b1SJoe Perches 5599f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5600f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5601b13edf7fSJoe Perches } 5602b13edf7fSJoe Perches } 5603b13edf7fSJoe Perches 5604f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 560513214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 560613214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5607cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 560813214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5609cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5610cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5611aad4f614SJoe Perches my @allowed = (); 5612aad4f614SJoe Perches my $allow = 0; 561313214adfSAndy Whitcroft my $seen = 0; 5614773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5615cf655043SAndy Whitcroft my $ln = $linenr - 1; 561613214adfSAndy Whitcroft for my $chunk (@chunks) { 561713214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 561813214adfSAndy Whitcroft 5619773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5620773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5621773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5622773647a0SAndy Whitcroft 5623aad4f614SJoe Perches $allowed[$allow] = 0; 5624773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5625773647a0SAndy Whitcroft 5626773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5627773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5628773647a0SAndy Whitcroft 5629773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5630cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5631cf655043SAndy Whitcroft 5632773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 563313214adfSAndy Whitcroft 563413214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 563513214adfSAndy Whitcroft 5636aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5637cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5638cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5639aad4f614SJoe Perches $allowed[$allow] = 1; 564013214adfSAndy Whitcroft } 564113214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5642cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5643aad4f614SJoe Perches $allowed[$allow] = 1; 564413214adfSAndy Whitcroft } 5645cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5646cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5647aad4f614SJoe Perches $allowed[$allow] = 1; 564813214adfSAndy Whitcroft } 5649aad4f614SJoe Perches $allow++; 565013214adfSAndy Whitcroft } 5651aad4f614SJoe Perches if ($seen) { 5652aad4f614SJoe Perches my $sum_allowed = 0; 5653aad4f614SJoe Perches foreach (@allowed) { 5654aad4f614SJoe Perches $sum_allowed += $_; 5655aad4f614SJoe Perches } 5656aad4f614SJoe Perches if ($sum_allowed == 0) { 5657000d1cc1SJoe Perches WARN("BRACES", 5658000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5659aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5660aad4f614SJoe Perches $seen != $allow) { 5661aad4f614SJoe Perches CHK("BRACES", 5662aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5663aad4f614SJoe Perches } 566413214adfSAndy Whitcroft } 566513214adfSAndy Whitcroft } 566613214adfSAndy Whitcroft } 5667773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 566813214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5669cf655043SAndy Whitcroft my $allowed = 0; 5670f0a594c1SAndy Whitcroft 5671cf655043SAndy Whitcroft # Check the pre-context. 5672cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5673cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5674cf655043SAndy Whitcroft $allowed = 1; 5675f0a594c1SAndy Whitcroft } 5676773647a0SAndy Whitcroft 5677773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5678773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5679773647a0SAndy Whitcroft 5680cf655043SAndy Whitcroft # Check the condition. 5681cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5682773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5683cf655043SAndy Whitcroft if (defined $cond) { 5684773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5685cf655043SAndy Whitcroft } 5686cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5687cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5688cf655043SAndy Whitcroft $allowed = 1; 5689cf655043SAndy Whitcroft } 5690cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5691cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5692cf655043SAndy Whitcroft $allowed = 1; 5693cf655043SAndy Whitcroft } 5694cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5695cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5696cf655043SAndy Whitcroft $allowed = 1; 5697cf655043SAndy Whitcroft } 5698cf655043SAndy Whitcroft # Check the post-context. 5699cf655043SAndy Whitcroft if (defined $chunks[1]) { 5700cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5701cf655043SAndy Whitcroft if (defined $cond) { 5702773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5703cf655043SAndy Whitcroft } 5704cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5705cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5706cf655043SAndy Whitcroft $allowed = 1; 5707cf655043SAndy Whitcroft } 5708cf655043SAndy Whitcroft } 5709cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5710f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5711e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5712cf655043SAndy Whitcroft 5713000d1cc1SJoe Perches WARN("BRACES", 5714000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5715f0a594c1SAndy Whitcroft } 5716f0a594c1SAndy Whitcroft } 5717f0a594c1SAndy Whitcroft 5718e4c5babdSJoe Perches# check for single line unbalanced braces 571995330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 572095330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5721e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5722e4c5babdSJoe Perches } 5723e4c5babdSJoe Perches 57240979ae66SJoe Perches# check for unnecessary blank lines around braces 572577b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5726f8e58219SJoe Perches if (CHK("BRACES", 5727f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5728f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5729f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5730f8e58219SJoe Perches } 57310979ae66SJoe Perches } 573277b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5733f8e58219SJoe Perches if (CHK("BRACES", 5734f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5735f8e58219SJoe Perches $fix) { 5736f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5737f8e58219SJoe Perches } 57380979ae66SJoe Perches } 57390979ae66SJoe Perches 57404a0df2efSAndy Whitcroft# no volatiles please 57416c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 57426c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5743000d1cc1SJoe Perches WARN("VOLATILE", 57448c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 57454a0df2efSAndy Whitcroft } 57464a0df2efSAndy Whitcroft 57475e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 57485e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 57495e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 57505e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 575133acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 57525e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 57535e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 57545e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 57555e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 57565e4f6ba5SJoe Perches $fix && 57575e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 57585e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 57595e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 57605e4f6ba5SJoe Perches my $comma_close = ""; 57615e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 57625e4f6ba5SJoe Perches $comma_close = $1; 57635e4f6ba5SJoe Perches } 57645e4f6ba5SJoe Perches 57655e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 57665e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 57675e4f6ba5SJoe Perches my $fixedline = $prevrawline; 57685e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 57695e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 57705e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 57715e4f6ba5SJoe Perches $fixedline = $rawline; 57725e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 57735e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 57745e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 57755e4f6ba5SJoe Perches } 57765e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 57775e4f6ba5SJoe Perches } 57785e4f6ba5SJoe Perches } 57795e4f6ba5SJoe Perches 57805e4f6ba5SJoe Perches# check for missing a space in a string concatenation 57815e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 57825e4f6ba5SJoe Perches WARN('MISSING_SPACE', 57835e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 57845e4f6ba5SJoe Perches } 57855e4f6ba5SJoe Perches 578677cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5787e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5788e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5789e4b7d309SJoe Perches# function declarations 579077cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 579177cb8546SJoe Perches defined($context_function) && 5792e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5793e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 579477cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5795e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 579677cb8546SJoe Perches } 579777cb8546SJoe Perches 57985e4f6ba5SJoe Perches# check for spaces before a quoted newline 57995e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 58005e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 58015e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 58025e4f6ba5SJoe Perches $fix) { 58035e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 58045e4f6ba5SJoe Perches } 58055e4f6ba5SJoe Perches 58065e4f6ba5SJoe Perches } 58075e4f6ba5SJoe Perches 5808f17dba4fSJoe Perches# concatenated string without spaces between elements 580979682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 581079682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 581179682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 581279682c0cSJoe Perches $fix) { 581379682c0cSJoe Perches while ($line =~ /($String)/g) { 581479682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 581579682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 581679682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 581779682c0cSJoe Perches } 581879682c0cSJoe Perches } 5819f17dba4fSJoe Perches } 5820f17dba4fSJoe Perches 582190ad30e5SJoe Perches# uncoalesced string fragments 582233acb54aSJoe Perches if ($line =~ /$String\s*"/) { 582379682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 582479682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 582579682c0cSJoe Perches $fix) { 582679682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 582779682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 582879682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 582979682c0cSJoe Perches } 583079682c0cSJoe Perches } 583190ad30e5SJoe Perches } 583290ad30e5SJoe Perches 5833522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5834522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5835522b837cSAlexey Dobriyan my $show_Z = 1; 58365e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5837522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 58385e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5839522b837cSAlexey Dobriyan # check for %L 5840522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 58415e4f6ba5SJoe Perches WARN("PRINTF_L", 5842522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5843522b837cSAlexey Dobriyan $show_L = 0; 58445e4f6ba5SJoe Perches } 5845522b837cSAlexey Dobriyan # check for %Z 5846522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5847522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5848522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5849522b837cSAlexey Dobriyan $show_Z = 0; 5850522b837cSAlexey Dobriyan } 5851522b837cSAlexey Dobriyan # check for 0x<decimal> 5852522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5853522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 58546e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 58556e300757SJoe Perches } 58565e4f6ba5SJoe Perches } 58575e4f6ba5SJoe Perches 58585e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 58593f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 58605e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 58615e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 58625e4f6ba5SJoe Perches } 58635e4f6ba5SJoe Perches 586400df344fSAndy Whitcroft# warn about #if 0 5865c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 586660f89010SPrakruthi Deepak Heragu WARN("IF_0", 586760f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 586860f89010SPrakruthi Deepak Heragu } 586960f89010SPrakruthi Deepak Heragu 587060f89010SPrakruthi Deepak Heragu# warn about #if 1 587160f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 587260f89010SPrakruthi Deepak Heragu WARN("IF_1", 587360f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 58744a0df2efSAndy Whitcroft } 58754a0df2efSAndy Whitcroft 587603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 587703df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5878100425deSJoe Perches my $tested = quotemeta($1); 5879100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5880100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5881100425deSJoe Perches my $func = $1; 5882100425deSJoe Perches if (WARN('NEEDLESS_IF', 5883100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5884100425deSJoe Perches $fix) { 5885100425deSJoe Perches my $do_fix = 1; 5886100425deSJoe Perches my $leading_tabs = ""; 5887100425deSJoe Perches my $new_leading_tabs = ""; 5888100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5889100425deSJoe Perches $leading_tabs = $1; 5890100425deSJoe Perches } else { 5891100425deSJoe Perches $do_fix = 0; 5892100425deSJoe Perches } 5893100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5894100425deSJoe Perches $new_leading_tabs = $1; 5895100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5896100425deSJoe Perches $do_fix = 0; 5897100425deSJoe Perches } 5898100425deSJoe Perches } else { 5899100425deSJoe Perches $do_fix = 0; 5900100425deSJoe Perches } 5901100425deSJoe Perches if ($do_fix) { 5902100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5903100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5904100425deSJoe Perches } 5905100425deSJoe Perches } 59064c432a8fSGreg Kroah-Hartman } 59074c432a8fSGreg Kroah-Hartman } 5908f0a594c1SAndy Whitcroft 5909ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5910ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5911ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5912ebfdc409SJoe Perches (defined $1 || defined $3) && 5913ebfdc409SJoe Perches $linenr > 3) { 5914ebfdc409SJoe Perches my $testval = $2; 5915ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5916ebfdc409SJoe Perches 5917ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5918ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5919ebfdc409SJoe Perches 5920e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 5921e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 5922ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5923ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5924ebfdc409SJoe Perches } 5925ebfdc409SJoe Perches } 5926ebfdc409SJoe Perches 5927f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5928dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5929f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5930f78d98f6SJoe Perches my $level = $1; 5931f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5932f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5933f78d98f6SJoe Perches $fix) { 5934f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5935f78d98f6SJoe Perches } 5936f78d98f6SJoe Perches } 5937f78d98f6SJoe Perches 593845c55e92SJoe Perches# check for logging continuations 593945c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 594045c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 594145c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 594245c55e92SJoe Perches } 594345c55e92SJoe Perches 5944abb08a53SJoe Perches# check for mask then right shift without a parentheses 59455b57980dSJoe Perches if ($perl_version_ok && 5946abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5947abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5948abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5949abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5950abb08a53SJoe Perches } 5951abb08a53SJoe Perches 5952b75ac618SJoe Perches# check for pointer comparisons to NULL 59535b57980dSJoe Perches if ($perl_version_ok) { 5954b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5955b75ac618SJoe Perches my $val = $1; 5956b75ac618SJoe Perches my $equal = "!"; 5957b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5958b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5959b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5960b75ac618SJoe Perches $fix) { 5961b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5962b75ac618SJoe Perches } 5963b75ac618SJoe Perches } 5964b75ac618SJoe Perches } 5965b75ac618SJoe Perches 59668716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 59678716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 59688716de38SJoe Perches my $attr = $1; 59698716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 59708716de38SJoe Perches my $ptr = $1; 59718716de38SJoe Perches my $var = $2; 59728716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 59738716de38SJoe Perches ERROR("MISPLACED_INIT", 59748716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 59758716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 59768716de38SJoe Perches WARN("MISPLACED_INIT", 59778716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 59788716de38SJoe Perches $fix) { 5979194f66fcSJoe 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; 59808716de38SJoe Perches } 59818716de38SJoe Perches } 59828716de38SJoe Perches } 59838716de38SJoe Perches 5984e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5985e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5986e970b884SJoe Perches my $attr = $1; 5987e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5988e970b884SJoe Perches my $attr_prefix = $1; 5989e970b884SJoe Perches my $attr_type = $2; 5990e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5991e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5992e970b884SJoe Perches $fix) { 5993194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5994e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5995e970b884SJoe Perches } 5996e970b884SJoe Perches } 5997e970b884SJoe Perches 5998e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5999e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 6000e970b884SJoe Perches my $attr = $1; 6001e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 6002e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 6003e970b884SJoe Perches $fix) { 6004194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 6005e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 6006e970b884SJoe Perches $lead = rtrim($1); 6007e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 6008e970b884SJoe Perches $lead = "${lead}const "; 6009194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 6010e970b884SJoe Perches } 6011e970b884SJoe Perches } 6012e970b884SJoe Perches 6013c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 6014c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 6015c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 6016c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 6017c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 6018c17893c7SJoe Perches $fix) { 6019c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 6020c17893c7SJoe Perches } 6021c17893c7SJoe Perches } 6022c17893c7SJoe Perches 6023fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 6024fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 6025fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 6026fbdb8138SJoe Perches my $constant_func = $1; 6027fbdb8138SJoe Perches my $func = $constant_func; 6028fbdb8138SJoe Perches $func =~ s/^__constant_//; 6029fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 6030fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 6031fbdb8138SJoe Perches $fix) { 6032194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 6033fbdb8138SJoe Perches } 6034fbdb8138SJoe Perches } 6035fbdb8138SJoe Perches 60361a15a250SPatrick Pannuto# prefer usleep_range over udelay 603737581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 603843c1d77cSJoe Perches my $delay = $1; 60391a15a250SPatrick Pannuto # ignore udelay's < 10, however 604043c1d77cSJoe Perches if (! ($delay < 10) ) { 6041000d1cc1SJoe Perches CHK("USLEEP_RANGE", 6042458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 604343c1d77cSJoe Perches } 604443c1d77cSJoe Perches if ($delay > 2000) { 604543c1d77cSJoe Perches WARN("LONG_UDELAY", 604643c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 60471a15a250SPatrick Pannuto } 60481a15a250SPatrick Pannuto } 60491a15a250SPatrick Pannuto 605009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 605109ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 605209ef8725SPatrick Pannuto if ($1 < 20) { 6053000d1cc1SJoe Perches WARN("MSLEEP", 6054458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 605509ef8725SPatrick Pannuto } 605609ef8725SPatrick Pannuto } 605709ef8725SPatrick Pannuto 605836ec1939SJoe Perches# check for comparisons of jiffies 605936ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 606036ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 606136ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 606236ec1939SJoe Perches } 606336ec1939SJoe Perches 60649d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 60659d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 60669d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 60679d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 60689d7a34a5SJoe Perches } 60699d7a34a5SJoe Perches 607000df344fSAndy Whitcroft# warn about #ifdefs in C files 6071c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 607200df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 607300df344fSAndy Whitcroft# print "$herecurr"; 607400df344fSAndy Whitcroft# $clean = 0; 607500df344fSAndy Whitcroft# } 607600df344fSAndy Whitcroft 607722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 6078c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 60793705ce5bSJoe Perches if (ERROR("SPACING", 60803705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 60813705ce5bSJoe Perches $fix) { 6082194f66fcSJoe Perches $fixed[$fixlinenr] =~ 60833705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 60843705ce5bSJoe Perches } 60853705ce5bSJoe Perches 608622f2a2efSAndy Whitcroft } 608722f2a2efSAndy Whitcroft 60884a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 6089171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 6090171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 60914a0df2efSAndy Whitcroft my $which = $1; 60924a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6093000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 6094000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 60954a0df2efSAndy Whitcroft } 60964a0df2efSAndy Whitcroft } 60974a0df2efSAndy Whitcroft# check for memory barriers without a comment. 6098402c2553SMichael S. Tsirkin 6099402c2553SMichael S. Tsirkin my $barriers = qr{ 6100402c2553SMichael S. Tsirkin mb| 6101402c2553SMichael S. Tsirkin rmb| 6102ad83ec6cSWill Deacon wmb 6103402c2553SMichael S. Tsirkin }x; 6104402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 6105402c2553SMichael S. Tsirkin mb__before_atomic| 6106402c2553SMichael S. Tsirkin mb__after_atomic| 6107402c2553SMichael S. Tsirkin store_release| 6108402c2553SMichael S. Tsirkin load_acquire| 6109402c2553SMichael S. Tsirkin store_mb| 6110402c2553SMichael S. Tsirkin (?:$barriers) 6111402c2553SMichael S. Tsirkin }x; 6112402c2553SMichael S. Tsirkin my $all_barriers = qr{ 6113402c2553SMichael S. Tsirkin (?:$barriers)| 611443e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 611543e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 6116402c2553SMichael S. Tsirkin }x; 6117402c2553SMichael S. Tsirkin 6118402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 61194a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6120c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 6121000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 61224a0df2efSAndy Whitcroft } 61234a0df2efSAndy Whitcroft } 61243ad81779SPaul E. McKenney 6125f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 6126f4073b0fSMichael S. Tsirkin 6127f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 6128f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 6129f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 6130f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 6131f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 6132f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 6133f4073b0fSMichael S. Tsirkin } 6134f4073b0fSMichael S. Tsirkin 6135cb426e99SJoe Perches# check for waitqueue_active without a comment. 6136cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 6137cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 6138cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 6139cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 6140cb426e99SJoe Perches } 6141cb426e99SJoe Perches } 61423ad81779SPaul E. McKenney 61435099a722SMarco Elver# check for data_race without a comment. 61445099a722SMarco Elver if ($line =~ /\bdata_race\s*\(/) { 61455099a722SMarco Elver if (!ctx_has_comment($first_line, $linenr)) { 61465099a722SMarco Elver WARN("DATA_RACE", 61475099a722SMarco Elver "data_race without comment\n" . $herecurr); 61485099a722SMarco Elver } 61495099a722SMarco Elver } 61505099a722SMarco Elver 61514a0df2efSAndy Whitcroft# check of hardware specific defines 6152c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 6153000d1cc1SJoe Perches CHK("ARCH_DEFINES", 6154000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 61550a920b5bSAndy Whitcroft } 6156653d4876SAndy Whitcroft 6157596ed45bSJoe Perches# check that the storage class is not after a type 6158596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 6159000d1cc1SJoe Perches WARN("STORAGE_CLASS", 6160596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 6161596ed45bSJoe Perches } 6162596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 6163596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 6164596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 6165596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 6166596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 6167596ed45bSJoe Perches WARN("STORAGE_CLASS", 6168596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 6169d4977c78STobias Klauser } 6170d4977c78STobias Klauser 6171de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 6172de7d4f0eSAndy Whitcroft# storage class and type. 61739c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 61749c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 6175000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 6176000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 6177de7d4f0eSAndy Whitcroft } 6178de7d4f0eSAndy Whitcroft 61798905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 61802b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61812b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 6182d5e616fcSJoe Perches if (WARN("INLINE", 6183d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 6184d5e616fcSJoe Perches $fix) { 6185194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 6186d5e616fcSJoe Perches 6187d5e616fcSJoe Perches } 61888905a67cSAndy Whitcroft } 61898905a67cSAndy Whitcroft 61903d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 61912b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61922b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 6193000d1cc1SJoe Perches WARN("PREFER_PACKED", 6194000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 61953d130fd0SJoe Perches } 61963d130fd0SJoe Perches 619739b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 61982b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61992b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 6200000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 6201000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 620239b7e287SJoe Perches } 620339b7e287SJoe Perches 6204462811d9SJoe Perches# Check for __attribute__ section, prefer __section 6205462811d9SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6206462811d9SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { 6207462811d9SJoe Perches my $old = substr($rawline, $-[1], $+[1] - $-[1]); 6208462811d9SJoe Perches my $new = substr($old, 1, -1); 6209462811d9SJoe Perches if (WARN("PREFER_SECTION", 6210462811d9SJoe Perches "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && 6211462811d9SJoe Perches $fix) { 6212462811d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; 6213462811d9SJoe Perches } 6214462811d9SJoe Perches } 6215462811d9SJoe Perches 62165f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 62172b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 62182b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 6219d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 6220d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 6221d5e616fcSJoe Perches $fix) { 6222194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 6223d5e616fcSJoe Perches 6224d5e616fcSJoe Perches } 62255f14d3bdSJoe Perches } 62265f14d3bdSJoe Perches 62276061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 62282b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 62292b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 6230d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 6231d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 6232d5e616fcSJoe Perches $fix) { 6233194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 6234d5e616fcSJoe Perches } 62356061d949SJoe Perches } 62366061d949SJoe Perches 6237619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 62385b57980dSJoe Perches if ($perl_version_ok && 6239619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 6240619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6241619a908aSJoe Perches $line =~ /\b__weak\b/)) { 6242619a908aSJoe Perches ERROR("WEAK_DECLARATION", 6243619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 6244619a908aSJoe Perches } 6245619a908aSJoe Perches 6246fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 6247e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6248fd39f904STomas Winkler $realfile !~ m@\btools/@ && 6249e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6250e6176fa4SJoe Perches my $type = $1; 6251e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 6252e6176fa4SJoe Perches $type = $1; 6253e6176fa4SJoe Perches my $kernel_type = 'u'; 6254e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 6255e6176fa4SJoe Perches $type =~ /(\d+)/; 6256e6176fa4SJoe Perches $kernel_type .= $1; 6257e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 6258e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6259e6176fa4SJoe Perches $fix) { 6260e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6261e6176fa4SJoe Perches } 6262e6176fa4SJoe Perches } 6263e6176fa4SJoe Perches } 6264e6176fa4SJoe Perches 6265938224b5SJoe Perches# check for cast of C90 native int or longer types constants 6266938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6267938224b5SJoe Perches my $cast = $1; 6268938224b5SJoe Perches my $const = $2; 6269938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 6270938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 6271938224b5SJoe Perches $fix) { 6272938224b5SJoe Perches my $suffix = ""; 6273938224b5SJoe Perches my $newconst = $const; 6274938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 6275938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6276938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6277938224b5SJoe Perches $suffix .= 'LL'; 6278938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6279938224b5SJoe Perches $suffix .= 'L'; 6280938224b5SJoe Perches } 6281938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6282938224b5SJoe Perches } 6283938224b5SJoe Perches } 6284938224b5SJoe Perches 62858f53a9b8SJoe Perches# check for sizeof(&) 62868f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6287000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6288000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 62898f53a9b8SJoe Perches } 62908f53a9b8SJoe Perches 629166c80b60SJoe Perches# check for sizeof without parenthesis 629266c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6293d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6294d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6295d5e616fcSJoe Perches $fix) { 6296194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6297d5e616fcSJoe Perches } 629866c80b60SJoe Perches } 629966c80b60SJoe Perches 630088982feaSJoe Perches# check for struct spinlock declarations 630188982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 630288982feaSJoe Perches WARN("USE_SPINLOCK_T", 630388982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 630488982feaSJoe Perches } 630588982feaSJoe Perches 6306a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 630706668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6308a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6309caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6310caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6311d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6312d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6313d5e616fcSJoe Perches $fix) { 6314194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6315d5e616fcSJoe Perches } 6316a6962d72SJoe Perches } 6317a6962d72SJoe Perches } 6318a6962d72SJoe Perches 63190b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 63205b57980dSJoe Perches if ($perl_version_ok && 63210b523769SJoe Perches defined $stat && 63220b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 63230b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6324e3c6bc95STobin C. Harding my $stat_real; 6325e3c6bc95STobin C. Harding 63260b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 63270b523769SJoe Perches $lc = $lc + $linenr; 63280b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6329ffe07513SJoe Perches my $specifier; 6330ffe07513SJoe Perches my $extension; 63313bd32d6aSSakari Ailus my $qualifier; 6332ffe07513SJoe Perches my $bad_specifier = ""; 63330b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 63340b523769SJoe Perches $fmt =~ s/%%//g; 6335e3c6bc95STobin C. Harding 63363bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6337e3c6bc95STobin C. Harding $specifier = $1; 6338e3c6bc95STobin C. Harding $extension = $2; 63393bd32d6aSSakari Ailus $qualifier = $3; 6340361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 63413bd32d6aSSakari Ailus ($extension eq "f" && 63423bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6343e3c6bc95STobin C. Harding $bad_specifier = $specifier; 63440b523769SJoe Perches last; 63450b523769SJoe Perches } 6346e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6347e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6348e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 63490b523769SJoe Perches } 6350e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6351e3c6bc95STobin 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"); 6352e3c6bc95STobin C. Harding } 6353e3c6bc95STobin C. Harding } 6354e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 63552a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 63561df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 63571df7338aSSergey Senozhatsky my $use = ""; 6358e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 63591df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6360e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 63611df7338aSSergey Senozhatsky } 63622a9f9d85STobin C. Harding 63630b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6364e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6365e3c6bc95STobin C. Harding } 63660b523769SJoe Perches } 63670b523769SJoe Perches } 63680b523769SJoe Perches 6369554e165cSAndy Whitcroft# Check for misused memsets 63705b57980dSJoe Perches if ($perl_version_ok && 6371d1fe9c09SJoe Perches defined $stat && 63729e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6373554e165cSAndy Whitcroft 6374d7c76ba7SJoe Perches my $ms_addr = $2; 6375d1fe9c09SJoe Perches my $ms_val = $7; 6376d1fe9c09SJoe Perches my $ms_size = $12; 6377d7c76ba7SJoe Perches 6378554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6379554e165cSAndy Whitcroft ERROR("MEMSET", 6380d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6381554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6382554e165cSAndy Whitcroft WARN("MEMSET", 6383d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6384d7c76ba7SJoe Perches } 6385d7c76ba7SJoe Perches } 6386d7c76ba7SJoe Perches 638798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 63885b57980dSJoe Perches# if ($perl_version_ok && 6389f333195dSJoe Perches# defined $stat && 6390f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6391f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6392f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6393f333195dSJoe Perches# $fix) { 6394f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6395f333195dSJoe Perches# } 6396f333195dSJoe Perches# } 639798a9bba5SJoe Perches 6398b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 63995b57980dSJoe Perches# if ($perl_version_ok && 6400f333195dSJoe Perches# defined $stat && 6401f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6402f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6403f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6404f333195dSJoe Perches# } 6405b6117d17SMateusz Kulikowski 64068617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 64078617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 64085b57980dSJoe Perches# if ($perl_version_ok && 6409f333195dSJoe Perches# defined $stat && 6410f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6411f333195dSJoe Perches# 6412f333195dSJoe Perches# my $ms_val = $7; 6413f333195dSJoe Perches# 6414f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6415f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6416f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6417f333195dSJoe Perches# $fix) { 6418f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6419f333195dSJoe Perches# } 6420f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6421f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6422f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6423f333195dSJoe Perches# $fix) { 6424f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6425f333195dSJoe Perches# } 6426f333195dSJoe Perches# } 6427f333195dSJoe Perches# } 64288617cd09SMateusz Kulikowski 6429d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 64305b57980dSJoe Perches if ($perl_version_ok && 6431d1fe9c09SJoe Perches defined $stat && 6432d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6433d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6434d7c76ba7SJoe Perches my $call = $1; 6435d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6436d7c76ba7SJoe Perches my $arg1 = $3; 6437d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6438d1fe9c09SJoe Perches my $arg2 = $8; 6439d7c76ba7SJoe Perches my $cast; 6440d7c76ba7SJoe Perches 6441d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6442d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6443d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6444d7c76ba7SJoe Perches $cast = $cast1; 6445d7c76ba7SJoe Perches } else { 6446d7c76ba7SJoe Perches $cast = $cast2; 6447d7c76ba7SJoe Perches } 6448d7c76ba7SJoe Perches WARN("MINMAX", 6449d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6450554e165cSAndy Whitcroft } 6451554e165cSAndy Whitcroft } 6452554e165cSAndy Whitcroft 64534a273195SJoe Perches# check usleep_range arguments 64545b57980dSJoe Perches if ($perl_version_ok && 64554a273195SJoe Perches defined $stat && 64564a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 64574a273195SJoe Perches my $min = $1; 64584a273195SJoe Perches my $max = $7; 64594a273195SJoe Perches if ($min eq $max) { 64604a273195SJoe Perches WARN("USLEEP_RANGE", 6461458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 64624a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 64634a273195SJoe Perches $min > $max) { 64644a273195SJoe Perches WARN("USLEEP_RANGE", 6465458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 64664a273195SJoe Perches } 64674a273195SJoe Perches } 64684a273195SJoe Perches 6469823b794cSJoe Perches# check for naked sscanf 64705b57980dSJoe Perches if ($perl_version_ok && 6471823b794cSJoe Perches defined $stat && 64726c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6473823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6474823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6475823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6476823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6477823b794cSJoe Perches $lc = $lc + $linenr; 64782a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6479823b794cSJoe Perches WARN("NAKED_SSCANF", 6480823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6481823b794cSJoe Perches } 6482823b794cSJoe Perches 6483afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 64845b57980dSJoe Perches if ($perl_version_ok && 6485afc819abSJoe Perches defined $stat && 6486afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6487afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6488afc819abSJoe Perches $lc = $lc + $linenr; 64892a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6490afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6491afc819abSJoe Perches my $format = $6; 6492afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6493afc819abSJoe Perches if ($count == 1 && 6494afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6495afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6496afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6497afc819abSJoe Perches } 6498afc819abSJoe Perches } 6499afc819abSJoe Perches } 6500afc819abSJoe Perches 650170dc8a48SJoe Perches# check for new externs in .h files. 650270dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 650370dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6504d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 650570dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 650670dc8a48SJoe Perches $fix) { 6507194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 650870dc8a48SJoe Perches } 650970dc8a48SJoe Perches } 651070dc8a48SJoe Perches 6511de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6512171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6513c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6514171ae1a4SAndy Whitcroft { 6515c45dcabdSAndy Whitcroft my $function_name = $1; 6516c45dcabdSAndy Whitcroft my $paren_space = $2; 6517171ae1a4SAndy Whitcroft 6518171ae1a4SAndy Whitcroft my $s = $stat; 6519171ae1a4SAndy Whitcroft if (defined $cond) { 6520171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6521171ae1a4SAndy Whitcroft } 6522d8b44b58SKees Cook if ($s =~ /^\s*;/) 6523c45dcabdSAndy Whitcroft { 6524000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6525000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6526de7d4f0eSAndy Whitcroft } 6527de7d4f0eSAndy Whitcroft 6528171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6529000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6530000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6531171ae1a4SAndy Whitcroft } 65329c9ba34eSAndy Whitcroft 65339c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 65349c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 65359c9ba34eSAndy Whitcroft { 6536000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6537000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6538171ae1a4SAndy Whitcroft } 6539171ae1a4SAndy Whitcroft 6540a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6541a0ad7596SJoe Perches if (defined $stat && 6542d8b44b58SKees Cook $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6543d8b44b58SKees Cook $1 ne "void") { 6544d8b44b58SKees Cook my $args = trim($1); 6545ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6546ca0d8929SJoe Perches my $arg = trim($1); 6547d8b44b58SKees Cook if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6548ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6549ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6550ca0d8929SJoe Perches } 6551ca0d8929SJoe Perches } 6552ca0d8929SJoe Perches } 6553ca0d8929SJoe Perches 6554a0ad7596SJoe Perches# check for function definitions 65555b57980dSJoe Perches if ($perl_version_ok && 6556a0ad7596SJoe Perches defined $stat && 6557a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6558a0ad7596SJoe Perches $context_function = $1; 6559a0ad7596SJoe Perches 6560a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6561a0ad7596SJoe Perches my $ok = 0; 6562a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6563a0ad7596SJoe Perches my $herectx = $here . "\n"; 6564a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6565a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6566a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6567a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6568a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6569a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6570a0ad7596SJoe Perches } 6571a0ad7596SJoe Perches if (!$ok) { 6572a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6573a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6574a0ad7596SJoe Perches } 6575a0ad7596SJoe Perches } 6576a0ad7596SJoe Perches 6577de7d4f0eSAndy Whitcroft# checks for new __setup's 6578de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6579de7d4f0eSAndy Whitcroft my $name = $1; 6580de7d4f0eSAndy Whitcroft 6581de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6582000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 65832581ac7cSTim Froidcoeur "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); 6584de7d4f0eSAndy Whitcroft } 6585653d4876SAndy Whitcroft } 65869c0ca6f9SAndy Whitcroft 6587e29a70f1SJoe Perches# check for pointless casting of alloc functions 6588e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6589000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6590000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 65919c0ca6f9SAndy Whitcroft } 659213214adfSAndy Whitcroft 6593a640d25cSJoe Perches# alloc style 6594a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 65955b57980dSJoe Perches if ($perl_version_ok && 6596e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6597a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6598a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6599a640d25cSJoe Perches } 6600a640d25cSJoe Perches 660160a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 66025b57980dSJoe Perches if ($perl_version_ok && 66031b4a2ed4SJoe Perches defined $stat && 66041b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 660560a55369SJoe Perches my $oldfunc = $3; 660660a55369SJoe Perches my $a1 = $4; 660760a55369SJoe Perches my $a2 = $10; 660860a55369SJoe Perches my $newfunc = "kmalloc_array"; 660960a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 661060a55369SJoe Perches my $r1 = $a1; 661160a55369SJoe Perches my $r2 = $a2; 661260a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 661360a55369SJoe Perches $r1 = $a2; 661460a55369SJoe Perches $r2 = $a1; 661560a55369SJoe Perches } 6616e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6617e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 66181b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6619e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6620e3d95a2aSTobin C. Harding 6621e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 66221b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 66231b4a2ed4SJoe Perches $cnt == 1 && 6624e367455aSJoe Perches $fix) { 6625194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; 662660a55369SJoe Perches } 662760a55369SJoe Perches } 662860a55369SJoe Perches } 662960a55369SJoe Perches 6630972fdea2SJoe Perches# check for krealloc arg reuse 66315b57980dSJoe Perches if ($perl_version_ok && 66324cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 66334cab63ceSJoe Perches $1 eq $3) { 6634972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6635972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6636972fdea2SJoe Perches } 6637972fdea2SJoe Perches 66385ce59ae0SJoe Perches# check for alloc argument mismatch 66395ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 66405ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 66415ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 66425ce59ae0SJoe Perches } 66435ce59ae0SJoe Perches 6644caf2a54fSJoe Perches# check for multiple semicolons 6645caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6646d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6647d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6648d5e616fcSJoe Perches $fix) { 6649194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6650d5e616fcSJoe Perches } 6651d1e2ad07SJoe Perches } 6652d1e2ad07SJoe Perches 6653cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6654cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6655cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 66560ab90191SJoe Perches my $ull = ""; 66570ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 66580ab90191SJoe Perches if (CHK("BIT_MACRO", 66590ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 66600ab90191SJoe Perches $fix) { 66610ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 66620ab90191SJoe Perches } 66630ab90191SJoe Perches } 66640ab90191SJoe Perches 666550161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) 66663e89ad85SJerome Forissier if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { 666750161266SJoe Perches WARN("IS_ENABLED_CONFIG", 66683e89ad85SJerome Forissier "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); 666950161266SJoe Perches } 667050161266SJoe Perches 66712d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 66723e89ad85SJerome 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*$/) { 66732d632745SJoe Perches my $config = $1; 66742d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 66753e89ad85SJerome Forissier "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && 66762d632745SJoe Perches $fix) { 66772d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 66782d632745SJoe Perches } 66792d632745SJoe Perches } 66802d632745SJoe Perches 6681f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 6682f36d3eb8SJoe Perches my @fallthroughs = ( 6683f36d3eb8SJoe Perches 'fallthrough', 6684f36d3eb8SJoe Perches '@fallthrough@', 6685f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 6686f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 6687f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 6688f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6689f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6690f36d3eb8SJoe Perches ); 6691f36d3eb8SJoe Perches if ($raw_comment ne '') { 6692f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 6693f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 6694f36d3eb8SJoe Perches my $msg_level = \&WARN; 6695f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 6696f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 6697f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 6698f36d3eb8SJoe Perches last; 6699f36d3eb8SJoe Perches } 6700f36d3eb8SJoe Perches } 6701f36d3eb8SJoe Perches } 6702f36d3eb8SJoe Perches 6703d1e2ad07SJoe Perches# check for switch/default statements without a break; 67045b57980dSJoe Perches if ($perl_version_ok && 6705d1e2ad07SJoe Perches defined $stat && 6706d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6707d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6708e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6709e3d95a2aSTobin C. Harding 6710d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6711d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6712caf2a54fSJoe Perches } 6713caf2a54fSJoe Perches 671413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6715d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6716d5e616fcSJoe Perches if (WARN("USE_FUNC", 6717d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6718d5e616fcSJoe Perches $fix) { 6719194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6720d5e616fcSJoe Perches } 672113214adfSAndy Whitcroft } 6722773647a0SAndy Whitcroft 672362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 672462ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 672562ec818fSJoe Perches ERROR("DATE_TIME", 672662ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 672762ec818fSJoe Perches } 672862ec818fSJoe Perches 67292c92488aSJoe Perches# check for use of yield() 67302c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 67312c92488aSJoe Perches WARN("YIELD", 67322c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 67332c92488aSJoe Perches } 67342c92488aSJoe Perches 6735179f8f40SJoe Perches# check for comparisons against true and false 6736179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6737179f8f40SJoe Perches my $lead = $1; 6738179f8f40SJoe Perches my $arg = $2; 6739179f8f40SJoe Perches my $test = $3; 6740179f8f40SJoe Perches my $otype = $4; 6741179f8f40SJoe Perches my $trail = $5; 6742179f8f40SJoe Perches my $op = "!"; 6743179f8f40SJoe Perches 6744179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6745179f8f40SJoe Perches 6746179f8f40SJoe Perches my $type = lc($otype); 6747179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6748179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6749179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6750179f8f40SJoe Perches $op = ""; 6751179f8f40SJoe Perches } 6752179f8f40SJoe Perches 6753179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6754179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6755179f8f40SJoe Perches 6756179f8f40SJoe Perches## maybe suggesting a correct construct would better 6757179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6758179f8f40SJoe Perches 6759179f8f40SJoe Perches } 6760179f8f40SJoe Perches } 6761179f8f40SJoe Perches 67624882720bSThomas Gleixner# check for semaphores initialized locked 67634882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6764000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6765000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6766773647a0SAndy Whitcroft } 67676712d858SJoe Perches 676867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 676967d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6770000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 677167d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6772773647a0SAndy Whitcroft } 67736712d858SJoe Perches 6774ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6775f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6776000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6777ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6778f3db6639SMichael Ellerman } 67796712d858SJoe Perches 67803d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 67813d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 67823d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 67833d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 67843d709ab5SPaul E. McKenney } 67853d709ab5SPaul E. McKenney 67869189c7e7SJoe Perches# check for deprecated apis 67879189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 67889189c7e7SJoe Perches my $deprecated_api = $1; 67899189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 67909189c7e7SJoe Perches WARN("DEPRECATED_API", 67919189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 67929189c7e7SJoe Perches } 67939189c7e7SJoe Perches 67940f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6795d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 6796ced69da1SQuentin Monnet if (defined($const_structs) && 6797ced69da1SQuentin Monnet $line !~ /\bconst\b/ && 6798d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6799000d1cc1SJoe Perches WARN("CONST_STRUCT", 6800d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 68012b6db5cbSAndy Whitcroft } 6802773647a0SAndy Whitcroft 6803773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6804773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6805773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6806c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6807c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6808171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6809171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6810171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6811773647a0SAndy Whitcroft { 6812000d1cc1SJoe Perches WARN("NR_CPUS", 6813000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6814773647a0SAndy Whitcroft } 68159c9ba34eSAndy Whitcroft 681652ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 681752ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 681852ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 681952ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 682052ea8506SJoe Perches } 682152ea8506SJoe Perches 6822acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 68235b57980dSJoe Perches if ($perl_version_ok && 6824acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6825acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6826acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6827acd9362cSJoe Perches } 6828acd9362cSJoe Perches 6829de3f186fSDenis Efremov# nested likely/unlikely calls 6830de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 6831de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 6832de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 6833de3f186fSDenis Efremov } 6834de3f186fSDenis Efremov 6835691d77b6SAndy Whitcroft# whine mightly about in_atomic 6836691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6837691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6838000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6839000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6840f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6841000d1cc1SJoe Perches WARN("IN_ATOMIC", 6842000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6843691d77b6SAndy Whitcroft } 6844691d77b6SAndy Whitcroft } 68451704f47bSPeter Zijlstra 68460f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 68470f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 68480f5225b0SPeter Zijlstra ERROR("LOCKING", 68490f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 68500f5225b0SPeter Zijlstra } 68510f5225b0SPeter Zijlstra 68521704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 68531704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 68541704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 68551704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 68561704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 68571704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6858000d1cc1SJoe Perches ERROR("LOCKDEP", 6859000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 68601704f47bSPeter Zijlstra } 68611704f47bSPeter Zijlstra } 686288f8831cSDave Jones 6863b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6864b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6865000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6866000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 686788f8831cSDave Jones } 68682435880fSJoe Perches 686900180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 687000180468SJoe Perches# and whether or not function naming is typical and if 687100180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 68725b57980dSJoe Perches if ($perl_version_ok && 687300180468SJoe Perches defined $stat && 687400180468SJoe 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*\)/) { 687500180468SJoe Perches my $var = $1; 687600180468SJoe Perches my $perms = $2; 687700180468SJoe Perches my $show = $3; 687800180468SJoe Perches my $store = $4; 687900180468SJoe Perches my $octal_perms = perms_to_octal($perms); 688000180468SJoe Perches if ($show =~ /^${var}_show$/ && 688100180468SJoe Perches $store =~ /^${var}_store$/ && 688200180468SJoe Perches $octal_perms eq "0644") { 688300180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 688400180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 688500180468SJoe Perches $fix) { 688600180468SJoe 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})/; 688700180468SJoe Perches } 688800180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 688900180468SJoe Perches $store =~ /^NULL$/ && 689000180468SJoe Perches $octal_perms eq "0444") { 689100180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 689200180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 689300180468SJoe Perches $fix) { 689400180468SJoe 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})/; 689500180468SJoe Perches } 689600180468SJoe Perches } elsif ($show =~ /^NULL$/ && 689700180468SJoe Perches $store =~ /^${var}_store$/ && 689800180468SJoe Perches $octal_perms eq "0200") { 689900180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 690000180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 690100180468SJoe Perches $fix) { 690200180468SJoe 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})/; 690300180468SJoe Perches } 690400180468SJoe Perches } elsif ($octal_perms eq "0644" || 690500180468SJoe Perches $octal_perms eq "0444" || 690600180468SJoe Perches $octal_perms eq "0200") { 690700180468SJoe Perches my $newshow = "$show"; 690800180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 690900180468SJoe Perches my $newstore = $store; 691000180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 691100180468SJoe Perches my $rename = ""; 691200180468SJoe Perches if ($show ne $newshow) { 691300180468SJoe Perches $rename .= " '$show' to '$newshow'"; 691400180468SJoe Perches } 691500180468SJoe Perches if ($store ne $newstore) { 691600180468SJoe Perches $rename .= " '$store' to '$newstore'"; 691700180468SJoe Perches } 691800180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 691900180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 692000180468SJoe Perches } else { 692100180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 692200180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 692300180468SJoe Perches } 692400180468SJoe Perches } 692500180468SJoe Perches 6926515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6927515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 692873121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 692973121534SJoe Perches# specific definition of not visible in sysfs. 693073121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 693173121534SJoe Perches# use the default permissions 69325b57980dSJoe Perches if ($perl_version_ok && 6933459cf0aeSJoe Perches defined $stat && 6934515a235eSJoe Perches $line =~ /$mode_perms_search/) { 69352435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 69362435880fSJoe Perches my $func = $entry->[0]; 69372435880fSJoe Perches my $arg_pos = $entry->[1]; 69382435880fSJoe Perches 6939459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6940459cf0aeSJoe Perches $lc = $lc + $linenr; 69412a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6942459cf0aeSJoe Perches 69432435880fSJoe Perches my $skip_args = ""; 69442435880fSJoe Perches if ($arg_pos > 1) { 69452435880fSJoe Perches $arg_pos--; 69462435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 69472435880fSJoe Perches } 6948f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6949459cf0aeSJoe Perches if ($stat =~ /$test/) { 69502435880fSJoe Perches my $val = $1; 69512435880fSJoe Perches $val = $6 if ($skip_args ne ""); 695273121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 695373121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 695473121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 69552435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6956459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6957f90774e1SJoe Perches } 6958f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6959c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6960459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 69612435880fSJoe Perches } 6962459cf0aeSJoe Perches } 6963459cf0aeSJoe Perches } 6964459cf0aeSJoe Perches } 6965459cf0aeSJoe Perches 6966459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6967bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 696800180468SJoe Perches my $oval = $1; 696900180468SJoe Perches my $octal = perms_to_octal($oval); 6970f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6971459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6972f90774e1SJoe Perches $fix) { 697300180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 69742435880fSJoe Perches } 697513214adfSAndy Whitcroft } 69765a6d20ceSBjorn Andersson 69775a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 69785a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 69795a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 69805a6d20ceSBjorn Andersson my $valid_licenses = qr{ 69815a6d20ceSBjorn Andersson GPL| 69825a6d20ceSBjorn Andersson GPL\ v2| 69835a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 69845a6d20ceSBjorn Andersson Dual\ BSD/GPL| 69855a6d20ceSBjorn Andersson Dual\ MIT/GPL| 69865a6d20ceSBjorn Andersson Dual\ MPL/GPL| 69875a6d20ceSBjorn Andersson Proprietary 69885a6d20ceSBjorn Andersson }x; 69895a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 69905a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 69915a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 69925a6d20ceSBjorn Andersson } 69935a6d20ceSBjorn Andersson } 69946a8d76cbSMatteo Croce 69956a8d76cbSMatteo Croce# check for sysctl duplicate constants 69966a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 69976a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 69986a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 69996a8d76cbSMatteo Croce } 7000515a235eSJoe Perches } 700113214adfSAndy Whitcroft 700213214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 700313214adfSAndy Whitcroft # so just keep quiet. 700413214adfSAndy Whitcroft if ($#rawlines == -1) { 700513214adfSAndy Whitcroft exit(0); 70060a920b5bSAndy Whitcroft } 70070a920b5bSAndy Whitcroft 70088905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 70098905a67cSAndy Whitcroft # things that appear to be patches. 70108905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 70118905a67cSAndy Whitcroft exit(0); 70128905a67cSAndy Whitcroft } 70138905a67cSAndy Whitcroft 70148905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 70158905a67cSAndy Whitcroft # just keep quiet. 70168905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 70178905a67cSAndy Whitcroft exit(0); 70188905a67cSAndy Whitcroft } 70198905a67cSAndy Whitcroft 7020a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 7021000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 7022000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 70230a920b5bSAndy Whitcroft } 7024cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 7025cd261496SGeert Uytterhoeven if ($signoff == 0) { 7026000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 7027000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 702848ca2d8aSDwaipayan Ray } elsif ($authorsignoff != 1) { 702948ca2d8aSDwaipayan Ray # authorsignoff values: 703048ca2d8aSDwaipayan Ray # 0 -> missing sign off 703148ca2d8aSDwaipayan Ray # 1 -> sign off identical 703248ca2d8aSDwaipayan Ray # 2 -> names and addresses match, comments mismatch 703348ca2d8aSDwaipayan Ray # 3 -> addresses match, names different 703448ca2d8aSDwaipayan Ray # 4 -> names match, addresses different 703548ca2d8aSDwaipayan Ray # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match 703648ca2d8aSDwaipayan Ray 703748ca2d8aSDwaipayan Ray my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; 703848ca2d8aSDwaipayan Ray 703948ca2d8aSDwaipayan Ray if ($authorsignoff == 0) { 704048ca2d8aSDwaipayan Ray ERROR("NO_AUTHOR_SIGN_OFF", 7041cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 704248ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 2) { 704348ca2d8aSDwaipayan Ray CHK("FROM_SIGN_OFF_MISMATCH", 704448ca2d8aSDwaipayan Ray "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); 704548ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 3) { 704648ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 704748ca2d8aSDwaipayan Ray "From:/Signed-off-by: email name mismatch: $sob_msg\n"); 704848ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 4) { 704948ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 705048ca2d8aSDwaipayan Ray "From:/Signed-off-by: email address mismatch: $sob_msg\n"); 705148ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 5) { 705248ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 705348ca2d8aSDwaipayan Ray "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); 705448ca2d8aSDwaipayan Ray } 7055cd261496SGeert Uytterhoeven } 70560a920b5bSAndy Whitcroft } 70570a920b5bSAndy Whitcroft 7058f0a594c1SAndy Whitcroft print report_dump(); 705913214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 706013214adfSAndy Whitcroft print "$filename " if ($summary_file); 70616c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 70626c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 70636c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 70646c72ffaaSAndy Whitcroft } 70658905a67cSAndy Whitcroft 7066d2c0a235SAndy Whitcroft if ($quiet == 0) { 7067ef212196SJoe Perches # If there were any defects found and not already fixing them 7068ef212196SJoe Perches if (!$clean and !$fix) { 7069ef212196SJoe Perches print << "EOM" 7070ef212196SJoe Perches 7071ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 7072ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 7073ef212196SJoe PerchesEOM 7074ef212196SJoe Perches } 7075d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 7076d2c0a235SAndy Whitcroft # then suggest that. 7077d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 7078b0781216SMike Frysinger $rpt_cleaners = 0; 7079d8469f16SJoe Perches print << "EOM" 7080d8469f16SJoe Perches 7081d8469f16SJoe PerchesNOTE: Whitespace errors detected. 7082d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 7083d8469f16SJoe PerchesEOM 7084d2c0a235SAndy Whitcroft } 7085d2c0a235SAndy Whitcroft } 7086d2c0a235SAndy Whitcroft 7087d752fcc8SJoe Perches if ($clean == 0 && $fix && 7088d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 7089d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 70909624b8d6SJoe Perches my $newfile = $filename; 70919624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 70923705ce5bSJoe Perches my $linecount = 0; 70933705ce5bSJoe Perches my $f; 70943705ce5bSJoe Perches 7095d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 7096d752fcc8SJoe Perches 70973705ce5bSJoe Perches open($f, '>', $newfile) 70983705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 70993705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 71003705ce5bSJoe Perches $linecount++; 71013705ce5bSJoe Perches if ($file) { 71023705ce5bSJoe Perches if ($linecount > 3) { 71033705ce5bSJoe Perches $fixed_line =~ s/^\+//; 71043705ce5bSJoe Perches print $f $fixed_line . "\n"; 71053705ce5bSJoe Perches } 71063705ce5bSJoe Perches } else { 71073705ce5bSJoe Perches print $f $fixed_line . "\n"; 71083705ce5bSJoe Perches } 71093705ce5bSJoe Perches } 71103705ce5bSJoe Perches close($f); 71113705ce5bSJoe Perches 71123705ce5bSJoe Perches if (!$quiet) { 71133705ce5bSJoe Perches print << "EOM"; 7114d8469f16SJoe Perches 71153705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 71163705ce5bSJoe Perches 71173705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 71183705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 71193705ce5bSJoe Perches 71203705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 71213705ce5bSJoe PerchesNo warranties, expressed or implied... 71223705ce5bSJoe PerchesEOM 71233705ce5bSJoe Perches } 71243705ce5bSJoe Perches } 71253705ce5bSJoe Perches 7126d8469f16SJoe Perches if ($quiet == 0) { 7127d8469f16SJoe Perches print "\n"; 7128d8469f16SJoe Perches if ($clean == 1) { 7129d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 7130d8469f16SJoe Perches } else { 7131d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 71320a920b5bSAndy Whitcroft } 71330a920b5bSAndy Whitcroft } 71340a920b5bSAndy Whitcroft return $clean; 71350a920b5bSAndy Whitcroft} 7136