1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl 2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0 3882ea1d6SJoe Perches# 4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit) 500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit) 62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) 7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]> 8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]> 90a920b5bSAndy Whitcroft 100a920b5bSAndy Whitcroftuse strict; 11cb77f0d6SKamil Rytarowskiuse warnings; 12c707a81dSJoe Perchesuse POSIX; 1336061e38SJoe Perchesuse File::Basename; 1436061e38SJoe Perchesuse Cwd 'abs_path'; 1557230297SJoe Perchesuse Term::ANSIColor qw(:constants); 16cd261496SGeert Uytterhoevenuse Encode qw(decode encode); 170a920b5bSAndy Whitcroft 180a920b5bSAndy Whitcroftmy $P = $0; 1936061e38SJoe Perchesmy $D = dirname(abs_path($P)); 200a920b5bSAndy Whitcroft 21000d1cc1SJoe Perchesmy $V = '0.32'; 220a920b5bSAndy Whitcroft 230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 240a920b5bSAndy Whitcroft 250a920b5bSAndy Whitcroftmy $quiet = 0; 260a920b5bSAndy Whitcroftmy $tree = 1; 270a920b5bSAndy Whitcroftmy $chk_signoff = 1; 280a920b5bSAndy Whitcroftmy $chk_patch = 1; 29773647a0SAndy Whitcroftmy $tst_only; 306c72ffaaSAndy Whitcroftmy $emacs = 0; 318905a67cSAndy Whitcroftmy $terse = 0; 3234d8815fSJoe Perchesmy $showfile = 0; 336c72ffaaSAndy Whitcroftmy $file = 0; 344a593c34SDu, Changbinmy $git = 0; 350dea9f1eSJoe Perchesmy %git_commits = (); 366c72ffaaSAndy Whitcroftmy $check = 0; 372ac73b4fSJoe Perchesmy $check_orig = 0; 388905a67cSAndy Whitcroftmy $summary = 1; 398905a67cSAndy Whitcroftmy $mailback = 0; 4013214adfSAndy Whitcroftmy $summary_file = 0; 41000d1cc1SJoe Perchesmy $show_types = 0; 423beb42ecSJoe Perchesmy $list_types = 0; 433705ce5bSJoe Perchesmy $fix = 0; 449624b8d6SJoe Perchesmy $fix_inplace = 0; 456c72ffaaSAndy Whitcroftmy $root; 460f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'}; 470f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot); 48c2fdda0dSAndy Whitcroftmy %debug; 493445686aSJoe Perchesmy %camelcase = (); 5091bfe484SJoe Perchesmy %use_type = (); 5191bfe484SJoe Perchesmy @use = (); 5291bfe484SJoe Perchesmy %ignore_type = (); 53000d1cc1SJoe Perchesmy @ignore = (); 5477f5b10aSHannes Edermy $help = 0; 55000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 56bdc48fa1SJoe Perchesmy $max_line_length = 100; 57d62a201fSDave Hansenmy $ignore_perl_version = 0; 58d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5956193274SVadim Bendeburymy $min_conf_desc_length = 4; 6066b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 61ebfd7d62SJoe Perchesmy $codespell = 0; 62f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 63bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch"; 64ced69da1SQuentin Monnetmy $typedefsfile; 65737c0767SJohn Brooksmy $color = "auto"; 6698005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE 67dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE 68dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git'; 69713a09deSAntonio Borneomy $tabsize = 8; 703e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_"; 7177f5b10aSHannes Eder 7277f5b10aSHannes Edersub help { 7377f5b10aSHannes Eder my ($exitcode) = @_; 7477f5b10aSHannes Eder 7577f5b10aSHannes Eder print << "EOM"; 7677f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 7777f5b10aSHannes EderVersion: $V 7877f5b10aSHannes Eder 7977f5b10aSHannes EderOptions: 8077f5b10aSHannes Eder -q, --quiet quiet 8177f5b10aSHannes Eder --no-tree run without a kernel tree 8277f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 8377f5b10aSHannes Eder --patch treat FILE as patchfile (default) 8477f5b10aSHannes Eder --emacs emacs compile window format 8577f5b10aSHannes Eder --terse one line per report 8634d8815fSJoe Perches --showfile emit diffed file position, not input file position 874a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 884a593c34SDu, Changbin single git commit with: 894a593c34SDu, Changbin <rev> 904a593c34SDu, Changbin <rev>^ 914a593c34SDu, Changbin <rev>~n 924a593c34SDu, Changbin multiple git commits with: 934a593c34SDu, Changbin <rev1>..<rev2> 944a593c34SDu, Changbin <rev1>...<rev2> 954a593c34SDu, Changbin <rev>-<count> 964a593c34SDu, Changbin git merges are ignored 9777f5b10aSHannes Eder -f, --file treat FILE as regular source file 9877f5b10aSHannes Eder --subjective, --strict enable more subjective tests 993beb42ecSJoe Perches --list-types list the possible message types 10091bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 101000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 1023beb42ecSJoe Perches --show-types show the specific message type in the output 103bdc48fa1SJoe Perches --max-line-length=n set the maximum line length, (default $max_line_length) 104bdc48fa1SJoe Perches if exceeded, warn on patches 105bdc48fa1SJoe Perches requires --strict for use with --file 10656193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 107bdc48fa1SJoe Perches --tab-size=n set the number of spaces for tab (default $tabsize) 10877f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10977f5b10aSHannes Eder --no-summary suppress the per-file summary 11077f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 11177f5b10aSHannes Eder --summary-file include the filename in summary 11277f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 11377f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 11477f5b10aSHannes Eder is all off) 11577f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 11677f5b10aSHannes Eder literally 1173705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1183705ce5bSJoe Perches If correctable single-line errors exist, create 1193705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1203705ce5bSJoe Perches with potential errors corrected to the preferred 1213705ce5bSJoe Perches checkpatch style 1229624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1239624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1249624b8d6SJoe Perches file. It's your fault if there's no backup or git 125d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 126d62a201fSDave Hansen runtime errors. 127ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 128f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 129ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 13075ad8c57SJerome Forissier --typedefsfile Read additional types from this file 131737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 132737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 1333e89ad85SJerome Forissier --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default 1343e89ad85SJerome Forissier ${CONFIG_}) 13577f5b10aSHannes Eder -h, --help, --version display this help and exit 13677f5b10aSHannes Eder 13777f5b10aSHannes EderWhen FILE is - read standard input. 13877f5b10aSHannes EderEOM 13977f5b10aSHannes Eder 14077f5b10aSHannes Eder exit($exitcode); 14177f5b10aSHannes Eder} 14277f5b10aSHannes Eder 1433beb42ecSJoe Perchessub uniq { 1443beb42ecSJoe Perches my %seen; 1453beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1463beb42ecSJoe Perches} 1473beb42ecSJoe Perches 1483beb42ecSJoe Perchessub list_types { 1493beb42ecSJoe Perches my ($exitcode) = @_; 1503beb42ecSJoe Perches 1513beb42ecSJoe Perches my $count = 0; 1523beb42ecSJoe Perches 1533beb42ecSJoe Perches local $/ = undef; 1543beb42ecSJoe Perches 1553beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1563beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1573beb42ecSJoe Perches 1583beb42ecSJoe Perches my $text = <$script>; 1593beb42ecSJoe Perches close($script); 1603beb42ecSJoe Perches 1613beb42ecSJoe Perches my @types = (); 1620547fa58SJean Delvare # Also catch when type or level is passed through a variable 1630547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1643beb42ecSJoe Perches push (@types, $_); 1653beb42ecSJoe Perches } 1663beb42ecSJoe Perches @types = sort(uniq(@types)); 1673beb42ecSJoe Perches print("#\tMessage type\n\n"); 1683beb42ecSJoe Perches foreach my $type (@types) { 1693beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1703beb42ecSJoe Perches } 1713beb42ecSJoe Perches 1723beb42ecSJoe Perches exit($exitcode); 1733beb42ecSJoe Perches} 1743beb42ecSJoe Perches 175000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 176000d1cc1SJoe Perchesif (-f $conf) { 177000d1cc1SJoe Perches my @conf_args; 178000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 179000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 180000d1cc1SJoe Perches 181000d1cc1SJoe Perches while (<$conffile>) { 182000d1cc1SJoe Perches my $line = $_; 183000d1cc1SJoe Perches 184000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 185000d1cc1SJoe Perches $line =~ s/^\s*//g; 186000d1cc1SJoe Perches $line =~ s/\s+/ /g; 187000d1cc1SJoe Perches 188000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 189000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 190000d1cc1SJoe Perches 191000d1cc1SJoe Perches my @words = split(" ", $line); 192000d1cc1SJoe Perches foreach my $word (@words) { 193000d1cc1SJoe Perches last if ($word =~ m/^#/); 194000d1cc1SJoe Perches push (@conf_args, $word); 195000d1cc1SJoe Perches } 196000d1cc1SJoe Perches } 197000d1cc1SJoe Perches close($conffile); 198000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 199000d1cc1SJoe Perches} 200000d1cc1SJoe Perches 201737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 202737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 203737c0767SJohn Brooksforeach (@ARGV) { 204737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 205737c0767SJohn Brooks $_ = "--color=$color"; 206737c0767SJohn Brooks } 207737c0767SJohn Brooks} 208737c0767SJohn Brooks 2090a920b5bSAndy WhitcroftGetOptions( 2106c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2110a920b5bSAndy Whitcroft 'tree!' => \$tree, 2120a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2130a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2146c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2158905a67cSAndy Whitcroft 'terse!' => \$terse, 21634d8815fSJoe Perches 'showfile!' => \$showfile, 21777f5b10aSHannes Eder 'f|file!' => \$file, 2184a593c34SDu, Changbin 'g|git!' => \$git, 2196c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2206c72ffaaSAndy Whitcroft 'strict!' => \$check, 221000d1cc1SJoe Perches 'ignore=s' => \@ignore, 22291bfe484SJoe Perches 'types=s' => \@use, 223000d1cc1SJoe Perches 'show-types!' => \$show_types, 2243beb42ecSJoe Perches 'list-types!' => \$list_types, 2256cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 22656193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 227713a09deSAntonio Borneo 'tab-size=i' => \$tabsize, 2286c72ffaaSAndy Whitcroft 'root=s' => \$root, 2298905a67cSAndy Whitcroft 'summary!' => \$summary, 2308905a67cSAndy Whitcroft 'mailback!' => \$mailback, 23113214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2323705ce5bSJoe Perches 'fix!' => \$fix, 2339624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 234d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 235c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 236773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 237ebfd7d62SJoe Perches 'codespell!' => \$codespell, 238ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 23975ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 240737c0767SJohn Brooks 'color=s' => \$color, 241737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 242737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 2433e89ad85SJerome Forissier 'kconfig-prefix=s' => \${CONFIG_}, 24477f5b10aSHannes Eder 'h|help' => \$help, 24577f5b10aSHannes Eder 'version' => \$help 24677f5b10aSHannes Eder) or help(1); 24777f5b10aSHannes Eder 24877f5b10aSHannes Ederhelp(0) if ($help); 2490a920b5bSAndy Whitcroft 2503beb42ecSJoe Percheslist_types(0) if ($list_types); 2513beb42ecSJoe Perches 2529624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2532ac73b4fSJoe Perches$check_orig = $check; 2549624b8d6SJoe Perches 25532f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); 25632f30ca9SJoe Perches 2570a920b5bSAndy Whitcroftmy $exit = 0; 2580a920b5bSAndy Whitcroft 2595b57980dSJoe Perchesmy $perl_version_ok = 1; 260d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2615b57980dSJoe Perches $perl_version_ok = 0; 262d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2635b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 264d62a201fSDave Hansen} 265d62a201fSDave Hansen 26645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2670a920b5bSAndy Whitcroftif ($#ARGV < 0) { 26845107ff6SAllen Hubbe push(@ARGV, '-'); 2690a920b5bSAndy Whitcroft} 2700a920b5bSAndy Whitcroft 271737c0767SJohn Brooksif ($color =~ /^[01]$/) { 272737c0767SJohn Brooks $color = !$color; 273737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 274737c0767SJohn Brooks $color = 1; 275737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 276737c0767SJohn Brooks $color = 0; 277737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 278737c0767SJohn Brooks $color = (-t STDOUT); 279737c0767SJohn Brooks} else { 28032f30ca9SJoe Perches die "$P: Invalid color mode: $color\n"; 281737c0767SJohn Brooks} 282737c0767SJohn Brooks 283713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1 28432f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); 285713a09deSAntonio Borneo 28691bfe484SJoe Perchessub hash_save_array_words { 28791bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 28891bfe484SJoe Perches 28991bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 29091bfe484SJoe Perches foreach my $word (@array) { 291000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 292000d1cc1SJoe Perches $word =~ s/^\s*//g; 293000d1cc1SJoe Perches $word =~ s/\s+/ /g; 294000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 295000d1cc1SJoe Perches 296000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 297000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 298000d1cc1SJoe Perches 29991bfe484SJoe Perches $hashRef->{$word}++; 300000d1cc1SJoe Perches } 30191bfe484SJoe Perches} 30291bfe484SJoe Perches 30391bfe484SJoe Perchessub hash_show_words { 30491bfe484SJoe Perches my ($hashRef, $prefix) = @_; 30591bfe484SJoe Perches 3063c816e49SJoe Perches if (keys %$hashRef) { 307d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 30858cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 30991bfe484SJoe Perches print " $word"; 31091bfe484SJoe Perches } 311d8469f16SJoe Perches print "\n"; 31291bfe484SJoe Perches } 31391bfe484SJoe Perches} 31491bfe484SJoe Perches 31591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 31691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 317000d1cc1SJoe Perches 318c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 319c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3207429c690SAndy Whitcroftmy $dbg_type = 0; 321a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 322c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 32321caa13cSAndy Whitcroft ## no critic 32421caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 32521caa13cSAndy Whitcroft die "$@" if ($@); 326c2fdda0dSAndy Whitcroft} 327c2fdda0dSAndy Whitcroft 328d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 329d2c0a235SAndy Whitcroft 3308905a67cSAndy Whitcroftif ($terse) { 3318905a67cSAndy Whitcroft $emacs = 1; 3328905a67cSAndy Whitcroft $quiet++; 3338905a67cSAndy Whitcroft} 3348905a67cSAndy Whitcroft 3356c72ffaaSAndy Whitcroftif ($tree) { 3366c72ffaaSAndy Whitcroft if (defined $root) { 3376c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3386c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3396c72ffaaSAndy Whitcroft } 3406c72ffaaSAndy Whitcroft } else { 3416c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3426c72ffaaSAndy Whitcroft $root = '.'; 3436c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3446c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3456c72ffaaSAndy Whitcroft $root = $1; 3466c72ffaaSAndy Whitcroft } 3476c72ffaaSAndy Whitcroft } 3486c72ffaaSAndy Whitcroft 3496c72ffaaSAndy Whitcroft if (!defined $root) { 3500a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3510a920b5bSAndy Whitcroft exit(2); 3520a920b5bSAndy Whitcroft } 3536c72ffaaSAndy Whitcroft} 3546c72ffaaSAndy Whitcroft 3556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3566c72ffaaSAndy Whitcroft 3572ceb532bSAndy Whitcroftour $Ident = qr{ 3582ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3592ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3602ceb532bSAndy Whitcroft }x; 3616c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3626c72ffaaSAndy Whitcroftour $Sparse = qr{ 3636c72ffaaSAndy Whitcroft __user| 3646c72ffaaSAndy Whitcroft __kernel| 3656c72ffaaSAndy Whitcroft __force| 3666c72ffaaSAndy Whitcroft __iomem| 3676c72ffaaSAndy Whitcroft __must_check| 368417495edSAndy Whitcroft __kprobes| 369165e72a6SSven Eckelmann __ref| 37033aa4597SGeert Uytterhoeven __refconst| 37133aa4597SGeert Uytterhoeven __refdata| 372ad315455SBoqun Feng __rcu| 373ad315455SBoqun Feng __private 3746c72ffaaSAndy Whitcroft }x; 375e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 376e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 377e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 378e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 379e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3808716de38SJoe Perches 38152131292SWolfram Sang# Notes to $Attribute: 38252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3836c72ffaaSAndy Whitcroftour $Attribute = qr{ 3846c72ffaaSAndy Whitcroft const| 38503f1df7dSJoe Perches __percpu| 38603f1df7dSJoe Perches __nocast| 38703f1df7dSJoe Perches __safe| 38846d832f5SMichael S. Tsirkin __bitwise| 38903f1df7dSJoe Perches __packed__| 39003f1df7dSJoe Perches __packed2__| 39103f1df7dSJoe Perches __naked| 39203f1df7dSJoe Perches __maybe_unused| 39303f1df7dSJoe Perches __always_unused| 39403f1df7dSJoe Perches __noreturn| 39503f1df7dSJoe Perches __used| 39603f1df7dSJoe Perches __cold| 397e23ef1f3SJoe Perches __pure| 39803f1df7dSJoe Perches __noclone| 39903f1df7dSJoe Perches __deprecated| 4006c72ffaaSAndy Whitcroft __read_mostly| 401c5967e98SJoe Perches __ro_after_init| 4026c72ffaaSAndy Whitcroft __kprobes| 4038716de38SJoe Perches $InitAttribute| 40424e1d81aSAndy Whitcroft ____cacheline_aligned| 40524e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 4065fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 4075fe3af11SAndy Whitcroft __weak 4086c72ffaaSAndy Whitcroft }x; 409c45dcabdSAndy Whitcroftour $Modifier; 41091cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 4116c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 4126c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 4136c72ffaaSAndy Whitcroft 41495e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 41595e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 41695e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 41795e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4182435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 419c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 420326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 421326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 422326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 42374349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4242435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 425326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 426447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 42723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4286c72ffaaSAndy Whitcroftour $Operators = qr{ 4296c72ffaaSAndy Whitcroft <=|>=|==|!=| 4306c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 43123f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4326c72ffaaSAndy Whitcroft }x; 4336c72ffaaSAndy Whitcroft 43491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 43591cb5195SJoe Perches 436ab7e23f3SJoe Perchesour $BasicType; 4378905a67cSAndy Whitcroftour $NonptrType; 4381813087dSJoe Perchesour $NonptrTypeMisordered; 4398716de38SJoe Perchesour $NonptrTypeWithAttr; 4408905a67cSAndy Whitcroftour $Type; 4411813087dSJoe Perchesour $TypeMisordered; 4428905a67cSAndy Whitcroftour $Declare; 4431813087dSJoe Perchesour $DeclareMisordered; 4448905a67cSAndy Whitcroft 44515662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 44615662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 447171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 448171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 449171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 450171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 451171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 452171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 453171ae1a4SAndy Whitcroft}x; 454171ae1a4SAndy Whitcroft 45515662b3eSJoe Perchesour $UTF8 = qr{ 45615662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 45715662b3eSJoe Perches | $NON_ASCII_UTF8 45815662b3eSJoe Perches}x; 45915662b3eSJoe Perches 460e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 461021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 462021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 463021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 464021158b4SJoe Perches)}; 465e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 466fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4678ed22cadSAndy Whitcroft atomic_t 4688ed22cadSAndy Whitcroft)}; 469e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 470e6176fa4SJoe Perches $typeC99Typedefs\b| 471e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 472e6176fa4SJoe Perches $typeKernelTypedefs\b 473e6176fa4SJoe Perches)}; 4748ed22cadSAndy Whitcroft 4756d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4766d32f7a3SJoe Perches 477691e669bSJoe Perchesour $logFunctions = qr{(?x: 478758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4797d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 48087bd499aSJoe Perches TP_printk| 4816e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 482b0531722SJoe Perches panic| 48306668727SJoe Perches MODULE_[A-Z_]+| 48406668727SJoe Perches seq_vprintf|seq_printf|seq_puts 485691e669bSJoe Perches)}; 486691e669bSJoe Perches 487e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 488e29a70f1SJoe Perches (?:(?:devm_)? 489e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 490e29a70f1SJoe Perches kstrdup(?:_const)? | 491e29a70f1SJoe Perches kmemdup(?:_nul)?) | 492461e1565SChristophe JAILLET (?:\w+)?alloc_skb(?:_ip_align)? | 493e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 494e29a70f1SJoe Perches dma_alloc_coherent 495e29a70f1SJoe Perches)}; 496e29a70f1SJoe Perches 49720112475SJoe Perchesour $signature_tags = qr{(?xi: 49820112475SJoe Perches Signed-off-by:| 499d499480cSJorge Ramirez-Ortiz Co-developed-by:| 50020112475SJoe Perches Acked-by:| 50120112475SJoe Perches Tested-by:| 50220112475SJoe Perches Reviewed-by:| 50320112475SJoe Perches Reported-by:| 5048543ae12SMugunthan V N Suggested-by:| 50520112475SJoe Perches To:| 50620112475SJoe Perches Cc: 50720112475SJoe Perches)}; 50820112475SJoe Perches 509831242abSAditya Srivastavasub edit_distance_min { 510831242abSAditya Srivastava my (@arr) = @_; 511831242abSAditya Srivastava my $len = scalar @arr; 512831242abSAditya Srivastava if ((scalar @arr) < 1) { 513831242abSAditya Srivastava # if underflow, return 514831242abSAditya Srivastava return; 515831242abSAditya Srivastava } 516831242abSAditya Srivastava my $min = $arr[0]; 517831242abSAditya Srivastava for my $i (0 .. ($len-1)) { 518831242abSAditya Srivastava if ($arr[$i] < $min) { 519831242abSAditya Srivastava $min = $arr[$i]; 520831242abSAditya Srivastava } 521831242abSAditya Srivastava } 522831242abSAditya Srivastava return $min; 523831242abSAditya Srivastava} 524831242abSAditya Srivastava 525831242abSAditya Srivastavasub get_edit_distance { 526831242abSAditya Srivastava my ($str1, $str2) = @_; 527831242abSAditya Srivastava $str1 = lc($str1); 528831242abSAditya Srivastava $str2 = lc($str2); 529831242abSAditya Srivastava $str1 =~ s/-//g; 530831242abSAditya Srivastava $str2 =~ s/-//g; 531831242abSAditya Srivastava my $len1 = length($str1); 532831242abSAditya Srivastava my $len2 = length($str2); 533831242abSAditya Srivastava # two dimensional array storing minimum edit distance 534831242abSAditya Srivastava my @distance; 535831242abSAditya Srivastava for my $i (0 .. $len1) { 536831242abSAditya Srivastava for my $j (0 .. $len2) { 537831242abSAditya Srivastava if ($i == 0) { 538831242abSAditya Srivastava $distance[$i][$j] = $j; 539831242abSAditya Srivastava } elsif ($j == 0) { 540831242abSAditya Srivastava $distance[$i][$j] = $i; 541831242abSAditya Srivastava } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) { 542831242abSAditya Srivastava $distance[$i][$j] = $distance[$i - 1][$j - 1]; 543831242abSAditya Srivastava } else { 544831242abSAditya Srivastava my $dist1 = $distance[$i][$j - 1]; #insert distance 545831242abSAditya Srivastava my $dist2 = $distance[$i - 1][$j]; # remove 546831242abSAditya Srivastava my $dist3 = $distance[$i - 1][$j - 1]; #replace 547831242abSAditya Srivastava $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3); 548831242abSAditya Srivastava } 549831242abSAditya Srivastava } 550831242abSAditya Srivastava } 551831242abSAditya Srivastava return $distance[$len1][$len2]; 552831242abSAditya Srivastava} 553831242abSAditya Srivastava 554831242abSAditya Srivastavasub find_standard_signature { 555831242abSAditya Srivastava my ($sign_off) = @_; 556831242abSAditya Srivastava my @standard_signature_tags = ( 557831242abSAditya Srivastava 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:', 558831242abSAditya Srivastava 'Reviewed-by:', 'Reported-by:', 'Suggested-by:' 559831242abSAditya Srivastava ); 560831242abSAditya Srivastava foreach my $signature (@standard_signature_tags) { 561831242abSAditya Srivastava return $signature if (get_edit_distance($sign_off, $signature) <= 2); 562831242abSAditya Srivastava } 563831242abSAditya Srivastava 564831242abSAditya Srivastava return ""; 565831242abSAditya Srivastava} 566831242abSAditya Srivastava 5671813087dSJoe Perchesour @typeListMisordered = ( 5681813087dSJoe Perches qr{char\s+(?:un)?signed}, 5691813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 5701813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 5711813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 5721813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 5731813087dSJoe Perches qr{short\s+(?:un)?signed}, 5741813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5751813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5761813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5771813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5781813087dSJoe Perches qr{int\s+(?:un)?signed}, 5791813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5801813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5811813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5821813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5831813087dSJoe Perches qr{long\s+(?:un)?signed}, 5841813087dSJoe Perches); 5851813087dSJoe Perches 5868905a67cSAndy Whitcroftour @typeList = ( 5878905a67cSAndy Whitcroft qr{void}, 5880c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5890c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5900c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5910c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5920c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5930c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5940c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5950c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5960c773d9dSJoe Perches qr{(?:un)?signed}, 5978905a67cSAndy Whitcroft qr{float}, 5988905a67cSAndy Whitcroft qr{double}, 5998905a67cSAndy Whitcroft qr{bool}, 6008905a67cSAndy Whitcroft qr{struct\s+$Ident}, 6018905a67cSAndy Whitcroft qr{union\s+$Ident}, 6028905a67cSAndy Whitcroft qr{enum\s+$Ident}, 6038905a67cSAndy Whitcroft qr{${Ident}_t}, 6048905a67cSAndy Whitcroft qr{${Ident}_handler}, 6058905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 6061813087dSJoe Perches @typeListMisordered, 6078905a67cSAndy Whitcroft); 608938224b5SJoe Perches 609938224b5SJoe Perchesour $C90_int_types = qr{(?x: 610938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 611938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 612938224b5SJoe Perches long\s+long\s+(?:un)?signed| 613938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 614938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 615938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 616938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 617938224b5SJoe Perches 618938224b5SJoe Perches long\s+int\s+(?:un)?signed| 619938224b5SJoe Perches long\s+(?:un)?signed\s+int| 620938224b5SJoe Perches long\s+(?:un)?signed| 621938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 622938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 623938224b5SJoe Perches int\s+long\s+(?:un)?signed| 624938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 625938224b5SJoe Perches 626938224b5SJoe Perches int\s+(?:un)?signed| 627938224b5SJoe Perches (?:(?:un)?signed\s+)?int 628938224b5SJoe Perches)}; 629938224b5SJoe Perches 630485ff23eSAlex Dowadour @typeListFile = (); 6318716de38SJoe Perchesour @typeListWithAttr = ( 6328716de38SJoe Perches @typeList, 6338716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 6348716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 6358716de38SJoe Perches); 6368716de38SJoe Perches 637c45dcabdSAndy Whitcroftour @modifierList = ( 638c45dcabdSAndy Whitcroft qr{fastcall}, 639c45dcabdSAndy Whitcroft); 640485ff23eSAlex Dowadour @modifierListFile = (); 6418905a67cSAndy Whitcroft 6422435880fSJoe Perchesour @mode_permission_funcs = ( 6432435880fSJoe Perches ["module_param", 3], 6442435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 6452435880fSJoe Perches ["module_param_array_named", 5], 6462435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 6472435880fSJoe Perches ["proc_create(?:_data|)", 2], 648459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 649459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 650459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 651459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 652459cf0aeSJoe Perches ["__ATTR", 2], 6532435880fSJoe Perches); 6542435880fSJoe Perches 6551a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; 6561a3dcf2eSJoe Perches 657515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 658515a235eSJoe Perchesour $mode_perms_search = ""; 659515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 660515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 661515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 662515a235eSJoe Perches} 66300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 664515a235eSJoe Perches 6659189c7e7SJoe Perchesour %deprecated_apis = ( 6669189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 6679189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 6689189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 6699189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 6709189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 6719189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 6729189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 6739189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 6749189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 6759189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6769189c7e7SJoe Perches); 6779189c7e7SJoe Perches 6789189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6799189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6809189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6819189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6829189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6839189c7e7SJoe Perches} 6849189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6859189c7e7SJoe Perches 686b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 687b392c64fSJoe Perches S_IWUGO | 688b392c64fSJoe Perches S_IWOTH | 689b392c64fSJoe Perches S_IRWXUGO | 690b392c64fSJoe Perches S_IALLUGO | 691b392c64fSJoe Perches 0[0-7][0-7][2367] 692b392c64fSJoe Perches}x; 693b392c64fSJoe Perches 694f90774e1SJoe Perchesour %mode_permission_string_types = ( 695f90774e1SJoe Perches "S_IRWXU" => 0700, 696f90774e1SJoe Perches "S_IRUSR" => 0400, 697f90774e1SJoe Perches "S_IWUSR" => 0200, 698f90774e1SJoe Perches "S_IXUSR" => 0100, 699f90774e1SJoe Perches "S_IRWXG" => 0070, 700f90774e1SJoe Perches "S_IRGRP" => 0040, 701f90774e1SJoe Perches "S_IWGRP" => 0020, 702f90774e1SJoe Perches "S_IXGRP" => 0010, 703f90774e1SJoe Perches "S_IRWXO" => 0007, 704f90774e1SJoe Perches "S_IROTH" => 0004, 705f90774e1SJoe Perches "S_IWOTH" => 0002, 706f90774e1SJoe Perches "S_IXOTH" => 0001, 707f90774e1SJoe Perches "S_IRWXUGO" => 0777, 708f90774e1SJoe Perches "S_IRUGO" => 0444, 709f90774e1SJoe Perches "S_IWUGO" => 0222, 710f90774e1SJoe Perches "S_IXUGO" => 0111, 711f90774e1SJoe Perches); 712f90774e1SJoe Perches 713f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 714f90774e1SJoe Perchesour $mode_perms_string_search = ""; 715f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 716f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 717f90774e1SJoe Perches $mode_perms_string_search .= $entry; 718f90774e1SJoe Perches} 71900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 72000180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 72100180468SJoe Perches ${single_mode_perms_string_search} 72200180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 72300180468SJoe Perches}x; 72400180468SJoe Perches 72500180468SJoe Perchessub perms_to_octal { 72600180468SJoe Perches my ($string) = @_; 72700180468SJoe Perches 72800180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 72900180468SJoe Perches 73000180468SJoe Perches my $val = ""; 73100180468SJoe Perches my $oval = ""; 73200180468SJoe Perches my $to = 0; 73300180468SJoe Perches my $curpos = 0; 73400180468SJoe Perches my $lastpos = 0; 73500180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 73600180468SJoe Perches $curpos = pos($string); 73700180468SJoe Perches my $match = $2; 73800180468SJoe Perches my $omatch = $1; 73900180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 74000180468SJoe Perches $lastpos = $curpos; 74100180468SJoe Perches $to |= $mode_permission_string_types{$match}; 74200180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 74300180468SJoe Perches $val .= $match; 74400180468SJoe Perches $oval .= $omatch; 74500180468SJoe Perches } 74600180468SJoe Perches $oval =~ s/^\s*\|\s*//; 74700180468SJoe Perches $oval =~ s/\s*\|\s*$//; 74800180468SJoe Perches return sprintf("%04o", $to); 74900180468SJoe Perches} 750f90774e1SJoe Perches 7517840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 7527840a94cSWolfram Sang irq| 753cdcee686SSergey Ryazanov memory| 754cdcee686SSergey Ryazanov time| 755cdcee686SSergey Ryazanov reboot 7567840a94cSWolfram Sang)}; 7577840a94cSWolfram Sang# memory.h: ARM has a custom one 7587840a94cSWolfram Sang 75966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 76066b47b4aSKees Cookmy $misspellings; 76166b47b4aSKees Cookmy %spelling_fix; 76236061e38SJoe Perches 76336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 76466b47b4aSKees Cook while (<$spelling>) { 76566b47b4aSKees Cook my $line = $_; 76666b47b4aSKees Cook 76766b47b4aSKees Cook $line =~ s/\s*\n?$//g; 76866b47b4aSKees Cook $line =~ s/^\s*//g; 76966b47b4aSKees Cook 77066b47b4aSKees Cook next if ($line =~ m/^\s*#/); 77166b47b4aSKees Cook next if ($line =~ m/^\s*$/); 77266b47b4aSKees Cook 77366b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 77466b47b4aSKees Cook 77566b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 77666b47b4aSKees Cook } 77766b47b4aSKees Cook close($spelling); 77836061e38SJoe Perches} else { 77936061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 78036061e38SJoe Perches} 78166b47b4aSKees Cook 782ebfd7d62SJoe Perchesif ($codespell) { 783ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 784ebfd7d62SJoe Perches while (<$spelling>) { 785ebfd7d62SJoe Perches my $line = $_; 786ebfd7d62SJoe Perches 787ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 788ebfd7d62SJoe Perches $line =~ s/^\s*//g; 789ebfd7d62SJoe Perches 790ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 791ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 792ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 793ebfd7d62SJoe Perches 794ebfd7d62SJoe Perches $line =~ s/,.*$//; 795ebfd7d62SJoe Perches 796ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 797ebfd7d62SJoe Perches 798ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 799ebfd7d62SJoe Perches } 800ebfd7d62SJoe Perches close($spelling); 801ebfd7d62SJoe Perches } else { 802ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 803ebfd7d62SJoe Perches } 804ebfd7d62SJoe Perches} 805ebfd7d62SJoe Perches 806ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 807ebfd7d62SJoe Perches 80875ad8c57SJerome Forissiersub read_words { 80975ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 81075ad8c57SJerome Forissier 81175ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 81275ad8c57SJerome Forissier while (<$words>) { 813bf1fa1daSJoe Perches my $line = $_; 814bf1fa1daSJoe Perches 815bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 816bf1fa1daSJoe Perches $line =~ s/^\s*//g; 817bf1fa1daSJoe Perches 818bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 819bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 820bf1fa1daSJoe Perches if ($line =~ /\s/) { 82175ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 822bf1fa1daSJoe Perches next; 823bf1fa1daSJoe Perches } 824bf1fa1daSJoe Perches 825ced69da1SQuentin Monnet $$wordsRef .= '|' if (defined $$wordsRef); 82675ad8c57SJerome Forissier $$wordsRef .= $line; 827bf1fa1daSJoe Perches } 82875ad8c57SJerome Forissier close($file); 82975ad8c57SJerome Forissier return 1; 830bf1fa1daSJoe Perches } 831bf1fa1daSJoe Perches 83275ad8c57SJerome Forissier return 0; 83375ad8c57SJerome Forissier} 83475ad8c57SJerome Forissier 835ced69da1SQuentin Monnetmy $const_structs; 836ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) { 83775ad8c57SJerome Forissier read_words(\$const_structs, $conststructsfile) 83875ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 839ced69da1SQuentin Monnet} 84075ad8c57SJerome Forissier 841ced69da1SQuentin Monnetif (defined($typedefsfile)) { 842ced69da1SQuentin Monnet my $typeOtherTypedefs; 84375ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 84475ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 845ced69da1SQuentin Monnet $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); 84675ad8c57SJerome Forissier} 84775ad8c57SJerome Forissier 8488905a67cSAndy Whitcroftsub build_types { 849485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 850485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 8511813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 8528716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 853c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 854ab7e23f3SJoe Perches $BasicType = qr{ 855ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 856ab7e23f3SJoe Perches (?:${all}\b) 857ab7e23f3SJoe Perches }x; 8588905a67cSAndy Whitcroft $NonptrType = qr{ 859d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 860cf655043SAndy Whitcroft (?: 8616b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 8628ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 863c45dcabdSAndy Whitcroft (?:${all}\b) 864cf655043SAndy Whitcroft ) 865c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 8668905a67cSAndy Whitcroft }x; 8671813087dSJoe Perches $NonptrTypeMisordered = qr{ 8681813087dSJoe Perches (?:$Modifier\s+|const\s+)* 8691813087dSJoe Perches (?: 8701813087dSJoe Perches (?:${Misordered}\b) 8711813087dSJoe Perches ) 8721813087dSJoe Perches (?:\s+$Modifier|\s+const)* 8731813087dSJoe Perches }x; 8748716de38SJoe Perches $NonptrTypeWithAttr = qr{ 8758716de38SJoe Perches (?:$Modifier\s+|const\s+)* 8768716de38SJoe Perches (?: 8778716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8788716de38SJoe Perches (?:$typeTypedefs\b)| 8798716de38SJoe Perches (?:${allWithAttr}\b) 8808716de38SJoe Perches ) 8818716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8828716de38SJoe Perches }x; 8838905a67cSAndy Whitcroft $Type = qr{ 884c45dcabdSAndy Whitcroft $NonptrType 8857b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 886c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8878905a67cSAndy Whitcroft }x; 8881813087dSJoe Perches $TypeMisordered = qr{ 8891813087dSJoe Perches $NonptrTypeMisordered 8907b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 8911813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8921813087dSJoe Perches }x; 89391cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8941813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8958905a67cSAndy Whitcroft} 8968905a67cSAndy Whitcroftbuild_types(); 8976c72ffaaSAndy Whitcroft 8987d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 899d1fe9c09SJoe Perches 900d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 901d1fe9c09SJoe Perches# requires at least perl version v5.10.0 902d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 903d1fe9c09SJoe Perches 904d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 9052435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 906c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 9077d2367afSJoe Perches 908f8422308SJoe Perchesour $declaration_macros = qr{(?x: 9093e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 910fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 9113d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 912f8422308SJoe Perches)}; 913f8422308SJoe Perches 9148d0325ccSAditya Srivastavaour %allow_repeated_words = ( 9158d0325ccSAditya Srivastava add => '', 9168d0325ccSAditya Srivastava added => '', 9178d0325ccSAditya Srivastava bad => '', 9188d0325ccSAditya Srivastava be => '', 9198d0325ccSAditya Srivastava); 9208d0325ccSAditya Srivastava 9217d2367afSJoe Perchessub deparenthesize { 9227d2367afSJoe Perches my ($string) = @_; 9237d2367afSJoe Perches return "" if (!defined($string)); 9245b9553abSJoe Perches 9255b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 9265b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 9275b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 9285b9553abSJoe Perches } 9295b9553abSJoe Perches 9307d2367afSJoe Perches $string =~ s@\s+@ @g; 9315b9553abSJoe Perches 9327d2367afSJoe Perches return $string; 9337d2367afSJoe Perches} 9347d2367afSJoe Perches 9353445686aSJoe Perchessub seed_camelcase_file { 9363445686aSJoe Perches my ($file) = @_; 9373445686aSJoe Perches 9383445686aSJoe Perches return if (!(-f $file)); 9393445686aSJoe Perches 9403445686aSJoe Perches local $/; 9413445686aSJoe Perches 9423445686aSJoe Perches open(my $include_file, '<', "$file") 9433445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 9443445686aSJoe Perches my $text = <$include_file>; 9453445686aSJoe Perches close($include_file); 9463445686aSJoe Perches 9473445686aSJoe Perches my @lines = split('\n', $text); 9483445686aSJoe Perches 9493445686aSJoe Perches foreach my $line (@lines) { 9503445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 9513445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 9523445686aSJoe Perches $camelcase{$1} = 1; 95311ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 95411ea516aSJoe Perches $camelcase{$1} = 1; 95511ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 9563445686aSJoe Perches $camelcase{$1} = 1; 9573445686aSJoe Perches } 9583445686aSJoe Perches } 9593445686aSJoe Perches} 9603445686aSJoe Perches 961cd28b119SJoe Perchesour %maintained_status = (); 962cd28b119SJoe Perches 96385b0ee18SJoe Perchessub is_maintained_obsolete { 96485b0ee18SJoe Perches my ($filename) = @_; 96585b0ee18SJoe Perches 966f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 96785b0ee18SJoe Perches 968cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 969cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 970cd28b119SJoe Perches } 97185b0ee18SJoe Perches 972cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 97385b0ee18SJoe Perches} 97485b0ee18SJoe Perches 9753b6e8ac9SJoe Perchessub is_SPDX_License_valid { 9763b6e8ac9SJoe Perches my ($license) = @_; 9773b6e8ac9SJoe Perches 9780f7f635bSJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); 9793b6e8ac9SJoe Perches 98056294112SJoe Perches my $root_path = abs_path($root); 98156294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 9823b6e8ac9SJoe Perches return 0 if ($status ne ""); 9833b6e8ac9SJoe Perches return 1; 9843b6e8ac9SJoe Perches} 9853b6e8ac9SJoe Perches 9863445686aSJoe Perchesmy $camelcase_seeded = 0; 9873445686aSJoe Perchessub seed_camelcase_includes { 9883445686aSJoe Perches return if ($camelcase_seeded); 9893445686aSJoe Perches 9903445686aSJoe Perches my $files; 991c707a81dSJoe Perches my $camelcase_cache = ""; 992c707a81dSJoe Perches my @include_files = (); 993c707a81dSJoe Perches 994c707a81dSJoe Perches $camelcase_seeded = 1; 995351b2a1fSJoe Perches 9960f7f635bSJoe Perches if (-e "$gitroot") { 997dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 998351b2a1fSJoe Perches chomp $git_last_include_commit; 999c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 1000c707a81dSJoe Perches } else { 1001c707a81dSJoe Perches my $last_mod_date = 0; 1002c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 1003c707a81dSJoe Perches @include_files = split('\n', $files); 1004c707a81dSJoe Perches foreach my $file (@include_files) { 1005c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 1006c707a81dSJoe Perches localtime((stat $file)[9])); 1007c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 1008c707a81dSJoe Perches } 1009c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 1010c707a81dSJoe Perches } 1011c707a81dSJoe Perches 1012c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 1013c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 1014c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 1015351b2a1fSJoe Perches while (<$camelcase_file>) { 1016351b2a1fSJoe Perches chomp; 1017351b2a1fSJoe Perches $camelcase{$_} = 1; 1018351b2a1fSJoe Perches } 1019351b2a1fSJoe Perches close($camelcase_file); 1020351b2a1fSJoe Perches 1021351b2a1fSJoe Perches return; 1022351b2a1fSJoe Perches } 1023c707a81dSJoe Perches 10240f7f635bSJoe Perches if (-e "$gitroot") { 1025dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 1026c707a81dSJoe Perches @include_files = split('\n', $files); 10273445686aSJoe Perches } 1028c707a81dSJoe Perches 10293445686aSJoe Perches foreach my $file (@include_files) { 10303445686aSJoe Perches seed_camelcase_file($file); 10313445686aSJoe Perches } 1032351b2a1fSJoe Perches 1033c707a81dSJoe Perches if ($camelcase_cache ne "") { 1034351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 1035c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 1036c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 1037351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 1038351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 1039351b2a1fSJoe Perches } 1040351b2a1fSJoe Perches close($camelcase_file); 1041351b2a1fSJoe Perches } 10423445686aSJoe Perches} 10433445686aSJoe Perches 1044f5f61325SJoe Perchessub git_is_single_file { 1045f5f61325SJoe Perches my ($filename) = @_; 1046f5f61325SJoe Perches 1047f5f61325SJoe Perches return 0 if ((which("git") eq "") || !(-e "$gitroot")); 1048f5f61325SJoe Perches 1049f5f61325SJoe Perches my $output = `${git_command} ls-files -- $filename 2>/dev/null`; 1050f5f61325SJoe Perches my $count = $output =~ tr/\n//; 1051f5f61325SJoe Perches return $count eq 1 && $output =~ m{^${filename}$}; 1052f5f61325SJoe Perches} 1053f5f61325SJoe Perches 1054d311cd44SJoe Perchessub git_commit_info { 1055d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 1056d311cd44SJoe Perches 10570f7f635bSJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); 1058d311cd44SJoe Perches 1059dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 1060d311cd44SJoe Perches $output =~ s/^\s*//gm; 1061d311cd44SJoe Perches my @lines = split("\n", $output); 1062d311cd44SJoe Perches 10630d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 10640d7835fcSJoe Perches 10655a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 1066d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 1067d311cd44SJoe Perches# all matching commit ids, but it's very slow... 1068d311cd44SJoe Perches# 1069d311cd44SJoe Perches# echo "checking commits $1..." 1070d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 1071d311cd44SJoe Perches# while read line ; do 1072d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 1073d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 1074d311cd44SJoe Perches# done 1075d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 1076948b133aSHeinrich Schuchardt $id = undef; 1077d311cd44SJoe Perches } else { 1078d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 1079d311cd44SJoe Perches $desc = substr($lines[0], 41); 1080d311cd44SJoe Perches } 1081d311cd44SJoe Perches 1082d311cd44SJoe Perches return ($id, $desc); 1083d311cd44SJoe Perches} 1084d311cd44SJoe Perches 10856c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 10860a920b5bSAndy Whitcroft 108700df344fSAndy Whitcroftmy @rawlines = (); 1088c2fdda0dSAndy Whitcroftmy @lines = (); 10893705ce5bSJoe Perchesmy @fixed = (); 1090d752fcc8SJoe Perchesmy @fixed_inserted = (); 1091d752fcc8SJoe Perchesmy @fixed_deleted = (); 1092194f66fcSJoe Perchesmy $fixlinenr = -1; 1093194f66fcSJoe Perches 10944a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10954a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 10960f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot"); 10974a593c34SDu, Changbin 10984a593c34SDu, Changbinif ($git) { 10994a593c34SDu, Changbin my @commits = (); 11000dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 11014a593c34SDu, Changbin my $git_range; 110228898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 110328898fd1SJoe Perches $git_range = "-$2 $1"; 11044a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 11054a593c34SDu, Changbin $git_range = "$commit_expr"; 11064a593c34SDu, Changbin } else { 11070dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 11080dea9f1eSJoe Perches } 1109dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 11100dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 111128898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 111228898fd1SJoe Perches next if (!defined($1) || !defined($2)); 11130dea9f1eSJoe Perches my $sha1 = $1; 11140dea9f1eSJoe Perches my $subject = $2; 11150dea9f1eSJoe Perches unshift(@commits, $sha1); 11160dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 11174a593c34SDu, Changbin } 11184a593c34SDu, Changbin } 11194a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 11204a593c34SDu, Changbin @ARGV = @commits; 11214a593c34SDu, Changbin} 11224a593c34SDu, Changbin 1123c2fdda0dSAndy Whitcroftmy $vname; 112498005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 11256c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 112621caa13cSAndy Whitcroft my $FILE; 1127f5f61325SJoe Perches my $is_git_file = git_is_single_file($filename); 1128f5f61325SJoe Perches my $oldfile = $file; 1129f5f61325SJoe Perches $file = 1 if ($is_git_file); 11304a593c34SDu, Changbin if ($git) { 11314a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 11324a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 11334a593c34SDu, Changbin } elsif ($file) { 113421caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 11356c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 113621caa13cSAndy Whitcroft } elsif ($filename eq '-') { 113721caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 11386c72ffaaSAndy Whitcroft } else { 113921caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 11406c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 11416c72ffaaSAndy Whitcroft } 1142c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1143c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 11444a593c34SDu, Changbin } elsif ($git) { 11450dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1146c2fdda0dSAndy Whitcroft } else { 1147c2fdda0dSAndy Whitcroft $vname = $filename; 1148c2fdda0dSAndy Whitcroft } 114921caa13cSAndy Whitcroft while (<$FILE>) { 11500a920b5bSAndy Whitcroft chomp; 115100df344fSAndy Whitcroft push(@rawlines, $_); 1152c7f574d0SGeert Uytterhoeven $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); 11536c72ffaaSAndy Whitcroft } 115421caa13cSAndy Whitcroft close($FILE); 1155d8469f16SJoe Perches 1156d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1157d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1158d8469f16SJoe Perches print "$vname\n"; 1159d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1160d8469f16SJoe Perches } 1161d8469f16SJoe Perches 1162c2fdda0dSAndy Whitcroft if (!process($filename)) { 11630a920b5bSAndy Whitcroft $exit = 1; 11640a920b5bSAndy Whitcroft } 116500df344fSAndy Whitcroft @rawlines = (); 116613214adfSAndy Whitcroft @lines = (); 11673705ce5bSJoe Perches @fixed = (); 1168d752fcc8SJoe Perches @fixed_inserted = (); 1169d752fcc8SJoe Perches @fixed_deleted = (); 1170194f66fcSJoe Perches $fixlinenr = -1; 1171485ff23eSAlex Dowad @modifierListFile = (); 1172485ff23eSAlex Dowad @typeListFile = (); 1173485ff23eSAlex Dowad build_types(); 1174f5f61325SJoe Perches $file = $oldfile if ($is_git_file); 11750a920b5bSAndy Whitcroft} 11760a920b5bSAndy Whitcroft 1177d8469f16SJoe Perchesif (!$quiet) { 11783c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 11793c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 11803c816e49SJoe Perches 11815b57980dSJoe Perches if (!$perl_version_ok) { 1182d8469f16SJoe Perches print << "EOM" 1183d8469f16SJoe Perches 1184d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 11855b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1186d8469f16SJoe PerchesEOM 1187d8469f16SJoe Perches } 1188d8469f16SJoe Perches if ($exit) { 1189d8469f16SJoe Perches print << "EOM" 1190d8469f16SJoe Perches 1191d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1192d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1193d8469f16SJoe PerchesEOM 1194d8469f16SJoe Perches } 1195d8469f16SJoe Perches} 1196d8469f16SJoe Perches 11970a920b5bSAndy Whitcroftexit($exit); 11980a920b5bSAndy Whitcroft 11990a920b5bSAndy Whitcroftsub top_of_kernel_tree { 12006c72ffaaSAndy Whitcroft my ($root) = @_; 12016c72ffaaSAndy Whitcroft 12026c72ffaaSAndy Whitcroft my @tree_check = ( 12036c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 12046c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 12056c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 12066c72ffaaSAndy Whitcroft ); 12076c72ffaaSAndy Whitcroft 12086c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 12096c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 12100a920b5bSAndy Whitcroft return 0; 12110a920b5bSAndy Whitcroft } 12126c72ffaaSAndy Whitcroft } 12136c72ffaaSAndy Whitcroft return 1; 12146c72ffaaSAndy Whitcroft} 12150a920b5bSAndy Whitcroft 121620112475SJoe Perchessub parse_email { 121720112475SJoe Perches my ($formatted_email) = @_; 121820112475SJoe Perches 121920112475SJoe Perches my $name = ""; 1220fccaebf0SDwaipayan Ray my $quoted = ""; 1221dfa05c28SJoe Perches my $name_comment = ""; 122220112475SJoe Perches my $address = ""; 122320112475SJoe Perches my $comment = ""; 122420112475SJoe Perches 122520112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 122620112475SJoe Perches $name = $1; 122720112475SJoe Perches $address = $2; 122820112475SJoe Perches $comment = $3 if defined $3; 122920112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 123020112475SJoe Perches $address = $1; 123120112475SJoe Perches $comment = $2 if defined $2; 123220112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 123320112475SJoe Perches $address = $1; 123420112475SJoe Perches $comment = $2 if defined $2; 123585e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 123620112475SJoe Perches $name = $formatted_email; 12373705ce5bSJoe Perches $name = trim($name); 123820112475SJoe Perches $name =~ s/^\"|\"$//g; 123920112475SJoe Perches # If there's a name left after stripping spaces and 124020112475SJoe Perches # leading quotes, and the address doesn't have both 124120112475SJoe Perches # leading and trailing angle brackets, the address 124220112475SJoe Perches # is invalid. ie: 124320112475SJoe Perches # "joe smith [email protected]" bad 124420112475SJoe Perches # "joe smith <[email protected]" bad 124520112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 124620112475SJoe Perches $name = ""; 124720112475SJoe Perches $address = ""; 124820112475SJoe Perches $comment = ""; 124920112475SJoe Perches } 125020112475SJoe Perches } 125120112475SJoe Perches 1252fccaebf0SDwaipayan Ray # Extract comments from names excluding quoted parts 1253fccaebf0SDwaipayan Ray # "John D. (Doe)" - Do not extract 1254fccaebf0SDwaipayan Ray if ($name =~ s/\"(.+)\"//) { 1255fccaebf0SDwaipayan Ray $quoted = $1; 1256dfa05c28SJoe Perches } 1257fccaebf0SDwaipayan Ray while ($name =~ s/\s*($balanced_parens)\s*/ /) { 1258fccaebf0SDwaipayan Ray $name_comment .= trim($1); 1259fccaebf0SDwaipayan Ray } 1260fccaebf0SDwaipayan Ray $name =~ s/^[ \"]+|[ \"]+$//g; 1261fccaebf0SDwaipayan Ray $name = trim("$quoted $name"); 1262fccaebf0SDwaipayan Ray 12633705ce5bSJoe Perches $address = trim($address); 126420112475SJoe Perches $address =~ s/^\<|\>$//g; 1265fccaebf0SDwaipayan Ray $comment = trim($comment); 126620112475SJoe Perches 126720112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 126820112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 126920112475SJoe Perches $name = "\"$name\""; 127020112475SJoe Perches } 127120112475SJoe Perches 1272dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 127320112475SJoe Perches} 127420112475SJoe Perches 127520112475SJoe Perchessub format_email { 127648ca2d8aSDwaipayan Ray my ($name, $name_comment, $address, $comment) = @_; 127720112475SJoe Perches 127820112475SJoe Perches my $formatted_email; 127920112475SJoe Perches 1280fccaebf0SDwaipayan Ray $name =~ s/^[ \"]+|[ \"]+$//g; 12813705ce5bSJoe Perches $address = trim($address); 1282fccaebf0SDwaipayan Ray $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes 128320112475SJoe Perches 128420112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 128520112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 128620112475SJoe Perches $name = "\"$name\""; 128720112475SJoe Perches } 128820112475SJoe Perches 1289fccaebf0SDwaipayan Ray $name_comment = trim($name_comment); 1290fccaebf0SDwaipayan Ray $name_comment = " $name_comment" if ($name_comment ne ""); 1291fccaebf0SDwaipayan Ray $comment = trim($comment); 1292fccaebf0SDwaipayan Ray $comment = " $comment" if ($comment ne ""); 1293fccaebf0SDwaipayan Ray 129420112475SJoe Perches if ("$name" eq "") { 129520112475SJoe Perches $formatted_email = "$address"; 129620112475SJoe Perches } else { 129748ca2d8aSDwaipayan Ray $formatted_email = "$name$name_comment <$address>"; 129820112475SJoe Perches } 129948ca2d8aSDwaipayan Ray $formatted_email .= "$comment"; 130020112475SJoe Perches return $formatted_email; 130120112475SJoe Perches} 130220112475SJoe Perches 1303dfa05c28SJoe Perchessub reformat_email { 1304dfa05c28SJoe Perches my ($email) = @_; 1305dfa05c28SJoe Perches 1306dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 130748ca2d8aSDwaipayan Ray return format_email($email_name, $name_comment, $email_address, $comment); 1308dfa05c28SJoe Perches} 1309dfa05c28SJoe Perches 1310dfa05c28SJoe Perchessub same_email_addresses { 1311fccaebf0SDwaipayan Ray my ($email1, $email2) = @_; 1312dfa05c28SJoe Perches 1313dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1314dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1315dfa05c28SJoe Perches 131648ca2d8aSDwaipayan Ray return $email1_name eq $email2_name && 131748ca2d8aSDwaipayan Ray $email1_address eq $email2_address && 131848ca2d8aSDwaipayan Ray $name1_comment eq $name2_comment && 131948ca2d8aSDwaipayan Ray $comment1 eq $comment2; 132048ca2d8aSDwaipayan Ray} 1321dfa05c28SJoe Perches 1322d311cd44SJoe Perchessub which { 1323d311cd44SJoe Perches my ($bin) = @_; 1324d311cd44SJoe Perches 1325d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1326d311cd44SJoe Perches if (-e "$path/$bin") { 1327d311cd44SJoe Perches return "$path/$bin"; 1328d311cd44SJoe Perches } 1329d311cd44SJoe Perches } 1330d311cd44SJoe Perches 1331d311cd44SJoe Perches return ""; 1332d311cd44SJoe Perches} 1333d311cd44SJoe Perches 1334000d1cc1SJoe Perchessub which_conf { 1335000d1cc1SJoe Perches my ($conf) = @_; 1336000d1cc1SJoe Perches 1337000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1338000d1cc1SJoe Perches if (-e "$path/$conf") { 1339000d1cc1SJoe Perches return "$path/$conf"; 1340000d1cc1SJoe Perches } 1341000d1cc1SJoe Perches } 1342000d1cc1SJoe Perches 1343000d1cc1SJoe Perches return ""; 1344000d1cc1SJoe Perches} 1345000d1cc1SJoe Perches 13460a920b5bSAndy Whitcroftsub expand_tabs { 13470a920b5bSAndy Whitcroft my ($str) = @_; 13480a920b5bSAndy Whitcroft 13490a920b5bSAndy Whitcroft my $res = ''; 13500a920b5bSAndy Whitcroft my $n = 0; 13510a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 13520a920b5bSAndy Whitcroft if ($c eq "\t") { 13530a920b5bSAndy Whitcroft $res .= ' '; 13540a920b5bSAndy Whitcroft $n++; 1355713a09deSAntonio Borneo for (; ($n % $tabsize) != 0; $n++) { 13560a920b5bSAndy Whitcroft $res .= ' '; 13570a920b5bSAndy Whitcroft } 13580a920b5bSAndy Whitcroft next; 13590a920b5bSAndy Whitcroft } 13600a920b5bSAndy Whitcroft $res .= $c; 13610a920b5bSAndy Whitcroft $n++; 13620a920b5bSAndy Whitcroft } 13630a920b5bSAndy Whitcroft 13640a920b5bSAndy Whitcroft return $res; 13650a920b5bSAndy Whitcroft} 13666c72ffaaSAndy Whitcroftsub copy_spacing { 1367773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 13686c72ffaaSAndy Whitcroft return $res; 13696c72ffaaSAndy Whitcroft} 13700a920b5bSAndy Whitcroft 13714a0df2efSAndy Whitcroftsub line_stats { 13724a0df2efSAndy Whitcroft my ($line) = @_; 13734a0df2efSAndy Whitcroft 13744a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 13754a0df2efSAndy Whitcroft $line =~ s/^.//; 13764a0df2efSAndy Whitcroft $line = expand_tabs($line); 13774a0df2efSAndy Whitcroft 13784a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 13794a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 13804a0df2efSAndy Whitcroft 13814a0df2efSAndy Whitcroft return (length($line), length($white)); 13824a0df2efSAndy Whitcroft} 13834a0df2efSAndy Whitcroft 1384773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1385773647a0SAndy Whitcroft 1386773647a0SAndy Whitcroftsub sanitise_line_reset { 1387773647a0SAndy Whitcroft my ($in_comment) = @_; 1388773647a0SAndy Whitcroft 1389773647a0SAndy Whitcroft if ($in_comment) { 1390773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1391773647a0SAndy Whitcroft } else { 1392773647a0SAndy Whitcroft $sanitise_quote = ''; 1393773647a0SAndy Whitcroft } 1394773647a0SAndy Whitcroft} 139500df344fSAndy Whitcroftsub sanitise_line { 139600df344fSAndy Whitcroft my ($line) = @_; 139700df344fSAndy Whitcroft 139800df344fSAndy Whitcroft my $res = ''; 139900df344fSAndy Whitcroft my $l = ''; 140000df344fSAndy Whitcroft 1401c2fdda0dSAndy Whitcroft my $qlen = 0; 1402773647a0SAndy Whitcroft my $off = 0; 1403773647a0SAndy Whitcroft my $c; 140400df344fSAndy Whitcroft 1405773647a0SAndy Whitcroft # Always copy over the diff marker. 1406773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1407773647a0SAndy Whitcroft 1408773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1409773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1410773647a0SAndy Whitcroft 14118d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1412773647a0SAndy Whitcroft # and end, all to $;. 1413773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1414773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1415773647a0SAndy Whitcroft 1416773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1417773647a0SAndy Whitcroft $off++; 141800df344fSAndy Whitcroft next; 1419773647a0SAndy Whitcroft } 142081bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1421773647a0SAndy Whitcroft $sanitise_quote = ''; 1422773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1423773647a0SAndy Whitcroft $off++; 1424773647a0SAndy Whitcroft next; 1425773647a0SAndy Whitcroft } 1426113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1427113f04a8SDaniel Walker $sanitise_quote = '//'; 1428113f04a8SDaniel Walker 1429113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1430113f04a8SDaniel Walker $off++; 1431113f04a8SDaniel Walker next; 1432113f04a8SDaniel Walker } 1433773647a0SAndy Whitcroft 1434773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1435773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1436773647a0SAndy Whitcroft $c eq "\\") { 1437773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1438773647a0SAndy Whitcroft $off++; 1439773647a0SAndy Whitcroft next; 1440773647a0SAndy Whitcroft } 1441773647a0SAndy Whitcroft # Regular quotes. 1442773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1443773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1444773647a0SAndy Whitcroft $sanitise_quote = $c; 1445773647a0SAndy Whitcroft 1446773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1447773647a0SAndy Whitcroft next; 1448773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1449773647a0SAndy Whitcroft $sanitise_quote = ''; 145000df344fSAndy Whitcroft } 145100df344fSAndy Whitcroft } 1452773647a0SAndy Whitcroft 1453fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1454773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1455773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1456113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1457113f04a8SDaniel Walker substr($res, $off, 1, $;); 1458773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1459773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 146000df344fSAndy Whitcroft } else { 1461773647a0SAndy Whitcroft substr($res, $off, 1, $c); 146200df344fSAndy Whitcroft } 1463c2fdda0dSAndy Whitcroft } 1464c2fdda0dSAndy Whitcroft 1465113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1466113f04a8SDaniel Walker $sanitise_quote = ''; 1467113f04a8SDaniel Walker } 1468113f04a8SDaniel Walker 1469c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1470c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1471c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1472c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1473c2fdda0dSAndy Whitcroft 1474c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1475c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1476c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1477c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1478c2fdda0dSAndy Whitcroft } 1479c2fdda0dSAndy Whitcroft 1480dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1481dadf680dSJoe Perches my $match = $1; 1482dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1483dadf680dSJoe Perches } 1484dadf680dSJoe Perches 148500df344fSAndy Whitcroft return $res; 148600df344fSAndy Whitcroft} 148700df344fSAndy Whitcroft 1488a6962d72SJoe Perchessub get_quoted_string { 1489a6962d72SJoe Perches my ($line, $rawline) = @_; 1490a6962d72SJoe Perches 1491478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 149233acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1493a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1494a6962d72SJoe Perches} 1495a6962d72SJoe Perches 14968905a67cSAndy Whitcroftsub ctx_statement_block { 14978905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 14988905a67cSAndy Whitcroft my $line = $linenr - 1; 14998905a67cSAndy Whitcroft my $blk = ''; 15008905a67cSAndy Whitcroft my $soff = $off; 15018905a67cSAndy Whitcroft my $coff = $off - 1; 1502773647a0SAndy Whitcroft my $coff_set = 0; 15038905a67cSAndy Whitcroft 150413214adfSAndy Whitcroft my $loff = 0; 150513214adfSAndy Whitcroft 15068905a67cSAndy Whitcroft my $type = ''; 15078905a67cSAndy Whitcroft my $level = 0; 1508a2750645SAndy Whitcroft my @stack = (); 1509cf655043SAndy Whitcroft my $p; 15108905a67cSAndy Whitcroft my $c; 15118905a67cSAndy Whitcroft my $len = 0; 151213214adfSAndy Whitcroft 151313214adfSAndy Whitcroft my $remainder; 15148905a67cSAndy Whitcroft while (1) { 1515a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1516a2750645SAndy Whitcroft 1517773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 15188905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 15198905a67cSAndy Whitcroft # context. 15208905a67cSAndy Whitcroft if ($off >= $len) { 15218905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1522dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1523c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 15248905a67cSAndy Whitcroft $remain--; 152513214adfSAndy Whitcroft $loff = $len; 1526c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 15278905a67cSAndy Whitcroft $len = length($blk); 15288905a67cSAndy Whitcroft $line++; 15298905a67cSAndy Whitcroft last; 15308905a67cSAndy Whitcroft } 15318905a67cSAndy Whitcroft # Bail if there is no further context. 15328905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 153313214adfSAndy Whitcroft if ($off >= $len) { 15348905a67cSAndy Whitcroft last; 15358905a67cSAndy Whitcroft } 1536f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1537f74bd194SAndy Whitcroft $level++; 1538f74bd194SAndy Whitcroft $type = '#'; 1539f74bd194SAndy Whitcroft } 15408905a67cSAndy Whitcroft } 1541cf655043SAndy Whitcroft $p = $c; 15428905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 154313214adfSAndy Whitcroft $remainder = substr($blk, $off); 15448905a67cSAndy Whitcroft 1545773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 15464635f4fbSAndy Whitcroft 15474635f4fbSAndy Whitcroft # Handle nested #if/#else. 15484635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 15494635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 15504635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 15514635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 15524635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 15534635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 15544635f4fbSAndy Whitcroft } 15554635f4fbSAndy Whitcroft 15568905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 15578905a67cSAndy Whitcroft # outermost level. 15588905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 15598905a67cSAndy Whitcroft last; 15608905a67cSAndy Whitcroft } 15618905a67cSAndy Whitcroft 156213214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1563773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1564773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1565773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1566773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1567773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1568773647a0SAndy Whitcroft $coff_set = 1; 1569773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1570773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 157113214adfSAndy Whitcroft } 157213214adfSAndy Whitcroft 15738905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 15748905a67cSAndy Whitcroft $level++; 15758905a67cSAndy Whitcroft $type = '('; 15768905a67cSAndy Whitcroft } 15778905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 15788905a67cSAndy Whitcroft $level--; 15798905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 15808905a67cSAndy Whitcroft 15818905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 15828905a67cSAndy Whitcroft $coff = $off; 1583773647a0SAndy Whitcroft $coff_set = 1; 1584773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 15858905a67cSAndy Whitcroft } 15868905a67cSAndy Whitcroft } 15878905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 15888905a67cSAndy Whitcroft $level++; 15898905a67cSAndy Whitcroft $type = '{'; 15908905a67cSAndy Whitcroft } 15918905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 15928905a67cSAndy Whitcroft $level--; 15938905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 15948905a67cSAndy Whitcroft 15958905a67cSAndy Whitcroft if ($level == 0) { 1596b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1597b998e001SPatrick Pannuto $off++; 1598b998e001SPatrick Pannuto } 15998905a67cSAndy Whitcroft last; 16008905a67cSAndy Whitcroft } 16018905a67cSAndy Whitcroft } 1602f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1603f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1604f74bd194SAndy Whitcroft $level--; 1605f74bd194SAndy Whitcroft $type = ''; 1606f74bd194SAndy Whitcroft $off++; 1607f74bd194SAndy Whitcroft last; 1608f74bd194SAndy Whitcroft } 16098905a67cSAndy Whitcroft $off++; 16108905a67cSAndy Whitcroft } 1611a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 161213214adfSAndy Whitcroft if ($off == $len) { 1613a3bb97a7SAndy Whitcroft $loff = $len + 1; 161413214adfSAndy Whitcroft $line++; 161513214adfSAndy Whitcroft $remain--; 161613214adfSAndy Whitcroft } 16178905a67cSAndy Whitcroft 16188905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 16198905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 16208905a67cSAndy Whitcroft 16218905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 16228905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 16238905a67cSAndy Whitcroft 1624773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 162513214adfSAndy Whitcroft 162613214adfSAndy Whitcroft return ($statement, $condition, 162713214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 162813214adfSAndy Whitcroft} 162913214adfSAndy Whitcroft 1630cf655043SAndy Whitcroftsub statement_lines { 1631cf655043SAndy Whitcroft my ($stmt) = @_; 1632cf655043SAndy Whitcroft 1633cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1634cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1635cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1636cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1637cf655043SAndy Whitcroft 1638cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1639cf655043SAndy Whitcroft 1640cf655043SAndy Whitcroft return $#stmt_lines + 2; 1641cf655043SAndy Whitcroft} 1642cf655043SAndy Whitcroft 1643cf655043SAndy Whitcroftsub statement_rawlines { 1644cf655043SAndy Whitcroft my ($stmt) = @_; 1645cf655043SAndy Whitcroft 1646cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1647cf655043SAndy Whitcroft 1648cf655043SAndy Whitcroft return $#stmt_lines + 2; 1649cf655043SAndy Whitcroft} 1650cf655043SAndy Whitcroft 1651cf655043SAndy Whitcroftsub statement_block_size { 1652cf655043SAndy Whitcroft my ($stmt) = @_; 1653cf655043SAndy Whitcroft 1654cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1655cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1656cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1657cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1658cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1659cf655043SAndy Whitcroft 1660cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1661cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1662cf655043SAndy Whitcroft 1663cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1664cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1665cf655043SAndy Whitcroft 1666cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1667cf655043SAndy Whitcroft return $stmt_lines; 1668cf655043SAndy Whitcroft } else { 1669cf655043SAndy Whitcroft return $stmt_statements; 1670cf655043SAndy Whitcroft } 1671cf655043SAndy Whitcroft} 1672cf655043SAndy Whitcroft 167313214adfSAndy Whitcroftsub ctx_statement_full { 167413214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 167513214adfSAndy Whitcroft my ($statement, $condition, $level); 167613214adfSAndy Whitcroft 167713214adfSAndy Whitcroft my (@chunks); 167813214adfSAndy Whitcroft 1679cf655043SAndy Whitcroft # Grab the first conditional/block pair. 168013214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 168113214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1682773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 168313214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1684cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1685cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1686cf655043SAndy Whitcroft } 1687cf655043SAndy Whitcroft 1688cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1689cf655043SAndy Whitcroft # could continue the statement. 1690cf655043SAndy Whitcroft for (;;) { 169113214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 169213214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1693cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1694773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1695cf655043SAndy Whitcroft #print "C: push\n"; 1696cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 169713214adfSAndy Whitcroft } 169813214adfSAndy Whitcroft 169913214adfSAndy Whitcroft return ($level, $linenr, @chunks); 17008905a67cSAndy Whitcroft} 17018905a67cSAndy Whitcroft 17024a0df2efSAndy Whitcroftsub ctx_block_get { 1703f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 17044a0df2efSAndy Whitcroft my $line; 17054a0df2efSAndy Whitcroft my $start = $linenr - 1; 17064a0df2efSAndy Whitcroft my $blk = ''; 17074a0df2efSAndy Whitcroft my @o; 17084a0df2efSAndy Whitcroft my @c; 17094a0df2efSAndy Whitcroft my @res = (); 17104a0df2efSAndy Whitcroft 1711f0a594c1SAndy Whitcroft my $level = 0; 17124635f4fbSAndy Whitcroft my @stack = ($level); 171300df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 171400df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 171500df344fSAndy Whitcroft $remain--; 171600df344fSAndy Whitcroft 171700df344fSAndy Whitcroft $blk .= $rawlines[$line]; 17184635f4fbSAndy Whitcroft 17194635f4fbSAndy Whitcroft # Handle nested #if/#else. 172001464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 17214635f4fbSAndy Whitcroft push(@stack, $level); 172201464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 17234635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 172401464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 17254635f4fbSAndy Whitcroft $level = pop(@stack); 17264635f4fbSAndy Whitcroft } 17274635f4fbSAndy Whitcroft 172801464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1729f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1730f0a594c1SAndy Whitcroft if ($off > 0) { 1731f0a594c1SAndy Whitcroft $off--; 1732f0a594c1SAndy Whitcroft next; 1733f0a594c1SAndy Whitcroft } 17344a0df2efSAndy Whitcroft 1735f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1736f0a594c1SAndy Whitcroft $level--; 1737f0a594c1SAndy Whitcroft last if ($level == 0); 1738f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1739f0a594c1SAndy Whitcroft $level++; 1740f0a594c1SAndy Whitcroft } 1741f0a594c1SAndy Whitcroft } 17424a0df2efSAndy Whitcroft 1743f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 174400df344fSAndy Whitcroft push(@res, $rawlines[$line]); 17454a0df2efSAndy Whitcroft } 17464a0df2efSAndy Whitcroft 1747f0a594c1SAndy Whitcroft last if ($level == 0); 17484a0df2efSAndy Whitcroft } 17494a0df2efSAndy Whitcroft 1750f0a594c1SAndy Whitcroft return ($level, @res); 17514a0df2efSAndy Whitcroft} 17524a0df2efSAndy Whitcroftsub ctx_block_outer { 17534a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 17544a0df2efSAndy Whitcroft 1755f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1756f0a594c1SAndy Whitcroft return @r; 17574a0df2efSAndy Whitcroft} 17584a0df2efSAndy Whitcroftsub ctx_block { 17594a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 17604a0df2efSAndy Whitcroft 1761f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1762f0a594c1SAndy Whitcroft return @r; 1763653d4876SAndy Whitcroft} 1764653d4876SAndy Whitcroftsub ctx_statement { 1765f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1766f0a594c1SAndy Whitcroft 1767f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1768f0a594c1SAndy Whitcroft return @r; 1769f0a594c1SAndy Whitcroft} 1770f0a594c1SAndy Whitcroftsub ctx_block_level { 1771653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1772653d4876SAndy Whitcroft 1773f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 17744a0df2efSAndy Whitcroft} 17759c0ca6f9SAndy Whitcroftsub ctx_statement_level { 17769c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 17779c0ca6f9SAndy Whitcroft 17789c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 17799c0ca6f9SAndy Whitcroft} 17804a0df2efSAndy Whitcroft 17814a0df2efSAndy Whitcroftsub ctx_locate_comment { 17824a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17834a0df2efSAndy Whitcroft 1784a55ee0ccSJoe Perches # If c99 comment on the current line, or the line before or after 1785a55ee0ccSJoe Perches my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); 1786a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1787a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); 1788a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1789a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); 1790a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1791a55ee0ccSJoe Perches 17924a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1793a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 17944a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 17954a0df2efSAndy Whitcroft 17964a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 17974a0df2efSAndy Whitcroft # comment. 17984a0df2efSAndy Whitcroft my $in_comment = 0; 17994a0df2efSAndy Whitcroft $current_comment = ''; 18004a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 180100df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 180200df344fSAndy Whitcroft #warn " $line\n"; 18034a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 18044a0df2efSAndy Whitcroft $in_comment = 1; 18054a0df2efSAndy Whitcroft } 18064a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 18074a0df2efSAndy Whitcroft $in_comment = 1; 18084a0df2efSAndy Whitcroft } 18094a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 18104a0df2efSAndy Whitcroft $current_comment = ''; 18114a0df2efSAndy Whitcroft } 18124a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 18134a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 18144a0df2efSAndy Whitcroft $in_comment = 0; 18154a0df2efSAndy Whitcroft } 18164a0df2efSAndy Whitcroft } 18174a0df2efSAndy Whitcroft 18184a0df2efSAndy Whitcroft chomp($current_comment); 18194a0df2efSAndy Whitcroft return($current_comment); 18204a0df2efSAndy Whitcroft} 18214a0df2efSAndy Whitcroftsub ctx_has_comment { 18224a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 18234a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 18244a0df2efSAndy Whitcroft 182500df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 18264a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 18274a0df2efSAndy Whitcroft 18284a0df2efSAndy Whitcroft return ($cmt ne ''); 18294a0df2efSAndy Whitcroft} 18304a0df2efSAndy Whitcroft 18314d001e4dSAndy Whitcroftsub raw_line { 18324d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 18334d001e4dSAndy Whitcroft 18344d001e4dSAndy Whitcroft my $offset = $linenr - 1; 18354d001e4dSAndy Whitcroft $cnt++; 18364d001e4dSAndy Whitcroft 18374d001e4dSAndy Whitcroft my $line; 18384d001e4dSAndy Whitcroft while ($cnt) { 18394d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 18404d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 18414d001e4dSAndy Whitcroft $cnt--; 18424d001e4dSAndy Whitcroft } 18434d001e4dSAndy Whitcroft 18444d001e4dSAndy Whitcroft return $line; 18454d001e4dSAndy Whitcroft} 18464d001e4dSAndy Whitcroft 18472a9f9d85STobin C. Hardingsub get_stat_real { 18482a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 18492a9f9d85STobin C. Harding 18502a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 18512a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 18522a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 18532a9f9d85STobin C. Harding } 18542a9f9d85STobin C. Harding 18552a9f9d85STobin C. Harding return $stat_real; 18562a9f9d85STobin C. Harding} 18572a9f9d85STobin C. Harding 1858e3d95a2aSTobin C. Hardingsub get_stat_here { 1859e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1860e3d95a2aSTobin C. Harding 1861e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1862e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1863e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1864e3d95a2aSTobin C. Harding } 1865e3d95a2aSTobin C. Harding 1866e3d95a2aSTobin C. Harding return $herectx; 1867e3d95a2aSTobin C. Harding} 1868e3d95a2aSTobin C. Harding 18690a920b5bSAndy Whitcroftsub cat_vet { 18700a920b5bSAndy Whitcroft my ($vet) = @_; 18719c0ca6f9SAndy Whitcroft my ($res, $coded); 18720a920b5bSAndy Whitcroft 18739c0ca6f9SAndy Whitcroft $res = ''; 18746c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 18756c72ffaaSAndy Whitcroft $res .= $1; 18766c72ffaaSAndy Whitcroft if ($2 ne '') { 18779c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 18786c72ffaaSAndy Whitcroft $res .= $coded; 18796c72ffaaSAndy Whitcroft } 18809c0ca6f9SAndy Whitcroft } 18819c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 18820a920b5bSAndy Whitcroft 18839c0ca6f9SAndy Whitcroft return $res; 18840a920b5bSAndy Whitcroft} 18850a920b5bSAndy Whitcroft 1886c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1887cf655043SAndy Whitcroftmy $av_pending; 1888c2fdda0dSAndy Whitcroftmy @av_paren_type; 18891f65f947SAndy Whitcroftmy $av_pend_colon; 1890c2fdda0dSAndy Whitcroft 1891c2fdda0dSAndy Whitcroftsub annotate_reset { 1892c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1893cf655043SAndy Whitcroft $av_pending = '_'; 1894cf655043SAndy Whitcroft @av_paren_type = ('E'); 18951f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1896c2fdda0dSAndy Whitcroft} 1897c2fdda0dSAndy Whitcroft 18986c72ffaaSAndy Whitcroftsub annotate_values { 18996c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 19006c72ffaaSAndy Whitcroft 19016c72ffaaSAndy Whitcroft my $res; 19021f65f947SAndy Whitcroft my $var = '_' x length($stream); 19036c72ffaaSAndy Whitcroft my $cur = $stream; 19046c72ffaaSAndy Whitcroft 1905c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 19066c72ffaaSAndy Whitcroft 19076c72ffaaSAndy Whitcroft while (length($cur)) { 1908773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1909cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1910171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 19116c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1912c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1913c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1914cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1915c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 19166c72ffaaSAndy Whitcroft } 19176c72ffaaSAndy Whitcroft 1918c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 19199446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 19209446ef56SAndy Whitcroft push(@av_paren_type, $type); 1921addcdceaSAndy Whitcroft $type = 'c'; 19229446ef56SAndy Whitcroft 1923e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1924c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 19256c72ffaaSAndy Whitcroft $type = 'T'; 19266c72ffaaSAndy Whitcroft 1927389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1928389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1929389a2fe5SAndy Whitcroft $type = 'T'; 1930389a2fe5SAndy Whitcroft 1931c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1932171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1933c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1934171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1935171ae1a4SAndy Whitcroft if ($2 ne '') { 1936cf655043SAndy Whitcroft $av_pending = 'N'; 1937171ae1a4SAndy Whitcroft } 1938171ae1a4SAndy Whitcroft $type = 'E'; 1939171ae1a4SAndy Whitcroft 1940c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1941171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1942171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1943171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 19446c72ffaaSAndy Whitcroft 1945c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1946cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1947c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1948cf655043SAndy Whitcroft 1949cf655043SAndy Whitcroft push(@av_paren_type, $type); 1950cf655043SAndy Whitcroft push(@av_paren_type, $type); 1951171ae1a4SAndy Whitcroft $type = 'E'; 1952cf655043SAndy Whitcroft 1953c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1954cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1955cf655043SAndy Whitcroft $av_preprocessor = 1; 1956cf655043SAndy Whitcroft 1957cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1958cf655043SAndy Whitcroft 1959171ae1a4SAndy Whitcroft $type = 'E'; 1960cf655043SAndy Whitcroft 1961c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1962cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1963cf655043SAndy Whitcroft 1964cf655043SAndy Whitcroft $av_preprocessor = 1; 1965cf655043SAndy Whitcroft 1966cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1967cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1968cf655043SAndy Whitcroft pop(@av_paren_type); 1969cf655043SAndy Whitcroft push(@av_paren_type, $type); 1970171ae1a4SAndy Whitcroft $type = 'E'; 19716c72ffaaSAndy Whitcroft 19726c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1973c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 19746c72ffaaSAndy Whitcroft 1975171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1976171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1977171ae1a4SAndy Whitcroft $av_pending = $type; 1978171ae1a4SAndy Whitcroft $type = 'N'; 1979171ae1a4SAndy Whitcroft 19806c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1981c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 19826c72ffaaSAndy Whitcroft if (defined $2) { 1983cf655043SAndy Whitcroft $av_pending = 'V'; 19846c72ffaaSAndy Whitcroft } 19856c72ffaaSAndy Whitcroft $type = 'N'; 19866c72ffaaSAndy Whitcroft 198714b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1988c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 198914b111c1SAndy Whitcroft $av_pending = 'E'; 19906c72ffaaSAndy Whitcroft $type = 'N'; 19916c72ffaaSAndy Whitcroft 19921f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 19931f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 19941f65f947SAndy Whitcroft $av_pend_colon = 'C'; 19951f65f947SAndy Whitcroft $type = 'N'; 19961f65f947SAndy Whitcroft 199714b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1998c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 19996c72ffaaSAndy Whitcroft $type = 'N'; 20006c72ffaaSAndy Whitcroft 20016c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 2002c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 2003cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 2004cf655043SAndy Whitcroft $av_pending = '_'; 20056c72ffaaSAndy Whitcroft $type = 'N'; 20066c72ffaaSAndy Whitcroft 20076c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 2008cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 2009cf655043SAndy Whitcroft if ($new_type ne '_') { 2010cf655043SAndy Whitcroft $type = $new_type; 2011c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 2012c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 20136c72ffaaSAndy Whitcroft } else { 2014c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 20156c72ffaaSAndy Whitcroft } 20166c72ffaaSAndy Whitcroft 2017c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 2018c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 2019c8cb2ca3SAndy Whitcroft $type = 'V'; 2020cf655043SAndy Whitcroft $av_pending = 'V'; 20216c72ffaaSAndy Whitcroft 20228e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 20238e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 20241f65f947SAndy Whitcroft $av_pend_colon = 'B'; 20258e761b04SAndy Whitcroft } elsif ($type eq 'E') { 20268e761b04SAndy Whitcroft $av_pend_colon = 'L'; 20271f65f947SAndy Whitcroft } 20281f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 20291f65f947SAndy Whitcroft $type = 'V'; 20301f65f947SAndy Whitcroft 20316c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 2032c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 20336c72ffaaSAndy Whitcroft $type = 'V'; 20346c72ffaaSAndy Whitcroft 20356c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 2036c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 20376c72ffaaSAndy Whitcroft $type = 'N'; 20386c72ffaaSAndy Whitcroft 2039cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 2040c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 204113214adfSAndy Whitcroft $type = 'E'; 20421f65f947SAndy Whitcroft $av_pend_colon = 'O'; 204313214adfSAndy Whitcroft 20448e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 20458e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 20468e761b04SAndy Whitcroft $type = 'C'; 20478e761b04SAndy Whitcroft 20481f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 20491f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 20501f65f947SAndy Whitcroft $type = 'N'; 20511f65f947SAndy Whitcroft 20521f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 20531f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 20541f65f947SAndy Whitcroft 20551f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 20561f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 20571f65f947SAndy Whitcroft $type = 'E'; 20581f65f947SAndy Whitcroft } else { 20591f65f947SAndy Whitcroft $type = 'N'; 20601f65f947SAndy Whitcroft } 20611f65f947SAndy Whitcroft $av_pend_colon = 'O'; 20621f65f947SAndy Whitcroft 20638e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 206413214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 20656c72ffaaSAndy Whitcroft $type = 'N'; 20666c72ffaaSAndy Whitcroft 20670d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 206874048ed8SAndy Whitcroft my $variant; 206974048ed8SAndy Whitcroft 207074048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 207174048ed8SAndy Whitcroft if ($type eq 'V') { 207274048ed8SAndy Whitcroft $variant = 'B'; 207374048ed8SAndy Whitcroft } else { 207474048ed8SAndy Whitcroft $variant = 'U'; 207574048ed8SAndy Whitcroft } 207674048ed8SAndy Whitcroft 207774048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 207874048ed8SAndy Whitcroft $type = 'N'; 207974048ed8SAndy Whitcroft 20806c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 2081c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 20826c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 20836c72ffaaSAndy Whitcroft $type = 'N'; 20846c72ffaaSAndy Whitcroft } 20856c72ffaaSAndy Whitcroft 20866c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 2087c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 20886c72ffaaSAndy Whitcroft } 20896c72ffaaSAndy Whitcroft if (defined $1) { 20906c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 20916c72ffaaSAndy Whitcroft $res .= $type x length($1); 20926c72ffaaSAndy Whitcroft } 20936c72ffaaSAndy Whitcroft } 20946c72ffaaSAndy Whitcroft 20951f65f947SAndy Whitcroft return ($res, $var); 20966c72ffaaSAndy Whitcroft} 20976c72ffaaSAndy Whitcroft 20988905a67cSAndy Whitcroftsub possible { 209913214adfSAndy Whitcroft my ($possible, $line) = @_; 21009a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 21010776e594SAndy Whitcroft ^(?: 21020776e594SAndy Whitcroft $Modifier| 21030776e594SAndy Whitcroft $Storage| 21040776e594SAndy Whitcroft $Type| 21059a974fdbSAndy Whitcroft DEFINE_\S+ 21069a974fdbSAndy Whitcroft )$| 21079a974fdbSAndy Whitcroft ^(?: 21080776e594SAndy Whitcroft goto| 21090776e594SAndy Whitcroft return| 21100776e594SAndy Whitcroft case| 21110776e594SAndy Whitcroft else| 21120776e594SAndy Whitcroft asm|__asm__| 211389a88353SAndy Whitcroft do| 211489a88353SAndy Whitcroft \#| 211589a88353SAndy Whitcroft \#\#| 21169a974fdbSAndy Whitcroft )(?:\s|$)| 21170776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 21189a974fdbSAndy Whitcroft )}x; 21199a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 21209a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2121c45dcabdSAndy Whitcroft # Check for modifiers. 2122c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2123c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2124c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2125c45dcabdSAndy Whitcroft 2126c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2127c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2128d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 21299a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2130d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2131485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2132d2506586SAndy Whitcroft } 21339a974fdbSAndy Whitcroft } 2134c45dcabdSAndy Whitcroft 2135c45dcabdSAndy Whitcroft } else { 213613214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2137485ff23eSAlex Dowad push(@typeListFile, $possible); 2138c45dcabdSAndy Whitcroft } 21398905a67cSAndy Whitcroft build_types(); 21400776e594SAndy Whitcroft } else { 21410776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 21428905a67cSAndy Whitcroft } 21438905a67cSAndy Whitcroft} 21448905a67cSAndy Whitcroft 21456c72ffaaSAndy Whitcroftmy $prefix = ''; 21466c72ffaaSAndy Whitcroft 2147000d1cc1SJoe Perchessub show_type { 2148cbec18afSJoe Perches my ($type) = @_; 214991bfe484SJoe Perches 2150522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2151522b837cSAlexey Dobriyan 2152cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2153cbec18afSJoe Perches 2154cbec18afSJoe Perches return !defined $ignore_type{$type}; 2155000d1cc1SJoe Perches} 2156000d1cc1SJoe Perches 2157f0a594c1SAndy Whitcroftsub report { 2158cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2159cbec18afSJoe Perches 2160cbec18afSJoe Perches if (!show_type($type) || 2161cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2162773647a0SAndy Whitcroft return 0; 2163773647a0SAndy Whitcroft } 216457230297SJoe Perches my $output = ''; 2165737c0767SJohn Brooks if ($color) { 216657230297SJoe Perches if ($level eq 'ERROR') { 216757230297SJoe Perches $output .= RED; 216857230297SJoe Perches } elsif ($level eq 'WARNING') { 216957230297SJoe Perches $output .= YELLOW; 2170000d1cc1SJoe Perches } else { 217157230297SJoe Perches $output .= GREEN; 2172000d1cc1SJoe Perches } 217357230297SJoe Perches } 217457230297SJoe Perches $output .= $prefix . $level . ':'; 217557230297SJoe Perches if ($show_types) { 2176737c0767SJohn Brooks $output .= BLUE if ($color); 217757230297SJoe Perches $output .= "$type:"; 217857230297SJoe Perches } 2179737c0767SJohn Brooks $output .= RESET if ($color); 218057230297SJoe Perches $output .= ' ' . $msg . "\n"; 218134d8815fSJoe Perches 218234d8815fSJoe Perches if ($showfile) { 218334d8815fSJoe Perches my @lines = split("\n", $output, -1); 218434d8815fSJoe Perches splice(@lines, 1, 1); 218534d8815fSJoe Perches $output = join("\n", @lines); 218634d8815fSJoe Perches } 218757230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 21888905a67cSAndy Whitcroft 218957230297SJoe Perches push(our @report, $output); 2190773647a0SAndy Whitcroft 2191773647a0SAndy Whitcroft return 1; 2192f0a594c1SAndy Whitcroft} 2193cbec18afSJoe Perches 2194f0a594c1SAndy Whitcroftsub report_dump { 219513214adfSAndy Whitcroft our @report; 2196f0a594c1SAndy Whitcroft} 2197000d1cc1SJoe Perches 2198d752fcc8SJoe Perchessub fixup_current_range { 2199d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2200d752fcc8SJoe Perches 2201d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2202d752fcc8SJoe Perches my $o = $1; 2203d752fcc8SJoe Perches my $l = $2; 2204d752fcc8SJoe Perches my $no = $o + $offset; 2205d752fcc8SJoe Perches my $nl = $l + $length; 2206d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2207d752fcc8SJoe Perches } 2208d752fcc8SJoe Perches} 2209d752fcc8SJoe Perches 2210d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2211d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2212d752fcc8SJoe Perches 2213d752fcc8SJoe Perches my $range_last_linenr = 0; 2214d752fcc8SJoe Perches my $delta_offset = 0; 2215d752fcc8SJoe Perches 2216d752fcc8SJoe Perches my $old_linenr = 0; 2217d752fcc8SJoe Perches my $new_linenr = 0; 2218d752fcc8SJoe Perches 2219d752fcc8SJoe Perches my $next_insert = 0; 2220d752fcc8SJoe Perches my $next_delete = 0; 2221d752fcc8SJoe Perches 2222d752fcc8SJoe Perches my @lines = (); 2223d752fcc8SJoe Perches 2224d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2225d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2226d752fcc8SJoe Perches 2227d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2228d752fcc8SJoe Perches my $save_line = 1; 2229d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2230323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2231d752fcc8SJoe Perches $delta_offset = 0; 2232d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2233d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2234d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2235d752fcc8SJoe Perches } 2236d752fcc8SJoe Perches 2237d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2238d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2239d752fcc8SJoe Perches $save_line = 0; 2240d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2241d752fcc8SJoe Perches } 2242d752fcc8SJoe Perches 2243d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2244d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2245d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2246d752fcc8SJoe Perches $new_linenr++; 2247d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2248d752fcc8SJoe Perches } 2249d752fcc8SJoe Perches 2250d752fcc8SJoe Perches if ($save_line) { 2251d752fcc8SJoe Perches push(@lines, $line); 2252d752fcc8SJoe Perches $new_linenr++; 2253d752fcc8SJoe Perches } 2254d752fcc8SJoe Perches 2255d752fcc8SJoe Perches $old_linenr++; 2256d752fcc8SJoe Perches } 2257d752fcc8SJoe Perches 2258d752fcc8SJoe Perches return @lines; 2259d752fcc8SJoe Perches} 2260d752fcc8SJoe Perches 2261f2d7e4d4SJoe Perchessub fix_insert_line { 2262f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2263f2d7e4d4SJoe Perches 2264f2d7e4d4SJoe Perches my $inserted = { 2265f2d7e4d4SJoe Perches LINENR => $linenr, 2266f2d7e4d4SJoe Perches LINE => $line, 2267f2d7e4d4SJoe Perches }; 2268f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2269f2d7e4d4SJoe Perches} 2270f2d7e4d4SJoe Perches 2271f2d7e4d4SJoe Perchessub fix_delete_line { 2272f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2273f2d7e4d4SJoe Perches 2274f2d7e4d4SJoe Perches my $deleted = { 2275f2d7e4d4SJoe Perches LINENR => $linenr, 2276f2d7e4d4SJoe Perches LINE => $line, 2277f2d7e4d4SJoe Perches }; 2278f2d7e4d4SJoe Perches 2279f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2280f2d7e4d4SJoe Perches} 2281f2d7e4d4SJoe Perches 2282de7d4f0eSAndy Whitcroftsub ERROR { 2283cbec18afSJoe Perches my ($type, $msg) = @_; 2284cbec18afSJoe Perches 2285cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2286de7d4f0eSAndy Whitcroft our $clean = 0; 22876c72ffaaSAndy Whitcroft our $cnt_error++; 22883705ce5bSJoe Perches return 1; 2289de7d4f0eSAndy Whitcroft } 22903705ce5bSJoe Perches return 0; 2291773647a0SAndy Whitcroft} 2292de7d4f0eSAndy Whitcroftsub WARN { 2293cbec18afSJoe Perches my ($type, $msg) = @_; 2294cbec18afSJoe Perches 2295cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2296de7d4f0eSAndy Whitcroft our $clean = 0; 22976c72ffaaSAndy Whitcroft our $cnt_warn++; 22983705ce5bSJoe Perches return 1; 2299de7d4f0eSAndy Whitcroft } 23003705ce5bSJoe Perches return 0; 2301773647a0SAndy Whitcroft} 2302de7d4f0eSAndy Whitcroftsub CHK { 2303cbec18afSJoe Perches my ($type, $msg) = @_; 2304cbec18afSJoe Perches 2305cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2306de7d4f0eSAndy Whitcroft our $clean = 0; 23076c72ffaaSAndy Whitcroft our $cnt_chk++; 23083705ce5bSJoe Perches return 1; 23096c72ffaaSAndy Whitcroft } 23103705ce5bSJoe Perches return 0; 2311de7d4f0eSAndy Whitcroft} 2312de7d4f0eSAndy Whitcroft 23136ecd9674SAndy Whitcroftsub check_absolute_file { 23146ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 23156ecd9674SAndy Whitcroft my $file = $absolute; 23166ecd9674SAndy Whitcroft 23176ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 23186ecd9674SAndy Whitcroft 23196ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 23206ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 23216ecd9674SAndy Whitcroft if (-f "$root/$file") { 23226ecd9674SAndy Whitcroft ##print "file<$file>\n"; 23236ecd9674SAndy Whitcroft last; 23246ecd9674SAndy Whitcroft } 23256ecd9674SAndy Whitcroft } 23266ecd9674SAndy Whitcroft if (! -f _) { 23276ecd9674SAndy Whitcroft return 0; 23286ecd9674SAndy Whitcroft } 23296ecd9674SAndy Whitcroft 23306ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 23316ecd9674SAndy Whitcroft my $prefix = $absolute; 23326ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 23336ecd9674SAndy Whitcroft 23346ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 23356ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2336000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2337000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 23386ecd9674SAndy Whitcroft } 23396ecd9674SAndy Whitcroft} 23406ecd9674SAndy Whitcroft 23413705ce5bSJoe Perchessub trim { 23423705ce5bSJoe Perches my ($string) = @_; 23433705ce5bSJoe Perches 2344b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2345b34c648bSJoe Perches 2346b34c648bSJoe Perches return $string; 2347b34c648bSJoe Perches} 2348b34c648bSJoe Perches 2349b34c648bSJoe Perchessub ltrim { 2350b34c648bSJoe Perches my ($string) = @_; 2351b34c648bSJoe Perches 2352b34c648bSJoe Perches $string =~ s/^\s+//; 2353b34c648bSJoe Perches 2354b34c648bSJoe Perches return $string; 2355b34c648bSJoe Perches} 2356b34c648bSJoe Perches 2357b34c648bSJoe Perchessub rtrim { 2358b34c648bSJoe Perches my ($string) = @_; 2359b34c648bSJoe Perches 2360b34c648bSJoe Perches $string =~ s/\s+$//; 23613705ce5bSJoe Perches 23623705ce5bSJoe Perches return $string; 23633705ce5bSJoe Perches} 23643705ce5bSJoe Perches 236552ea8506SJoe Perchessub string_find_replace { 236652ea8506SJoe Perches my ($string, $find, $replace) = @_; 236752ea8506SJoe Perches 236852ea8506SJoe Perches $string =~ s/$find/$replace/g; 236952ea8506SJoe Perches 237052ea8506SJoe Perches return $string; 237152ea8506SJoe Perches} 237252ea8506SJoe Perches 23733705ce5bSJoe Perchessub tabify { 23743705ce5bSJoe Perches my ($leading) = @_; 23753705ce5bSJoe Perches 2376713a09deSAntonio Borneo my $source_indent = $tabsize; 23773705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 23783705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 23793705ce5bSJoe Perches 23803705ce5bSJoe Perches #convert leading spaces to tabs 23813705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 23823705ce5bSJoe Perches #Remove spaces before a tab 23833705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 23843705ce5bSJoe Perches 23853705ce5bSJoe Perches return "$leading"; 23863705ce5bSJoe Perches} 23873705ce5bSJoe Perches 2388d1fe9c09SJoe Perchessub pos_last_openparen { 2389d1fe9c09SJoe Perches my ($line) = @_; 2390d1fe9c09SJoe Perches 2391d1fe9c09SJoe Perches my $pos = 0; 2392d1fe9c09SJoe Perches 2393d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2394d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2395d1fe9c09SJoe Perches 2396d1fe9c09SJoe Perches my $last_openparen = 0; 2397d1fe9c09SJoe Perches 2398d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2399d1fe9c09SJoe Perches return -1; 2400d1fe9c09SJoe Perches } 2401d1fe9c09SJoe Perches 2402d1fe9c09SJoe Perches my $len = length($line); 2403d1fe9c09SJoe Perches 2404d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2405d1fe9c09SJoe Perches my $string = substr($line, $pos); 2406d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2407d1fe9c09SJoe Perches $pos += length($1) - 1; 2408d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2409d1fe9c09SJoe Perches $last_openparen = $pos; 2410d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2411d1fe9c09SJoe Perches last; 2412d1fe9c09SJoe Perches } 2413d1fe9c09SJoe Perches } 2414d1fe9c09SJoe Perches 241591cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2416d1fe9c09SJoe Perches} 2417d1fe9c09SJoe Perches 2418f36d3eb8SJoe Perchessub get_raw_comment { 2419f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2420f36d3eb8SJoe Perches my $comment = ''; 2421f36d3eb8SJoe Perches 2422f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2423f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2424f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2425f36d3eb8SJoe Perches } 2426f36d3eb8SJoe Perches } 2427f36d3eb8SJoe Perches 2428f36d3eb8SJoe Perches return $comment; 2429f36d3eb8SJoe Perches} 2430f36d3eb8SJoe Perches 24310a920b5bSAndy Whitcroftsub process { 24320a920b5bSAndy Whitcroft my $filename = shift; 24330a920b5bSAndy Whitcroft 24340a920b5bSAndy Whitcroft my $linenr=0; 24350a920b5bSAndy Whitcroft my $prevline=""; 2436c2fdda0dSAndy Whitcroft my $prevrawline=""; 24370a920b5bSAndy Whitcroft my $stashline=""; 2438c2fdda0dSAndy Whitcroft my $stashrawline=""; 24390a920b5bSAndy Whitcroft 24404a0df2efSAndy Whitcroft my $length; 24410a920b5bSAndy Whitcroft my $indent; 24420a920b5bSAndy Whitcroft my $previndent=0; 24430a920b5bSAndy Whitcroft my $stashindent=0; 24440a920b5bSAndy Whitcroft 2445de7d4f0eSAndy Whitcroft our $clean = 1; 24460a920b5bSAndy Whitcroft my $signoff = 0; 2447cd261496SGeert Uytterhoeven my $author = ''; 2448cd261496SGeert Uytterhoeven my $authorsignoff = 0; 244948ca2d8aSDwaipayan Ray my $author_sob = ''; 24500a920b5bSAndy Whitcroft my $is_patch = 0; 2451133712a2SRob Herring my $is_binding_patch = -1; 245229ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 245315662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 245444d303ebSJoe Perches my $has_patch_separator = 0; #Found a --- line 2455ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2456490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2457bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 24582a076f40SJoe Perches my $commit_log_long_line = 0; 2459e518e9a5SJoe Perches my $commit_log_has_diff = 0; 246013f1937eSJoe Perches my $reported_maintainer_file = 0; 2461fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2462fa64205dSPasi Savanainen 2463365dd4eaSJoe Perches my $last_blank_line = 0; 24645e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2465365dd4eaSJoe Perches 246613214adfSAndy Whitcroft our @report = (); 24676c72ffaaSAndy Whitcroft our $cnt_lines = 0; 24686c72ffaaSAndy Whitcroft our $cnt_error = 0; 24696c72ffaaSAndy Whitcroft our $cnt_warn = 0; 24706c72ffaaSAndy Whitcroft our $cnt_chk = 0; 24716c72ffaaSAndy Whitcroft 24720a920b5bSAndy Whitcroft # Trace the real file/line as we go. 24730a920b5bSAndy Whitcroft my $realfile = ''; 24740a920b5bSAndy Whitcroft my $realline = 0; 24750a920b5bSAndy Whitcroft my $realcnt = 0; 24760a920b5bSAndy Whitcroft my $here = ''; 247777cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 24780a920b5bSAndy Whitcroft my $in_comment = 0; 2479c2fdda0dSAndy Whitcroft my $comment_edge = 0; 24800a920b5bSAndy Whitcroft my $first_line = 0; 24811e855726SWolfram Sang my $p1_prefix = ''; 24820a920b5bSAndy Whitcroft 248313214adfSAndy Whitcroft my $prev_values = 'E'; 248413214adfSAndy Whitcroft 248513214adfSAndy Whitcroft # suppression flags 2486773647a0SAndy Whitcroft my %suppress_ifbraces; 2487170d3a22SAndy Whitcroft my %suppress_whiletrailers; 24882b474a1aSAndy Whitcroft my %suppress_export; 24893e469cdcSAndy Whitcroft my $suppress_statement = 0; 2490653d4876SAndy Whitcroft 24917e51f197SJoe Perches my %signatures = (); 2492323c1260SJoe Perches 2493c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2494de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2495c2fdda0dSAndy Whitcroft # 2496de7d4f0eSAndy Whitcroft my @setup_docs = (); 2497de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2498773647a0SAndy Whitcroft 2499d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2500d8b07710SJoe Perches 25019f3a8992SRob Herring my $checklicenseline = 1; 25029f3a8992SRob Herring 2503773647a0SAndy Whitcroft sanitise_line_reset(); 2504c2fdda0dSAndy Whitcroft my $line; 2505c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2506773647a0SAndy Whitcroft $linenr++; 2507773647a0SAndy Whitcroft $line = $rawline; 2508c2fdda0dSAndy Whitcroft 25093705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 25103705ce5bSJoe Perches 2511773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2512de7d4f0eSAndy Whitcroft $setup_docs = 0; 25132581ac7cSTim Froidcoeur if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { 2514de7d4f0eSAndy Whitcroft $setup_docs = 1; 2515de7d4f0eSAndy Whitcroft } 2516773647a0SAndy Whitcroft #next; 2517de7d4f0eSAndy Whitcroft } 251874fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2519773647a0SAndy Whitcroft $realline=$1-1; 2520773647a0SAndy Whitcroft if (defined $2) { 2521773647a0SAndy Whitcroft $realcnt=$3+1; 2522773647a0SAndy Whitcroft } else { 2523773647a0SAndy Whitcroft $realcnt=1+1; 2524773647a0SAndy Whitcroft } 2525c45dcabdSAndy Whitcroft $in_comment = 0; 2526773647a0SAndy Whitcroft 2527773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2528773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2529773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2530773647a0SAndy Whitcroft # at context start. 2531773647a0SAndy Whitcroft my $edge; 253201fa9147SAndy Whitcroft my $cnt = $realcnt; 253301fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 253401fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 253501fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 253601fa9147SAndy Whitcroft $cnt--; 253701fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2538721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2539fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2540fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2541fae17daeSAndy Whitcroft ($edge) = $1; 2542fae17daeSAndy Whitcroft last; 2543fae17daeSAndy Whitcroft } 2544773647a0SAndy Whitcroft } 2545773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2546773647a0SAndy Whitcroft $in_comment = 1; 2547773647a0SAndy Whitcroft } 2548773647a0SAndy Whitcroft 2549773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2550773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2551773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2552773647a0SAndy Whitcroft if (!defined $edge && 255383242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2554773647a0SAndy Whitcroft { 2555773647a0SAndy Whitcroft $in_comment = 1; 2556773647a0SAndy Whitcroft } 2557773647a0SAndy Whitcroft 2558773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2559773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2560773647a0SAndy Whitcroft 2561171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2562773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2563171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2564773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2565773647a0SAndy Whitcroft } 2566773647a0SAndy Whitcroft push(@lines, $line); 2567773647a0SAndy Whitcroft 2568773647a0SAndy Whitcroft if ($realcnt > 1) { 2569773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2570773647a0SAndy Whitcroft } else { 2571773647a0SAndy Whitcroft $realcnt = 0; 2572773647a0SAndy Whitcroft } 2573773647a0SAndy Whitcroft 2574773647a0SAndy Whitcroft #print "==>$rawline\n"; 2575773647a0SAndy Whitcroft #print "-->$line\n"; 2576de7d4f0eSAndy Whitcroft 2577de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2578de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2579de7d4f0eSAndy Whitcroft } 2580de7d4f0eSAndy Whitcroft } 2581de7d4f0eSAndy Whitcroft 25826c72ffaaSAndy Whitcroft $prefix = ''; 25836c72ffaaSAndy Whitcroft 2584773647a0SAndy Whitcroft $realcnt = 0; 2585773647a0SAndy Whitcroft $linenr = 0; 2586194f66fcSJoe Perches $fixlinenr = -1; 25870a920b5bSAndy Whitcroft foreach my $line (@lines) { 25880a920b5bSAndy Whitcroft $linenr++; 2589194f66fcSJoe Perches $fixlinenr++; 25901b5539b1SJoe Perches my $sline = $line; #copy of $line 25911b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 25920a920b5bSAndy Whitcroft 2593c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2594f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 25956c72ffaaSAndy Whitcroft 259612c253abSJoe Perches# check if it's a mode change, rename or start of a patch 259712c253abSJoe Perches if (!$in_commit_log && 259812c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 259912c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 260012c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 260112c253abSJoe Perches $is_patch = 1; 260212c253abSJoe Perches } 260312c253abSJoe Perches 26040a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2605e518e9a5SJoe Perches if (!$in_commit_log && 260674fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 260774fd4f34SJoe Perches my $context = $4; 26080a920b5bSAndy Whitcroft $is_patch = 1; 26094a0df2efSAndy Whitcroft $first_line = $linenr + 1; 26100a920b5bSAndy Whitcroft $realline=$1-1; 26110a920b5bSAndy Whitcroft if (defined $2) { 26120a920b5bSAndy Whitcroft $realcnt=$3+1; 26130a920b5bSAndy Whitcroft } else { 26140a920b5bSAndy Whitcroft $realcnt=1+1; 26150a920b5bSAndy Whitcroft } 2616c2fdda0dSAndy Whitcroft annotate_reset(); 261713214adfSAndy Whitcroft $prev_values = 'E'; 261813214adfSAndy Whitcroft 2619773647a0SAndy Whitcroft %suppress_ifbraces = (); 2620170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 26212b474a1aSAndy Whitcroft %suppress_export = (); 26223e469cdcSAndy Whitcroft $suppress_statement = 0; 262374fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 262474fd4f34SJoe Perches $context_function = $1; 262574fd4f34SJoe Perches } else { 262674fd4f34SJoe Perches undef $context_function; 262774fd4f34SJoe Perches } 26280a920b5bSAndy Whitcroft next; 26290a920b5bSAndy Whitcroft 26304a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 26314a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 26324a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2633773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 26340a920b5bSAndy Whitcroft $realline++; 2635d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 26360a920b5bSAndy Whitcroft 26374a0df2efSAndy Whitcroft # Measure the line length and indent. 2638c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 26390a920b5bSAndy Whitcroft 26400a920b5bSAndy Whitcroft # Track the previous line. 26410a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 26420a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2643c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2644c2fdda0dSAndy Whitcroft 2645773647a0SAndy Whitcroft #warn "line<$line>\n"; 26466c72ffaaSAndy Whitcroft 2647d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2648d8aaf121SAndy Whitcroft $realcnt--; 26490a920b5bSAndy Whitcroft } 26500a920b5bSAndy Whitcroft 2651cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2652cc77cdcaSAndy Whitcroft 26536c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 26546c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2655773647a0SAndy Whitcroft 26562ac73b4fSJoe Perches my $found_file = 0; 2657773647a0SAndy Whitcroft # extract the filename as it passes 26583bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 26593bf9a009SRabin Vincent $realfile = $1; 26602b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2661270c49a0SJoe Perches $in_commit_log = 0; 26622ac73b4fSJoe Perches $found_file = 1; 26633bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2664773647a0SAndy Whitcroft $realfile = $1; 26652b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2666270c49a0SJoe Perches $in_commit_log = 0; 26671e855726SWolfram Sang 26681e855726SWolfram Sang $p1_prefix = $1; 2669e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2670e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2671000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2672000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 26731e855726SWolfram Sang } 2674773647a0SAndy Whitcroft 2675c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2676000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2677000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2678773647a0SAndy Whitcroft } 26792ac73b4fSJoe Perches $found_file = 1; 26802ac73b4fSJoe Perches } 26812ac73b4fSJoe Perches 268234d8815fSJoe Perches#make up the handle for any error we report on this line 268334d8815fSJoe Perches if ($showfile) { 268434d8815fSJoe Perches $prefix = "$realfile:$realline: " 268534d8815fSJoe Perches } elsif ($emacs) { 26867d3a9f67SJoe Perches if ($file) { 26877d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 26887d3a9f67SJoe Perches } else { 268934d8815fSJoe Perches $prefix = "$filename:$linenr: "; 269034d8815fSJoe Perches } 26917d3a9f67SJoe Perches } 269234d8815fSJoe Perches 26932ac73b4fSJoe Perches if ($found_file) { 269485b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 269585b0ee18SJoe Perches WARN("OBSOLETE", 269685b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 269785b0ee18SJoe Perches } 26987bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 26992ac73b4fSJoe Perches $check = 1; 27002ac73b4fSJoe Perches } else { 27012ac73b4fSJoe Perches $check = $check_orig; 27022ac73b4fSJoe Perches } 27039f3a8992SRob Herring $checklicenseline = 1; 2704133712a2SRob Herring 2705133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2706133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2707133712a2SRob Herring 2708133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2709133712a2SRob Herring 2710133712a2SRob Herring if (($last_binding_patch != -1) && 2711133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2712133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2713858e6845SMauro Carvalho Chehab "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); 2714133712a2SRob Herring } 2715133712a2SRob Herring } 2716133712a2SRob Herring 2717773647a0SAndy Whitcroft next; 2718773647a0SAndy Whitcroft } 2719773647a0SAndy Whitcroft 2720389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 27210a920b5bSAndy Whitcroft 2722c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2723c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2724c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 27250a920b5bSAndy Whitcroft 27266c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 27276c72ffaaSAndy Whitcroft 2728490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2729490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2730490b292cSJoe Perches if ($in_commit_log) { 2731490b292cSJoe Perches if ($line !~ /^\s*$/) { 2732490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2733490b292cSJoe Perches } 2734490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2735490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2736490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2737490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2738490b292cSJoe Perches } 2739490b292cSJoe Perches 2740e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2741e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 274213e45417SMrinal Pandey (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && 274313e45417SMrinal Pandey $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || 2744e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2745e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2746e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2747e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2748e518e9a5SJoe Perches $commit_log_has_diff = 1; 2749e518e9a5SJoe Perches } 2750e518e9a5SJoe Perches 27513bf9a009SRabin Vincent# Check for incorrect file permissions 27523bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 27533bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 275404db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 275504db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2756000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2757000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 27583bf9a009SRabin Vincent } 27593bf9a009SRabin Vincent } 27603bf9a009SRabin Vincent 2761cd261496SGeert Uytterhoeven# Check the patch for a From: 2762cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2763cd261496SGeert Uytterhoeven $author = $1; 2764e7f929f3SDwaipayan Ray my $curline = $linenr; 2765e7f929f3SDwaipayan Ray while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { 2766e7f929f3SDwaipayan Ray $author .= $1; 2767e7f929f3SDwaipayan Ray } 2768cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2769cd261496SGeert Uytterhoeven $author =~ s/"//g; 2770dfa05c28SJoe Perches $author = reformat_email($author); 2771cd261496SGeert Uytterhoeven } 2772cd261496SGeert Uytterhoeven 277320112475SJoe Perches# Check the patch for a signoff: 2774dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 27754a0df2efSAndy Whitcroft $signoff++; 277615662b3eSJoe Perches $in_commit_log = 0; 277748ca2d8aSDwaipayan Ray if ($author ne '' && $authorsignoff != 1) { 2778fccaebf0SDwaipayan Ray if (same_email_addresses($1, $author)) { 2779cd261496SGeert Uytterhoeven $authorsignoff = 1; 278048ca2d8aSDwaipayan Ray } else { 278148ca2d8aSDwaipayan Ray my $ctx = $1; 278248ca2d8aSDwaipayan Ray my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); 278348ca2d8aSDwaipayan Ray my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); 278448ca2d8aSDwaipayan Ray 278548ca2d8aSDwaipayan Ray if ($email_address eq $author_address && $email_name eq $author_name) { 278648ca2d8aSDwaipayan Ray $author_sob = $ctx; 278748ca2d8aSDwaipayan Ray $authorsignoff = 2; 278848ca2d8aSDwaipayan Ray } elsif ($email_address eq $author_address) { 278948ca2d8aSDwaipayan Ray $author_sob = $ctx; 279048ca2d8aSDwaipayan Ray $authorsignoff = 3; 279148ca2d8aSDwaipayan Ray } elsif ($email_name eq $author_name) { 279248ca2d8aSDwaipayan Ray $author_sob = $ctx; 279348ca2d8aSDwaipayan Ray $authorsignoff = 4; 279448ca2d8aSDwaipayan Ray 279548ca2d8aSDwaipayan Ray my $address1 = $email_address; 279648ca2d8aSDwaipayan Ray my $address2 = $author_address; 279748ca2d8aSDwaipayan Ray 279848ca2d8aSDwaipayan Ray if ($address1 =~ /(\S+)\+\S+(\@.*)/) { 279948ca2d8aSDwaipayan Ray $address1 = "$1$2"; 280048ca2d8aSDwaipayan Ray } 280148ca2d8aSDwaipayan Ray if ($address2 =~ /(\S+)\+\S+(\@.*)/) { 280248ca2d8aSDwaipayan Ray $address2 = "$1$2"; 280348ca2d8aSDwaipayan Ray } 280448ca2d8aSDwaipayan Ray if ($address1 eq $address2) { 280548ca2d8aSDwaipayan Ray $authorsignoff = 5; 280648ca2d8aSDwaipayan Ray } 280748ca2d8aSDwaipayan Ray } 2808cd261496SGeert Uytterhoeven } 2809cd261496SGeert Uytterhoeven } 28100a920b5bSAndy Whitcroft } 281120112475SJoe Perches 281244d303ebSJoe Perches# Check for patch separator 281344d303ebSJoe Perches if ($line =~ /^---$/) { 281444d303ebSJoe Perches $has_patch_separator = 1; 281544d303ebSJoe Perches $in_commit_log = 0; 281644d303ebSJoe Perches } 281744d303ebSJoe Perches 2818e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2819e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2820e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2821e0d975b1SJoe Perches $reported_maintainer_file = 1; 2822e0d975b1SJoe Perches } 2823e0d975b1SJoe Perches 282420112475SJoe Perches# Check signature styles 2825270c49a0SJoe Perches if (!$in_header_lines && 2826ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 282720112475SJoe Perches my $space_before = $1; 282820112475SJoe Perches my $sign_off = $2; 282920112475SJoe Perches my $space_after = $3; 283020112475SJoe Perches my $email = $4; 283120112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 283220112475SJoe Perches 2833ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2834831242abSAditya Srivastava my $suggested_signature = find_standard_signature($sign_off); 2835831242abSAditya Srivastava if ($suggested_signature eq "") { 2836ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2837ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2838831242abSAditya Srivastava } else { 2839831242abSAditya Srivastava if (WARN("BAD_SIGN_OFF", 2840831242abSAditya Srivastava "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) && 2841831242abSAditya Srivastava $fix) { 2842831242abSAditya Srivastava $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/; 2843831242abSAditya Srivastava } 2844831242abSAditya Srivastava } 2845ce0338dfSJoe Perches } 284620112475SJoe Perches if (defined $space_before && $space_before ne "") { 28473705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 28483705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 28493705ce5bSJoe Perches $fix) { 2850194f66fcSJoe Perches $fixed[$fixlinenr] = 28513705ce5bSJoe Perches "$ucfirst_sign_off $email"; 28523705ce5bSJoe Perches } 285320112475SJoe Perches } 285420112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 28553705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 28563705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 28573705ce5bSJoe Perches $fix) { 2858194f66fcSJoe Perches $fixed[$fixlinenr] = 28593705ce5bSJoe Perches "$ucfirst_sign_off $email"; 28603705ce5bSJoe Perches } 28613705ce5bSJoe Perches 286220112475SJoe Perches } 286320112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 28643705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 28653705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 28663705ce5bSJoe Perches $fix) { 2867194f66fcSJoe Perches $fixed[$fixlinenr] = 28683705ce5bSJoe Perches "$ucfirst_sign_off $email"; 28693705ce5bSJoe Perches } 287020112475SJoe Perches } 287120112475SJoe Perches 2872dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 287348ca2d8aSDwaipayan Ray my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); 287420112475SJoe Perches if ($suggested_email eq "") { 2875000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2876000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 287720112475SJoe Perches } else { 287820112475SJoe Perches my $dequoted = $suggested_email; 287920112475SJoe Perches $dequoted =~ s/^"//; 288020112475SJoe Perches $dequoted =~ s/" </ </; 288120112475SJoe Perches # Don't force email to have quotes 288220112475SJoe Perches # Allow just an angle bracketed address 2883fccaebf0SDwaipayan Ray if (!same_email_addresses($email, $suggested_email)) { 2884fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 2885fccaebf0SDwaipayan Ray "email address '$email' might be better as '$suggested_email'\n" . $herecurr) && 2886fccaebf0SDwaipayan Ray $fix) { 2887fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/; 2888fccaebf0SDwaipayan Ray } 2889fccaebf0SDwaipayan Ray } 2890fccaebf0SDwaipayan Ray 2891fccaebf0SDwaipayan Ray # Address part shouldn't have comments 2892fccaebf0SDwaipayan Ray my $stripped_address = $email_address; 2893fccaebf0SDwaipayan Ray $stripped_address =~ s/\([^\(\)]*\)//g; 2894fccaebf0SDwaipayan Ray if ($email_address ne $stripped_address) { 2895fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 2896fccaebf0SDwaipayan Ray "address part of email should not have comments: '$email_address'\n" . $herecurr) && 2897fccaebf0SDwaipayan Ray $fix) { 2898fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/; 2899fccaebf0SDwaipayan Ray } 2900fccaebf0SDwaipayan Ray } 2901fccaebf0SDwaipayan Ray 2902fccaebf0SDwaipayan Ray # Only one name comment should be allowed 2903fccaebf0SDwaipayan Ray my $comment_count = () = $name_comment =~ /\([^\)]+\)/g; 2904fccaebf0SDwaipayan Ray if ($comment_count > 1) { 2905000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2906fccaebf0SDwaipayan Ray "Use a single name comment in email: '$email'\n" . $herecurr); 2907fccaebf0SDwaipayan Ray } 2908fccaebf0SDwaipayan Ray 2909fccaebf0SDwaipayan Ray 2910fccaebf0SDwaipayan Ray # [email protected] or [email protected] shouldn't 2911e73d2715SDwaipayan Ray # have an email name. In addition comments should strictly 2912fccaebf0SDwaipayan Ray # begin with a # 2913fccaebf0SDwaipayan Ray if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) { 2914fccaebf0SDwaipayan Ray if (($comment ne "" && $comment !~ /^#.+/) || 2915fccaebf0SDwaipayan Ray ($email_name ne "")) { 2916fccaebf0SDwaipayan Ray my $cur_name = $email_name; 2917fccaebf0SDwaipayan Ray my $new_comment = $comment; 2918fccaebf0SDwaipayan Ray $cur_name =~ s/[a-zA-Z\s\-\"]+//g; 2919fccaebf0SDwaipayan Ray 2920fccaebf0SDwaipayan Ray # Remove brackets enclosing comment text 2921fccaebf0SDwaipayan Ray # and # from start of comments to get comment text 2922fccaebf0SDwaipayan Ray $new_comment =~ s/^\((.*)\)$/$1/; 2923fccaebf0SDwaipayan Ray $new_comment =~ s/^\[(.*)\]$/$1/; 2924fccaebf0SDwaipayan Ray $new_comment =~ s/^[\s\#]+|\s+$//g; 2925fccaebf0SDwaipayan Ray 2926fccaebf0SDwaipayan Ray $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment); 2927fccaebf0SDwaipayan Ray $new_comment = " # $new_comment" if ($new_comment ne ""); 2928fccaebf0SDwaipayan Ray my $new_email = "$email_address$new_comment"; 2929fccaebf0SDwaipayan Ray 2930fccaebf0SDwaipayan Ray if (WARN("BAD_STABLE_ADDRESS_STYLE", 2931fccaebf0SDwaipayan Ray "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) && 2932fccaebf0SDwaipayan Ray $fix) { 2933fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 2934fccaebf0SDwaipayan Ray } 2935fccaebf0SDwaipayan Ray } 2936fccaebf0SDwaipayan Ray } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) { 2937fccaebf0SDwaipayan Ray my $new_comment = $comment; 2938fccaebf0SDwaipayan Ray 2939fccaebf0SDwaipayan Ray # Extract comment text from within brackets or 2940fccaebf0SDwaipayan Ray # c89 style /*...*/ comments 2941fccaebf0SDwaipayan Ray $new_comment =~ s/^\[(.*)\]$/$1/; 2942fccaebf0SDwaipayan Ray $new_comment =~ s/^\/\*(.*)\*\/$/$1/; 2943fccaebf0SDwaipayan Ray 2944fccaebf0SDwaipayan Ray $new_comment = trim($new_comment); 2945fccaebf0SDwaipayan Ray $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo 2946fccaebf0SDwaipayan Ray $new_comment = "($new_comment)" if ($new_comment ne ""); 2947fccaebf0SDwaipayan Ray my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment); 2948fccaebf0SDwaipayan Ray 2949fccaebf0SDwaipayan Ray if (WARN("BAD_SIGN_OFF", 2950fccaebf0SDwaipayan Ray "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) && 2951fccaebf0SDwaipayan Ray $fix) { 2952fccaebf0SDwaipayan Ray $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; 2953fccaebf0SDwaipayan Ray } 295420112475SJoe Perches } 29550a920b5bSAndy Whitcroft } 29567e51f197SJoe Perches 29577e51f197SJoe Perches# Check for duplicate signatures 29587e51f197SJoe Perches my $sig_nospace = $line; 29597e51f197SJoe Perches $sig_nospace =~ s/\s//g; 29607e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 29617e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 29627e51f197SJoe Perches WARN("BAD_SIGN_OFF", 29637e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 29647e51f197SJoe Perches } else { 29657e51f197SJoe Perches $signatures{$sig_nospace} = 1; 29667e51f197SJoe Perches } 29676c5d24eeSSean Christopherson 29686c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 29696c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 29706c5d24eeSSean Christopherson if ($email eq $author) { 29716c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29726c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 29736c5d24eeSSean Christopherson } 29746c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 29756c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29766c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 29776c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 29786c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29796c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 29806c5d24eeSSean Christopherson } elsif ($1 ne $email) { 29816c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 29826c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 29836c5d24eeSSean Christopherson } 29846c5d24eeSSean Christopherson } 29850a920b5bSAndy Whitcroft } 29860a920b5bSAndy Whitcroft 2987a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2988a2fe16b9SJoe Perches if ($in_header_lines && 2989a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2990a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2991a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2992a2fe16b9SJoe Perches } 2993a2fe16b9SJoe Perches 299444d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context 299544d303ebSJoe Perches if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 29967580c5b9SAditya Srivastava if (ERROR("GERRIT_CHANGE_ID", 29977580c5b9SAditya Srivastava "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) && 29987580c5b9SAditya Srivastava $fix) { 29997580c5b9SAditya Srivastava fix_delete_line($fixlinenr, $rawline); 30007580c5b9SAditya Srivastava } 30017ebd05efSChristopher Covington } 30027ebd05efSChristopher Covington 3003369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 3004369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 3005369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 3006369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 3007369c8dd3SJoe Perches # timestamp 3008634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 3009634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 3010634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 3011634cffccSJoe Perches # stack dump address styles 3012369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 3013369c8dd3SJoe Perches } 3014369c8dd3SJoe Perches 30152a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 30162a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 3017bf4daf12SJoe Perches length($line) > 75 && 3018bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 3019bf4daf12SJoe Perches # file delta changes 3020bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 3021bf4daf12SJoe Perches # filename then : 302227b379afSAditya Srivastava $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i || 302327b379afSAditya Srivastava # A Fixes: or Link: line or signature tag line 3024bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 30252a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 30262a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 30272a076f40SJoe Perches $commit_log_long_line = 1; 30282a076f40SJoe Perches } 30292a076f40SJoe Perches 3030bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 3031bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 3032bf4daf12SJoe Perches $line =~ /^\s*$/) { 3033bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 3034bf4daf12SJoe Perches } 3035bf4daf12SJoe Perches 3036*084a617aSDwaipayan Ray# Check for lines starting with a # 3037*084a617aSDwaipayan Ray if ($in_commit_log && $line =~ /^#/) { 3038*084a617aSDwaipayan Ray if (WARN("COMMIT_COMMENT_SYMBOL", 3039*084a617aSDwaipayan Ray "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) && 3040*084a617aSDwaipayan Ray $fix) { 3041*084a617aSDwaipayan Ray $fixed[$fixlinenr] =~ s/^/ /; 3042*084a617aSDwaipayan Ray } 3043*084a617aSDwaipayan Ray } 3044*084a617aSDwaipayan Ray 30450d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 3046369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 3047a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 3048e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 3049fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 3050aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 3051369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 3052bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 3053fe043ea1SJoe Perches my $init_char = "c"; 3054fe043ea1SJoe Perches my $orig_commit = ""; 30550d7835fcSJoe Perches my $short = 1; 30560d7835fcSJoe Perches my $long = 0; 30570d7835fcSJoe Perches my $case = 1; 30580d7835fcSJoe Perches my $space = 1; 30590d7835fcSJoe Perches my $hasdesc = 0; 306019c146a6SJoe Perches my $hasparens = 0; 30610d7835fcSJoe Perches my $id = '0123456789ab'; 30620d7835fcSJoe Perches my $orig_desc = "commit description"; 30630d7835fcSJoe Perches my $description = ""; 30640d7835fcSJoe Perches 3065fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 3066fe043ea1SJoe Perches $init_char = $1; 3067fe043ea1SJoe Perches $orig_commit = lc($2); 3068fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 3069fe043ea1SJoe Perches $orig_commit = lc($1); 3070fe043ea1SJoe Perches } 3071fe043ea1SJoe Perches 30720d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 30730d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 30740d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 30750d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 30760d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 30770d7835fcSJoe Perches $orig_desc = $1; 307819c146a6SJoe Perches $hasparens = 1; 30790d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 30800d7835fcSJoe Perches defined $rawlines[$linenr] && 30810d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 30820d7835fcSJoe Perches $orig_desc = $1; 308319c146a6SJoe Perches $hasparens = 1; 3084b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 3085b671fde0SJoe Perches defined $rawlines[$linenr] && 3086b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 3087b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 3088b671fde0SJoe Perches $orig_desc = $1; 3089b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 3090b671fde0SJoe Perches $orig_desc .= " " . $1; 309119c146a6SJoe Perches $hasparens = 1; 30920d7835fcSJoe Perches } 30930d7835fcSJoe Perches 30940d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 30950d7835fcSJoe Perches $id, $orig_desc); 30960d7835fcSJoe Perches 3097948b133aSHeinrich Schuchardt if (defined($id) && 3098948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 3099d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 31000d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 31010d7835fcSJoe Perches } 3102d311cd44SJoe Perches } 3103d311cd44SJoe Perches 310413f1937eSJoe Perches# Check for added, moved or deleted files 310513f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 310613f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 310713f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 310813f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 310913f1937eSJoe Perches (defined($1) || defined($2))))) { 3110a82603a8SAndrew Jeffery $is_patch = 1; 311113f1937eSJoe Perches $reported_maintainer_file = 1; 311213f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 311313f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 311413f1937eSJoe Perches } 311513f1937eSJoe Perches 3116e400edb1SRob Herring# Check for adding new DT bindings not in schema format 3117e400edb1SRob Herring if (!$in_commit_log && 3118e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 3119e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 3120e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 3121e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 3122e400edb1SRob Herring } 3123e400edb1SRob Herring 312400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 31258905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 3126000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 3127000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 31286c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 3129de7d4f0eSAndy Whitcroft } 3130de7d4f0eSAndy Whitcroft 3131de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 3132de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 3133171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 3134171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 3135171ae1a4SAndy Whitcroft 3136171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 3137171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 3138171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 3139171ae1a4SAndy Whitcroft 314034d99219SJoe Perches CHK("INVALID_UTF8", 3141000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 314200df344fSAndy Whitcroft } 31430a920b5bSAndy Whitcroft 314415662b3eSJoe Perches# Check if it's the start of a commit log 314515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 314615662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 3147eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 3148eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 314915662b3eSJoe Perches $in_header_lines = 0; 315015662b3eSJoe Perches $in_commit_log = 1; 3151ed43c4e5SAllen Hubbe $has_commit_log = 1; 315215662b3eSJoe Perches } 315315662b3eSJoe Perches 3154fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 3155fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 3156fa64205dSPasi Savanainen if ($in_header_lines && 3157fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 3158fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 3159fa64205dSPasi Savanainen $non_utf8_charset = 1; 3160fa64205dSPasi Savanainen } 3161fa64205dSPasi Savanainen 3162fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 316315662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 3164fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 316515662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 316615662b3eSJoe Perches } 316715662b3eSJoe Perches 3168d6430f71SJoe Perches# Check for absolute kernel paths in commit message 3169d6430f71SJoe Perches if ($tree && $in_commit_log) { 3170d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 3171d6430f71SJoe Perches my $file = $1; 3172d6430f71SJoe Perches 3173d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 3174d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 3175d6430f71SJoe Perches # 3176d6430f71SJoe Perches } else { 3177d6430f71SJoe Perches check_absolute_file($file, $herecurr); 3178d6430f71SJoe Perches } 3179d6430f71SJoe Perches } 3180d6430f71SJoe Perches } 3181d6430f71SJoe Perches 318266b47b4aSKees Cook# Check for various typo / spelling mistakes 318366d7a382SJoe Perches if (defined($misspellings) && 318466d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 3185ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 318666b47b4aSKees Cook my $typo = $1; 318766b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 318866b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 318966b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 31900675a8fbSJean Delvare my $msg_level = \&WARN; 31910675a8fbSJean Delvare $msg_level = \&CHK if ($file); 31920675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 319366b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 319466b47b4aSKees Cook $fix) { 319566b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 319666b47b4aSKees Cook } 319766b47b4aSKees Cook } 319866b47b4aSKees Cook } 319966b47b4aSKees Cook 3200a8dd86bfSMatteo Croce# check for invalid commit id 3201a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 3202a8dd86bfSMatteo Croce my $id; 3203a8dd86bfSMatteo Croce my $description; 3204a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 3205a8dd86bfSMatteo Croce if (!defined($id)) { 3206a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 3207a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 3208a8dd86bfSMatteo Croce } 3209a8dd86bfSMatteo Croce } 3210a8dd86bfSMatteo Croce 3211310cd06bSJoe Perches# check for repeated words separated by a single space 32128d0325ccSAditya Srivastava# avoid false positive from list command eg, '-rw-r--r-- 1 root root' 32138d0325ccSAditya Srivastava if (($rawline =~ /^\+/ || $in_commit_log) && 32148d0325ccSAditya Srivastava $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) { 32151db81a68SDwaipayan Ray pos($rawline) = 1 if (!$in_commit_log); 3216310cd06bSJoe Perches while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { 3217310cd06bSJoe Perches 3218310cd06bSJoe Perches my $first = $1; 3219310cd06bSJoe Perches my $second = $2; 32201db81a68SDwaipayan Ray my $start_pos = $-[1]; 32211db81a68SDwaipayan Ray my $end_pos = $+[2]; 3222310cd06bSJoe Perches if ($first =~ /(?:struct|union|enum)/) { 3223310cd06bSJoe Perches pos($rawline) += length($first) + length($second) + 1; 3224310cd06bSJoe Perches next; 3225310cd06bSJoe Perches } 3226310cd06bSJoe Perches 32271db81a68SDwaipayan Ray next if (lc($first) ne lc($second)); 3228310cd06bSJoe Perches next if ($first eq 'long'); 3229310cd06bSJoe Perches 32301db81a68SDwaipayan Ray # check for character before and after the word matches 32311db81a68SDwaipayan Ray my $start_char = ''; 32321db81a68SDwaipayan Ray my $end_char = ''; 32331db81a68SDwaipayan Ray $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1)); 32341db81a68SDwaipayan Ray $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline)); 32351db81a68SDwaipayan Ray 32361db81a68SDwaipayan Ray next if ($start_char =~ /^\S$/); 32371db81a68SDwaipayan Ray next if (index(" \t.,;?!", $end_char) == -1); 32381db81a68SDwaipayan Ray 32398d0325ccSAditya Srivastava # avoid repeating hex occurrences like 'ff ff fe 09 ...' 32408d0325ccSAditya Srivastava if ($first =~ /\b[0-9a-f]{2,}\b/i) { 32418d0325ccSAditya Srivastava next if (!exists($allow_repeated_words{lc($first)})); 32428d0325ccSAditya Srivastava } 32438d0325ccSAditya Srivastava 3244310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3245310cd06bSJoe Perches "Possible repeated word: '$first'\n" . $herecurr) && 3246310cd06bSJoe Perches $fix) { 3247310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; 3248310cd06bSJoe Perches } 3249310cd06bSJoe Perches } 3250310cd06bSJoe Perches 3251310cd06bSJoe Perches # if it's a repeated word on consecutive lines in a comment block 3252310cd06bSJoe Perches if ($prevline =~ /$;+\s*$/ && 3253310cd06bSJoe Perches $prevrawline =~ /($word_pattern)\s*$/) { 3254310cd06bSJoe Perches my $last_word = $1; 3255310cd06bSJoe Perches if ($rawline =~ /^\+\s*\*\s*$last_word /) { 3256310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3257310cd06bSJoe Perches "Possible repeated word: '$last_word'\n" . $hereprev) && 3258310cd06bSJoe Perches $fix) { 3259310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; 3260310cd06bSJoe Perches } 3261310cd06bSJoe Perches } 3262310cd06bSJoe Perches } 3263310cd06bSJoe Perches } 3264310cd06bSJoe Perches 326530670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 326630670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 326700df344fSAndy Whitcroft 32680a920b5bSAndy Whitcroft#trailing whitespace 32699c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 3270c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3271d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 3272d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 3273d5e616fcSJoe Perches $fix) { 3274194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 3275d5e616fcSJoe Perches } 3276c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 3277c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32783705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 32793705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 32803705ce5bSJoe Perches $fix) { 3281194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 32823705ce5bSJoe Perches } 32833705ce5bSJoe Perches 3284d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 32850a920b5bSAndy Whitcroft } 32865368df20SAndy Whitcroft 32874783f894SJosh Triplett# Check for FSF mailing addresses. 3288109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 32891bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 32903e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 32913e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 32924783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32930675a8fbSJean Delvare my $msg_level = \&ERROR; 32940675a8fbSJean Delvare $msg_level = \&CHK if ($file); 32950675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 32964783f894SJosh 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) 32974783f894SJosh Triplett } 32984783f894SJosh Triplett 32993354957aSAndi Kleen# check for Kconfig help text having a real description 33009fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 33019fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 33023354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 3303678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3304678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3305678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3306678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 33073354957aSAndi Kleen my $length = 0; 33089fe287d7SAndy Whitcroft my $cnt = $realcnt; 33099fe287d7SAndy Whitcroft my $ln = $linenr + 1; 33109fe287d7SAndy Whitcroft my $f; 3311a1385803SAndy Whitcroft my $is_start = 0; 33129fe287d7SAndy Whitcroft my $is_end = 0; 3313a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 33149fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 33159fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 33169fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 33179fe287d7SAndy Whitcroft 33189fe287d7SAndy Whitcroft next if ($f =~ /^-/); 33198d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3320a1385803SAndy Whitcroft 332186adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3322a1385803SAndy Whitcroft $is_start = 1; 332322a4ac02SMasahiro Yamada } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 3324a1385803SAndy Whitcroft $length = -1; 3325a1385803SAndy Whitcroft } 3326a1385803SAndy Whitcroft 33279fe287d7SAndy Whitcroft $f =~ s/^.//; 33283354957aSAndi Kleen $f =~ s/#.*//; 33293354957aSAndi Kleen $f =~ s/^\s+//; 33303354957aSAndi Kleen next if ($f =~ /^$/); 3331678ae162SUlf Magnusson 3332678ae162SUlf Magnusson # This only checks context lines in the patch 3333678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3334678ae162SUlf Magnusson # positives, even though some of these are 3335678ae162SUlf Magnusson # common words in help texts 3336678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3337678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 33389fe287d7SAndy Whitcroft $is_end = 1; 33399fe287d7SAndy Whitcroft last; 33409fe287d7SAndy Whitcroft } 33413354957aSAndi Kleen $length++; 33423354957aSAndi Kleen } 334356193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3344000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 334556193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 334656193274SVadim Bendebury } 3347a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 33483354957aSAndi Kleen } 33493354957aSAndi Kleen 33507ccf41a8SJoe Perches# check MAINTAINERS entries 33517ccf41a8SJoe Perches if ($realfile =~ /^MAINTAINERS$/) { 33527ccf41a8SJoe Perches# check MAINTAINERS entries for the right form 33537ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 3354628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3355628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3356628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3357628f91a2SJoe Perches $fix) { 3358628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3359628f91a2SJoe Perches } 3360628f91a2SJoe Perches } 33617ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too 33627ccf41a8SJoe Perches my $preferred_order = 'MRLSWQBCPTFXNK'; 33637ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 33647ccf41a8SJoe Perches $prevrawline =~ /^[\+ ][A-Z]:/) { 33657ccf41a8SJoe Perches $rawline =~ /^\+([A-Z]):\s*(.*)/; 33667ccf41a8SJoe Perches my $cur = $1; 33677ccf41a8SJoe Perches my $curval = $2; 33687ccf41a8SJoe Perches $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; 33697ccf41a8SJoe Perches my $prev = $1; 33707ccf41a8SJoe Perches my $prevval = $2; 33717ccf41a8SJoe Perches my $curindex = index($preferred_order, $cur); 33727ccf41a8SJoe Perches my $previndex = index($preferred_order, $prev); 33737ccf41a8SJoe Perches if ($curindex < 0) { 33747ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 33757ccf41a8SJoe Perches "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); 33767ccf41a8SJoe Perches } else { 33777ccf41a8SJoe Perches if ($previndex >= 0 && $curindex < $previndex) { 33787ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 33797ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); 33807ccf41a8SJoe Perches } elsif ((($prev eq 'F' && $cur eq 'F') || 33817ccf41a8SJoe Perches ($prev eq 'X' && $cur eq 'X')) && 33827ccf41a8SJoe Perches ($prevval cmp $curval) > 0) { 33837ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 33847ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); 33857ccf41a8SJoe Perches } 33867ccf41a8SJoe Perches } 33877ccf41a8SJoe Perches } 33887ccf41a8SJoe Perches } 3389628f91a2SJoe Perches 3390327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3391327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3392327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3393327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3394327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3395327953e9SChristoph Jaeger } 3396327953e9SChristoph Jaeger 3397c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3398c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3399c68e5878SArnaud Lacombe my $flag = $1; 3400c68e5878SArnaud Lacombe my $replacement = { 3401c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3402c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3403c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3404c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3405c68e5878SArnaud Lacombe }; 3406c68e5878SArnaud Lacombe 3407c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3408c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3409c68e5878SArnaud Lacombe } 3410c68e5878SArnaud Lacombe 3411bff5da43SRob Herring# check for DT compatible documentation 34127dd05b38SFlorian Vaussard if (defined $root && 34137dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 34147dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 34157dd05b38SFlorian Vaussard 3416bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3417bff5da43SRob Herring 3418cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3419852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3420cc93319bSFlorian Vaussard 3421bff5da43SRob Herring foreach my $compat (@compats) { 3422bff5da43SRob Herring my $compat2 = $compat; 3423185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3424185d566bSRob Herring my $compat3 = $compat; 3425185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3426185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3427bff5da43SRob Herring if ( $? >> 8 ) { 3428bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3429bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3430bff5da43SRob Herring } 3431bff5da43SRob Herring 34324fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 34334fbf32a6SFlorian Vaussard my $vendor = $1; 3434852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3435bff5da43SRob Herring if ( $? >> 8 ) { 3436bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3437cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3438bff5da43SRob Herring } 3439bff5da43SRob Herring } 3440bff5da43SRob Herring } 3441bff5da43SRob Herring 34429f3a8992SRob Herring# check for using SPDX license tag at beginning of files 34439f3a8992SRob Herring if ($realline == $checklicenseline) { 34449f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 34459f3a8992SRob Herring $checklicenseline = 2; 34469f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 34479f3a8992SRob Herring my $comment = ""; 34489f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 34499f3a8992SRob Herring $comment = '/*'; 34509f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 34519f3a8992SRob Herring $comment = '//'; 3452c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 34539f3a8992SRob Herring $comment = '#'; 34549f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 34559f3a8992SRob Herring $comment = '..'; 34569f3a8992SRob Herring } 34579f3a8992SRob Herring 3458fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3459fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3460fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3461ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3462fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3463fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3464fdf13693SJoe Perches } 3465fdf13693SJoe Perches 34669f3a8992SRob Herring if ($comment !~ /^$/ && 3467ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 34689f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 34699f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 34703b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 34713b6e8ac9SJoe Perches my $spdx_license = $1; 34723b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 34733b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 34743b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 34753b6e8ac9SJoe Perches } 347650c92900SLubomir Rintel if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 347750c92900SLubomir Rintel not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 347850c92900SLubomir Rintel my $msg_level = \&WARN; 347950c92900SLubomir Rintel $msg_level = \&CHK if ($file); 348050c92900SLubomir Rintel if (&{$msg_level}("SPDX_LICENSE_TAG", 348150c92900SLubomir Rintel 348250c92900SLubomir Rintel "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 348350c92900SLubomir Rintel $fix) { 348450c92900SLubomir Rintel $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 348550c92900SLubomir Rintel } 348650c92900SLubomir Rintel } 34879f3a8992SRob Herring } 34889f3a8992SRob Herring } 34899f3a8992SRob Herring } 34909f3a8992SRob Herring 3491a0154cdbSJoe Perches# check for embedded filenames 3492a0154cdbSJoe Perches if ($rawline =~ /^\+.*\Q$realfile\E/) { 3493a0154cdbSJoe Perches WARN("EMBEDDED_FILENAME", 3494a0154cdbSJoe Perches "It's generally not useful to have the filename in the file\n" . $herecurr); 3495a0154cdbSJoe Perches } 3496a0154cdbSJoe Perches 34975368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3498d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 34995368df20SAndy Whitcroft 3500a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3501a8da38a9SJoe Perches if ($realline != $checklicenseline && 3502a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3503a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3504a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3505a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3506a8da38a9SJoe Perches } 3507a8da38a9SJoe Perches 350847e0c88bSJoe Perches# line length limit (with some exclusions) 350947e0c88bSJoe Perches# 351047e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 351147e0c88bSJoe Perches# logging functions like pr_info that end in a string 351247e0c88bSJoe Perches# lines with a single string 351347e0c88bSJoe Perches# #defines that are a single string 35142e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 351547e0c88bSJoe Perches# 351647e0c88bSJoe Perches# There are 3 different line length message types: 3517ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 351847e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 351947e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 352047e0c88bSJoe Perches# 352147e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 352247e0c88bSJoe Perches# 352347e0c88bSJoe Perches 3524b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 352547e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 352647e0c88bSJoe Perches 352747e0c88bSJoe Perches # Check the allowed long line types first 352847e0c88bSJoe Perches 352947e0c88bSJoe Perches # logging functions that end in a string that starts 353047e0c88bSJoe Perches # before $max_line_length 353147e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 353247e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 353347e0c88bSJoe Perches $msg_type = ""; 353447e0c88bSJoe Perches 353547e0c88bSJoe Perches # lines with only strings (w/ possible termination) 353647e0c88bSJoe Perches # #defines with only strings 353747e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 353847e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 353947e0c88bSJoe Perches $msg_type = ""; 354047e0c88bSJoe Perches 3541cc147506SJoe Perches # More special cases 3542cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3543cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3544d560a5f8SJoe Perches $msg_type = ""; 3545d560a5f8SJoe Perches 35462e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 35472e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 35482e4bbbc5SAndreas Brauchli $msg_type = ""; 35492e4bbbc5SAndreas Brauchli 355047e0c88bSJoe Perches # Otherwise set the alternate message types 355147e0c88bSJoe Perches 355247e0c88bSJoe Perches # a comment starts before $max_line_length 355347e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 355447e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 355547e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 355647e0c88bSJoe Perches 355747e0c88bSJoe Perches # a quoted string starts before $max_line_length 355847e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 355947e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 356047e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 356147e0c88bSJoe Perches } 356247e0c88bSJoe Perches 356347e0c88bSJoe Perches if ($msg_type ne "" && 356447e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 3565bdc48fa1SJoe Perches my $msg_level = \&WARN; 3566bdc48fa1SJoe Perches $msg_level = \&CHK if ($file); 3567bdc48fa1SJoe Perches &{$msg_level}($msg_type, 3568bdc48fa1SJoe Perches "line length of $length exceeds $max_line_length columns\n" . $herecurr); 35690a920b5bSAndy Whitcroft } 357047e0c88bSJoe Perches } 35710a920b5bSAndy Whitcroft 35728905a67cSAndy Whitcroft# check for adding lines without a newline. 35738905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 357447ca69b8STom Rix if (WARN("MISSING_EOF_NEWLINE", 357547ca69b8STom Rix "adding a line without newline at end of file\n" . $herecurr) && 357647ca69b8STom Rix $fix) { 357747ca69b8STom Rix fix_delete_line($fixlinenr+1, "No newline at end of file"); 357847ca69b8STom Rix } 35798905a67cSAndy Whitcroft } 35808905a67cSAndy Whitcroft 3581b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3582de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 35830a920b5bSAndy Whitcroft 35840a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3585713a09deSAntonio Borneo# more than $tabsize must use tabs. 3586c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3587c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3588c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3589d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 35903705ce5bSJoe Perches if (ERROR("CODE_INDENT", 35913705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 35923705ce5bSJoe Perches $fix) { 3593194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 35943705ce5bSJoe Perches } 35950a920b5bSAndy Whitcroft } 35960a920b5bSAndy Whitcroft 359708e44365SAlberto Panizzo# check for space before tabs. 359808e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 359908e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 36003705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 36013705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 36023705ce5bSJoe Perches $fix) { 3603194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3604713a09deSAntonio Borneo s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3605194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3606c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 36073705ce5bSJoe Perches } 360808e44365SAlberto Panizzo } 360908e44365SAlberto Panizzo 36106a487211SJoe Perches# check for assignments on the start of a line 36116a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 3612da7355abSAditya Srivastava my $operator = $1; 3613da7355abSAditya Srivastava if (CHK("ASSIGNMENT_CONTINUATIONS", 3614da7355abSAditya Srivastava "Assignment operator '$1' should be on the previous line\n" . $hereprev) && 3615da7355abSAditya Srivastava $fix && $prevrawline =~ /^\+/) { 3616da7355abSAditya Srivastava # add assignment operator to the previous line, remove from current line 3617da7355abSAditya Srivastava $fixed[$fixlinenr - 1] .= " $operator"; 3618da7355abSAditya Srivastava $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 3619da7355abSAditya Srivastava } 36206a487211SJoe Perches } 36216a487211SJoe Perches 3622d1fe9c09SJoe Perches# check for && or || at the start of a line 3623d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 36248e08f076SAditya Srivastava my $operator = $1; 36258e08f076SAditya Srivastava if (CHK("LOGICAL_CONTINUATIONS", 36268e08f076SAditya Srivastava "Logical continuations should be on the previous line\n" . $hereprev) && 36278e08f076SAditya Srivastava $fix && $prevrawline =~ /^\+/) { 36288e08f076SAditya Srivastava # insert logical operator at last non-comment, non-whitepsace char on previous line 36298e08f076SAditya Srivastava $prevline =~ /[\s$;]*$/; 36308e08f076SAditya Srivastava my $line_end = substr($prevrawline, $-[0]); 36318e08f076SAditya Srivastava $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/; 36328e08f076SAditya Srivastava $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; 36338e08f076SAditya Srivastava } 3634d1fe9c09SJoe Perches } 3635d1fe9c09SJoe Perches 3636a91e8994SJoe Perches# check indentation starts on a tab stop 36375b57980dSJoe Perches if ($perl_version_ok && 3638bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3639a91e8994SJoe Perches my $indent = length($1); 3640713a09deSAntonio Borneo if ($indent % $tabsize) { 3641a91e8994SJoe Perches if (WARN("TABSTOP", 3642a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3643a91e8994SJoe Perches $fix) { 3644713a09deSAntonio Borneo $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3645a91e8994SJoe Perches } 3646a91e8994SJoe Perches } 3647a91e8994SJoe Perches } 3648a91e8994SJoe Perches 3649d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 36505b57980dSJoe Perches if ($perl_version_ok && 3651fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3652d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3653d1fe9c09SJoe Perches my $oldindent = $1; 3654d1fe9c09SJoe Perches my $rest = $2; 3655d1fe9c09SJoe Perches 3656d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3657d1fe9c09SJoe Perches if ($pos >= 0) { 3658b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3659b34a26f3SJoe Perches my $newindent = $2; 3660d1fe9c09SJoe Perches 3661d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3662713a09deSAntonio Borneo "\t" x ($pos / $tabsize) . 3663713a09deSAntonio Borneo " " x ($pos % $tabsize); 3664d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3665d1fe9c09SJoe Perches 3666d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3667d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 36683705ce5bSJoe Perches 36693705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 36703705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 36713705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3672194f66fcSJoe Perches $fixed[$fixlinenr] =~ 36733705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 36743705ce5bSJoe Perches } 3675d1fe9c09SJoe Perches } 3676d1fe9c09SJoe Perches } 3677d1fe9c09SJoe Perches } 3678d1fe9c09SJoe Perches 36796ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 36806ab3a970SJoe Perches# avoid checking a few false positives: 36816ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 36826ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 36836ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 36846ab3a970SJoe Perches# multiline macros that define functions 36856ab3a970SJoe Perches# known attributes or the __attribute__ keyword 36866ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 36876ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 36883705ce5bSJoe Perches if (CHK("SPACING", 3689f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 36903705ce5bSJoe Perches $fix) { 3691194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3692f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 36933705ce5bSJoe Perches } 3694aad4f614SJoe Perches } 3695aad4f614SJoe Perches 369686406b1cSJoe Perches# Block comment styles 369786406b1cSJoe Perches# Networking with an initial /* 369805880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3699fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 370085ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 3701c70735c2SŁukasz Stelmach $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier 370205880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 370305880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 370405880600SJoe Perches } 370505880600SJoe Perches 370686406b1cSJoe Perches# Block comments use * on subsequent lines 370786406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 370886406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3709a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 371061135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3711a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 371286406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 371386406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3714a605e32eSJoe Perches } 3715a605e32eSJoe Perches 371686406b1cSJoe Perches# Block comments use */ on trailing lines 371786406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3718c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3719c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3720c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 372186406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 372286406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 372305880600SJoe Perches } 372405880600SJoe Perches 372508eb9b80SJoe Perches# Block comment * alignment 372608eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3727af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3728af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3729af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 373008eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3731af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3732af207524SJoe Perches my $oldindent; 373308eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3734af207524SJoe Perches if (defined($1)) { 3735af207524SJoe Perches $oldindent = expand_tabs($1); 3736af207524SJoe Perches } else { 3737af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3738af207524SJoe Perches $oldindent = expand_tabs($1); 3739af207524SJoe Perches } 374008eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 374108eb9b80SJoe Perches my $newindent = $1; 374208eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3743af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 374408eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 374508eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 374608eb9b80SJoe Perches } 374708eb9b80SJoe Perches } 374808eb9b80SJoe Perches 37497f619191SJoe Perches# check for missing blank lines after struct/union declarations 37507f619191SJoe Perches# with exceptions for various attributes and macros 37517f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 37527f619191SJoe Perches $line =~ /^\+/ && 37537f619191SJoe Perches !($line =~ /^\+\s*$/ || 37547f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 37557f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 37567f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 37577f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 37587f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 37597f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 37600bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 37617f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3762d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3763d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3764d752fcc8SJoe Perches $fix) { 3765f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3766d752fcc8SJoe Perches } 37677f619191SJoe Perches } 37687f619191SJoe Perches 3769365dd4eaSJoe Perches# check for multiple consecutive blank lines 3770365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3771365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3772365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3773d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3774d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3775d752fcc8SJoe Perches $fix) { 3776f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3777d752fcc8SJoe Perches } 3778d752fcc8SJoe Perches 3779365dd4eaSJoe Perches $last_blank_line = $linenr; 3780365dd4eaSJoe Perches } 3781365dd4eaSJoe Perches 37823b617e3bSJoe Perches# check for missing blank lines after declarations 37833f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 37843f7bac03SJoe Perches # actual declarations 37853f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 37865a4e1fd3SJoe Perches # function pointer declarations 37875a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 37883f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 37893f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 37903f7bac03SJoe Perches # known declaration macros 37913f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 37923f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 37933f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 37943f7bac03SJoe Perches # other possible extensions of declaration lines 37953f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 37963f7bac03SJoe Perches # not starting a section or a macro "\" extended line 37973f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 37983f7bac03SJoe Perches # looks like a declaration 37993f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 38005a4e1fd3SJoe Perches # function pointer declarations 38015a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 38023f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 38033f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 38043f7bac03SJoe Perches # known declaration macros 38053f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 38063f7bac03SJoe Perches # start of struct or union or enum 3807328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 38083f7bac03SJoe Perches # start or end of block or continuation of declaration 38093f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 38103f7bac03SJoe Perches # bitfield continuation 38113f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 38123f7bac03SJoe Perches # other possible extensions of declaration lines 38133f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 38143f7bac03SJoe Perches # indentation of previous and current line are the same 38153f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3816d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3817d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3818d752fcc8SJoe Perches $fix) { 3819f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3820d752fcc8SJoe Perches } 38213b617e3bSJoe Perches } 38223b617e3bSJoe Perches 38235f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 38246b4c5bebSAndy Whitcroft# Exceptions: 38256b4c5bebSAndy Whitcroft# 1) within comments 38266b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 38276b4c5bebSAndy Whitcroft# 3) hanging labels 38283705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 38295f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 38303705ce5bSJoe Perches if (WARN("LEADING_SPACE", 38313705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 38323705ce5bSJoe Perches $fix) { 3833194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 38343705ce5bSJoe Perches } 38355f7ddae6SRaffaele Recalcati } 38365f7ddae6SRaffaele Recalcati 3837b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3838b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3839b9ea10d6SAndy Whitcroft 38405751a24eSJoe Perches# check for unusual line ending [ or ( 38415751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 38425751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 38435751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 38445751a24eSJoe Perches } 38455751a24eSJoe Perches 38464dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 38474dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 38484dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 38494dbed76fSJoe Perches $context_function = $1; 38504dbed76fSJoe Perches } 38514dbed76fSJoe Perches 38524dbed76fSJoe Perches# check if this appears to be the end of function declaration 38534dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 38544dbed76fSJoe Perches undef $context_function; 38554dbed76fSJoe Perches } 38564dbed76fSJoe Perches 3857032a4c0fSJoe Perches# check indentation of any line with a bare else 3858840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3859032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3860032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3861032a4c0fSJoe Perches my $tabs = length($1) + 1; 3862840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3863840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3864840080a0SJoe Perches defined $lines[$linenr] && 3865840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3866032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3867032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3868032a4c0fSJoe Perches } 3869032a4c0fSJoe Perches } 3870032a4c0fSJoe Perches 3871c00df19aSJoe Perches# check indentation of a line with a break; 3872dc58bc55SJoe Perches# if the previous line is a goto, return or break 3873dc58bc55SJoe Perches# and is indented the same # of tabs 3874c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3875c00df19aSJoe Perches my $tabs = $1; 3876dc58bc55SJoe Perches if ($prevline =~ /^\+$tabs(goto|return|break)\b/) { 3877dc58bc55SJoe Perches if (WARN("UNNECESSARY_BREAK", 3878dc58bc55SJoe Perches "break is not useful after a $1\n" . $hereprev) && 3879dc58bc55SJoe Perches $fix) { 3880dc58bc55SJoe Perches fix_delete_line($fixlinenr, $rawline); 3881dc58bc55SJoe Perches } 3882c00df19aSJoe Perches } 3883c00df19aSJoe Perches } 3884c00df19aSJoe Perches 3885c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3886cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3887000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3888000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3889c2fdda0dSAndy Whitcroft } 389022f2a2efSAndy Whitcroft 389156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 389256e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 389356e77d70SJoe Perches WARN("HOTPLUG_SECTION", 389456e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 389556e77d70SJoe Perches } 389656e77d70SJoe Perches 38979c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 38982b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 38992b474a1aSAndy Whitcroft $realline_next); 39003e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3901ca819864SJoe Perches if ($linenr > $suppress_statement && 39021b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3903170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3904f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3905171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3906171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3907171ae1a4SAndy Whitcroft 39083e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 39093e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 39103e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 39113e469cdcSAndy Whitcroft # until we hit end of it. 39123e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 39133e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 39143e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 39153e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 39163e469cdcSAndy Whitcroft } 3917f74bd194SAndy Whitcroft 39182b474a1aSAndy Whitcroft # Find the real next line. 39192b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 39202b474a1aSAndy Whitcroft if (defined $realline_next && 39212b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 39222b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 39232b474a1aSAndy Whitcroft $realline_next++; 39242b474a1aSAndy Whitcroft } 39252b474a1aSAndy Whitcroft 3926171ae1a4SAndy Whitcroft my $s = $stat; 3927171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3928cf655043SAndy Whitcroft 3929c2fdda0dSAndy Whitcroft # Ignore goto labels. 3930171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3931c2fdda0dSAndy Whitcroft 3932c2fdda0dSAndy Whitcroft # Ignore functions being called 3933171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3934c2fdda0dSAndy Whitcroft 3935463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3936463f2864SAndy Whitcroft 3937c45dcabdSAndy Whitcroft # declarations always start with types 3938d2506586SAndy 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) { 3939c45dcabdSAndy Whitcroft my $type = $1; 3940c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3941c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3942c45dcabdSAndy Whitcroft 39436c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3944a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3945c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3946c2fdda0dSAndy Whitcroft } 39478905a67cSAndy Whitcroft 39486c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 394965863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3950c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 39519c0ca6f9SAndy Whitcroft } 39528905a67cSAndy Whitcroft 39538905a67cSAndy Whitcroft # Check for any sort of function declaration. 39548905a67cSAndy Whitcroft # int foo(something bar, other baz); 39558905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3956171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 39578905a67cSAndy Whitcroft my ($name_len) = length($1); 39588905a67cSAndy Whitcroft 3959cf655043SAndy Whitcroft my $ctx = $s; 3960773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 39618905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3962cf655043SAndy Whitcroft 39638905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3964c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 39658905a67cSAndy Whitcroft 3966c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 39678905a67cSAndy Whitcroft } 39688905a67cSAndy Whitcroft } 39698905a67cSAndy Whitcroft } 39708905a67cSAndy Whitcroft 39719c0ca6f9SAndy Whitcroft } 39729c0ca6f9SAndy Whitcroft 397300df344fSAndy Whitcroft# 397400df344fSAndy Whitcroft# Checks which may be anchored in the context. 397500df344fSAndy Whitcroft# 397600df344fSAndy Whitcroft 397700df344fSAndy Whitcroft# Check for switch () and associated case and default 397800df344fSAndy Whitcroft# statements should be at the same indent. 397900df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 398000df344fSAndy Whitcroft my $err = ''; 398100df344fSAndy Whitcroft my $sep = ''; 398200df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 398300df344fSAndy Whitcroft shift(@ctx); 398400df344fSAndy Whitcroft for my $ctx (@ctx) { 398500df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 398600df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 398700df344fSAndy Whitcroft $indent != $cindent) { 398800df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 398900df344fSAndy Whitcroft $sep = ''; 399000df344fSAndy Whitcroft } else { 399100df344fSAndy Whitcroft $sep = "[...]\n"; 399200df344fSAndy Whitcroft } 399300df344fSAndy Whitcroft } 399400df344fSAndy Whitcroft if ($err ne '') { 3995000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3996000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3997de7d4f0eSAndy Whitcroft } 3998de7d4f0eSAndy Whitcroft } 3999de7d4f0eSAndy Whitcroft 4000de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 4001de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 40020fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 4003773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 4004773647a0SAndy Whitcroft 40059c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 40068eef05ddSJoe Perches 40078eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 40088eef05ddSJoe Perches WARN("DEEP_INDENTATION", 40098eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 40108eef05ddSJoe Perches } 40118eef05ddSJoe Perches 4012de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 4013de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 4014de7d4f0eSAndy Whitcroft 4015548596d5SAndy Whitcroft my $ctx_ln = $linenr; 4016548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 4017de7d4f0eSAndy Whitcroft 4018548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 4019548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 4020548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 4021548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 4022548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 4023773647a0SAndy Whitcroft $ctx_ln++; 4024773647a0SAndy Whitcroft } 4025548596d5SAndy Whitcroft 402653210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 402753210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 4028773647a0SAndy Whitcroft 4029773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 4030000d1cc1SJoe Perches ERROR("OPEN_BRACE", 4031000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 403201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 403300df344fSAndy Whitcroft } 4034773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 4035773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 4036773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 4037773647a0SAndy Whitcroft { 40389c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 40399c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 4040000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 4041000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 404201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 40439c0ca6f9SAndy Whitcroft } 40449c0ca6f9SAndy Whitcroft } 404500df344fSAndy Whitcroft } 404600df344fSAndy Whitcroft 40474d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 4048f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 40493e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 40503e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 40513e469cdcSAndy Whitcroft if (!defined $stat); 40524d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 40534d001e4dSAndy Whitcroft 40544d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 40554d001e4dSAndy Whitcroft 40569f5af480SJoe Perches # remove inline comments 40579f5af480SJoe Perches $s =~ s/$;/ /g; 40589f5af480SJoe Perches $c =~ s/$;/ /g; 40594d001e4dSAndy Whitcroft 40604d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 40616f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 40626f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 40634d001e4dSAndy Whitcroft 40649f5af480SJoe Perches # Make sure we remove the line prefixes as we have 40659f5af480SJoe Perches # none on the first line, and are going to readd them 40669f5af480SJoe Perches # where necessary. 40679f5af480SJoe Perches $s =~ s/\n./\n/gs; 40689f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 40699f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 40709f5af480SJoe Perches } 40719f5af480SJoe Perches 40724d001e4dSAndy Whitcroft # We want to check the first line inside the block 40734d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 40744d001e4dSAndy Whitcroft # 1) any blank line termination 40754d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 40764d001e4dSAndy Whitcroft # 3) any do (...) { 40774d001e4dSAndy Whitcroft my $continuation = 0; 40784d001e4dSAndy Whitcroft my $check = 0; 40794d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 40804d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 40814d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 40824d001e4dSAndy Whitcroft $continuation = 1; 40834d001e4dSAndy Whitcroft } 40849bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 40854d001e4dSAndy Whitcroft $check = 1; 40864d001e4dSAndy Whitcroft $cond_lines++; 40874d001e4dSAndy Whitcroft } 40884d001e4dSAndy Whitcroft 40894d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 40904d001e4dSAndy Whitcroft # preprocessor statement. 40914d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 40924d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 40934d001e4dSAndy Whitcroft $check = 0; 40944d001e4dSAndy Whitcroft } 40954d001e4dSAndy Whitcroft 40969bd49efeSAndy Whitcroft my $cond_ptr = -1; 4097740504c6SAndy Whitcroft $continuation = 0; 40989bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 40999bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 41004d001e4dSAndy Whitcroft 4101f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 4102f16fa28fSAndy Whitcroft # is not linear. 4103f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 4104f16fa28fSAndy Whitcroft $check = 0; 4105f16fa28fSAndy Whitcroft } 4106f16fa28fSAndy Whitcroft 41079bd49efeSAndy Whitcroft # Ignore: 41089bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 41099bd49efeSAndy Whitcroft # 2) preprocessor lines, and 41109bd49efeSAndy Whitcroft # 3) labels. 4111740504c6SAndy Whitcroft if ($continuation || 4112740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 41139bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 41149bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 4115740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 411630dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 41179bd49efeSAndy Whitcroft $cond_lines++; 41189bd49efeSAndy Whitcroft } 41194d001e4dSAndy Whitcroft } 412030dad6ebSAndy Whitcroft } 41214d001e4dSAndy Whitcroft 41224d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 41234d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 41244d001e4dSAndy Whitcroft 41254d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 41264d001e4dSAndy Whitcroft # this is not this patch's fault. 41274d001e4dSAndy Whitcroft if (!defined($stat_real) || 41284d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 41294d001e4dSAndy Whitcroft $check = 0; 41304d001e4dSAndy Whitcroft } 41314d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 41324d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 41334d001e4dSAndy Whitcroft } 41344d001e4dSAndy Whitcroft 41359bd49efeSAndy 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"; 41364d001e4dSAndy Whitcroft 41379f5af480SJoe Perches if ($check && $s ne '' && 4138713a09deSAntonio Borneo (($sindent % $tabsize) != 0 || 41399f5af480SJoe Perches ($sindent < $indent) || 4140f6950a73SJoe Perches ($sindent == $indent && 4141f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 4142713a09deSAntonio Borneo ($sindent > $indent + $tabsize))) { 4143000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 4144000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 41454d001e4dSAndy Whitcroft } 41464d001e4dSAndy Whitcroft } 41474d001e4dSAndy Whitcroft 41486c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 41496c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 41501f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 41511f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 41526c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 4153c2fdda0dSAndy Whitcroft if ($dbg_values) { 4154c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 4155cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 4156cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 41571f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 4158c2fdda0dSAndy Whitcroft } 41596c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 41606c72ffaaSAndy Whitcroft 416100df344fSAndy Whitcroft#ignore lines not being added 41623705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 416300df344fSAndy Whitcroft 416499ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings 416599ca38c2SJoe Perches# e.g.: int foo = foo, *bar = NULL; 416699ca38c2SJoe Perches# struct foo bar = *(&(bar)); 416799ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { 416899ca38c2SJoe Perches my $var = $1; 416999ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { 417099ca38c2SJoe Perches WARN("SELF_ASSIGNMENT", 417199ca38c2SJoe Perches "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); 417299ca38c2SJoe Perches } 417399ca38c2SJoe Perches } 417499ca38c2SJoe Perches 417511ca40a0SJoe Perches# check for dereferences that span multiple lines 417611ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 417711ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 417811ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 417911ca40a0SJoe Perches my $ref = $1; 418011ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 418111ca40a0SJoe Perches $ref .= $1; 418211ca40a0SJoe Perches $ref =~ s/\s//g; 418311ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 418411ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 418511ca40a0SJoe Perches } 418611ca40a0SJoe Perches 4187a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 4188c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 4189a1ce18e4SJoe Perches my $type = $1; 4190a1ce18e4SJoe Perches my $var = $2; 4191207a8e84SJoe Perches $var = "" if (!defined $var); 4192207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 4193a1ce18e4SJoe Perches my $sign = $1; 4194a1ce18e4SJoe Perches my $pointer = $2; 4195a1ce18e4SJoe Perches 4196a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 4197a1ce18e4SJoe Perches 4198a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 4199a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 4200a1ce18e4SJoe Perches $fix) { 4201a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 4202207a8e84SJoe Perches my $comp_pointer = $pointer; 4203207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 4204207a8e84SJoe Perches $decl .= $comp_pointer; 4205207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 4206207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 4207a1ce18e4SJoe Perches } 4208a1ce18e4SJoe Perches } 4209a1ce18e4SJoe Perches } 4210a1ce18e4SJoe Perches 4211653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 42127429c690SAndy Whitcroft if ($dbg_type) { 42137429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 4214000d1cc1SJoe Perches ERROR("TEST_TYPE", 4215000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 42167429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 4217000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 4218000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 42197429c690SAndy Whitcroft } 4220653d4876SAndy Whitcroft next; 4221653d4876SAndy Whitcroft } 4222a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 4223a1ef277eSAndy Whitcroft if ($dbg_attr) { 42249360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 4225000d1cc1SJoe Perches ERROR("TEST_ATTR", 4226000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 42279360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 4228000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 4229000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 4230a1ef277eSAndy Whitcroft } 4231a1ef277eSAndy Whitcroft next; 4232a1ef277eSAndy Whitcroft } 4233653d4876SAndy Whitcroft 4234f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 423599423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 423699423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 4237d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 4238d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 4239f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 4240f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4241f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 4242d752fcc8SJoe Perches my $fixedline = $prevrawline; 4243d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 4244f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4245d752fcc8SJoe Perches $fixedline = $line; 42468d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 4247f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4248d752fcc8SJoe Perches } 4249f0a594c1SAndy Whitcroft } 4250f0a594c1SAndy Whitcroft 425100df344fSAndy Whitcroft# 425200df344fSAndy Whitcroft# Checks which are anchored on the added line. 425300df344fSAndy Whitcroft# 425400df344fSAndy Whitcroft 4255653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 4256c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 4257653d4876SAndy Whitcroft my $path = $1; 4258653d4876SAndy Whitcroft if ($path =~ m{//}) { 4259000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 4260495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 4261495e9d84SJoe Perches } 4262495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 4263495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 4264495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 4265653d4876SAndy Whitcroft } 4266653d4876SAndy Whitcroft } 4267653d4876SAndy Whitcroft 426800df344fSAndy Whitcroft# no C99 // comments 426900df344fSAndy Whitcroft if ($line =~ m{//}) { 42703705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 42713705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 42723705ce5bSJoe Perches $fix) { 4273194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 42743705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 42753705ce5bSJoe Perches my $comment = trim($1); 4276194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 42773705ce5bSJoe Perches } 42783705ce5bSJoe Perches } 427900df344fSAndy Whitcroft } 428000df344fSAndy Whitcroft # Remove C99 comments. 42810a920b5bSAndy Whitcroft $line =~ s@//.*@@; 42826c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 42830a920b5bSAndy Whitcroft 42842b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 42852b474a1aSAndy Whitcroft# the whole statement. 42862b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 42872b474a1aSAndy Whitcroft if (defined $realline_next && 42882b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 42892b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 42902b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 42912b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 42923cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 42933cbf62dfSAndy Whitcroft # a prefix: 42943cbf62dfSAndy Whitcroft # XXX(foo); 42953cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 4296653d4876SAndy Whitcroft my $name = $1; 429787a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 42983cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 42993cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 43003cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 43013cbf62dfSAndy Whitcroft 43023cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 43032b474a1aSAndy Whitcroft \n.}\s*$| 430448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 430548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 430648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 43072b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 43082b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 430948012058SAndy Whitcroft )/x) { 43102b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 43112b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 43122b474a1aSAndy Whitcroft } else { 43132b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 43140a920b5bSAndy Whitcroft } 43150a920b5bSAndy Whitcroft } 43162b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 43172b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 43182b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 43192b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_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 4543243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4544243f3803SJoe Perches my $orig = $1; 4545243f3803SJoe Perches my $level = lc($orig); 4546243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 45478f26b837SJoe Perches my $level2 = $level; 45488f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4549243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4550daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4551243f3803SJoe Perches } 4552243f3803SJoe Perches 4553dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4554dc139313SJoe Perches my $orig = $1; 4555dc139313SJoe Perches my $level = lc($orig); 4556dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4557dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4558dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4559dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4560dc139313SJoe Perches } 4561dc139313SJoe Perches 45628020b253SNicolas Boichat# trace_printk should not be used in production code. 45638020b253SNicolas Boichat if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { 45648020b253SNicolas Boichat WARN("TRACE_PRINTK", 45658020b253SNicolas Boichat "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); 45668020b253SNicolas Boichat } 45678020b253SNicolas Boichat 456891c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 456991c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 457091c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 457191c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 457291c9afafSAndy Lutomirski WARN("ENOSYS", 457391c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 457491c9afafSAndy Lutomirski } 457591c9afafSAndy Lutomirski 45766b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches. 45776b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 45786b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected. 45796b9ea5ffSJakub Kicinski if (!$file && $line =~ /\bENOTSUPP\b/) { 45806b9ea5ffSJakub Kicinski if (WARN("ENOTSUPP", 45816b9ea5ffSJakub Kicinski "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 45826b9ea5ffSJakub Kicinski $fix) { 45836b9ea5ffSJakub Kicinski $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 45846b9ea5ffSJakub Kicinski } 45856b9ea5ffSJakub Kicinski } 45866b9ea5ffSJakub Kicinski 4587653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4588653d4876SAndy Whitcroft# or if closed on same line 45895b57980dSJoe Perches if ($perl_version_ok && 45902d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 45912d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 45922d453e3bSJoe Perches $sline !~ /}/) { 45938d182478SJoe Perches if (ERROR("OPEN_BRACE", 45942d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 45958d182478SJoe Perches $fix) { 45968d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 45978d182478SJoe Perches my $fixed_line = $rawline; 459803f49351SDwaipayan Ray $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/; 45998d182478SJoe Perches my $line1 = $1; 46008d182478SJoe Perches my $line2 = $2; 46018d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 46028d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 46038d182478SJoe Perches if ($line2 !~ /^\s*$/) { 46048d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 46058d182478SJoe Perches } 46068d182478SJoe Perches } 46070a920b5bSAndy Whitcroft } 4608653d4876SAndy Whitcroft 46098905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 46108905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 46118905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 46128d182478SJoe Perches if (ERROR("OPEN_BRACE", 46138d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 46148d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 46158d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 46168d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 46178d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 46188d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 46198d182478SJoe Perches $fixedline = $rawline; 46208d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 46218d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 46228d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 46238d182478SJoe Perches } 46248d182478SJoe Perches } 46258905a67cSAndy Whitcroft } 46268905a67cSAndy Whitcroft 46270c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 46283705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 46293705ce5bSJoe Perches if (WARN("SPACING", 46303705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 46313705ce5bSJoe Perches $fix) { 4632194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46333705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 46343705ce5bSJoe Perches } 46350c73b4ebSAndy Whitcroft } 46360c73b4ebSAndy Whitcroft 463731070b5dSJoe Perches# Function pointer declarations 463831070b5dSJoe Perches# check spacing between type, funcptr, and args 463931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 464091f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 464131070b5dSJoe Perches my $declare = $1; 464231070b5dSJoe Perches my $pre_pointer_space = $2; 464331070b5dSJoe Perches my $post_pointer_space = $3; 464431070b5dSJoe Perches my $funcname = $4; 464531070b5dSJoe Perches my $post_funcname_space = $5; 464631070b5dSJoe Perches my $pre_args_space = $6; 464731070b5dSJoe Perches 464891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 464991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 465091f72e9cSJoe Perches# don't need a space so don't warn for those. 465191f72e9cSJoe Perches my $post_declare_space = ""; 465291f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 465391f72e9cSJoe Perches $post_declare_space = $1; 465491f72e9cSJoe Perches $declare = rtrim($declare); 465591f72e9cSJoe Perches } 465691f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 465731070b5dSJoe Perches WARN("SPACING", 465831070b5dSJoe Perches "missing space after return type\n" . $herecurr); 465991f72e9cSJoe Perches $post_declare_space = " "; 466031070b5dSJoe Perches } 466131070b5dSJoe Perches 466231070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 466391f72e9cSJoe Perches# This test is not currently implemented because these declarations are 466491f72e9cSJoe Perches# equivalent to 466591f72e9cSJoe Perches# int foo(int bar, ...) 466691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 466791f72e9cSJoe Perches# 466891f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 466991f72e9cSJoe Perches# WARN("SPACING", 467091f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 467191f72e9cSJoe Perches# } 467231070b5dSJoe Perches 467331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 467431070b5dSJoe Perches if (defined $pre_pointer_space && 467531070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 467631070b5dSJoe Perches WARN("SPACING", 467731070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 467831070b5dSJoe Perches } 467931070b5dSJoe Perches 468031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 468131070b5dSJoe Perches if (defined $post_pointer_space && 468231070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 468331070b5dSJoe Perches WARN("SPACING", 468431070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 468531070b5dSJoe Perches } 468631070b5dSJoe Perches 468731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 468831070b5dSJoe Perches if (defined $post_funcname_space && 468931070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 469031070b5dSJoe Perches WARN("SPACING", 469131070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 469231070b5dSJoe Perches } 469331070b5dSJoe Perches 469431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 469531070b5dSJoe Perches if (defined $pre_args_space && 469631070b5dSJoe Perches $pre_args_space =~ /^\s/) { 469731070b5dSJoe Perches WARN("SPACING", 469831070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 469931070b5dSJoe Perches } 470031070b5dSJoe Perches 470131070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4702194f66fcSJoe Perches $fixed[$fixlinenr] =~ 470391f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 470431070b5dSJoe Perches } 470531070b5dSJoe Perches } 470631070b5dSJoe Perches 47078d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 47088d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4709fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4710fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 47118d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 47128d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 47138d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4714fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 471538dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 47163705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 47173705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 47183705ce5bSJoe Perches $fix) { 4719194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47203705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 47213705ce5bSJoe Perches } 47228d31cfceSAndy Whitcroft } 47238d31cfceSAndy Whitcroft } 47248d31cfceSAndy Whitcroft 4725f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 47266c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4727c2fdda0dSAndy Whitcroft my $name = $1; 4728773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4729773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4730c2fdda0dSAndy Whitcroft 4731c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4732773647a0SAndy Whitcroft if ($name =~ /^(?: 4733773647a0SAndy Whitcroft if|for|while|switch|return|case| 4734773647a0SAndy Whitcroft volatile|__volatile__| 4735773647a0SAndy Whitcroft __attribute__|format|__extension__| 4736773647a0SAndy Whitcroft asm|__asm__)$/x) 4737773647a0SAndy Whitcroft { 4738c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4739c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4740c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4741c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4742773647a0SAndy Whitcroft 4743773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4744c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4745c2fdda0dSAndy Whitcroft 4746c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4747c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4748773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4749c2fdda0dSAndy Whitcroft 4750c2fdda0dSAndy Whitcroft } else { 47513705ce5bSJoe Perches if (WARN("SPACING", 47523705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 47533705ce5bSJoe Perches $fix) { 4754194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47553705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 47563705ce5bSJoe Perches } 4757f0a594c1SAndy Whitcroft } 47586c72ffaaSAndy Whitcroft } 47599a4cad4eSEric Nelson 4760653d4876SAndy Whitcroft# Check operator spacing. 47610a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 47623705ce5bSJoe Perches my $fixed_line = ""; 47633705ce5bSJoe Perches my $line_fixed = 0; 47643705ce5bSJoe Perches 47659c0ca6f9SAndy Whitcroft my $ops = qr{ 47669c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 47679c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 47689c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 47691f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 477084731623SJoe Perches \?:|\?|: 47719c0ca6f9SAndy Whitcroft }x; 4772cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 47733705ce5bSJoe Perches 47743705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 47753705ce5bSJoe Perches## foreach my $el (@elements) { 47763705ce5bSJoe Perches## print("el: <$el>\n"); 47773705ce5bSJoe Perches## } 47783705ce5bSJoe Perches 47793705ce5bSJoe Perches my @fix_elements = (); 478000df344fSAndy Whitcroft my $off = 0; 47816c72ffaaSAndy Whitcroft 47823705ce5bSJoe Perches foreach my $el (@elements) { 47833705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 47843705ce5bSJoe Perches $off += length($el); 47853705ce5bSJoe Perches } 47863705ce5bSJoe Perches 47873705ce5bSJoe Perches $off = 0; 47883705ce5bSJoe Perches 47896c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4790b34c648bSJoe Perches my $last_after = -1; 47916c72ffaaSAndy Whitcroft 47920a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 47933705ce5bSJoe Perches 47943705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 47953705ce5bSJoe Perches 47963705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 47973705ce5bSJoe Perches 47984a0df2efSAndy Whitcroft $off += length($elements[$n]); 47994a0df2efSAndy Whitcroft 480025985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4801773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4802773647a0SAndy Whitcroft my $cc = ''; 4803773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4804773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4805773647a0SAndy Whitcroft } 4806773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4807773647a0SAndy Whitcroft 48084a0df2efSAndy Whitcroft my $a = ''; 48094a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 48104a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4811cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 48124a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 48134a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4814773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 48154a0df2efSAndy Whitcroft 48160a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 48174a0df2efSAndy Whitcroft 48184a0df2efSAndy Whitcroft my $c = ''; 48190a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 48204a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 48214a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4822cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 48234a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 48244a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 48258b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 48264a0df2efSAndy Whitcroft } else { 48274a0df2efSAndy Whitcroft $c = 'E'; 48280a920b5bSAndy Whitcroft } 48290a920b5bSAndy Whitcroft 48304a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 48314a0df2efSAndy Whitcroft 48324a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 48334a0df2efSAndy Whitcroft 48346c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4835de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 48360a920b5bSAndy Whitcroft 483774048ed8SAndy Whitcroft # Pull out the value of this operator. 48386c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 48390a920b5bSAndy Whitcroft 48401f65f947SAndy Whitcroft # Get the full operator variant. 48411f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 48421f65f947SAndy Whitcroft 484313214adfSAndy Whitcroft # Ignore operators passed as parameters. 484413214adfSAndy Whitcroft if ($op_type ne 'V' && 4845d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 484613214adfSAndy Whitcroft 4847cf655043SAndy Whitcroft# # Ignore comments 4848cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 484913214adfSAndy Whitcroft 4850d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 485113214adfSAndy Whitcroft } elsif ($op eq ';') { 4852cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4853cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 48543705ce5bSJoe Perches if (ERROR("SPACING", 48553705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4856b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 48573705ce5bSJoe Perches $line_fixed = 1; 48583705ce5bSJoe Perches } 4859d8aaf121SAndy Whitcroft } 4860d8aaf121SAndy Whitcroft 4861d8aaf121SAndy Whitcroft # // is a comment 4862d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 48630a920b5bSAndy Whitcroft 4864b00e4814SJoe Perches # : when part of a bitfield 4865b00e4814SJoe Perches } elsif ($opv eq ':B') { 4866b00e4814SJoe Perches # skip the bitfield test for now 4867b00e4814SJoe Perches 48681f65f947SAndy Whitcroft # No spaces for: 48691f65f947SAndy Whitcroft # -> 4870b00e4814SJoe Perches } elsif ($op eq '->') { 48714a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 48723705ce5bSJoe Perches if (ERROR("SPACING", 48733705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4874b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 48753705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 48763705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 48773705ce5bSJoe Perches } 4878b34c648bSJoe Perches $line_fixed = 1; 48793705ce5bSJoe Perches } 48800a920b5bSAndy Whitcroft } 48810a920b5bSAndy Whitcroft 48822381097bSJoe Perches # , must not have a space before and must have a space on the right. 48830a920b5bSAndy Whitcroft } elsif ($op eq ',') { 48842381097bSJoe Perches my $rtrim_before = 0; 48852381097bSJoe Perches my $space_after = 0; 48862381097bSJoe Perches if ($ctx =~ /Wx./) { 48872381097bSJoe Perches if (ERROR("SPACING", 48882381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 48892381097bSJoe Perches $line_fixed = 1; 48902381097bSJoe Perches $rtrim_before = 1; 48912381097bSJoe Perches } 48922381097bSJoe Perches } 4893cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 48943705ce5bSJoe Perches if (ERROR("SPACING", 48953705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 48963705ce5bSJoe Perches $line_fixed = 1; 4897b34c648bSJoe Perches $last_after = $n; 48982381097bSJoe Perches $space_after = 1; 48992381097bSJoe Perches } 49002381097bSJoe Perches } 49012381097bSJoe Perches if ($rtrim_before || $space_after) { 49022381097bSJoe Perches if ($rtrim_before) { 49032381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 49042381097bSJoe Perches } else { 49052381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 49062381097bSJoe Perches } 49072381097bSJoe Perches if ($space_after) { 49082381097bSJoe Perches $good .= " "; 49093705ce5bSJoe Perches } 49100a920b5bSAndy Whitcroft } 49110a920b5bSAndy Whitcroft 49129c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 491374048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 49149c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 49159c0ca6f9SAndy Whitcroft 49169c0ca6f9SAndy Whitcroft # unary operators should have a space before and 49179c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 49189c0ca6f9SAndy Whitcroft # unary operator, or a cast 49199c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 492074048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 49210d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4922cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 49233705ce5bSJoe Perches if (ERROR("SPACING", 49243705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4925b34c648bSJoe Perches if ($n != $last_after + 2) { 4926b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 49273705ce5bSJoe Perches $line_fixed = 1; 49283705ce5bSJoe Perches } 49290a920b5bSAndy Whitcroft } 4930b34c648bSJoe Perches } 4931a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4932171ae1a4SAndy Whitcroft # A unary '*' may be const 4933171ae1a4SAndy Whitcroft 4934171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 49353705ce5bSJoe Perches if (ERROR("SPACING", 49363705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4937b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 49383705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 49393705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 49403705ce5bSJoe Perches } 4941b34c648bSJoe Perches $line_fixed = 1; 49423705ce5bSJoe Perches } 49430a920b5bSAndy Whitcroft } 49440a920b5bSAndy Whitcroft 49450a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 49460a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4947773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 49483705ce5bSJoe Perches if (ERROR("SPACING", 49493705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4950b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 49513705ce5bSJoe Perches $line_fixed = 1; 49523705ce5bSJoe Perches } 49530a920b5bSAndy Whitcroft } 4954773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4955773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 49563705ce5bSJoe Perches if (ERROR("SPACING", 49573705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4958b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 49593705ce5bSJoe Perches $line_fixed = 1; 49603705ce5bSJoe Perches } 4961653d4876SAndy Whitcroft } 4962773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 49633705ce5bSJoe Perches if (ERROR("SPACING", 49643705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4965b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 49663705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 49673705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4968773647a0SAndy Whitcroft } 4969b34c648bSJoe Perches $line_fixed = 1; 49703705ce5bSJoe Perches } 49713705ce5bSJoe Perches } 49720a920b5bSAndy Whitcroft 49730a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 49749c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 49759c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 49769c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4977c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4978c2fdda0dSAndy Whitcroft $op eq '%') 49790a920b5bSAndy Whitcroft { 4980d2e025f3SJoe Perches if ($check) { 4981d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4982d2e025f3SJoe Perches if (CHK("SPACING", 4983d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4984d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4985d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4986d2e025f3SJoe Perches $line_fixed = 1; 4987d2e025f3SJoe Perches } 4988d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4989d2e025f3SJoe Perches if (CHK("SPACING", 4990d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4991d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4992d2e025f3SJoe Perches $line_fixed = 1; 4993d2e025f3SJoe Perches } 4994d2e025f3SJoe Perches } 4995d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 49963705ce5bSJoe Perches if (ERROR("SPACING", 49973705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4998b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4999b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 5000b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5001b34c648bSJoe Perches } 50023705ce5bSJoe Perches $line_fixed = 1; 50033705ce5bSJoe Perches } 50040a920b5bSAndy Whitcroft } 50050a920b5bSAndy Whitcroft 50061f65f947SAndy Whitcroft # A colon needs no spaces before when it is 50071f65f947SAndy Whitcroft # terminating a case value or a label. 50081f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 50091f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 50103705ce5bSJoe Perches if (ERROR("SPACING", 50113705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 5012b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 50133705ce5bSJoe Perches $line_fixed = 1; 50143705ce5bSJoe Perches } 50151f65f947SAndy Whitcroft } 50161f65f947SAndy Whitcroft 50170a920b5bSAndy Whitcroft # All the others need spaces both sides. 5018cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 50191f65f947SAndy Whitcroft my $ok = 0; 50201f65f947SAndy Whitcroft 502122f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 50221f65f947SAndy Whitcroft if (($op eq '<' && 50231f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 50241f65f947SAndy Whitcroft ($op eq '>' && 50251f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 50261f65f947SAndy Whitcroft { 50271f65f947SAndy Whitcroft $ok = 1; 50281f65f947SAndy Whitcroft } 50291f65f947SAndy Whitcroft 5030e0df7e1fSJoe Perches # for asm volatile statements 5031e0df7e1fSJoe Perches # ignore a colon with another 5032e0df7e1fSJoe Perches # colon immediately before or after 5033e0df7e1fSJoe Perches if (($op eq ':') && 5034e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 5035e0df7e1fSJoe Perches $ok = 1; 5036e0df7e1fSJoe Perches } 5037e0df7e1fSJoe Perches 503884731623SJoe Perches # messages are ERROR, but ?: are CHK 50391f65f947SAndy Whitcroft if ($ok == 0) { 50400675a8fbSJean Delvare my $msg_level = \&ERROR; 50410675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 504284731623SJoe Perches 50430675a8fbSJean Delvare if (&{$msg_level}("SPACING", 50443705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 5045b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 5046b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 5047b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 5048b34c648bSJoe Perches } 50493705ce5bSJoe Perches $line_fixed = 1; 50503705ce5bSJoe Perches } 50510a920b5bSAndy Whitcroft } 505222f2a2efSAndy Whitcroft } 50534a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 50543705ce5bSJoe Perches 50553705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 50563705ce5bSJoe Perches 50573705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 50580a920b5bSAndy Whitcroft } 50593705ce5bSJoe Perches 50603705ce5bSJoe Perches if (($#elements % 2) == 0) { 50613705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 50623705ce5bSJoe Perches } 50633705ce5bSJoe Perches 5064194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 5065194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 50663705ce5bSJoe Perches } 50673705ce5bSJoe Perches 50683705ce5bSJoe Perches 50690a920b5bSAndy Whitcroft } 50700a920b5bSAndy Whitcroft 5071786b6326SJoe Perches# check for whitespace before a non-naked semicolon 5072d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 5073786b6326SJoe Perches if (WARN("SPACING", 5074786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 5075786b6326SJoe Perches $fix) { 5076194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 5077786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 5078786b6326SJoe Perches } 5079786b6326SJoe Perches } 5080786b6326SJoe Perches 5081f0a594c1SAndy Whitcroft# check for multiple assignments 5082f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 5083000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 5084000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 5085f0a594c1SAndy Whitcroft } 5086f0a594c1SAndy Whitcroft 508722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 508822f2a2efSAndy Whitcroft## # continuation. 508922f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 509022f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 509122f2a2efSAndy Whitcroft## 509222f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 5093e73d2715SDwaipayan Ray## # falsely report the parameters of functions. 509422f2a2efSAndy Whitcroft## my $ln = $line; 509522f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 509622f2a2efSAndy Whitcroft## } 509722f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 5098000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 5099000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 510022f2a2efSAndy Whitcroft## } 510122f2a2efSAndy Whitcroft## } 5102f0a594c1SAndy Whitcroft 51030a920b5bSAndy Whitcroft#need space before brace following if, while, etc 51046b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 51056ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 51063705ce5bSJoe Perches if (ERROR("SPACING", 51073705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 51083705ce5bSJoe Perches $fix) { 51096ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 51103705ce5bSJoe Perches } 5111de7d4f0eSAndy Whitcroft } 5112de7d4f0eSAndy Whitcroft 5113c4a62ef9SJoe Perches## # check for blank lines before declarations 5114c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 5115c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 5116c4a62ef9SJoe Perches## WARN("SPACING", 5117c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 5118c4a62ef9SJoe Perches## } 5119c4a62ef9SJoe Perches## 5120c4a62ef9SJoe Perches 5121de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 5122de7d4f0eSAndy Whitcroft# on the line 512394fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 5124d5e616fcSJoe Perches if (ERROR("SPACING", 5125d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 5126d5e616fcSJoe Perches $fix) { 5127194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5128d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 5129d5e616fcSJoe Perches } 51300a920b5bSAndy Whitcroft } 51310a920b5bSAndy Whitcroft 513222f2a2efSAndy Whitcroft# check spacing on square brackets 513322f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 51343705ce5bSJoe Perches if (ERROR("SPACING", 51353705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 51363705ce5bSJoe Perches $fix) { 5137194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51383705ce5bSJoe Perches s/\[\s+/\[/; 51393705ce5bSJoe Perches } 514022f2a2efSAndy Whitcroft } 514122f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 51423705ce5bSJoe Perches if (ERROR("SPACING", 51433705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 51443705ce5bSJoe Perches $fix) { 5145194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51463705ce5bSJoe Perches s/\s+\]/\]/; 51473705ce5bSJoe Perches } 514822f2a2efSAndy Whitcroft } 514922f2a2efSAndy Whitcroft 5150c45dcabdSAndy Whitcroft# check spacing on parentheses 51519c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 51529c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 51533705ce5bSJoe Perches if (ERROR("SPACING", 51543705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 51553705ce5bSJoe Perches $fix) { 5156194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51573705ce5bSJoe Perches s/\(\s+/\(/; 51583705ce5bSJoe Perches } 515922f2a2efSAndy Whitcroft } 516013214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 5161c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 5162c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 51633705ce5bSJoe Perches if (ERROR("SPACING", 51643705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 51653705ce5bSJoe Perches $fix) { 5166194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51673705ce5bSJoe Perches s/\s+\)/\)/; 51683705ce5bSJoe Perches } 516922f2a2efSAndy Whitcroft } 517022f2a2efSAndy Whitcroft 5171e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 5172e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 5173e2826fd0SJoe Perches 5174e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 5175ea4acbb1SJoe Perches my $var = $1; 5176ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5177ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 5178ea4acbb1SJoe Perches $fix) { 5179ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 5180ea4acbb1SJoe Perches } 5181ea4acbb1SJoe Perches } 5182ea4acbb1SJoe Perches 5183ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 5184ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 5185ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 5186ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 5187ea4acbb1SJoe Perches my $var = $2; 5188ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 5189ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 5190ea4acbb1SJoe Perches $fix) { 5191ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 5192ea4acbb1SJoe Perches $var2 =~ s/\s//g; 5193ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 5194ea4acbb1SJoe Perches } 5195e2826fd0SJoe Perches } 5196e2826fd0SJoe Perches 519763b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 5198a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 5199a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 52005b57980dSJoe Perches $perl_version_ok && defined($stat) && 520163b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 520263b7c73eSJoe Perches my $if_stat = $1; 520363b7c73eSJoe Perches my $test = substr($2, 1, -1); 520463b7c73eSJoe Perches my $herectx; 520563b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 520663b7c73eSJoe Perches my $match = $1; 520763b7c73eSJoe Perches # avoid parentheses around potential macro args 520863b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 520963b7c73eSJoe Perches if (!defined($herectx)) { 521063b7c73eSJoe Perches $herectx = $here . "\n"; 521163b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 521263b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 521363b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 521463b7c73eSJoe Perches $herectx .= $rl . "\n"; 521563b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 521663b7c73eSJoe Perches } 521763b7c73eSJoe Perches } 521863b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 521963b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 522063b7c73eSJoe Perches } 522163b7c73eSJoe Perches } 522263b7c73eSJoe Perches 52230a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 52244a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 52250a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 52263705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 52273705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 52283705ce5bSJoe Perches $fix) { 5229194f66fcSJoe Perches $fixed[$fixlinenr] =~ 52303705ce5bSJoe Perches s/^(.)\s+/$1/; 52313705ce5bSJoe Perches } 52320a920b5bSAndy Whitcroft } 52330a920b5bSAndy Whitcroft 523440873abaSJoe Perches# check if a statement with a comma should be two statements like: 523540873abaSJoe Perches# foo = bar(), /* comma should be semicolon */ 523640873abaSJoe Perches# bar = baz(); 523740873abaSJoe Perches if (defined($stat) && 523840873abaSJoe Perches $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { 523940873abaSJoe Perches my $cnt = statement_rawlines($stat); 524040873abaSJoe Perches my $herectx = get_stat_here($linenr, $cnt, $here); 524140873abaSJoe Perches WARN("SUSPECT_COMMA_SEMICOLON", 524240873abaSJoe Perches "Possible comma where semicolon could be used\n" . $herectx); 524340873abaSJoe Perches } 524440873abaSJoe Perches 52455b9553abSJoe Perches# return is not a function 5246507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 5247c45dcabdSAndy Whitcroft my $spacing = $1; 52485b57980dSJoe Perches if ($perl_version_ok && 52495b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 52505b9553abSJoe Perches my $value = $1; 52515b9553abSJoe Perches $value = deparenthesize($value); 52525b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 5253000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 5254000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 52555b9553abSJoe Perches } 5256c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 5257000d1cc1SJoe Perches ERROR("SPACING", 5258000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 5259c45dcabdSAndy Whitcroft } 5260c45dcabdSAndy Whitcroft } 5261507e5141SJoe Perches 5262b43ae21bSJoe Perches# unnecessary return in a void function 5263b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 5264b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 5265b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 5266b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 5267b43ae21bSJoe Perches $linenr >= 3 && 5268b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 5269b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 52709819cf25SJoe Perches WARN("RETURN_VOID", 5271b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 52729819cf25SJoe Perches } 52739819cf25SJoe Perches 5274189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 52755b57980dSJoe Perches if ($perl_version_ok && 5276189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 5277189248d8SJoe Perches my $openparens = $1; 5278189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 5279189248d8SJoe Perches my $msg = ""; 5280189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 5281189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 5282189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 5283189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 5284189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 5285189248d8SJoe Perches } 5286189248d8SJoe Perches } 5287189248d8SJoe Perches 5288c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 5289c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 5290c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 5291c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 52925b57980dSJoe Perches if ($perl_version_ok && 5293c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 5294c5595fa2SJoe Perches my $lead = $1; 5295c5595fa2SJoe Perches my $const = $2; 5296c5595fa2SJoe Perches my $comp = $3; 5297c5595fa2SJoe Perches my $to = $4; 5298c5595fa2SJoe Perches my $newcomp = $comp; 5299f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 5300c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 5301c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 5302c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 5303c5595fa2SJoe Perches $fix) { 5304c5595fa2SJoe Perches if ($comp eq "<") { 5305c5595fa2SJoe Perches $newcomp = ">"; 5306c5595fa2SJoe Perches } elsif ($comp eq "<=") { 5307c5595fa2SJoe Perches $newcomp = ">="; 5308c5595fa2SJoe Perches } elsif ($comp eq ">") { 5309c5595fa2SJoe Perches $newcomp = "<"; 5310c5595fa2SJoe Perches } elsif ($comp eq ">=") { 5311c5595fa2SJoe Perches $newcomp = "<="; 5312c5595fa2SJoe Perches } 5313c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 5314c5595fa2SJoe Perches } 5315c5595fa2SJoe Perches } 5316c5595fa2SJoe Perches 5317f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 5318f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 531953a3c448SAndy Whitcroft my $name = $1; 532053a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 5321000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 5322f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 532353a3c448SAndy Whitcroft } 532453a3c448SAndy Whitcroft } 5325c45dcabdSAndy Whitcroft 53260a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 53274a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 53283705ce5bSJoe Perches if (ERROR("SPACING", 53293705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 53303705ce5bSJoe Perches $fix) { 5331194f66fcSJoe Perches $fixed[$fixlinenr] =~ 53323705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 53333705ce5bSJoe Perches } 53340a920b5bSAndy Whitcroft } 53350a920b5bSAndy Whitcroft 5336f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 5337f5fe35ddSAndy Whitcroft# statements after the conditional. 5338170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 53393e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 53403e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 53413e469cdcSAndy Whitcroft if (!defined $stat); 5342170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 5343170d3a22SAndy Whitcroft $remain_next, $off_next); 5344170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 5345170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 5346170d3a22SAndy Whitcroft 5347170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 5348170d3a22SAndy Whitcroft # If the statement carries leading newlines, 5349170d3a22SAndy Whitcroft # then count those as offsets. 5350170d3a22SAndy Whitcroft my ($whitespace) = 5351170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 5352170d3a22SAndy Whitcroft my $offset = 5353170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 5354170d3a22SAndy Whitcroft 5355170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 5356170d3a22SAndy Whitcroft $offset} = 1; 5357170d3a22SAndy Whitcroft } 5358170d3a22SAndy Whitcroft } 5359170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 5360c11230f4SJoe Perches defined($stat) && defined($cond) && 5361170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 5362171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 53638905a67cSAndy Whitcroft 5364b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 536565b64b3bSJoe Perches if (ERROR("ASSIGN_IN_IF", 536665b64b3bSJoe Perches "do not use assignment in if condition\n" . $herecurr) && 536765b64b3bSJoe Perches $fix && $perl_version_ok) { 536865b64b3bSJoe Perches if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { 536965b64b3bSJoe Perches my $space = $1; 537065b64b3bSJoe Perches my $not = $2; 537165b64b3bSJoe Perches my $statement = $3; 537265b64b3bSJoe Perches my $assigned = $4; 537365b64b3bSJoe Perches my $test = $8; 537465b64b3bSJoe Perches my $against = $9; 537565b64b3bSJoe Perches my $brace = $15; 537665b64b3bSJoe Perches fix_delete_line($fixlinenr, $rawline); 537765b64b3bSJoe Perches fix_insert_line($fixlinenr, "$space$statement;"); 537865b64b3bSJoe Perches my $newline = "${space}if ("; 537965b64b3bSJoe Perches $newline .= '!' if defined($not); 538065b64b3bSJoe Perches $newline .= '(' if (defined $not && defined($test) && defined($against)); 538165b64b3bSJoe Perches $newline .= "$assigned"; 538265b64b3bSJoe Perches $newline .= " $test $against" if (defined($test) && defined($against)); 538365b64b3bSJoe Perches $newline .= ')' if (defined $not && defined($test) && defined($against)); 538465b64b3bSJoe Perches $newline .= ')'; 538565b64b3bSJoe Perches $newline .= " {" if (defined($brace)); 538665b64b3bSJoe Perches fix_insert_line($fixlinenr + 1, $newline); 538765b64b3bSJoe Perches } 538865b64b3bSJoe Perches } 53898905a67cSAndy Whitcroft } 53908905a67cSAndy Whitcroft 53918905a67cSAndy Whitcroft # Find out what is on the end of the line after the 53928905a67cSAndy Whitcroft # conditional. 5393773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 53948905a67cSAndy Whitcroft $s =~ s/\n.*//g; 539513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 539653210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 539753210168SAndy Whitcroft $c !~ /}\s*while\s*/) 5398773647a0SAndy Whitcroft { 5399bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 5400bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 5401bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 540242bdf74cSHidetoshi Seto my $stat_real = ''; 5403bb44ad39SAndy Whitcroft 540442bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 540542bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 5406bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 5407bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 5408bb44ad39SAndy Whitcroft } 5409bb44ad39SAndy Whitcroft 5410000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5411000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 54128905a67cSAndy Whitcroft } 54138905a67cSAndy Whitcroft } 54148905a67cSAndy Whitcroft 541513214adfSAndy Whitcroft# Check for bitwise tests written as boolean 541613214adfSAndy Whitcroft if ($line =~ / 541713214adfSAndy Whitcroft (?: 541813214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 541913214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 542013214adfSAndy Whitcroft (?:\&\&|\|\|) 542113214adfSAndy Whitcroft | 542213214adfSAndy Whitcroft (?:\&\&|\|\|) 542313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 542413214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 542513214adfSAndy Whitcroft )/x) 542613214adfSAndy Whitcroft { 5427000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 5428000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 542913214adfSAndy Whitcroft } 543013214adfSAndy Whitcroft 54318905a67cSAndy Whitcroft# if and else should not have general statements after it 543213214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 543313214adfSAndy Whitcroft my $s = $1; 543413214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 543513214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5436000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5437000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 54380a920b5bSAndy Whitcroft } 543913214adfSAndy Whitcroft } 544039667782SAndy Whitcroft# if should not continue a brace 544139667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5442000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5443048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 544439667782SAndy Whitcroft $herecurr); 544539667782SAndy Whitcroft } 5446a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5447a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5448a1080bf8SAndy Whitcroft $line !~ /\G(?: 54493fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5450a1080bf8SAndy Whitcroft \s*return\s+ 5451a1080bf8SAndy Whitcroft )/xg) 5452a1080bf8SAndy Whitcroft { 5453000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5454000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5455a1080bf8SAndy Whitcroft } 54560a920b5bSAndy Whitcroft 54570a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 54580a920b5bSAndy Whitcroft # indent level to be relevant to each other. 54598b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 54600a920b5bSAndy Whitcroft $previndent == $indent) { 54618b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 54628b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 54638b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 54648b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 54658b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 54668b8856f4SJoe Perches my $fixedline = $prevrawline; 54678b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 54688b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 54698b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 54708b8856f4SJoe Perches } 54718b8856f4SJoe Perches $fixedline = $rawline; 54728b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 54738b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 54748b8856f4SJoe Perches } 54750a920b5bSAndy Whitcroft } 54760a920b5bSAndy Whitcroft 54778b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5478c2fdda0dSAndy Whitcroft $previndent == $indent) { 5479c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5480c2fdda0dSAndy Whitcroft 5481c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5482c2fdda0dSAndy Whitcroft # conditional. 5483773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5484c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5485c2fdda0dSAndy Whitcroft 5486c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 54878b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 54888b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 54898b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 54908b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 54918b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 54928b8856f4SJoe Perches my $fixedline = $prevrawline; 54938b8856f4SJoe Perches my $trailing = $rawline; 54948b8856f4SJoe Perches $trailing =~ s/^\+//; 54958b8856f4SJoe Perches $trailing = trim($trailing); 54968b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 54978b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 54988b8856f4SJoe Perches } 5499c2fdda0dSAndy Whitcroft } 5500c2fdda0dSAndy Whitcroft } 5501c2fdda0dSAndy Whitcroft 550295e2c602SJoe Perches#Specific variable tests 5503323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5504323c1260SJoe Perches my $var = $1; 550595e2c602SJoe Perches 550695e2c602SJoe Perches#CamelCase 5507807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5508be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 55094104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values 55104104a206SŁukasz Stelmach $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ && 551122735ce8SJoe Perches#Ignore Page<foo> variants 5512807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5513d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5514d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5515d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5516f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5517f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 55187e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 55197e781f67SJoe Perches my $word = $1; 55207e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5521d8b07710SJoe Perches if ($check) { 5522d8b07710SJoe Perches seed_camelcase_includes(); 5523d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5524d8b07710SJoe Perches seed_camelcase_file($realfile); 5525d8b07710SJoe Perches $camelcase_file_seeded = 1; 5526d8b07710SJoe Perches } 5527d8b07710SJoe Perches } 55287e781f67SJoe Perches if (!defined $camelcase{$word}) { 55297e781f67SJoe Perches $camelcase{$word} = 1; 5530be79794bSJoe Perches CHK("CAMELCASE", 55317e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 55327e781f67SJoe Perches } 5533323c1260SJoe Perches } 5534323c1260SJoe Perches } 55353445686aSJoe Perches } 55360a920b5bSAndy Whitcroft 55370a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5538d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5539d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5540d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5541d5e616fcSJoe Perches $fix) { 5542194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5543d5e616fcSJoe Perches } 55440a920b5bSAndy Whitcroft } 55450a920b5bSAndy Whitcroft 55460e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 55470e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5548c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5549e09dec48SAndy Whitcroft my $file = "$1.h"; 5550e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5551e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5552e09dec48SAndy Whitcroft $realfile ne $checkfile && 55537840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5554c45dcabdSAndy Whitcroft { 55550e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 55560e212e0aSFabian Frederick if ($asminclude > 0) { 5557e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5558000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5559000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5560e09dec48SAndy Whitcroft } else { 5561000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5562000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5563e09dec48SAndy Whitcroft } 55640a920b5bSAndy Whitcroft } 55650a920b5bSAndy Whitcroft } 55660e212e0aSFabian Frederick } 55670a920b5bSAndy Whitcroft 5568653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5569653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5570cf655043SAndy Whitcroft# in a known good container 5571b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5572b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5573d8aaf121SAndy Whitcroft my $ln = $linenr; 5574d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5575c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5576c45dcabdSAndy Whitcroft my $ctx = ''; 557708a2843eSJoe Perches my $has_flow_statement = 0; 557808a2843eSJoe Perches my $has_arg_concat = 0; 5579c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5580f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5581f74bd194SAndy Whitcroft $ctx = $dstat; 5582c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5583a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5584c45dcabdSAndy Whitcroft 558508a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 558662e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 558708a2843eSJoe Perches 5588f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5589f59b64bfSJoe Perches my $define_args = $1; 5590f59b64bfSJoe Perches my $define_stmt = $dstat; 5591f59b64bfSJoe Perches my @def_args = (); 5592f59b64bfSJoe Perches 5593f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5594f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5595f59b64bfSJoe Perches $define_args =~ s/\s*//g; 55968c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5597f59b64bfSJoe Perches @def_args = split(",", $define_args); 5598f59b64bfSJoe Perches } 5599f59b64bfSJoe Perches 5600292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5601c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5602c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5603c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5604c45dcabdSAndy Whitcroft 5605c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 56062e44e803SDwaipayan Ray while ($dstat =~ s/\([^\(\)]*\)/1u/ || 56072e44e803SDwaipayan Ray $dstat =~ s/\{[^\{\}]*\}/1u/ || 56082e44e803SDwaipayan Ray $dstat =~ s/.\[[^\[\]]*\]/1u/) 5609bf30d6edSAndy Whitcroft { 5610c45dcabdSAndy Whitcroft } 5611c45dcabdSAndy Whitcroft 5612342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 561333acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 561433acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5615e45bab8eSAndy Whitcroft { 5616e45bab8eSAndy Whitcroft } 5617e45bab8eSAndy Whitcroft 561842e15293SJoe Perches # Make asm volatile uses seem like a generic function 561942e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 562042e15293SJoe Perches 5621c45dcabdSAndy Whitcroft my $exceptions = qr{ 5622c45dcabdSAndy Whitcroft $Declare| 5623c45dcabdSAndy Whitcroft module_param_named| 5624a0a0a7a9SKees Cook MODULE_PARM_DESC| 5625c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5626c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5627383099fdSAndy Whitcroft __typeof__\(| 562822fd2d3eSStefani Seibold union| 562922fd2d3eSStefani Seibold struct| 5630ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 56316b10df42SVladimir Zapolskiy ^\"|\"$| 56326b10df42SVladimir Zapolskiy ^\[ 5633c45dcabdSAndy Whitcroft }x; 56345eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5635f59b64bfSJoe Perches 5636f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5637f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5638e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5639f59b64bfSJoe Perches 5640f74bd194SAndy Whitcroft if ($dstat ne '' && 5641f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5642f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 56433cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5644356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5645f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5646f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5647e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 564872f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 56492e44e803SDwaipayan Ray $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} 5650f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5651f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5652f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 56534e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5654f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5655c45dcabdSAndy Whitcroft { 5656e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5657e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5658e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5659e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5660f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5661f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5662f74bd194SAndy Whitcroft } else { 5663000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5664388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5665d8aaf121SAndy Whitcroft } 5666f59b64bfSJoe Perches 5667f59b64bfSJoe Perches } 56685207649bSJoe Perches 56695207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 56705207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 56715207649bSJoe Perches my $first = 1; 56725207649bSJoe Perches $define_stmt = ""; 56735207649bSJoe Perches foreach my $l (@stmt_array) { 56745207649bSJoe Perches $l =~ s/\\$//; 56755207649bSJoe Perches if ($first) { 56765207649bSJoe Perches $define_stmt = $l; 56775207649bSJoe Perches $first = 0; 56785207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 56795207649bSJoe Perches $define_stmt .= substr($l, 1); 56805207649bSJoe Perches } 56815207649bSJoe Perches } 56825207649bSJoe Perches $define_stmt =~ s/$;//g; 56835207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 56845207649bSJoe Perches $define_stmt = trim($define_stmt); 56855207649bSJoe Perches 5686f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5687f59b64bfSJoe Perches foreach my $arg (@def_args) { 5688f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 56899192d41aSJoe Perches next if ($arg =~ /^type$/i); 56907fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 56916dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 56927fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 56937fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5694d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5695f59b64bfSJoe Perches if ($use_cnt > 1) { 5696f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5697f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5698f59b64bfSJoe Perches } 56999192d41aSJoe Perches# check if any macro arguments may have other precedence issues 57007fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 57019192d41aSJoe Perches ((defined($1) && $1 ne ',') || 57029192d41aSJoe Perches (defined($2) && $2 ne ','))) { 57039192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 57049192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 57059192d41aSJoe Perches } 57060a920b5bSAndy Whitcroft } 57075023d347SJoe Perches 570808a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 570908a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 571008a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 571108a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5712e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 571308a2843eSJoe Perches 571408a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 571508a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 571608a2843eSJoe Perches } 571708a2843eSJoe Perches 5718481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 57195023d347SJoe Perches 57205023d347SJoe Perches } else { 57215023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5722481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5723481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 57245023d347SJoe Perches $line =~ /^\+.*\\$/) { 57255023d347SJoe Perches WARN("LINE_CONTINUATIONS", 57265023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 57275023d347SJoe Perches } 5728653d4876SAndy Whitcroft } 57290a920b5bSAndy Whitcroft 5730b13edf7fSJoe Perches# do {} while (0) macro tests: 5731b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5732b13edf7fSJoe Perches# macro should not end with a semicolon 57335b57980dSJoe Perches if ($perl_version_ok && 5734b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5735b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5736b13edf7fSJoe Perches my $ln = $linenr; 5737b13edf7fSJoe Perches my $cnt = $realcnt; 5738b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5739b13edf7fSJoe Perches my $ctx = ''; 5740b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5741b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5742b13edf7fSJoe Perches $ctx = $dstat; 5743b13edf7fSJoe Perches 5744b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 57451b36b201SJoe Perches $dstat =~ s/$;/ /g; 5746b13edf7fSJoe Perches 5747b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5748b13edf7fSJoe Perches my $stmts = $2; 5749b13edf7fSJoe Perches my $semis = $3; 5750b13edf7fSJoe Perches 5751b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5752b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5753e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5754b13edf7fSJoe Perches 5755ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5756ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5757b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5758b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5759b13edf7fSJoe Perches } 5760b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5761b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5762b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5763b13edf7fSJoe Perches } 5764f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5765f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5766f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5767e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5768f5ef95b1SJoe Perches 5769f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5770f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5771b13edf7fSJoe Perches } 5772b13edf7fSJoe Perches } 5773b13edf7fSJoe Perches 5774f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 577513214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 577613214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5777cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 577813214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5779cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5780cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5781aad4f614SJoe Perches my @allowed = (); 5782aad4f614SJoe Perches my $allow = 0; 578313214adfSAndy Whitcroft my $seen = 0; 5784773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5785cf655043SAndy Whitcroft my $ln = $linenr - 1; 578613214adfSAndy Whitcroft for my $chunk (@chunks) { 578713214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 578813214adfSAndy Whitcroft 5789773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5790773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5791773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5792773647a0SAndy Whitcroft 5793aad4f614SJoe Perches $allowed[$allow] = 0; 5794773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5795773647a0SAndy Whitcroft 5796773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5797773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5798773647a0SAndy Whitcroft 5799773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5800cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5801cf655043SAndy Whitcroft 5802773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 580313214adfSAndy Whitcroft 580413214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 580513214adfSAndy Whitcroft 5806aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5807cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5808cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5809aad4f614SJoe Perches $allowed[$allow] = 1; 581013214adfSAndy Whitcroft } 581113214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5812cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5813aad4f614SJoe Perches $allowed[$allow] = 1; 581413214adfSAndy Whitcroft } 5815cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5816cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5817aad4f614SJoe Perches $allowed[$allow] = 1; 581813214adfSAndy Whitcroft } 5819aad4f614SJoe Perches $allow++; 582013214adfSAndy Whitcroft } 5821aad4f614SJoe Perches if ($seen) { 5822aad4f614SJoe Perches my $sum_allowed = 0; 5823aad4f614SJoe Perches foreach (@allowed) { 5824aad4f614SJoe Perches $sum_allowed += $_; 5825aad4f614SJoe Perches } 5826aad4f614SJoe Perches if ($sum_allowed == 0) { 5827000d1cc1SJoe Perches WARN("BRACES", 5828000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5829aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5830aad4f614SJoe Perches $seen != $allow) { 5831aad4f614SJoe Perches CHK("BRACES", 5832aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5833aad4f614SJoe Perches } 583413214adfSAndy Whitcroft } 583513214adfSAndy Whitcroft } 583613214adfSAndy Whitcroft } 5837773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 583813214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5839cf655043SAndy Whitcroft my $allowed = 0; 5840f0a594c1SAndy Whitcroft 5841cf655043SAndy Whitcroft # Check the pre-context. 5842cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5843cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5844cf655043SAndy Whitcroft $allowed = 1; 5845f0a594c1SAndy Whitcroft } 5846773647a0SAndy Whitcroft 5847773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5848773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5849773647a0SAndy Whitcroft 5850cf655043SAndy Whitcroft # Check the condition. 5851cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5852773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5853cf655043SAndy Whitcroft if (defined $cond) { 5854773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5855cf655043SAndy Whitcroft } 5856cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5857cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5858cf655043SAndy Whitcroft $allowed = 1; 5859cf655043SAndy Whitcroft } 5860cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5861cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5862cf655043SAndy Whitcroft $allowed = 1; 5863cf655043SAndy Whitcroft } 5864cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5865cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5866cf655043SAndy Whitcroft $allowed = 1; 5867cf655043SAndy Whitcroft } 5868cf655043SAndy Whitcroft # Check the post-context. 5869cf655043SAndy Whitcroft if (defined $chunks[1]) { 5870cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5871cf655043SAndy Whitcroft if (defined $cond) { 5872773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5873cf655043SAndy Whitcroft } 5874cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5875cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5876cf655043SAndy Whitcroft $allowed = 1; 5877cf655043SAndy Whitcroft } 5878cf655043SAndy Whitcroft } 5879cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5880f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5881e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5882cf655043SAndy Whitcroft 5883000d1cc1SJoe Perches WARN("BRACES", 5884000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5885f0a594c1SAndy Whitcroft } 5886f0a594c1SAndy Whitcroft } 5887f0a594c1SAndy Whitcroft 5888e4c5babdSJoe Perches# check for single line unbalanced braces 588995330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 589095330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5891e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5892e4c5babdSJoe Perches } 5893e4c5babdSJoe Perches 58940979ae66SJoe Perches# check for unnecessary blank lines around braces 589577b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5896f8e58219SJoe Perches if (CHK("BRACES", 5897f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5898f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5899f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5900f8e58219SJoe Perches } 59010979ae66SJoe Perches } 590277b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5903f8e58219SJoe Perches if (CHK("BRACES", 5904f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5905f8e58219SJoe Perches $fix) { 5906f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5907f8e58219SJoe Perches } 59080979ae66SJoe Perches } 59090979ae66SJoe Perches 59104a0df2efSAndy Whitcroft# no volatiles please 59116c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 59126c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5913000d1cc1SJoe Perches WARN("VOLATILE", 59148c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 59154a0df2efSAndy Whitcroft } 59164a0df2efSAndy Whitcroft 59175e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 59185e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 59195e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 59205e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 592133acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 59225e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 59235e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 59245e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 59255e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 59265e4f6ba5SJoe Perches $fix && 59275e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 59285e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 59295e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 59305e4f6ba5SJoe Perches my $comma_close = ""; 59315e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 59325e4f6ba5SJoe Perches $comma_close = $1; 59335e4f6ba5SJoe Perches } 59345e4f6ba5SJoe Perches 59355e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 59365e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 59375e4f6ba5SJoe Perches my $fixedline = $prevrawline; 59385e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 59395e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 59405e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 59415e4f6ba5SJoe Perches $fixedline = $rawline; 59425e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 59435e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 59445e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 59455e4f6ba5SJoe Perches } 59465e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 59475e4f6ba5SJoe Perches } 59485e4f6ba5SJoe Perches } 59495e4f6ba5SJoe Perches 59505e4f6ba5SJoe Perches# check for missing a space in a string concatenation 59515e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 59525e4f6ba5SJoe Perches WARN('MISSING_SPACE', 59535e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 59545e4f6ba5SJoe Perches } 59555e4f6ba5SJoe Perches 595677cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5957e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5958e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5959e4b7d309SJoe Perches# function declarations 596077cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 596177cb8546SJoe Perches defined($context_function) && 5962e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5963e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 596477cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5965e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 596677cb8546SJoe Perches } 596777cb8546SJoe Perches 59685e4f6ba5SJoe Perches# check for spaces before a quoted newline 59695e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 59705e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 59715e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 59725e4f6ba5SJoe Perches $fix) { 59735e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 59745e4f6ba5SJoe Perches } 59755e4f6ba5SJoe Perches 59765e4f6ba5SJoe Perches } 59775e4f6ba5SJoe Perches 5978f17dba4fSJoe Perches# concatenated string without spaces between elements 597979682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 598079682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 598179682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 598279682c0cSJoe Perches $fix) { 598379682c0cSJoe Perches while ($line =~ /($String)/g) { 598479682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 598579682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 598679682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 598779682c0cSJoe Perches } 598879682c0cSJoe Perches } 5989f17dba4fSJoe Perches } 5990f17dba4fSJoe Perches 599190ad30e5SJoe Perches# uncoalesced string fragments 599233acb54aSJoe Perches if ($line =~ /$String\s*"/) { 599379682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 599479682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 599579682c0cSJoe Perches $fix) { 599679682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 599779682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 599879682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 599979682c0cSJoe Perches } 600079682c0cSJoe Perches } 600190ad30e5SJoe Perches } 600290ad30e5SJoe Perches 6003522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 6004522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 6005522b837cSAlexey Dobriyan my $show_Z = 1; 60065e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 6007522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 60085e4f6ba5SJoe Perches $string =~ s/%%/__/g; 6009522b837cSAlexey Dobriyan # check for %L 6010522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 60115e4f6ba5SJoe Perches WARN("PRINTF_L", 6012522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 6013522b837cSAlexey Dobriyan $show_L = 0; 60145e4f6ba5SJoe Perches } 6015522b837cSAlexey Dobriyan # check for %Z 6016522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 6017522b837cSAlexey Dobriyan WARN("PRINTF_Z", 6018522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 6019522b837cSAlexey Dobriyan $show_Z = 0; 6020522b837cSAlexey Dobriyan } 6021522b837cSAlexey Dobriyan # check for 0x<decimal> 6022522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 6023522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 60246e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 60256e300757SJoe Perches } 60265e4f6ba5SJoe Perches } 60275e4f6ba5SJoe Perches 60285e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 60293f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 60305e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 60315e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 60325e4f6ba5SJoe Perches } 60335e4f6ba5SJoe Perches 603400df344fSAndy Whitcroft# warn about #if 0 6035c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 603660f89010SPrakruthi Deepak Heragu WARN("IF_0", 603760f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 603860f89010SPrakruthi Deepak Heragu } 603960f89010SPrakruthi Deepak Heragu 604060f89010SPrakruthi Deepak Heragu# warn about #if 1 604160f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 604260f89010SPrakruthi Deepak Heragu WARN("IF_1", 604360f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 60444a0df2efSAndy Whitcroft } 60454a0df2efSAndy Whitcroft 604603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 604703df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 6048100425deSJoe Perches my $tested = quotemeta($1); 6049100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 6050100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 6051100425deSJoe Perches my $func = $1; 6052100425deSJoe Perches if (WARN('NEEDLESS_IF', 6053100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 6054100425deSJoe Perches $fix) { 6055100425deSJoe Perches my $do_fix = 1; 6056100425deSJoe Perches my $leading_tabs = ""; 6057100425deSJoe Perches my $new_leading_tabs = ""; 6058100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 6059100425deSJoe Perches $leading_tabs = $1; 6060100425deSJoe Perches } else { 6061100425deSJoe Perches $do_fix = 0; 6062100425deSJoe Perches } 6063100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 6064100425deSJoe Perches $new_leading_tabs = $1; 6065100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 6066100425deSJoe Perches $do_fix = 0; 6067100425deSJoe Perches } 6068100425deSJoe Perches } else { 6069100425deSJoe Perches $do_fix = 0; 6070100425deSJoe Perches } 6071100425deSJoe Perches if ($do_fix) { 6072100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 6073100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 6074100425deSJoe Perches } 6075100425deSJoe Perches } 60764c432a8fSGreg Kroah-Hartman } 60774c432a8fSGreg Kroah-Hartman } 6078f0a594c1SAndy Whitcroft 6079ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 6080ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 6081ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 6082ebfdc409SJoe Perches (defined $1 || defined $3) && 6083ebfdc409SJoe Perches $linenr > 3) { 6084ebfdc409SJoe Perches my $testval = $2; 6085ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 6086ebfdc409SJoe Perches 6087ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 6088ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 6089ebfdc409SJoe Perches 6090e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 6091e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 6092ebfdc409SJoe Perches WARN("OOM_MESSAGE", 6093ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 6094ebfdc409SJoe Perches } 6095ebfdc409SJoe Perches } 6096ebfdc409SJoe Perches 6097f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 6098dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 6099f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 6100f78d98f6SJoe Perches my $level = $1; 6101f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 6102f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 6103f78d98f6SJoe Perches $fix) { 6104f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 6105f78d98f6SJoe Perches } 6106f78d98f6SJoe Perches } 6107f78d98f6SJoe Perches 610845c55e92SJoe Perches# check for logging continuations 610945c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 611045c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 611145c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 611245c55e92SJoe Perches } 611345c55e92SJoe Perches 611470eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions 611570eb2275SDwaipayan Ray if (defined $stat && 611670eb2275SDwaipayan Ray $line =~ /\b$logFunctions\s*\(/ && 611770eb2275SDwaipayan Ray index($stat, '"') >= 0) { 611870eb2275SDwaipayan Ray my $lc = $stat =~ tr@\n@@; 611970eb2275SDwaipayan Ray $lc = $lc + $linenr; 612070eb2275SDwaipayan Ray my $stat_real = get_stat_real($linenr, $lc); 612170eb2275SDwaipayan Ray pos($stat_real) = index($stat_real, '"'); 612270eb2275SDwaipayan Ray while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { 612370eb2275SDwaipayan Ray my $pspec = $1; 612470eb2275SDwaipayan Ray my $h = $2; 612570eb2275SDwaipayan Ray my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; 612670eb2275SDwaipayan Ray if (WARN("UNNECESSARY_MODIFIER", 612770eb2275SDwaipayan Ray "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && 612870eb2275SDwaipayan Ray $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { 612970eb2275SDwaipayan Ray my $nspec = $pspec; 613070eb2275SDwaipayan Ray $nspec =~ s/h//g; 613170eb2275SDwaipayan Ray $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; 613270eb2275SDwaipayan Ray } 613370eb2275SDwaipayan Ray } 613470eb2275SDwaipayan Ray } 613570eb2275SDwaipayan Ray 6136abb08a53SJoe Perches# check for mask then right shift without a parentheses 61375b57980dSJoe Perches if ($perl_version_ok && 6138abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 6139abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 6140abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 6141abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 6142abb08a53SJoe Perches } 6143abb08a53SJoe Perches 6144b75ac618SJoe Perches# check for pointer comparisons to NULL 61455b57980dSJoe Perches if ($perl_version_ok) { 6146b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 6147b75ac618SJoe Perches my $val = $1; 6148b75ac618SJoe Perches my $equal = "!"; 6149b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 6150b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 6151b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 6152b75ac618SJoe Perches $fix) { 6153b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 6154b75ac618SJoe Perches } 6155b75ac618SJoe Perches } 6156b75ac618SJoe Perches } 6157b75ac618SJoe Perches 61588716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 61598716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 61608716de38SJoe Perches my $attr = $1; 61618716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 61628716de38SJoe Perches my $ptr = $1; 61638716de38SJoe Perches my $var = $2; 61648716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 61658716de38SJoe Perches ERROR("MISPLACED_INIT", 61668716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 61678716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 61688716de38SJoe Perches WARN("MISPLACED_INIT", 61698716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 61708716de38SJoe Perches $fix) { 6171194f66fcSJoe 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; 61728716de38SJoe Perches } 61738716de38SJoe Perches } 61748716de38SJoe Perches } 61758716de38SJoe Perches 6176e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 6177e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 6178e970b884SJoe Perches my $attr = $1; 6179e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 6180e970b884SJoe Perches my $attr_prefix = $1; 6181e970b884SJoe Perches my $attr_type = $2; 6182e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 6183e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 6184e970b884SJoe Perches $fix) { 6185194f66fcSJoe Perches $fixed[$fixlinenr] =~ 6186e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 6187e970b884SJoe Perches } 6188e970b884SJoe Perches } 6189e970b884SJoe Perches 6190e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 6191e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 6192e970b884SJoe Perches my $attr = $1; 6193e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 6194e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 6195e970b884SJoe Perches $fix) { 6196194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 6197e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 6198e970b884SJoe Perches $lead = rtrim($1); 6199e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 6200e970b884SJoe Perches $lead = "${lead}const "; 6201194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 6202e970b884SJoe Perches } 6203e970b884SJoe Perches } 6204e970b884SJoe Perches 6205c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 6206c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 6207c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 6208c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 6209c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 6210c17893c7SJoe Perches $fix) { 6211c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 6212c17893c7SJoe Perches } 6213c17893c7SJoe Perches } 6214c17893c7SJoe Perches 6215fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 6216fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 6217fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 6218fbdb8138SJoe Perches my $constant_func = $1; 6219fbdb8138SJoe Perches my $func = $constant_func; 6220fbdb8138SJoe Perches $func =~ s/^__constant_//; 6221fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 6222fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 6223fbdb8138SJoe Perches $fix) { 6224194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 6225fbdb8138SJoe Perches } 6226fbdb8138SJoe Perches } 6227fbdb8138SJoe Perches 62281a15a250SPatrick Pannuto# prefer usleep_range over udelay 622937581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 623043c1d77cSJoe Perches my $delay = $1; 62311a15a250SPatrick Pannuto # ignore udelay's < 10, however 623243c1d77cSJoe Perches if (! ($delay < 10) ) { 6233000d1cc1SJoe Perches CHK("USLEEP_RANGE", 6234458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 623543c1d77cSJoe Perches } 623643c1d77cSJoe Perches if ($delay > 2000) { 623743c1d77cSJoe Perches WARN("LONG_UDELAY", 623843c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 62391a15a250SPatrick Pannuto } 62401a15a250SPatrick Pannuto } 62411a15a250SPatrick Pannuto 624209ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 624309ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 624409ef8725SPatrick Pannuto if ($1 < 20) { 6245000d1cc1SJoe Perches WARN("MSLEEP", 6246458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 624709ef8725SPatrick Pannuto } 624809ef8725SPatrick Pannuto } 624909ef8725SPatrick Pannuto 625036ec1939SJoe Perches# check for comparisons of jiffies 625136ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 625236ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 625336ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 625436ec1939SJoe Perches } 625536ec1939SJoe Perches 62569d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 62579d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 62589d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 62599d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 62609d7a34a5SJoe Perches } 62619d7a34a5SJoe Perches 626200df344fSAndy Whitcroft# warn about #ifdefs in C files 6263c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 626400df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 626500df344fSAndy Whitcroft# print "$herecurr"; 626600df344fSAndy Whitcroft# $clean = 0; 626700df344fSAndy Whitcroft# } 626800df344fSAndy Whitcroft 626922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 6270c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 62713705ce5bSJoe Perches if (ERROR("SPACING", 62723705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 62733705ce5bSJoe Perches $fix) { 6274194f66fcSJoe Perches $fixed[$fixlinenr] =~ 62753705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 62763705ce5bSJoe Perches } 62773705ce5bSJoe Perches 627822f2a2efSAndy Whitcroft } 627922f2a2efSAndy Whitcroft 62804a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 6281171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 6282171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 62834a0df2efSAndy Whitcroft my $which = $1; 62844a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6285000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 6286000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 62874a0df2efSAndy Whitcroft } 62884a0df2efSAndy Whitcroft } 62894a0df2efSAndy Whitcroft# check for memory barriers without a comment. 6290402c2553SMichael S. Tsirkin 6291402c2553SMichael S. Tsirkin my $barriers = qr{ 6292402c2553SMichael S. Tsirkin mb| 6293402c2553SMichael S. Tsirkin rmb| 6294ad83ec6cSWill Deacon wmb 6295402c2553SMichael S. Tsirkin }x; 6296402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 6297402c2553SMichael S. Tsirkin mb__before_atomic| 6298402c2553SMichael S. Tsirkin mb__after_atomic| 6299402c2553SMichael S. Tsirkin store_release| 6300402c2553SMichael S. Tsirkin load_acquire| 6301402c2553SMichael S. Tsirkin store_mb| 6302402c2553SMichael S. Tsirkin (?:$barriers) 6303402c2553SMichael S. Tsirkin }x; 6304402c2553SMichael S. Tsirkin my $all_barriers = qr{ 6305402c2553SMichael S. Tsirkin (?:$barriers)| 630643e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 630743e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 6308402c2553SMichael S. Tsirkin }x; 6309402c2553SMichael S. Tsirkin 6310402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 63114a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6312c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 6313000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 63144a0df2efSAndy Whitcroft } 63154a0df2efSAndy Whitcroft } 63163ad81779SPaul E. McKenney 6317f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 6318f4073b0fSMichael S. Tsirkin 6319f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 6320f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 6321f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 6322f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 6323f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 6324f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 6325f4073b0fSMichael S. Tsirkin } 6326f4073b0fSMichael S. Tsirkin 6327cb426e99SJoe Perches# check for waitqueue_active without a comment. 6328cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 6329cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 6330cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 6331cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 6332cb426e99SJoe Perches } 6333cb426e99SJoe Perches } 63343ad81779SPaul E. McKenney 63355099a722SMarco Elver# check for data_race without a comment. 63365099a722SMarco Elver if ($line =~ /\bdata_race\s*\(/) { 63375099a722SMarco Elver if (!ctx_has_comment($first_line, $linenr)) { 63385099a722SMarco Elver WARN("DATA_RACE", 63395099a722SMarco Elver "data_race without comment\n" . $herecurr); 63405099a722SMarco Elver } 63415099a722SMarco Elver } 63425099a722SMarco Elver 63434a0df2efSAndy Whitcroft# check of hardware specific defines 6344c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 6345000d1cc1SJoe Perches CHK("ARCH_DEFINES", 6346000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 63470a920b5bSAndy Whitcroft } 6348653d4876SAndy Whitcroft 6349596ed45bSJoe Perches# check that the storage class is not after a type 6350596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 6351000d1cc1SJoe Perches WARN("STORAGE_CLASS", 6352596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 6353596ed45bSJoe Perches } 6354596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 6355596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 6356596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 6357596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 6358596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 6359596ed45bSJoe Perches WARN("STORAGE_CLASS", 6360596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 6361d4977c78STobias Klauser } 6362d4977c78STobias Klauser 6363de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 6364de7d4f0eSAndy Whitcroft# storage class and type. 63659c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 63669c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 6367000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 6368000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 6369de7d4f0eSAndy Whitcroft } 6370de7d4f0eSAndy Whitcroft 63718905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 63722b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 63732b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 6374d5e616fcSJoe Perches if (WARN("INLINE", 6375d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 6376d5e616fcSJoe Perches $fix) { 6377194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 6378d5e616fcSJoe Perches 6379d5e616fcSJoe Perches } 63808905a67cSAndy Whitcroft } 63818905a67cSAndy Whitcroft 63827ebe1d17SDwaipayan Ray# Check for compiler attributes 63832b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 63847ebe1d17SDwaipayan Ray $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) { 63857ebe1d17SDwaipayan Ray my $attr = $1; 63867ebe1d17SDwaipayan Ray $attr =~ s/\s*\(\s*(.*)\)\s*/$1/; 63877ebe1d17SDwaipayan Ray 63887ebe1d17SDwaipayan Ray my %attr_list = ( 63890830aab0SJoe Perches "alias" => "__alias", 63907ebe1d17SDwaipayan Ray "aligned" => "__aligned", 63917ebe1d17SDwaipayan Ray "always_inline" => "__always_inline", 63927ebe1d17SDwaipayan Ray "assume_aligned" => "__assume_aligned", 63937ebe1d17SDwaipayan Ray "cold" => "__cold", 63947ebe1d17SDwaipayan Ray "const" => "__attribute_const__", 63957ebe1d17SDwaipayan Ray "copy" => "__copy", 63967ebe1d17SDwaipayan Ray "designated_init" => "__designated_init", 63977ebe1d17SDwaipayan Ray "externally_visible" => "__visible", 63987ebe1d17SDwaipayan Ray "format" => "printf|scanf", 63997ebe1d17SDwaipayan Ray "gnu_inline" => "__gnu_inline", 64007ebe1d17SDwaipayan Ray "malloc" => "__malloc", 64017ebe1d17SDwaipayan Ray "mode" => "__mode", 64027ebe1d17SDwaipayan Ray "no_caller_saved_registers" => "__no_caller_saved_registers", 64037ebe1d17SDwaipayan Ray "noclone" => "__noclone", 64047ebe1d17SDwaipayan Ray "noinline" => "noinline", 64057ebe1d17SDwaipayan Ray "nonstring" => "__nonstring", 64067ebe1d17SDwaipayan Ray "noreturn" => "__noreturn", 64077ebe1d17SDwaipayan Ray "packed" => "__packed", 64087ebe1d17SDwaipayan Ray "pure" => "__pure", 6409339f29d9SJoe Perches "section" => "__section", 64100830aab0SJoe Perches "used" => "__used", 64110830aab0SJoe Perches "weak" => "__weak" 64127ebe1d17SDwaipayan Ray ); 64137ebe1d17SDwaipayan Ray 64147ebe1d17SDwaipayan Ray while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) { 6415339f29d9SJoe Perches my $orig_attr = $1; 64167ebe1d17SDwaipayan Ray my $params = ''; 64177ebe1d17SDwaipayan Ray $params = $2 if defined($2); 6418339f29d9SJoe Perches my $curr_attr = $orig_attr; 64197ebe1d17SDwaipayan Ray $curr_attr =~ s/^[\s_]+|[\s_]+$//g; 64207ebe1d17SDwaipayan Ray if (exists($attr_list{$curr_attr})) { 6421339f29d9SJoe Perches my $new = $attr_list{$curr_attr}; 64227ebe1d17SDwaipayan Ray if ($curr_attr eq "format" && $params) { 64237ebe1d17SDwaipayan Ray $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/; 6424339f29d9SJoe Perches $new = "__$1\($2"; 64257ebe1d17SDwaipayan Ray } else { 6426339f29d9SJoe Perches $new = "$new$params"; 64277ebe1d17SDwaipayan Ray } 64287ebe1d17SDwaipayan Ray if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 6429339f29d9SJoe Perches "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) && 64307ebe1d17SDwaipayan Ray $fix) { 6431339f29d9SJoe Perches my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?'; 6432339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/$remove//; 6433339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/; 6434339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/; 6435339f29d9SJoe Perches $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//; 64367ebe1d17SDwaipayan Ray } 643739b7e287SJoe Perches } 6438462811d9SJoe Perches } 6439462811d9SJoe Perches 64407ebe1d17SDwaipayan Ray # Check for __attribute__ unused, prefer __always_unused or __maybe_unused 64417ebe1d17SDwaipayan Ray if ($attr =~ /^_*unused/) { 64427ebe1d17SDwaipayan Ray WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", 64437ebe1d17SDwaipayan Ray "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr); 6444d5e616fcSJoe Perches } 64456061d949SJoe Perches } 64466061d949SJoe Perches 6447619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 64485b57980dSJoe Perches if ($perl_version_ok && 6449619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 6450619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6451619a908aSJoe Perches $line =~ /\b__weak\b/)) { 6452619a908aSJoe Perches ERROR("WEAK_DECLARATION", 6453619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 6454619a908aSJoe Perches } 6455619a908aSJoe Perches 6456fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 6457e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6458fd39f904STomas Winkler $realfile !~ m@\btools/@ && 6459e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6460e6176fa4SJoe Perches my $type = $1; 6461e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 6462e6176fa4SJoe Perches $type = $1; 6463e6176fa4SJoe Perches my $kernel_type = 'u'; 6464e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 6465e6176fa4SJoe Perches $type =~ /(\d+)/; 6466e6176fa4SJoe Perches $kernel_type .= $1; 6467e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 6468e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6469e6176fa4SJoe Perches $fix) { 6470e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6471e6176fa4SJoe Perches } 6472e6176fa4SJoe Perches } 6473e6176fa4SJoe Perches } 6474e6176fa4SJoe Perches 6475938224b5SJoe Perches# check for cast of C90 native int or longer types constants 6476938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6477938224b5SJoe Perches my $cast = $1; 6478938224b5SJoe Perches my $const = $2; 6479938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 6480938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 6481938224b5SJoe Perches $fix) { 6482938224b5SJoe Perches my $suffix = ""; 6483938224b5SJoe Perches my $newconst = $const; 6484938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 6485938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6486938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6487938224b5SJoe Perches $suffix .= 'LL'; 6488938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6489938224b5SJoe Perches $suffix .= 'L'; 6490938224b5SJoe Perches } 6491938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6492938224b5SJoe Perches } 6493938224b5SJoe Perches } 6494938224b5SJoe Perches 64958f53a9b8SJoe Perches# check for sizeof(&) 64968f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6497000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6498000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 64998f53a9b8SJoe Perches } 65008f53a9b8SJoe Perches 650166c80b60SJoe Perches# check for sizeof without parenthesis 650266c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6503d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6504d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6505d5e616fcSJoe Perches $fix) { 6506194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6507d5e616fcSJoe Perches } 650866c80b60SJoe Perches } 650966c80b60SJoe Perches 651088982feaSJoe Perches# check for struct spinlock declarations 651188982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 651288982feaSJoe Perches WARN("USE_SPINLOCK_T", 651388982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 651488982feaSJoe Perches } 651588982feaSJoe Perches 6516a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 651706668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6518a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6519caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6520caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6521d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6522d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6523d5e616fcSJoe Perches $fix) { 6524194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6525d5e616fcSJoe Perches } 6526a6962d72SJoe Perches } 6527a6962d72SJoe Perches } 6528a6962d72SJoe Perches 65290b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 65305b57980dSJoe Perches if ($perl_version_ok && 65310b523769SJoe Perches defined $stat && 65320b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 65330b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6534e3c6bc95STobin C. Harding my $stat_real; 6535e3c6bc95STobin C. Harding 65360b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 65370b523769SJoe Perches $lc = $lc + $linenr; 65380b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6539ffe07513SJoe Perches my $specifier; 6540ffe07513SJoe Perches my $extension; 65413bd32d6aSSakari Ailus my $qualifier; 6542ffe07513SJoe Perches my $bad_specifier = ""; 65430b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 65440b523769SJoe Perches $fmt =~ s/%%//g; 6545e3c6bc95STobin C. Harding 65463bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6547e3c6bc95STobin C. Harding $specifier = $1; 6548e3c6bc95STobin C. Harding $extension = $2; 65493bd32d6aSSakari Ailus $qualifier = $3; 6550361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 65513bd32d6aSSakari Ailus ($extension eq "f" && 65523bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6553e3c6bc95STobin C. Harding $bad_specifier = $specifier; 65540b523769SJoe Perches last; 65550b523769SJoe Perches } 6556e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6557e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6558e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 65590b523769SJoe Perches } 6560e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6561e3c6bc95STobin 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"); 6562e3c6bc95STobin C. Harding } 6563e3c6bc95STobin C. Harding } 6564e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 65652a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 65661df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 65671df7338aSSergey Senozhatsky my $use = ""; 6568e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 65691df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6570e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 65711df7338aSSergey Senozhatsky } 65722a9f9d85STobin C. Harding 65730b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6574e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6575e3c6bc95STobin C. Harding } 65760b523769SJoe Perches } 65770b523769SJoe Perches } 65780b523769SJoe Perches 6579554e165cSAndy Whitcroft# Check for misused memsets 65805b57980dSJoe Perches if ($perl_version_ok && 6581d1fe9c09SJoe Perches defined $stat && 65829e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6583554e165cSAndy Whitcroft 6584d7c76ba7SJoe Perches my $ms_addr = $2; 6585d1fe9c09SJoe Perches my $ms_val = $7; 6586d1fe9c09SJoe Perches my $ms_size = $12; 6587d7c76ba7SJoe Perches 6588554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6589554e165cSAndy Whitcroft ERROR("MEMSET", 6590d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6591554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6592554e165cSAndy Whitcroft WARN("MEMSET", 6593d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6594d7c76ba7SJoe Perches } 6595d7c76ba7SJoe Perches } 6596d7c76ba7SJoe Perches 659798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 65985b57980dSJoe Perches# if ($perl_version_ok && 6599f333195dSJoe Perches# defined $stat && 6600f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6601f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6602f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6603f333195dSJoe Perches# $fix) { 6604f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6605f333195dSJoe Perches# } 6606f333195dSJoe Perches# } 660798a9bba5SJoe Perches 6608b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 66095b57980dSJoe Perches# if ($perl_version_ok && 6610f333195dSJoe Perches# defined $stat && 6611f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6612f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6613f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6614f333195dSJoe Perches# } 6615b6117d17SMateusz Kulikowski 66168617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 66178617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 66185b57980dSJoe Perches# if ($perl_version_ok && 6619f333195dSJoe Perches# defined $stat && 6620f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6621f333195dSJoe Perches# 6622f333195dSJoe Perches# my $ms_val = $7; 6623f333195dSJoe Perches# 6624f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6625f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6626f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6627f333195dSJoe Perches# $fix) { 6628f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6629f333195dSJoe Perches# } 6630f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6631f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6632f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6633f333195dSJoe Perches# $fix) { 6634f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6635f333195dSJoe Perches# } 6636f333195dSJoe Perches# } 6637f333195dSJoe Perches# } 66388617cd09SMateusz Kulikowski 6639d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 66405b57980dSJoe Perches if ($perl_version_ok && 6641d1fe9c09SJoe Perches defined $stat && 6642d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6643d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6644d7c76ba7SJoe Perches my $call = $1; 6645d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6646d7c76ba7SJoe Perches my $arg1 = $3; 6647d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6648d1fe9c09SJoe Perches my $arg2 = $8; 6649d7c76ba7SJoe Perches my $cast; 6650d7c76ba7SJoe Perches 6651d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6652d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6653d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6654d7c76ba7SJoe Perches $cast = $cast1; 6655d7c76ba7SJoe Perches } else { 6656d7c76ba7SJoe Perches $cast = $cast2; 6657d7c76ba7SJoe Perches } 6658d7c76ba7SJoe Perches WARN("MINMAX", 6659d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6660554e165cSAndy Whitcroft } 6661554e165cSAndy Whitcroft } 6662554e165cSAndy Whitcroft 66634a273195SJoe Perches# check usleep_range arguments 66645b57980dSJoe Perches if ($perl_version_ok && 66654a273195SJoe Perches defined $stat && 66664a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 66674a273195SJoe Perches my $min = $1; 66684a273195SJoe Perches my $max = $7; 66694a273195SJoe Perches if ($min eq $max) { 66704a273195SJoe Perches WARN("USLEEP_RANGE", 6671458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 66724a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 66734a273195SJoe Perches $min > $max) { 66744a273195SJoe Perches WARN("USLEEP_RANGE", 6675458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 66764a273195SJoe Perches } 66774a273195SJoe Perches } 66784a273195SJoe Perches 6679823b794cSJoe Perches# check for naked sscanf 66805b57980dSJoe Perches if ($perl_version_ok && 6681823b794cSJoe Perches defined $stat && 66826c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6683823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6684823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6685823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6686823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6687823b794cSJoe Perches $lc = $lc + $linenr; 66882a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6689823b794cSJoe Perches WARN("NAKED_SSCANF", 6690823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6691823b794cSJoe Perches } 6692823b794cSJoe Perches 6693afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 66945b57980dSJoe Perches if ($perl_version_ok && 6695afc819abSJoe Perches defined $stat && 6696afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6697afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6698afc819abSJoe Perches $lc = $lc + $linenr; 66992a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6700afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6701afc819abSJoe Perches my $format = $6; 6702afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6703afc819abSJoe Perches if ($count == 1 && 6704afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6705afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6706afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6707afc819abSJoe Perches } 6708afc819abSJoe Perches } 6709afc819abSJoe Perches } 6710afc819abSJoe Perches 671170dc8a48SJoe Perches# check for new externs in .h files. 671270dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 671370dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6714d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 671570dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 671670dc8a48SJoe Perches $fix) { 6717194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 671870dc8a48SJoe Perches } 671970dc8a48SJoe Perches } 672070dc8a48SJoe Perches 6721de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6722171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6723c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6724171ae1a4SAndy Whitcroft { 6725c45dcabdSAndy Whitcroft my $function_name = $1; 6726c45dcabdSAndy Whitcroft my $paren_space = $2; 6727171ae1a4SAndy Whitcroft 6728171ae1a4SAndy Whitcroft my $s = $stat; 6729171ae1a4SAndy Whitcroft if (defined $cond) { 6730171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6731171ae1a4SAndy Whitcroft } 6732d8b44b58SKees Cook if ($s =~ /^\s*;/) 6733c45dcabdSAndy Whitcroft { 6734000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6735000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6736de7d4f0eSAndy Whitcroft } 6737de7d4f0eSAndy Whitcroft 6738171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6739000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6740000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6741171ae1a4SAndy Whitcroft } 67429c9ba34eSAndy Whitcroft 67439c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 67449c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 67459c9ba34eSAndy Whitcroft { 6746000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6747000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6748171ae1a4SAndy Whitcroft } 6749171ae1a4SAndy Whitcroft 6750a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6751a0ad7596SJoe Perches if (defined $stat && 6752d8b44b58SKees Cook $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6753d8b44b58SKees Cook $1 ne "void") { 6754d8b44b58SKees Cook my $args = trim($1); 6755ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6756ca0d8929SJoe Perches my $arg = trim($1); 6757d8b44b58SKees Cook if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6758ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6759ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6760ca0d8929SJoe Perches } 6761ca0d8929SJoe Perches } 6762ca0d8929SJoe Perches } 6763ca0d8929SJoe Perches 6764a0ad7596SJoe Perches# check for function definitions 67655b57980dSJoe Perches if ($perl_version_ok && 6766a0ad7596SJoe Perches defined $stat && 6767a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6768a0ad7596SJoe Perches $context_function = $1; 6769a0ad7596SJoe Perches 6770a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6771a0ad7596SJoe Perches my $ok = 0; 6772a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6773a0ad7596SJoe Perches my $herectx = $here . "\n"; 6774a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6775a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6776a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6777a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6778a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6779a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6780a0ad7596SJoe Perches } 6781a0ad7596SJoe Perches if (!$ok) { 6782a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6783a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6784a0ad7596SJoe Perches } 6785a0ad7596SJoe Perches } 6786a0ad7596SJoe Perches 6787de7d4f0eSAndy Whitcroft# checks for new __setup's 6788de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6789de7d4f0eSAndy Whitcroft my $name = $1; 6790de7d4f0eSAndy Whitcroft 6791de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6792000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 67932581ac7cSTim Froidcoeur "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); 6794de7d4f0eSAndy Whitcroft } 6795653d4876SAndy Whitcroft } 67969c0ca6f9SAndy Whitcroft 6797e29a70f1SJoe Perches# check for pointless casting of alloc functions 6798e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6799000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6800000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 68019c0ca6f9SAndy Whitcroft } 680213214adfSAndy Whitcroft 6803a640d25cSJoe Perches# alloc style 6804a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 68055b57980dSJoe Perches if ($perl_version_ok && 6806e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6807a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6808a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6809a640d25cSJoe Perches } 6810a640d25cSJoe Perches 681160a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 68125b57980dSJoe Perches if ($perl_version_ok && 68131b4a2ed4SJoe Perches defined $stat && 68141b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 681560a55369SJoe Perches my $oldfunc = $3; 681660a55369SJoe Perches my $a1 = $4; 681760a55369SJoe Perches my $a2 = $10; 681860a55369SJoe Perches my $newfunc = "kmalloc_array"; 681960a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 682060a55369SJoe Perches my $r1 = $a1; 682160a55369SJoe Perches my $r2 = $a2; 682260a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 682360a55369SJoe Perches $r1 = $a2; 682460a55369SJoe Perches $r2 = $a1; 682560a55369SJoe Perches } 6826e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6827e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 68281b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6829e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6830e3d95a2aSTobin C. Harding 6831e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 68321b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 68331b4a2ed4SJoe Perches $cnt == 1 && 6834e367455aSJoe Perches $fix) { 6835194f66fcSJoe 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; 683660a55369SJoe Perches } 683760a55369SJoe Perches } 683860a55369SJoe Perches } 683960a55369SJoe Perches 6840972fdea2SJoe Perches# check for krealloc arg reuse 68415b57980dSJoe Perches if ($perl_version_ok && 68424cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 68434cab63ceSJoe Perches $1 eq $3) { 6844972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6845972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6846972fdea2SJoe Perches } 6847972fdea2SJoe Perches 68485ce59ae0SJoe Perches# check for alloc argument mismatch 68495ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 68505ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 68515ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 68525ce59ae0SJoe Perches } 68535ce59ae0SJoe Perches 6854caf2a54fSJoe Perches# check for multiple semicolons 6855caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6856d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6857d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6858d5e616fcSJoe Perches $fix) { 6859194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6860d5e616fcSJoe Perches } 6861d1e2ad07SJoe Perches } 6862d1e2ad07SJoe Perches 6863cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6864cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6865cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 68660ab90191SJoe Perches my $ull = ""; 68670ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 68680ab90191SJoe Perches if (CHK("BIT_MACRO", 68690ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 68700ab90191SJoe Perches $fix) { 68710ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 68720ab90191SJoe Perches } 68730ab90191SJoe Perches } 68740ab90191SJoe Perches 687550161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) 68763e89ad85SJerome Forissier if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { 687750161266SJoe Perches WARN("IS_ENABLED_CONFIG", 68783e89ad85SJerome Forissier "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); 687950161266SJoe Perches } 688050161266SJoe Perches 68812d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 68823e89ad85SJerome 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*$/) { 68832d632745SJoe Perches my $config = $1; 68842d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 68853e89ad85SJerome Forissier "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && 68862d632745SJoe Perches $fix) { 68872d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 68882d632745SJoe Perches } 68892d632745SJoe Perches } 68902d632745SJoe Perches 6891f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 6892f36d3eb8SJoe Perches my @fallthroughs = ( 6893f36d3eb8SJoe Perches 'fallthrough', 6894f36d3eb8SJoe Perches '@fallthrough@', 6895f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 6896f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 6897f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 6898f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6899f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6900f36d3eb8SJoe Perches ); 6901f36d3eb8SJoe Perches if ($raw_comment ne '') { 6902f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 6903f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 6904f36d3eb8SJoe Perches my $msg_level = \&WARN; 6905f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 6906f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 6907f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 6908f36d3eb8SJoe Perches last; 6909f36d3eb8SJoe Perches } 6910f36d3eb8SJoe Perches } 6911f36d3eb8SJoe Perches } 6912f36d3eb8SJoe Perches 6913d1e2ad07SJoe Perches# check for switch/default statements without a break; 69145b57980dSJoe Perches if ($perl_version_ok && 6915d1e2ad07SJoe Perches defined $stat && 6916d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6917d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6918e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6919e3d95a2aSTobin C. Harding 6920d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6921d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6922caf2a54fSJoe Perches } 6923caf2a54fSJoe Perches 692413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6925d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6926d5e616fcSJoe Perches if (WARN("USE_FUNC", 6927d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6928d5e616fcSJoe Perches $fix) { 6929194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6930d5e616fcSJoe Perches } 693113214adfSAndy Whitcroft } 6932773647a0SAndy Whitcroft 693362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 693462ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 693562ec818fSJoe Perches ERROR("DATE_TIME", 693662ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 693762ec818fSJoe Perches } 693862ec818fSJoe Perches 69392c92488aSJoe Perches# check for use of yield() 69402c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 69412c92488aSJoe Perches WARN("YIELD", 69422c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 69432c92488aSJoe Perches } 69442c92488aSJoe Perches 6945179f8f40SJoe Perches# check for comparisons against true and false 6946179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6947179f8f40SJoe Perches my $lead = $1; 6948179f8f40SJoe Perches my $arg = $2; 6949179f8f40SJoe Perches my $test = $3; 6950179f8f40SJoe Perches my $otype = $4; 6951179f8f40SJoe Perches my $trail = $5; 6952179f8f40SJoe Perches my $op = "!"; 6953179f8f40SJoe Perches 6954179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6955179f8f40SJoe Perches 6956179f8f40SJoe Perches my $type = lc($otype); 6957179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6958179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6959179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6960179f8f40SJoe Perches $op = ""; 6961179f8f40SJoe Perches } 6962179f8f40SJoe Perches 6963179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6964179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6965179f8f40SJoe Perches 6966179f8f40SJoe Perches## maybe suggesting a correct construct would better 6967179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6968179f8f40SJoe Perches 6969179f8f40SJoe Perches } 6970179f8f40SJoe Perches } 6971179f8f40SJoe Perches 69724882720bSThomas Gleixner# check for semaphores initialized locked 69734882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6974000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6975000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6976773647a0SAndy Whitcroft } 69776712d858SJoe Perches 697867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 697967d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6980000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 698167d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6982773647a0SAndy Whitcroft } 69836712d858SJoe Perches 6984ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6985f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6986000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6987ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6988f3db6639SMichael Ellerman } 69896712d858SJoe Perches 69903d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 69913d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 69923d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 69933d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 69943d709ab5SPaul E. McKenney } 69953d709ab5SPaul E. McKenney 69969189c7e7SJoe Perches# check for deprecated apis 69979189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 69989189c7e7SJoe Perches my $deprecated_api = $1; 69999189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 70009189c7e7SJoe Perches WARN("DEPRECATED_API", 70019189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 70029189c7e7SJoe Perches } 70039189c7e7SJoe Perches 70040f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 7005d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 7006ced69da1SQuentin Monnet if (defined($const_structs) && 7007ced69da1SQuentin Monnet $line !~ /\bconst\b/ && 7008d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 7009000d1cc1SJoe Perches WARN("CONST_STRUCT", 7010d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 70112b6db5cbSAndy Whitcroft } 7012773647a0SAndy Whitcroft 7013773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 7014773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 7015773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 7016c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 7017c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 7018171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 7019171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 7020171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 7021773647a0SAndy Whitcroft { 7022000d1cc1SJoe Perches WARN("NR_CPUS", 7023000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 7024773647a0SAndy Whitcroft } 70259c9ba34eSAndy Whitcroft 702652ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 702752ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 702852ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 702952ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 703052ea8506SJoe Perches } 703152ea8506SJoe Perches 7032acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 70335b57980dSJoe Perches if ($perl_version_ok && 7034acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 7035acd9362cSJoe Perches WARN("LIKELY_MISUSE", 7036acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 7037acd9362cSJoe Perches } 7038acd9362cSJoe Perches 7039de3f186fSDenis Efremov# nested likely/unlikely calls 7040de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 7041de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 7042de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 7043de3f186fSDenis Efremov } 7044de3f186fSDenis Efremov 7045691d77b6SAndy Whitcroft# whine mightly about in_atomic 7046691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 7047691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 7048000d1cc1SJoe Perches ERROR("IN_ATOMIC", 7049000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 7050f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 7051000d1cc1SJoe Perches WARN("IN_ATOMIC", 7052000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 7053691d77b6SAndy Whitcroft } 7054691d77b6SAndy Whitcroft } 70551704f47bSPeter Zijlstra 70560f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 70570f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 70580f5225b0SPeter Zijlstra ERROR("LOCKING", 70590f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 70600f5225b0SPeter Zijlstra } 70610f5225b0SPeter Zijlstra 70621704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 70631704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 70641704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 70651704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 70661704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 70671704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 7068000d1cc1SJoe Perches ERROR("LOCKDEP", 7069000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 70701704f47bSPeter Zijlstra } 70711704f47bSPeter Zijlstra } 707288f8831cSDave Jones 7073b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 7074b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 7075000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 7076000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 707788f8831cSDave Jones } 70782435880fSJoe Perches 707900180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 708000180468SJoe Perches# and whether or not function naming is typical and if 708100180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 70825b57980dSJoe Perches if ($perl_version_ok && 708300180468SJoe Perches defined $stat && 708400180468SJoe 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*\)/) { 708500180468SJoe Perches my $var = $1; 708600180468SJoe Perches my $perms = $2; 708700180468SJoe Perches my $show = $3; 708800180468SJoe Perches my $store = $4; 708900180468SJoe Perches my $octal_perms = perms_to_octal($perms); 709000180468SJoe Perches if ($show =~ /^${var}_show$/ && 709100180468SJoe Perches $store =~ /^${var}_store$/ && 709200180468SJoe Perches $octal_perms eq "0644") { 709300180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 709400180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 709500180468SJoe Perches $fix) { 709600180468SJoe 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})/; 709700180468SJoe Perches } 709800180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 709900180468SJoe Perches $store =~ /^NULL$/ && 710000180468SJoe Perches $octal_perms eq "0444") { 710100180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 710200180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 710300180468SJoe Perches $fix) { 710400180468SJoe 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})/; 710500180468SJoe Perches } 710600180468SJoe Perches } elsif ($show =~ /^NULL$/ && 710700180468SJoe Perches $store =~ /^${var}_store$/ && 710800180468SJoe Perches $octal_perms eq "0200") { 710900180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 711000180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 711100180468SJoe Perches $fix) { 711200180468SJoe 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})/; 711300180468SJoe Perches } 711400180468SJoe Perches } elsif ($octal_perms eq "0644" || 711500180468SJoe Perches $octal_perms eq "0444" || 711600180468SJoe Perches $octal_perms eq "0200") { 711700180468SJoe Perches my $newshow = "$show"; 711800180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 711900180468SJoe Perches my $newstore = $store; 712000180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 712100180468SJoe Perches my $rename = ""; 712200180468SJoe Perches if ($show ne $newshow) { 712300180468SJoe Perches $rename .= " '$show' to '$newshow'"; 712400180468SJoe Perches } 712500180468SJoe Perches if ($store ne $newstore) { 712600180468SJoe Perches $rename .= " '$store' to '$newstore'"; 712700180468SJoe Perches } 712800180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 712900180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 713000180468SJoe Perches } else { 713100180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 713200180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 713300180468SJoe Perches } 713400180468SJoe Perches } 713500180468SJoe Perches 7136515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 7137515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 713873121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 713973121534SJoe Perches# specific definition of not visible in sysfs. 714073121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 714173121534SJoe Perches# use the default permissions 71425b57980dSJoe Perches if ($perl_version_ok && 7143459cf0aeSJoe Perches defined $stat && 7144515a235eSJoe Perches $line =~ /$mode_perms_search/) { 71452435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 71462435880fSJoe Perches my $func = $entry->[0]; 71472435880fSJoe Perches my $arg_pos = $entry->[1]; 71482435880fSJoe Perches 7149459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 7150459cf0aeSJoe Perches $lc = $lc + $linenr; 71512a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 7152459cf0aeSJoe Perches 71532435880fSJoe Perches my $skip_args = ""; 71542435880fSJoe Perches if ($arg_pos > 1) { 71552435880fSJoe Perches $arg_pos--; 71562435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 71572435880fSJoe Perches } 7158f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 7159459cf0aeSJoe Perches if ($stat =~ /$test/) { 71602435880fSJoe Perches my $val = $1; 71612435880fSJoe Perches $val = $6 if ($skip_args ne ""); 716273121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 716373121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 716473121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 71652435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 7166459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 7167f90774e1SJoe Perches } 7168f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 7169c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 7170459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 71712435880fSJoe Perches } 7172459cf0aeSJoe Perches } 7173459cf0aeSJoe Perches } 7174459cf0aeSJoe Perches } 7175459cf0aeSJoe Perches 7176459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 7177bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 717800180468SJoe Perches my $oval = $1; 717900180468SJoe Perches my $octal = perms_to_octal($oval); 7180f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 7181459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 7182f90774e1SJoe Perches $fix) { 718300180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 71842435880fSJoe Perches } 718513214adfSAndy Whitcroft } 71865a6d20ceSBjorn Andersson 71875a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 71885a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 71895a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 71905a6d20ceSBjorn Andersson my $valid_licenses = qr{ 71915a6d20ceSBjorn Andersson GPL| 71925a6d20ceSBjorn Andersson GPL\ v2| 71935a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 71945a6d20ceSBjorn Andersson Dual\ BSD/GPL| 71955a6d20ceSBjorn Andersson Dual\ MIT/GPL| 71965a6d20ceSBjorn Andersson Dual\ MPL/GPL| 71975a6d20ceSBjorn Andersson Proprietary 71985a6d20ceSBjorn Andersson }x; 71995a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 72005a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 72015a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 72025a6d20ceSBjorn Andersson } 72035a6d20ceSBjorn Andersson } 72046a8d76cbSMatteo Croce 72056a8d76cbSMatteo Croce# check for sysctl duplicate constants 72066a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 72076a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 72086a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 72096a8d76cbSMatteo Croce } 7210515a235eSJoe Perches } 721113214adfSAndy Whitcroft 721213214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 721313214adfSAndy Whitcroft # so just keep quiet. 721413214adfSAndy Whitcroft if ($#rawlines == -1) { 721513214adfSAndy Whitcroft exit(0); 72160a920b5bSAndy Whitcroft } 72170a920b5bSAndy Whitcroft 72188905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 72198905a67cSAndy Whitcroft # things that appear to be patches. 72208905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 72218905a67cSAndy Whitcroft exit(0); 72228905a67cSAndy Whitcroft } 72238905a67cSAndy Whitcroft 7224e73d2715SDwaipayan Ray # This is not a patch, and we are in 'no-patch' mode so 72258905a67cSAndy Whitcroft # just keep quiet. 72268905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 72278905a67cSAndy Whitcroft exit(0); 72288905a67cSAndy Whitcroft } 72298905a67cSAndy Whitcroft 7230a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 7231000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 7232000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 72330a920b5bSAndy Whitcroft } 7234cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 7235cd261496SGeert Uytterhoeven if ($signoff == 0) { 7236000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 7237000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 723848ca2d8aSDwaipayan Ray } elsif ($authorsignoff != 1) { 723948ca2d8aSDwaipayan Ray # authorsignoff values: 724048ca2d8aSDwaipayan Ray # 0 -> missing sign off 724148ca2d8aSDwaipayan Ray # 1 -> sign off identical 724248ca2d8aSDwaipayan Ray # 2 -> names and addresses match, comments mismatch 724348ca2d8aSDwaipayan Ray # 3 -> addresses match, names different 724448ca2d8aSDwaipayan Ray # 4 -> names match, addresses different 724548ca2d8aSDwaipayan Ray # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match 724648ca2d8aSDwaipayan Ray 724748ca2d8aSDwaipayan Ray my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; 724848ca2d8aSDwaipayan Ray 724948ca2d8aSDwaipayan Ray if ($authorsignoff == 0) { 725048ca2d8aSDwaipayan Ray ERROR("NO_AUTHOR_SIGN_OFF", 7251cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 725248ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 2) { 725348ca2d8aSDwaipayan Ray CHK("FROM_SIGN_OFF_MISMATCH", 725448ca2d8aSDwaipayan Ray "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); 725548ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 3) { 725648ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 725748ca2d8aSDwaipayan Ray "From:/Signed-off-by: email name mismatch: $sob_msg\n"); 725848ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 4) { 725948ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 726048ca2d8aSDwaipayan Ray "From:/Signed-off-by: email address mismatch: $sob_msg\n"); 726148ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 5) { 726248ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 726348ca2d8aSDwaipayan Ray "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); 726448ca2d8aSDwaipayan Ray } 7265cd261496SGeert Uytterhoeven } 72660a920b5bSAndy Whitcroft } 72670a920b5bSAndy Whitcroft 7268f0a594c1SAndy Whitcroft print report_dump(); 726913214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 727013214adfSAndy Whitcroft print "$filename " if ($summary_file); 72716c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 72726c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 72736c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 72746c72ffaaSAndy Whitcroft } 72758905a67cSAndy Whitcroft 7276d2c0a235SAndy Whitcroft if ($quiet == 0) { 7277ef212196SJoe Perches # If there were any defects found and not already fixing them 7278ef212196SJoe Perches if (!$clean and !$fix) { 7279ef212196SJoe Perches print << "EOM" 7280ef212196SJoe Perches 7281ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 7282ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 7283ef212196SJoe PerchesEOM 7284ef212196SJoe Perches } 7285d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 7286d2c0a235SAndy Whitcroft # then suggest that. 7287d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 7288b0781216SMike Frysinger $rpt_cleaners = 0; 7289d8469f16SJoe Perches print << "EOM" 7290d8469f16SJoe Perches 7291d8469f16SJoe PerchesNOTE: Whitespace errors detected. 7292d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 7293d8469f16SJoe PerchesEOM 7294d2c0a235SAndy Whitcroft } 7295d2c0a235SAndy Whitcroft } 7296d2c0a235SAndy Whitcroft 7297d752fcc8SJoe Perches if ($clean == 0 && $fix && 7298d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 7299d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 73009624b8d6SJoe Perches my $newfile = $filename; 73019624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 73023705ce5bSJoe Perches my $linecount = 0; 73033705ce5bSJoe Perches my $f; 73043705ce5bSJoe Perches 7305d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 7306d752fcc8SJoe Perches 73073705ce5bSJoe Perches open($f, '>', $newfile) 73083705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 73093705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 73103705ce5bSJoe Perches $linecount++; 73113705ce5bSJoe Perches if ($file) { 73123705ce5bSJoe Perches if ($linecount > 3) { 73133705ce5bSJoe Perches $fixed_line =~ s/^\+//; 73143705ce5bSJoe Perches print $f $fixed_line . "\n"; 73153705ce5bSJoe Perches } 73163705ce5bSJoe Perches } else { 73173705ce5bSJoe Perches print $f $fixed_line . "\n"; 73183705ce5bSJoe Perches } 73193705ce5bSJoe Perches } 73203705ce5bSJoe Perches close($f); 73213705ce5bSJoe Perches 73223705ce5bSJoe Perches if (!$quiet) { 73233705ce5bSJoe Perches print << "EOM"; 7324d8469f16SJoe Perches 73253705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 73263705ce5bSJoe Perches 73273705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 73283705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 73293705ce5bSJoe Perches 73303705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 73313705ce5bSJoe PerchesNo warranties, expressed or implied... 73323705ce5bSJoe PerchesEOM 73333705ce5bSJoe Perches } 73343705ce5bSJoe Perches } 73353705ce5bSJoe Perches 7336d8469f16SJoe Perches if ($quiet == 0) { 7337d8469f16SJoe Perches print "\n"; 7338d8469f16SJoe Perches if ($clean == 1) { 7339d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 7340d8469f16SJoe Perches } else { 7341d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 73420a920b5bSAndy Whitcroft } 73430a920b5bSAndy Whitcroft } 73440a920b5bSAndy Whitcroft return $clean; 73450a920b5bSAndy Whitcroft} 7346