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| 385b5e8736aSJoe Perches volatile| 38603f1df7dSJoe Perches __percpu| 38703f1df7dSJoe Perches __nocast| 38803f1df7dSJoe Perches __safe| 38946d832f5SMichael S. Tsirkin __bitwise| 39003f1df7dSJoe Perches __packed__| 39103f1df7dSJoe Perches __packed2__| 39203f1df7dSJoe Perches __naked| 39303f1df7dSJoe Perches __maybe_unused| 39403f1df7dSJoe Perches __always_unused| 39503f1df7dSJoe Perches __noreturn| 39603f1df7dSJoe Perches __used| 39703f1df7dSJoe Perches __cold| 398e23ef1f3SJoe Perches __pure| 39903f1df7dSJoe Perches __noclone| 40003f1df7dSJoe Perches __deprecated| 4016c72ffaaSAndy Whitcroft __read_mostly| 402c5967e98SJoe Perches __ro_after_init| 4036c72ffaaSAndy Whitcroft __kprobes| 4048716de38SJoe Perches $InitAttribute| 40524e1d81aSAndy Whitcroft ____cacheline_aligned| 40624e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 4075fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 4085fe3af11SAndy Whitcroft __weak 4096c72ffaaSAndy Whitcroft }x; 410c45dcabdSAndy Whitcroftour $Modifier; 41191cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 4126c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 4136c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 4146c72ffaaSAndy Whitcroft 41595e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 41695e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 41795e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 41895e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4192435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 420c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 421326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 422326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 423326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 42474349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4252435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 426326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 427447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 42823f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4296c72ffaaSAndy Whitcroftour $Operators = qr{ 4306c72ffaaSAndy Whitcroft <=|>=|==|!=| 4316c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 43223f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4336c72ffaaSAndy Whitcroft }x; 4346c72ffaaSAndy Whitcroft 43591cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 43691cb5195SJoe Perches 437ab7e23f3SJoe Perchesour $BasicType; 4388905a67cSAndy Whitcroftour $NonptrType; 4391813087dSJoe Perchesour $NonptrTypeMisordered; 4408716de38SJoe Perchesour $NonptrTypeWithAttr; 4418905a67cSAndy Whitcroftour $Type; 4421813087dSJoe Perchesour $TypeMisordered; 4438905a67cSAndy Whitcroftour $Declare; 4441813087dSJoe Perchesour $DeclareMisordered; 4458905a67cSAndy Whitcroft 44615662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 44715662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 448171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 449171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 450171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 451171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 452171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 453171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 454171ae1a4SAndy Whitcroft}x; 455171ae1a4SAndy Whitcroft 45615662b3eSJoe Perchesour $UTF8 = qr{ 45715662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 45815662b3eSJoe Perches | $NON_ASCII_UTF8 45915662b3eSJoe Perches}x; 46015662b3eSJoe Perches 461e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 462021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 463021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 464021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 465021158b4SJoe Perches)}; 466e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 467fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4688ed22cadSAndy Whitcroft atomic_t 4698ed22cadSAndy Whitcroft)}; 470e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 471e6176fa4SJoe Perches $typeC99Typedefs\b| 472e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 473e6176fa4SJoe Perches $typeKernelTypedefs\b 474e6176fa4SJoe Perches)}; 4758ed22cadSAndy Whitcroft 4766d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4776d32f7a3SJoe Perches 478691e669bSJoe Perchesour $logFunctions = qr{(?x: 479758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4807d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 48187bd499aSJoe Perches TP_printk| 4826e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 483b0531722SJoe Perches panic| 48406668727SJoe Perches MODULE_[A-Z_]+| 48506668727SJoe Perches seq_vprintf|seq_printf|seq_puts 486691e669bSJoe Perches)}; 487691e669bSJoe Perches 488e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 489e29a70f1SJoe Perches (?:(?:devm_)? 490e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 491e29a70f1SJoe Perches kstrdup(?:_const)? | 492e29a70f1SJoe Perches kmemdup(?:_nul)?) | 493461e1565SChristophe JAILLET (?:\w+)?alloc_skb(?:_ip_align)? | 494e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 495e29a70f1SJoe Perches dma_alloc_coherent 496e29a70f1SJoe Perches)}; 497e29a70f1SJoe Perches 49820112475SJoe Perchesour $signature_tags = qr{(?xi: 49920112475SJoe Perches Signed-off-by:| 500d499480cSJorge Ramirez-Ortiz Co-developed-by:| 50120112475SJoe Perches Acked-by:| 50220112475SJoe Perches Tested-by:| 50320112475SJoe Perches Reviewed-by:| 50420112475SJoe Perches Reported-by:| 5058543ae12SMugunthan V N Suggested-by:| 50620112475SJoe Perches To:| 50720112475SJoe Perches Cc: 50820112475SJoe Perches)}; 50920112475SJoe Perches 510831242abSAditya Srivastavasub edit_distance_min { 511831242abSAditya Srivastava my (@arr) = @_; 512831242abSAditya Srivastava my $len = scalar @arr; 513831242abSAditya Srivastava if ((scalar @arr) < 1) { 514831242abSAditya Srivastava # if underflow, return 515831242abSAditya Srivastava return; 516831242abSAditya Srivastava } 517831242abSAditya Srivastava my $min = $arr[0]; 518831242abSAditya Srivastava for my $i (0 .. ($len-1)) { 519831242abSAditya Srivastava if ($arr[$i] < $min) { 520831242abSAditya Srivastava $min = $arr[$i]; 521831242abSAditya Srivastava } 522831242abSAditya Srivastava } 523831242abSAditya Srivastava return $min; 524831242abSAditya Srivastava} 525831242abSAditya Srivastava 526831242abSAditya Srivastavasub get_edit_distance { 527831242abSAditya Srivastava my ($str1, $str2) = @_; 528831242abSAditya Srivastava $str1 = lc($str1); 529831242abSAditya Srivastava $str2 = lc($str2); 530831242abSAditya Srivastava $str1 =~ s/-//g; 531831242abSAditya Srivastava $str2 =~ s/-//g; 532831242abSAditya Srivastava my $len1 = length($str1); 533831242abSAditya Srivastava my $len2 = length($str2); 534831242abSAditya Srivastava # two dimensional array storing minimum edit distance 535831242abSAditya Srivastava my @distance; 536831242abSAditya Srivastava for my $i (0 .. $len1) { 537831242abSAditya Srivastava for my $j (0 .. $len2) { 538831242abSAditya Srivastava if ($i == 0) { 539831242abSAditya Srivastava $distance[$i][$j] = $j; 540831242abSAditya Srivastava } elsif ($j == 0) { 541831242abSAditya Srivastava $distance[$i][$j] = $i; 542831242abSAditya Srivastava } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) { 543831242abSAditya Srivastava $distance[$i][$j] = $distance[$i - 1][$j - 1]; 544831242abSAditya Srivastava } else { 545831242abSAditya Srivastava my $dist1 = $distance[$i][$j - 1]; #insert distance 546831242abSAditya Srivastava my $dist2 = $distance[$i - 1][$j]; # remove 547831242abSAditya Srivastava my $dist3 = $distance[$i - 1][$j - 1]; #replace 548831242abSAditya Srivastava $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3); 549831242abSAditya Srivastava } 550831242abSAditya Srivastava } 551831242abSAditya Srivastava } 552831242abSAditya Srivastava return $distance[$len1][$len2]; 553831242abSAditya Srivastava} 554831242abSAditya Srivastava 555831242abSAditya Srivastavasub find_standard_signature { 556831242abSAditya Srivastava my ($sign_off) = @_; 557831242abSAditya Srivastava my @standard_signature_tags = ( 558831242abSAditya Srivastava 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:', 559831242abSAditya Srivastava 'Reviewed-by:', 'Reported-by:', 'Suggested-by:' 560831242abSAditya Srivastava ); 561831242abSAditya Srivastava foreach my $signature (@standard_signature_tags) { 562831242abSAditya Srivastava return $signature if (get_edit_distance($sign_off, $signature) <= 2); 563831242abSAditya Srivastava } 564831242abSAditya Srivastava 565831242abSAditya Srivastava return ""; 566831242abSAditya Srivastava} 567831242abSAditya Srivastava 5681813087dSJoe Perchesour @typeListMisordered = ( 5691813087dSJoe Perches qr{char\s+(?:un)?signed}, 5701813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 5711813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 5721813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 5731813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 5741813087dSJoe Perches qr{short\s+(?:un)?signed}, 5751813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5761813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5771813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5781813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5791813087dSJoe Perches qr{int\s+(?:un)?signed}, 5801813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5811813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5821813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5831813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5841813087dSJoe Perches qr{long\s+(?:un)?signed}, 5851813087dSJoe Perches); 5861813087dSJoe Perches 5878905a67cSAndy Whitcroftour @typeList = ( 5888905a67cSAndy Whitcroft qr{void}, 5890c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5900c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5910c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5920c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5930c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5940c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5950c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5960c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5970c773d9dSJoe Perches qr{(?:un)?signed}, 5988905a67cSAndy Whitcroft qr{float}, 5998905a67cSAndy Whitcroft qr{double}, 6008905a67cSAndy Whitcroft qr{bool}, 6018905a67cSAndy Whitcroft qr{struct\s+$Ident}, 6028905a67cSAndy Whitcroft qr{union\s+$Ident}, 6038905a67cSAndy Whitcroft qr{enum\s+$Ident}, 6048905a67cSAndy Whitcroft qr{${Ident}_t}, 6058905a67cSAndy Whitcroft qr{${Ident}_handler}, 6068905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 6071813087dSJoe Perches @typeListMisordered, 6088905a67cSAndy Whitcroft); 609938224b5SJoe Perches 610938224b5SJoe Perchesour $C90_int_types = qr{(?x: 611938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 612938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 613938224b5SJoe Perches long\s+long\s+(?:un)?signed| 614938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 615938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 616938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 617938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 618938224b5SJoe Perches 619938224b5SJoe Perches long\s+int\s+(?:un)?signed| 620938224b5SJoe Perches long\s+(?:un)?signed\s+int| 621938224b5SJoe Perches long\s+(?:un)?signed| 622938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 623938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 624938224b5SJoe Perches int\s+long\s+(?:un)?signed| 625938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 626938224b5SJoe Perches 627938224b5SJoe Perches int\s+(?:un)?signed| 628938224b5SJoe Perches (?:(?:un)?signed\s+)?int 629938224b5SJoe Perches)}; 630938224b5SJoe Perches 631485ff23eSAlex Dowadour @typeListFile = (); 6328716de38SJoe Perchesour @typeListWithAttr = ( 6338716de38SJoe Perches @typeList, 6348716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 6358716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 6368716de38SJoe Perches); 6378716de38SJoe Perches 638c45dcabdSAndy Whitcroftour @modifierList = ( 639c45dcabdSAndy Whitcroft qr{fastcall}, 640c45dcabdSAndy Whitcroft); 641485ff23eSAlex Dowadour @modifierListFile = (); 6428905a67cSAndy Whitcroft 6432435880fSJoe Perchesour @mode_permission_funcs = ( 6442435880fSJoe Perches ["module_param", 3], 6452435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 6462435880fSJoe Perches ["module_param_array_named", 5], 6472435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 6482435880fSJoe Perches ["proc_create(?:_data|)", 2], 649459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 650459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 651459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 652459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 653459cf0aeSJoe Perches ["__ATTR", 2], 6542435880fSJoe Perches); 6552435880fSJoe Perches 6561a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; 6571a3dcf2eSJoe Perches 658515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 659515a235eSJoe Perchesour $mode_perms_search = ""; 660515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 661515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 662515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 663515a235eSJoe Perches} 66400180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 665515a235eSJoe Perches 6669189c7e7SJoe Perchesour %deprecated_apis = ( 6679189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 6689189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 6699189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 6709189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 6719189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 6729189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 6739189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 6749189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 6759189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 6769189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6779189c7e7SJoe Perches); 6789189c7e7SJoe Perches 6799189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6809189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6819189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6829189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6839189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6849189c7e7SJoe Perches} 6859189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6869189c7e7SJoe Perches 687b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 688b392c64fSJoe Perches S_IWUGO | 689b392c64fSJoe Perches S_IWOTH | 690b392c64fSJoe Perches S_IRWXUGO | 691b392c64fSJoe Perches S_IALLUGO | 692b392c64fSJoe Perches 0[0-7][0-7][2367] 693b392c64fSJoe Perches}x; 694b392c64fSJoe Perches 695f90774e1SJoe Perchesour %mode_permission_string_types = ( 696f90774e1SJoe Perches "S_IRWXU" => 0700, 697f90774e1SJoe Perches "S_IRUSR" => 0400, 698f90774e1SJoe Perches "S_IWUSR" => 0200, 699f90774e1SJoe Perches "S_IXUSR" => 0100, 700f90774e1SJoe Perches "S_IRWXG" => 0070, 701f90774e1SJoe Perches "S_IRGRP" => 0040, 702f90774e1SJoe Perches "S_IWGRP" => 0020, 703f90774e1SJoe Perches "S_IXGRP" => 0010, 704f90774e1SJoe Perches "S_IRWXO" => 0007, 705f90774e1SJoe Perches "S_IROTH" => 0004, 706f90774e1SJoe Perches "S_IWOTH" => 0002, 707f90774e1SJoe Perches "S_IXOTH" => 0001, 708f90774e1SJoe Perches "S_IRWXUGO" => 0777, 709f90774e1SJoe Perches "S_IRUGO" => 0444, 710f90774e1SJoe Perches "S_IWUGO" => 0222, 711f90774e1SJoe Perches "S_IXUGO" => 0111, 712f90774e1SJoe Perches); 713f90774e1SJoe Perches 714f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 715f90774e1SJoe Perchesour $mode_perms_string_search = ""; 716f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 717f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 718f90774e1SJoe Perches $mode_perms_string_search .= $entry; 719f90774e1SJoe Perches} 72000180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 72100180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 72200180468SJoe Perches ${single_mode_perms_string_search} 72300180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 72400180468SJoe Perches}x; 72500180468SJoe Perches 72600180468SJoe Perchessub perms_to_octal { 72700180468SJoe Perches my ($string) = @_; 72800180468SJoe Perches 72900180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 73000180468SJoe Perches 73100180468SJoe Perches my $val = ""; 73200180468SJoe Perches my $oval = ""; 73300180468SJoe Perches my $to = 0; 73400180468SJoe Perches my $curpos = 0; 73500180468SJoe Perches my $lastpos = 0; 73600180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 73700180468SJoe Perches $curpos = pos($string); 73800180468SJoe Perches my $match = $2; 73900180468SJoe Perches my $omatch = $1; 74000180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 74100180468SJoe Perches $lastpos = $curpos; 74200180468SJoe Perches $to |= $mode_permission_string_types{$match}; 74300180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 74400180468SJoe Perches $val .= $match; 74500180468SJoe Perches $oval .= $omatch; 74600180468SJoe Perches } 74700180468SJoe Perches $oval =~ s/^\s*\|\s*//; 74800180468SJoe Perches $oval =~ s/\s*\|\s*$//; 74900180468SJoe Perches return sprintf("%04o", $to); 75000180468SJoe Perches} 751f90774e1SJoe Perches 7527840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 7537840a94cSWolfram Sang irq| 754cdcee686SSergey Ryazanov memory| 755cdcee686SSergey Ryazanov time| 756cdcee686SSergey Ryazanov reboot 7577840a94cSWolfram Sang)}; 7587840a94cSWolfram Sang# memory.h: ARM has a custom one 7597840a94cSWolfram Sang 76066b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 76166b47b4aSKees Cookmy $misspellings; 76266b47b4aSKees Cookmy %spelling_fix; 76336061e38SJoe Perches 76436061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 76566b47b4aSKees Cook while (<$spelling>) { 76666b47b4aSKees Cook my $line = $_; 76766b47b4aSKees Cook 76866b47b4aSKees Cook $line =~ s/\s*\n?$//g; 76966b47b4aSKees Cook $line =~ s/^\s*//g; 77066b47b4aSKees Cook 77166b47b4aSKees Cook next if ($line =~ m/^\s*#/); 77266b47b4aSKees Cook next if ($line =~ m/^\s*$/); 77366b47b4aSKees Cook 77466b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 77566b47b4aSKees Cook 77666b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 77766b47b4aSKees Cook } 77866b47b4aSKees Cook close($spelling); 77936061e38SJoe Perches} else { 78036061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 78136061e38SJoe Perches} 78266b47b4aSKees Cook 783ebfd7d62SJoe Perchesif ($codespell) { 784ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 785ebfd7d62SJoe Perches while (<$spelling>) { 786ebfd7d62SJoe Perches my $line = $_; 787ebfd7d62SJoe Perches 788ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 789ebfd7d62SJoe Perches $line =~ s/^\s*//g; 790ebfd7d62SJoe Perches 791ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 792ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 793ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 794ebfd7d62SJoe Perches 795ebfd7d62SJoe Perches $line =~ s/,.*$//; 796ebfd7d62SJoe Perches 797ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 798ebfd7d62SJoe Perches 799ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 800ebfd7d62SJoe Perches } 801ebfd7d62SJoe Perches close($spelling); 802ebfd7d62SJoe Perches } else { 803ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 804ebfd7d62SJoe Perches } 805ebfd7d62SJoe Perches} 806ebfd7d62SJoe Perches 807ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 808ebfd7d62SJoe Perches 80975ad8c57SJerome Forissiersub read_words { 81075ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 81175ad8c57SJerome Forissier 81275ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 81375ad8c57SJerome Forissier while (<$words>) { 814bf1fa1daSJoe Perches my $line = $_; 815bf1fa1daSJoe Perches 816bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 817bf1fa1daSJoe Perches $line =~ s/^\s*//g; 818bf1fa1daSJoe Perches 819bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 820bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 821bf1fa1daSJoe Perches if ($line =~ /\s/) { 82275ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 823bf1fa1daSJoe Perches next; 824bf1fa1daSJoe Perches } 825bf1fa1daSJoe Perches 826ced69da1SQuentin Monnet $$wordsRef .= '|' if (defined $$wordsRef); 82775ad8c57SJerome Forissier $$wordsRef .= $line; 828bf1fa1daSJoe Perches } 82975ad8c57SJerome Forissier close($file); 83075ad8c57SJerome Forissier return 1; 831bf1fa1daSJoe Perches } 832bf1fa1daSJoe Perches 83375ad8c57SJerome Forissier return 0; 83475ad8c57SJerome Forissier} 83575ad8c57SJerome Forissier 836ced69da1SQuentin Monnetmy $const_structs; 837ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) { 83875ad8c57SJerome Forissier read_words(\$const_structs, $conststructsfile) 83975ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 840ced69da1SQuentin Monnet} 84175ad8c57SJerome Forissier 842ced69da1SQuentin Monnetif (defined($typedefsfile)) { 843ced69da1SQuentin Monnet my $typeOtherTypedefs; 84475ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 84575ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 846ced69da1SQuentin Monnet $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); 84775ad8c57SJerome Forissier} 84875ad8c57SJerome Forissier 8498905a67cSAndy Whitcroftsub build_types { 850485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 851485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 8521813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 8538716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 854c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 855ab7e23f3SJoe Perches $BasicType = qr{ 856ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 857ab7e23f3SJoe Perches (?:${all}\b) 858ab7e23f3SJoe Perches }x; 8598905a67cSAndy Whitcroft $NonptrType = qr{ 860d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 861cf655043SAndy Whitcroft (?: 8626b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 8638ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 864c45dcabdSAndy Whitcroft (?:${all}\b) 865cf655043SAndy Whitcroft ) 866c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 8678905a67cSAndy Whitcroft }x; 8681813087dSJoe Perches $NonptrTypeMisordered = qr{ 8691813087dSJoe Perches (?:$Modifier\s+|const\s+)* 8701813087dSJoe Perches (?: 8711813087dSJoe Perches (?:${Misordered}\b) 8721813087dSJoe Perches ) 8731813087dSJoe Perches (?:\s+$Modifier|\s+const)* 8741813087dSJoe Perches }x; 8758716de38SJoe Perches $NonptrTypeWithAttr = qr{ 8768716de38SJoe Perches (?:$Modifier\s+|const\s+)* 8778716de38SJoe Perches (?: 8788716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8798716de38SJoe Perches (?:$typeTypedefs\b)| 8808716de38SJoe Perches (?:${allWithAttr}\b) 8818716de38SJoe Perches ) 8828716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8838716de38SJoe Perches }x; 8848905a67cSAndy Whitcroft $Type = qr{ 885c45dcabdSAndy Whitcroft $NonptrType 8867b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 887c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8888905a67cSAndy Whitcroft }x; 8891813087dSJoe Perches $TypeMisordered = qr{ 8901813087dSJoe Perches $NonptrTypeMisordered 8917b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 8921813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8931813087dSJoe Perches }x; 89491cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8951813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8968905a67cSAndy Whitcroft} 8978905a67cSAndy Whitcroftbuild_types(); 8986c72ffaaSAndy Whitcroft 8997d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 900d1fe9c09SJoe Perches 901d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 902d1fe9c09SJoe Perches# requires at least perl version v5.10.0 903d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 904d1fe9c09SJoe Perches 905d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 9062435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 907c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 9087d2367afSJoe Perches 909f8422308SJoe Perchesour $declaration_macros = qr{(?x: 9103e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 911fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 9123d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 913f8422308SJoe Perches)}; 914f8422308SJoe Perches 9158d0325ccSAditya Srivastavaour %allow_repeated_words = ( 9168d0325ccSAditya Srivastava add => '', 9178d0325ccSAditya Srivastava added => '', 9188d0325ccSAditya Srivastava bad => '', 9198d0325ccSAditya Srivastava be => '', 9208d0325ccSAditya Srivastava); 9218d0325ccSAditya Srivastava 9227d2367afSJoe Perchessub deparenthesize { 9237d2367afSJoe Perches my ($string) = @_; 9247d2367afSJoe Perches return "" if (!defined($string)); 9255b9553abSJoe Perches 9265b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 9275b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 9285b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 9295b9553abSJoe Perches } 9305b9553abSJoe Perches 9317d2367afSJoe Perches $string =~ s@\s+@ @g; 9325b9553abSJoe Perches 9337d2367afSJoe Perches return $string; 9347d2367afSJoe Perches} 9357d2367afSJoe Perches 9363445686aSJoe Perchessub seed_camelcase_file { 9373445686aSJoe Perches my ($file) = @_; 9383445686aSJoe Perches 9393445686aSJoe Perches return if (!(-f $file)); 9403445686aSJoe Perches 9413445686aSJoe Perches local $/; 9423445686aSJoe Perches 9433445686aSJoe Perches open(my $include_file, '<', "$file") 9443445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 9453445686aSJoe Perches my $text = <$include_file>; 9463445686aSJoe Perches close($include_file); 9473445686aSJoe Perches 9483445686aSJoe Perches my @lines = split('\n', $text); 9493445686aSJoe Perches 9503445686aSJoe Perches foreach my $line (@lines) { 9513445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 9523445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 9533445686aSJoe Perches $camelcase{$1} = 1; 95411ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 95511ea516aSJoe Perches $camelcase{$1} = 1; 95611ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 9573445686aSJoe Perches $camelcase{$1} = 1; 9583445686aSJoe Perches } 9593445686aSJoe Perches } 9603445686aSJoe Perches} 9613445686aSJoe Perches 962cd28b119SJoe Perchesour %maintained_status = (); 963cd28b119SJoe Perches 96485b0ee18SJoe Perchessub is_maintained_obsolete { 96585b0ee18SJoe Perches my ($filename) = @_; 96685b0ee18SJoe Perches 967f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 96885b0ee18SJoe Perches 969cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 970cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 971cd28b119SJoe Perches } 97285b0ee18SJoe Perches 973cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 97485b0ee18SJoe Perches} 97585b0ee18SJoe Perches 9763b6e8ac9SJoe Perchessub is_SPDX_License_valid { 9773b6e8ac9SJoe Perches my ($license) = @_; 9783b6e8ac9SJoe Perches 9790f7f635bSJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); 9803b6e8ac9SJoe Perches 98156294112SJoe Perches my $root_path = abs_path($root); 98256294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 9833b6e8ac9SJoe Perches return 0 if ($status ne ""); 9843b6e8ac9SJoe Perches return 1; 9853b6e8ac9SJoe Perches} 9863b6e8ac9SJoe Perches 9873445686aSJoe Perchesmy $camelcase_seeded = 0; 9883445686aSJoe Perchessub seed_camelcase_includes { 9893445686aSJoe Perches return if ($camelcase_seeded); 9903445686aSJoe Perches 9913445686aSJoe Perches my $files; 992c707a81dSJoe Perches my $camelcase_cache = ""; 993c707a81dSJoe Perches my @include_files = (); 994c707a81dSJoe Perches 995c707a81dSJoe Perches $camelcase_seeded = 1; 996351b2a1fSJoe Perches 9970f7f635bSJoe Perches if (-e "$gitroot") { 998dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 999351b2a1fSJoe Perches chomp $git_last_include_commit; 1000c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 1001c707a81dSJoe Perches } else { 1002c707a81dSJoe Perches my $last_mod_date = 0; 1003c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 1004c707a81dSJoe Perches @include_files = split('\n', $files); 1005c707a81dSJoe Perches foreach my $file (@include_files) { 1006c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 1007c707a81dSJoe Perches localtime((stat $file)[9])); 1008c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 1009c707a81dSJoe Perches } 1010c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 1011c707a81dSJoe Perches } 1012c707a81dSJoe Perches 1013c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 1014c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 1015c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 1016351b2a1fSJoe Perches while (<$camelcase_file>) { 1017351b2a1fSJoe Perches chomp; 1018351b2a1fSJoe Perches $camelcase{$_} = 1; 1019351b2a1fSJoe Perches } 1020351b2a1fSJoe Perches close($camelcase_file); 1021351b2a1fSJoe Perches 1022351b2a1fSJoe Perches return; 1023351b2a1fSJoe Perches } 1024c707a81dSJoe Perches 10250f7f635bSJoe Perches if (-e "$gitroot") { 1026dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 1027c707a81dSJoe Perches @include_files = split('\n', $files); 10283445686aSJoe Perches } 1029c707a81dSJoe Perches 10303445686aSJoe Perches foreach my $file (@include_files) { 10313445686aSJoe Perches seed_camelcase_file($file); 10323445686aSJoe Perches } 1033351b2a1fSJoe Perches 1034c707a81dSJoe Perches if ($camelcase_cache ne "") { 1035351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 1036c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 1037c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 1038351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 1039351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 1040351b2a1fSJoe Perches } 1041351b2a1fSJoe Perches close($camelcase_file); 1042351b2a1fSJoe Perches } 10433445686aSJoe Perches} 10443445686aSJoe Perches 1045f5f61325SJoe Perchessub git_is_single_file { 1046f5f61325SJoe Perches my ($filename) = @_; 1047f5f61325SJoe Perches 1048f5f61325SJoe Perches return 0 if ((which("git") eq "") || !(-e "$gitroot")); 1049f5f61325SJoe Perches 1050f5f61325SJoe Perches my $output = `${git_command} ls-files -- $filename 2>/dev/null`; 1051f5f61325SJoe Perches my $count = $output =~ tr/\n//; 1052f5f61325SJoe Perches return $count eq 1 && $output =~ m{^${filename}$}; 1053f5f61325SJoe Perches} 1054f5f61325SJoe Perches 1055d311cd44SJoe Perchessub git_commit_info { 1056d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 1057d311cd44SJoe Perches 10580f7f635bSJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); 1059d311cd44SJoe Perches 1060dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 1061d311cd44SJoe Perches $output =~ s/^\s*//gm; 1062d311cd44SJoe Perches my @lines = split("\n", $output); 1063d311cd44SJoe Perches 10640d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 10650d7835fcSJoe Perches 10665a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 1067d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 1068d311cd44SJoe Perches# all matching commit ids, but it's very slow... 1069d311cd44SJoe Perches# 1070d311cd44SJoe Perches# echo "checking commits $1..." 1071d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 1072d311cd44SJoe Perches# while read line ; do 1073d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 1074d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 1075d311cd44SJoe Perches# done 1076d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 1077948b133aSHeinrich Schuchardt $id = undef; 1078d311cd44SJoe Perches } else { 1079d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 1080d311cd44SJoe Perches $desc = substr($lines[0], 41); 1081d311cd44SJoe Perches } 1082d311cd44SJoe Perches 1083d311cd44SJoe Perches return ($id, $desc); 1084d311cd44SJoe Perches} 1085d311cd44SJoe Perches 10866c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 10870a920b5bSAndy Whitcroft 108800df344fSAndy Whitcroftmy @rawlines = (); 1089c2fdda0dSAndy Whitcroftmy @lines = (); 10903705ce5bSJoe Perchesmy @fixed = (); 1091d752fcc8SJoe Perchesmy @fixed_inserted = (); 1092d752fcc8SJoe Perchesmy @fixed_deleted = (); 1093194f66fcSJoe Perchesmy $fixlinenr = -1; 1094194f66fcSJoe Perches 10954a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10964a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 10970f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot"); 10984a593c34SDu, Changbin 10994a593c34SDu, Changbinif ($git) { 11004a593c34SDu, Changbin my @commits = (); 11010dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 11024a593c34SDu, Changbin my $git_range; 110328898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 110428898fd1SJoe Perches $git_range = "-$2 $1"; 11054a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 11064a593c34SDu, Changbin $git_range = "$commit_expr"; 11074a593c34SDu, Changbin } else { 11080dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 11090dea9f1eSJoe Perches } 1110dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 11110dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 111228898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 111328898fd1SJoe Perches next if (!defined($1) || !defined($2)); 11140dea9f1eSJoe Perches my $sha1 = $1; 11150dea9f1eSJoe Perches my $subject = $2; 11160dea9f1eSJoe Perches unshift(@commits, $sha1); 11170dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 11184a593c34SDu, Changbin } 11194a593c34SDu, Changbin } 11204a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 11214a593c34SDu, Changbin @ARGV = @commits; 11224a593c34SDu, Changbin} 11234a593c34SDu, Changbin 1124c2fdda0dSAndy Whitcroftmy $vname; 112598005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 11266c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 112721caa13cSAndy Whitcroft my $FILE; 1128f5f61325SJoe Perches my $is_git_file = git_is_single_file($filename); 1129f5f61325SJoe Perches my $oldfile = $file; 1130f5f61325SJoe Perches $file = 1 if ($is_git_file); 11314a593c34SDu, Changbin if ($git) { 11324a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 11334a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 11344a593c34SDu, Changbin } elsif ($file) { 113521caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 11366c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 113721caa13cSAndy Whitcroft } elsif ($filename eq '-') { 113821caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 11396c72ffaaSAndy Whitcroft } else { 114021caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 11416c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 11426c72ffaaSAndy Whitcroft } 1143c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1144c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 11454a593c34SDu, Changbin } elsif ($git) { 11460dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1147c2fdda0dSAndy Whitcroft } else { 1148c2fdda0dSAndy Whitcroft $vname = $filename; 1149c2fdda0dSAndy Whitcroft } 115021caa13cSAndy Whitcroft while (<$FILE>) { 11510a920b5bSAndy Whitcroft chomp; 115200df344fSAndy Whitcroft push(@rawlines, $_); 1153c7f574d0SGeert Uytterhoeven $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); 11546c72ffaaSAndy Whitcroft } 115521caa13cSAndy Whitcroft close($FILE); 1156d8469f16SJoe Perches 1157d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1158d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1159d8469f16SJoe Perches print "$vname\n"; 1160d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1161d8469f16SJoe Perches } 1162d8469f16SJoe Perches 1163c2fdda0dSAndy Whitcroft if (!process($filename)) { 11640a920b5bSAndy Whitcroft $exit = 1; 11650a920b5bSAndy Whitcroft } 116600df344fSAndy Whitcroft @rawlines = (); 116713214adfSAndy Whitcroft @lines = (); 11683705ce5bSJoe Perches @fixed = (); 1169d752fcc8SJoe Perches @fixed_inserted = (); 1170d752fcc8SJoe Perches @fixed_deleted = (); 1171194f66fcSJoe Perches $fixlinenr = -1; 1172485ff23eSAlex Dowad @modifierListFile = (); 1173485ff23eSAlex Dowad @typeListFile = (); 1174485ff23eSAlex Dowad build_types(); 1175f5f61325SJoe Perches $file = $oldfile if ($is_git_file); 11760a920b5bSAndy Whitcroft} 11770a920b5bSAndy Whitcroft 1178d8469f16SJoe Perchesif (!$quiet) { 11793c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 11803c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 11813c816e49SJoe Perches 11825b57980dSJoe Perches if (!$perl_version_ok) { 1183d8469f16SJoe Perches print << "EOM" 1184d8469f16SJoe Perches 1185d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 11865b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1187d8469f16SJoe PerchesEOM 1188d8469f16SJoe Perches } 1189d8469f16SJoe Perches if ($exit) { 1190d8469f16SJoe Perches print << "EOM" 1191d8469f16SJoe Perches 1192d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1193d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1194d8469f16SJoe PerchesEOM 1195d8469f16SJoe Perches } 1196d8469f16SJoe Perches} 1197d8469f16SJoe Perches 11980a920b5bSAndy Whitcroftexit($exit); 11990a920b5bSAndy Whitcroft 12000a920b5bSAndy Whitcroftsub top_of_kernel_tree { 12016c72ffaaSAndy Whitcroft my ($root) = @_; 12026c72ffaaSAndy Whitcroft 12036c72ffaaSAndy Whitcroft my @tree_check = ( 12046c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 12056c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 12066c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 12076c72ffaaSAndy Whitcroft ); 12086c72ffaaSAndy Whitcroft 12096c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 12106c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 12110a920b5bSAndy Whitcroft return 0; 12120a920b5bSAndy Whitcroft } 12136c72ffaaSAndy Whitcroft } 12146c72ffaaSAndy Whitcroft return 1; 12156c72ffaaSAndy Whitcroft} 12160a920b5bSAndy Whitcroft 121720112475SJoe Perchessub parse_email { 121820112475SJoe Perches my ($formatted_email) = @_; 121920112475SJoe Perches 122020112475SJoe Perches my $name = ""; 1221fccaebf0SDwaipayan Ray my $quoted = ""; 1222dfa05c28SJoe Perches my $name_comment = ""; 122320112475SJoe Perches my $address = ""; 122420112475SJoe Perches my $comment = ""; 122520112475SJoe Perches 122620112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 122720112475SJoe Perches $name = $1; 122820112475SJoe Perches $address = $2; 122920112475SJoe Perches $comment = $3 if defined $3; 123020112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 123120112475SJoe Perches $address = $1; 123220112475SJoe Perches $comment = $2 if defined $2; 123320112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 123420112475SJoe Perches $address = $1; 123520112475SJoe Perches $comment = $2 if defined $2; 123685e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 123720112475SJoe Perches $name = $formatted_email; 12383705ce5bSJoe Perches $name = trim($name); 123920112475SJoe Perches $name =~ s/^\"|\"$//g; 124020112475SJoe Perches # If there's a name left after stripping spaces and 124120112475SJoe Perches # leading quotes, and the address doesn't have both 124220112475SJoe Perches # leading and trailing angle brackets, the address 124320112475SJoe Perches # is invalid. ie: 124420112475SJoe Perches # "joe smith [email protected]" bad 124520112475SJoe Perches # "joe smith <[email protected]" bad 124620112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 124720112475SJoe Perches $name = ""; 124820112475SJoe Perches $address = ""; 124920112475SJoe Perches $comment = ""; 125020112475SJoe Perches } 125120112475SJoe Perches } 125220112475SJoe Perches 1253fccaebf0SDwaipayan Ray # Extract comments from names excluding quoted parts 1254fccaebf0SDwaipayan Ray # "John D. (Doe)" - Do not extract 1255fccaebf0SDwaipayan Ray if ($name =~ s/\"(.+)\"//) { 1256fccaebf0SDwaipayan Ray $quoted = $1; 1257dfa05c28SJoe Perches } 1258fccaebf0SDwaipayan Ray while ($name =~ s/\s*($balanced_parens)\s*/ /) { 1259fccaebf0SDwaipayan Ray $name_comment .= trim($1); 1260fccaebf0SDwaipayan Ray } 1261fccaebf0SDwaipayan Ray $name =~ s/^[ \"]+|[ \"]+$//g; 1262fccaebf0SDwaipayan Ray $name = trim("$quoted $name"); 1263fccaebf0SDwaipayan Ray 12643705ce5bSJoe Perches $address = trim($address); 126520112475SJoe Perches $address =~ s/^\<|\>$//g; 1266fccaebf0SDwaipayan Ray $comment = trim($comment); 126720112475SJoe Perches 126820112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 126920112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 127020112475SJoe Perches $name = "\"$name\""; 127120112475SJoe Perches } 127220112475SJoe Perches 1273dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 127420112475SJoe Perches} 127520112475SJoe Perches 127620112475SJoe Perchessub format_email { 127748ca2d8aSDwaipayan Ray my ($name, $name_comment, $address, $comment) = @_; 127820112475SJoe Perches 127920112475SJoe Perches my $formatted_email; 128020112475SJoe Perches 1281fccaebf0SDwaipayan Ray $name =~ s/^[ \"]+|[ \"]+$//g; 12823705ce5bSJoe Perches $address = trim($address); 1283fccaebf0SDwaipayan Ray $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes 128420112475SJoe Perches 128520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 128620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 128720112475SJoe Perches $name = "\"$name\""; 128820112475SJoe Perches } 128920112475SJoe Perches 1290fccaebf0SDwaipayan Ray $name_comment = trim($name_comment); 1291fccaebf0SDwaipayan Ray $name_comment = " $name_comment" if ($name_comment ne ""); 1292fccaebf0SDwaipayan Ray $comment = trim($comment); 1293fccaebf0SDwaipayan Ray $comment = " $comment" if ($comment ne ""); 1294fccaebf0SDwaipayan Ray 129520112475SJoe Perches if ("$name" eq "") { 129620112475SJoe Perches $formatted_email = "$address"; 129720112475SJoe Perches } else { 129848ca2d8aSDwaipayan Ray $formatted_email = "$name$name_comment <$address>"; 129920112475SJoe Perches } 130048ca2d8aSDwaipayan Ray $formatted_email .= "$comment"; 130120112475SJoe Perches return $formatted_email; 130220112475SJoe Perches} 130320112475SJoe Perches 1304dfa05c28SJoe Perchessub reformat_email { 1305dfa05c28SJoe Perches my ($email) = @_; 1306dfa05c28SJoe Perches 1307dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 130848ca2d8aSDwaipayan Ray return format_email($email_name, $name_comment, $email_address, $comment); 1309dfa05c28SJoe Perches} 1310dfa05c28SJoe Perches 1311dfa05c28SJoe Perchessub same_email_addresses { 1312fccaebf0SDwaipayan Ray my ($email1, $email2) = @_; 1313dfa05c28SJoe Perches 1314dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1315dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1316dfa05c28SJoe Perches 131748ca2d8aSDwaipayan Ray return $email1_name eq $email2_name && 131848ca2d8aSDwaipayan Ray $email1_address eq $email2_address && 131948ca2d8aSDwaipayan Ray $name1_comment eq $name2_comment && 132048ca2d8aSDwaipayan Ray $comment1 eq $comment2; 132148ca2d8aSDwaipayan Ray} 1322dfa05c28SJoe Perches 1323d311cd44SJoe Perchessub which { 1324d311cd44SJoe Perches my ($bin) = @_; 1325d311cd44SJoe Perches 1326d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1327d311cd44SJoe Perches if (-e "$path/$bin") { 1328d311cd44SJoe Perches return "$path/$bin"; 1329d311cd44SJoe Perches } 1330d311cd44SJoe Perches } 1331d311cd44SJoe Perches 1332d311cd44SJoe Perches return ""; 1333d311cd44SJoe Perches} 1334d311cd44SJoe Perches 1335000d1cc1SJoe Perchessub which_conf { 1336000d1cc1SJoe Perches my ($conf) = @_; 1337000d1cc1SJoe Perches 1338000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1339000d1cc1SJoe Perches if (-e "$path/$conf") { 1340000d1cc1SJoe Perches return "$path/$conf"; 1341000d1cc1SJoe Perches } 1342000d1cc1SJoe Perches } 1343000d1cc1SJoe Perches 1344000d1cc1SJoe Perches return ""; 1345000d1cc1SJoe Perches} 1346000d1cc1SJoe Perches 13470a920b5bSAndy Whitcroftsub expand_tabs { 13480a920b5bSAndy Whitcroft my ($str) = @_; 13490a920b5bSAndy Whitcroft 13500a920b5bSAndy Whitcroft my $res = ''; 13510a920b5bSAndy Whitcroft my $n = 0; 13520a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 13530a920b5bSAndy Whitcroft if ($c eq "\t") { 13540a920b5bSAndy Whitcroft $res .= ' '; 13550a920b5bSAndy Whitcroft $n++; 1356713a09deSAntonio Borneo for (; ($n % $tabsize) != 0; $n++) { 13570a920b5bSAndy Whitcroft $res .= ' '; 13580a920b5bSAndy Whitcroft } 13590a920b5bSAndy Whitcroft next; 13600a920b5bSAndy Whitcroft } 13610a920b5bSAndy Whitcroft $res .= $c; 13620a920b5bSAndy Whitcroft $n++; 13630a920b5bSAndy Whitcroft } 13640a920b5bSAndy Whitcroft 13650a920b5bSAndy Whitcroft return $res; 13660a920b5bSAndy Whitcroft} 13676c72ffaaSAndy Whitcroftsub copy_spacing { 1368773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 13696c72ffaaSAndy Whitcroft return $res; 13706c72ffaaSAndy Whitcroft} 13710a920b5bSAndy Whitcroft 13724a0df2efSAndy Whitcroftsub line_stats { 13734a0df2efSAndy Whitcroft my ($line) = @_; 13744a0df2efSAndy Whitcroft 13754a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 13764a0df2efSAndy Whitcroft $line =~ s/^.//; 13774a0df2efSAndy Whitcroft $line = expand_tabs($line); 13784a0df2efSAndy Whitcroft 13794a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 13804a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 13814a0df2efSAndy Whitcroft 13824a0df2efSAndy Whitcroft return (length($line), length($white)); 13834a0df2efSAndy Whitcroft} 13844a0df2efSAndy Whitcroft 1385773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1386773647a0SAndy Whitcroft 1387773647a0SAndy Whitcroftsub sanitise_line_reset { 1388773647a0SAndy Whitcroft my ($in_comment) = @_; 1389773647a0SAndy Whitcroft 1390773647a0SAndy Whitcroft if ($in_comment) { 1391773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1392773647a0SAndy Whitcroft } else { 1393773647a0SAndy Whitcroft $sanitise_quote = ''; 1394773647a0SAndy Whitcroft } 1395773647a0SAndy Whitcroft} 139600df344fSAndy Whitcroftsub sanitise_line { 139700df344fSAndy Whitcroft my ($line) = @_; 139800df344fSAndy Whitcroft 139900df344fSAndy Whitcroft my $res = ''; 140000df344fSAndy Whitcroft my $l = ''; 140100df344fSAndy Whitcroft 1402c2fdda0dSAndy Whitcroft my $qlen = 0; 1403773647a0SAndy Whitcroft my $off = 0; 1404773647a0SAndy Whitcroft my $c; 140500df344fSAndy Whitcroft 1406773647a0SAndy Whitcroft # Always copy over the diff marker. 1407773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1408773647a0SAndy Whitcroft 1409773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1410773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1411773647a0SAndy Whitcroft 14128d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1413773647a0SAndy Whitcroft # and end, all to $;. 1414773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1415773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1416773647a0SAndy Whitcroft 1417773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1418773647a0SAndy Whitcroft $off++; 141900df344fSAndy Whitcroft next; 1420773647a0SAndy Whitcroft } 142181bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1422773647a0SAndy Whitcroft $sanitise_quote = ''; 1423773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1424773647a0SAndy Whitcroft $off++; 1425773647a0SAndy Whitcroft next; 1426773647a0SAndy Whitcroft } 1427113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1428113f04a8SDaniel Walker $sanitise_quote = '//'; 1429113f04a8SDaniel Walker 1430113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1431113f04a8SDaniel Walker $off++; 1432113f04a8SDaniel Walker next; 1433113f04a8SDaniel Walker } 1434773647a0SAndy Whitcroft 1435773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1436773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1437773647a0SAndy Whitcroft $c eq "\\") { 1438773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1439773647a0SAndy Whitcroft $off++; 1440773647a0SAndy Whitcroft next; 1441773647a0SAndy Whitcroft } 1442773647a0SAndy Whitcroft # Regular quotes. 1443773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1444773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1445773647a0SAndy Whitcroft $sanitise_quote = $c; 1446773647a0SAndy Whitcroft 1447773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1448773647a0SAndy Whitcroft next; 1449773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1450773647a0SAndy Whitcroft $sanitise_quote = ''; 145100df344fSAndy Whitcroft } 145200df344fSAndy Whitcroft } 1453773647a0SAndy Whitcroft 1454fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1455773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1456773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1457113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1458113f04a8SDaniel Walker substr($res, $off, 1, $;); 1459773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1460773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 146100df344fSAndy Whitcroft } else { 1462773647a0SAndy Whitcroft substr($res, $off, 1, $c); 146300df344fSAndy Whitcroft } 1464c2fdda0dSAndy Whitcroft } 1465c2fdda0dSAndy Whitcroft 1466113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1467113f04a8SDaniel Walker $sanitise_quote = ''; 1468113f04a8SDaniel Walker } 1469113f04a8SDaniel Walker 1470c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1471c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1472c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1473c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1474c2fdda0dSAndy Whitcroft 1475c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1476c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1477c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1478c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1479c2fdda0dSAndy Whitcroft } 1480c2fdda0dSAndy Whitcroft 1481dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1482dadf680dSJoe Perches my $match = $1; 1483dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1484dadf680dSJoe Perches } 1485dadf680dSJoe Perches 148600df344fSAndy Whitcroft return $res; 148700df344fSAndy Whitcroft} 148800df344fSAndy Whitcroft 1489a6962d72SJoe Perchessub get_quoted_string { 1490a6962d72SJoe Perches my ($line, $rawline) = @_; 1491a6962d72SJoe Perches 1492478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 149333acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1494a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1495a6962d72SJoe Perches} 1496a6962d72SJoe Perches 14978905a67cSAndy Whitcroftsub ctx_statement_block { 14988905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 14998905a67cSAndy Whitcroft my $line = $linenr - 1; 15008905a67cSAndy Whitcroft my $blk = ''; 15018905a67cSAndy Whitcroft my $soff = $off; 15028905a67cSAndy Whitcroft my $coff = $off - 1; 1503773647a0SAndy Whitcroft my $coff_set = 0; 15048905a67cSAndy Whitcroft 150513214adfSAndy Whitcroft my $loff = 0; 150613214adfSAndy Whitcroft 15078905a67cSAndy Whitcroft my $type = ''; 15088905a67cSAndy Whitcroft my $level = 0; 1509a2750645SAndy Whitcroft my @stack = (); 1510cf655043SAndy Whitcroft my $p; 15118905a67cSAndy Whitcroft my $c; 15128905a67cSAndy Whitcroft my $len = 0; 151313214adfSAndy Whitcroft 151413214adfSAndy Whitcroft my $remainder; 15158905a67cSAndy Whitcroft while (1) { 1516a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1517a2750645SAndy Whitcroft 1518773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 15198905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 15208905a67cSAndy Whitcroft # context. 15218905a67cSAndy Whitcroft if ($off >= $len) { 15228905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1523dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1524c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 15258905a67cSAndy Whitcroft $remain--; 152613214adfSAndy Whitcroft $loff = $len; 1527c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 15288905a67cSAndy Whitcroft $len = length($blk); 15298905a67cSAndy Whitcroft $line++; 15308905a67cSAndy Whitcroft last; 15318905a67cSAndy Whitcroft } 15328905a67cSAndy Whitcroft # Bail if there is no further context. 15338905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 153413214adfSAndy Whitcroft if ($off >= $len) { 15358905a67cSAndy Whitcroft last; 15368905a67cSAndy Whitcroft } 1537f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1538f74bd194SAndy Whitcroft $level++; 1539f74bd194SAndy Whitcroft $type = '#'; 1540f74bd194SAndy Whitcroft } 15418905a67cSAndy Whitcroft } 1542cf655043SAndy Whitcroft $p = $c; 15438905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 154413214adfSAndy Whitcroft $remainder = substr($blk, $off); 15458905a67cSAndy Whitcroft 1546773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 15474635f4fbSAndy Whitcroft 15484635f4fbSAndy Whitcroft # Handle nested #if/#else. 15494635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 15504635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 15514635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 15524635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 15534635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 15544635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 15554635f4fbSAndy Whitcroft } 15564635f4fbSAndy Whitcroft 15578905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 15588905a67cSAndy Whitcroft # outermost level. 15598905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 15608905a67cSAndy Whitcroft last; 15618905a67cSAndy Whitcroft } 15628905a67cSAndy Whitcroft 156313214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1564773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1565773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1566773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1567773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1568773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1569773647a0SAndy Whitcroft $coff_set = 1; 1570773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1571773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 157213214adfSAndy Whitcroft } 157313214adfSAndy Whitcroft 15748905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 15758905a67cSAndy Whitcroft $level++; 15768905a67cSAndy Whitcroft $type = '('; 15778905a67cSAndy Whitcroft } 15788905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 15798905a67cSAndy Whitcroft $level--; 15808905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 15818905a67cSAndy Whitcroft 15828905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 15838905a67cSAndy Whitcroft $coff = $off; 1584773647a0SAndy Whitcroft $coff_set = 1; 1585773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 15868905a67cSAndy Whitcroft } 15878905a67cSAndy Whitcroft } 15888905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 15898905a67cSAndy Whitcroft $level++; 15908905a67cSAndy Whitcroft $type = '{'; 15918905a67cSAndy Whitcroft } 15928905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 15938905a67cSAndy Whitcroft $level--; 15948905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 15958905a67cSAndy Whitcroft 15968905a67cSAndy Whitcroft if ($level == 0) { 1597b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1598b998e001SPatrick Pannuto $off++; 1599b998e001SPatrick Pannuto } 16008905a67cSAndy Whitcroft last; 16018905a67cSAndy Whitcroft } 16028905a67cSAndy Whitcroft } 1603f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1604f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1605f74bd194SAndy Whitcroft $level--; 1606f74bd194SAndy Whitcroft $type = ''; 1607f74bd194SAndy Whitcroft $off++; 1608f74bd194SAndy Whitcroft last; 1609f74bd194SAndy Whitcroft } 16108905a67cSAndy Whitcroft $off++; 16118905a67cSAndy Whitcroft } 1612a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 161313214adfSAndy Whitcroft if ($off == $len) { 1614a3bb97a7SAndy Whitcroft $loff = $len + 1; 161513214adfSAndy Whitcroft $line++; 161613214adfSAndy Whitcroft $remain--; 161713214adfSAndy Whitcroft } 16188905a67cSAndy Whitcroft 16198905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 16208905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 16218905a67cSAndy Whitcroft 16228905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 16238905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 16248905a67cSAndy Whitcroft 1625773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 162613214adfSAndy Whitcroft 162713214adfSAndy Whitcroft return ($statement, $condition, 162813214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 162913214adfSAndy Whitcroft} 163013214adfSAndy Whitcroft 1631cf655043SAndy Whitcroftsub statement_lines { 1632cf655043SAndy Whitcroft my ($stmt) = @_; 1633cf655043SAndy Whitcroft 1634cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1635cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1636cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1637cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1638cf655043SAndy Whitcroft 1639cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1640cf655043SAndy Whitcroft 1641cf655043SAndy Whitcroft return $#stmt_lines + 2; 1642cf655043SAndy Whitcroft} 1643cf655043SAndy Whitcroft 1644cf655043SAndy Whitcroftsub statement_rawlines { 1645cf655043SAndy Whitcroft my ($stmt) = @_; 1646cf655043SAndy Whitcroft 1647cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1648cf655043SAndy Whitcroft 1649cf655043SAndy Whitcroft return $#stmt_lines + 2; 1650cf655043SAndy Whitcroft} 1651cf655043SAndy Whitcroft 1652cf655043SAndy Whitcroftsub statement_block_size { 1653cf655043SAndy Whitcroft my ($stmt) = @_; 1654cf655043SAndy Whitcroft 1655cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1656cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1657cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1658cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1659cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1660cf655043SAndy Whitcroft 1661cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1662cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1663cf655043SAndy Whitcroft 1664cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1665cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1666cf655043SAndy Whitcroft 1667cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1668cf655043SAndy Whitcroft return $stmt_lines; 1669cf655043SAndy Whitcroft } else { 1670cf655043SAndy Whitcroft return $stmt_statements; 1671cf655043SAndy Whitcroft } 1672cf655043SAndy Whitcroft} 1673cf655043SAndy Whitcroft 167413214adfSAndy Whitcroftsub ctx_statement_full { 167513214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 167613214adfSAndy Whitcroft my ($statement, $condition, $level); 167713214adfSAndy Whitcroft 167813214adfSAndy Whitcroft my (@chunks); 167913214adfSAndy Whitcroft 1680cf655043SAndy Whitcroft # Grab the first conditional/block pair. 168113214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 168213214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1683773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 168413214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1685cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1686cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1687cf655043SAndy Whitcroft } 1688cf655043SAndy Whitcroft 1689cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1690cf655043SAndy Whitcroft # could continue the statement. 1691cf655043SAndy Whitcroft for (;;) { 169213214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 169313214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1694cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1695773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1696cf655043SAndy Whitcroft #print "C: push\n"; 1697cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 169813214adfSAndy Whitcroft } 169913214adfSAndy Whitcroft 170013214adfSAndy Whitcroft return ($level, $linenr, @chunks); 17018905a67cSAndy Whitcroft} 17028905a67cSAndy Whitcroft 17034a0df2efSAndy Whitcroftsub ctx_block_get { 1704f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 17054a0df2efSAndy Whitcroft my $line; 17064a0df2efSAndy Whitcroft my $start = $linenr - 1; 17074a0df2efSAndy Whitcroft my $blk = ''; 17084a0df2efSAndy Whitcroft my @o; 17094a0df2efSAndy Whitcroft my @c; 17104a0df2efSAndy Whitcroft my @res = (); 17114a0df2efSAndy Whitcroft 1712f0a594c1SAndy Whitcroft my $level = 0; 17134635f4fbSAndy Whitcroft my @stack = ($level); 171400df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 171500df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 171600df344fSAndy Whitcroft $remain--; 171700df344fSAndy Whitcroft 171800df344fSAndy Whitcroft $blk .= $rawlines[$line]; 17194635f4fbSAndy Whitcroft 17204635f4fbSAndy Whitcroft # Handle nested #if/#else. 172101464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 17224635f4fbSAndy Whitcroft push(@stack, $level); 172301464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 17244635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 172501464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 17264635f4fbSAndy Whitcroft $level = pop(@stack); 17274635f4fbSAndy Whitcroft } 17284635f4fbSAndy Whitcroft 172901464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1730f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1731f0a594c1SAndy Whitcroft if ($off > 0) { 1732f0a594c1SAndy Whitcroft $off--; 1733f0a594c1SAndy Whitcroft next; 1734f0a594c1SAndy Whitcroft } 17354a0df2efSAndy Whitcroft 1736f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1737f0a594c1SAndy Whitcroft $level--; 1738f0a594c1SAndy Whitcroft last if ($level == 0); 1739f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1740f0a594c1SAndy Whitcroft $level++; 1741f0a594c1SAndy Whitcroft } 1742f0a594c1SAndy Whitcroft } 17434a0df2efSAndy Whitcroft 1744f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 174500df344fSAndy Whitcroft push(@res, $rawlines[$line]); 17464a0df2efSAndy Whitcroft } 17474a0df2efSAndy Whitcroft 1748f0a594c1SAndy Whitcroft last if ($level == 0); 17494a0df2efSAndy Whitcroft } 17504a0df2efSAndy Whitcroft 1751f0a594c1SAndy Whitcroft return ($level, @res); 17524a0df2efSAndy Whitcroft} 17534a0df2efSAndy Whitcroftsub ctx_block_outer { 17544a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 17554a0df2efSAndy Whitcroft 1756f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1757f0a594c1SAndy Whitcroft return @r; 17584a0df2efSAndy Whitcroft} 17594a0df2efSAndy Whitcroftsub ctx_block { 17604a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 17614a0df2efSAndy Whitcroft 1762f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1763f0a594c1SAndy Whitcroft return @r; 1764653d4876SAndy Whitcroft} 1765653d4876SAndy Whitcroftsub ctx_statement { 1766f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1767f0a594c1SAndy Whitcroft 1768f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1769f0a594c1SAndy Whitcroft return @r; 1770f0a594c1SAndy Whitcroft} 1771f0a594c1SAndy Whitcroftsub ctx_block_level { 1772653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1773653d4876SAndy Whitcroft 1774f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 17754a0df2efSAndy Whitcroft} 17769c0ca6f9SAndy Whitcroftsub ctx_statement_level { 17779c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 17789c0ca6f9SAndy Whitcroft 17799c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 17809c0ca6f9SAndy Whitcroft} 17814a0df2efSAndy Whitcroft 17824a0df2efSAndy Whitcroftsub ctx_locate_comment { 17834a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17844a0df2efSAndy Whitcroft 1785a55ee0ccSJoe Perches # If c99 comment on the current line, or the line before or after 1786a55ee0ccSJoe Perches my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); 1787a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1788a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); 1789a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1790a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); 1791a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1792a55ee0ccSJoe Perches 17934a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1794a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 17954a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 17964a0df2efSAndy Whitcroft 17974a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 17984a0df2efSAndy Whitcroft # comment. 17994a0df2efSAndy Whitcroft my $in_comment = 0; 18004a0df2efSAndy Whitcroft $current_comment = ''; 18014a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 180200df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 180300df344fSAndy Whitcroft #warn " $line\n"; 18044a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 18054a0df2efSAndy Whitcroft $in_comment = 1; 18064a0df2efSAndy Whitcroft } 18074a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 18084a0df2efSAndy Whitcroft $in_comment = 1; 18094a0df2efSAndy Whitcroft } 18104a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 18114a0df2efSAndy Whitcroft $current_comment = ''; 18124a0df2efSAndy Whitcroft } 18134a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 18144a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 18154a0df2efSAndy Whitcroft $in_comment = 0; 18164a0df2efSAndy Whitcroft } 18174a0df2efSAndy Whitcroft } 18184a0df2efSAndy Whitcroft 18194a0df2efSAndy Whitcroft chomp($current_comment); 18204a0df2efSAndy Whitcroft return($current_comment); 18214a0df2efSAndy Whitcroft} 18224a0df2efSAndy Whitcroftsub ctx_has_comment { 18234a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 18244a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 18254a0df2efSAndy Whitcroft 182600df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 18274a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 18284a0df2efSAndy Whitcroft 18294a0df2efSAndy Whitcroft return ($cmt ne ''); 18304a0df2efSAndy Whitcroft} 18314a0df2efSAndy Whitcroft 18324d001e4dSAndy Whitcroftsub raw_line { 18334d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 18344d001e4dSAndy Whitcroft 18354d001e4dSAndy Whitcroft my $offset = $linenr - 1; 18364d001e4dSAndy Whitcroft $cnt++; 18374d001e4dSAndy Whitcroft 18384d001e4dSAndy Whitcroft my $line; 18394d001e4dSAndy Whitcroft while ($cnt) { 18404d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 18414d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 18424d001e4dSAndy Whitcroft $cnt--; 18434d001e4dSAndy Whitcroft } 18444d001e4dSAndy Whitcroft 18454d001e4dSAndy Whitcroft return $line; 18464d001e4dSAndy Whitcroft} 18474d001e4dSAndy Whitcroft 18482a9f9d85STobin C. Hardingsub get_stat_real { 18492a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 18502a9f9d85STobin C. Harding 18512a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 18522a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 18532a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 18542a9f9d85STobin C. Harding } 18552a9f9d85STobin C. Harding 18562a9f9d85STobin C. Harding return $stat_real; 18572a9f9d85STobin C. Harding} 18582a9f9d85STobin C. Harding 1859e3d95a2aSTobin C. Hardingsub get_stat_here { 1860e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1861e3d95a2aSTobin C. Harding 1862e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1863e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1864e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1865e3d95a2aSTobin C. Harding } 1866e3d95a2aSTobin C. Harding 1867e3d95a2aSTobin C. Harding return $herectx; 1868e3d95a2aSTobin C. Harding} 1869e3d95a2aSTobin C. Harding 18700a920b5bSAndy Whitcroftsub cat_vet { 18710a920b5bSAndy Whitcroft my ($vet) = @_; 18729c0ca6f9SAndy Whitcroft my ($res, $coded); 18730a920b5bSAndy Whitcroft 18749c0ca6f9SAndy Whitcroft $res = ''; 18756c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 18766c72ffaaSAndy Whitcroft $res .= $1; 18776c72ffaaSAndy Whitcroft if ($2 ne '') { 18789c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 18796c72ffaaSAndy Whitcroft $res .= $coded; 18806c72ffaaSAndy Whitcroft } 18819c0ca6f9SAndy Whitcroft } 18829c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 18830a920b5bSAndy Whitcroft 18849c0ca6f9SAndy Whitcroft return $res; 18850a920b5bSAndy Whitcroft} 18860a920b5bSAndy Whitcroft 1887c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1888cf655043SAndy Whitcroftmy $av_pending; 1889c2fdda0dSAndy Whitcroftmy @av_paren_type; 18901f65f947SAndy Whitcroftmy $av_pend_colon; 1891c2fdda0dSAndy Whitcroft 1892c2fdda0dSAndy Whitcroftsub annotate_reset { 1893c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1894cf655043SAndy Whitcroft $av_pending = '_'; 1895cf655043SAndy Whitcroft @av_paren_type = ('E'); 18961f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1897c2fdda0dSAndy Whitcroft} 1898c2fdda0dSAndy Whitcroft 18996c72ffaaSAndy Whitcroftsub annotate_values { 19006c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 19016c72ffaaSAndy Whitcroft 19026c72ffaaSAndy Whitcroft my $res; 19031f65f947SAndy Whitcroft my $var = '_' x length($stream); 19046c72ffaaSAndy Whitcroft my $cur = $stream; 19056c72ffaaSAndy Whitcroft 1906c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 19076c72ffaaSAndy Whitcroft 19086c72ffaaSAndy Whitcroft while (length($cur)) { 1909773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1910cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1911171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 19126c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1913c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1914c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1915cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1916c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 19176c72ffaaSAndy Whitcroft } 19186c72ffaaSAndy Whitcroft 1919c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 19209446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 19219446ef56SAndy Whitcroft push(@av_paren_type, $type); 1922addcdceaSAndy Whitcroft $type = 'c'; 19239446ef56SAndy Whitcroft 1924e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1925c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 19266c72ffaaSAndy Whitcroft $type = 'T'; 19276c72ffaaSAndy Whitcroft 1928389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1929389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1930389a2fe5SAndy Whitcroft $type = 'T'; 1931389a2fe5SAndy Whitcroft 1932c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1933171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1934c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1935171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1936171ae1a4SAndy Whitcroft if ($2 ne '') { 1937cf655043SAndy Whitcroft $av_pending = 'N'; 1938171ae1a4SAndy Whitcroft } 1939171ae1a4SAndy Whitcroft $type = 'E'; 1940171ae1a4SAndy Whitcroft 1941c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1942171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1943171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1944171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 19456c72ffaaSAndy Whitcroft 1946c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1947cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1948c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1949cf655043SAndy Whitcroft 1950cf655043SAndy Whitcroft push(@av_paren_type, $type); 1951cf655043SAndy Whitcroft push(@av_paren_type, $type); 1952171ae1a4SAndy Whitcroft $type = 'E'; 1953cf655043SAndy Whitcroft 1954c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1955cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1956cf655043SAndy Whitcroft $av_preprocessor = 1; 1957cf655043SAndy Whitcroft 1958cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1959cf655043SAndy Whitcroft 1960171ae1a4SAndy Whitcroft $type = 'E'; 1961cf655043SAndy Whitcroft 1962c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1963cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1964cf655043SAndy Whitcroft 1965cf655043SAndy Whitcroft $av_preprocessor = 1; 1966cf655043SAndy Whitcroft 1967cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1968cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1969cf655043SAndy Whitcroft pop(@av_paren_type); 1970cf655043SAndy Whitcroft push(@av_paren_type, $type); 1971171ae1a4SAndy Whitcroft $type = 'E'; 19726c72ffaaSAndy Whitcroft 19736c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1974c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 19756c72ffaaSAndy Whitcroft 1976171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1977171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1978171ae1a4SAndy Whitcroft $av_pending = $type; 1979171ae1a4SAndy Whitcroft $type = 'N'; 1980171ae1a4SAndy Whitcroft 19816c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1982c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 19836c72ffaaSAndy Whitcroft if (defined $2) { 1984cf655043SAndy Whitcroft $av_pending = 'V'; 19856c72ffaaSAndy Whitcroft } 19866c72ffaaSAndy Whitcroft $type = 'N'; 19876c72ffaaSAndy Whitcroft 198814b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1989c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 199014b111c1SAndy Whitcroft $av_pending = 'E'; 19916c72ffaaSAndy Whitcroft $type = 'N'; 19926c72ffaaSAndy Whitcroft 19931f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 19941f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 19951f65f947SAndy Whitcroft $av_pend_colon = 'C'; 19961f65f947SAndy Whitcroft $type = 'N'; 19971f65f947SAndy Whitcroft 199814b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1999c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 20006c72ffaaSAndy Whitcroft $type = 'N'; 20016c72ffaaSAndy Whitcroft 20026c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 2003c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 2004cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 2005cf655043SAndy Whitcroft $av_pending = '_'; 20066c72ffaaSAndy Whitcroft $type = 'N'; 20076c72ffaaSAndy Whitcroft 20086c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 2009cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 2010cf655043SAndy Whitcroft if ($new_type ne '_') { 2011cf655043SAndy Whitcroft $type = $new_type; 2012c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 2013c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 20146c72ffaaSAndy Whitcroft } else { 2015c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 20166c72ffaaSAndy Whitcroft } 20176c72ffaaSAndy Whitcroft 2018c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 2019c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 2020c8cb2ca3SAndy Whitcroft $type = 'V'; 2021cf655043SAndy Whitcroft $av_pending = 'V'; 20226c72ffaaSAndy Whitcroft 20238e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 20248e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 20251f65f947SAndy Whitcroft $av_pend_colon = 'B'; 20268e761b04SAndy Whitcroft } elsif ($type eq 'E') { 20278e761b04SAndy Whitcroft $av_pend_colon = 'L'; 20281f65f947SAndy Whitcroft } 20291f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 20301f65f947SAndy Whitcroft $type = 'V'; 20311f65f947SAndy Whitcroft 20326c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 2033c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 20346c72ffaaSAndy Whitcroft $type = 'V'; 20356c72ffaaSAndy Whitcroft 20366c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 2037c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 20386c72ffaaSAndy Whitcroft $type = 'N'; 20396c72ffaaSAndy Whitcroft 2040cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 2041c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 204213214adfSAndy Whitcroft $type = 'E'; 20431f65f947SAndy Whitcroft $av_pend_colon = 'O'; 204413214adfSAndy Whitcroft 20458e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 20468e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 20478e761b04SAndy Whitcroft $type = 'C'; 20488e761b04SAndy Whitcroft 20491f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 20501f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 20511f65f947SAndy Whitcroft $type = 'N'; 20521f65f947SAndy Whitcroft 20531f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 20541f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 20551f65f947SAndy Whitcroft 20561f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 20571f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 20581f65f947SAndy Whitcroft $type = 'E'; 20591f65f947SAndy Whitcroft } else { 20601f65f947SAndy Whitcroft $type = 'N'; 20611f65f947SAndy Whitcroft } 20621f65f947SAndy Whitcroft $av_pend_colon = 'O'; 20631f65f947SAndy Whitcroft 20648e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 206513214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 20666c72ffaaSAndy Whitcroft $type = 'N'; 20676c72ffaaSAndy Whitcroft 20680d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 206974048ed8SAndy Whitcroft my $variant; 207074048ed8SAndy Whitcroft 207174048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 207274048ed8SAndy Whitcroft if ($type eq 'V') { 207374048ed8SAndy Whitcroft $variant = 'B'; 207474048ed8SAndy Whitcroft } else { 207574048ed8SAndy Whitcroft $variant = 'U'; 207674048ed8SAndy Whitcroft } 207774048ed8SAndy Whitcroft 207874048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 207974048ed8SAndy Whitcroft $type = 'N'; 208074048ed8SAndy Whitcroft 20816c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 2082c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 20836c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 20846c72ffaaSAndy Whitcroft $type = 'N'; 20856c72ffaaSAndy Whitcroft } 20866c72ffaaSAndy Whitcroft 20876c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 2088c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 20896c72ffaaSAndy Whitcroft } 20906c72ffaaSAndy Whitcroft if (defined $1) { 20916c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 20926c72ffaaSAndy Whitcroft $res .= $type x length($1); 20936c72ffaaSAndy Whitcroft } 20946c72ffaaSAndy Whitcroft } 20956c72ffaaSAndy Whitcroft 20961f65f947SAndy Whitcroft return ($res, $var); 20976c72ffaaSAndy Whitcroft} 20986c72ffaaSAndy Whitcroft 20998905a67cSAndy Whitcroftsub possible { 210013214adfSAndy Whitcroft my ($possible, $line) = @_; 21019a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 21020776e594SAndy Whitcroft ^(?: 21030776e594SAndy Whitcroft $Modifier| 21040776e594SAndy Whitcroft $Storage| 21050776e594SAndy Whitcroft $Type| 21069a974fdbSAndy Whitcroft DEFINE_\S+ 21079a974fdbSAndy Whitcroft )$| 21089a974fdbSAndy Whitcroft ^(?: 21090776e594SAndy Whitcroft goto| 21100776e594SAndy Whitcroft return| 21110776e594SAndy Whitcroft case| 21120776e594SAndy Whitcroft else| 21130776e594SAndy Whitcroft asm|__asm__| 211489a88353SAndy Whitcroft do| 211589a88353SAndy Whitcroft \#| 211689a88353SAndy Whitcroft \#\#| 21179a974fdbSAndy Whitcroft )(?:\s|$)| 21180776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 21199a974fdbSAndy Whitcroft )}x; 21209a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 21219a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2122c45dcabdSAndy Whitcroft # Check for modifiers. 2123c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2124c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2125c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2126c45dcabdSAndy Whitcroft 2127c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2128c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2129d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 21309a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2131d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2132485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2133d2506586SAndy Whitcroft } 21349a974fdbSAndy Whitcroft } 2135c45dcabdSAndy Whitcroft 2136c45dcabdSAndy Whitcroft } else { 213713214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2138485ff23eSAlex Dowad push(@typeListFile, $possible); 2139c45dcabdSAndy Whitcroft } 21408905a67cSAndy Whitcroft build_types(); 21410776e594SAndy Whitcroft } else { 21420776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 21438905a67cSAndy Whitcroft } 21448905a67cSAndy Whitcroft} 21458905a67cSAndy Whitcroft 21466c72ffaaSAndy Whitcroftmy $prefix = ''; 21476c72ffaaSAndy Whitcroft 2148000d1cc1SJoe Perchessub show_type { 2149cbec18afSJoe Perches my ($type) = @_; 215091bfe484SJoe Perches 2151522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2152522b837cSAlexey Dobriyan 2153cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2154cbec18afSJoe Perches 2155cbec18afSJoe Perches return !defined $ignore_type{$type}; 2156000d1cc1SJoe Perches} 2157000d1cc1SJoe Perches 2158f0a594c1SAndy Whitcroftsub report { 2159cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2160cbec18afSJoe Perches 2161cbec18afSJoe Perches if (!show_type($type) || 2162cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2163773647a0SAndy Whitcroft return 0; 2164773647a0SAndy Whitcroft } 216557230297SJoe Perches my $output = ''; 2166737c0767SJohn Brooks if ($color) { 216757230297SJoe Perches if ($level eq 'ERROR') { 216857230297SJoe Perches $output .= RED; 216957230297SJoe Perches } elsif ($level eq 'WARNING') { 217057230297SJoe Perches $output .= YELLOW; 2171000d1cc1SJoe Perches } else { 217257230297SJoe Perches $output .= GREEN; 2173000d1cc1SJoe Perches } 217457230297SJoe Perches } 217557230297SJoe Perches $output .= $prefix . $level . ':'; 217657230297SJoe Perches if ($show_types) { 2177737c0767SJohn Brooks $output .= BLUE if ($color); 217857230297SJoe Perches $output .= "$type:"; 217957230297SJoe Perches } 2180737c0767SJohn Brooks $output .= RESET if ($color); 218157230297SJoe Perches $output .= ' ' . $msg . "\n"; 218234d8815fSJoe Perches 218334d8815fSJoe Perches if ($showfile) { 218434d8815fSJoe Perches my @lines = split("\n", $output, -1); 218534d8815fSJoe Perches splice(@lines, 1, 1); 218634d8815fSJoe Perches $output = join("\n", @lines); 218734d8815fSJoe Perches } 218857230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 21898905a67cSAndy Whitcroft 219057230297SJoe Perches push(our @report, $output); 2191773647a0SAndy Whitcroft 2192773647a0SAndy Whitcroft return 1; 2193f0a594c1SAndy Whitcroft} 2194cbec18afSJoe Perches 2195f0a594c1SAndy Whitcroftsub report_dump { 219613214adfSAndy Whitcroft our @report; 2197f0a594c1SAndy Whitcroft} 2198000d1cc1SJoe Perches 2199d752fcc8SJoe Perchessub fixup_current_range { 2200d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2201d752fcc8SJoe Perches 2202d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2203d752fcc8SJoe Perches my $o = $1; 2204d752fcc8SJoe Perches my $l = $2; 2205d752fcc8SJoe Perches my $no = $o + $offset; 2206d752fcc8SJoe Perches my $nl = $l + $length; 2207d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2208d752fcc8SJoe Perches } 2209d752fcc8SJoe Perches} 2210d752fcc8SJoe Perches 2211d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2212d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2213d752fcc8SJoe Perches 2214d752fcc8SJoe Perches my $range_last_linenr = 0; 2215d752fcc8SJoe Perches my $delta_offset = 0; 2216d752fcc8SJoe Perches 2217d752fcc8SJoe Perches my $old_linenr = 0; 2218d752fcc8SJoe Perches my $new_linenr = 0; 2219d752fcc8SJoe Perches 2220d752fcc8SJoe Perches my $next_insert = 0; 2221d752fcc8SJoe Perches my $next_delete = 0; 2222d752fcc8SJoe Perches 2223d752fcc8SJoe Perches my @lines = (); 2224d752fcc8SJoe Perches 2225d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2226d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2227d752fcc8SJoe Perches 2228d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2229d752fcc8SJoe Perches my $save_line = 1; 2230d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2231323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2232d752fcc8SJoe Perches $delta_offset = 0; 2233d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2234d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2235d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2236d752fcc8SJoe Perches } 2237d752fcc8SJoe Perches 2238d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2239d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2240d752fcc8SJoe Perches $save_line = 0; 2241d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2242d752fcc8SJoe Perches } 2243d752fcc8SJoe Perches 2244d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2245d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2246d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2247d752fcc8SJoe Perches $new_linenr++; 2248d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2249d752fcc8SJoe Perches } 2250d752fcc8SJoe Perches 2251d752fcc8SJoe Perches if ($save_line) { 2252d752fcc8SJoe Perches push(@lines, $line); 2253d752fcc8SJoe Perches $new_linenr++; 2254d752fcc8SJoe Perches } 2255d752fcc8SJoe Perches 2256d752fcc8SJoe Perches $old_linenr++; 2257d752fcc8SJoe Perches } 2258d752fcc8SJoe Perches 2259d752fcc8SJoe Perches return @lines; 2260d752fcc8SJoe Perches} 2261d752fcc8SJoe Perches 2262f2d7e4d4SJoe Perchessub fix_insert_line { 2263f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2264f2d7e4d4SJoe Perches 2265f2d7e4d4SJoe Perches my $inserted = { 2266f2d7e4d4SJoe Perches LINENR => $linenr, 2267f2d7e4d4SJoe Perches LINE => $line, 2268f2d7e4d4SJoe Perches }; 2269f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2270f2d7e4d4SJoe Perches} 2271f2d7e4d4SJoe Perches 2272f2d7e4d4SJoe Perchessub fix_delete_line { 2273f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2274f2d7e4d4SJoe Perches 2275f2d7e4d4SJoe Perches my $deleted = { 2276f2d7e4d4SJoe Perches LINENR => $linenr, 2277f2d7e4d4SJoe Perches LINE => $line, 2278f2d7e4d4SJoe Perches }; 2279f2d7e4d4SJoe Perches 2280f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2281f2d7e4d4SJoe Perches} 2282f2d7e4d4SJoe Perches 2283de7d4f0eSAndy Whitcroftsub ERROR { 2284cbec18afSJoe Perches my ($type, $msg) = @_; 2285cbec18afSJoe Perches 2286cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2287de7d4f0eSAndy Whitcroft our $clean = 0; 22886c72ffaaSAndy Whitcroft our $cnt_error++; 22893705ce5bSJoe Perches return 1; 2290de7d4f0eSAndy Whitcroft } 22913705ce5bSJoe Perches return 0; 2292773647a0SAndy Whitcroft} 2293de7d4f0eSAndy Whitcroftsub WARN { 2294cbec18afSJoe Perches my ($type, $msg) = @_; 2295cbec18afSJoe Perches 2296cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2297de7d4f0eSAndy Whitcroft our $clean = 0; 22986c72ffaaSAndy Whitcroft our $cnt_warn++; 22993705ce5bSJoe Perches return 1; 2300de7d4f0eSAndy Whitcroft } 23013705ce5bSJoe Perches return 0; 2302773647a0SAndy Whitcroft} 2303de7d4f0eSAndy Whitcroftsub CHK { 2304cbec18afSJoe Perches my ($type, $msg) = @_; 2305cbec18afSJoe Perches 2306cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2307de7d4f0eSAndy Whitcroft our $clean = 0; 23086c72ffaaSAndy Whitcroft our $cnt_chk++; 23093705ce5bSJoe Perches return 1; 23106c72ffaaSAndy Whitcroft } 23113705ce5bSJoe Perches return 0; 2312de7d4f0eSAndy Whitcroft} 2313de7d4f0eSAndy Whitcroft 23146ecd9674SAndy Whitcroftsub check_absolute_file { 23156ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 23166ecd9674SAndy Whitcroft my $file = $absolute; 23176ecd9674SAndy Whitcroft 23186ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 23196ecd9674SAndy Whitcroft 23206ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 23216ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 23226ecd9674SAndy Whitcroft if (-f "$root/$file") { 23236ecd9674SAndy Whitcroft ##print "file<$file>\n"; 23246ecd9674SAndy Whitcroft last; 23256ecd9674SAndy Whitcroft } 23266ecd9674SAndy Whitcroft } 23276ecd9674SAndy Whitcroft if (! -f _) { 23286ecd9674SAndy Whitcroft return 0; 23296ecd9674SAndy Whitcroft } 23306ecd9674SAndy Whitcroft 23316ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 23326ecd9674SAndy Whitcroft my $prefix = $absolute; 23336ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 23346ecd9674SAndy Whitcroft 23356ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 23366ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2337000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2338000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 23396ecd9674SAndy Whitcroft } 23406ecd9674SAndy Whitcroft} 23416ecd9674SAndy Whitcroft 23423705ce5bSJoe Perchessub trim { 23433705ce5bSJoe Perches my ($string) = @_; 23443705ce5bSJoe Perches 2345b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2346b34c648bSJoe Perches 2347b34c648bSJoe Perches return $string; 2348b34c648bSJoe Perches} 2349b34c648bSJoe Perches 2350b34c648bSJoe Perchessub ltrim { 2351b34c648bSJoe Perches my ($string) = @_; 2352b34c648bSJoe Perches 2353b34c648bSJoe Perches $string =~ s/^\s+//; 2354b34c648bSJoe Perches 2355b34c648bSJoe Perches return $string; 2356b34c648bSJoe Perches} 2357b34c648bSJoe Perches 2358b34c648bSJoe Perchessub rtrim { 2359b34c648bSJoe Perches my ($string) = @_; 2360b34c648bSJoe Perches 2361b34c648bSJoe Perches $string =~ s/\s+$//; 23623705ce5bSJoe Perches 23633705ce5bSJoe Perches return $string; 23643705ce5bSJoe Perches} 23653705ce5bSJoe Perches 236652ea8506SJoe Perchessub string_find_replace { 236752ea8506SJoe Perches my ($string, $find, $replace) = @_; 236852ea8506SJoe Perches 236952ea8506SJoe Perches $string =~ s/$find/$replace/g; 237052ea8506SJoe Perches 237152ea8506SJoe Perches return $string; 237252ea8506SJoe Perches} 237352ea8506SJoe Perches 23743705ce5bSJoe Perchessub tabify { 23753705ce5bSJoe Perches my ($leading) = @_; 23763705ce5bSJoe Perches 2377713a09deSAntonio Borneo my $source_indent = $tabsize; 23783705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 23793705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 23803705ce5bSJoe Perches 23813705ce5bSJoe Perches #convert leading spaces to tabs 23823705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 23833705ce5bSJoe Perches #Remove spaces before a tab 23843705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 23853705ce5bSJoe Perches 23863705ce5bSJoe Perches return "$leading"; 23873705ce5bSJoe Perches} 23883705ce5bSJoe Perches 2389d1fe9c09SJoe Perchessub pos_last_openparen { 2390d1fe9c09SJoe Perches my ($line) = @_; 2391d1fe9c09SJoe Perches 2392d1fe9c09SJoe Perches my $pos = 0; 2393d1fe9c09SJoe Perches 2394d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2395d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2396d1fe9c09SJoe Perches 2397d1fe9c09SJoe Perches my $last_openparen = 0; 2398d1fe9c09SJoe Perches 2399d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2400d1fe9c09SJoe Perches return -1; 2401d1fe9c09SJoe Perches } 2402d1fe9c09SJoe Perches 2403d1fe9c09SJoe Perches my $len = length($line); 2404d1fe9c09SJoe Perches 2405d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2406d1fe9c09SJoe Perches my $string = substr($line, $pos); 2407d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2408d1fe9c09SJoe Perches $pos += length($1) - 1; 2409d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2410d1fe9c09SJoe Perches $last_openparen = $pos; 2411d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2412d1fe9c09SJoe Perches last; 2413d1fe9c09SJoe Perches } 2414d1fe9c09SJoe Perches } 2415d1fe9c09SJoe Perches 241691cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2417d1fe9c09SJoe Perches} 2418d1fe9c09SJoe Perches 2419f36d3eb8SJoe Perchessub get_raw_comment { 2420f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2421f36d3eb8SJoe Perches my $comment = ''; 2422f36d3eb8SJoe Perches 2423f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2424f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2425f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2426f36d3eb8SJoe Perches } 2427f36d3eb8SJoe Perches } 2428f36d3eb8SJoe Perches 2429f36d3eb8SJoe Perches return $comment; 2430f36d3eb8SJoe Perches} 2431f36d3eb8SJoe Perches 24320a920b5bSAndy Whitcroftsub process { 24330a920b5bSAndy Whitcroft my $filename = shift; 24340a920b5bSAndy Whitcroft 24350a920b5bSAndy Whitcroft my $linenr=0; 24360a920b5bSAndy Whitcroft my $prevline=""; 2437c2fdda0dSAndy Whitcroft my $prevrawline=""; 24380a920b5bSAndy Whitcroft my $stashline=""; 2439c2fdda0dSAndy Whitcroft my $stashrawline=""; 24400a920b5bSAndy Whitcroft 24414a0df2efSAndy Whitcroft my $length; 24420a920b5bSAndy Whitcroft my $indent; 24430a920b5bSAndy Whitcroft my $previndent=0; 24440a920b5bSAndy Whitcroft my $stashindent=0; 24450a920b5bSAndy Whitcroft 2446de7d4f0eSAndy Whitcroft our $clean = 1; 24470a920b5bSAndy Whitcroft my $signoff = 0; 2448cd261496SGeert Uytterhoeven my $author = ''; 2449cd261496SGeert Uytterhoeven my $authorsignoff = 0; 245048ca2d8aSDwaipayan Ray my $author_sob = ''; 24510a920b5bSAndy Whitcroft my $is_patch = 0; 2452133712a2SRob Herring my $is_binding_patch = -1; 245329ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 245415662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 245544d303ebSJoe Perches my $has_patch_separator = 0; #Found a --- line 2456ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2457490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2458bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 24592a076f40SJoe Perches my $commit_log_long_line = 0; 2460e518e9a5SJoe Perches my $commit_log_has_diff = 0; 246113f1937eSJoe Perches my $reported_maintainer_file = 0; 2462fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2463fa64205dSPasi Savanainen 2464365dd4eaSJoe Perches my $last_blank_line = 0; 24655e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2466365dd4eaSJoe Perches 246713214adfSAndy Whitcroft our @report = (); 24686c72ffaaSAndy Whitcroft our $cnt_lines = 0; 24696c72ffaaSAndy Whitcroft our $cnt_error = 0; 24706c72ffaaSAndy Whitcroft our $cnt_warn = 0; 24716c72ffaaSAndy Whitcroft our $cnt_chk = 0; 24726c72ffaaSAndy Whitcroft 24730a920b5bSAndy Whitcroft # Trace the real file/line as we go. 24740a920b5bSAndy Whitcroft my $realfile = ''; 24750a920b5bSAndy Whitcroft my $realline = 0; 24760a920b5bSAndy Whitcroft my $realcnt = 0; 24770a920b5bSAndy Whitcroft my $here = ''; 247877cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 24790a920b5bSAndy Whitcroft my $in_comment = 0; 2480c2fdda0dSAndy Whitcroft my $comment_edge = 0; 24810a920b5bSAndy Whitcroft my $first_line = 0; 24821e855726SWolfram Sang my $p1_prefix = ''; 24830a920b5bSAndy Whitcroft 248413214adfSAndy Whitcroft my $prev_values = 'E'; 248513214adfSAndy Whitcroft 248613214adfSAndy Whitcroft # suppression flags 2487773647a0SAndy Whitcroft my %suppress_ifbraces; 2488170d3a22SAndy Whitcroft my %suppress_whiletrailers; 24892b474a1aSAndy Whitcroft my %suppress_export; 24903e469cdcSAndy Whitcroft my $suppress_statement = 0; 2491653d4876SAndy Whitcroft 24927e51f197SJoe Perches my %signatures = (); 2493323c1260SJoe Perches 2494c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2495de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2496c2fdda0dSAndy Whitcroft # 2497de7d4f0eSAndy Whitcroft my @setup_docs = (); 2498de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2499773647a0SAndy Whitcroft 2500d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2501d8b07710SJoe Perches 25029f3a8992SRob Herring my $checklicenseline = 1; 25039f3a8992SRob Herring 2504773647a0SAndy Whitcroft sanitise_line_reset(); 2505c2fdda0dSAndy Whitcroft my $line; 2506c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2507773647a0SAndy Whitcroft $linenr++; 2508773647a0SAndy Whitcroft $line = $rawline; 2509c2fdda0dSAndy Whitcroft 25103705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 25113705ce5bSJoe Perches 2512773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2513de7d4f0eSAndy Whitcroft $setup_docs = 0; 25142581ac7cSTim Froidcoeur if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { 2515de7d4f0eSAndy Whitcroft $setup_docs = 1; 2516de7d4f0eSAndy Whitcroft } 2517773647a0SAndy Whitcroft #next; 2518de7d4f0eSAndy Whitcroft } 251974fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2520773647a0SAndy Whitcroft $realline=$1-1; 2521773647a0SAndy Whitcroft if (defined $2) { 2522773647a0SAndy Whitcroft $realcnt=$3+1; 2523773647a0SAndy Whitcroft } else { 2524773647a0SAndy Whitcroft $realcnt=1+1; 2525773647a0SAndy Whitcroft } 2526c45dcabdSAndy Whitcroft $in_comment = 0; 2527773647a0SAndy Whitcroft 2528773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2529773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2530773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2531773647a0SAndy Whitcroft # at context start. 2532773647a0SAndy Whitcroft my $edge; 253301fa9147SAndy Whitcroft my $cnt = $realcnt; 253401fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 253501fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 253601fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 253701fa9147SAndy Whitcroft $cnt--; 253801fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2539721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2540fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2541fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2542fae17daeSAndy Whitcroft ($edge) = $1; 2543fae17daeSAndy Whitcroft last; 2544fae17daeSAndy Whitcroft } 2545773647a0SAndy Whitcroft } 2546773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2547773647a0SAndy Whitcroft $in_comment = 1; 2548773647a0SAndy Whitcroft } 2549773647a0SAndy Whitcroft 2550773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2551773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2552773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2553773647a0SAndy Whitcroft if (!defined $edge && 255483242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2555773647a0SAndy Whitcroft { 2556773647a0SAndy Whitcroft $in_comment = 1; 2557773647a0SAndy Whitcroft } 2558773647a0SAndy Whitcroft 2559773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2560773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2561773647a0SAndy Whitcroft 2562171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2563773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2564171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2565773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2566773647a0SAndy Whitcroft } 2567773647a0SAndy Whitcroft push(@lines, $line); 2568773647a0SAndy Whitcroft 2569773647a0SAndy Whitcroft if ($realcnt > 1) { 2570773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2571773647a0SAndy Whitcroft } else { 2572773647a0SAndy Whitcroft $realcnt = 0; 2573773647a0SAndy Whitcroft } 2574773647a0SAndy Whitcroft 2575773647a0SAndy Whitcroft #print "==>$rawline\n"; 2576773647a0SAndy Whitcroft #print "-->$line\n"; 2577de7d4f0eSAndy Whitcroft 2578de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2579de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2580de7d4f0eSAndy Whitcroft } 2581de7d4f0eSAndy Whitcroft } 2582de7d4f0eSAndy Whitcroft 25836c72ffaaSAndy Whitcroft $prefix = ''; 25846c72ffaaSAndy Whitcroft 2585773647a0SAndy Whitcroft $realcnt = 0; 2586773647a0SAndy Whitcroft $linenr = 0; 2587194f66fcSJoe Perches $fixlinenr = -1; 25880a920b5bSAndy Whitcroft foreach my $line (@lines) { 25890a920b5bSAndy Whitcroft $linenr++; 2590194f66fcSJoe Perches $fixlinenr++; 25911b5539b1SJoe Perches my $sline = $line; #copy of $line 25921b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 25930a920b5bSAndy Whitcroft 2594c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2595f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 25966c72ffaaSAndy Whitcroft 259712c253abSJoe Perches# check if it's a mode change, rename or start of a patch 259812c253abSJoe Perches if (!$in_commit_log && 259912c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 260012c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 260112c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 260212c253abSJoe Perches $is_patch = 1; 260312c253abSJoe Perches } 260412c253abSJoe Perches 26050a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2606e518e9a5SJoe Perches if (!$in_commit_log && 260774fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 260874fd4f34SJoe Perches my $context = $4; 26090a920b5bSAndy Whitcroft $is_patch = 1; 26104a0df2efSAndy Whitcroft $first_line = $linenr + 1; 26110a920b5bSAndy Whitcroft $realline=$1-1; 26120a920b5bSAndy Whitcroft if (defined $2) { 26130a920b5bSAndy Whitcroft $realcnt=$3+1; 26140a920b5bSAndy Whitcroft } else { 26150a920b5bSAndy Whitcroft $realcnt=1+1; 26160a920b5bSAndy Whitcroft } 2617c2fdda0dSAndy Whitcroft annotate_reset(); 261813214adfSAndy Whitcroft $prev_values = 'E'; 261913214adfSAndy Whitcroft 2620773647a0SAndy Whitcroft %suppress_ifbraces = (); 2621170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 26222b474a1aSAndy Whitcroft %suppress_export = (); 26233e469cdcSAndy Whitcroft $suppress_statement = 0; 262474fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 262574fd4f34SJoe Perches $context_function = $1; 262674fd4f34SJoe Perches } else { 262774fd4f34SJoe Perches undef $context_function; 262874fd4f34SJoe Perches } 26290a920b5bSAndy Whitcroft next; 26300a920b5bSAndy Whitcroft 26314a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 26324a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 26334a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2634773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 26350a920b5bSAndy Whitcroft $realline++; 2636d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 26370a920b5bSAndy Whitcroft 26384a0df2efSAndy Whitcroft # Measure the line length and indent. 2639c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 26400a920b5bSAndy Whitcroft 26410a920b5bSAndy Whitcroft # Track the previous line. 26420a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 26430a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2644c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2645c2fdda0dSAndy Whitcroft 2646773647a0SAndy Whitcroft #warn "line<$line>\n"; 26476c72ffaaSAndy Whitcroft 2648d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2649d8aaf121SAndy Whitcroft $realcnt--; 26500a920b5bSAndy Whitcroft } 26510a920b5bSAndy Whitcroft 2652cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2653cc77cdcaSAndy Whitcroft 26546c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 26556c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2656773647a0SAndy Whitcroft 26572ac73b4fSJoe Perches my $found_file = 0; 2658773647a0SAndy Whitcroft # extract the filename as it passes 26593bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 26603bf9a009SRabin Vincent $realfile = $1; 26612b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2662270c49a0SJoe Perches $in_commit_log = 0; 26632ac73b4fSJoe Perches $found_file = 1; 26643bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2665773647a0SAndy Whitcroft $realfile = $1; 26662b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2667270c49a0SJoe Perches $in_commit_log = 0; 26681e855726SWolfram Sang 26691e855726SWolfram Sang $p1_prefix = $1; 2670e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2671e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2672000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2673000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 26741e855726SWolfram Sang } 2675773647a0SAndy Whitcroft 2676c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2677000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2678000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2679773647a0SAndy Whitcroft } 26802ac73b4fSJoe Perches $found_file = 1; 26812ac73b4fSJoe Perches } 26822ac73b4fSJoe Perches 268334d8815fSJoe Perches#make up the handle for any error we report on this line 268434d8815fSJoe Perches if ($showfile) { 268534d8815fSJoe Perches $prefix = "$realfile:$realline: " 268634d8815fSJoe Perches } elsif ($emacs) { 26877d3a9f67SJoe Perches if ($file) { 26887d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 26897d3a9f67SJoe Perches } else { 269034d8815fSJoe Perches $prefix = "$filename:$linenr: "; 269134d8815fSJoe Perches } 26927d3a9f67SJoe Perches } 269334d8815fSJoe Perches 26942ac73b4fSJoe Perches if ($found_file) { 269585b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 269685b0ee18SJoe Perches WARN("OBSOLETE", 269785b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 269885b0ee18SJoe Perches } 26997bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 27002ac73b4fSJoe Perches $check = 1; 27012ac73b4fSJoe Perches } else { 27022ac73b4fSJoe Perches $check = $check_orig; 27032ac73b4fSJoe Perches } 27049f3a8992SRob Herring $checklicenseline = 1; 2705133712a2SRob Herring 2706133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2707133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2708133712a2SRob Herring 2709133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2710133712a2SRob Herring 2711133712a2SRob Herring if (($last_binding_patch != -1) && 2712133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2713133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2714858e6845SMauro Carvalho Chehab "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); 2715133712a2SRob Herring } 2716133712a2SRob Herring } 2717133712a2SRob Herring 2718773647a0SAndy Whitcroft next; 2719773647a0SAndy Whitcroft } 2720773647a0SAndy Whitcroft 2721389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 27220a920b5bSAndy Whitcroft 2723c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2724c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2725c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 27260a920b5bSAndy Whitcroft 27276c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 27286c72ffaaSAndy Whitcroft 2729490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2730490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2731490b292cSJoe Perches if ($in_commit_log) { 2732490b292cSJoe Perches if ($line !~ /^\s*$/) { 2733490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2734490b292cSJoe Perches } 2735490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2736490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2737490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2738490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2739490b292cSJoe Perches } 2740490b292cSJoe Perches 2741e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2742e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 274313e45417SMrinal Pandey (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && 274413e45417SMrinal Pandey $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || 2745e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2746e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2747e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2748e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2749e518e9a5SJoe Perches $commit_log_has_diff = 1; 2750e518e9a5SJoe Perches } 2751e518e9a5SJoe Perches 27523bf9a009SRabin Vincent# Check for incorrect file permissions 27533bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 27543bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 275504db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 275604db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2757000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2758000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 27593bf9a009SRabin Vincent } 27603bf9a009SRabin Vincent } 27613bf9a009SRabin Vincent 2762cd261496SGeert Uytterhoeven# Check the patch for a From: 2763cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2764cd261496SGeert Uytterhoeven $author = $1; 2765e7f929f3SDwaipayan Ray my $curline = $linenr; 2766e7f929f3SDwaipayan Ray while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { 2767e7f929f3SDwaipayan Ray $author .= $1; 2768e7f929f3SDwaipayan Ray } 2769cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2770cd261496SGeert Uytterhoeven $author =~ s/"//g; 2771dfa05c28SJoe Perches $author = reformat_email($author); 2772cd261496SGeert Uytterhoeven } 2773cd261496SGeert Uytterhoeven 277420112475SJoe Perches# Check the patch for a signoff: 2775dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 27764a0df2efSAndy Whitcroft $signoff++; 277715662b3eSJoe Perches $in_commit_log = 0; 277848ca2d8aSDwaipayan Ray if ($author ne '' && $authorsignoff != 1) { 2779fccaebf0SDwaipayan Ray if (same_email_addresses($1, $author)) { 2780cd261496SGeert Uytterhoeven $authorsignoff = 1; 278148ca2d8aSDwaipayan Ray } else { 278248ca2d8aSDwaipayan Ray my $ctx = $1; 278348ca2d8aSDwaipayan Ray my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); 278448ca2d8aSDwaipayan Ray my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); 278548ca2d8aSDwaipayan Ray 278648ca2d8aSDwaipayan Ray if ($email_address eq $author_address && $email_name eq $author_name) { 278748ca2d8aSDwaipayan Ray $author_sob = $ctx; 278848ca2d8aSDwaipayan Ray $authorsignoff = 2; 278948ca2d8aSDwaipayan Ray } elsif ($email_address eq $author_address) { 279048ca2d8aSDwaipayan Ray $author_sob = $ctx; 279148ca2d8aSDwaipayan Ray $authorsignoff = 3; 279248ca2d8aSDwaipayan Ray } elsif ($email_name eq $author_name) { 279348ca2d8aSDwaipayan Ray $author_sob = $ctx; 279448ca2d8aSDwaipayan Ray $authorsignoff = 4; 279548ca2d8aSDwaipayan Ray 279648ca2d8aSDwaipayan Ray my $address1 = $email_address; 279748ca2d8aSDwaipayan Ray my $address2 = $author_address; 279848ca2d8aSDwaipayan Ray 279948ca2d8aSDwaipayan Ray if ($address1 =~ /(\S+)\+\S+(\@.*)/) { 280048ca2d8aSDwaipayan Ray $address1 = "$1$2"; 280148ca2d8aSDwaipayan Ray } 280248ca2d8aSDwaipayan Ray if ($address2 =~ /(\S+)\+\S+(\@.*)/) { 280348ca2d8aSDwaipayan Ray $address2 = "$1$2"; 280448ca2d8aSDwaipayan Ray } 280548ca2d8aSDwaipayan Ray if ($address1 eq $address2) { 280648ca2d8aSDwaipayan Ray $authorsignoff = 5; 280748ca2d8aSDwaipayan Ray } 280848ca2d8aSDwaipayan Ray } 2809cd261496SGeert Uytterhoeven } 2810cd261496SGeert Uytterhoeven } 28110a920b5bSAndy Whitcroft } 281220112475SJoe Perches 281344d303ebSJoe Perches# Check for patch separator 281444d303ebSJoe Perches if ($line =~ /^---$/) { 281544d303ebSJoe Perches $has_patch_separator = 1; 281644d303ebSJoe Perches $in_commit_log = 0; 281744d303ebSJoe Perches } 281844d303ebSJoe Perches 2819e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2820e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2821e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2822e0d975b1SJoe Perches $reported_maintainer_file = 1; 2823e0d975b1SJoe Perches } 2824e0d975b1SJoe Perches 282520112475SJoe Perches# Check signature styles 2826270c49a0SJoe Perches if (!$in_header_lines && 2827ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 282820112475SJoe Perches my $space_before = $1; 282920112475SJoe Perches my $sign_off = $2; 283020112475SJoe Perches my $space_after = $3; 283120112475SJoe Perches my $email = $4; 283220112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 283320112475SJoe Perches 2834ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2835831242abSAditya Srivastava my $suggested_signature = find_standard_signature($sign_off); 2836831242abSAditya Srivastava if ($suggested_signature eq "") { 2837ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2838ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2839831242abSAditya Srivastava } else { 2840831242abSAditya Srivastava if (WARN("BAD_SIGN_OFF", 2841831242abSAditya Srivastava "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) && 2842831242abSAditya Srivastava $fix) { 2843831242abSAditya Srivastava $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/; 2844831242abSAditya Srivastava } 2845831242abSAditya Srivastava } 2846ce0338dfSJoe Perches } 284720112475SJoe Perches if (defined $space_before && $space_before ne "") { 28483705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 28493705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 28503705ce5bSJoe Perches $fix) { 2851194f66fcSJoe Perches $fixed[$fixlinenr] = 28523705ce5bSJoe Perches "$ucfirst_sign_off $email"; 28533705ce5bSJoe Perches } 285420112475SJoe Perches } 285520112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 28563705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 28573705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 28583705ce5bSJoe Perches $fix) { 2859194f66fcSJoe Perches $fixed[$fixlinenr] = 28603705ce5bSJoe Perches "$ucfirst_sign_off $email"; 28613705ce5bSJoe Perches } 28623705ce5bSJoe Perches 286320112475SJoe Perches } 286420112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 28653705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 28663705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 28673705ce5bSJoe Perches $fix) { 2868194f66fcSJoe Perches $fixed[$fixlinenr] = 28693705ce5bSJoe Perches "$ucfirst_sign_off $email"; 28703705ce5bSJoe Perches } 287120112475SJoe Perches } 287220112475SJoe Perches 2873dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 287448ca2d8aSDwaipayan Ray my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); 287520112475SJoe Perches if ($suggested_email eq "") { 2876000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2877000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 287820112475SJoe Perches } else { 287920112475SJoe Perches my $dequoted = $suggested_email; 288020112475SJoe Perches $dequoted =~ s/^"//; 288120112475SJoe Perches $dequoted =~ s/" </ </; 288220112475SJoe Perches # Don't force email to have quotes 288320112475SJoe Perches # Allow just an angle bracketed address 2884fccaebf0SDwaipayan Ray if (!same_email_addresses($email, $suggested_email)) { 2885fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 2886fccaebf0SDwaipayan Ray "email address '$email' might be better as '$suggested_email'\n" . $herecurr) && 2887fccaebf0SDwaipayan Ray $fix) { 2888fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/; 2889fccaebf0SDwaipayan Ray } 2890fccaebf0SDwaipayan Ray } 2891fccaebf0SDwaipayan Ray 2892fccaebf0SDwaipayan Ray # Address part shouldn't have comments 2893fccaebf0SDwaipayan Ray my $stripped_address = $email_address; 2894fccaebf0SDwaipayan Ray $stripped_address =~ s/\([^\(\)]*\)//g; 2895fccaebf0SDwaipayan Ray if ($email_address ne $stripped_address) { 2896fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 2897fccaebf0SDwaipayan Ray "address part of email should not have comments: '$email_address'\n" . $herecurr) && 2898fccaebf0SDwaipayan Ray $fix) { 2899fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/; 2900fccaebf0SDwaipayan Ray } 2901fccaebf0SDwaipayan Ray } 2902fccaebf0SDwaipayan Ray 2903fccaebf0SDwaipayan Ray # Only one name comment should be allowed 2904fccaebf0SDwaipayan Ray my $comment_count = () = $name_comment =~ /\([^\)]+\)/g; 2905fccaebf0SDwaipayan Ray if ($comment_count > 1) { 2906000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2907fccaebf0SDwaipayan Ray "Use a single name comment in email: '$email'\n" . $herecurr); 2908fccaebf0SDwaipayan Ray } 2909fccaebf0SDwaipayan Ray 2910fccaebf0SDwaipayan Ray 2911fccaebf0SDwaipayan Ray # [email protected] or [email protected] shouldn't 2912e73d2715SDwaipayan Ray # have an email name. In addition comments should strictly 2913fccaebf0SDwaipayan Ray # begin with a # 2914fccaebf0SDwaipayan Ray if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) { 2915fccaebf0SDwaipayan Ray if (($comment ne "" && $comment !~ /^#.+/) || 2916fccaebf0SDwaipayan Ray ($email_name ne "")) { 2917fccaebf0SDwaipayan Ray my $cur_name = $email_name; 2918fccaebf0SDwaipayan Ray my $new_comment = $comment; 2919fccaebf0SDwaipayan Ray $cur_name =~ s/[a-zA-Z\s\-\"]+//g; 2920fccaebf0SDwaipayan Ray 2921fccaebf0SDwaipayan Ray # Remove brackets enclosing comment text 2922fccaebf0SDwaipayan Ray # and # from start of comments to get comment text 2923fccaebf0SDwaipayan Ray $new_comment =~ s/^\((.*)\)$/$1/; 2924fccaebf0SDwaipayan Ray $new_comment =~ s/^\[(.*)\]$/$1/; 2925fccaebf0SDwaipayan Ray $new_comment =~ s/^[\s\#]+|\s+$//g; 2926fccaebf0SDwaipayan Ray 2927fccaebf0SDwaipayan Ray $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment); 2928fccaebf0SDwaipayan Ray $new_comment = " # $new_comment" if ($new_comment ne ""); 2929fccaebf0SDwaipayan Ray my $new_email = "$email_address$new_comment"; 2930fccaebf0SDwaipayan Ray 2931fccaebf0SDwaipayan Ray if (WARN("BAD_STABLE_ADDRESS_STYLE", 2932fccaebf0SDwaipayan Ray "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) && 2933fccaebf0SDwaipayan Ray $fix) { 2934fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 2935fccaebf0SDwaipayan Ray } 2936fccaebf0SDwaipayan Ray } 2937fccaebf0SDwaipayan Ray } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) { 2938fccaebf0SDwaipayan Ray my $new_comment = $comment; 2939fccaebf0SDwaipayan Ray 2940fccaebf0SDwaipayan Ray # Extract comment text from within brackets or 2941fccaebf0SDwaipayan Ray # c89 style /*...*/ comments 2942fccaebf0SDwaipayan Ray $new_comment =~ s/^\[(.*)\]$/$1/; 2943fccaebf0SDwaipayan Ray $new_comment =~ s/^\/\*(.*)\*\/$/$1/; 2944fccaebf0SDwaipayan Ray 2945fccaebf0SDwaipayan Ray $new_comment = trim($new_comment); 2946fccaebf0SDwaipayan Ray $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo 2947fccaebf0SDwaipayan Ray $new_comment = "($new_comment)" if ($new_comment ne ""); 2948fccaebf0SDwaipayan Ray my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment); 2949fccaebf0SDwaipayan Ray 2950fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 2951fccaebf0SDwaipayan Ray "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) && 2952fccaebf0SDwaipayan Ray $fix) { 2953fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 2954fccaebf0SDwaipayan Ray } 295520112475SJoe Perches } 29560a920b5bSAndy Whitcroft } 29577e51f197SJoe Perches 29587e51f197SJoe Perches# Check for duplicate signatures 29597e51f197SJoe Perches my $sig_nospace = $line; 29607e51f197SJoe Perches $sig_nospace =~ s/\s//g; 29617e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 29627e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 29637e51f197SJoe Perches WARN("BAD_SIGN_OFF", 29647e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 29657e51f197SJoe Perches } else { 29667e51f197SJoe Perches $signatures{$sig_nospace} = 1; 29677e51f197SJoe Perches } 29686c5d24eeSSean Christopherson 29696c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 29706c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 29716c5d24eeSSean Christopherson if ($email eq $author) { 29726c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29736c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 29746c5d24eeSSean Christopherson } 29756c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 29766c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29776c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 29786c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 29796c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29806c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 29816c5d24eeSSean Christopherson } elsif ($1 ne $email) { 29826c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29836c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 29846c5d24eeSSean Christopherson } 29856c5d24eeSSean Christopherson } 29860a920b5bSAndy Whitcroft } 29870a920b5bSAndy Whitcroft 2988a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2989a2fe16b9SJoe Perches if ($in_header_lines && 2990a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2991a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2992a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2993a2fe16b9SJoe Perches } 2994a2fe16b9SJoe Perches 299544d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context 299644d303ebSJoe Perches if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 29977580c5b9SAditya Srivastava if (ERROR("GERRIT_CHANGE_ID", 29987580c5b9SAditya Srivastava "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) && 29997580c5b9SAditya Srivastava $fix) { 30007580c5b9SAditya Srivastava fix_delete_line($fixlinenr, $rawline); 30017580c5b9SAditya Srivastava } 30027ebd05efSChristopher Covington } 30037ebd05efSChristopher Covington 3004369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 3005369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 3006369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 3007369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 3008369c8dd3SJoe Perches # timestamp 3009634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 3010634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 3011634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 3012634cffccSJoe Perches # stack dump address styles 3013369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 3014369c8dd3SJoe Perches } 3015369c8dd3SJoe Perches 30162a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 30172a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 3018bf4daf12SJoe Perches length($line) > 75 && 3019bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 3020bf4daf12SJoe Perches # file delta changes 3021bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 3022bf4daf12SJoe Perches # filename then : 302327b379afSAditya Srivastava $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i || 302427b379afSAditya Srivastava # A Fixes: or Link: line or signature tag line 3025bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 30262a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 30272a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 30282a076f40SJoe Perches $commit_log_long_line = 1; 30292a076f40SJoe Perches } 30302a076f40SJoe Perches 3031bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 3032bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 3033bf4daf12SJoe Perches $line =~ /^\s*$/) { 3034bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 3035bf4daf12SJoe Perches } 3036bf4daf12SJoe Perches 3037084a617aSDwaipayan Ray# Check for lines starting with a # 3038084a617aSDwaipayan Ray if ($in_commit_log && $line =~ /^#/) { 3039084a617aSDwaipayan Ray if (WARN("COMMIT_COMMENT_SYMBOL", 3040084a617aSDwaipayan Ray "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) && 3041084a617aSDwaipayan Ray $fix) { 3042084a617aSDwaipayan Ray $fixed[$fixlinenr] =~ s/^/ /; 3043084a617aSDwaipayan Ray } 3044084a617aSDwaipayan Ray } 3045084a617aSDwaipayan Ray 30460d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 3047369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 3048a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 3049e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 3050fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 3051aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 3052369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 3053bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 3054fe043ea1SJoe Perches my $init_char = "c"; 3055fe043ea1SJoe Perches my $orig_commit = ""; 30560d7835fcSJoe Perches my $short = 1; 30570d7835fcSJoe Perches my $long = 0; 30580d7835fcSJoe Perches my $case = 1; 30590d7835fcSJoe Perches my $space = 1; 30600d7835fcSJoe Perches my $hasdesc = 0; 306119c146a6SJoe Perches my $hasparens = 0; 30620d7835fcSJoe Perches my $id = '0123456789ab'; 30630d7835fcSJoe Perches my $orig_desc = "commit description"; 30640d7835fcSJoe Perches my $description = ""; 30650d7835fcSJoe Perches 3066fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 3067fe043ea1SJoe Perches $init_char = $1; 3068fe043ea1SJoe Perches $orig_commit = lc($2); 3069fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 3070fe043ea1SJoe Perches $orig_commit = lc($1); 3071fe043ea1SJoe Perches } 3072fe043ea1SJoe Perches 30730d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 30740d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 30750d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 30760d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 30770d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 30780d7835fcSJoe Perches $orig_desc = $1; 307919c146a6SJoe Perches $hasparens = 1; 30800d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 30810d7835fcSJoe Perches defined $rawlines[$linenr] && 30820d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 30830d7835fcSJoe Perches $orig_desc = $1; 308419c146a6SJoe Perches $hasparens = 1; 3085b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 3086b671fde0SJoe Perches defined $rawlines[$linenr] && 3087b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 3088b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 3089b671fde0SJoe Perches $orig_desc = $1; 3090b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 3091b671fde0SJoe Perches $orig_desc .= " " . $1; 309219c146a6SJoe Perches $hasparens = 1; 30930d7835fcSJoe Perches } 30940d7835fcSJoe Perches 30950d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 30960d7835fcSJoe Perches $id, $orig_desc); 30970d7835fcSJoe Perches 3098948b133aSHeinrich Schuchardt if (defined($id) && 3099948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 3100d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 31010d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 31020d7835fcSJoe Perches } 3103d311cd44SJoe Perches } 3104d311cd44SJoe Perches 310513f1937eSJoe Perches# Check for added, moved or deleted files 310613f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 310713f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 310813f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 310913f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 311013f1937eSJoe Perches (defined($1) || defined($2))))) { 3111a82603a8SAndrew Jeffery $is_patch = 1; 311213f1937eSJoe Perches $reported_maintainer_file = 1; 311313f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 311413f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 311513f1937eSJoe Perches } 311613f1937eSJoe Perches 3117e400edb1SRob Herring# Check for adding new DT bindings not in schema format 3118e400edb1SRob Herring if (!$in_commit_log && 3119e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 3120e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 3121e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 3122e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 3123e400edb1SRob Herring } 3124e400edb1SRob Herring 312500df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 31268905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 3127000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 3128000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 31296c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 3130de7d4f0eSAndy Whitcroft } 3131de7d4f0eSAndy Whitcroft 3132de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 3133de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 3134171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 3135171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 3136171ae1a4SAndy Whitcroft 3137171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 3138171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 3139171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 3140171ae1a4SAndy Whitcroft 314134d99219SJoe Perches CHK("INVALID_UTF8", 3142000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 314300df344fSAndy Whitcroft } 31440a920b5bSAndy Whitcroft 314515662b3eSJoe Perches# Check if it's the start of a commit log 314615662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 314715662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 3148eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 3149eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 315015662b3eSJoe Perches $in_header_lines = 0; 315115662b3eSJoe Perches $in_commit_log = 1; 3152ed43c4e5SAllen Hubbe $has_commit_log = 1; 315315662b3eSJoe Perches } 315415662b3eSJoe Perches 3155fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 3156fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 3157fa64205dSPasi Savanainen if ($in_header_lines && 3158fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 3159fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 3160fa64205dSPasi Savanainen $non_utf8_charset = 1; 3161fa64205dSPasi Savanainen } 3162fa64205dSPasi Savanainen 3163fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 316415662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 3165fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 316615662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 316715662b3eSJoe Perches } 316815662b3eSJoe Perches 3169d6430f71SJoe Perches# Check for absolute kernel paths in commit message 3170d6430f71SJoe Perches if ($tree && $in_commit_log) { 3171d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 3172d6430f71SJoe Perches my $file = $1; 3173d6430f71SJoe Perches 3174d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 3175d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 3176d6430f71SJoe Perches # 3177d6430f71SJoe Perches } else { 3178d6430f71SJoe Perches check_absolute_file($file, $herecurr); 3179d6430f71SJoe Perches } 3180d6430f71SJoe Perches } 3181d6430f71SJoe Perches } 3182d6430f71SJoe Perches 318366b47b4aSKees Cook# Check for various typo / spelling mistakes 318466d7a382SJoe Perches if (defined($misspellings) && 318566d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 31867da07c31SDwaipayan Ray while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) { 318766b47b4aSKees Cook my $typo = $1; 31887da07c31SDwaipayan Ray my $blank = copy_spacing($rawline); 31897da07c31SDwaipayan Ray my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo); 31907da07c31SDwaipayan Ray my $hereptr = "$hereline$ptr\n"; 319166b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 319266b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 319366b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 31940675a8fbSJean Delvare my $msg_level = \&WARN; 31950675a8fbSJean Delvare $msg_level = \&CHK if ($file); 31960675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 31977da07c31SDwaipayan Ray "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) && 319866b47b4aSKees Cook $fix) { 319966b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 320066b47b4aSKees Cook } 320166b47b4aSKees Cook } 320266b47b4aSKees Cook } 320366b47b4aSKees Cook 3204a8dd86bfSMatteo Croce# check for invalid commit id 3205a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 3206a8dd86bfSMatteo Croce my $id; 3207a8dd86bfSMatteo Croce my $description; 3208a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 3209a8dd86bfSMatteo Croce if (!defined($id)) { 3210a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 3211a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 3212a8dd86bfSMatteo Croce } 3213a8dd86bfSMatteo Croce } 3214a8dd86bfSMatteo Croce 3215310cd06bSJoe Perches# check for repeated words separated by a single space 32168d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root' 32178d0325ccSAditya Srivastava if (($rawline =~ /^\+/ || $in_commit_log) && 32188d0325ccSAditya Srivastava $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) { 32191db81a68SDwaipayan Ray pos($rawline) = 1 if (!$in_commit_log); 3220310cd06bSJoe Perches while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { 3221310cd06bSJoe Perches 3222310cd06bSJoe Perches my $first = $1; 3223310cd06bSJoe Perches my $second = $2; 32241db81a68SDwaipayan Ray my $start_pos = $-[1]; 32251db81a68SDwaipayan Ray my $end_pos = $+[2]; 3226310cd06bSJoe Perches if ($first =~ /(?:struct|union|enum)/) { 3227310cd06bSJoe Perches pos($rawline) += length($first) + length($second) + 1; 3228310cd06bSJoe Perches next; 3229310cd06bSJoe Perches } 3230310cd06bSJoe Perches 32311db81a68SDwaipayan Ray next if (lc($first) ne lc($second)); 3232310cd06bSJoe Perches next if ($first eq 'long'); 3233310cd06bSJoe Perches 32341db81a68SDwaipayan Ray # check for character before and after the word matches 32351db81a68SDwaipayan Ray my $start_char = ''; 32361db81a68SDwaipayan Ray my $end_char = ''; 32371db81a68SDwaipayan Ray $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1)); 32381db81a68SDwaipayan Ray $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline)); 32391db81a68SDwaipayan Ray 32401db81a68SDwaipayan Ray next if ($start_char =~ /^\S$/); 32411db81a68SDwaipayan Ray next if (index(" \t.,;?!", $end_char) == -1); 32421db81a68SDwaipayan Ray 32438d0325ccSAditya Srivastava # avoid repeating hex occurrences like 'ff ff fe 09 ...' 32448d0325ccSAditya Srivastava if ($first =~ /\b[0-9a-f]{2,}\b/i) { 32458d0325ccSAditya Srivastava next if (!exists($allow_repeated_words{lc($first)})); 32468d0325ccSAditya Srivastava } 32478d0325ccSAditya Srivastava 3248310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3249310cd06bSJoe Perches "Possible repeated word: '$first'\n" . $herecurr) && 3250310cd06bSJoe Perches $fix) { 3251310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; 3252310cd06bSJoe Perches } 3253310cd06bSJoe Perches } 3254310cd06bSJoe Perches 3255310cd06bSJoe Perches # if it's a repeated word on consecutive lines in a comment block 3256310cd06bSJoe Perches if ($prevline =~ /$;+\s*$/ && 3257310cd06bSJoe Perches $prevrawline =~ /($word_pattern)\s*$/) { 3258310cd06bSJoe Perches my $last_word = $1; 3259310cd06bSJoe Perches if ($rawline =~ /^\+\s*\*\s*$last_word /) { 3260310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3261310cd06bSJoe Perches "Possible repeated word: '$last_word'\n" . $hereprev) && 3262310cd06bSJoe Perches $fix) { 3263310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; 3264310cd06bSJoe Perches } 3265310cd06bSJoe Perches } 3266310cd06bSJoe Perches } 3267310cd06bSJoe Perches } 3268310cd06bSJoe Perches 326930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 327030670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 327100df344fSAndy Whitcroft 32720a920b5bSAndy Whitcroft#trailing whitespace 32739c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 3274c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3275d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 3276d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 3277d5e616fcSJoe Perches $fix) { 3278194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 3279d5e616fcSJoe Perches } 3280c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 3281c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32823705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 32833705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 32843705ce5bSJoe Perches $fix) { 3285194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 32863705ce5bSJoe Perches } 32873705ce5bSJoe Perches 3288d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 32890a920b5bSAndy Whitcroft } 32905368df20SAndy Whitcroft 32914783f894SJosh Triplett# Check for FSF mailing addresses. 3292109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 32931bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 32943e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 32953e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 32964783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32970675a8fbSJean Delvare my $msg_level = \&ERROR; 32980675a8fbSJean Delvare $msg_level = \&CHK if ($file); 32990675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 33004783f894SJosh 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) 33014783f894SJosh Triplett } 33024783f894SJosh Triplett 33033354957aSAndi Kleen# check for Kconfig help text having a real description 33049fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 33059fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 33063354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 3307678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3308678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3309678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3310678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 33113354957aSAndi Kleen my $length = 0; 33129fe287d7SAndy Whitcroft my $cnt = $realcnt; 33139fe287d7SAndy Whitcroft my $ln = $linenr + 1; 33149fe287d7SAndy Whitcroft my $f; 3315a1385803SAndy Whitcroft my $is_start = 0; 33169fe287d7SAndy Whitcroft my $is_end = 0; 3317a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 33189fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 33199fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 33209fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 33219fe287d7SAndy Whitcroft 33229fe287d7SAndy Whitcroft next if ($f =~ /^-/); 33238d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3324a1385803SAndy Whitcroft 332586adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3326a1385803SAndy Whitcroft $is_start = 1; 332722a4ac02SMasahiro Yamada } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 3328a1385803SAndy Whitcroft $length = -1; 3329a1385803SAndy Whitcroft } 3330a1385803SAndy Whitcroft 33319fe287d7SAndy Whitcroft $f =~ s/^.//; 33323354957aSAndi Kleen $f =~ s/#.*//; 33333354957aSAndi Kleen $f =~ s/^\s+//; 33343354957aSAndi Kleen next if ($f =~ /^$/); 3335678ae162SUlf Magnusson 3336678ae162SUlf Magnusson # This only checks context lines in the patch 3337678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3338678ae162SUlf Magnusson # positives, even though some of these are 3339678ae162SUlf Magnusson # common words in help texts 3340678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3341678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 33429fe287d7SAndy Whitcroft $is_end = 1; 33439fe287d7SAndy Whitcroft last; 33449fe287d7SAndy Whitcroft } 33453354957aSAndi Kleen $length++; 33463354957aSAndi Kleen } 334756193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3348000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 334956193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 335056193274SVadim Bendebury } 3351a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 33523354957aSAndi Kleen } 33533354957aSAndi Kleen 33547ccf41a8SJoe Perches# check MAINTAINERS entries 33557ccf41a8SJoe Perches if ($realfile =~ /^MAINTAINERS$/) { 33567ccf41a8SJoe Perches# check MAINTAINERS entries for the right form 33577ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 3358628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3359628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3360628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3361628f91a2SJoe Perches $fix) { 3362628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3363628f91a2SJoe Perches } 3364628f91a2SJoe Perches } 33657ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too 33667ccf41a8SJoe Perches my $preferred_order = 'MRLSWQBCPTFXNK'; 33677ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 33687ccf41a8SJoe Perches $prevrawline =~ /^[\+ ][A-Z]:/) { 33697ccf41a8SJoe Perches $rawline =~ /^\+([A-Z]):\s*(.*)/; 33707ccf41a8SJoe Perches my $cur = $1; 33717ccf41a8SJoe Perches my $curval = $2; 33727ccf41a8SJoe Perches $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; 33737ccf41a8SJoe Perches my $prev = $1; 33747ccf41a8SJoe Perches my $prevval = $2; 33757ccf41a8SJoe Perches my $curindex = index($preferred_order, $cur); 33767ccf41a8SJoe Perches my $previndex = index($preferred_order, $prev); 33777ccf41a8SJoe Perches if ($curindex < 0) { 33787ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 33797ccf41a8SJoe Perches "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); 33807ccf41a8SJoe Perches } else { 33817ccf41a8SJoe Perches if ($previndex >= 0 && $curindex < $previndex) { 33827ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 33837ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); 33847ccf41a8SJoe Perches } elsif ((($prev eq 'F' && $cur eq 'F') || 33857ccf41a8SJoe Perches ($prev eq 'X' && $cur eq 'X')) && 33867ccf41a8SJoe Perches ($prevval cmp $curval) > 0) { 33877ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 33887ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); 33897ccf41a8SJoe Perches } 33907ccf41a8SJoe Perches } 33917ccf41a8SJoe Perches } 33927ccf41a8SJoe Perches } 3393628f91a2SJoe Perches 3394c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3395c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3396c68e5878SArnaud Lacombe my $flag = $1; 3397c68e5878SArnaud Lacombe my $replacement = { 3398c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3399c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3400c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3401c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3402c68e5878SArnaud Lacombe }; 3403c68e5878SArnaud Lacombe 3404c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3405c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3406c68e5878SArnaud Lacombe } 3407c68e5878SArnaud Lacombe 3408bff5da43SRob Herring# check for DT compatible documentation 34097dd05b38SFlorian Vaussard if (defined $root && 34107dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 34117dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 34127dd05b38SFlorian Vaussard 3413bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3414bff5da43SRob Herring 3415cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3416852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3417cc93319bSFlorian Vaussard 3418bff5da43SRob Herring foreach my $compat (@compats) { 3419bff5da43SRob Herring my $compat2 = $compat; 3420185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3421185d566bSRob Herring my $compat3 = $compat; 3422185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3423185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3424bff5da43SRob Herring if ( $? >> 8 ) { 3425bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3426bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3427bff5da43SRob Herring } 3428bff5da43SRob Herring 34294fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 34304fbf32a6SFlorian Vaussard my $vendor = $1; 3431852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3432bff5da43SRob Herring if ( $? >> 8 ) { 3433bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3434cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3435bff5da43SRob Herring } 3436bff5da43SRob Herring } 3437bff5da43SRob Herring } 3438bff5da43SRob Herring 34399f3a8992SRob Herring# check for using SPDX license tag at beginning of files 34409f3a8992SRob Herring if ($realline == $checklicenseline) { 34419f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 34429f3a8992SRob Herring $checklicenseline = 2; 34439f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 34449f3a8992SRob Herring my $comment = ""; 34459f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 34469f3a8992SRob Herring $comment = '/*'; 34479f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 34489f3a8992SRob Herring $comment = '//'; 3449c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 34509f3a8992SRob Herring $comment = '#'; 34519f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 34529f3a8992SRob Herring $comment = '..'; 34539f3a8992SRob Herring } 34549f3a8992SRob Herring 3455fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3456fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3457fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3458ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3459fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3460fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3461fdf13693SJoe Perches } 3462fdf13693SJoe Perches 34639f3a8992SRob Herring if ($comment !~ /^$/ && 3464ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 34659f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 34669f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 34673b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 34683b6e8ac9SJoe Perches my $spdx_license = $1; 34693b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 34703b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 34713b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 34723b6e8ac9SJoe Perches } 347350c92900SLubomir Rintel if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 347450c92900SLubomir Rintel not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 347550c92900SLubomir Rintel my $msg_level = \&WARN; 347650c92900SLubomir Rintel $msg_level = \&CHK if ($file); 347750c92900SLubomir Rintel if (&{$msg_level}("SPDX_LICENSE_TAG", 347850c92900SLubomir Rintel 347950c92900SLubomir Rintel "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 348050c92900SLubomir Rintel $fix) { 348150c92900SLubomir Rintel $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 348250c92900SLubomir Rintel } 348350c92900SLubomir Rintel } 34849f3a8992SRob Herring } 34859f3a8992SRob Herring } 34869f3a8992SRob Herring } 34879f3a8992SRob Herring 3488a0154cdbSJoe Perches# check for embedded filenames 3489a0154cdbSJoe Perches if ($rawline =~ /^\+.*\Q$realfile\E/) { 3490a0154cdbSJoe Perches WARN("EMBEDDED_FILENAME", 3491a0154cdbSJoe Perches "It's generally not useful to have the filename in the file\n" . $herecurr); 3492a0154cdbSJoe Perches } 3493a0154cdbSJoe Perches 34945368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3495d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 34965368df20SAndy Whitcroft 3497a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3498a8da38a9SJoe Perches if ($realline != $checklicenseline && 3499a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3500a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3501a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3502a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3503a8da38a9SJoe Perches } 3504a8da38a9SJoe Perches 350547e0c88bSJoe Perches# line length limit (with some exclusions) 350647e0c88bSJoe Perches# 350747e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 350847e0c88bSJoe Perches# logging functions like pr_info that end in a string 350947e0c88bSJoe Perches# lines with a single string 351047e0c88bSJoe Perches# #defines that are a single string 35112e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 351247e0c88bSJoe Perches# 351347e0c88bSJoe Perches# There are 3 different line length message types: 3514ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 351547e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 351647e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 351747e0c88bSJoe Perches# 351847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 351947e0c88bSJoe Perches# 352047e0c88bSJoe Perches 3521b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 352247e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 352347e0c88bSJoe Perches 352447e0c88bSJoe Perches # Check the allowed long line types first 352547e0c88bSJoe Perches 352647e0c88bSJoe Perches # logging functions that end in a string that starts 352747e0c88bSJoe Perches # before $max_line_length 352847e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 352947e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 353047e0c88bSJoe Perches $msg_type = ""; 353147e0c88bSJoe Perches 353247e0c88bSJoe Perches # lines with only strings (w/ possible termination) 353347e0c88bSJoe Perches # #defines with only strings 353447e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 353547e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 353647e0c88bSJoe Perches $msg_type = ""; 353747e0c88bSJoe Perches 3538cc147506SJoe Perches # More special cases 3539cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3540cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3541d560a5f8SJoe Perches $msg_type = ""; 3542d560a5f8SJoe Perches 35432e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 35442e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 35452e4bbbc5SAndreas Brauchli $msg_type = ""; 35462e4bbbc5SAndreas Brauchli 354747e0c88bSJoe Perches # Otherwise set the alternate message types 354847e0c88bSJoe Perches 354947e0c88bSJoe Perches # a comment starts before $max_line_length 355047e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 355147e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 355247e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 355347e0c88bSJoe Perches 355447e0c88bSJoe Perches # a quoted string starts before $max_line_length 355547e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 355647e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 355747e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 355847e0c88bSJoe Perches } 355947e0c88bSJoe Perches 356047e0c88bSJoe Perches if ($msg_type ne "" && 356147e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 3562bdc48fa1SJoe Perches my $msg_level = \&WARN; 3563bdc48fa1SJoe Perches $msg_level = \&CHK if ($file); 3564bdc48fa1SJoe Perches &{$msg_level}($msg_type, 3565bdc48fa1SJoe Perches "line length of $length exceeds $max_line_length columns\n" . $herecurr); 35660a920b5bSAndy Whitcroft } 356747e0c88bSJoe Perches } 35680a920b5bSAndy Whitcroft 35698905a67cSAndy Whitcroft# check for adding lines without a newline. 35708905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 357147ca69b8STom Rix if (WARN("MISSING_EOF_NEWLINE", 357247ca69b8STom Rix "adding a line without newline at end of file\n" . $herecurr) && 357347ca69b8STom Rix $fix) { 357447ca69b8STom Rix fix_delete_line($fixlinenr+1, "No newline at end of file"); 357547ca69b8STom Rix } 35768905a67cSAndy Whitcroft } 35778905a67cSAndy Whitcroft 3578b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3579de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 35800a920b5bSAndy Whitcroft 35810a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3582713a09deSAntonio Borneo# more than $tabsize must use tabs. 3583c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3584c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3585c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3586d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 35873705ce5bSJoe Perches if (ERROR("CODE_INDENT", 35883705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 35893705ce5bSJoe Perches $fix) { 3590194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 35913705ce5bSJoe Perches } 35920a920b5bSAndy Whitcroft } 35930a920b5bSAndy Whitcroft 359408e44365SAlberto Panizzo# check for space before tabs. 359508e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 359608e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 35973705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 35983705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 35993705ce5bSJoe Perches $fix) { 3600194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3601713a09deSAntonio Borneo s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3602194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3603c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 36043705ce5bSJoe Perches } 360508e44365SAlberto Panizzo } 360608e44365SAlberto Panizzo 36076a487211SJoe Perches# check for assignments on the start of a line 36086a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 3609da7355abSAditya Srivastava my $operator = $1; 3610da7355abSAditya Srivastava if (CHK("ASSIGNMENT_CONTINUATIONS", 3611da7355abSAditya Srivastava "Assignment operator '$1' should be on the previous line\n" . $hereprev) && 3612da7355abSAditya Srivastava $fix && $prevrawline =~ /^\+/) { 3613da7355abSAditya Srivastava # add assignment operator to the previous line, remove from current line 3614da7355abSAditya Srivastava $fixed[$fixlinenr - 1] .= " $operator"; 3615da7355abSAditya Srivastava $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 3616da7355abSAditya Srivastava } 36176a487211SJoe Perches } 36186a487211SJoe Perches 3619d1fe9c09SJoe Perches# check for && or || at the start of a line 3620d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 36218e08f076SAditya Srivastava my $operator = $1; 36228e08f076SAditya Srivastava if (CHK("LOGICAL_CONTINUATIONS", 36238e08f076SAditya Srivastava "Logical continuations should be on the previous line\n" . $hereprev) && 36248e08f076SAditya Srivastava $fix && $prevrawline =~ /^\+/) { 36258e08f076SAditya Srivastava # insert logical operator at last non-comment, non-whitepsace char on previous line 36268e08f076SAditya Srivastava $prevline =~ /[\s$;]*$/; 36278e08f076SAditya Srivastava my $line_end = substr($prevrawline, $-[0]); 36288e08f076SAditya Srivastava $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/; 36298e08f076SAditya Srivastava $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 36308e08f076SAditya Srivastava } 3631d1fe9c09SJoe Perches } 3632d1fe9c09SJoe Perches 3633a91e8994SJoe Perches# check indentation starts on a tab stop 36345b57980dSJoe Perches if ($perl_version_ok && 3635bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3636a91e8994SJoe Perches my $indent = length($1); 3637713a09deSAntonio Borneo if ($indent % $tabsize) { 3638a91e8994SJoe Perches if (WARN("TABSTOP", 3639a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3640a91e8994SJoe Perches $fix) { 3641713a09deSAntonio Borneo $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3642a91e8994SJoe Perches } 3643a91e8994SJoe Perches } 3644a91e8994SJoe Perches } 3645a91e8994SJoe Perches 3646d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 36475b57980dSJoe Perches if ($perl_version_ok && 3648fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3649d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3650d1fe9c09SJoe Perches my $oldindent = $1; 3651d1fe9c09SJoe Perches my $rest = $2; 3652d1fe9c09SJoe Perches 3653d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3654d1fe9c09SJoe Perches if ($pos >= 0) { 3655b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3656b34a26f3SJoe Perches my $newindent = $2; 3657d1fe9c09SJoe Perches 3658d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3659713a09deSAntonio Borneo "\t" x ($pos / $tabsize) . 3660713a09deSAntonio Borneo " " x ($pos % $tabsize); 3661d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3662d1fe9c09SJoe Perches 3663d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3664d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 36653705ce5bSJoe Perches 36663705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 36673705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 36683705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3669194f66fcSJoe Perches $fixed[$fixlinenr] =~ 36703705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 36713705ce5bSJoe Perches } 3672d1fe9c09SJoe Perches } 3673d1fe9c09SJoe Perches } 3674d1fe9c09SJoe Perches } 3675d1fe9c09SJoe Perches 36766ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 36776ab3a970SJoe Perches# avoid checking a few false positives: 36786ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 36796ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 36806ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 36816ab3a970SJoe Perches# multiline macros that define functions 36826ab3a970SJoe Perches# known attributes or the __attribute__ keyword 36836ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 36846ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 36853705ce5bSJoe Perches if (CHK("SPACING", 3686f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 36873705ce5bSJoe Perches $fix) { 3688194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3689f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 36903705ce5bSJoe Perches } 3691aad4f614SJoe Perches } 3692aad4f614SJoe Perches 369386406b1cSJoe Perches# Block comment styles 369486406b1cSJoe Perches# Networking with an initial /* 369505880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3696fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 369785ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 3698c70735c2SŁukasz Stelmach $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier 369905880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 370005880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 370105880600SJoe Perches } 370205880600SJoe Perches 370386406b1cSJoe Perches# Block comments use * on subsequent lines 370486406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 370586406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3706a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 370761135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3708a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 370986406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 371086406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3711a605e32eSJoe Perches } 3712a605e32eSJoe Perches 371386406b1cSJoe Perches# Block comments use */ on trailing lines 371486406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3715c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3716c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3717c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 371886406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 371986406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 372005880600SJoe Perches } 372105880600SJoe Perches 372208eb9b80SJoe Perches# Block comment * alignment 372308eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3724af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3725af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3726af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 372708eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3728af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3729af207524SJoe Perches my $oldindent; 373008eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3731af207524SJoe Perches if (defined($1)) { 3732af207524SJoe Perches $oldindent = expand_tabs($1); 3733af207524SJoe Perches } else { 3734af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3735af207524SJoe Perches $oldindent = expand_tabs($1); 3736af207524SJoe Perches } 373708eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 373808eb9b80SJoe Perches my $newindent = $1; 373908eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3740af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 374108eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 374208eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 374308eb9b80SJoe Perches } 374408eb9b80SJoe Perches } 374508eb9b80SJoe Perches 37467f619191SJoe Perches# check for missing blank lines after struct/union declarations 37477f619191SJoe Perches# with exceptions for various attributes and macros 37487f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 37497f619191SJoe Perches $line =~ /^\+/ && 37507f619191SJoe Perches !($line =~ /^\+\s*$/ || 37517f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 37527f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 37537f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 37547f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 37557f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 37567f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 37570bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 37587f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3759d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3760d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3761d752fcc8SJoe Perches $fix) { 3762f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3763d752fcc8SJoe Perches } 37647f619191SJoe Perches } 37657f619191SJoe Perches 3766365dd4eaSJoe Perches# check for multiple consecutive blank lines 3767365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3768365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3769365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3770d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3771d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3772d752fcc8SJoe Perches $fix) { 3773f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3774d752fcc8SJoe Perches } 3775d752fcc8SJoe Perches 3776365dd4eaSJoe Perches $last_blank_line = $linenr; 3777365dd4eaSJoe Perches } 3778365dd4eaSJoe Perches 37793b617e3bSJoe Perches# check for missing blank lines after declarations 3780b5e8736aSJoe Perches# (declarations must have the same indentation and not be at the start of line) 3781b5e8736aSJoe Perches if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) { 3782b5e8736aSJoe Perches # use temporaries 3783b5e8736aSJoe Perches my $sl = $sline; 3784b5e8736aSJoe Perches my $pl = $prevline; 3785b5e8736aSJoe Perches # remove $Attribute/$Sparse uses to simplify comparisons 3786b5e8736aSJoe Perches $sl =~ s/\b(?:$Attribute|$Sparse)\b//g; 3787b5e8736aSJoe Perches $pl =~ s/\b(?:$Attribute|$Sparse)\b//g; 3788b5e8736aSJoe Perches if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 37895a4e1fd3SJoe Perches # function pointer declarations 3790b5e8736aSJoe Perches $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 37913f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 3792b5e8736aSJoe Perches $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 37933f7bac03SJoe Perches # known declaration macros 3794b5e8736aSJoe Perches $pl =~ /^\+\s+$declaration_macros/) && 37953f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 3796b5e8736aSJoe Perches !($pl =~ /^\+\s+$c90_Keywords\b/ || 37973f7bac03SJoe Perches # other possible extensions of declaration lines 3798b5e8736aSJoe Perches $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 37993f7bac03SJoe Perches # not starting a section or a macro "\" extended line 3800b5e8736aSJoe Perches $pl =~ /(?:\{\s*|\\)$/) && 38013f7bac03SJoe Perches # looks like a declaration 3802b5e8736aSJoe Perches !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 38035a4e1fd3SJoe Perches # function pointer declarations 3804b5e8736aSJoe Perches $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 38053f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 3806b5e8736aSJoe Perches $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 38073f7bac03SJoe Perches # known declaration macros 3808b5e8736aSJoe Perches $sl =~ /^\+\s+$declaration_macros/ || 38093f7bac03SJoe Perches # start of struct or union or enum 3810b5e8736aSJoe Perches $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 38113f7bac03SJoe Perches # start or end of block or continuation of declaration 3812b5e8736aSJoe Perches $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 38133f7bac03SJoe Perches # bitfield continuation 3814b5e8736aSJoe Perches $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 38153f7bac03SJoe Perches # other possible extensions of declaration lines 3816b5e8736aSJoe Perches $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) { 3817d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3818d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3819d752fcc8SJoe Perches $fix) { 3820f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3821d752fcc8SJoe Perches } 38223b617e3bSJoe Perches } 3823b5e8736aSJoe Perches } 38243b617e3bSJoe Perches 38255f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 38266b4c5bebSAndy Whitcroft# Exceptions: 38276b4c5bebSAndy Whitcroft# 1) within comments 38286b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 38296b4c5bebSAndy Whitcroft# 3) hanging labels 38303705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 38315f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 38323705ce5bSJoe Perches if (WARN("LEADING_SPACE", 38333705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 38343705ce5bSJoe Perches $fix) { 3835194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 38363705ce5bSJoe Perches } 38375f7ddae6SRaffaele Recalcati } 38385f7ddae6SRaffaele Recalcati 3839b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3840b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3841b9ea10d6SAndy Whitcroft 38425751a24eSJoe Perches# check for unusual line ending [ or ( 38435751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 38445751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 38455751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 38465751a24eSJoe Perches } 38475751a24eSJoe Perches 38484dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 38494dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 38504dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 38514dbed76fSJoe Perches $context_function = $1; 38524dbed76fSJoe Perches } 38534dbed76fSJoe Perches 38544dbed76fSJoe Perches# check if this appears to be the end of function declaration 38554dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 38564dbed76fSJoe Perches undef $context_function; 38574dbed76fSJoe Perches } 38584dbed76fSJoe Perches 3859032a4c0fSJoe Perches# check indentation of any line with a bare else 3860840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3861032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3862032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3863032a4c0fSJoe Perches my $tabs = length($1) + 1; 3864840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3865840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3866840080a0SJoe Perches defined $lines[$linenr] && 3867840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3868032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3869032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3870032a4c0fSJoe Perches } 3871032a4c0fSJoe Perches } 3872032a4c0fSJoe Perches 3873c00df19aSJoe Perches# check indentation of a line with a break; 3874dc58bc55SJoe Perches# if the previous line is a goto, return or break 3875dc58bc55SJoe Perches# and is indented the same # of tabs 3876c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3877c00df19aSJoe Perches my $tabs = $1; 3878dc58bc55SJoe Perches if ($prevline =~ /^\+$tabs(goto|return|break)\b/) { 3879dc58bc55SJoe Perches if (WARN("UNNECESSARY_BREAK", 3880dc58bc55SJoe Perches "break is not useful after a $1\n" . $hereprev) && 3881dc58bc55SJoe Perches $fix) { 3882dc58bc55SJoe Perches fix_delete_line($fixlinenr, $rawline); 3883dc58bc55SJoe Perches } 3884c00df19aSJoe Perches } 3885c00df19aSJoe Perches } 3886c00df19aSJoe Perches 3887c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3888cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3889000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3890000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3891c2fdda0dSAndy Whitcroft } 389222f2a2efSAndy Whitcroft 389356e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 389456e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 389556e77d70SJoe Perches WARN("HOTPLUG_SECTION", 389656e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 389756e77d70SJoe Perches } 389856e77d70SJoe Perches 38999c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 39002b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 39012b474a1aSAndy Whitcroft $realline_next); 39023e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3903ca819864SJoe Perches if ($linenr > $suppress_statement && 39041b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3905170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3906f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3907171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3908171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3909171ae1a4SAndy Whitcroft 39103e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 39113e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 39123e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 39133e469cdcSAndy Whitcroft # until we hit end of it. 39143e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 39153e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 39163e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 39173e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 39183e469cdcSAndy Whitcroft } 3919f74bd194SAndy Whitcroft 39202b474a1aSAndy Whitcroft # Find the real next line. 39212b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 39222b474a1aSAndy Whitcroft if (defined $realline_next && 39232b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 39242b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 39252b474a1aSAndy Whitcroft $realline_next++; 39262b474a1aSAndy Whitcroft } 39272b474a1aSAndy Whitcroft 3928171ae1a4SAndy Whitcroft my $s = $stat; 3929171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3930cf655043SAndy Whitcroft 3931c2fdda0dSAndy Whitcroft # Ignore goto labels. 3932171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3933c2fdda0dSAndy Whitcroft 3934c2fdda0dSAndy Whitcroft # Ignore functions being called 3935171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3936c2fdda0dSAndy Whitcroft 3937463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3938463f2864SAndy Whitcroft 3939c45dcabdSAndy Whitcroft # declarations always start with types 3940d2506586SAndy 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) { 3941c45dcabdSAndy Whitcroft my $type = $1; 3942c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3943c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3944c45dcabdSAndy Whitcroft 39456c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3946a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3947c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3948c2fdda0dSAndy Whitcroft } 39498905a67cSAndy Whitcroft 39506c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 395165863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3952c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 39539c0ca6f9SAndy Whitcroft } 39548905a67cSAndy Whitcroft 39558905a67cSAndy Whitcroft # Check for any sort of function declaration. 39568905a67cSAndy Whitcroft # int foo(something bar, other baz); 39578905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3958171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 39598905a67cSAndy Whitcroft my ($name_len) = length($1); 39608905a67cSAndy Whitcroft 3961cf655043SAndy Whitcroft my $ctx = $s; 3962773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 39638905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3964cf655043SAndy Whitcroft 39658905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3966c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 39678905a67cSAndy Whitcroft 3968c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 39698905a67cSAndy Whitcroft } 39708905a67cSAndy Whitcroft } 39718905a67cSAndy Whitcroft } 39728905a67cSAndy Whitcroft 39739c0ca6f9SAndy Whitcroft } 39749c0ca6f9SAndy Whitcroft 397500df344fSAndy Whitcroft# 397600df344fSAndy Whitcroft# Checks which may be anchored in the context. 397700df344fSAndy Whitcroft# 397800df344fSAndy Whitcroft 397900df344fSAndy Whitcroft# Check for switch () and associated case and default 398000df344fSAndy Whitcroft# statements should be at the same indent. 398100df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 398200df344fSAndy Whitcroft my $err = ''; 398300df344fSAndy Whitcroft my $sep = ''; 398400df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 398500df344fSAndy Whitcroft shift(@ctx); 398600df344fSAndy Whitcroft for my $ctx (@ctx) { 398700df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 398800df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 398900df344fSAndy Whitcroft $indent != $cindent) { 399000df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 399100df344fSAndy Whitcroft $sep = ''; 399200df344fSAndy Whitcroft } else { 399300df344fSAndy Whitcroft $sep = "[...]\n"; 399400df344fSAndy Whitcroft } 399500df344fSAndy Whitcroft } 399600df344fSAndy Whitcroft if ($err ne '') { 3997000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3998000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3999de7d4f0eSAndy Whitcroft } 4000de7d4f0eSAndy Whitcroft } 4001de7d4f0eSAndy Whitcroft 4002de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 4003de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 40040fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 4005773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 4006773647a0SAndy Whitcroft 40079c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 40088eef05ddSJoe Perches 40098eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 40108eef05ddSJoe Perches WARN("DEEP_INDENTATION", 40118eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 40128eef05ddSJoe Perches } 40138eef05ddSJoe Perches 4014de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 4015de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 4016de7d4f0eSAndy Whitcroft 4017548596d5SAndy Whitcroft my $ctx_ln = $linenr; 4018548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 4019de7d4f0eSAndy Whitcroft 4020548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 4021548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 4022548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 4023548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 4024548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 4025773647a0SAndy Whitcroft $ctx_ln++; 4026773647a0SAndy Whitcroft } 4027548596d5SAndy Whitcroft 402853210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 402953210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 4030773647a0SAndy Whitcroft 4031773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 4032000d1cc1SJoe Perches ERROR("OPEN_BRACE", 4033000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 403401464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 403500df344fSAndy Whitcroft } 4036773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 4037773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 4038773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 4039773647a0SAndy Whitcroft { 40409c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 40419c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 4042000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 4043000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 404401464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 40459c0ca6f9SAndy Whitcroft } 40469c0ca6f9SAndy Whitcroft } 404700df344fSAndy Whitcroft } 404800df344fSAndy Whitcroft 40494d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 4050f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 40513e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 40523e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 40533e469cdcSAndy Whitcroft if (!defined $stat); 40544d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 40554d001e4dSAndy Whitcroft 40564d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 40574d001e4dSAndy Whitcroft 40589f5af480SJoe Perches # remove inline comments 40599f5af480SJoe Perches $s =~ s/$;/ /g; 40609f5af480SJoe Perches $c =~ s/$;/ /g; 40614d001e4dSAndy Whitcroft 40624d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 40636f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 40646f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 40654d001e4dSAndy Whitcroft 40669f5af480SJoe Perches # Make sure we remove the line prefixes as we have 40679f5af480SJoe Perches # none on the first line, and are going to readd them 40689f5af480SJoe Perches # where necessary. 40699f5af480SJoe Perches $s =~ s/\n./\n/gs; 40709f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 40719f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 40729f5af480SJoe Perches } 40739f5af480SJoe Perches 40744d001e4dSAndy Whitcroft # We want to check the first line inside the block 40754d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 40764d001e4dSAndy Whitcroft # 1) any blank line termination 40774d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 40784d001e4dSAndy Whitcroft # 3) any do (...) { 40794d001e4dSAndy Whitcroft my $continuation = 0; 40804d001e4dSAndy Whitcroft my $check = 0; 40814d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 40824d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 40834d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 40844d001e4dSAndy Whitcroft $continuation = 1; 40854d001e4dSAndy Whitcroft } 40869bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 40874d001e4dSAndy Whitcroft $check = 1; 40884d001e4dSAndy Whitcroft $cond_lines++; 40894d001e4dSAndy Whitcroft } 40904d001e4dSAndy Whitcroft 40914d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 40924d001e4dSAndy Whitcroft # preprocessor statement. 40934d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 40944d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 40954d001e4dSAndy Whitcroft $check = 0; 40964d001e4dSAndy Whitcroft } 40974d001e4dSAndy Whitcroft 40989bd49efeSAndy Whitcroft my $cond_ptr = -1; 4099740504c6SAndy Whitcroft $continuation = 0; 41009bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 41019bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 41024d001e4dSAndy Whitcroft 4103f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 4104f16fa28fSAndy Whitcroft # is not linear. 4105f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 4106f16fa28fSAndy Whitcroft $check = 0; 4107f16fa28fSAndy Whitcroft } 4108f16fa28fSAndy Whitcroft 41099bd49efeSAndy Whitcroft # Ignore: 41109bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 41119bd49efeSAndy Whitcroft # 2) preprocessor lines, and 41129bd49efeSAndy Whitcroft # 3) labels. 4113740504c6SAndy Whitcroft if ($continuation || 4114740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 41159bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 41169bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 4117740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 411830dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 41199bd49efeSAndy Whitcroft $cond_lines++; 41209bd49efeSAndy Whitcroft } 41214d001e4dSAndy Whitcroft } 412230dad6ebSAndy Whitcroft } 41234d001e4dSAndy Whitcroft 41244d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 41254d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 41264d001e4dSAndy Whitcroft 41274d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 41284d001e4dSAndy Whitcroft # this is not this patch's fault. 41294d001e4dSAndy Whitcroft if (!defined($stat_real) || 41304d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 41314d001e4dSAndy Whitcroft $check = 0; 41324d001e4dSAndy Whitcroft } 41334d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 41344d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 41354d001e4dSAndy Whitcroft } 41364d001e4dSAndy Whitcroft 41379bd49efeSAndy 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"; 41384d001e4dSAndy Whitcroft 41399f5af480SJoe Perches if ($check && $s ne '' && 4140713a09deSAntonio Borneo (($sindent % $tabsize) != 0 || 41419f5af480SJoe Perches ($sindent < $indent) || 4142f6950a73SJoe Perches ($sindent == $indent && 4143f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 4144713a09deSAntonio Borneo ($sindent > $indent + $tabsize))) { 4145000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 4146000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 41474d001e4dSAndy Whitcroft } 41484d001e4dSAndy Whitcroft } 41494d001e4dSAndy Whitcroft 41506c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 41516c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 41521f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 41531f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 41546c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 4155c2fdda0dSAndy Whitcroft if ($dbg_values) { 4156c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 4157cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 4158cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 41591f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 4160c2fdda0dSAndy Whitcroft } 41616c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 41626c72ffaaSAndy Whitcroft 416300df344fSAndy Whitcroft#ignore lines not being added 41643705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 416500df344fSAndy Whitcroft 416699ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings 416799ca38c2SJoe Perches# e.g.: int foo = foo, *bar = NULL; 416899ca38c2SJoe Perches# struct foo bar = *(&(bar)); 416999ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { 417099ca38c2SJoe Perches my $var = $1; 417199ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { 417299ca38c2SJoe Perches WARN("SELF_ASSIGNMENT", 417399ca38c2SJoe Perches "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); 417499ca38c2SJoe Perches } 417599ca38c2SJoe Perches } 417699ca38c2SJoe Perches 417711ca40a0SJoe Perches# check for dereferences that span multiple lines 417811ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 417911ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 418011ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 418111ca40a0SJoe Perches my $ref = $1; 418211ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 418311ca40a0SJoe Perches $ref .= $1; 418411ca40a0SJoe Perches $ref =~ s/\s//g; 418511ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 418611ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 418711ca40a0SJoe Perches } 418811ca40a0SJoe Perches 4189a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 4190c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 4191a1ce18e4SJoe Perches my $type = $1; 4192a1ce18e4SJoe Perches my $var = $2; 4193207a8e84SJoe Perches $var = "" if (!defined $var); 4194207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 4195a1ce18e4SJoe Perches my $sign = $1; 4196a1ce18e4SJoe Perches my $pointer = $2; 4197a1ce18e4SJoe Perches 4198a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 4199a1ce18e4SJoe Perches 4200a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 4201a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 4202a1ce18e4SJoe Perches $fix) { 4203a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 4204207a8e84SJoe Perches my $comp_pointer = $pointer; 4205207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 4206207a8e84SJoe Perches $decl .= $comp_pointer; 4207207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 4208207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 4209a1ce18e4SJoe Perches } 4210a1ce18e4SJoe Perches } 4211a1ce18e4SJoe Perches } 4212a1ce18e4SJoe Perches 4213653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 42147429c690SAndy Whitcroft if ($dbg_type) { 42157429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 4216000d1cc1SJoe Perches ERROR("TEST_TYPE", 4217000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 42187429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 4219000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 4220000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 42217429c690SAndy Whitcroft } 4222653d4876SAndy Whitcroft next; 4223653d4876SAndy Whitcroft } 4224a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 4225a1ef277eSAndy Whitcroft if ($dbg_attr) { 42269360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 4227000d1cc1SJoe Perches ERROR("TEST_ATTR", 4228000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 42299360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 4230000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 4231000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 4232a1ef277eSAndy Whitcroft } 4233a1ef277eSAndy Whitcroft next; 4234a1ef277eSAndy Whitcroft } 4235653d4876SAndy Whitcroft 4236f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 423799423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 423899423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 4239d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 4240d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 4241f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 4242f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4243f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 4244d752fcc8SJoe Perches my $fixedline = $prevrawline; 4245d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 4246f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4247d752fcc8SJoe Perches $fixedline = $line; 42488d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 4249f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4250d752fcc8SJoe Perches } 4251f0a594c1SAndy Whitcroft } 4252f0a594c1SAndy Whitcroft 425300df344fSAndy Whitcroft# 425400df344fSAndy Whitcroft# Checks which are anchored on the added line. 425500df344fSAndy Whitcroft# 425600df344fSAndy Whitcroft 4257653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 4258c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 4259653d4876SAndy Whitcroft my $path = $1; 4260653d4876SAndy Whitcroft if ($path =~ m{//}) { 4261000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 4262495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 4263495e9d84SJoe Perches } 4264495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 4265495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 4266495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 4267653d4876SAndy Whitcroft } 4268653d4876SAndy Whitcroft } 4269653d4876SAndy Whitcroft 427000df344fSAndy Whitcroft# no C99 // comments 427100df344fSAndy Whitcroft if ($line =~ m{//}) { 42723705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 42733705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 42743705ce5bSJoe Perches $fix) { 4275194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 42763705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 42773705ce5bSJoe Perches my $comment = trim($1); 4278194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 42793705ce5bSJoe Perches } 42803705ce5bSJoe Perches } 428100df344fSAndy Whitcroft } 428200df344fSAndy Whitcroft # Remove C99 comments. 42830a920b5bSAndy Whitcroft $line =~ s@//.*@@; 42846c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 42850a920b5bSAndy Whitcroft 42862b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 42872b474a1aSAndy Whitcroft# the whole statement. 42882b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 42892b474a1aSAndy Whitcroft if (defined $realline_next && 42902b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 42912b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 429236794822SChristoph Hellwig ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) { 42933cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 42943cbf62dfSAndy Whitcroft # a prefix: 42953cbf62dfSAndy Whitcroft # XXX(foo); 42963cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 4297653d4876SAndy Whitcroft my $name = $1; 429887a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 42993cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 43003cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 43013cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 43023cbf62dfSAndy Whitcroft 43033cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 43042b474a1aSAndy Whitcroft \n.}\s*$| 430548012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 430648012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 430748012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 43082b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 43092b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 431048012058SAndy Whitcroft )/x) { 43112b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 43122b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 43132b474a1aSAndy Whitcroft } else { 43142b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 43150a920b5bSAndy Whitcroft } 43160a920b5bSAndy Whitcroft } 43172b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 43182b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 431936794822SChristoph Hellwig ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) { 43202b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 43212b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 43222b474a1aSAndy Whitcroft } 43232b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 43242b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 4325000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 4326000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 43272b474a1aSAndy Whitcroft } 43280a920b5bSAndy Whitcroft 43295150bda4SJoe Eloff# check for global initialisers. 43306d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 4331d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 43326d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 4333d5e616fcSJoe Perches $fix) { 43346d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 4335d5e616fcSJoe Perches } 4336f0a594c1SAndy Whitcroft } 43370a920b5bSAndy Whitcroft# check for static initialisers. 43386d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 4339d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 43406d32f7a3SJoe Perches "do not initialise statics to $1\n" . 4341d5e616fcSJoe Perches $herecurr) && 4342d5e616fcSJoe Perches $fix) { 43436d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 4344d5e616fcSJoe Perches } 43450a920b5bSAndy Whitcroft } 43460a920b5bSAndy Whitcroft 43471813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 43481813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 43491813087dSJoe Perches my $tmp = trim($1); 43501813087dSJoe Perches WARN("MISORDERED_TYPE", 43511813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 43521813087dSJoe Perches } 43531813087dSJoe Perches 4354809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 4355809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 4356809e082eSJoe Perches my $type = trim($1); 4357809e082eSJoe Perches next if ($type !~ /\bint\b/); 4358809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 4359809e082eSJoe Perches my $new_type = $type; 4360809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 4361809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 4362809e082eSJoe Perches $new_type =~ s/^const\s+//; 4363809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 4364809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 4365809e082eSJoe Perches $new_type =~ s/\s+/ /g; 4366809e082eSJoe Perches $new_type = trim($new_type); 4367809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 4368809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 4369809e082eSJoe Perches $fix) { 4370809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 4371809e082eSJoe Perches } 4372809e082eSJoe Perches } 4373809e082eSJoe Perches 4374cb710ecaSJoe Perches# check for static const char * arrays. 4375cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 4376000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4377000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 4378cb710ecaSJoe Perches $herecurr); 4379cb710ecaSJoe Perches } 4380cb710ecaSJoe Perches 438177b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 438277b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 438377b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 438477b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 438577b8c0a8SJoe Perches $fix) { 438677b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 438777b8c0a8SJoe Perches } 438877b8c0a8SJoe Perches } 438977b8c0a8SJoe Perches 4390cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 4391cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4392000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4393000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4394cb710ecaSJoe Perches $herecurr); 4395cb710ecaSJoe Perches } 4396cb710ecaSJoe Perches 4397ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4398ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4399ab7e23f3SJoe Perches my $found = $1; 4400ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4401ab7e23f3SJoe Perches WARN("CONST_CONST", 4402ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4403ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4404ab7e23f3SJoe Perches WARN("CONST_CONST", 4405ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4406ab7e23f3SJoe Perches } 4407ab7e23f3SJoe Perches } 4408ab7e23f3SJoe Perches 440973169765SJoe Perches# check for const static or static <non ptr type> const declarations 441073169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const' 441173169765SJoe Perches if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ || 441273169765SJoe Perches $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) { 441373169765SJoe Perches if (WARN("STATIC_CONST", 441473169765SJoe Perches "Move const after static - use 'static const $1'\n" . $herecurr) && 441573169765SJoe Perches $fix) { 441673169765SJoe Perches $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/; 441773169765SJoe Perches $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/; 441873169765SJoe Perches } 441973169765SJoe Perches } 442073169765SJoe Perches 44219b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 44229b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 44239b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 44249b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 44259b0fa60dSJoe Perches $herecurr); 44269b0fa60dSJoe Perches } 44279b0fa60dSJoe Perches 4428b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4429b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4430b598b670SJoe Perches my $array = $1; 4431b598b670SJoe 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*\))@) { 4432b598b670SJoe Perches my $array_div = $1; 4433b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4434b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4435b598b670SJoe Perches $fix) { 4436b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4437b598b670SJoe Perches } 4438b598b670SJoe Perches } 4439b598b670SJoe Perches } 4440b598b670SJoe Perches 4441b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 444216b7f3c8SJoe Perches if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { 4443b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4444b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4445b36190c5SJoe Perches $fix) { 4446194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4447b36190c5SJoe Perches } 4448b36190c5SJoe Perches } 4449b36190c5SJoe Perches 4450653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4451653d4876SAndy Whitcroft# make sense. 4452653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 44538054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4454c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 44558ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 445646d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4457000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4458000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 44590a920b5bSAndy Whitcroft } 44600a920b5bSAndy Whitcroft 44610a920b5bSAndy Whitcroft# * goes on variable not on type 446265863862SAndy Whitcroft # (char*[ const]) 4463bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4464bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 44653705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4466d8aaf121SAndy Whitcroft 446765863862SAndy Whitcroft # Should start with a space. 446865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 446965863862SAndy Whitcroft # Should not end with a space. 447065863862SAndy Whitcroft $to =~ s/\s+$//; 447165863862SAndy Whitcroft # '*'s should not have spaces between. 4472f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 447365863862SAndy Whitcroft } 4474d8aaf121SAndy Whitcroft 44753705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 447665863862SAndy Whitcroft if ($from ne $to) { 44773705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 44783705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 44793705ce5bSJoe Perches $fix) { 44803705ce5bSJoe Perches my $sub_from = $ident; 44813705ce5bSJoe Perches my $sub_to = $ident; 44823705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4483194f66fcSJoe Perches $fixed[$fixlinenr] =~ 44843705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 44853705ce5bSJoe Perches } 448665863862SAndy Whitcroft } 4487bfcb2cc7SAndy Whitcroft } 4488bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4489bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 44903705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4491d8aaf121SAndy Whitcroft 449265863862SAndy Whitcroft # Should start with a space. 449365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 449465863862SAndy Whitcroft # Should not end with a space. 449565863862SAndy Whitcroft $to =~ s/\s+$//; 449665863862SAndy Whitcroft # '*'s should not have spaces between. 4497f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 449865863862SAndy Whitcroft } 449965863862SAndy Whitcroft # Modifiers should have spaces. 450065863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 450165863862SAndy Whitcroft 45023705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4503667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 45043705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 45053705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 45063705ce5bSJoe Perches $fix) { 45073705ce5bSJoe Perches 45083705ce5bSJoe Perches my $sub_from = $match; 45093705ce5bSJoe Perches my $sub_to = $match; 45103705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4511194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45123705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 45133705ce5bSJoe Perches } 451465863862SAndy Whitcroft } 45150a920b5bSAndy Whitcroft } 45160a920b5bSAndy Whitcroft 45179d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 45189d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 45190675a8fbSJean Delvare my $msg_level = \&WARN; 45200675a8fbSJean Delvare $msg_level = \&CHK if ($file); 45210675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 45229d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 45239d3e3c70SJoe Perches } 45240a920b5bSAndy Whitcroft 45259d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 45268905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4527000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4528000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 45298905a67cSAndy Whitcroft } 45308905a67cSAndy Whitcroft 453117441227SJoe Perches# check for uses of printk_ratelimit 453217441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4533000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4534000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 453517441227SJoe Perches } 453617441227SJoe Perches 4537eeef5733SJoe Perches# printk should use KERN_* levels 4538eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4539000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4540eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 454100df344fSAndy Whitcroft } 45420a920b5bSAndy Whitcroft 4543f5eea3b0SJoe Perches# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL> 4544f5eea3b0SJoe Perches if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) { 4545f5eea3b0SJoe Perches my $printk = $1; 4546f5eea3b0SJoe Perches my $modifier = $2; 4547f5eea3b0SJoe Perches my $orig = $3; 4548f5eea3b0SJoe Perches $modifier = "" if (!defined($modifier)); 4549243f3803SJoe Perches my $level = lc($orig); 4550243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 45518f26b837SJoe Perches my $level2 = $level; 45528f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4553f5eea3b0SJoe Perches $level .= $modifier; 4554f5eea3b0SJoe Perches $level2 .= $modifier; 4555243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4556f5eea3b0SJoe Perches "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr); 4557243f3803SJoe Perches } 4558243f3803SJoe Perches 4559f5eea3b0SJoe Perches# prefer dev_<level> to dev_printk(KERN_<LEVEL> 4560dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4561dc139313SJoe Perches my $orig = $1; 4562dc139313SJoe Perches my $level = lc($orig); 4563dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4564dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4565dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4566dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4567dc139313SJoe Perches } 4568dc139313SJoe Perches 45698020b253SNicolas Boichat# trace_printk should not be used in production code. 45708020b253SNicolas Boichat if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { 45718020b253SNicolas Boichat WARN("TRACE_PRINTK", 45728020b253SNicolas Boichat "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); 45738020b253SNicolas Boichat } 45748020b253SNicolas Boichat 457591c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 457691c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 457791c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 457891c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 457991c9afafSAndy Lutomirski WARN("ENOSYS", 458091c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 458191c9afafSAndy Lutomirski } 458291c9afafSAndy Lutomirski 45836b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches. 45846b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 45856b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected. 45866b9ea5ffSJakub Kicinski if (!$file && $line =~ /\bENOTSUPP\b/) { 45876b9ea5ffSJakub Kicinski if (WARN("ENOTSUPP", 45886b9ea5ffSJakub Kicinski "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 45896b9ea5ffSJakub Kicinski $fix) { 45906b9ea5ffSJakub Kicinski $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 45916b9ea5ffSJakub Kicinski } 45926b9ea5ffSJakub Kicinski } 45936b9ea5ffSJakub Kicinski 4594653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4595653d4876SAndy Whitcroft# or if closed on same line 45965b57980dSJoe Perches if ($perl_version_ok && 45972d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 45982d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 45992d453e3bSJoe Perches $sline !~ /}/) { 46008d182478SJoe Perches if (ERROR("OPEN_BRACE", 46012d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 46028d182478SJoe Perches $fix) { 46038d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 46048d182478SJoe Perches my $fixed_line = $rawline; 460503f49351SDwaipayan Ray $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/; 46068d182478SJoe Perches my $line1 = $1; 46078d182478SJoe Perches my $line2 = $2; 46088d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 46098d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 46108d182478SJoe Perches if ($line2 !~ /^\s*$/) { 46118d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 46128d182478SJoe Perches } 46138d182478SJoe Perches } 46140a920b5bSAndy Whitcroft } 4615653d4876SAndy Whitcroft 46168905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 46178905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 46188905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 46198d182478SJoe Perches if (ERROR("OPEN_BRACE", 46208d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 46218d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 46228d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 46238d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 46248d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 46258d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 46268d182478SJoe Perches $fixedline = $rawline; 46278d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 46288d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 46298d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 46308d182478SJoe Perches } 46318d182478SJoe Perches } 46328905a67cSAndy Whitcroft } 46338905a67cSAndy Whitcroft 46340c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 46353705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 46363705ce5bSJoe Perches if (WARN("SPACING", 46373705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 46383705ce5bSJoe Perches $fix) { 4639194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46403705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 46413705ce5bSJoe Perches } 46420c73b4ebSAndy Whitcroft } 46430c73b4ebSAndy Whitcroft 464431070b5dSJoe Perches# Function pointer declarations 464531070b5dSJoe Perches# check spacing between type, funcptr, and args 464631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 464791f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 464831070b5dSJoe Perches my $declare = $1; 464931070b5dSJoe Perches my $pre_pointer_space = $2; 465031070b5dSJoe Perches my $post_pointer_space = $3; 465131070b5dSJoe Perches my $funcname = $4; 465231070b5dSJoe Perches my $post_funcname_space = $5; 465331070b5dSJoe Perches my $pre_args_space = $6; 465431070b5dSJoe Perches 465591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 465691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 465791f72e9cSJoe Perches# don't need a space so don't warn for those. 465891f72e9cSJoe Perches my $post_declare_space = ""; 465991f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 466091f72e9cSJoe Perches $post_declare_space = $1; 466191f72e9cSJoe Perches $declare = rtrim($declare); 466291f72e9cSJoe Perches } 466391f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 466431070b5dSJoe Perches WARN("SPACING", 466531070b5dSJoe Perches "missing space after return type\n" . $herecurr); 466691f72e9cSJoe Perches $post_declare_space = " "; 466731070b5dSJoe Perches } 466831070b5dSJoe Perches 466931070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 467091f72e9cSJoe Perches# This test is not currently implemented because these declarations are 467191f72e9cSJoe Perches# equivalent to 467291f72e9cSJoe Perches# int foo(int bar, ...) 467391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 467491f72e9cSJoe Perches# 467591f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 467691f72e9cSJoe Perches# WARN("SPACING", 467791f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 467891f72e9cSJoe Perches# } 467931070b5dSJoe Perches 468031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 468131070b5dSJoe Perches if (defined $pre_pointer_space && 468231070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 468331070b5dSJoe Perches WARN("SPACING", 468431070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 468531070b5dSJoe Perches } 468631070b5dSJoe Perches 468731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 468831070b5dSJoe Perches if (defined $post_pointer_space && 468931070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 469031070b5dSJoe Perches WARN("SPACING", 469131070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 469231070b5dSJoe Perches } 469331070b5dSJoe Perches 469431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 469531070b5dSJoe Perches if (defined $post_funcname_space && 469631070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 469731070b5dSJoe Perches WARN("SPACING", 469831070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 469931070b5dSJoe Perches } 470031070b5dSJoe Perches 470131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 470231070b5dSJoe Perches if (defined $pre_args_space && 470331070b5dSJoe Perches $pre_args_space =~ /^\s/) { 470431070b5dSJoe Perches WARN("SPACING", 470531070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 470631070b5dSJoe Perches } 470731070b5dSJoe Perches 470831070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4709194f66fcSJoe Perches $fixed[$fixlinenr] =~ 471091f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 471131070b5dSJoe Perches } 471231070b5dSJoe Perches } 471331070b5dSJoe Perches 47148d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 47158d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4716fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4717fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 47188d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 47198d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 47208d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4721fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 472238dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 47233705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 47243705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 47253705ce5bSJoe Perches $fix) { 4726194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47273705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 47283705ce5bSJoe Perches } 47298d31cfceSAndy Whitcroft } 47308d31cfceSAndy Whitcroft } 47318d31cfceSAndy Whitcroft 4732f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 47336c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4734c2fdda0dSAndy Whitcroft my $name = $1; 4735773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4736773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4737c2fdda0dSAndy Whitcroft 4738c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4739773647a0SAndy Whitcroft if ($name =~ /^(?: 4740773647a0SAndy Whitcroft if|for|while|switch|return|case| 4741773647a0SAndy Whitcroft volatile|__volatile__| 4742773647a0SAndy Whitcroft __attribute__|format|__extension__| 4743773647a0SAndy Whitcroft asm|__asm__)$/x) 4744773647a0SAndy Whitcroft { 4745c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4746c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4747c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4748c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4749773647a0SAndy Whitcroft 4750773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4751c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4752c2fdda0dSAndy Whitcroft 4753c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4754c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4755773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4756c2fdda0dSAndy Whitcroft 4757c2fdda0dSAndy Whitcroft } else { 47583705ce5bSJoe Perches if (WARN("SPACING", 47593705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 47603705ce5bSJoe Perches $fix) { 4761194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47623705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 47633705ce5bSJoe Perches } 4764f0a594c1SAndy Whitcroft } 47656c72ffaaSAndy Whitcroft } 47669a4cad4eSEric Nelson 4767653d4876SAndy Whitcroft# Check operator spacing. 47680a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 47693705ce5bSJoe Perches my $fixed_line = ""; 47703705ce5bSJoe Perches my $line_fixed = 0; 47713705ce5bSJoe Perches 47729c0ca6f9SAndy Whitcroft my $ops = qr{ 47739c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 47749c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 47759c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 47761f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 477784731623SJoe Perches \?:|\?|: 47789c0ca6f9SAndy Whitcroft }x; 4779cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 47803705ce5bSJoe Perches 47813705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 47823705ce5bSJoe Perches## foreach my $el (@elements) { 47833705ce5bSJoe Perches## print("el: <$el>\n"); 47843705ce5bSJoe Perches## } 47853705ce5bSJoe Perches 47863705ce5bSJoe Perches my @fix_elements = (); 478700df344fSAndy Whitcroft my $off = 0; 47886c72ffaaSAndy Whitcroft 47893705ce5bSJoe Perches foreach my $el (@elements) { 47903705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 47913705ce5bSJoe Perches $off += length($el); 47923705ce5bSJoe Perches } 47933705ce5bSJoe Perches 47943705ce5bSJoe Perches $off = 0; 47953705ce5bSJoe Perches 47966c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4797b34c648bSJoe Perches my $last_after = -1; 47986c72ffaaSAndy Whitcroft 47990a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 48003705ce5bSJoe Perches 48013705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 48023705ce5bSJoe Perches 48033705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 48043705ce5bSJoe Perches 48054a0df2efSAndy Whitcroft $off += length($elements[$n]); 48064a0df2efSAndy Whitcroft 480725985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4808773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4809773647a0SAndy Whitcroft my $cc = ''; 4810773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4811773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4812773647a0SAndy Whitcroft } 4813773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4814773647a0SAndy Whitcroft 48154a0df2efSAndy Whitcroft my $a = ''; 48164a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 48174a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4818cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 48194a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 48204a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4821773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 48224a0df2efSAndy Whitcroft 48230a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 48244a0df2efSAndy Whitcroft 48254a0df2efSAndy Whitcroft my $c = ''; 48260a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 48274a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 48284a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4829cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 48304a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 48314a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 48328b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 48334a0df2efSAndy Whitcroft } else { 48344a0df2efSAndy Whitcroft $c = 'E'; 48350a920b5bSAndy Whitcroft } 48360a920b5bSAndy Whitcroft 48374a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 48384a0df2efSAndy Whitcroft 48394a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 48404a0df2efSAndy Whitcroft 48416c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4842de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 48430a920b5bSAndy Whitcroft 484474048ed8SAndy Whitcroft # Pull out the value of this operator. 48456c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 48460a920b5bSAndy Whitcroft 48471f65f947SAndy Whitcroft # Get the full operator variant. 48481f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 48491f65f947SAndy Whitcroft 485013214adfSAndy Whitcroft # Ignore operators passed as parameters. 485113214adfSAndy Whitcroft if ($op_type ne 'V' && 4852d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 485313214adfSAndy Whitcroft 4854cf655043SAndy Whitcroft# # Ignore comments 4855cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 485613214adfSAndy Whitcroft 4857d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 485813214adfSAndy Whitcroft } elsif ($op eq ';') { 4859cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4860cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 48613705ce5bSJoe Perches if (ERROR("SPACING", 48623705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4863b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 48643705ce5bSJoe Perches $line_fixed = 1; 48653705ce5bSJoe Perches } 4866d8aaf121SAndy Whitcroft } 4867d8aaf121SAndy Whitcroft 4868d8aaf121SAndy Whitcroft # // is a comment 4869d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 48700a920b5bSAndy Whitcroft 4871b00e4814SJoe Perches # : when part of a bitfield 4872b00e4814SJoe Perches } elsif ($opv eq ':B') { 4873b00e4814SJoe Perches # skip the bitfield test for now 4874b00e4814SJoe Perches 48751f65f947SAndy Whitcroft # No spaces for: 48761f65f947SAndy Whitcroft # -> 4877b00e4814SJoe Perches } elsif ($op eq '->') { 48784a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 48793705ce5bSJoe Perches if (ERROR("SPACING", 48803705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4881b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 48823705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 48833705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 48843705ce5bSJoe Perches } 4885b34c648bSJoe Perches $line_fixed = 1; 48863705ce5bSJoe Perches } 48870a920b5bSAndy Whitcroft } 48880a920b5bSAndy Whitcroft 48892381097bSJoe Perches # , must not have a space before and must have a space on the right. 48900a920b5bSAndy Whitcroft } elsif ($op eq ',') { 48912381097bSJoe Perches my $rtrim_before = 0; 48922381097bSJoe Perches my $space_after = 0; 48932381097bSJoe Perches if ($ctx =~ /Wx./) { 48942381097bSJoe Perches if (ERROR("SPACING", 48952381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 48962381097bSJoe Perches $line_fixed = 1; 48972381097bSJoe Perches $rtrim_before = 1; 48982381097bSJoe Perches } 48992381097bSJoe Perches } 4900cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 49013705ce5bSJoe Perches if (ERROR("SPACING", 49023705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 49033705ce5bSJoe Perches $line_fixed = 1; 4904b34c648bSJoe Perches $last_after = $n; 49052381097bSJoe Perches $space_after = 1; 49062381097bSJoe Perches } 49072381097bSJoe Perches } 49082381097bSJoe Perches if ($rtrim_before || $space_after) { 49092381097bSJoe Perches if ($rtrim_before) { 49102381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 49112381097bSJoe Perches } else { 49122381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 49132381097bSJoe Perches } 49142381097bSJoe Perches if ($space_after) { 49152381097bSJoe Perches $good .= " "; 49163705ce5bSJoe Perches } 49170a920b5bSAndy Whitcroft } 49180a920b5bSAndy Whitcroft 49199c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 492074048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 49219c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 49229c0ca6f9SAndy Whitcroft 49239c0ca6f9SAndy Whitcroft # unary operators should have a space before and 49249c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 49259c0ca6f9SAndy Whitcroft # unary operator, or a cast 49269c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 492774048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 49280d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4929cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 49303705ce5bSJoe Perches if (ERROR("SPACING", 49313705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4932b34c648bSJoe Perches if ($n != $last_after + 2) { 4933b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 49343705ce5bSJoe Perches $line_fixed = 1; 49353705ce5bSJoe Perches } 49360a920b5bSAndy Whitcroft } 4937b34c648bSJoe Perches } 4938a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4939171ae1a4SAndy Whitcroft # A unary '*' may be const 4940171ae1a4SAndy Whitcroft 4941171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 49423705ce5bSJoe Perches if (ERROR("SPACING", 49433705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4944b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 49453705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 49463705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 49473705ce5bSJoe Perches } 4948b34c648bSJoe Perches $line_fixed = 1; 49493705ce5bSJoe Perches } 49500a920b5bSAndy Whitcroft } 49510a920b5bSAndy Whitcroft 49520a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 49530a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4954773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 49553705ce5bSJoe Perches if (ERROR("SPACING", 49563705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4957b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 49583705ce5bSJoe Perches $line_fixed = 1; 49593705ce5bSJoe Perches } 49600a920b5bSAndy Whitcroft } 4961773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4962773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 49633705ce5bSJoe Perches if (ERROR("SPACING", 49643705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4965b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 49663705ce5bSJoe Perches $line_fixed = 1; 49673705ce5bSJoe Perches } 4968653d4876SAndy Whitcroft } 4969773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 49703705ce5bSJoe Perches if (ERROR("SPACING", 49713705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4972b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 49733705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 49743705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4975773647a0SAndy Whitcroft } 4976b34c648bSJoe Perches $line_fixed = 1; 49773705ce5bSJoe Perches } 49783705ce5bSJoe Perches } 49790a920b5bSAndy Whitcroft 49800a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 49819c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 49829c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 49839c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4984c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4985c2fdda0dSAndy Whitcroft $op eq '%') 49860a920b5bSAndy Whitcroft { 4987d2e025f3SJoe Perches if ($check) { 4988d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4989d2e025f3SJoe Perches if (CHK("SPACING", 4990d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4991d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4992d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4993d2e025f3SJoe Perches $line_fixed = 1; 4994d2e025f3SJoe Perches } 4995d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4996d2e025f3SJoe Perches if (CHK("SPACING", 4997d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4998d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4999d2e025f3SJoe Perches $line_fixed = 1; 5000d2e025f3SJoe Perches } 5001d2e025f3SJoe Perches } 5002d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 50033705ce5bSJoe Perches if (ERROR("SPACING", 50043705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 5005b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5006b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 5007b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5008b34c648bSJoe Perches } 50093705ce5bSJoe Perches $line_fixed = 1; 50103705ce5bSJoe Perches } 50110a920b5bSAndy Whitcroft } 50120a920b5bSAndy Whitcroft 50131f65f947SAndy Whitcroft # A colon needs no spaces before when it is 50141f65f947SAndy Whitcroft # terminating a case value or a label. 50151f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 50161f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 50173705ce5bSJoe Perches if (ERROR("SPACING", 50183705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 5019b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 50203705ce5bSJoe Perches $line_fixed = 1; 50213705ce5bSJoe Perches } 50221f65f947SAndy Whitcroft } 50231f65f947SAndy Whitcroft 50240a920b5bSAndy Whitcroft # All the others need spaces both sides. 5025cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 50261f65f947SAndy Whitcroft my $ok = 0; 50271f65f947SAndy Whitcroft 502822f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 50291f65f947SAndy Whitcroft if (($op eq '<' && 50301f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 50311f65f947SAndy Whitcroft ($op eq '>' && 50321f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 50331f65f947SAndy Whitcroft { 50341f65f947SAndy Whitcroft $ok = 1; 50351f65f947SAndy Whitcroft } 50361f65f947SAndy Whitcroft 5037e0df7e1fSJoe Perches # for asm volatile statements 5038e0df7e1fSJoe Perches # ignore a colon with another 5039e0df7e1fSJoe Perches # colon immediately before or after 5040e0df7e1fSJoe Perches if (($op eq ':') && 5041e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 5042e0df7e1fSJoe Perches $ok = 1; 5043e0df7e1fSJoe Perches } 5044e0df7e1fSJoe Perches 504584731623SJoe Perches # messages are ERROR, but ?: are CHK 50461f65f947SAndy Whitcroft if ($ok == 0) { 50470675a8fbSJean Delvare my $msg_level = \&ERROR; 50480675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 504984731623SJoe Perches 50500675a8fbSJean Delvare if (&{$msg_level}("SPACING", 50513705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 5052b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5053b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 5054b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5055b34c648bSJoe Perches } 50563705ce5bSJoe Perches $line_fixed = 1; 50573705ce5bSJoe Perches } 50580a920b5bSAndy Whitcroft } 505922f2a2efSAndy Whitcroft } 50604a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 50613705ce5bSJoe Perches 50623705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 50633705ce5bSJoe Perches 50643705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 50650a920b5bSAndy Whitcroft } 50663705ce5bSJoe Perches 50673705ce5bSJoe Perches if (($#elements % 2) == 0) { 50683705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 50693705ce5bSJoe Perches } 50703705ce5bSJoe Perches 5071194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 5072194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 50733705ce5bSJoe Perches } 50743705ce5bSJoe Perches 50753705ce5bSJoe Perches 50760a920b5bSAndy Whitcroft } 50770a920b5bSAndy Whitcroft 5078786b6326SJoe Perches# check for whitespace before a non-naked semicolon 5079d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 5080786b6326SJoe Perches if (WARN("SPACING", 5081786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 5082786b6326SJoe Perches $fix) { 5083194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 5084786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 5085786b6326SJoe Perches } 5086786b6326SJoe Perches } 5087786b6326SJoe Perches 5088f0a594c1SAndy Whitcroft# check for multiple assignments 5089f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 5090000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 5091000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 5092f0a594c1SAndy Whitcroft } 5093f0a594c1SAndy Whitcroft 509422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 509522f2a2efSAndy Whitcroft## # continuation. 509622f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 509722f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 509822f2a2efSAndy Whitcroft## 509922f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 5100e73d2715SDwaipayan Ray## # falsely report the parameters of functions. 510122f2a2efSAndy Whitcroft## my $ln = $line; 510222f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 510322f2a2efSAndy Whitcroft## } 510422f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 5105000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 5106000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 510722f2a2efSAndy Whitcroft## } 510822f2a2efSAndy Whitcroft## } 5109f0a594c1SAndy Whitcroft 51100a920b5bSAndy Whitcroft#need space before brace following if, while, etc 51116b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 51126ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 51133705ce5bSJoe Perches if (ERROR("SPACING", 51143705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 51153705ce5bSJoe Perches $fix) { 51166ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 51173705ce5bSJoe Perches } 5118de7d4f0eSAndy Whitcroft } 5119de7d4f0eSAndy Whitcroft 5120c4a62ef9SJoe Perches## # check for blank lines before declarations 5121c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 5122c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 5123c4a62ef9SJoe Perches## WARN("SPACING", 5124c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 5125c4a62ef9SJoe Perches## } 5126c4a62ef9SJoe Perches## 5127c4a62ef9SJoe Perches 5128de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 5129de7d4f0eSAndy Whitcroft# on the line 513094fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 5131d5e616fcSJoe Perches if (ERROR("SPACING", 5132d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 5133d5e616fcSJoe Perches $fix) { 5134194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5135d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 5136d5e616fcSJoe Perches } 51370a920b5bSAndy Whitcroft } 51380a920b5bSAndy Whitcroft 513922f2a2efSAndy Whitcroft# check spacing on square brackets 514022f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 51413705ce5bSJoe Perches if (ERROR("SPACING", 51423705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 51433705ce5bSJoe Perches $fix) { 5144194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51453705ce5bSJoe Perches s/\[\s+/\[/; 51463705ce5bSJoe Perches } 514722f2a2efSAndy Whitcroft } 514822f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 51493705ce5bSJoe Perches if (ERROR("SPACING", 51503705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 51513705ce5bSJoe Perches $fix) { 5152194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51533705ce5bSJoe Perches s/\s+\]/\]/; 51543705ce5bSJoe Perches } 515522f2a2efSAndy Whitcroft } 515622f2a2efSAndy Whitcroft 5157c45dcabdSAndy Whitcroft# check spacing on parentheses 51589c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 51599c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 51603705ce5bSJoe Perches if (ERROR("SPACING", 51613705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 51623705ce5bSJoe Perches $fix) { 5163194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51643705ce5bSJoe Perches s/\(\s+/\(/; 51653705ce5bSJoe Perches } 516622f2a2efSAndy Whitcroft } 516713214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 5168c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 5169c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 51703705ce5bSJoe Perches if (ERROR("SPACING", 51713705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 51723705ce5bSJoe Perches $fix) { 5173194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51743705ce5bSJoe Perches s/\s+\)/\)/; 51753705ce5bSJoe Perches } 517622f2a2efSAndy Whitcroft } 517722f2a2efSAndy Whitcroft 5178e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 5179e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 5180e2826fd0SJoe Perches 5181e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 5182ea4acbb1SJoe Perches my $var = $1; 5183ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5184ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 5185ea4acbb1SJoe Perches $fix) { 5186ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 5187ea4acbb1SJoe Perches } 5188ea4acbb1SJoe Perches } 5189ea4acbb1SJoe Perches 5190ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 5191ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 5192ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 5193ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 5194ea4acbb1SJoe Perches my $var = $2; 5195ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5196ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 5197ea4acbb1SJoe Perches $fix) { 5198ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 5199ea4acbb1SJoe Perches $var2 =~ s/\s//g; 5200ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 5201ea4acbb1SJoe Perches } 5202e2826fd0SJoe Perches } 5203e2826fd0SJoe Perches 520463b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 5205a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 5206a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 52075b57980dSJoe Perches $perl_version_ok && defined($stat) && 520863b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 520963b7c73eSJoe Perches my $if_stat = $1; 521063b7c73eSJoe Perches my $test = substr($2, 1, -1); 521163b7c73eSJoe Perches my $herectx; 521263b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 521363b7c73eSJoe Perches my $match = $1; 521463b7c73eSJoe Perches # avoid parentheses around potential macro args 521563b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 521663b7c73eSJoe Perches if (!defined($herectx)) { 521763b7c73eSJoe Perches $herectx = $here . "\n"; 521863b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 521963b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 522063b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 522163b7c73eSJoe Perches $herectx .= $rl . "\n"; 522263b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 522363b7c73eSJoe Perches } 522463b7c73eSJoe Perches } 522563b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 522663b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 522763b7c73eSJoe Perches } 522863b7c73eSJoe Perches } 522963b7c73eSJoe Perches 52300a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 52314a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 52320a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 52333705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 52343705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 52353705ce5bSJoe Perches $fix) { 5236194f66fcSJoe Perches $fixed[$fixlinenr] =~ 52373705ce5bSJoe Perches s/^(.)\s+/$1/; 52383705ce5bSJoe Perches } 52390a920b5bSAndy Whitcroft } 52400a920b5bSAndy Whitcroft 524140873abaSJoe Perches# check if a statement with a comma should be two statements like: 524240873abaSJoe Perches# foo = bar(), /* comma should be semicolon */ 524340873abaSJoe Perches# bar = baz(); 524440873abaSJoe Perches if (defined($stat) && 524540873abaSJoe Perches $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { 524640873abaSJoe Perches my $cnt = statement_rawlines($stat); 524740873abaSJoe Perches my $herectx = get_stat_here($linenr, $cnt, $here); 524840873abaSJoe Perches WARN("SUSPECT_COMMA_SEMICOLON", 524940873abaSJoe Perches "Possible comma where semicolon could be used\n" . $herectx); 525040873abaSJoe Perches } 525140873abaSJoe Perches 52525b9553abSJoe Perches# return is not a function 5253507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 5254c45dcabdSAndy Whitcroft my $spacing = $1; 52555b57980dSJoe Perches if ($perl_version_ok && 52565b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 52575b9553abSJoe Perches my $value = $1; 52585b9553abSJoe Perches $value = deparenthesize($value); 52595b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 5260000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 5261000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 52625b9553abSJoe Perches } 5263c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 5264000d1cc1SJoe Perches ERROR("SPACING", 5265000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 5266c45dcabdSAndy Whitcroft } 5267c45dcabdSAndy Whitcroft } 5268507e5141SJoe Perches 5269b43ae21bSJoe Perches# unnecessary return in a void function 5270b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 5271b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 5272b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 5273b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 5274b43ae21bSJoe Perches $linenr >= 3 && 5275b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 5276b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 52779819cf25SJoe Perches WARN("RETURN_VOID", 5278b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 52799819cf25SJoe Perches } 52809819cf25SJoe Perches 5281189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 52825b57980dSJoe Perches if ($perl_version_ok && 5283189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 5284189248d8SJoe Perches my $openparens = $1; 5285189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 5286189248d8SJoe Perches my $msg = ""; 5287189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 5288189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 5289189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 5290189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 5291189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 5292189248d8SJoe Perches } 5293189248d8SJoe Perches } 5294189248d8SJoe Perches 5295c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 5296c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 5297c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 5298c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 52995b57980dSJoe Perches if ($perl_version_ok && 5300c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 5301c5595fa2SJoe Perches my $lead = $1; 5302c5595fa2SJoe Perches my $const = $2; 5303c5595fa2SJoe Perches my $comp = $3; 5304c5595fa2SJoe Perches my $to = $4; 5305c5595fa2SJoe Perches my $newcomp = $comp; 5306f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 5307c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 5308c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 5309c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 5310c5595fa2SJoe Perches $fix) { 5311c5595fa2SJoe Perches if ($comp eq "<") { 5312c5595fa2SJoe Perches $newcomp = ">"; 5313c5595fa2SJoe Perches } elsif ($comp eq "<=") { 5314c5595fa2SJoe Perches $newcomp = ">="; 5315c5595fa2SJoe Perches } elsif ($comp eq ">") { 5316c5595fa2SJoe Perches $newcomp = "<"; 5317c5595fa2SJoe Perches } elsif ($comp eq ">=") { 5318c5595fa2SJoe Perches $newcomp = "<="; 5319c5595fa2SJoe Perches } 5320c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 5321c5595fa2SJoe Perches } 5322c5595fa2SJoe Perches } 5323c5595fa2SJoe Perches 5324f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 5325f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 532653a3c448SAndy Whitcroft my $name = $1; 532753a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 5328000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 5329f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 533053a3c448SAndy Whitcroft } 533153a3c448SAndy Whitcroft } 5332c45dcabdSAndy Whitcroft 53330a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 53344a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 53353705ce5bSJoe Perches if (ERROR("SPACING", 53363705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 53373705ce5bSJoe Perches $fix) { 5338194f66fcSJoe Perches $fixed[$fixlinenr] =~ 53393705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 53403705ce5bSJoe Perches } 53410a920b5bSAndy Whitcroft } 53420a920b5bSAndy Whitcroft 5343f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 5344f5fe35ddSAndy Whitcroft# statements after the conditional. 5345170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 53463e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 53473e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 53483e469cdcSAndy Whitcroft if (!defined $stat); 5349170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 5350170d3a22SAndy Whitcroft $remain_next, $off_next); 5351170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 5352170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 5353170d3a22SAndy Whitcroft 5354170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 5355170d3a22SAndy Whitcroft # If the statement carries leading newlines, 5356170d3a22SAndy Whitcroft # then count those as offsets. 5357170d3a22SAndy Whitcroft my ($whitespace) = 5358170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 5359170d3a22SAndy Whitcroft my $offset = 5360170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 5361170d3a22SAndy Whitcroft 5362170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 5363170d3a22SAndy Whitcroft $offset} = 1; 5364170d3a22SAndy Whitcroft } 5365170d3a22SAndy Whitcroft } 5366170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 5367c11230f4SJoe Perches defined($stat) && defined($cond) && 5368170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 5369171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 53708905a67cSAndy Whitcroft 5371b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 537265b64b3bSJoe Perches if (ERROR("ASSIGN_IN_IF", 537365b64b3bSJoe Perches "do not use assignment in if condition\n" . $herecurr) && 537465b64b3bSJoe Perches $fix && $perl_version_ok) { 537565b64b3bSJoe Perches if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { 537665b64b3bSJoe Perches my $space = $1; 537765b64b3bSJoe Perches my $not = $2; 537865b64b3bSJoe Perches my $statement = $3; 537965b64b3bSJoe Perches my $assigned = $4; 538065b64b3bSJoe Perches my $test = $8; 538165b64b3bSJoe Perches my $against = $9; 538265b64b3bSJoe Perches my $brace = $15; 538365b64b3bSJoe Perches fix_delete_line($fixlinenr, $rawline); 538465b64b3bSJoe Perches fix_insert_line($fixlinenr, "$space$statement;"); 538565b64b3bSJoe Perches my $newline = "${space}if ("; 538665b64b3bSJoe Perches $newline .= '!' if defined($not); 538765b64b3bSJoe Perches $newline .= '(' if (defined $not && defined($test) && defined($against)); 538865b64b3bSJoe Perches $newline .= "$assigned"; 538965b64b3bSJoe Perches $newline .= " $test $against" if (defined($test) && defined($against)); 539065b64b3bSJoe Perches $newline .= ')' if (defined $not && defined($test) && defined($against)); 539165b64b3bSJoe Perches $newline .= ')'; 539265b64b3bSJoe Perches $newline .= " {" if (defined($brace)); 539365b64b3bSJoe Perches fix_insert_line($fixlinenr + 1, $newline); 539465b64b3bSJoe Perches } 539565b64b3bSJoe Perches } 53968905a67cSAndy Whitcroft } 53978905a67cSAndy Whitcroft 53988905a67cSAndy Whitcroft # Find out what is on the end of the line after the 53998905a67cSAndy Whitcroft # conditional. 5400773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 54018905a67cSAndy Whitcroft $s =~ s/\n.*//g; 540213214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 540353210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 540453210168SAndy Whitcroft $c !~ /}\s*while\s*/) 5405773647a0SAndy Whitcroft { 5406bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 5407bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 5408bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 540942bdf74cSHidetoshi Seto my $stat_real = ''; 5410bb44ad39SAndy Whitcroft 541142bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 541242bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 5413bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 5414bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 5415bb44ad39SAndy Whitcroft } 5416bb44ad39SAndy Whitcroft 5417000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5418000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 54198905a67cSAndy Whitcroft } 54208905a67cSAndy Whitcroft } 54218905a67cSAndy Whitcroft 542213214adfSAndy Whitcroft# Check for bitwise tests written as boolean 542313214adfSAndy Whitcroft if ($line =~ / 542413214adfSAndy Whitcroft (?: 542513214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 542613214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 542713214adfSAndy Whitcroft (?:\&\&|\|\|) 542813214adfSAndy Whitcroft | 542913214adfSAndy Whitcroft (?:\&\&|\|\|) 543013214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 543113214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 543213214adfSAndy Whitcroft )/x) 543313214adfSAndy Whitcroft { 5434000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 5435000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 543613214adfSAndy Whitcroft } 543713214adfSAndy Whitcroft 54388905a67cSAndy Whitcroft# if and else should not have general statements after it 543913214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 544013214adfSAndy Whitcroft my $s = $1; 544113214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 544213214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5443000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5444000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 54450a920b5bSAndy Whitcroft } 544613214adfSAndy Whitcroft } 544739667782SAndy Whitcroft# if should not continue a brace 544839667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5449000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5450048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 545139667782SAndy Whitcroft $herecurr); 545239667782SAndy Whitcroft } 5453a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5454a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5455a1080bf8SAndy Whitcroft $line !~ /\G(?: 54563fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5457a1080bf8SAndy Whitcroft \s*return\s+ 5458a1080bf8SAndy Whitcroft )/xg) 5459a1080bf8SAndy Whitcroft { 5460000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5461000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5462a1080bf8SAndy Whitcroft } 54630a920b5bSAndy Whitcroft 54640a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 54650a920b5bSAndy Whitcroft # indent level to be relevant to each other. 54668b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 54670a920b5bSAndy Whitcroft $previndent == $indent) { 54688b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 54698b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 54708b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 54718b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 54728b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 54738b8856f4SJoe Perches my $fixedline = $prevrawline; 54748b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 54758b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 54768b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 54778b8856f4SJoe Perches } 54788b8856f4SJoe Perches $fixedline = $rawline; 54798b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 54808b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 54818b8856f4SJoe Perches } 54820a920b5bSAndy Whitcroft } 54830a920b5bSAndy Whitcroft 54848b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5485c2fdda0dSAndy Whitcroft $previndent == $indent) { 5486c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5487c2fdda0dSAndy Whitcroft 5488c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5489c2fdda0dSAndy Whitcroft # conditional. 5490773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5491c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5492c2fdda0dSAndy Whitcroft 5493c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 54948b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 54958b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 54968b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 54978b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 54988b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 54998b8856f4SJoe Perches my $fixedline = $prevrawline; 55008b8856f4SJoe Perches my $trailing = $rawline; 55018b8856f4SJoe Perches $trailing =~ s/^\+//; 55028b8856f4SJoe Perches $trailing = trim($trailing); 55038b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 55048b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 55058b8856f4SJoe Perches } 5506c2fdda0dSAndy Whitcroft } 5507c2fdda0dSAndy Whitcroft } 5508c2fdda0dSAndy Whitcroft 550995e2c602SJoe Perches#Specific variable tests 5510323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5511323c1260SJoe Perches my $var = $1; 551295e2c602SJoe Perches 551395e2c602SJoe Perches#CamelCase 5514807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5515be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 55164104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values 55174104a206SŁukasz Stelmach $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ && 551822735ce8SJoe Perches#Ignore Page<foo> variants 5519807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5520d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5521d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5522d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5523f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5524f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 55257e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 55267e781f67SJoe Perches my $word = $1; 55277e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5528d8b07710SJoe Perches if ($check) { 5529d8b07710SJoe Perches seed_camelcase_includes(); 5530d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5531d8b07710SJoe Perches seed_camelcase_file($realfile); 5532d8b07710SJoe Perches $camelcase_file_seeded = 1; 5533d8b07710SJoe Perches } 5534d8b07710SJoe Perches } 55357e781f67SJoe Perches if (!defined $camelcase{$word}) { 55367e781f67SJoe Perches $camelcase{$word} = 1; 5537be79794bSJoe Perches CHK("CAMELCASE", 55387e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 55397e781f67SJoe Perches } 5540323c1260SJoe Perches } 5541323c1260SJoe Perches } 55423445686aSJoe Perches } 55430a920b5bSAndy Whitcroft 55440a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5545d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5546d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5547d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5548d5e616fcSJoe Perches $fix) { 5549194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5550d5e616fcSJoe Perches } 55510a920b5bSAndy Whitcroft } 55520a920b5bSAndy Whitcroft 55530e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 55540e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5555c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5556e09dec48SAndy Whitcroft my $file = "$1.h"; 5557e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5558e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5559e09dec48SAndy Whitcroft $realfile ne $checkfile && 55607840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5561c45dcabdSAndy Whitcroft { 55620e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 55630e212e0aSFabian Frederick if ($asminclude > 0) { 5564e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5565000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5566000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5567e09dec48SAndy Whitcroft } else { 5568000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5569000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5570e09dec48SAndy Whitcroft } 55710a920b5bSAndy Whitcroft } 55720a920b5bSAndy Whitcroft } 55730e212e0aSFabian Frederick } 55740a920b5bSAndy Whitcroft 5575653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5576653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5577cf655043SAndy Whitcroft# in a known good container 5578b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5579b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5580d8aaf121SAndy Whitcroft my $ln = $linenr; 5581d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5582c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5583c45dcabdSAndy Whitcroft my $ctx = ''; 558408a2843eSJoe Perches my $has_flow_statement = 0; 558508a2843eSJoe Perches my $has_arg_concat = 0; 5586c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5587f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5588f74bd194SAndy Whitcroft $ctx = $dstat; 5589c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5590a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5591c45dcabdSAndy Whitcroft 559208a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 559362e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 559408a2843eSJoe Perches 5595f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5596f59b64bfSJoe Perches my $define_args = $1; 5597f59b64bfSJoe Perches my $define_stmt = $dstat; 5598f59b64bfSJoe Perches my @def_args = (); 5599f59b64bfSJoe Perches 5600f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5601f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5602f59b64bfSJoe Perches $define_args =~ s/\s*//g; 56038c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5604f59b64bfSJoe Perches @def_args = split(",", $define_args); 5605f59b64bfSJoe Perches } 5606f59b64bfSJoe Perches 5607292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5608c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5609c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5610c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5611c45dcabdSAndy Whitcroft 5612c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 56132e44e803SDwaipayan Ray while ($dstat =~ s/\([^\(\)]*\)/1u/ || 56142e44e803SDwaipayan Ray $dstat =~ s/\{[^\{\}]*\}/1u/ || 56152e44e803SDwaipayan Ray $dstat =~ s/.\[[^\[\]]*\]/1u/) 5616bf30d6edSAndy Whitcroft { 5617c45dcabdSAndy Whitcroft } 5618c45dcabdSAndy Whitcroft 5619342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 562033acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 562133acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5622e45bab8eSAndy Whitcroft { 5623e45bab8eSAndy Whitcroft } 5624e45bab8eSAndy Whitcroft 562542e15293SJoe Perches # Make asm volatile uses seem like a generic function 562642e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 562742e15293SJoe Perches 5628c45dcabdSAndy Whitcroft my $exceptions = qr{ 5629c45dcabdSAndy Whitcroft $Declare| 5630c45dcabdSAndy Whitcroft module_param_named| 5631a0a0a7a9SKees Cook MODULE_PARM_DESC| 5632c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5633c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5634383099fdSAndy Whitcroft __typeof__\(| 563522fd2d3eSStefani Seibold union| 563622fd2d3eSStefani Seibold struct| 5637ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 56386b10df42SVladimir Zapolskiy ^\"|\"$| 56396b10df42SVladimir Zapolskiy ^\[ 5640c45dcabdSAndy Whitcroft }x; 56415eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5642f59b64bfSJoe Perches 5643f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5644f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5645e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5646f59b64bfSJoe Perches 5647f74bd194SAndy Whitcroft if ($dstat ne '' && 5648f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5649f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 56503cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5651356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5652f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5653f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5654e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 565572f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 56562e44e803SDwaipayan Ray $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} 5657f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5658f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5659f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 56604e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5661f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5662c45dcabdSAndy Whitcroft { 5663e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5664e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5665e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5666e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5667f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5668f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5669f74bd194SAndy Whitcroft } else { 5670000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5671388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5672d8aaf121SAndy Whitcroft } 5673f59b64bfSJoe Perches 5674f59b64bfSJoe Perches } 56755207649bSJoe Perches 56765207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 56775207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 56785207649bSJoe Perches my $first = 1; 56795207649bSJoe Perches $define_stmt = ""; 56805207649bSJoe Perches foreach my $l (@stmt_array) { 56815207649bSJoe Perches $l =~ s/\\$//; 56825207649bSJoe Perches if ($first) { 56835207649bSJoe Perches $define_stmt = $l; 56845207649bSJoe Perches $first = 0; 56855207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 56865207649bSJoe Perches $define_stmt .= substr($l, 1); 56875207649bSJoe Perches } 56885207649bSJoe Perches } 56895207649bSJoe Perches $define_stmt =~ s/$;//g; 56905207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 56915207649bSJoe Perches $define_stmt = trim($define_stmt); 56925207649bSJoe Perches 5693f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5694f59b64bfSJoe Perches foreach my $arg (@def_args) { 5695f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 56969192d41aSJoe Perches next if ($arg =~ /^type$/i); 56977fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 56986dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 56997fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 57007fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5701d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5702f59b64bfSJoe Perches if ($use_cnt > 1) { 5703f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5704f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5705f59b64bfSJoe Perches } 57069192d41aSJoe Perches# check if any macro arguments may have other precedence issues 57077fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 57089192d41aSJoe Perches ((defined($1) && $1 ne ',') || 57099192d41aSJoe Perches (defined($2) && $2 ne ','))) { 57109192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 57119192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 57129192d41aSJoe Perches } 57130a920b5bSAndy Whitcroft } 57145023d347SJoe Perches 571508a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 571608a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 571708a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 571808a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5719e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 572008a2843eSJoe Perches 572108a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 572208a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 572308a2843eSJoe Perches } 572408a2843eSJoe Perches 5725481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 57265023d347SJoe Perches 57275023d347SJoe Perches } else { 57285023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5729481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5730481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 57315023d347SJoe Perches $line =~ /^\+.*\\$/) { 57325023d347SJoe Perches WARN("LINE_CONTINUATIONS", 57335023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 57345023d347SJoe Perches } 5735653d4876SAndy Whitcroft } 57360a920b5bSAndy Whitcroft 5737b13edf7fSJoe Perches# do {} while (0) macro tests: 5738b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5739b13edf7fSJoe Perches# macro should not end with a semicolon 57405b57980dSJoe Perches if ($perl_version_ok && 5741b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5742b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5743b13edf7fSJoe Perches my $ln = $linenr; 5744b13edf7fSJoe Perches my $cnt = $realcnt; 5745b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5746b13edf7fSJoe Perches my $ctx = ''; 5747b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5748b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5749b13edf7fSJoe Perches $ctx = $dstat; 5750b13edf7fSJoe Perches 5751b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 57521b36b201SJoe Perches $dstat =~ s/$;/ /g; 5753b13edf7fSJoe Perches 5754b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5755b13edf7fSJoe Perches my $stmts = $2; 5756b13edf7fSJoe Perches my $semis = $3; 5757b13edf7fSJoe Perches 5758b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5759b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5760e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5761b13edf7fSJoe Perches 5762ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5763ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5764b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5765b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5766b13edf7fSJoe Perches } 5767b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5768b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5769b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5770b13edf7fSJoe Perches } 5771f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5772f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5773f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5774e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5775f5ef95b1SJoe Perches 5776f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5777f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5778b13edf7fSJoe Perches } 5779b13edf7fSJoe Perches } 5780b13edf7fSJoe Perches 5781f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 578213214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 578313214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5784cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 578513214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5786cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5787cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5788aad4f614SJoe Perches my @allowed = (); 5789aad4f614SJoe Perches my $allow = 0; 579013214adfSAndy Whitcroft my $seen = 0; 5791773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5792cf655043SAndy Whitcroft my $ln = $linenr - 1; 579313214adfSAndy Whitcroft for my $chunk (@chunks) { 579413214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 579513214adfSAndy Whitcroft 5796773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5797773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5798773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5799773647a0SAndy Whitcroft 5800aad4f614SJoe Perches $allowed[$allow] = 0; 5801773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5802773647a0SAndy Whitcroft 5803773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5804773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5805773647a0SAndy Whitcroft 5806773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5807cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5808cf655043SAndy Whitcroft 5809773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 581013214adfSAndy Whitcroft 581113214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 581213214adfSAndy Whitcroft 5813aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5814cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5815cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5816aad4f614SJoe Perches $allowed[$allow] = 1; 581713214adfSAndy Whitcroft } 581813214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5819cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5820aad4f614SJoe Perches $allowed[$allow] = 1; 582113214adfSAndy Whitcroft } 5822cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5823cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5824aad4f614SJoe Perches $allowed[$allow] = 1; 582513214adfSAndy Whitcroft } 5826aad4f614SJoe Perches $allow++; 582713214adfSAndy Whitcroft } 5828aad4f614SJoe Perches if ($seen) { 5829aad4f614SJoe Perches my $sum_allowed = 0; 5830aad4f614SJoe Perches foreach (@allowed) { 5831aad4f614SJoe Perches $sum_allowed += $_; 5832aad4f614SJoe Perches } 5833aad4f614SJoe Perches if ($sum_allowed == 0) { 5834000d1cc1SJoe Perches WARN("BRACES", 5835000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5836aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5837aad4f614SJoe Perches $seen != $allow) { 5838aad4f614SJoe Perches CHK("BRACES", 5839aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5840aad4f614SJoe Perches } 584113214adfSAndy Whitcroft } 584213214adfSAndy Whitcroft } 584313214adfSAndy Whitcroft } 5844773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 584513214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5846cf655043SAndy Whitcroft my $allowed = 0; 5847f0a594c1SAndy Whitcroft 5848cf655043SAndy Whitcroft # Check the pre-context. 5849cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5850cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5851cf655043SAndy Whitcroft $allowed = 1; 5852f0a594c1SAndy Whitcroft } 5853773647a0SAndy Whitcroft 5854773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5855773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5856773647a0SAndy Whitcroft 5857cf655043SAndy Whitcroft # Check the condition. 5858cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5859773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5860cf655043SAndy Whitcroft if (defined $cond) { 5861773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5862cf655043SAndy Whitcroft } 5863cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5864cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5865cf655043SAndy Whitcroft $allowed = 1; 5866cf655043SAndy Whitcroft } 5867cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5868cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5869cf655043SAndy Whitcroft $allowed = 1; 5870cf655043SAndy Whitcroft } 5871cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5872cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5873cf655043SAndy Whitcroft $allowed = 1; 5874cf655043SAndy Whitcroft } 5875cf655043SAndy Whitcroft # Check the post-context. 5876cf655043SAndy Whitcroft if (defined $chunks[1]) { 5877cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5878cf655043SAndy Whitcroft if (defined $cond) { 5879773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5880cf655043SAndy Whitcroft } 5881cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5882cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5883cf655043SAndy Whitcroft $allowed = 1; 5884cf655043SAndy Whitcroft } 5885cf655043SAndy Whitcroft } 5886cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5887f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5888e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5889cf655043SAndy Whitcroft 5890000d1cc1SJoe Perches WARN("BRACES", 5891000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5892f0a594c1SAndy Whitcroft } 5893f0a594c1SAndy Whitcroft } 5894f0a594c1SAndy Whitcroft 5895e4c5babdSJoe Perches# check for single line unbalanced braces 589695330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 589795330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5898e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5899e4c5babdSJoe Perches } 5900e4c5babdSJoe Perches 59010979ae66SJoe Perches# check for unnecessary blank lines around braces 590277b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5903f8e58219SJoe Perches if (CHK("BRACES", 5904f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5905f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5906f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5907f8e58219SJoe Perches } 59080979ae66SJoe Perches } 590977b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5910f8e58219SJoe Perches if (CHK("BRACES", 5911f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5912f8e58219SJoe Perches $fix) { 5913f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5914f8e58219SJoe Perches } 59150979ae66SJoe Perches } 59160979ae66SJoe Perches 59174a0df2efSAndy Whitcroft# no volatiles please 59186c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 59196c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5920000d1cc1SJoe Perches WARN("VOLATILE", 59218c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 59224a0df2efSAndy Whitcroft } 59234a0df2efSAndy Whitcroft 59245e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 59255e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 59265e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 59275e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 592833acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 59295e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 59305e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 59315e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 59325e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 59335e4f6ba5SJoe Perches $fix && 59345e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 59355e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 59365e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 59375e4f6ba5SJoe Perches my $comma_close = ""; 59385e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 59395e4f6ba5SJoe Perches $comma_close = $1; 59405e4f6ba5SJoe Perches } 59415e4f6ba5SJoe Perches 59425e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 59435e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 59445e4f6ba5SJoe Perches my $fixedline = $prevrawline; 59455e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 59465e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 59475e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 59485e4f6ba5SJoe Perches $fixedline = $rawline; 59495e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 59505e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 59515e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 59525e4f6ba5SJoe Perches } 59535e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 59545e4f6ba5SJoe Perches } 59555e4f6ba5SJoe Perches } 59565e4f6ba5SJoe Perches 59575e4f6ba5SJoe Perches# check for missing a space in a string concatenation 59585e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 59595e4f6ba5SJoe Perches WARN('MISSING_SPACE', 59605e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 59615e4f6ba5SJoe Perches } 59625e4f6ba5SJoe Perches 596377cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5964e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5965e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5966e4b7d309SJoe Perches# function declarations 596777cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 596877cb8546SJoe Perches defined($context_function) && 5969e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5970e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 597177cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5972e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 597377cb8546SJoe Perches } 597477cb8546SJoe Perches 59755e4f6ba5SJoe Perches# check for spaces before a quoted newline 59765e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 59775e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 59785e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 59795e4f6ba5SJoe Perches $fix) { 59805e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 59815e4f6ba5SJoe Perches } 59825e4f6ba5SJoe Perches 59835e4f6ba5SJoe Perches } 59845e4f6ba5SJoe Perches 5985f17dba4fSJoe Perches# concatenated string without spaces between elements 598679682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 598779682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 598879682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 598979682c0cSJoe Perches $fix) { 599079682c0cSJoe Perches while ($line =~ /($String)/g) { 599179682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 599279682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 599379682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 599479682c0cSJoe Perches } 599579682c0cSJoe Perches } 5996f17dba4fSJoe Perches } 5997f17dba4fSJoe Perches 599890ad30e5SJoe Perches# uncoalesced string fragments 599933acb54aSJoe Perches if ($line =~ /$String\s*"/) { 600079682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 600179682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 600279682c0cSJoe Perches $fix) { 600379682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 600479682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 600579682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 600679682c0cSJoe Perches } 600779682c0cSJoe Perches } 600890ad30e5SJoe Perches } 600990ad30e5SJoe Perches 6010522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 6011522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 6012522b837cSAlexey Dobriyan my $show_Z = 1; 60135e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 6014522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 60155e4f6ba5SJoe Perches $string =~ s/%%/__/g; 6016522b837cSAlexey Dobriyan # check for %L 6017522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 60185e4f6ba5SJoe Perches WARN("PRINTF_L", 6019522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 6020522b837cSAlexey Dobriyan $show_L = 0; 60215e4f6ba5SJoe Perches } 6022522b837cSAlexey Dobriyan # check for %Z 6023522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 6024522b837cSAlexey Dobriyan WARN("PRINTF_Z", 6025522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 6026522b837cSAlexey Dobriyan $show_Z = 0; 6027522b837cSAlexey Dobriyan } 6028522b837cSAlexey Dobriyan # check for 0x<decimal> 6029522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 6030522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 60316e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 60326e300757SJoe Perches } 60335e4f6ba5SJoe Perches } 60345e4f6ba5SJoe Perches 60355e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 60363f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 60375e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 60385e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 60395e4f6ba5SJoe Perches } 60405e4f6ba5SJoe Perches 604100df344fSAndy Whitcroft# warn about #if 0 6042c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 604360f89010SPrakruthi Deepak Heragu WARN("IF_0", 604460f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 604560f89010SPrakruthi Deepak Heragu } 604660f89010SPrakruthi Deepak Heragu 604760f89010SPrakruthi Deepak Heragu# warn about #if 1 604860f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 604960f89010SPrakruthi Deepak Heragu WARN("IF_1", 605060f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 60514a0df2efSAndy Whitcroft } 60524a0df2efSAndy Whitcroft 605303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 605403df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 6055100425deSJoe Perches my $tested = quotemeta($1); 6056100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 6057100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 6058100425deSJoe Perches my $func = $1; 6059100425deSJoe Perches if (WARN('NEEDLESS_IF', 6060100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 6061100425deSJoe Perches $fix) { 6062100425deSJoe Perches my $do_fix = 1; 6063100425deSJoe Perches my $leading_tabs = ""; 6064100425deSJoe Perches my $new_leading_tabs = ""; 6065100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 6066100425deSJoe Perches $leading_tabs = $1; 6067100425deSJoe Perches } else { 6068100425deSJoe Perches $do_fix = 0; 6069100425deSJoe Perches } 6070100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 6071100425deSJoe Perches $new_leading_tabs = $1; 6072100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 6073100425deSJoe Perches $do_fix = 0; 6074100425deSJoe Perches } 6075100425deSJoe Perches } else { 6076100425deSJoe Perches $do_fix = 0; 6077100425deSJoe Perches } 6078100425deSJoe Perches if ($do_fix) { 6079100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 6080100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 6081100425deSJoe Perches } 6082100425deSJoe Perches } 60834c432a8fSGreg Kroah-Hartman } 60844c432a8fSGreg Kroah-Hartman } 6085f0a594c1SAndy Whitcroft 6086ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 6087ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 6088ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 6089ebfdc409SJoe Perches (defined $1 || defined $3) && 6090ebfdc409SJoe Perches $linenr > 3) { 6091ebfdc409SJoe Perches my $testval = $2; 6092ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 6093ebfdc409SJoe Perches 6094ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 6095ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 6096ebfdc409SJoe Perches 6097e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 6098e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 6099ebfdc409SJoe Perches WARN("OOM_MESSAGE", 6100ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 6101ebfdc409SJoe Perches } 6102ebfdc409SJoe Perches } 6103ebfdc409SJoe Perches 6104f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 6105dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 6106f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 6107f78d98f6SJoe Perches my $level = $1; 6108f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 6109f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 6110f78d98f6SJoe Perches $fix) { 6111f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 6112f78d98f6SJoe Perches } 6113f78d98f6SJoe Perches } 6114f78d98f6SJoe Perches 611545c55e92SJoe Perches# check for logging continuations 611645c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 611745c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 611845c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 611945c55e92SJoe Perches } 612045c55e92SJoe Perches 612170eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions 612270eb2275SDwaipayan Ray if (defined $stat && 612370eb2275SDwaipayan Ray $line =~ /\b$logFunctions\s*\(/ && 612470eb2275SDwaipayan Ray index($stat, '"') >= 0) { 612570eb2275SDwaipayan Ray my $lc = $stat =~ tr@\n@@; 612670eb2275SDwaipayan Ray $lc = $lc + $linenr; 612770eb2275SDwaipayan Ray my $stat_real = get_stat_real($linenr, $lc); 612870eb2275SDwaipayan Ray pos($stat_real) = index($stat_real, '"'); 612970eb2275SDwaipayan Ray while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { 613070eb2275SDwaipayan Ray my $pspec = $1; 613170eb2275SDwaipayan Ray my $h = $2; 613270eb2275SDwaipayan Ray my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; 613370eb2275SDwaipayan Ray if (WARN("UNNECESSARY_MODIFIER", 613470eb2275SDwaipayan Ray "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && 613570eb2275SDwaipayan Ray $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { 613670eb2275SDwaipayan Ray my $nspec = $pspec; 613770eb2275SDwaipayan Ray $nspec =~ s/h//g; 613870eb2275SDwaipayan Ray $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; 613970eb2275SDwaipayan Ray } 614070eb2275SDwaipayan Ray } 614170eb2275SDwaipayan Ray } 614270eb2275SDwaipayan Ray 6143abb08a53SJoe Perches# check for mask then right shift without a parentheses 61445b57980dSJoe Perches if ($perl_version_ok && 6145abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 6146abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 6147abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 6148abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 6149abb08a53SJoe Perches } 6150abb08a53SJoe Perches 6151b75ac618SJoe Perches# check for pointer comparisons to NULL 61525b57980dSJoe Perches if ($perl_version_ok) { 6153b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 6154b75ac618SJoe Perches my $val = $1; 6155b75ac618SJoe Perches my $equal = "!"; 6156b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 6157b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 6158b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 6159b75ac618SJoe Perches $fix) { 6160b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 6161b75ac618SJoe Perches } 6162b75ac618SJoe Perches } 6163b75ac618SJoe Perches } 6164b75ac618SJoe Perches 61658716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 61668716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 61678716de38SJoe Perches my $attr = $1; 61688716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 61698716de38SJoe Perches my $ptr = $1; 61708716de38SJoe Perches my $var = $2; 61718716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 61728716de38SJoe Perches ERROR("MISPLACED_INIT", 61738716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 61748716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 61758716de38SJoe Perches WARN("MISPLACED_INIT", 61768716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 61778716de38SJoe Perches $fix) { 6178194f66fcSJoe 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; 61798716de38SJoe Perches } 61808716de38SJoe Perches } 61818716de38SJoe Perches } 61828716de38SJoe Perches 6183e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 6184e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 6185e970b884SJoe Perches my $attr = $1; 6186e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 6187e970b884SJoe Perches my $attr_prefix = $1; 6188e970b884SJoe Perches my $attr_type = $2; 6189e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 6190e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 6191e970b884SJoe Perches $fix) { 6192194f66fcSJoe Perches $fixed[$fixlinenr] =~ 6193e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 6194e970b884SJoe Perches } 6195e970b884SJoe Perches } 6196e970b884SJoe Perches 6197e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 6198e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 6199e970b884SJoe Perches my $attr = $1; 6200e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 6201e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 6202e970b884SJoe Perches $fix) { 6203194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 6204e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 6205e970b884SJoe Perches $lead = rtrim($1); 6206e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 6207e970b884SJoe Perches $lead = "${lead}const "; 6208194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 6209e970b884SJoe Perches } 6210e970b884SJoe Perches } 6211e970b884SJoe Perches 6212c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 6213c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 6214c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 6215c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 6216c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 6217c17893c7SJoe Perches $fix) { 6218c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 6219c17893c7SJoe Perches } 6220c17893c7SJoe Perches } 6221c17893c7SJoe Perches 6222fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 6223fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 6224fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 6225fbdb8138SJoe Perches my $constant_func = $1; 6226fbdb8138SJoe Perches my $func = $constant_func; 6227fbdb8138SJoe Perches $func =~ s/^__constant_//; 6228fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 6229fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 6230fbdb8138SJoe Perches $fix) { 6231194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 6232fbdb8138SJoe Perches } 6233fbdb8138SJoe Perches } 6234fbdb8138SJoe Perches 62351a15a250SPatrick Pannuto# prefer usleep_range over udelay 623637581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 623743c1d77cSJoe Perches my $delay = $1; 62381a15a250SPatrick Pannuto # ignore udelay's < 10, however 623943c1d77cSJoe Perches if (! ($delay < 10) ) { 6240000d1cc1SJoe Perches CHK("USLEEP_RANGE", 6241458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 624243c1d77cSJoe Perches } 624343c1d77cSJoe Perches if ($delay > 2000) { 624443c1d77cSJoe Perches WARN("LONG_UDELAY", 624543c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 62461a15a250SPatrick Pannuto } 62471a15a250SPatrick Pannuto } 62481a15a250SPatrick Pannuto 624909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 625009ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 625109ef8725SPatrick Pannuto if ($1 < 20) { 6252000d1cc1SJoe Perches WARN("MSLEEP", 6253458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 625409ef8725SPatrick Pannuto } 625509ef8725SPatrick Pannuto } 625609ef8725SPatrick Pannuto 625736ec1939SJoe Perches# check for comparisons of jiffies 625836ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 625936ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 626036ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 626136ec1939SJoe Perches } 626236ec1939SJoe Perches 62639d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 62649d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 62659d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 62669d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 62679d7a34a5SJoe Perches } 62689d7a34a5SJoe Perches 626900df344fSAndy Whitcroft# warn about #ifdefs in C files 6270c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 627100df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 627200df344fSAndy Whitcroft# print "$herecurr"; 627300df344fSAndy Whitcroft# $clean = 0; 627400df344fSAndy Whitcroft# } 627500df344fSAndy Whitcroft 627622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 6277c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 62783705ce5bSJoe Perches if (ERROR("SPACING", 62793705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 62803705ce5bSJoe Perches $fix) { 6281194f66fcSJoe Perches $fixed[$fixlinenr] =~ 62823705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 62833705ce5bSJoe Perches } 62843705ce5bSJoe Perches 628522f2a2efSAndy Whitcroft } 628622f2a2efSAndy Whitcroft 62874a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 6288171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 6289171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 62904a0df2efSAndy Whitcroft my $which = $1; 62914a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6292000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 6293000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 62944a0df2efSAndy Whitcroft } 62954a0df2efSAndy Whitcroft } 62964a0df2efSAndy Whitcroft# check for memory barriers without a comment. 6297402c2553SMichael S. Tsirkin 6298402c2553SMichael S. Tsirkin my $barriers = qr{ 6299402c2553SMichael S. Tsirkin mb| 6300402c2553SMichael S. Tsirkin rmb| 6301ad83ec6cSWill Deacon wmb 6302402c2553SMichael S. Tsirkin }x; 6303402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 6304402c2553SMichael S. Tsirkin mb__before_atomic| 6305402c2553SMichael S. Tsirkin mb__after_atomic| 6306402c2553SMichael S. Tsirkin store_release| 6307402c2553SMichael S. Tsirkin load_acquire| 6308402c2553SMichael S. Tsirkin store_mb| 6309402c2553SMichael S. Tsirkin (?:$barriers) 6310402c2553SMichael S. Tsirkin }x; 6311402c2553SMichael S. Tsirkin my $all_barriers = qr{ 6312402c2553SMichael S. Tsirkin (?:$barriers)| 631343e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 631443e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 6315402c2553SMichael S. Tsirkin }x; 6316402c2553SMichael S. Tsirkin 6317402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 63184a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6319c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 6320000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 63214a0df2efSAndy Whitcroft } 63224a0df2efSAndy Whitcroft } 63233ad81779SPaul E. McKenney 6324f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 6325f4073b0fSMichael S. Tsirkin 6326f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 6327f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 6328f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 6329f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 6330f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 6331f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 6332f4073b0fSMichael S. Tsirkin } 6333f4073b0fSMichael S. Tsirkin 6334cb426e99SJoe Perches# check for waitqueue_active without a comment. 6335cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 6336cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 6337cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 6338cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 6339cb426e99SJoe Perches } 6340cb426e99SJoe Perches } 63413ad81779SPaul E. McKenney 63425099a722SMarco Elver# check for data_race without a comment. 63435099a722SMarco Elver if ($line =~ /\bdata_race\s*\(/) { 63445099a722SMarco Elver if (!ctx_has_comment($first_line, $linenr)) { 63455099a722SMarco Elver WARN("DATA_RACE", 63465099a722SMarco Elver "data_race without comment\n" . $herecurr); 63475099a722SMarco Elver } 63485099a722SMarco Elver } 63495099a722SMarco Elver 63504a0df2efSAndy Whitcroft# check of hardware specific defines 6351c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 6352000d1cc1SJoe Perches CHK("ARCH_DEFINES", 6353000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 63540a920b5bSAndy Whitcroft } 6355653d4876SAndy Whitcroft 6356596ed45bSJoe Perches# check that the storage class is not after a type 6357596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 6358000d1cc1SJoe Perches WARN("STORAGE_CLASS", 6359596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 6360596ed45bSJoe Perches } 6361596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 6362596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 6363596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 6364596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 6365596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 6366596ed45bSJoe Perches WARN("STORAGE_CLASS", 6367596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 6368d4977c78STobias Klauser } 6369d4977c78STobias Klauser 6370de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 6371de7d4f0eSAndy Whitcroft# storage class and type. 63729c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 63739c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 6374000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 6375000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 6376de7d4f0eSAndy Whitcroft } 6377de7d4f0eSAndy Whitcroft 63788905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 63792b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 63802b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 6381d5e616fcSJoe Perches if (WARN("INLINE", 6382d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 6383d5e616fcSJoe Perches $fix) { 6384194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 6385d5e616fcSJoe Perches 6386d5e616fcSJoe Perches } 63878905a67cSAndy Whitcroft } 63888905a67cSAndy Whitcroft 63897ebe1d17SDwaipayan Ray# Check for compiler attributes 63902b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 63917ebe1d17SDwaipayan Ray $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) { 63927ebe1d17SDwaipayan Ray my $attr = $1; 63937ebe1d17SDwaipayan Ray $attr =~ s/\s*\(\s*(.*)\)\s*/$1/; 63947ebe1d17SDwaipayan Ray 63957ebe1d17SDwaipayan Ray my %attr_list = ( 63960830aab0SJoe Perches "alias" => "__alias", 63977ebe1d17SDwaipayan Ray "aligned" => "__aligned", 63987ebe1d17SDwaipayan Ray "always_inline" => "__always_inline", 63997ebe1d17SDwaipayan Ray "assume_aligned" => "__assume_aligned", 64007ebe1d17SDwaipayan Ray "cold" => "__cold", 64017ebe1d17SDwaipayan Ray "const" => "__attribute_const__", 64027ebe1d17SDwaipayan Ray "copy" => "__copy", 64037ebe1d17SDwaipayan Ray "designated_init" => "__designated_init", 64047ebe1d17SDwaipayan Ray "externally_visible" => "__visible", 64057ebe1d17SDwaipayan Ray "format" => "printf|scanf", 64067ebe1d17SDwaipayan Ray "gnu_inline" => "__gnu_inline", 64077ebe1d17SDwaipayan Ray "malloc" => "__malloc", 64087ebe1d17SDwaipayan Ray "mode" => "__mode", 64097ebe1d17SDwaipayan Ray "no_caller_saved_registers" => "__no_caller_saved_registers", 64107ebe1d17SDwaipayan Ray "noclone" => "__noclone", 64117ebe1d17SDwaipayan Ray "noinline" => "noinline", 64127ebe1d17SDwaipayan Ray "nonstring" => "__nonstring", 64137ebe1d17SDwaipayan Ray "noreturn" => "__noreturn", 64147ebe1d17SDwaipayan Ray "packed" => "__packed", 64157ebe1d17SDwaipayan Ray "pure" => "__pure", 6416339f29d9SJoe Perches "section" => "__section", 64170830aab0SJoe Perches "used" => "__used", 64180830aab0SJoe Perches "weak" => "__weak" 64197ebe1d17SDwaipayan Ray ); 64207ebe1d17SDwaipayan Ray 64217ebe1d17SDwaipayan Ray while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) { 6422339f29d9SJoe Perches my $orig_attr = $1; 64237ebe1d17SDwaipayan Ray my $params = ''; 64247ebe1d17SDwaipayan Ray $params = $2 if defined($2); 6425339f29d9SJoe Perches my $curr_attr = $orig_attr; 64267ebe1d17SDwaipayan Ray $curr_attr =~ s/^[\s_]+|[\s_]+$//g; 64277ebe1d17SDwaipayan Ray if (exists($attr_list{$curr_attr})) { 6428339f29d9SJoe Perches my $new = $attr_list{$curr_attr}; 64297ebe1d17SDwaipayan Ray if ($curr_attr eq "format" && $params) { 64307ebe1d17SDwaipayan Ray $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/; 6431339f29d9SJoe Perches $new = "__$1\($2"; 64327ebe1d17SDwaipayan Ray } else { 6433339f29d9SJoe Perches $new = "$new$params"; 64347ebe1d17SDwaipayan Ray } 64357ebe1d17SDwaipayan Ray if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 6436339f29d9SJoe Perches "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) && 64377ebe1d17SDwaipayan Ray $fix) { 6438339f29d9SJoe Perches my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?'; 6439339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/$remove//; 6440339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/; 6441339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/; 6442339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//; 64437ebe1d17SDwaipayan Ray } 644439b7e287SJoe Perches } 6445462811d9SJoe Perches } 6446462811d9SJoe Perches 64477ebe1d17SDwaipayan Ray # Check for __attribute__ unused, prefer __always_unused or __maybe_unused 64487ebe1d17SDwaipayan Ray if ($attr =~ /^_*unused/) { 64497ebe1d17SDwaipayan Ray WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 64507ebe1d17SDwaipayan Ray "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr); 6451d5e616fcSJoe Perches } 64526061d949SJoe Perches } 64536061d949SJoe Perches 6454619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 64555b57980dSJoe Perches if ($perl_version_ok && 6456619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 6457619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6458619a908aSJoe Perches $line =~ /\b__weak\b/)) { 6459619a908aSJoe Perches ERROR("WEAK_DECLARATION", 6460619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 6461619a908aSJoe Perches } 6462619a908aSJoe Perches 6463fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 6464e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6465fd39f904STomas Winkler $realfile !~ m@\btools/@ && 6466e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6467e6176fa4SJoe Perches my $type = $1; 6468e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 6469e6176fa4SJoe Perches $type = $1; 6470e6176fa4SJoe Perches my $kernel_type = 'u'; 6471e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 6472e6176fa4SJoe Perches $type =~ /(\d+)/; 6473e6176fa4SJoe Perches $kernel_type .= $1; 6474e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 6475e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6476e6176fa4SJoe Perches $fix) { 6477e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6478e6176fa4SJoe Perches } 6479e6176fa4SJoe Perches } 6480e6176fa4SJoe Perches } 6481e6176fa4SJoe Perches 6482938224b5SJoe Perches# check for cast of C90 native int or longer types constants 6483938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6484938224b5SJoe Perches my $cast = $1; 6485938224b5SJoe Perches my $const = $2; 6486938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 6487938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 6488938224b5SJoe Perches $fix) { 6489938224b5SJoe Perches my $suffix = ""; 6490938224b5SJoe Perches my $newconst = $const; 6491938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 6492938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6493938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6494938224b5SJoe Perches $suffix .= 'LL'; 6495938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6496938224b5SJoe Perches $suffix .= 'L'; 6497938224b5SJoe Perches } 6498938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6499938224b5SJoe Perches } 6500938224b5SJoe Perches } 6501938224b5SJoe Perches 65028f53a9b8SJoe Perches# check for sizeof(&) 65038f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6504000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6505000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 65068f53a9b8SJoe Perches } 65078f53a9b8SJoe Perches 650866c80b60SJoe Perches# check for sizeof without parenthesis 650966c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6510d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6511d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6512d5e616fcSJoe Perches $fix) { 6513194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6514d5e616fcSJoe Perches } 651566c80b60SJoe Perches } 651666c80b60SJoe Perches 651788982feaSJoe Perches# check for struct spinlock declarations 651888982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 651988982feaSJoe Perches WARN("USE_SPINLOCK_T", 652088982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 652188982feaSJoe Perches } 652288982feaSJoe Perches 6523a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 652406668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6525a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6526caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6527caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6528d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6529d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6530d5e616fcSJoe Perches $fix) { 6531194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6532d5e616fcSJoe Perches } 6533a6962d72SJoe Perches } 6534a6962d72SJoe Perches } 6535a6962d72SJoe Perches 65360b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 65375b57980dSJoe Perches if ($perl_version_ok && 65380b523769SJoe Perches defined $stat && 65390b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 65400b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6541e3c6bc95STobin C. Harding my $stat_real; 6542e3c6bc95STobin C. Harding 65430b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 65440b523769SJoe Perches $lc = $lc + $linenr; 65450b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6546ffe07513SJoe Perches my $specifier; 6547ffe07513SJoe Perches my $extension; 65483bd32d6aSSakari Ailus my $qualifier; 6549ffe07513SJoe Perches my $bad_specifier = ""; 65500b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 65510b523769SJoe Perches $fmt =~ s/%%//g; 6552e3c6bc95STobin C. Harding 65533bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6554e3c6bc95STobin C. Harding $specifier = $1; 6555e3c6bc95STobin C. Harding $extension = $2; 65563bd32d6aSSakari Ailus $qualifier = $3; 6557361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 65583bd32d6aSSakari Ailus ($extension eq "f" && 65593bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6560e3c6bc95STobin C. Harding $bad_specifier = $specifier; 65610b523769SJoe Perches last; 65620b523769SJoe Perches } 6563e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6564e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6565e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 65660b523769SJoe Perches } 6567e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6568e3c6bc95STobin 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"); 6569e3c6bc95STobin C. Harding } 6570e3c6bc95STobin C. Harding } 6571e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 65722a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 65731df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 65741df7338aSSergey Senozhatsky my $use = ""; 6575e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 65761df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6577e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 65781df7338aSSergey Senozhatsky } 65792a9f9d85STobin C. Harding 65800b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6581e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6582e3c6bc95STobin C. Harding } 65830b523769SJoe Perches } 65840b523769SJoe Perches } 65850b523769SJoe Perches 6586554e165cSAndy Whitcroft# Check for misused memsets 65875b57980dSJoe Perches if ($perl_version_ok && 6588d1fe9c09SJoe Perches defined $stat && 65899e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6590554e165cSAndy Whitcroft 6591d7c76ba7SJoe Perches my $ms_addr = $2; 6592d1fe9c09SJoe Perches my $ms_val = $7; 6593d1fe9c09SJoe Perches my $ms_size = $12; 6594d7c76ba7SJoe Perches 6595554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6596554e165cSAndy Whitcroft ERROR("MEMSET", 6597d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6598554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6599554e165cSAndy Whitcroft WARN("MEMSET", 6600d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6601d7c76ba7SJoe Perches } 6602d7c76ba7SJoe Perches } 6603d7c76ba7SJoe Perches 660498a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 66055b57980dSJoe Perches# if ($perl_version_ok && 6606f333195dSJoe Perches# defined $stat && 6607f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6608f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6609f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6610f333195dSJoe Perches# $fix) { 6611f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6612f333195dSJoe Perches# } 6613f333195dSJoe Perches# } 661498a9bba5SJoe Perches 6615b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 66165b57980dSJoe Perches# if ($perl_version_ok && 6617f333195dSJoe Perches# defined $stat && 6618f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6619f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6620f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6621f333195dSJoe Perches# } 6622b6117d17SMateusz Kulikowski 66238617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 66248617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 66255b57980dSJoe Perches# if ($perl_version_ok && 6626f333195dSJoe Perches# defined $stat && 6627f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6628f333195dSJoe Perches# 6629f333195dSJoe Perches# my $ms_val = $7; 6630f333195dSJoe Perches# 6631f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6632f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6633f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6634f333195dSJoe Perches# $fix) { 6635f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6636f333195dSJoe Perches# } 6637f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6638f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6639f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6640f333195dSJoe Perches# $fix) { 6641f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6642f333195dSJoe Perches# } 6643f333195dSJoe Perches# } 6644f333195dSJoe Perches# } 66458617cd09SMateusz Kulikowski 66465dbdb2d8SJoe Perches# strlcpy uses that should likely be strscpy 66475dbdb2d8SJoe Perches if ($line =~ /\bstrlcpy\s*\(/) { 66485dbdb2d8SJoe Perches WARN("STRLCPY", 66495dbdb2d8SJoe Perches "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr); 66505dbdb2d8SJoe Perches } 66515dbdb2d8SJoe Perches 6652d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 66535b57980dSJoe Perches if ($perl_version_ok && 6654d1fe9c09SJoe Perches defined $stat && 6655d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6656d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6657d7c76ba7SJoe Perches my $call = $1; 6658d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6659d7c76ba7SJoe Perches my $arg1 = $3; 6660d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6661d1fe9c09SJoe Perches my $arg2 = $8; 6662d7c76ba7SJoe Perches my $cast; 6663d7c76ba7SJoe Perches 6664d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6665d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6666d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6667d7c76ba7SJoe Perches $cast = $cast1; 6668d7c76ba7SJoe Perches } else { 6669d7c76ba7SJoe Perches $cast = $cast2; 6670d7c76ba7SJoe Perches } 6671d7c76ba7SJoe Perches WARN("MINMAX", 6672d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6673554e165cSAndy Whitcroft } 6674554e165cSAndy Whitcroft } 6675554e165cSAndy Whitcroft 66764a273195SJoe Perches# check usleep_range arguments 66775b57980dSJoe Perches if ($perl_version_ok && 66784a273195SJoe Perches defined $stat && 66794a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 66804a273195SJoe Perches my $min = $1; 66814a273195SJoe Perches my $max = $7; 66824a273195SJoe Perches if ($min eq $max) { 66834a273195SJoe Perches WARN("USLEEP_RANGE", 6684458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 66854a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 66864a273195SJoe Perches $min > $max) { 66874a273195SJoe Perches WARN("USLEEP_RANGE", 6688458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 66894a273195SJoe Perches } 66904a273195SJoe Perches } 66914a273195SJoe Perches 6692823b794cSJoe Perches# check for naked sscanf 66935b57980dSJoe Perches if ($perl_version_ok && 6694823b794cSJoe Perches defined $stat && 66956c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6696823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6697823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6698823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6699823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6700823b794cSJoe Perches $lc = $lc + $linenr; 67012a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6702823b794cSJoe Perches WARN("NAKED_SSCANF", 6703823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6704823b794cSJoe Perches } 6705823b794cSJoe Perches 6706afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 67075b57980dSJoe Perches if ($perl_version_ok && 6708afc819abSJoe Perches defined $stat && 6709afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6710afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6711afc819abSJoe Perches $lc = $lc + $linenr; 67122a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6713afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6714afc819abSJoe Perches my $format = $6; 6715afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6716afc819abSJoe Perches if ($count == 1 && 6717afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6718afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6719afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6720afc819abSJoe Perches } 6721afc819abSJoe Perches } 6722afc819abSJoe Perches } 6723afc819abSJoe Perches 672470dc8a48SJoe Perches# check for new externs in .h files. 672570dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 672670dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6727d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 672870dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 672970dc8a48SJoe Perches $fix) { 6730194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 673170dc8a48SJoe Perches } 673270dc8a48SJoe Perches } 673370dc8a48SJoe Perches 6734de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6735171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6736c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6737171ae1a4SAndy Whitcroft { 6738c45dcabdSAndy Whitcroft my $function_name = $1; 6739c45dcabdSAndy Whitcroft my $paren_space = $2; 6740171ae1a4SAndy Whitcroft 6741171ae1a4SAndy Whitcroft my $s = $stat; 6742171ae1a4SAndy Whitcroft if (defined $cond) { 6743171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6744171ae1a4SAndy Whitcroft } 6745d8b44b58SKees Cook if ($s =~ /^\s*;/) 6746c45dcabdSAndy Whitcroft { 6747000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6748000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6749de7d4f0eSAndy Whitcroft } 6750de7d4f0eSAndy Whitcroft 6751171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6752000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6753000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6754171ae1a4SAndy Whitcroft } 67559c9ba34eSAndy Whitcroft 67569c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 67579c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 67589c9ba34eSAndy Whitcroft { 6759000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6760000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6761171ae1a4SAndy Whitcroft } 6762171ae1a4SAndy Whitcroft 6763a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6764a0ad7596SJoe Perches if (defined $stat && 6765d8b44b58SKees Cook $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6766d8b44b58SKees Cook $1 ne "void") { 6767d8b44b58SKees Cook my $args = trim($1); 6768ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6769ca0d8929SJoe Perches my $arg = trim($1); 6770d8b44b58SKees Cook if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6771ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6772ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6773ca0d8929SJoe Perches } 6774ca0d8929SJoe Perches } 6775ca0d8929SJoe Perches } 6776ca0d8929SJoe Perches 6777a0ad7596SJoe Perches# check for function definitions 67785b57980dSJoe Perches if ($perl_version_ok && 6779a0ad7596SJoe Perches defined $stat && 6780a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6781a0ad7596SJoe Perches $context_function = $1; 6782a0ad7596SJoe Perches 6783a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6784a0ad7596SJoe Perches my $ok = 0; 6785a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6786a0ad7596SJoe Perches my $herectx = $here . "\n"; 6787a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6788a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6789a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6790a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6791a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6792a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6793a0ad7596SJoe Perches } 6794a0ad7596SJoe Perches if (!$ok) { 6795a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6796a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6797a0ad7596SJoe Perches } 6798a0ad7596SJoe Perches } 6799a0ad7596SJoe Perches 6800de7d4f0eSAndy Whitcroft# checks for new __setup's 6801de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6802de7d4f0eSAndy Whitcroft my $name = $1; 6803de7d4f0eSAndy Whitcroft 6804de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6805000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 68062581ac7cSTim Froidcoeur "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); 6807de7d4f0eSAndy Whitcroft } 6808653d4876SAndy Whitcroft } 68099c0ca6f9SAndy Whitcroft 6810e29a70f1SJoe Perches# check for pointless casting of alloc functions 6811e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6812000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6813000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 68149c0ca6f9SAndy Whitcroft } 681513214adfSAndy Whitcroft 6816a640d25cSJoe Perches# alloc style 6817a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 68185b57980dSJoe Perches if ($perl_version_ok && 6819e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6820a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6821a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6822a640d25cSJoe Perches } 6823a640d25cSJoe Perches 682460a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 68255b57980dSJoe Perches if ($perl_version_ok && 68261b4a2ed4SJoe Perches defined $stat && 68271b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 682860a55369SJoe Perches my $oldfunc = $3; 682960a55369SJoe Perches my $a1 = $4; 683060a55369SJoe Perches my $a2 = $10; 683160a55369SJoe Perches my $newfunc = "kmalloc_array"; 683260a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 683360a55369SJoe Perches my $r1 = $a1; 683460a55369SJoe Perches my $r2 = $a2; 683560a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 683660a55369SJoe Perches $r1 = $a2; 683760a55369SJoe Perches $r2 = $a1; 683860a55369SJoe Perches } 6839e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6840e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 68411b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6842e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6843e3d95a2aSTobin C. Harding 6844e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 68451b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 68461b4a2ed4SJoe Perches $cnt == 1 && 6847e367455aSJoe Perches $fix) { 6848194f66fcSJoe 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; 684960a55369SJoe Perches } 685060a55369SJoe Perches } 685160a55369SJoe Perches } 685260a55369SJoe Perches 6853972fdea2SJoe Perches# check for krealloc arg reuse 68545b57980dSJoe Perches if ($perl_version_ok && 68554cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 68564cab63ceSJoe Perches $1 eq $3) { 6857972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6858972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6859972fdea2SJoe Perches } 6860972fdea2SJoe Perches 68615ce59ae0SJoe Perches# check for alloc argument mismatch 68625ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 68635ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 68645ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 68655ce59ae0SJoe Perches } 68665ce59ae0SJoe Perches 6867caf2a54fSJoe Perches# check for multiple semicolons 6868caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6869d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6870d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6871d5e616fcSJoe Perches $fix) { 6872194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6873d5e616fcSJoe Perches } 6874d1e2ad07SJoe Perches } 6875d1e2ad07SJoe Perches 6876cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6877cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6878cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 68790ab90191SJoe Perches my $ull = ""; 68800ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 68810ab90191SJoe Perches if (CHK("BIT_MACRO", 68820ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 68830ab90191SJoe Perches $fix) { 68840ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 68850ab90191SJoe Perches } 68860ab90191SJoe Perches } 68870ab90191SJoe Perches 688850161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) 68893e89ad85SJerome Forissier if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { 689050161266SJoe Perches WARN("IS_ENABLED_CONFIG", 68913e89ad85SJerome Forissier "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); 689250161266SJoe Perches } 689350161266SJoe Perches 68942d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 68953e89ad85SJerome 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*$/) { 68962d632745SJoe Perches my $config = $1; 68972d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 68983e89ad85SJerome Forissier "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && 68992d632745SJoe Perches $fix) { 69002d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 69012d632745SJoe Perches } 69022d632745SJoe Perches } 69032d632745SJoe Perches 6904f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 6905f36d3eb8SJoe Perches my @fallthroughs = ( 6906f36d3eb8SJoe Perches 'fallthrough', 6907f36d3eb8SJoe Perches '@fallthrough@', 6908f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 6909f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 6910f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 6911f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6912f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6913f36d3eb8SJoe Perches ); 6914f36d3eb8SJoe Perches if ($raw_comment ne '') { 6915f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 6916f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 6917f36d3eb8SJoe Perches my $msg_level = \&WARN; 6918f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 6919f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 6920f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 6921f36d3eb8SJoe Perches last; 6922f36d3eb8SJoe Perches } 6923f36d3eb8SJoe Perches } 6924f36d3eb8SJoe Perches } 6925f36d3eb8SJoe Perches 6926d1e2ad07SJoe Perches# check for switch/default statements without a break; 69275b57980dSJoe Perches if ($perl_version_ok && 6928d1e2ad07SJoe Perches defined $stat && 6929d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6930d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6931e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6932e3d95a2aSTobin C. Harding 6933d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6934d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6935caf2a54fSJoe Perches } 6936caf2a54fSJoe Perches 693713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6938d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6939d5e616fcSJoe Perches if (WARN("USE_FUNC", 6940d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6941d5e616fcSJoe Perches $fix) { 6942194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6943d5e616fcSJoe Perches } 694413214adfSAndy Whitcroft } 6945773647a0SAndy Whitcroft 694662ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 694762ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 694862ec818fSJoe Perches ERROR("DATE_TIME", 694962ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 695062ec818fSJoe Perches } 695162ec818fSJoe Perches 69522c92488aSJoe Perches# check for use of yield() 69532c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 69542c92488aSJoe Perches WARN("YIELD", 69552c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 69562c92488aSJoe Perches } 69572c92488aSJoe Perches 6958179f8f40SJoe Perches# check for comparisons against true and false 6959179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6960179f8f40SJoe Perches my $lead = $1; 6961179f8f40SJoe Perches my $arg = $2; 6962179f8f40SJoe Perches my $test = $3; 6963179f8f40SJoe Perches my $otype = $4; 6964179f8f40SJoe Perches my $trail = $5; 6965179f8f40SJoe Perches my $op = "!"; 6966179f8f40SJoe Perches 6967179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6968179f8f40SJoe Perches 6969179f8f40SJoe Perches my $type = lc($otype); 6970179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6971179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6972179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6973179f8f40SJoe Perches $op = ""; 6974179f8f40SJoe Perches } 6975179f8f40SJoe Perches 6976179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6977179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6978179f8f40SJoe Perches 6979179f8f40SJoe Perches## maybe suggesting a correct construct would better 6980179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6981179f8f40SJoe Perches 6982179f8f40SJoe Perches } 6983179f8f40SJoe Perches } 6984179f8f40SJoe Perches 69854882720bSThomas Gleixner# check for semaphores initialized locked 69864882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6987000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6988000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6989773647a0SAndy Whitcroft } 69906712d858SJoe Perches 699167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 699267d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6993000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 699467d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6995773647a0SAndy Whitcroft } 69966712d858SJoe Perches 6997ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6998f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6999000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 7000ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 7001f3db6639SMichael Ellerman } 70026712d858SJoe Perches 70033d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 70043d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 70053d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 70063d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 70073d709ab5SPaul E. McKenney } 70083d709ab5SPaul E. McKenney 70099189c7e7SJoe Perches# check for deprecated apis 70109189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 70119189c7e7SJoe Perches my $deprecated_api = $1; 70129189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 70139189c7e7SJoe Perches WARN("DEPRECATED_API", 70149189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 70159189c7e7SJoe Perches } 70169189c7e7SJoe Perches 70170f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 7018d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 7019ced69da1SQuentin Monnet if (defined($const_structs) && 7020ced69da1SQuentin Monnet $line !~ /\bconst\b/ && 7021d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 7022000d1cc1SJoe Perches WARN("CONST_STRUCT", 7023d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 70242b6db5cbSAndy Whitcroft } 7025773647a0SAndy Whitcroft 7026773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 7027773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 7028*35cdcbfcSPeng Wang# ignore designated initializers using NR_CPUS 7029773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 7030c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 7031c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 7032171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 7033171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 7034*35cdcbfcSPeng Wang $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ && 7035*35cdcbfcSPeng Wang $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/) 7036773647a0SAndy Whitcroft { 7037000d1cc1SJoe Perches WARN("NR_CPUS", 7038000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 7039773647a0SAndy Whitcroft } 70409c9ba34eSAndy Whitcroft 704152ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 704252ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 704352ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 704452ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 704552ea8506SJoe Perches } 704652ea8506SJoe Perches 7047acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 70485b57980dSJoe Perches if ($perl_version_ok && 7049acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 7050acd9362cSJoe Perches WARN("LIKELY_MISUSE", 7051acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 7052acd9362cSJoe Perches } 7053acd9362cSJoe Perches 7054de3f186fSDenis Efremov# nested likely/unlikely calls 7055de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 7056de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 7057de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 7058de3f186fSDenis Efremov } 7059de3f186fSDenis Efremov 7060691d77b6SAndy Whitcroft# whine mightly about in_atomic 7061691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 7062691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 7063000d1cc1SJoe Perches ERROR("IN_ATOMIC", 7064000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 7065f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 7066000d1cc1SJoe Perches WARN("IN_ATOMIC", 7067000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 7068691d77b6SAndy Whitcroft } 7069691d77b6SAndy Whitcroft } 70701704f47bSPeter Zijlstra 70711704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 70721704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 70731704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 70741704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 70751704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 70761704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 7077000d1cc1SJoe Perches ERROR("LOCKDEP", 7078000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 70791704f47bSPeter Zijlstra } 70801704f47bSPeter Zijlstra } 708188f8831cSDave Jones 7082b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 7083b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 7084000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 7085000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 708688f8831cSDave Jones } 70872435880fSJoe Perches 708800180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 708900180468SJoe Perches# and whether or not function naming is typical and if 709000180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 70915b57980dSJoe Perches if ($perl_version_ok && 709200180468SJoe Perches defined $stat && 709300180468SJoe 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*\)/) { 709400180468SJoe Perches my $var = $1; 709500180468SJoe Perches my $perms = $2; 709600180468SJoe Perches my $show = $3; 709700180468SJoe Perches my $store = $4; 709800180468SJoe Perches my $octal_perms = perms_to_octal($perms); 709900180468SJoe Perches if ($show =~ /^${var}_show$/ && 710000180468SJoe Perches $store =~ /^${var}_store$/ && 710100180468SJoe Perches $octal_perms eq "0644") { 710200180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 710300180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 710400180468SJoe Perches $fix) { 710500180468SJoe 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})/; 710600180468SJoe Perches } 710700180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 710800180468SJoe Perches $store =~ /^NULL$/ && 710900180468SJoe Perches $octal_perms eq "0444") { 711000180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 711100180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 711200180468SJoe Perches $fix) { 711300180468SJoe 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})/; 711400180468SJoe Perches } 711500180468SJoe Perches } elsif ($show =~ /^NULL$/ && 711600180468SJoe Perches $store =~ /^${var}_store$/ && 711700180468SJoe Perches $octal_perms eq "0200") { 711800180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 711900180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 712000180468SJoe Perches $fix) { 712100180468SJoe 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})/; 712200180468SJoe Perches } 712300180468SJoe Perches } elsif ($octal_perms eq "0644" || 712400180468SJoe Perches $octal_perms eq "0444" || 712500180468SJoe Perches $octal_perms eq "0200") { 712600180468SJoe Perches my $newshow = "$show"; 712700180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 712800180468SJoe Perches my $newstore = $store; 712900180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 713000180468SJoe Perches my $rename = ""; 713100180468SJoe Perches if ($show ne $newshow) { 713200180468SJoe Perches $rename .= " '$show' to '$newshow'"; 713300180468SJoe Perches } 713400180468SJoe Perches if ($store ne $newstore) { 713500180468SJoe Perches $rename .= " '$store' to '$newstore'"; 713600180468SJoe Perches } 713700180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 713800180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 713900180468SJoe Perches } else { 714000180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 714100180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 714200180468SJoe Perches } 714300180468SJoe Perches } 714400180468SJoe Perches 7145515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 7146515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 714773121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 714873121534SJoe Perches# specific definition of not visible in sysfs. 714973121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 715073121534SJoe Perches# use the default permissions 71515b57980dSJoe Perches if ($perl_version_ok && 7152459cf0aeSJoe Perches defined $stat && 7153515a235eSJoe Perches $line =~ /$mode_perms_search/) { 71542435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 71552435880fSJoe Perches my $func = $entry->[0]; 71562435880fSJoe Perches my $arg_pos = $entry->[1]; 71572435880fSJoe Perches 7158459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 7159459cf0aeSJoe Perches $lc = $lc + $linenr; 71602a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 7161459cf0aeSJoe Perches 71622435880fSJoe Perches my $skip_args = ""; 71632435880fSJoe Perches if ($arg_pos > 1) { 71642435880fSJoe Perches $arg_pos--; 71652435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 71662435880fSJoe Perches } 7167f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 7168459cf0aeSJoe Perches if ($stat =~ /$test/) { 71692435880fSJoe Perches my $val = $1; 71702435880fSJoe Perches $val = $6 if ($skip_args ne ""); 717173121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 717273121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 717373121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 71742435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 7175459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 7176f90774e1SJoe Perches } 7177f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 7178c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 7179459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 71802435880fSJoe Perches } 7181459cf0aeSJoe Perches } 7182459cf0aeSJoe Perches } 7183459cf0aeSJoe Perches } 7184459cf0aeSJoe Perches 7185459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 7186bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 718700180468SJoe Perches my $oval = $1; 718800180468SJoe Perches my $octal = perms_to_octal($oval); 7189f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 7190459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 7191f90774e1SJoe Perches $fix) { 719200180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 71932435880fSJoe Perches } 719413214adfSAndy Whitcroft } 71955a6d20ceSBjorn Andersson 71965a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 71975a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 71985a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 71995a6d20ceSBjorn Andersson my $valid_licenses = qr{ 72005a6d20ceSBjorn Andersson GPL| 72015a6d20ceSBjorn Andersson GPL\ v2| 72025a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 72035a6d20ceSBjorn Andersson Dual\ BSD/GPL| 72045a6d20ceSBjorn Andersson Dual\ MIT/GPL| 72055a6d20ceSBjorn Andersson Dual\ MPL/GPL| 72065a6d20ceSBjorn Andersson Proprietary 72075a6d20ceSBjorn Andersson }x; 72085a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 72095a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 72105a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 72115a6d20ceSBjorn Andersson } 72125a6d20ceSBjorn Andersson } 72136a8d76cbSMatteo Croce 72146a8d76cbSMatteo Croce# check for sysctl duplicate constants 72156a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 72166a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 72176a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 72186a8d76cbSMatteo Croce } 7219515a235eSJoe Perches } 722013214adfSAndy Whitcroft 722113214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 722213214adfSAndy Whitcroft # so just keep quiet. 722313214adfSAndy Whitcroft if ($#rawlines == -1) { 722413214adfSAndy Whitcroft exit(0); 72250a920b5bSAndy Whitcroft } 72260a920b5bSAndy Whitcroft 72278905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 72288905a67cSAndy Whitcroft # things that appear to be patches. 72298905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 72308905a67cSAndy Whitcroft exit(0); 72318905a67cSAndy Whitcroft } 72328905a67cSAndy Whitcroft 7233e73d2715SDwaipayan Ray # This is not a patch, and we are in 'no-patch' mode so 72348905a67cSAndy Whitcroft # just keep quiet. 72358905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 72368905a67cSAndy Whitcroft exit(0); 72378905a67cSAndy Whitcroft } 72388905a67cSAndy Whitcroft 7239a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 7240000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 7241000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 72420a920b5bSAndy Whitcroft } 7243cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 7244cd261496SGeert Uytterhoeven if ($signoff == 0) { 7245000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 7246000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 724748ca2d8aSDwaipayan Ray } elsif ($authorsignoff != 1) { 724848ca2d8aSDwaipayan Ray # authorsignoff values: 724948ca2d8aSDwaipayan Ray # 0 -> missing sign off 725048ca2d8aSDwaipayan Ray # 1 -> sign off identical 725148ca2d8aSDwaipayan Ray # 2 -> names and addresses match, comments mismatch 725248ca2d8aSDwaipayan Ray # 3 -> addresses match, names different 725348ca2d8aSDwaipayan Ray # 4 -> names match, addresses different 725448ca2d8aSDwaipayan Ray # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match 725548ca2d8aSDwaipayan Ray 725648ca2d8aSDwaipayan Ray my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; 725748ca2d8aSDwaipayan Ray 725848ca2d8aSDwaipayan Ray if ($authorsignoff == 0) { 725948ca2d8aSDwaipayan Ray ERROR("NO_AUTHOR_SIGN_OFF", 7260cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 726148ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 2) { 726248ca2d8aSDwaipayan Ray CHK("FROM_SIGN_OFF_MISMATCH", 726348ca2d8aSDwaipayan Ray "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); 726448ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 3) { 726548ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 726648ca2d8aSDwaipayan Ray "From:/Signed-off-by: email name mismatch: $sob_msg\n"); 726748ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 4) { 726848ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 726948ca2d8aSDwaipayan Ray "From:/Signed-off-by: email address mismatch: $sob_msg\n"); 727048ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 5) { 727148ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 727248ca2d8aSDwaipayan Ray "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); 727348ca2d8aSDwaipayan Ray } 7274cd261496SGeert Uytterhoeven } 72750a920b5bSAndy Whitcroft } 72760a920b5bSAndy Whitcroft 7277f0a594c1SAndy Whitcroft print report_dump(); 727813214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 727913214adfSAndy Whitcroft print "$filename " if ($summary_file); 72806c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 72816c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 72826c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 72836c72ffaaSAndy Whitcroft } 72848905a67cSAndy Whitcroft 7285d2c0a235SAndy Whitcroft if ($quiet == 0) { 7286ef212196SJoe Perches # If there were any defects found and not already fixing them 7287ef212196SJoe Perches if (!$clean and !$fix) { 7288ef212196SJoe Perches print << "EOM" 7289ef212196SJoe Perches 7290ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 7291ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 7292ef212196SJoe PerchesEOM 7293ef212196SJoe Perches } 7294d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 7295d2c0a235SAndy Whitcroft # then suggest that. 7296d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 7297b0781216SMike Frysinger $rpt_cleaners = 0; 7298d8469f16SJoe Perches print << "EOM" 7299d8469f16SJoe Perches 7300d8469f16SJoe PerchesNOTE: Whitespace errors detected. 7301d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 7302d8469f16SJoe PerchesEOM 7303d2c0a235SAndy Whitcroft } 7304d2c0a235SAndy Whitcroft } 7305d2c0a235SAndy Whitcroft 7306d752fcc8SJoe Perches if ($clean == 0 && $fix && 7307d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 7308d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 73099624b8d6SJoe Perches my $newfile = $filename; 73109624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 73113705ce5bSJoe Perches my $linecount = 0; 73123705ce5bSJoe Perches my $f; 73133705ce5bSJoe Perches 7314d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 7315d752fcc8SJoe Perches 73163705ce5bSJoe Perches open($f, '>', $newfile) 73173705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 73183705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 73193705ce5bSJoe Perches $linecount++; 73203705ce5bSJoe Perches if ($file) { 73213705ce5bSJoe Perches if ($linecount > 3) { 73223705ce5bSJoe Perches $fixed_line =~ s/^\+//; 73233705ce5bSJoe Perches print $f $fixed_line . "\n"; 73243705ce5bSJoe Perches } 73253705ce5bSJoe Perches } else { 73263705ce5bSJoe Perches print $f $fixed_line . "\n"; 73273705ce5bSJoe Perches } 73283705ce5bSJoe Perches } 73293705ce5bSJoe Perches close($f); 73303705ce5bSJoe Perches 73313705ce5bSJoe Perches if (!$quiet) { 73323705ce5bSJoe Perches print << "EOM"; 7333d8469f16SJoe Perches 73343705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 73353705ce5bSJoe Perches 73363705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 73373705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 73383705ce5bSJoe Perches 73393705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 73403705ce5bSJoe PerchesNo warranties, expressed or implied... 73413705ce5bSJoe PerchesEOM 73423705ce5bSJoe Perches } 73433705ce5bSJoe Perches } 73443705ce5bSJoe Perches 7345d8469f16SJoe Perches if ($quiet == 0) { 7346d8469f16SJoe Perches print "\n"; 7347d8469f16SJoe Perches if ($clean == 1) { 7348d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 7349d8469f16SJoe Perches } else { 7350d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 73510a920b5bSAndy Whitcroft } 73520a920b5bSAndy Whitcroft } 73530a920b5bSAndy Whitcroft return $clean; 73540a920b5bSAndy Whitcroft} 7355