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; 46*0f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'}; 47*0f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot); 48c2fdda0dSAndy Whitcroftmy %debug; 493445686aSJoe Perchesmy %camelcase = (); 5091bfe484SJoe Perchesmy %use_type = (); 5191bfe484SJoe Perchesmy @use = (); 5291bfe484SJoe Perchesmy %ignore_type = (); 53000d1cc1SJoe Perchesmy @ignore = (); 5477f5b10aSHannes Edermy $help = 0; 55000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 56bdc48fa1SJoe Perchesmy $max_line_length = 100; 57d62a201fSDave Hansenmy $ignore_perl_version = 0; 58d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5956193274SVadim Bendeburymy $min_conf_desc_length = 4; 6066b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 61ebfd7d62SJoe Perchesmy $codespell = 0; 62f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 63bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch"; 64ced69da1SQuentin Monnetmy $typedefsfile; 65737c0767SJohn Brooksmy $color = "auto"; 6698005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE 67dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE 68dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git'; 69713a09deSAntonio Borneomy $tabsize = 8; 703e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_"; 7177f5b10aSHannes Eder 7277f5b10aSHannes Edersub help { 7377f5b10aSHannes Eder my ($exitcode) = @_; 7477f5b10aSHannes Eder 7577f5b10aSHannes Eder print << "EOM"; 7677f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 7777f5b10aSHannes EderVersion: $V 7877f5b10aSHannes Eder 7977f5b10aSHannes EderOptions: 8077f5b10aSHannes Eder -q, --quiet quiet 8177f5b10aSHannes Eder --no-tree run without a kernel tree 8277f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 8377f5b10aSHannes Eder --patch treat FILE as patchfile (default) 8477f5b10aSHannes Eder --emacs emacs compile window format 8577f5b10aSHannes Eder --terse one line per report 8634d8815fSJoe Perches --showfile emit diffed file position, not input file position 874a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 884a593c34SDu, Changbin single git commit with: 894a593c34SDu, Changbin <rev> 904a593c34SDu, Changbin <rev>^ 914a593c34SDu, Changbin <rev>~n 924a593c34SDu, Changbin multiple git commits with: 934a593c34SDu, Changbin <rev1>..<rev2> 944a593c34SDu, Changbin <rev1>...<rev2> 954a593c34SDu, Changbin <rev>-<count> 964a593c34SDu, Changbin git merges are ignored 9777f5b10aSHannes Eder -f, --file treat FILE as regular source file 9877f5b10aSHannes Eder --subjective, --strict enable more subjective tests 993beb42ecSJoe Perches --list-types list the possible message types 10091bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 101000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 1023beb42ecSJoe Perches --show-types show the specific message type in the output 103bdc48fa1SJoe Perches --max-line-length=n set the maximum line length, (default $max_line_length) 104bdc48fa1SJoe Perches if exceeded, warn on patches 105bdc48fa1SJoe Perches requires --strict for use with --file 10656193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 107bdc48fa1SJoe Perches --tab-size=n set the number of spaces for tab (default $tabsize) 10877f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10977f5b10aSHannes Eder --no-summary suppress the per-file summary 11077f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 11177f5b10aSHannes Eder --summary-file include the filename in summary 11277f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 11377f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 11477f5b10aSHannes Eder is all off) 11577f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 11677f5b10aSHannes Eder literally 1173705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1183705ce5bSJoe Perches If correctable single-line errors exist, create 1193705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1203705ce5bSJoe Perches with potential errors corrected to the preferred 1213705ce5bSJoe Perches checkpatch style 1229624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1239624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1249624b8d6SJoe Perches file. It's your fault if there's no backup or git 125d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 126d62a201fSDave Hansen runtime errors. 127ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 128f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 129ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 13075ad8c57SJerome Forissier --typedefsfile Read additional types from this file 131737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 132737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 1333e89ad85SJerome Forissier --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default 1343e89ad85SJerome Forissier ${CONFIG_}) 13577f5b10aSHannes Eder -h, --help, --version display this help and exit 13677f5b10aSHannes Eder 13777f5b10aSHannes EderWhen FILE is - read standard input. 13877f5b10aSHannes EderEOM 13977f5b10aSHannes Eder 14077f5b10aSHannes Eder exit($exitcode); 14177f5b10aSHannes Eder} 14277f5b10aSHannes Eder 1433beb42ecSJoe Perchessub uniq { 1443beb42ecSJoe Perches my %seen; 1453beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1463beb42ecSJoe Perches} 1473beb42ecSJoe Perches 1483beb42ecSJoe Perchessub list_types { 1493beb42ecSJoe Perches my ($exitcode) = @_; 1503beb42ecSJoe Perches 1513beb42ecSJoe Perches my $count = 0; 1523beb42ecSJoe Perches 1533beb42ecSJoe Perches local $/ = undef; 1543beb42ecSJoe Perches 1553beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1563beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1573beb42ecSJoe Perches 1583beb42ecSJoe Perches my $text = <$script>; 1593beb42ecSJoe Perches close($script); 1603beb42ecSJoe Perches 1613beb42ecSJoe Perches my @types = (); 1620547fa58SJean Delvare # Also catch when type or level is passed through a variable 1630547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1643beb42ecSJoe Perches push (@types, $_); 1653beb42ecSJoe Perches } 1663beb42ecSJoe Perches @types = sort(uniq(@types)); 1673beb42ecSJoe Perches print("#\tMessage type\n\n"); 1683beb42ecSJoe Perches foreach my $type (@types) { 1693beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1703beb42ecSJoe Perches } 1713beb42ecSJoe Perches 1723beb42ecSJoe Perches exit($exitcode); 1733beb42ecSJoe Perches} 1743beb42ecSJoe Perches 175000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 176000d1cc1SJoe Perchesif (-f $conf) { 177000d1cc1SJoe Perches my @conf_args; 178000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 179000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 180000d1cc1SJoe Perches 181000d1cc1SJoe Perches while (<$conffile>) { 182000d1cc1SJoe Perches my $line = $_; 183000d1cc1SJoe Perches 184000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 185000d1cc1SJoe Perches $line =~ s/^\s*//g; 186000d1cc1SJoe Perches $line =~ s/\s+/ /g; 187000d1cc1SJoe Perches 188000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 189000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 190000d1cc1SJoe Perches 191000d1cc1SJoe Perches my @words = split(" ", $line); 192000d1cc1SJoe Perches foreach my $word (@words) { 193000d1cc1SJoe Perches last if ($word =~ m/^#/); 194000d1cc1SJoe Perches push (@conf_args, $word); 195000d1cc1SJoe Perches } 196000d1cc1SJoe Perches } 197000d1cc1SJoe Perches close($conffile); 198000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 199000d1cc1SJoe Perches} 200000d1cc1SJoe Perches 201737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 202737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 203737c0767SJohn Brooksforeach (@ARGV) { 204737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 205737c0767SJohn Brooks $_ = "--color=$color"; 206737c0767SJohn Brooks } 207737c0767SJohn Brooks} 208737c0767SJohn Brooks 2090a920b5bSAndy WhitcroftGetOptions( 2106c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2110a920b5bSAndy Whitcroft 'tree!' => \$tree, 2120a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2130a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2146c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2158905a67cSAndy Whitcroft 'terse!' => \$terse, 21634d8815fSJoe Perches 'showfile!' => \$showfile, 21777f5b10aSHannes Eder 'f|file!' => \$file, 2184a593c34SDu, Changbin 'g|git!' => \$git, 2196c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2206c72ffaaSAndy Whitcroft 'strict!' => \$check, 221000d1cc1SJoe Perches 'ignore=s' => \@ignore, 22291bfe484SJoe Perches 'types=s' => \@use, 223000d1cc1SJoe Perches 'show-types!' => \$show_types, 2243beb42ecSJoe Perches 'list-types!' => \$list_types, 2256cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 22656193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 227713a09deSAntonio Borneo 'tab-size=i' => \$tabsize, 2286c72ffaaSAndy Whitcroft 'root=s' => \$root, 2298905a67cSAndy Whitcroft 'summary!' => \$summary, 2308905a67cSAndy Whitcroft 'mailback!' => \$mailback, 23113214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2323705ce5bSJoe Perches 'fix!' => \$fix, 2339624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 234d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 235c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 236773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 237ebfd7d62SJoe Perches 'codespell!' => \$codespell, 238ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 23975ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 240737c0767SJohn Brooks 'color=s' => \$color, 241737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 242737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 2433e89ad85SJerome Forissier 'kconfig-prefix=s' => \${CONFIG_}, 24477f5b10aSHannes Eder 'h|help' => \$help, 24577f5b10aSHannes Eder 'version' => \$help 24677f5b10aSHannes Eder) or help(1); 24777f5b10aSHannes Eder 24877f5b10aSHannes Ederhelp(0) if ($help); 2490a920b5bSAndy Whitcroft 2503beb42ecSJoe Percheslist_types(0) if ($list_types); 2513beb42ecSJoe Perches 2529624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2532ac73b4fSJoe Perches$check_orig = $check; 2549624b8d6SJoe Perches 25532f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); 25632f30ca9SJoe Perches 2570a920b5bSAndy Whitcroftmy $exit = 0; 2580a920b5bSAndy Whitcroft 2595b57980dSJoe Perchesmy $perl_version_ok = 1; 260d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2615b57980dSJoe Perches $perl_version_ok = 0; 262d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2635b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 264d62a201fSDave Hansen} 265d62a201fSDave Hansen 26645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2670a920b5bSAndy Whitcroftif ($#ARGV < 0) { 26845107ff6SAllen Hubbe push(@ARGV, '-'); 2690a920b5bSAndy Whitcroft} 2700a920b5bSAndy Whitcroft 271737c0767SJohn Brooksif ($color =~ /^[01]$/) { 272737c0767SJohn Brooks $color = !$color; 273737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 274737c0767SJohn Brooks $color = 1; 275737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 276737c0767SJohn Brooks $color = 0; 277737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 278737c0767SJohn Brooks $color = (-t STDOUT); 279737c0767SJohn Brooks} else { 28032f30ca9SJoe Perches die "$P: Invalid color mode: $color\n"; 281737c0767SJohn Brooks} 282737c0767SJohn Brooks 283713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1 28432f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); 285713a09deSAntonio Borneo 28691bfe484SJoe Perchessub hash_save_array_words { 28791bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 28891bfe484SJoe Perches 28991bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 29091bfe484SJoe Perches foreach my $word (@array) { 291000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 292000d1cc1SJoe Perches $word =~ s/^\s*//g; 293000d1cc1SJoe Perches $word =~ s/\s+/ /g; 294000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 295000d1cc1SJoe Perches 296000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 297000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 298000d1cc1SJoe Perches 29991bfe484SJoe Perches $hashRef->{$word}++; 300000d1cc1SJoe Perches } 30191bfe484SJoe Perches} 30291bfe484SJoe Perches 30391bfe484SJoe Perchessub hash_show_words { 30491bfe484SJoe Perches my ($hashRef, $prefix) = @_; 30591bfe484SJoe Perches 3063c816e49SJoe Perches if (keys %$hashRef) { 307d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 30858cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 30991bfe484SJoe Perches print " $word"; 31091bfe484SJoe Perches } 311d8469f16SJoe Perches print "\n"; 31291bfe484SJoe Perches } 31391bfe484SJoe Perches} 31491bfe484SJoe Perches 31591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 31691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 317000d1cc1SJoe Perches 318c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 319c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3207429c690SAndy Whitcroftmy $dbg_type = 0; 321a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 322c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 32321caa13cSAndy Whitcroft ## no critic 32421caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 32521caa13cSAndy Whitcroft die "$@" if ($@); 326c2fdda0dSAndy Whitcroft} 327c2fdda0dSAndy Whitcroft 328d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 329d2c0a235SAndy Whitcroft 3308905a67cSAndy Whitcroftif ($terse) { 3318905a67cSAndy Whitcroft $emacs = 1; 3328905a67cSAndy Whitcroft $quiet++; 3338905a67cSAndy Whitcroft} 3348905a67cSAndy Whitcroft 3356c72ffaaSAndy Whitcroftif ($tree) { 3366c72ffaaSAndy Whitcroft if (defined $root) { 3376c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3386c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3396c72ffaaSAndy Whitcroft } 3406c72ffaaSAndy Whitcroft } else { 3416c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3426c72ffaaSAndy Whitcroft $root = '.'; 3436c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3446c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3456c72ffaaSAndy Whitcroft $root = $1; 3466c72ffaaSAndy Whitcroft } 3476c72ffaaSAndy Whitcroft } 3486c72ffaaSAndy Whitcroft 3496c72ffaaSAndy Whitcroft if (!defined $root) { 3500a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3510a920b5bSAndy Whitcroft exit(2); 3520a920b5bSAndy Whitcroft } 3536c72ffaaSAndy Whitcroft} 3546c72ffaaSAndy Whitcroft 3556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3566c72ffaaSAndy Whitcroft 3572ceb532bSAndy Whitcroftour $Ident = qr{ 3582ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3592ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3602ceb532bSAndy Whitcroft }x; 3616c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3626c72ffaaSAndy Whitcroftour $Sparse = qr{ 3636c72ffaaSAndy Whitcroft __user| 3646c72ffaaSAndy Whitcroft __kernel| 3656c72ffaaSAndy Whitcroft __force| 3666c72ffaaSAndy Whitcroft __iomem| 3676c72ffaaSAndy Whitcroft __must_check| 368417495edSAndy Whitcroft __kprobes| 369165e72a6SSven Eckelmann __ref| 37033aa4597SGeert Uytterhoeven __refconst| 37133aa4597SGeert Uytterhoeven __refdata| 372ad315455SBoqun Feng __rcu| 373ad315455SBoqun Feng __private 3746c72ffaaSAndy Whitcroft }x; 375e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 376e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 377e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 378e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 379e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3808716de38SJoe Perches 38152131292SWolfram Sang# Notes to $Attribute: 38252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3836c72ffaaSAndy Whitcroftour $Attribute = qr{ 3846c72ffaaSAndy Whitcroft const| 38503f1df7dSJoe Perches __percpu| 38603f1df7dSJoe Perches __nocast| 38703f1df7dSJoe Perches __safe| 38846d832f5SMichael S. Tsirkin __bitwise| 38903f1df7dSJoe Perches __packed__| 39003f1df7dSJoe Perches __packed2__| 39103f1df7dSJoe Perches __naked| 39203f1df7dSJoe Perches __maybe_unused| 39303f1df7dSJoe Perches __always_unused| 39403f1df7dSJoe Perches __noreturn| 39503f1df7dSJoe Perches __used| 39603f1df7dSJoe Perches __cold| 397e23ef1f3SJoe Perches __pure| 39803f1df7dSJoe Perches __noclone| 39903f1df7dSJoe Perches __deprecated| 4006c72ffaaSAndy Whitcroft __read_mostly| 401c5967e98SJoe Perches __ro_after_init| 4026c72ffaaSAndy Whitcroft __kprobes| 4038716de38SJoe Perches $InitAttribute| 40424e1d81aSAndy Whitcroft ____cacheline_aligned| 40524e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 4065fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 4075fe3af11SAndy Whitcroft __weak 4086c72ffaaSAndy Whitcroft }x; 409c45dcabdSAndy Whitcroftour $Modifier; 41091cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 4116c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 4126c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 4136c72ffaaSAndy Whitcroft 41495e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 41595e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 41695e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 41795e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4182435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 419c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 420326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 421326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 422326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 42374349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4242435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 425326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 426447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 42723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4286c72ffaaSAndy Whitcroftour $Operators = qr{ 4296c72ffaaSAndy Whitcroft <=|>=|==|!=| 4306c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 43123f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4326c72ffaaSAndy Whitcroft }x; 4336c72ffaaSAndy Whitcroft 43491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 43591cb5195SJoe Perches 436ab7e23f3SJoe Perchesour $BasicType; 4378905a67cSAndy Whitcroftour $NonptrType; 4381813087dSJoe Perchesour $NonptrTypeMisordered; 4398716de38SJoe Perchesour $NonptrTypeWithAttr; 4408905a67cSAndy Whitcroftour $Type; 4411813087dSJoe Perchesour $TypeMisordered; 4428905a67cSAndy Whitcroftour $Declare; 4431813087dSJoe Perchesour $DeclareMisordered; 4448905a67cSAndy Whitcroft 44515662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 44615662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 447171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 448171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 449171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 450171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 451171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 452171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 453171ae1a4SAndy Whitcroft}x; 454171ae1a4SAndy Whitcroft 45515662b3eSJoe Perchesour $UTF8 = qr{ 45615662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 45715662b3eSJoe Perches | $NON_ASCII_UTF8 45815662b3eSJoe Perches}x; 45915662b3eSJoe Perches 460e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 461021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 462021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 463021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 464021158b4SJoe Perches)}; 465e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 466fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4678ed22cadSAndy Whitcroft atomic_t 4688ed22cadSAndy Whitcroft)}; 469e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 470e6176fa4SJoe Perches $typeC99Typedefs\b| 471e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 472e6176fa4SJoe Perches $typeKernelTypedefs\b 473e6176fa4SJoe Perches)}; 4748ed22cadSAndy Whitcroft 4756d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4766d32f7a3SJoe Perches 477691e669bSJoe Perchesour $logFunctions = qr{(?x: 478758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4797d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 48087bd499aSJoe Perches TP_printk| 4816e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 482b0531722SJoe Perches panic| 48306668727SJoe Perches MODULE_[A-Z_]+| 48406668727SJoe Perches seq_vprintf|seq_printf|seq_puts 485691e669bSJoe Perches)}; 486691e669bSJoe Perches 487e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 488e29a70f1SJoe Perches (?:(?:devm_)? 489e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 490e29a70f1SJoe Perches kstrdup(?:_const)? | 491e29a70f1SJoe Perches kmemdup(?:_nul)?) | 492461e1565SChristophe JAILLET (?:\w+)?alloc_skb(?:_ip_align)? | 493e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 494e29a70f1SJoe Perches dma_alloc_coherent 495e29a70f1SJoe Perches)}; 496e29a70f1SJoe Perches 49720112475SJoe Perchesour $signature_tags = qr{(?xi: 49820112475SJoe Perches Signed-off-by:| 499d499480cSJorge Ramirez-Ortiz Co-developed-by:| 50020112475SJoe Perches Acked-by:| 50120112475SJoe Perches Tested-by:| 50220112475SJoe Perches Reviewed-by:| 50320112475SJoe Perches Reported-by:| 5048543ae12SMugunthan V N Suggested-by:| 50520112475SJoe Perches To:| 50620112475SJoe Perches Cc: 50720112475SJoe Perches)}; 50820112475SJoe Perches 5091813087dSJoe Perchesour @typeListMisordered = ( 5101813087dSJoe Perches qr{char\s+(?:un)?signed}, 5111813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 5121813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 5131813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 5141813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 5151813087dSJoe Perches qr{short\s+(?:un)?signed}, 5161813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5171813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5181813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5191813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5201813087dSJoe Perches qr{int\s+(?:un)?signed}, 5211813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5221813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5231813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5241813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5251813087dSJoe Perches qr{long\s+(?:un)?signed}, 5261813087dSJoe Perches); 5271813087dSJoe Perches 5288905a67cSAndy Whitcroftour @typeList = ( 5298905a67cSAndy Whitcroft qr{void}, 5300c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5310c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5320c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5330c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5340c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5350c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5360c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5370c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5380c773d9dSJoe Perches qr{(?:un)?signed}, 5398905a67cSAndy Whitcroft qr{float}, 5408905a67cSAndy Whitcroft qr{double}, 5418905a67cSAndy Whitcroft qr{bool}, 5428905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5438905a67cSAndy Whitcroft qr{union\s+$Ident}, 5448905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5458905a67cSAndy Whitcroft qr{${Ident}_t}, 5468905a67cSAndy Whitcroft qr{${Ident}_handler}, 5478905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5481813087dSJoe Perches @typeListMisordered, 5498905a67cSAndy Whitcroft); 550938224b5SJoe Perches 551938224b5SJoe Perchesour $C90_int_types = qr{(?x: 552938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 553938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 554938224b5SJoe Perches long\s+long\s+(?:un)?signed| 555938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 556938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 557938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 558938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 559938224b5SJoe Perches 560938224b5SJoe Perches long\s+int\s+(?:un)?signed| 561938224b5SJoe Perches long\s+(?:un)?signed\s+int| 562938224b5SJoe Perches long\s+(?:un)?signed| 563938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 564938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 565938224b5SJoe Perches int\s+long\s+(?:un)?signed| 566938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 567938224b5SJoe Perches 568938224b5SJoe Perches int\s+(?:un)?signed| 569938224b5SJoe Perches (?:(?:un)?signed\s+)?int 570938224b5SJoe Perches)}; 571938224b5SJoe Perches 572485ff23eSAlex Dowadour @typeListFile = (); 5738716de38SJoe Perchesour @typeListWithAttr = ( 5748716de38SJoe Perches @typeList, 5758716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5768716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5778716de38SJoe Perches); 5788716de38SJoe Perches 579c45dcabdSAndy Whitcroftour @modifierList = ( 580c45dcabdSAndy Whitcroft qr{fastcall}, 581c45dcabdSAndy Whitcroft); 582485ff23eSAlex Dowadour @modifierListFile = (); 5838905a67cSAndy Whitcroft 5842435880fSJoe Perchesour @mode_permission_funcs = ( 5852435880fSJoe Perches ["module_param", 3], 5862435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5872435880fSJoe Perches ["module_param_array_named", 5], 5882435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5892435880fSJoe Perches ["proc_create(?:_data|)", 2], 590459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 591459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 592459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 593459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 594459cf0aeSJoe Perches ["__ATTR", 2], 5952435880fSJoe Perches); 5962435880fSJoe Perches 5971a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; 5981a3dcf2eSJoe Perches 599515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 600515a235eSJoe Perchesour $mode_perms_search = ""; 601515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 602515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 603515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 604515a235eSJoe Perches} 60500180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 606515a235eSJoe Perches 6079189c7e7SJoe Perchesour %deprecated_apis = ( 6089189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 6099189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 6109189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 6119189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 6129189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 6139189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 6149189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 6159189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 6169189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 6179189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6189189c7e7SJoe Perches); 6199189c7e7SJoe Perches 6209189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6219189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6229189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6239189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6249189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6259189c7e7SJoe Perches} 6269189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6279189c7e7SJoe Perches 628b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 629b392c64fSJoe Perches S_IWUGO | 630b392c64fSJoe Perches S_IWOTH | 631b392c64fSJoe Perches S_IRWXUGO | 632b392c64fSJoe Perches S_IALLUGO | 633b392c64fSJoe Perches 0[0-7][0-7][2367] 634b392c64fSJoe Perches}x; 635b392c64fSJoe Perches 636f90774e1SJoe Perchesour %mode_permission_string_types = ( 637f90774e1SJoe Perches "S_IRWXU" => 0700, 638f90774e1SJoe Perches "S_IRUSR" => 0400, 639f90774e1SJoe Perches "S_IWUSR" => 0200, 640f90774e1SJoe Perches "S_IXUSR" => 0100, 641f90774e1SJoe Perches "S_IRWXG" => 0070, 642f90774e1SJoe Perches "S_IRGRP" => 0040, 643f90774e1SJoe Perches "S_IWGRP" => 0020, 644f90774e1SJoe Perches "S_IXGRP" => 0010, 645f90774e1SJoe Perches "S_IRWXO" => 0007, 646f90774e1SJoe Perches "S_IROTH" => 0004, 647f90774e1SJoe Perches "S_IWOTH" => 0002, 648f90774e1SJoe Perches "S_IXOTH" => 0001, 649f90774e1SJoe Perches "S_IRWXUGO" => 0777, 650f90774e1SJoe Perches "S_IRUGO" => 0444, 651f90774e1SJoe Perches "S_IWUGO" => 0222, 652f90774e1SJoe Perches "S_IXUGO" => 0111, 653f90774e1SJoe Perches); 654f90774e1SJoe Perches 655f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 656f90774e1SJoe Perchesour $mode_perms_string_search = ""; 657f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 658f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 659f90774e1SJoe Perches $mode_perms_string_search .= $entry; 660f90774e1SJoe Perches} 66100180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 66200180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 66300180468SJoe Perches ${single_mode_perms_string_search} 66400180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 66500180468SJoe Perches}x; 66600180468SJoe Perches 66700180468SJoe Perchessub perms_to_octal { 66800180468SJoe Perches my ($string) = @_; 66900180468SJoe Perches 67000180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 67100180468SJoe Perches 67200180468SJoe Perches my $val = ""; 67300180468SJoe Perches my $oval = ""; 67400180468SJoe Perches my $to = 0; 67500180468SJoe Perches my $curpos = 0; 67600180468SJoe Perches my $lastpos = 0; 67700180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 67800180468SJoe Perches $curpos = pos($string); 67900180468SJoe Perches my $match = $2; 68000180468SJoe Perches my $omatch = $1; 68100180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 68200180468SJoe Perches $lastpos = $curpos; 68300180468SJoe Perches $to |= $mode_permission_string_types{$match}; 68400180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 68500180468SJoe Perches $val .= $match; 68600180468SJoe Perches $oval .= $omatch; 68700180468SJoe Perches } 68800180468SJoe Perches $oval =~ s/^\s*\|\s*//; 68900180468SJoe Perches $oval =~ s/\s*\|\s*$//; 69000180468SJoe Perches return sprintf("%04o", $to); 69100180468SJoe Perches} 692f90774e1SJoe Perches 6937840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6947840a94cSWolfram Sang irq| 695cdcee686SSergey Ryazanov memory| 696cdcee686SSergey Ryazanov time| 697cdcee686SSergey Ryazanov reboot 6987840a94cSWolfram Sang)}; 6997840a94cSWolfram Sang# memory.h: ARM has a custom one 7007840a94cSWolfram Sang 70166b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 70266b47b4aSKees Cookmy $misspellings; 70366b47b4aSKees Cookmy %spelling_fix; 70436061e38SJoe Perches 70536061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 70666b47b4aSKees Cook while (<$spelling>) { 70766b47b4aSKees Cook my $line = $_; 70866b47b4aSKees Cook 70966b47b4aSKees Cook $line =~ s/\s*\n?$//g; 71066b47b4aSKees Cook $line =~ s/^\s*//g; 71166b47b4aSKees Cook 71266b47b4aSKees Cook next if ($line =~ m/^\s*#/); 71366b47b4aSKees Cook next if ($line =~ m/^\s*$/); 71466b47b4aSKees Cook 71566b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 71666b47b4aSKees Cook 71766b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 71866b47b4aSKees Cook } 71966b47b4aSKees Cook close($spelling); 72036061e38SJoe Perches} else { 72136061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 72236061e38SJoe Perches} 72366b47b4aSKees Cook 724ebfd7d62SJoe Perchesif ($codespell) { 725ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 726ebfd7d62SJoe Perches while (<$spelling>) { 727ebfd7d62SJoe Perches my $line = $_; 728ebfd7d62SJoe Perches 729ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 730ebfd7d62SJoe Perches $line =~ s/^\s*//g; 731ebfd7d62SJoe Perches 732ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 733ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 734ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 735ebfd7d62SJoe Perches 736ebfd7d62SJoe Perches $line =~ s/,.*$//; 737ebfd7d62SJoe Perches 738ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 739ebfd7d62SJoe Perches 740ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 741ebfd7d62SJoe Perches } 742ebfd7d62SJoe Perches close($spelling); 743ebfd7d62SJoe Perches } else { 744ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 745ebfd7d62SJoe Perches } 746ebfd7d62SJoe Perches} 747ebfd7d62SJoe Perches 748ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 749ebfd7d62SJoe Perches 75075ad8c57SJerome Forissiersub read_words { 75175ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 75275ad8c57SJerome Forissier 75375ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 75475ad8c57SJerome Forissier while (<$words>) { 755bf1fa1daSJoe Perches my $line = $_; 756bf1fa1daSJoe Perches 757bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 758bf1fa1daSJoe Perches $line =~ s/^\s*//g; 759bf1fa1daSJoe Perches 760bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 761bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 762bf1fa1daSJoe Perches if ($line =~ /\s/) { 76375ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 764bf1fa1daSJoe Perches next; 765bf1fa1daSJoe Perches } 766bf1fa1daSJoe Perches 767ced69da1SQuentin Monnet $$wordsRef .= '|' if (defined $$wordsRef); 76875ad8c57SJerome Forissier $$wordsRef .= $line; 769bf1fa1daSJoe Perches } 77075ad8c57SJerome Forissier close($file); 77175ad8c57SJerome Forissier return 1; 772bf1fa1daSJoe Perches } 773bf1fa1daSJoe Perches 77475ad8c57SJerome Forissier return 0; 77575ad8c57SJerome Forissier} 77675ad8c57SJerome Forissier 777ced69da1SQuentin Monnetmy $const_structs; 778ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) { 77975ad8c57SJerome Forissier read_words(\$const_structs, $conststructsfile) 78075ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 781ced69da1SQuentin Monnet} 78275ad8c57SJerome Forissier 783ced69da1SQuentin Monnetif (defined($typedefsfile)) { 784ced69da1SQuentin Monnet my $typeOtherTypedefs; 78575ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 78675ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 787ced69da1SQuentin Monnet $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); 78875ad8c57SJerome Forissier} 78975ad8c57SJerome Forissier 7908905a67cSAndy Whitcroftsub build_types { 791485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 792485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7931813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7948716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 795c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 796ab7e23f3SJoe Perches $BasicType = qr{ 797ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 798ab7e23f3SJoe Perches (?:${all}\b) 799ab7e23f3SJoe Perches }x; 8008905a67cSAndy Whitcroft $NonptrType = qr{ 801d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 802cf655043SAndy Whitcroft (?: 8036b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 8048ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 805c45dcabdSAndy Whitcroft (?:${all}\b) 806cf655043SAndy Whitcroft ) 807c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 8088905a67cSAndy Whitcroft }x; 8091813087dSJoe Perches $NonptrTypeMisordered = qr{ 8101813087dSJoe Perches (?:$Modifier\s+|const\s+)* 8111813087dSJoe Perches (?: 8121813087dSJoe Perches (?:${Misordered}\b) 8131813087dSJoe Perches ) 8141813087dSJoe Perches (?:\s+$Modifier|\s+const)* 8151813087dSJoe Perches }x; 8168716de38SJoe Perches $NonptrTypeWithAttr = qr{ 8178716de38SJoe Perches (?:$Modifier\s+|const\s+)* 8188716de38SJoe Perches (?: 8198716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8208716de38SJoe Perches (?:$typeTypedefs\b)| 8218716de38SJoe Perches (?:${allWithAttr}\b) 8228716de38SJoe Perches ) 8238716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8248716de38SJoe Perches }x; 8258905a67cSAndy Whitcroft $Type = qr{ 826c45dcabdSAndy Whitcroft $NonptrType 8277b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 828c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8298905a67cSAndy Whitcroft }x; 8301813087dSJoe Perches $TypeMisordered = qr{ 8311813087dSJoe Perches $NonptrTypeMisordered 8327b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 8331813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8341813087dSJoe Perches }x; 83591cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8361813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8378905a67cSAndy Whitcroft} 8388905a67cSAndy Whitcroftbuild_types(); 8396c72ffaaSAndy Whitcroft 8407d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 841d1fe9c09SJoe Perches 842d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 843d1fe9c09SJoe Perches# requires at least perl version v5.10.0 844d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 845d1fe9c09SJoe Perches 846d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 8472435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 848c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 8497d2367afSJoe Perches 850f8422308SJoe Perchesour $declaration_macros = qr{(?x: 8513e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 852fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 8533d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 854f8422308SJoe Perches)}; 855f8422308SJoe Perches 8567d2367afSJoe Perchessub deparenthesize { 8577d2367afSJoe Perches my ($string) = @_; 8587d2367afSJoe Perches return "" if (!defined($string)); 8595b9553abSJoe Perches 8605b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8615b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8625b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8635b9553abSJoe Perches } 8645b9553abSJoe Perches 8657d2367afSJoe Perches $string =~ s@\s+@ @g; 8665b9553abSJoe Perches 8677d2367afSJoe Perches return $string; 8687d2367afSJoe Perches} 8697d2367afSJoe Perches 8703445686aSJoe Perchessub seed_camelcase_file { 8713445686aSJoe Perches my ($file) = @_; 8723445686aSJoe Perches 8733445686aSJoe Perches return if (!(-f $file)); 8743445686aSJoe Perches 8753445686aSJoe Perches local $/; 8763445686aSJoe Perches 8773445686aSJoe Perches open(my $include_file, '<', "$file") 8783445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8793445686aSJoe Perches my $text = <$include_file>; 8803445686aSJoe Perches close($include_file); 8813445686aSJoe Perches 8823445686aSJoe Perches my @lines = split('\n', $text); 8833445686aSJoe Perches 8843445686aSJoe Perches foreach my $line (@lines) { 8853445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8863445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8873445686aSJoe Perches $camelcase{$1} = 1; 88811ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 88911ea516aSJoe Perches $camelcase{$1} = 1; 89011ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8913445686aSJoe Perches $camelcase{$1} = 1; 8923445686aSJoe Perches } 8933445686aSJoe Perches } 8943445686aSJoe Perches} 8953445686aSJoe Perches 896cd28b119SJoe Perchesour %maintained_status = (); 897cd28b119SJoe Perches 89885b0ee18SJoe Perchessub is_maintained_obsolete { 89985b0ee18SJoe Perches my ($filename) = @_; 90085b0ee18SJoe Perches 901f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 90285b0ee18SJoe Perches 903cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 904cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 905cd28b119SJoe Perches } 90685b0ee18SJoe Perches 907cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 90885b0ee18SJoe Perches} 90985b0ee18SJoe Perches 9103b6e8ac9SJoe Perchessub is_SPDX_License_valid { 9113b6e8ac9SJoe Perches my ($license) = @_; 9123b6e8ac9SJoe Perches 913*0f7f635bSJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); 9143b6e8ac9SJoe Perches 91556294112SJoe Perches my $root_path = abs_path($root); 91656294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 9173b6e8ac9SJoe Perches return 0 if ($status ne ""); 9183b6e8ac9SJoe Perches return 1; 9193b6e8ac9SJoe Perches} 9203b6e8ac9SJoe Perches 9213445686aSJoe Perchesmy $camelcase_seeded = 0; 9223445686aSJoe Perchessub seed_camelcase_includes { 9233445686aSJoe Perches return if ($camelcase_seeded); 9243445686aSJoe Perches 9253445686aSJoe Perches my $files; 926c707a81dSJoe Perches my $camelcase_cache = ""; 927c707a81dSJoe Perches my @include_files = (); 928c707a81dSJoe Perches 929c707a81dSJoe Perches $camelcase_seeded = 1; 930351b2a1fSJoe Perches 931*0f7f635bSJoe Perches if (-e "$gitroot") { 932dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 933351b2a1fSJoe Perches chomp $git_last_include_commit; 934c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 935c707a81dSJoe Perches } else { 936c707a81dSJoe Perches my $last_mod_date = 0; 937c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 938c707a81dSJoe Perches @include_files = split('\n', $files); 939c707a81dSJoe Perches foreach my $file (@include_files) { 940c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 941c707a81dSJoe Perches localtime((stat $file)[9])); 942c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 943c707a81dSJoe Perches } 944c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 945c707a81dSJoe Perches } 946c707a81dSJoe Perches 947c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 948c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 949c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 950351b2a1fSJoe Perches while (<$camelcase_file>) { 951351b2a1fSJoe Perches chomp; 952351b2a1fSJoe Perches $camelcase{$_} = 1; 953351b2a1fSJoe Perches } 954351b2a1fSJoe Perches close($camelcase_file); 955351b2a1fSJoe Perches 956351b2a1fSJoe Perches return; 957351b2a1fSJoe Perches } 958c707a81dSJoe Perches 959*0f7f635bSJoe Perches if (-e "$gitroot") { 960dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 961c707a81dSJoe Perches @include_files = split('\n', $files); 9623445686aSJoe Perches } 963c707a81dSJoe Perches 9643445686aSJoe Perches foreach my $file (@include_files) { 9653445686aSJoe Perches seed_camelcase_file($file); 9663445686aSJoe Perches } 967351b2a1fSJoe Perches 968c707a81dSJoe Perches if ($camelcase_cache ne "") { 969351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 970c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 971c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 972351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 973351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 974351b2a1fSJoe Perches } 975351b2a1fSJoe Perches close($camelcase_file); 976351b2a1fSJoe Perches } 9773445686aSJoe Perches} 9783445686aSJoe Perches 979f5f61325SJoe Perchessub git_is_single_file { 980f5f61325SJoe Perches my ($filename) = @_; 981f5f61325SJoe Perches 982f5f61325SJoe Perches return 0 if ((which("git") eq "") || !(-e "$gitroot")); 983f5f61325SJoe Perches 984f5f61325SJoe Perches my $output = `${git_command} ls-files -- $filename 2>/dev/null`; 985f5f61325SJoe Perches my $count = $output =~ tr/\n//; 986f5f61325SJoe Perches return $count eq 1 && $output =~ m{^${filename}$}; 987f5f61325SJoe Perches} 988f5f61325SJoe Perches 989d311cd44SJoe Perchessub git_commit_info { 990d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 991d311cd44SJoe Perches 992*0f7f635bSJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); 993d311cd44SJoe Perches 994dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 995d311cd44SJoe Perches $output =~ s/^\s*//gm; 996d311cd44SJoe Perches my @lines = split("\n", $output); 997d311cd44SJoe Perches 9980d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9990d7835fcSJoe Perches 10005a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 1001d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 1002d311cd44SJoe Perches# all matching commit ids, but it's very slow... 1003d311cd44SJoe Perches# 1004d311cd44SJoe Perches# echo "checking commits $1..." 1005d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 1006d311cd44SJoe Perches# while read line ; do 1007d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 1008d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 1009d311cd44SJoe Perches# done 1010d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 1011948b133aSHeinrich Schuchardt $id = undef; 1012d311cd44SJoe Perches } else { 1013d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 1014d311cd44SJoe Perches $desc = substr($lines[0], 41); 1015d311cd44SJoe Perches } 1016d311cd44SJoe Perches 1017d311cd44SJoe Perches return ($id, $desc); 1018d311cd44SJoe Perches} 1019d311cd44SJoe Perches 10206c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 10210a920b5bSAndy Whitcroft 102200df344fSAndy Whitcroftmy @rawlines = (); 1023c2fdda0dSAndy Whitcroftmy @lines = (); 10243705ce5bSJoe Perchesmy @fixed = (); 1025d752fcc8SJoe Perchesmy @fixed_inserted = (); 1026d752fcc8SJoe Perchesmy @fixed_deleted = (); 1027194f66fcSJoe Perchesmy $fixlinenr = -1; 1028194f66fcSJoe Perches 10294a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10304a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 1031*0f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot"); 10324a593c34SDu, Changbin 10334a593c34SDu, Changbinif ($git) { 10344a593c34SDu, Changbin my @commits = (); 10350dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 10364a593c34SDu, Changbin my $git_range; 103728898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 103828898fd1SJoe Perches $git_range = "-$2 $1"; 10394a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 10404a593c34SDu, Changbin $git_range = "$commit_expr"; 10414a593c34SDu, Changbin } else { 10420dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 10430dea9f1eSJoe Perches } 1044dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 10450dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 104628898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 104728898fd1SJoe Perches next if (!defined($1) || !defined($2)); 10480dea9f1eSJoe Perches my $sha1 = $1; 10490dea9f1eSJoe Perches my $subject = $2; 10500dea9f1eSJoe Perches unshift(@commits, $sha1); 10510dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 10524a593c34SDu, Changbin } 10534a593c34SDu, Changbin } 10544a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 10554a593c34SDu, Changbin @ARGV = @commits; 10564a593c34SDu, Changbin} 10574a593c34SDu, Changbin 1058c2fdda0dSAndy Whitcroftmy $vname; 105998005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 10606c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 106121caa13cSAndy Whitcroft my $FILE; 1062f5f61325SJoe Perches my $is_git_file = git_is_single_file($filename); 1063f5f61325SJoe Perches my $oldfile = $file; 1064f5f61325SJoe Perches $file = 1 if ($is_git_file); 10654a593c34SDu, Changbin if ($git) { 10664a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 10674a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 10684a593c34SDu, Changbin } elsif ($file) { 106921caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 10706c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 107121caa13cSAndy Whitcroft } elsif ($filename eq '-') { 107221caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 10736c72ffaaSAndy Whitcroft } else { 107421caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 10756c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 10766c72ffaaSAndy Whitcroft } 1077c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1078c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 10794a593c34SDu, Changbin } elsif ($git) { 10800dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1081c2fdda0dSAndy Whitcroft } else { 1082c2fdda0dSAndy Whitcroft $vname = $filename; 1083c2fdda0dSAndy Whitcroft } 108421caa13cSAndy Whitcroft while (<$FILE>) { 10850a920b5bSAndy Whitcroft chomp; 108600df344fSAndy Whitcroft push(@rawlines, $_); 1087c7f574d0SGeert Uytterhoeven $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); 10886c72ffaaSAndy Whitcroft } 108921caa13cSAndy Whitcroft close($FILE); 1090d8469f16SJoe Perches 1091d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1092d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1093d8469f16SJoe Perches print "$vname\n"; 1094d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1095d8469f16SJoe Perches } 1096d8469f16SJoe Perches 1097c2fdda0dSAndy Whitcroft if (!process($filename)) { 10980a920b5bSAndy Whitcroft $exit = 1; 10990a920b5bSAndy Whitcroft } 110000df344fSAndy Whitcroft @rawlines = (); 110113214adfSAndy Whitcroft @lines = (); 11023705ce5bSJoe Perches @fixed = (); 1103d752fcc8SJoe Perches @fixed_inserted = (); 1104d752fcc8SJoe Perches @fixed_deleted = (); 1105194f66fcSJoe Perches $fixlinenr = -1; 1106485ff23eSAlex Dowad @modifierListFile = (); 1107485ff23eSAlex Dowad @typeListFile = (); 1108485ff23eSAlex Dowad build_types(); 1109f5f61325SJoe Perches $file = $oldfile if ($is_git_file); 11100a920b5bSAndy Whitcroft} 11110a920b5bSAndy Whitcroft 1112d8469f16SJoe Perchesif (!$quiet) { 11133c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 11143c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 11153c816e49SJoe Perches 11165b57980dSJoe Perches if (!$perl_version_ok) { 1117d8469f16SJoe Perches print << "EOM" 1118d8469f16SJoe Perches 1119d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 11205b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1121d8469f16SJoe PerchesEOM 1122d8469f16SJoe Perches } 1123d8469f16SJoe Perches if ($exit) { 1124d8469f16SJoe Perches print << "EOM" 1125d8469f16SJoe Perches 1126d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1127d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1128d8469f16SJoe PerchesEOM 1129d8469f16SJoe Perches } 1130d8469f16SJoe Perches} 1131d8469f16SJoe Perches 11320a920b5bSAndy Whitcroftexit($exit); 11330a920b5bSAndy Whitcroft 11340a920b5bSAndy Whitcroftsub top_of_kernel_tree { 11356c72ffaaSAndy Whitcroft my ($root) = @_; 11366c72ffaaSAndy Whitcroft 11376c72ffaaSAndy Whitcroft my @tree_check = ( 11386c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 11396c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 11406c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 11416c72ffaaSAndy Whitcroft ); 11426c72ffaaSAndy Whitcroft 11436c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 11446c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 11450a920b5bSAndy Whitcroft return 0; 11460a920b5bSAndy Whitcroft } 11476c72ffaaSAndy Whitcroft } 11486c72ffaaSAndy Whitcroft return 1; 11496c72ffaaSAndy Whitcroft} 11500a920b5bSAndy Whitcroft 115120112475SJoe Perchessub parse_email { 115220112475SJoe Perches my ($formatted_email) = @_; 115320112475SJoe Perches 115420112475SJoe Perches my $name = ""; 1155dfa05c28SJoe Perches my $name_comment = ""; 115620112475SJoe Perches my $address = ""; 115720112475SJoe Perches my $comment = ""; 115820112475SJoe Perches 115920112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 116020112475SJoe Perches $name = $1; 116120112475SJoe Perches $address = $2; 116220112475SJoe Perches $comment = $3 if defined $3; 116320112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 116420112475SJoe Perches $address = $1; 116520112475SJoe Perches $comment = $2 if defined $2; 116620112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 116720112475SJoe Perches $address = $1; 116820112475SJoe Perches $comment = $2 if defined $2; 116985e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 117020112475SJoe Perches $name = $formatted_email; 11713705ce5bSJoe Perches $name = trim($name); 117220112475SJoe Perches $name =~ s/^\"|\"$//g; 117320112475SJoe Perches # If there's a name left after stripping spaces and 117420112475SJoe Perches # leading quotes, and the address doesn't have both 117520112475SJoe Perches # leading and trailing angle brackets, the address 117620112475SJoe Perches # is invalid. ie: 117720112475SJoe Perches # "joe smith [email protected]" bad 117820112475SJoe Perches # "joe smith <[email protected]" bad 117920112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 118020112475SJoe Perches $name = ""; 118120112475SJoe Perches $address = ""; 118220112475SJoe Perches $comment = ""; 118320112475SJoe Perches } 118420112475SJoe Perches } 118520112475SJoe Perches 118648ca2d8aSDwaipayan Ray $comment = trim($comment); 11873705ce5bSJoe Perches $name = trim($name); 118820112475SJoe Perches $name =~ s/^\"|\"$//g; 118948ca2d8aSDwaipayan Ray if ($name =~ s/(\s*\([^\)]+\))\s*//) { 1190dfa05c28SJoe Perches $name_comment = trim($1); 1191dfa05c28SJoe Perches } 11923705ce5bSJoe Perches $address = trim($address); 119320112475SJoe Perches $address =~ s/^\<|\>$//g; 119420112475SJoe Perches 119520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 119620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 119720112475SJoe Perches $name = "\"$name\""; 119820112475SJoe Perches } 119920112475SJoe Perches 1200dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 120120112475SJoe Perches} 120220112475SJoe Perches 120320112475SJoe Perchessub format_email { 120448ca2d8aSDwaipayan Ray my ($name, $name_comment, $address, $comment) = @_; 120520112475SJoe Perches 120620112475SJoe Perches my $formatted_email; 120720112475SJoe Perches 120848ca2d8aSDwaipayan Ray $name_comment = trim($name_comment); 120948ca2d8aSDwaipayan Ray $comment = trim($comment); 12103705ce5bSJoe Perches $name = trim($name); 121120112475SJoe Perches $name =~ s/^\"|\"$//g; 12123705ce5bSJoe Perches $address = trim($address); 121320112475SJoe Perches 121420112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 121520112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 121620112475SJoe Perches $name = "\"$name\""; 121720112475SJoe Perches } 121820112475SJoe Perches 121920112475SJoe Perches if ("$name" eq "") { 122020112475SJoe Perches $formatted_email = "$address"; 122120112475SJoe Perches } else { 122248ca2d8aSDwaipayan Ray $formatted_email = "$name$name_comment <$address>"; 122320112475SJoe Perches } 122448ca2d8aSDwaipayan Ray $formatted_email .= "$comment"; 122520112475SJoe Perches return $formatted_email; 122620112475SJoe Perches} 122720112475SJoe Perches 1228dfa05c28SJoe Perchessub reformat_email { 1229dfa05c28SJoe Perches my ($email) = @_; 1230dfa05c28SJoe Perches 1231dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 123248ca2d8aSDwaipayan Ray return format_email($email_name, $name_comment, $email_address, $comment); 1233dfa05c28SJoe Perches} 1234dfa05c28SJoe Perches 1235dfa05c28SJoe Perchessub same_email_addresses { 123648ca2d8aSDwaipayan Ray my ($email1, $email2, $match_comment) = @_; 1237dfa05c28SJoe Perches 1238dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1239dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1240dfa05c28SJoe Perches 124148ca2d8aSDwaipayan Ray if ($match_comment != 1) { 1242dfa05c28SJoe Perches return $email1_name eq $email2_name && 1243dfa05c28SJoe Perches $email1_address eq $email2_address; 1244dfa05c28SJoe Perches } 124548ca2d8aSDwaipayan Ray return $email1_name eq $email2_name && 124648ca2d8aSDwaipayan Ray $email1_address eq $email2_address && 124748ca2d8aSDwaipayan Ray $name1_comment eq $name2_comment && 124848ca2d8aSDwaipayan Ray $comment1 eq $comment2; 124948ca2d8aSDwaipayan Ray} 1250dfa05c28SJoe Perches 1251d311cd44SJoe Perchessub which { 1252d311cd44SJoe Perches my ($bin) = @_; 1253d311cd44SJoe Perches 1254d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1255d311cd44SJoe Perches if (-e "$path/$bin") { 1256d311cd44SJoe Perches return "$path/$bin"; 1257d311cd44SJoe Perches } 1258d311cd44SJoe Perches } 1259d311cd44SJoe Perches 1260d311cd44SJoe Perches return ""; 1261d311cd44SJoe Perches} 1262d311cd44SJoe Perches 1263000d1cc1SJoe Perchessub which_conf { 1264000d1cc1SJoe Perches my ($conf) = @_; 1265000d1cc1SJoe Perches 1266000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1267000d1cc1SJoe Perches if (-e "$path/$conf") { 1268000d1cc1SJoe Perches return "$path/$conf"; 1269000d1cc1SJoe Perches } 1270000d1cc1SJoe Perches } 1271000d1cc1SJoe Perches 1272000d1cc1SJoe Perches return ""; 1273000d1cc1SJoe Perches} 1274000d1cc1SJoe Perches 12750a920b5bSAndy Whitcroftsub expand_tabs { 12760a920b5bSAndy Whitcroft my ($str) = @_; 12770a920b5bSAndy Whitcroft 12780a920b5bSAndy Whitcroft my $res = ''; 12790a920b5bSAndy Whitcroft my $n = 0; 12800a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 12810a920b5bSAndy Whitcroft if ($c eq "\t") { 12820a920b5bSAndy Whitcroft $res .= ' '; 12830a920b5bSAndy Whitcroft $n++; 1284713a09deSAntonio Borneo for (; ($n % $tabsize) != 0; $n++) { 12850a920b5bSAndy Whitcroft $res .= ' '; 12860a920b5bSAndy Whitcroft } 12870a920b5bSAndy Whitcroft next; 12880a920b5bSAndy Whitcroft } 12890a920b5bSAndy Whitcroft $res .= $c; 12900a920b5bSAndy Whitcroft $n++; 12910a920b5bSAndy Whitcroft } 12920a920b5bSAndy Whitcroft 12930a920b5bSAndy Whitcroft return $res; 12940a920b5bSAndy Whitcroft} 12956c72ffaaSAndy Whitcroftsub copy_spacing { 1296773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 12976c72ffaaSAndy Whitcroft return $res; 12986c72ffaaSAndy Whitcroft} 12990a920b5bSAndy Whitcroft 13004a0df2efSAndy Whitcroftsub line_stats { 13014a0df2efSAndy Whitcroft my ($line) = @_; 13024a0df2efSAndy Whitcroft 13034a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 13044a0df2efSAndy Whitcroft $line =~ s/^.//; 13054a0df2efSAndy Whitcroft $line = expand_tabs($line); 13064a0df2efSAndy Whitcroft 13074a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 13084a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 13094a0df2efSAndy Whitcroft 13104a0df2efSAndy Whitcroft return (length($line), length($white)); 13114a0df2efSAndy Whitcroft} 13124a0df2efSAndy Whitcroft 1313773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1314773647a0SAndy Whitcroft 1315773647a0SAndy Whitcroftsub sanitise_line_reset { 1316773647a0SAndy Whitcroft my ($in_comment) = @_; 1317773647a0SAndy Whitcroft 1318773647a0SAndy Whitcroft if ($in_comment) { 1319773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1320773647a0SAndy Whitcroft } else { 1321773647a0SAndy Whitcroft $sanitise_quote = ''; 1322773647a0SAndy Whitcroft } 1323773647a0SAndy Whitcroft} 132400df344fSAndy Whitcroftsub sanitise_line { 132500df344fSAndy Whitcroft my ($line) = @_; 132600df344fSAndy Whitcroft 132700df344fSAndy Whitcroft my $res = ''; 132800df344fSAndy Whitcroft my $l = ''; 132900df344fSAndy Whitcroft 1330c2fdda0dSAndy Whitcroft my $qlen = 0; 1331773647a0SAndy Whitcroft my $off = 0; 1332773647a0SAndy Whitcroft my $c; 133300df344fSAndy Whitcroft 1334773647a0SAndy Whitcroft # Always copy over the diff marker. 1335773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1336773647a0SAndy Whitcroft 1337773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1338773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1339773647a0SAndy Whitcroft 13408d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1341773647a0SAndy Whitcroft # and end, all to $;. 1342773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1343773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1344773647a0SAndy Whitcroft 1345773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1346773647a0SAndy Whitcroft $off++; 134700df344fSAndy Whitcroft next; 1348773647a0SAndy Whitcroft } 134981bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1350773647a0SAndy Whitcroft $sanitise_quote = ''; 1351773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1352773647a0SAndy Whitcroft $off++; 1353773647a0SAndy Whitcroft next; 1354773647a0SAndy Whitcroft } 1355113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1356113f04a8SDaniel Walker $sanitise_quote = '//'; 1357113f04a8SDaniel Walker 1358113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1359113f04a8SDaniel Walker $off++; 1360113f04a8SDaniel Walker next; 1361113f04a8SDaniel Walker } 1362773647a0SAndy Whitcroft 1363773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1364773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1365773647a0SAndy Whitcroft $c eq "\\") { 1366773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1367773647a0SAndy Whitcroft $off++; 1368773647a0SAndy Whitcroft next; 1369773647a0SAndy Whitcroft } 1370773647a0SAndy Whitcroft # Regular quotes. 1371773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1372773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1373773647a0SAndy Whitcroft $sanitise_quote = $c; 1374773647a0SAndy Whitcroft 1375773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1376773647a0SAndy Whitcroft next; 1377773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1378773647a0SAndy Whitcroft $sanitise_quote = ''; 137900df344fSAndy Whitcroft } 138000df344fSAndy Whitcroft } 1381773647a0SAndy Whitcroft 1382fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1383773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1384773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1385113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1386113f04a8SDaniel Walker substr($res, $off, 1, $;); 1387773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1388773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 138900df344fSAndy Whitcroft } else { 1390773647a0SAndy Whitcroft substr($res, $off, 1, $c); 139100df344fSAndy Whitcroft } 1392c2fdda0dSAndy Whitcroft } 1393c2fdda0dSAndy Whitcroft 1394113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1395113f04a8SDaniel Walker $sanitise_quote = ''; 1396113f04a8SDaniel Walker } 1397113f04a8SDaniel Walker 1398c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1399c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1400c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1401c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1402c2fdda0dSAndy Whitcroft 1403c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1404c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1405c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1406c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1407c2fdda0dSAndy Whitcroft } 1408c2fdda0dSAndy Whitcroft 1409dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1410dadf680dSJoe Perches my $match = $1; 1411dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1412dadf680dSJoe Perches } 1413dadf680dSJoe Perches 141400df344fSAndy Whitcroft return $res; 141500df344fSAndy Whitcroft} 141600df344fSAndy Whitcroft 1417a6962d72SJoe Perchessub get_quoted_string { 1418a6962d72SJoe Perches my ($line, $rawline) = @_; 1419a6962d72SJoe Perches 1420478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 142133acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1422a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1423a6962d72SJoe Perches} 1424a6962d72SJoe Perches 14258905a67cSAndy Whitcroftsub ctx_statement_block { 14268905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 14278905a67cSAndy Whitcroft my $line = $linenr - 1; 14288905a67cSAndy Whitcroft my $blk = ''; 14298905a67cSAndy Whitcroft my $soff = $off; 14308905a67cSAndy Whitcroft my $coff = $off - 1; 1431773647a0SAndy Whitcroft my $coff_set = 0; 14328905a67cSAndy Whitcroft 143313214adfSAndy Whitcroft my $loff = 0; 143413214adfSAndy Whitcroft 14358905a67cSAndy Whitcroft my $type = ''; 14368905a67cSAndy Whitcroft my $level = 0; 1437a2750645SAndy Whitcroft my @stack = (); 1438cf655043SAndy Whitcroft my $p; 14398905a67cSAndy Whitcroft my $c; 14408905a67cSAndy Whitcroft my $len = 0; 144113214adfSAndy Whitcroft 144213214adfSAndy Whitcroft my $remainder; 14438905a67cSAndy Whitcroft while (1) { 1444a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1445a2750645SAndy Whitcroft 1446773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 14478905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 14488905a67cSAndy Whitcroft # context. 14498905a67cSAndy Whitcroft if ($off >= $len) { 14508905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1451dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1452c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 14538905a67cSAndy Whitcroft $remain--; 145413214adfSAndy Whitcroft $loff = $len; 1455c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 14568905a67cSAndy Whitcroft $len = length($blk); 14578905a67cSAndy Whitcroft $line++; 14588905a67cSAndy Whitcroft last; 14598905a67cSAndy Whitcroft } 14608905a67cSAndy Whitcroft # Bail if there is no further context. 14618905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 146213214adfSAndy Whitcroft if ($off >= $len) { 14638905a67cSAndy Whitcroft last; 14648905a67cSAndy Whitcroft } 1465f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1466f74bd194SAndy Whitcroft $level++; 1467f74bd194SAndy Whitcroft $type = '#'; 1468f74bd194SAndy Whitcroft } 14698905a67cSAndy Whitcroft } 1470cf655043SAndy Whitcroft $p = $c; 14718905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 147213214adfSAndy Whitcroft $remainder = substr($blk, $off); 14738905a67cSAndy Whitcroft 1474773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 14754635f4fbSAndy Whitcroft 14764635f4fbSAndy Whitcroft # Handle nested #if/#else. 14774635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 14784635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 14794635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 14804635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 14814635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 14824635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 14834635f4fbSAndy Whitcroft } 14844635f4fbSAndy Whitcroft 14858905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 14868905a67cSAndy Whitcroft # outermost level. 14878905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 14888905a67cSAndy Whitcroft last; 14898905a67cSAndy Whitcroft } 14908905a67cSAndy Whitcroft 149113214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1492773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1493773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1494773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1495773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1496773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1497773647a0SAndy Whitcroft $coff_set = 1; 1498773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1499773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 150013214adfSAndy Whitcroft } 150113214adfSAndy Whitcroft 15028905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 15038905a67cSAndy Whitcroft $level++; 15048905a67cSAndy Whitcroft $type = '('; 15058905a67cSAndy Whitcroft } 15068905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 15078905a67cSAndy Whitcroft $level--; 15088905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 15098905a67cSAndy Whitcroft 15108905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 15118905a67cSAndy Whitcroft $coff = $off; 1512773647a0SAndy Whitcroft $coff_set = 1; 1513773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 15148905a67cSAndy Whitcroft } 15158905a67cSAndy Whitcroft } 15168905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 15178905a67cSAndy Whitcroft $level++; 15188905a67cSAndy Whitcroft $type = '{'; 15198905a67cSAndy Whitcroft } 15208905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 15218905a67cSAndy Whitcroft $level--; 15228905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 15238905a67cSAndy Whitcroft 15248905a67cSAndy Whitcroft if ($level == 0) { 1525b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1526b998e001SPatrick Pannuto $off++; 1527b998e001SPatrick Pannuto } 15288905a67cSAndy Whitcroft last; 15298905a67cSAndy Whitcroft } 15308905a67cSAndy Whitcroft } 1531f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1532f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1533f74bd194SAndy Whitcroft $level--; 1534f74bd194SAndy Whitcroft $type = ''; 1535f74bd194SAndy Whitcroft $off++; 1536f74bd194SAndy Whitcroft last; 1537f74bd194SAndy Whitcroft } 15388905a67cSAndy Whitcroft $off++; 15398905a67cSAndy Whitcroft } 1540a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 154113214adfSAndy Whitcroft if ($off == $len) { 1542a3bb97a7SAndy Whitcroft $loff = $len + 1; 154313214adfSAndy Whitcroft $line++; 154413214adfSAndy Whitcroft $remain--; 154513214adfSAndy Whitcroft } 15468905a67cSAndy Whitcroft 15478905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 15488905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 15498905a67cSAndy Whitcroft 15508905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 15518905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 15528905a67cSAndy Whitcroft 1553773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 155413214adfSAndy Whitcroft 155513214adfSAndy Whitcroft return ($statement, $condition, 155613214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 155713214adfSAndy Whitcroft} 155813214adfSAndy Whitcroft 1559cf655043SAndy Whitcroftsub statement_lines { 1560cf655043SAndy Whitcroft my ($stmt) = @_; 1561cf655043SAndy Whitcroft 1562cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1563cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1564cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1565cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1566cf655043SAndy Whitcroft 1567cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1568cf655043SAndy Whitcroft 1569cf655043SAndy Whitcroft return $#stmt_lines + 2; 1570cf655043SAndy Whitcroft} 1571cf655043SAndy Whitcroft 1572cf655043SAndy Whitcroftsub statement_rawlines { 1573cf655043SAndy Whitcroft my ($stmt) = @_; 1574cf655043SAndy Whitcroft 1575cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1576cf655043SAndy Whitcroft 1577cf655043SAndy Whitcroft return $#stmt_lines + 2; 1578cf655043SAndy Whitcroft} 1579cf655043SAndy Whitcroft 1580cf655043SAndy Whitcroftsub statement_block_size { 1581cf655043SAndy Whitcroft my ($stmt) = @_; 1582cf655043SAndy Whitcroft 1583cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1584cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1585cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1586cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1587cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1588cf655043SAndy Whitcroft 1589cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1590cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1591cf655043SAndy Whitcroft 1592cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1593cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1594cf655043SAndy Whitcroft 1595cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1596cf655043SAndy Whitcroft return $stmt_lines; 1597cf655043SAndy Whitcroft } else { 1598cf655043SAndy Whitcroft return $stmt_statements; 1599cf655043SAndy Whitcroft } 1600cf655043SAndy Whitcroft} 1601cf655043SAndy Whitcroft 160213214adfSAndy Whitcroftsub ctx_statement_full { 160313214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 160413214adfSAndy Whitcroft my ($statement, $condition, $level); 160513214adfSAndy Whitcroft 160613214adfSAndy Whitcroft my (@chunks); 160713214adfSAndy Whitcroft 1608cf655043SAndy Whitcroft # Grab the first conditional/block pair. 160913214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 161013214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1611773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 161213214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1613cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1614cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1615cf655043SAndy Whitcroft } 1616cf655043SAndy Whitcroft 1617cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1618cf655043SAndy Whitcroft # could continue the statement. 1619cf655043SAndy Whitcroft for (;;) { 162013214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 162113214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1622cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1623773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1624cf655043SAndy Whitcroft #print "C: push\n"; 1625cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 162613214adfSAndy Whitcroft } 162713214adfSAndy Whitcroft 162813214adfSAndy Whitcroft return ($level, $linenr, @chunks); 16298905a67cSAndy Whitcroft} 16308905a67cSAndy Whitcroft 16314a0df2efSAndy Whitcroftsub ctx_block_get { 1632f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 16334a0df2efSAndy Whitcroft my $line; 16344a0df2efSAndy Whitcroft my $start = $linenr - 1; 16354a0df2efSAndy Whitcroft my $blk = ''; 16364a0df2efSAndy Whitcroft my @o; 16374a0df2efSAndy Whitcroft my @c; 16384a0df2efSAndy Whitcroft my @res = (); 16394a0df2efSAndy Whitcroft 1640f0a594c1SAndy Whitcroft my $level = 0; 16414635f4fbSAndy Whitcroft my @stack = ($level); 164200df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 164300df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 164400df344fSAndy Whitcroft $remain--; 164500df344fSAndy Whitcroft 164600df344fSAndy Whitcroft $blk .= $rawlines[$line]; 16474635f4fbSAndy Whitcroft 16484635f4fbSAndy Whitcroft # Handle nested #if/#else. 164901464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 16504635f4fbSAndy Whitcroft push(@stack, $level); 165101464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 16524635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 165301464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 16544635f4fbSAndy Whitcroft $level = pop(@stack); 16554635f4fbSAndy Whitcroft } 16564635f4fbSAndy Whitcroft 165701464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1658f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1659f0a594c1SAndy Whitcroft if ($off > 0) { 1660f0a594c1SAndy Whitcroft $off--; 1661f0a594c1SAndy Whitcroft next; 1662f0a594c1SAndy Whitcroft } 16634a0df2efSAndy Whitcroft 1664f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1665f0a594c1SAndy Whitcroft $level--; 1666f0a594c1SAndy Whitcroft last if ($level == 0); 1667f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1668f0a594c1SAndy Whitcroft $level++; 1669f0a594c1SAndy Whitcroft } 1670f0a594c1SAndy Whitcroft } 16714a0df2efSAndy Whitcroft 1672f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 167300df344fSAndy Whitcroft push(@res, $rawlines[$line]); 16744a0df2efSAndy Whitcroft } 16754a0df2efSAndy Whitcroft 1676f0a594c1SAndy Whitcroft last if ($level == 0); 16774a0df2efSAndy Whitcroft } 16784a0df2efSAndy Whitcroft 1679f0a594c1SAndy Whitcroft return ($level, @res); 16804a0df2efSAndy Whitcroft} 16814a0df2efSAndy Whitcroftsub ctx_block_outer { 16824a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16834a0df2efSAndy Whitcroft 1684f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1685f0a594c1SAndy Whitcroft return @r; 16864a0df2efSAndy Whitcroft} 16874a0df2efSAndy Whitcroftsub ctx_block { 16884a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16894a0df2efSAndy Whitcroft 1690f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1691f0a594c1SAndy Whitcroft return @r; 1692653d4876SAndy Whitcroft} 1693653d4876SAndy Whitcroftsub ctx_statement { 1694f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1695f0a594c1SAndy Whitcroft 1696f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1697f0a594c1SAndy Whitcroft return @r; 1698f0a594c1SAndy Whitcroft} 1699f0a594c1SAndy Whitcroftsub ctx_block_level { 1700653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1701653d4876SAndy Whitcroft 1702f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 17034a0df2efSAndy Whitcroft} 17049c0ca6f9SAndy Whitcroftsub ctx_statement_level { 17059c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 17069c0ca6f9SAndy Whitcroft 17079c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 17089c0ca6f9SAndy Whitcroft} 17094a0df2efSAndy Whitcroft 17104a0df2efSAndy Whitcroftsub ctx_locate_comment { 17114a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17124a0df2efSAndy Whitcroft 1713a55ee0ccSJoe Perches # If c99 comment on the current line, or the line before or after 1714a55ee0ccSJoe Perches my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); 1715a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1716a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); 1717a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1718a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); 1719a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1720a55ee0ccSJoe Perches 17214a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1722a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 17234a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 17244a0df2efSAndy Whitcroft 17254a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 17264a0df2efSAndy Whitcroft # comment. 17274a0df2efSAndy Whitcroft my $in_comment = 0; 17284a0df2efSAndy Whitcroft $current_comment = ''; 17294a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 173000df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 173100df344fSAndy Whitcroft #warn " $line\n"; 17324a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 17334a0df2efSAndy Whitcroft $in_comment = 1; 17344a0df2efSAndy Whitcroft } 17354a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 17364a0df2efSAndy Whitcroft $in_comment = 1; 17374a0df2efSAndy Whitcroft } 17384a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 17394a0df2efSAndy Whitcroft $current_comment = ''; 17404a0df2efSAndy Whitcroft } 17414a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 17424a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 17434a0df2efSAndy Whitcroft $in_comment = 0; 17444a0df2efSAndy Whitcroft } 17454a0df2efSAndy Whitcroft } 17464a0df2efSAndy Whitcroft 17474a0df2efSAndy Whitcroft chomp($current_comment); 17484a0df2efSAndy Whitcroft return($current_comment); 17494a0df2efSAndy Whitcroft} 17504a0df2efSAndy Whitcroftsub ctx_has_comment { 17514a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17524a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 17534a0df2efSAndy Whitcroft 175400df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 17554a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 17564a0df2efSAndy Whitcroft 17574a0df2efSAndy Whitcroft return ($cmt ne ''); 17584a0df2efSAndy Whitcroft} 17594a0df2efSAndy Whitcroft 17604d001e4dSAndy Whitcroftsub raw_line { 17614d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 17624d001e4dSAndy Whitcroft 17634d001e4dSAndy Whitcroft my $offset = $linenr - 1; 17644d001e4dSAndy Whitcroft $cnt++; 17654d001e4dSAndy Whitcroft 17664d001e4dSAndy Whitcroft my $line; 17674d001e4dSAndy Whitcroft while ($cnt) { 17684d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 17694d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 17704d001e4dSAndy Whitcroft $cnt--; 17714d001e4dSAndy Whitcroft } 17724d001e4dSAndy Whitcroft 17734d001e4dSAndy Whitcroft return $line; 17744d001e4dSAndy Whitcroft} 17754d001e4dSAndy Whitcroft 17762a9f9d85STobin C. Hardingsub get_stat_real { 17772a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 17782a9f9d85STobin C. Harding 17792a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 17802a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 17812a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 17822a9f9d85STobin C. Harding } 17832a9f9d85STobin C. Harding 17842a9f9d85STobin C. Harding return $stat_real; 17852a9f9d85STobin C. Harding} 17862a9f9d85STobin C. Harding 1787e3d95a2aSTobin C. Hardingsub get_stat_here { 1788e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1789e3d95a2aSTobin C. Harding 1790e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1791e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1792e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1793e3d95a2aSTobin C. Harding } 1794e3d95a2aSTobin C. Harding 1795e3d95a2aSTobin C. Harding return $herectx; 1796e3d95a2aSTobin C. Harding} 1797e3d95a2aSTobin C. Harding 17980a920b5bSAndy Whitcroftsub cat_vet { 17990a920b5bSAndy Whitcroft my ($vet) = @_; 18009c0ca6f9SAndy Whitcroft my ($res, $coded); 18010a920b5bSAndy Whitcroft 18029c0ca6f9SAndy Whitcroft $res = ''; 18036c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 18046c72ffaaSAndy Whitcroft $res .= $1; 18056c72ffaaSAndy Whitcroft if ($2 ne '') { 18069c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 18076c72ffaaSAndy Whitcroft $res .= $coded; 18086c72ffaaSAndy Whitcroft } 18099c0ca6f9SAndy Whitcroft } 18109c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 18110a920b5bSAndy Whitcroft 18129c0ca6f9SAndy Whitcroft return $res; 18130a920b5bSAndy Whitcroft} 18140a920b5bSAndy Whitcroft 1815c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1816cf655043SAndy Whitcroftmy $av_pending; 1817c2fdda0dSAndy Whitcroftmy @av_paren_type; 18181f65f947SAndy Whitcroftmy $av_pend_colon; 1819c2fdda0dSAndy Whitcroft 1820c2fdda0dSAndy Whitcroftsub annotate_reset { 1821c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1822cf655043SAndy Whitcroft $av_pending = '_'; 1823cf655043SAndy Whitcroft @av_paren_type = ('E'); 18241f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1825c2fdda0dSAndy Whitcroft} 1826c2fdda0dSAndy Whitcroft 18276c72ffaaSAndy Whitcroftsub annotate_values { 18286c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 18296c72ffaaSAndy Whitcroft 18306c72ffaaSAndy Whitcroft my $res; 18311f65f947SAndy Whitcroft my $var = '_' x length($stream); 18326c72ffaaSAndy Whitcroft my $cur = $stream; 18336c72ffaaSAndy Whitcroft 1834c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 18356c72ffaaSAndy Whitcroft 18366c72ffaaSAndy Whitcroft while (length($cur)) { 1837773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1838cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1839171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 18406c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1841c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1842c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1843cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1844c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 18456c72ffaaSAndy Whitcroft } 18466c72ffaaSAndy Whitcroft 1847c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 18489446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 18499446ef56SAndy Whitcroft push(@av_paren_type, $type); 1850addcdceaSAndy Whitcroft $type = 'c'; 18519446ef56SAndy Whitcroft 1852e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1853c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 18546c72ffaaSAndy Whitcroft $type = 'T'; 18556c72ffaaSAndy Whitcroft 1856389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1857389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1858389a2fe5SAndy Whitcroft $type = 'T'; 1859389a2fe5SAndy Whitcroft 1860c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1861171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1862c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1863171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1864171ae1a4SAndy Whitcroft if ($2 ne '') { 1865cf655043SAndy Whitcroft $av_pending = 'N'; 1866171ae1a4SAndy Whitcroft } 1867171ae1a4SAndy Whitcroft $type = 'E'; 1868171ae1a4SAndy Whitcroft 1869c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1870171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1871171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1872171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 18736c72ffaaSAndy Whitcroft 1874c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1875cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1876c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1877cf655043SAndy Whitcroft 1878cf655043SAndy Whitcroft push(@av_paren_type, $type); 1879cf655043SAndy Whitcroft push(@av_paren_type, $type); 1880171ae1a4SAndy Whitcroft $type = 'E'; 1881cf655043SAndy Whitcroft 1882c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1883cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1884cf655043SAndy Whitcroft $av_preprocessor = 1; 1885cf655043SAndy Whitcroft 1886cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1887cf655043SAndy Whitcroft 1888171ae1a4SAndy Whitcroft $type = 'E'; 1889cf655043SAndy Whitcroft 1890c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1891cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1892cf655043SAndy Whitcroft 1893cf655043SAndy Whitcroft $av_preprocessor = 1; 1894cf655043SAndy Whitcroft 1895cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1896cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1897cf655043SAndy Whitcroft pop(@av_paren_type); 1898cf655043SAndy Whitcroft push(@av_paren_type, $type); 1899171ae1a4SAndy Whitcroft $type = 'E'; 19006c72ffaaSAndy Whitcroft 19016c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1902c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 19036c72ffaaSAndy Whitcroft 1904171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1905171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1906171ae1a4SAndy Whitcroft $av_pending = $type; 1907171ae1a4SAndy Whitcroft $type = 'N'; 1908171ae1a4SAndy Whitcroft 19096c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1910c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 19116c72ffaaSAndy Whitcroft if (defined $2) { 1912cf655043SAndy Whitcroft $av_pending = 'V'; 19136c72ffaaSAndy Whitcroft } 19146c72ffaaSAndy Whitcroft $type = 'N'; 19156c72ffaaSAndy Whitcroft 191614b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1917c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 191814b111c1SAndy Whitcroft $av_pending = 'E'; 19196c72ffaaSAndy Whitcroft $type = 'N'; 19206c72ffaaSAndy Whitcroft 19211f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 19221f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 19231f65f947SAndy Whitcroft $av_pend_colon = 'C'; 19241f65f947SAndy Whitcroft $type = 'N'; 19251f65f947SAndy Whitcroft 192614b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1927c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 19286c72ffaaSAndy Whitcroft $type = 'N'; 19296c72ffaaSAndy Whitcroft 19306c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1931c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1932cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1933cf655043SAndy Whitcroft $av_pending = '_'; 19346c72ffaaSAndy Whitcroft $type = 'N'; 19356c72ffaaSAndy Whitcroft 19366c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1937cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1938cf655043SAndy Whitcroft if ($new_type ne '_') { 1939cf655043SAndy Whitcroft $type = $new_type; 1940c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1941c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 19426c72ffaaSAndy Whitcroft } else { 1943c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 19446c72ffaaSAndy Whitcroft } 19456c72ffaaSAndy Whitcroft 1946c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1947c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1948c8cb2ca3SAndy Whitcroft $type = 'V'; 1949cf655043SAndy Whitcroft $av_pending = 'V'; 19506c72ffaaSAndy Whitcroft 19518e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 19528e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 19531f65f947SAndy Whitcroft $av_pend_colon = 'B'; 19548e761b04SAndy Whitcroft } elsif ($type eq 'E') { 19558e761b04SAndy Whitcroft $av_pend_colon = 'L'; 19561f65f947SAndy Whitcroft } 19571f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 19581f65f947SAndy Whitcroft $type = 'V'; 19591f65f947SAndy Whitcroft 19606c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1961c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 19626c72ffaaSAndy Whitcroft $type = 'V'; 19636c72ffaaSAndy Whitcroft 19646c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1965c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 19666c72ffaaSAndy Whitcroft $type = 'N'; 19676c72ffaaSAndy Whitcroft 1968cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1969c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 197013214adfSAndy Whitcroft $type = 'E'; 19711f65f947SAndy Whitcroft $av_pend_colon = 'O'; 197213214adfSAndy Whitcroft 19738e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 19748e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 19758e761b04SAndy Whitcroft $type = 'C'; 19768e761b04SAndy Whitcroft 19771f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 19781f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 19791f65f947SAndy Whitcroft $type = 'N'; 19801f65f947SAndy Whitcroft 19811f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 19821f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 19831f65f947SAndy Whitcroft 19841f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 19851f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 19861f65f947SAndy Whitcroft $type = 'E'; 19871f65f947SAndy Whitcroft } else { 19881f65f947SAndy Whitcroft $type = 'N'; 19891f65f947SAndy Whitcroft } 19901f65f947SAndy Whitcroft $av_pend_colon = 'O'; 19911f65f947SAndy Whitcroft 19928e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 199313214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 19946c72ffaaSAndy Whitcroft $type = 'N'; 19956c72ffaaSAndy Whitcroft 19960d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 199774048ed8SAndy Whitcroft my $variant; 199874048ed8SAndy Whitcroft 199974048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 200074048ed8SAndy Whitcroft if ($type eq 'V') { 200174048ed8SAndy Whitcroft $variant = 'B'; 200274048ed8SAndy Whitcroft } else { 200374048ed8SAndy Whitcroft $variant = 'U'; 200474048ed8SAndy Whitcroft } 200574048ed8SAndy Whitcroft 200674048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 200774048ed8SAndy Whitcroft $type = 'N'; 200874048ed8SAndy Whitcroft 20096c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 2010c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 20116c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 20126c72ffaaSAndy Whitcroft $type = 'N'; 20136c72ffaaSAndy Whitcroft } 20146c72ffaaSAndy Whitcroft 20156c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 2016c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 20176c72ffaaSAndy Whitcroft } 20186c72ffaaSAndy Whitcroft if (defined $1) { 20196c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 20206c72ffaaSAndy Whitcroft $res .= $type x length($1); 20216c72ffaaSAndy Whitcroft } 20226c72ffaaSAndy Whitcroft } 20236c72ffaaSAndy Whitcroft 20241f65f947SAndy Whitcroft return ($res, $var); 20256c72ffaaSAndy Whitcroft} 20266c72ffaaSAndy Whitcroft 20278905a67cSAndy Whitcroftsub possible { 202813214adfSAndy Whitcroft my ($possible, $line) = @_; 20299a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 20300776e594SAndy Whitcroft ^(?: 20310776e594SAndy Whitcroft $Modifier| 20320776e594SAndy Whitcroft $Storage| 20330776e594SAndy Whitcroft $Type| 20349a974fdbSAndy Whitcroft DEFINE_\S+ 20359a974fdbSAndy Whitcroft )$| 20369a974fdbSAndy Whitcroft ^(?: 20370776e594SAndy Whitcroft goto| 20380776e594SAndy Whitcroft return| 20390776e594SAndy Whitcroft case| 20400776e594SAndy Whitcroft else| 20410776e594SAndy Whitcroft asm|__asm__| 204289a88353SAndy Whitcroft do| 204389a88353SAndy Whitcroft \#| 204489a88353SAndy Whitcroft \#\#| 20459a974fdbSAndy Whitcroft )(?:\s|$)| 20460776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 20479a974fdbSAndy Whitcroft )}x; 20489a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 20499a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2050c45dcabdSAndy Whitcroft # Check for modifiers. 2051c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2052c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2053c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2054c45dcabdSAndy Whitcroft 2055c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2056c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2057d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 20589a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2059d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2060485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2061d2506586SAndy Whitcroft } 20629a974fdbSAndy Whitcroft } 2063c45dcabdSAndy Whitcroft 2064c45dcabdSAndy Whitcroft } else { 206513214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2066485ff23eSAlex Dowad push(@typeListFile, $possible); 2067c45dcabdSAndy Whitcroft } 20688905a67cSAndy Whitcroft build_types(); 20690776e594SAndy Whitcroft } else { 20700776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 20718905a67cSAndy Whitcroft } 20728905a67cSAndy Whitcroft} 20738905a67cSAndy Whitcroft 20746c72ffaaSAndy Whitcroftmy $prefix = ''; 20756c72ffaaSAndy Whitcroft 2076000d1cc1SJoe Perchessub show_type { 2077cbec18afSJoe Perches my ($type) = @_; 207891bfe484SJoe Perches 2079522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2080522b837cSAlexey Dobriyan 2081cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2082cbec18afSJoe Perches 2083cbec18afSJoe Perches return !defined $ignore_type{$type}; 2084000d1cc1SJoe Perches} 2085000d1cc1SJoe Perches 2086f0a594c1SAndy Whitcroftsub report { 2087cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2088cbec18afSJoe Perches 2089cbec18afSJoe Perches if (!show_type($type) || 2090cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2091773647a0SAndy Whitcroft return 0; 2092773647a0SAndy Whitcroft } 209357230297SJoe Perches my $output = ''; 2094737c0767SJohn Brooks if ($color) { 209557230297SJoe Perches if ($level eq 'ERROR') { 209657230297SJoe Perches $output .= RED; 209757230297SJoe Perches } elsif ($level eq 'WARNING') { 209857230297SJoe Perches $output .= YELLOW; 2099000d1cc1SJoe Perches } else { 210057230297SJoe Perches $output .= GREEN; 2101000d1cc1SJoe Perches } 210257230297SJoe Perches } 210357230297SJoe Perches $output .= $prefix . $level . ':'; 210457230297SJoe Perches if ($show_types) { 2105737c0767SJohn Brooks $output .= BLUE if ($color); 210657230297SJoe Perches $output .= "$type:"; 210757230297SJoe Perches } 2108737c0767SJohn Brooks $output .= RESET if ($color); 210957230297SJoe Perches $output .= ' ' . $msg . "\n"; 211034d8815fSJoe Perches 211134d8815fSJoe Perches if ($showfile) { 211234d8815fSJoe Perches my @lines = split("\n", $output, -1); 211334d8815fSJoe Perches splice(@lines, 1, 1); 211434d8815fSJoe Perches $output = join("\n", @lines); 211534d8815fSJoe Perches } 211657230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 21178905a67cSAndy Whitcroft 211857230297SJoe Perches push(our @report, $output); 2119773647a0SAndy Whitcroft 2120773647a0SAndy Whitcroft return 1; 2121f0a594c1SAndy Whitcroft} 2122cbec18afSJoe Perches 2123f0a594c1SAndy Whitcroftsub report_dump { 212413214adfSAndy Whitcroft our @report; 2125f0a594c1SAndy Whitcroft} 2126000d1cc1SJoe Perches 2127d752fcc8SJoe Perchessub fixup_current_range { 2128d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2129d752fcc8SJoe Perches 2130d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2131d752fcc8SJoe Perches my $o = $1; 2132d752fcc8SJoe Perches my $l = $2; 2133d752fcc8SJoe Perches my $no = $o + $offset; 2134d752fcc8SJoe Perches my $nl = $l + $length; 2135d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2136d752fcc8SJoe Perches } 2137d752fcc8SJoe Perches} 2138d752fcc8SJoe Perches 2139d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2140d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2141d752fcc8SJoe Perches 2142d752fcc8SJoe Perches my $range_last_linenr = 0; 2143d752fcc8SJoe Perches my $delta_offset = 0; 2144d752fcc8SJoe Perches 2145d752fcc8SJoe Perches my $old_linenr = 0; 2146d752fcc8SJoe Perches my $new_linenr = 0; 2147d752fcc8SJoe Perches 2148d752fcc8SJoe Perches my $next_insert = 0; 2149d752fcc8SJoe Perches my $next_delete = 0; 2150d752fcc8SJoe Perches 2151d752fcc8SJoe Perches my @lines = (); 2152d752fcc8SJoe Perches 2153d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2154d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2155d752fcc8SJoe Perches 2156d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2157d752fcc8SJoe Perches my $save_line = 1; 2158d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2159323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2160d752fcc8SJoe Perches $delta_offset = 0; 2161d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2162d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2163d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2164d752fcc8SJoe Perches } 2165d752fcc8SJoe Perches 2166d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2167d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2168d752fcc8SJoe Perches $save_line = 0; 2169d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2170d752fcc8SJoe Perches } 2171d752fcc8SJoe Perches 2172d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2173d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2174d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2175d752fcc8SJoe Perches $new_linenr++; 2176d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2177d752fcc8SJoe Perches } 2178d752fcc8SJoe Perches 2179d752fcc8SJoe Perches if ($save_line) { 2180d752fcc8SJoe Perches push(@lines, $line); 2181d752fcc8SJoe Perches $new_linenr++; 2182d752fcc8SJoe Perches } 2183d752fcc8SJoe Perches 2184d752fcc8SJoe Perches $old_linenr++; 2185d752fcc8SJoe Perches } 2186d752fcc8SJoe Perches 2187d752fcc8SJoe Perches return @lines; 2188d752fcc8SJoe Perches} 2189d752fcc8SJoe Perches 2190f2d7e4d4SJoe Perchessub fix_insert_line { 2191f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2192f2d7e4d4SJoe Perches 2193f2d7e4d4SJoe Perches my $inserted = { 2194f2d7e4d4SJoe Perches LINENR => $linenr, 2195f2d7e4d4SJoe Perches LINE => $line, 2196f2d7e4d4SJoe Perches }; 2197f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2198f2d7e4d4SJoe Perches} 2199f2d7e4d4SJoe Perches 2200f2d7e4d4SJoe Perchessub fix_delete_line { 2201f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2202f2d7e4d4SJoe Perches 2203f2d7e4d4SJoe Perches my $deleted = { 2204f2d7e4d4SJoe Perches LINENR => $linenr, 2205f2d7e4d4SJoe Perches LINE => $line, 2206f2d7e4d4SJoe Perches }; 2207f2d7e4d4SJoe Perches 2208f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2209f2d7e4d4SJoe Perches} 2210f2d7e4d4SJoe Perches 2211de7d4f0eSAndy Whitcroftsub ERROR { 2212cbec18afSJoe Perches my ($type, $msg) = @_; 2213cbec18afSJoe Perches 2214cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2215de7d4f0eSAndy Whitcroft our $clean = 0; 22166c72ffaaSAndy Whitcroft our $cnt_error++; 22173705ce5bSJoe Perches return 1; 2218de7d4f0eSAndy Whitcroft } 22193705ce5bSJoe Perches return 0; 2220773647a0SAndy Whitcroft} 2221de7d4f0eSAndy Whitcroftsub WARN { 2222cbec18afSJoe Perches my ($type, $msg) = @_; 2223cbec18afSJoe Perches 2224cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2225de7d4f0eSAndy Whitcroft our $clean = 0; 22266c72ffaaSAndy Whitcroft our $cnt_warn++; 22273705ce5bSJoe Perches return 1; 2228de7d4f0eSAndy Whitcroft } 22293705ce5bSJoe Perches return 0; 2230773647a0SAndy Whitcroft} 2231de7d4f0eSAndy Whitcroftsub CHK { 2232cbec18afSJoe Perches my ($type, $msg) = @_; 2233cbec18afSJoe Perches 2234cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2235de7d4f0eSAndy Whitcroft our $clean = 0; 22366c72ffaaSAndy Whitcroft our $cnt_chk++; 22373705ce5bSJoe Perches return 1; 22386c72ffaaSAndy Whitcroft } 22393705ce5bSJoe Perches return 0; 2240de7d4f0eSAndy Whitcroft} 2241de7d4f0eSAndy Whitcroft 22426ecd9674SAndy Whitcroftsub check_absolute_file { 22436ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 22446ecd9674SAndy Whitcroft my $file = $absolute; 22456ecd9674SAndy Whitcroft 22466ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 22476ecd9674SAndy Whitcroft 22486ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 22496ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 22506ecd9674SAndy Whitcroft if (-f "$root/$file") { 22516ecd9674SAndy Whitcroft ##print "file<$file>\n"; 22526ecd9674SAndy Whitcroft last; 22536ecd9674SAndy Whitcroft } 22546ecd9674SAndy Whitcroft } 22556ecd9674SAndy Whitcroft if (! -f _) { 22566ecd9674SAndy Whitcroft return 0; 22576ecd9674SAndy Whitcroft } 22586ecd9674SAndy Whitcroft 22596ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 22606ecd9674SAndy Whitcroft my $prefix = $absolute; 22616ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 22626ecd9674SAndy Whitcroft 22636ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 22646ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2265000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2266000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 22676ecd9674SAndy Whitcroft } 22686ecd9674SAndy Whitcroft} 22696ecd9674SAndy Whitcroft 22703705ce5bSJoe Perchessub trim { 22713705ce5bSJoe Perches my ($string) = @_; 22723705ce5bSJoe Perches 2273b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2274b34c648bSJoe Perches 2275b34c648bSJoe Perches return $string; 2276b34c648bSJoe Perches} 2277b34c648bSJoe Perches 2278b34c648bSJoe Perchessub ltrim { 2279b34c648bSJoe Perches my ($string) = @_; 2280b34c648bSJoe Perches 2281b34c648bSJoe Perches $string =~ s/^\s+//; 2282b34c648bSJoe Perches 2283b34c648bSJoe Perches return $string; 2284b34c648bSJoe Perches} 2285b34c648bSJoe Perches 2286b34c648bSJoe Perchessub rtrim { 2287b34c648bSJoe Perches my ($string) = @_; 2288b34c648bSJoe Perches 2289b34c648bSJoe Perches $string =~ s/\s+$//; 22903705ce5bSJoe Perches 22913705ce5bSJoe Perches return $string; 22923705ce5bSJoe Perches} 22933705ce5bSJoe Perches 229452ea8506SJoe Perchessub string_find_replace { 229552ea8506SJoe Perches my ($string, $find, $replace) = @_; 229652ea8506SJoe Perches 229752ea8506SJoe Perches $string =~ s/$find/$replace/g; 229852ea8506SJoe Perches 229952ea8506SJoe Perches return $string; 230052ea8506SJoe Perches} 230152ea8506SJoe Perches 23023705ce5bSJoe Perchessub tabify { 23033705ce5bSJoe Perches my ($leading) = @_; 23043705ce5bSJoe Perches 2305713a09deSAntonio Borneo my $source_indent = $tabsize; 23063705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 23073705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 23083705ce5bSJoe Perches 23093705ce5bSJoe Perches #convert leading spaces to tabs 23103705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 23113705ce5bSJoe Perches #Remove spaces before a tab 23123705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 23133705ce5bSJoe Perches 23143705ce5bSJoe Perches return "$leading"; 23153705ce5bSJoe Perches} 23163705ce5bSJoe Perches 2317d1fe9c09SJoe Perchessub pos_last_openparen { 2318d1fe9c09SJoe Perches my ($line) = @_; 2319d1fe9c09SJoe Perches 2320d1fe9c09SJoe Perches my $pos = 0; 2321d1fe9c09SJoe Perches 2322d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2323d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2324d1fe9c09SJoe Perches 2325d1fe9c09SJoe Perches my $last_openparen = 0; 2326d1fe9c09SJoe Perches 2327d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2328d1fe9c09SJoe Perches return -1; 2329d1fe9c09SJoe Perches } 2330d1fe9c09SJoe Perches 2331d1fe9c09SJoe Perches my $len = length($line); 2332d1fe9c09SJoe Perches 2333d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2334d1fe9c09SJoe Perches my $string = substr($line, $pos); 2335d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2336d1fe9c09SJoe Perches $pos += length($1) - 1; 2337d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2338d1fe9c09SJoe Perches $last_openparen = $pos; 2339d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2340d1fe9c09SJoe Perches last; 2341d1fe9c09SJoe Perches } 2342d1fe9c09SJoe Perches } 2343d1fe9c09SJoe Perches 234491cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2345d1fe9c09SJoe Perches} 2346d1fe9c09SJoe Perches 2347f36d3eb8SJoe Perchessub get_raw_comment { 2348f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2349f36d3eb8SJoe Perches my $comment = ''; 2350f36d3eb8SJoe Perches 2351f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2352f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2353f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2354f36d3eb8SJoe Perches } 2355f36d3eb8SJoe Perches } 2356f36d3eb8SJoe Perches 2357f36d3eb8SJoe Perches return $comment; 2358f36d3eb8SJoe Perches} 2359f36d3eb8SJoe Perches 23600a920b5bSAndy Whitcroftsub process { 23610a920b5bSAndy Whitcroft my $filename = shift; 23620a920b5bSAndy Whitcroft 23630a920b5bSAndy Whitcroft my $linenr=0; 23640a920b5bSAndy Whitcroft my $prevline=""; 2365c2fdda0dSAndy Whitcroft my $prevrawline=""; 23660a920b5bSAndy Whitcroft my $stashline=""; 2367c2fdda0dSAndy Whitcroft my $stashrawline=""; 23680a920b5bSAndy Whitcroft 23694a0df2efSAndy Whitcroft my $length; 23700a920b5bSAndy Whitcroft my $indent; 23710a920b5bSAndy Whitcroft my $previndent=0; 23720a920b5bSAndy Whitcroft my $stashindent=0; 23730a920b5bSAndy Whitcroft 2374de7d4f0eSAndy Whitcroft our $clean = 1; 23750a920b5bSAndy Whitcroft my $signoff = 0; 2376cd261496SGeert Uytterhoeven my $author = ''; 2377cd261496SGeert Uytterhoeven my $authorsignoff = 0; 237848ca2d8aSDwaipayan Ray my $author_sob = ''; 23790a920b5bSAndy Whitcroft my $is_patch = 0; 2380133712a2SRob Herring my $is_binding_patch = -1; 238129ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 238215662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 238344d303ebSJoe Perches my $has_patch_separator = 0; #Found a --- line 2384ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2385490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2386bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 23872a076f40SJoe Perches my $commit_log_long_line = 0; 2388e518e9a5SJoe Perches my $commit_log_has_diff = 0; 238913f1937eSJoe Perches my $reported_maintainer_file = 0; 2390fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2391fa64205dSPasi Savanainen 2392365dd4eaSJoe Perches my $last_blank_line = 0; 23935e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2394365dd4eaSJoe Perches 239513214adfSAndy Whitcroft our @report = (); 23966c72ffaaSAndy Whitcroft our $cnt_lines = 0; 23976c72ffaaSAndy Whitcroft our $cnt_error = 0; 23986c72ffaaSAndy Whitcroft our $cnt_warn = 0; 23996c72ffaaSAndy Whitcroft our $cnt_chk = 0; 24006c72ffaaSAndy Whitcroft 24010a920b5bSAndy Whitcroft # Trace the real file/line as we go. 24020a920b5bSAndy Whitcroft my $realfile = ''; 24030a920b5bSAndy Whitcroft my $realline = 0; 24040a920b5bSAndy Whitcroft my $realcnt = 0; 24050a920b5bSAndy Whitcroft my $here = ''; 240677cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 24070a920b5bSAndy Whitcroft my $in_comment = 0; 2408c2fdda0dSAndy Whitcroft my $comment_edge = 0; 24090a920b5bSAndy Whitcroft my $first_line = 0; 24101e855726SWolfram Sang my $p1_prefix = ''; 24110a920b5bSAndy Whitcroft 241213214adfSAndy Whitcroft my $prev_values = 'E'; 241313214adfSAndy Whitcroft 241413214adfSAndy Whitcroft # suppression flags 2415773647a0SAndy Whitcroft my %suppress_ifbraces; 2416170d3a22SAndy Whitcroft my %suppress_whiletrailers; 24172b474a1aSAndy Whitcroft my %suppress_export; 24183e469cdcSAndy Whitcroft my $suppress_statement = 0; 2419653d4876SAndy Whitcroft 24207e51f197SJoe Perches my %signatures = (); 2421323c1260SJoe Perches 2422c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2423de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2424c2fdda0dSAndy Whitcroft # 2425de7d4f0eSAndy Whitcroft my @setup_docs = (); 2426de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2427773647a0SAndy Whitcroft 2428d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2429d8b07710SJoe Perches 24309f3a8992SRob Herring my $checklicenseline = 1; 24319f3a8992SRob Herring 2432773647a0SAndy Whitcroft sanitise_line_reset(); 2433c2fdda0dSAndy Whitcroft my $line; 2434c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2435773647a0SAndy Whitcroft $linenr++; 2436773647a0SAndy Whitcroft $line = $rawline; 2437c2fdda0dSAndy Whitcroft 24383705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 24393705ce5bSJoe Perches 2440773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2441de7d4f0eSAndy Whitcroft $setup_docs = 0; 24422581ac7cSTim Froidcoeur if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { 2443de7d4f0eSAndy Whitcroft $setup_docs = 1; 2444de7d4f0eSAndy Whitcroft } 2445773647a0SAndy Whitcroft #next; 2446de7d4f0eSAndy Whitcroft } 244774fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2448773647a0SAndy Whitcroft $realline=$1-1; 2449773647a0SAndy Whitcroft if (defined $2) { 2450773647a0SAndy Whitcroft $realcnt=$3+1; 2451773647a0SAndy Whitcroft } else { 2452773647a0SAndy Whitcroft $realcnt=1+1; 2453773647a0SAndy Whitcroft } 2454c45dcabdSAndy Whitcroft $in_comment = 0; 2455773647a0SAndy Whitcroft 2456773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2457773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2458773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2459773647a0SAndy Whitcroft # at context start. 2460773647a0SAndy Whitcroft my $edge; 246101fa9147SAndy Whitcroft my $cnt = $realcnt; 246201fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 246301fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 246401fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 246501fa9147SAndy Whitcroft $cnt--; 246601fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2467721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2468fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2469fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2470fae17daeSAndy Whitcroft ($edge) = $1; 2471fae17daeSAndy Whitcroft last; 2472fae17daeSAndy Whitcroft } 2473773647a0SAndy Whitcroft } 2474773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2475773647a0SAndy Whitcroft $in_comment = 1; 2476773647a0SAndy Whitcroft } 2477773647a0SAndy Whitcroft 2478773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2479773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2480773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2481773647a0SAndy Whitcroft if (!defined $edge && 248283242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2483773647a0SAndy Whitcroft { 2484773647a0SAndy Whitcroft $in_comment = 1; 2485773647a0SAndy Whitcroft } 2486773647a0SAndy Whitcroft 2487773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2488773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2489773647a0SAndy Whitcroft 2490171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2491773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2492171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2493773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2494773647a0SAndy Whitcroft } 2495773647a0SAndy Whitcroft push(@lines, $line); 2496773647a0SAndy Whitcroft 2497773647a0SAndy Whitcroft if ($realcnt > 1) { 2498773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2499773647a0SAndy Whitcroft } else { 2500773647a0SAndy Whitcroft $realcnt = 0; 2501773647a0SAndy Whitcroft } 2502773647a0SAndy Whitcroft 2503773647a0SAndy Whitcroft #print "==>$rawline\n"; 2504773647a0SAndy Whitcroft #print "-->$line\n"; 2505de7d4f0eSAndy Whitcroft 2506de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2507de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2508de7d4f0eSAndy Whitcroft } 2509de7d4f0eSAndy Whitcroft } 2510de7d4f0eSAndy Whitcroft 25116c72ffaaSAndy Whitcroft $prefix = ''; 25126c72ffaaSAndy Whitcroft 2513773647a0SAndy Whitcroft $realcnt = 0; 2514773647a0SAndy Whitcroft $linenr = 0; 2515194f66fcSJoe Perches $fixlinenr = -1; 25160a920b5bSAndy Whitcroft foreach my $line (@lines) { 25170a920b5bSAndy Whitcroft $linenr++; 2518194f66fcSJoe Perches $fixlinenr++; 25191b5539b1SJoe Perches my $sline = $line; #copy of $line 25201b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 25210a920b5bSAndy Whitcroft 2522c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2523f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 25246c72ffaaSAndy Whitcroft 252512c253abSJoe Perches# check if it's a mode change, rename or start of a patch 252612c253abSJoe Perches if (!$in_commit_log && 252712c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 252812c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 252912c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 253012c253abSJoe Perches $is_patch = 1; 253112c253abSJoe Perches } 253212c253abSJoe Perches 25330a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2534e518e9a5SJoe Perches if (!$in_commit_log && 253574fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 253674fd4f34SJoe Perches my $context = $4; 25370a920b5bSAndy Whitcroft $is_patch = 1; 25384a0df2efSAndy Whitcroft $first_line = $linenr + 1; 25390a920b5bSAndy Whitcroft $realline=$1-1; 25400a920b5bSAndy Whitcroft if (defined $2) { 25410a920b5bSAndy Whitcroft $realcnt=$3+1; 25420a920b5bSAndy Whitcroft } else { 25430a920b5bSAndy Whitcroft $realcnt=1+1; 25440a920b5bSAndy Whitcroft } 2545c2fdda0dSAndy Whitcroft annotate_reset(); 254613214adfSAndy Whitcroft $prev_values = 'E'; 254713214adfSAndy Whitcroft 2548773647a0SAndy Whitcroft %suppress_ifbraces = (); 2549170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 25502b474a1aSAndy Whitcroft %suppress_export = (); 25513e469cdcSAndy Whitcroft $suppress_statement = 0; 255274fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 255374fd4f34SJoe Perches $context_function = $1; 255474fd4f34SJoe Perches } else { 255574fd4f34SJoe Perches undef $context_function; 255674fd4f34SJoe Perches } 25570a920b5bSAndy Whitcroft next; 25580a920b5bSAndy Whitcroft 25594a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 25604a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 25614a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2562773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 25630a920b5bSAndy Whitcroft $realline++; 2564d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 25650a920b5bSAndy Whitcroft 25664a0df2efSAndy Whitcroft # Measure the line length and indent. 2567c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 25680a920b5bSAndy Whitcroft 25690a920b5bSAndy Whitcroft # Track the previous line. 25700a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 25710a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2572c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2573c2fdda0dSAndy Whitcroft 2574773647a0SAndy Whitcroft #warn "line<$line>\n"; 25756c72ffaaSAndy Whitcroft 2576d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2577d8aaf121SAndy Whitcroft $realcnt--; 25780a920b5bSAndy Whitcroft } 25790a920b5bSAndy Whitcroft 2580cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2581cc77cdcaSAndy Whitcroft 25826c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 25836c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2584773647a0SAndy Whitcroft 25852ac73b4fSJoe Perches my $found_file = 0; 2586773647a0SAndy Whitcroft # extract the filename as it passes 25873bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 25883bf9a009SRabin Vincent $realfile = $1; 25892b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2590270c49a0SJoe Perches $in_commit_log = 0; 25912ac73b4fSJoe Perches $found_file = 1; 25923bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2593773647a0SAndy Whitcroft $realfile = $1; 25942b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2595270c49a0SJoe Perches $in_commit_log = 0; 25961e855726SWolfram Sang 25971e855726SWolfram Sang $p1_prefix = $1; 2598e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2599e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2600000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2601000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 26021e855726SWolfram Sang } 2603773647a0SAndy Whitcroft 2604c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2605000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2606000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2607773647a0SAndy Whitcroft } 26082ac73b4fSJoe Perches $found_file = 1; 26092ac73b4fSJoe Perches } 26102ac73b4fSJoe Perches 261134d8815fSJoe Perches#make up the handle for any error we report on this line 261234d8815fSJoe Perches if ($showfile) { 261334d8815fSJoe Perches $prefix = "$realfile:$realline: " 261434d8815fSJoe Perches } elsif ($emacs) { 26157d3a9f67SJoe Perches if ($file) { 26167d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 26177d3a9f67SJoe Perches } else { 261834d8815fSJoe Perches $prefix = "$filename:$linenr: "; 261934d8815fSJoe Perches } 26207d3a9f67SJoe Perches } 262134d8815fSJoe Perches 26222ac73b4fSJoe Perches if ($found_file) { 262385b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 262485b0ee18SJoe Perches WARN("OBSOLETE", 262585b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 262685b0ee18SJoe Perches } 26277bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 26282ac73b4fSJoe Perches $check = 1; 26292ac73b4fSJoe Perches } else { 26302ac73b4fSJoe Perches $check = $check_orig; 26312ac73b4fSJoe Perches } 26329f3a8992SRob Herring $checklicenseline = 1; 2633133712a2SRob Herring 2634133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2635133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2636133712a2SRob Herring 2637133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2638133712a2SRob Herring 2639133712a2SRob Herring if (($last_binding_patch != -1) && 2640133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2641133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2642858e6845SMauro Carvalho Chehab "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); 2643133712a2SRob Herring } 2644133712a2SRob Herring } 2645133712a2SRob Herring 2646773647a0SAndy Whitcroft next; 2647773647a0SAndy Whitcroft } 2648773647a0SAndy Whitcroft 2649389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 26500a920b5bSAndy Whitcroft 2651c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2652c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2653c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 26540a920b5bSAndy Whitcroft 26556c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 26566c72ffaaSAndy Whitcroft 2657490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2658490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2659490b292cSJoe Perches if ($in_commit_log) { 2660490b292cSJoe Perches if ($line !~ /^\s*$/) { 2661490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2662490b292cSJoe Perches } 2663490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2664490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2665490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2666490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2667490b292cSJoe Perches } 2668490b292cSJoe Perches 2669e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2670e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 267113e45417SMrinal Pandey (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && 267213e45417SMrinal Pandey $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || 2673e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2674e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2675e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2676e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2677e518e9a5SJoe Perches $commit_log_has_diff = 1; 2678e518e9a5SJoe Perches } 2679e518e9a5SJoe Perches 26803bf9a009SRabin Vincent# Check for incorrect file permissions 26813bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 26823bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 268304db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 268404db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2685000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2686000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 26873bf9a009SRabin Vincent } 26883bf9a009SRabin Vincent } 26893bf9a009SRabin Vincent 2690cd261496SGeert Uytterhoeven# Check the patch for a From: 2691cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2692cd261496SGeert Uytterhoeven $author = $1; 2693e7f929f3SDwaipayan Ray my $curline = $linenr; 2694e7f929f3SDwaipayan Ray while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { 2695e7f929f3SDwaipayan Ray $author .= $1; 2696e7f929f3SDwaipayan Ray } 2697cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2698cd261496SGeert Uytterhoeven $author =~ s/"//g; 2699dfa05c28SJoe Perches $author = reformat_email($author); 2700cd261496SGeert Uytterhoeven } 2701cd261496SGeert Uytterhoeven 270220112475SJoe Perches# Check the patch for a signoff: 2703dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 27044a0df2efSAndy Whitcroft $signoff++; 270515662b3eSJoe Perches $in_commit_log = 0; 270648ca2d8aSDwaipayan Ray if ($author ne '' && $authorsignoff != 1) { 270748ca2d8aSDwaipayan Ray if (same_email_addresses($1, $author, 1)) { 2708cd261496SGeert Uytterhoeven $authorsignoff = 1; 270948ca2d8aSDwaipayan Ray } else { 271048ca2d8aSDwaipayan Ray my $ctx = $1; 271148ca2d8aSDwaipayan Ray my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); 271248ca2d8aSDwaipayan Ray my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); 271348ca2d8aSDwaipayan Ray 271448ca2d8aSDwaipayan Ray if ($email_address eq $author_address && $email_name eq $author_name) { 271548ca2d8aSDwaipayan Ray $author_sob = $ctx; 271648ca2d8aSDwaipayan Ray $authorsignoff = 2; 271748ca2d8aSDwaipayan Ray } elsif ($email_address eq $author_address) { 271848ca2d8aSDwaipayan Ray $author_sob = $ctx; 271948ca2d8aSDwaipayan Ray $authorsignoff = 3; 272048ca2d8aSDwaipayan Ray } elsif ($email_name eq $author_name) { 272148ca2d8aSDwaipayan Ray $author_sob = $ctx; 272248ca2d8aSDwaipayan Ray $authorsignoff = 4; 272348ca2d8aSDwaipayan Ray 272448ca2d8aSDwaipayan Ray my $address1 = $email_address; 272548ca2d8aSDwaipayan Ray my $address2 = $author_address; 272648ca2d8aSDwaipayan Ray 272748ca2d8aSDwaipayan Ray if ($address1 =~ /(\S+)\+\S+(\@.*)/) { 272848ca2d8aSDwaipayan Ray $address1 = "$1$2"; 272948ca2d8aSDwaipayan Ray } 273048ca2d8aSDwaipayan Ray if ($address2 =~ /(\S+)\+\S+(\@.*)/) { 273148ca2d8aSDwaipayan Ray $address2 = "$1$2"; 273248ca2d8aSDwaipayan Ray } 273348ca2d8aSDwaipayan Ray if ($address1 eq $address2) { 273448ca2d8aSDwaipayan Ray $authorsignoff = 5; 273548ca2d8aSDwaipayan Ray } 273648ca2d8aSDwaipayan Ray } 2737cd261496SGeert Uytterhoeven } 2738cd261496SGeert Uytterhoeven } 27390a920b5bSAndy Whitcroft } 274020112475SJoe Perches 274144d303ebSJoe Perches# Check for patch separator 274244d303ebSJoe Perches if ($line =~ /^---$/) { 274344d303ebSJoe Perches $has_patch_separator = 1; 274444d303ebSJoe Perches $in_commit_log = 0; 274544d303ebSJoe Perches } 274644d303ebSJoe Perches 2747e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2748e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2749e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2750e0d975b1SJoe Perches $reported_maintainer_file = 1; 2751e0d975b1SJoe Perches } 2752e0d975b1SJoe Perches 275320112475SJoe Perches# Check signature styles 2754270c49a0SJoe Perches if (!$in_header_lines && 2755ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 275620112475SJoe Perches my $space_before = $1; 275720112475SJoe Perches my $sign_off = $2; 275820112475SJoe Perches my $space_after = $3; 275920112475SJoe Perches my $email = $4; 276020112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 276120112475SJoe Perches 2762ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2763ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2764ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2765ce0338dfSJoe Perches } 276620112475SJoe Perches if (defined $space_before && $space_before ne "") { 27673705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27683705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 27693705ce5bSJoe Perches $fix) { 2770194f66fcSJoe Perches $fixed[$fixlinenr] = 27713705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27723705ce5bSJoe Perches } 277320112475SJoe Perches } 277420112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 27753705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27763705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 27773705ce5bSJoe Perches $fix) { 2778194f66fcSJoe Perches $fixed[$fixlinenr] = 27793705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27803705ce5bSJoe Perches } 27813705ce5bSJoe Perches 278220112475SJoe Perches } 278320112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 27843705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27853705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 27863705ce5bSJoe Perches $fix) { 2787194f66fcSJoe Perches $fixed[$fixlinenr] = 27883705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27893705ce5bSJoe Perches } 279020112475SJoe Perches } 279120112475SJoe Perches 2792dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 279348ca2d8aSDwaipayan Ray my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); 279420112475SJoe Perches if ($suggested_email eq "") { 2795000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2796000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 279720112475SJoe Perches } else { 279820112475SJoe Perches my $dequoted = $suggested_email; 279920112475SJoe Perches $dequoted =~ s/^"//; 280020112475SJoe Perches $dequoted =~ s/" </ </; 280120112475SJoe Perches # Don't force email to have quotes 280220112475SJoe Perches # Allow just an angle bracketed address 280348ca2d8aSDwaipayan Ray if (!same_email_addresses($email, $suggested_email, 0)) { 2804000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 280548ca2d8aSDwaipayan Ray "email address '$email' might be better as '$suggested_email'\n" . $herecurr); 280620112475SJoe Perches } 28070a920b5bSAndy Whitcroft } 28087e51f197SJoe Perches 28097e51f197SJoe Perches# Check for duplicate signatures 28107e51f197SJoe Perches my $sig_nospace = $line; 28117e51f197SJoe Perches $sig_nospace =~ s/\s//g; 28127e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 28137e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 28147e51f197SJoe Perches WARN("BAD_SIGN_OFF", 28157e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 28167e51f197SJoe Perches } else { 28177e51f197SJoe Perches $signatures{$sig_nospace} = 1; 28187e51f197SJoe Perches } 28196c5d24eeSSean Christopherson 28206c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 28216c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 28226c5d24eeSSean Christopherson if ($email eq $author) { 28236c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28246c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 28256c5d24eeSSean Christopherson } 28266c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 28276c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28286c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 28296c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 28306c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28316c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 28326c5d24eeSSean Christopherson } elsif ($1 ne $email) { 28336c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 28346c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 28356c5d24eeSSean Christopherson } 28366c5d24eeSSean Christopherson } 28370a920b5bSAndy Whitcroft } 28380a920b5bSAndy Whitcroft 2839a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2840a2fe16b9SJoe Perches if ($in_header_lines && 2841a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2842a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2843a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2844a2fe16b9SJoe Perches } 2845a2fe16b9SJoe Perches 284644d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context 284744d303ebSJoe Perches if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 28487ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 284944d303ebSJoe Perches "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr); 28507ebd05efSChristopher Covington } 28517ebd05efSChristopher Covington 2852369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2853369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2854369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2855369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2856369c8dd3SJoe Perches # timestamp 2857634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 2858634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 2859634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 2860634cffccSJoe Perches # stack dump address styles 2861369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2862369c8dd3SJoe Perches } 2863369c8dd3SJoe Perches 28642a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 28652a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2866bf4daf12SJoe Perches length($line) > 75 && 2867bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2868bf4daf12SJoe Perches # file delta changes 2869bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2870bf4daf12SJoe Perches # filename then : 2871bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2872bf4daf12SJoe Perches # A Fixes: or Link: line 2873bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 28742a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 28752a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 28762a076f40SJoe Perches $commit_log_long_line = 1; 28772a076f40SJoe Perches } 28782a076f40SJoe Perches 2879bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2880bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2881bf4daf12SJoe Perches $line =~ /^\s*$/) { 2882bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2883bf4daf12SJoe Perches } 2884bf4daf12SJoe Perches 28850d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2886369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2887a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 2888e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2889fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2890aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2891369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2892bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2893fe043ea1SJoe Perches my $init_char = "c"; 2894fe043ea1SJoe Perches my $orig_commit = ""; 28950d7835fcSJoe Perches my $short = 1; 28960d7835fcSJoe Perches my $long = 0; 28970d7835fcSJoe Perches my $case = 1; 28980d7835fcSJoe Perches my $space = 1; 28990d7835fcSJoe Perches my $hasdesc = 0; 290019c146a6SJoe Perches my $hasparens = 0; 29010d7835fcSJoe Perches my $id = '0123456789ab'; 29020d7835fcSJoe Perches my $orig_desc = "commit description"; 29030d7835fcSJoe Perches my $description = ""; 29040d7835fcSJoe Perches 2905fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2906fe043ea1SJoe Perches $init_char = $1; 2907fe043ea1SJoe Perches $orig_commit = lc($2); 2908fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2909fe043ea1SJoe Perches $orig_commit = lc($1); 2910fe043ea1SJoe Perches } 2911fe043ea1SJoe Perches 29120d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 29130d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 29140d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 29150d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 29160d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 29170d7835fcSJoe Perches $orig_desc = $1; 291819c146a6SJoe Perches $hasparens = 1; 29190d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 29200d7835fcSJoe Perches defined $rawlines[$linenr] && 29210d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 29220d7835fcSJoe Perches $orig_desc = $1; 292319c146a6SJoe Perches $hasparens = 1; 2924b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2925b671fde0SJoe Perches defined $rawlines[$linenr] && 2926b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2927b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2928b671fde0SJoe Perches $orig_desc = $1; 2929b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2930b671fde0SJoe Perches $orig_desc .= " " . $1; 293119c146a6SJoe Perches $hasparens = 1; 29320d7835fcSJoe Perches } 29330d7835fcSJoe Perches 29340d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 29350d7835fcSJoe Perches $id, $orig_desc); 29360d7835fcSJoe Perches 2937948b133aSHeinrich Schuchardt if (defined($id) && 2938948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2939d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 29400d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 29410d7835fcSJoe Perches } 2942d311cd44SJoe Perches } 2943d311cd44SJoe Perches 294413f1937eSJoe Perches# Check for added, moved or deleted files 294513f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 294613f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 294713f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 294813f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 294913f1937eSJoe Perches (defined($1) || defined($2))))) { 2950a82603a8SAndrew Jeffery $is_patch = 1; 295113f1937eSJoe Perches $reported_maintainer_file = 1; 295213f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 295313f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 295413f1937eSJoe Perches } 295513f1937eSJoe Perches 2956e400edb1SRob Herring# Check for adding new DT bindings not in schema format 2957e400edb1SRob Herring if (!$in_commit_log && 2958e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 2959e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 2960e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 2961e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 2962e400edb1SRob Herring } 2963e400edb1SRob Herring 296400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 29658905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2966000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2967000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 29686c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2969de7d4f0eSAndy Whitcroft } 2970de7d4f0eSAndy Whitcroft 2971de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2972de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2973171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2974171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2975171ae1a4SAndy Whitcroft 2976171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2977171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2978171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2979171ae1a4SAndy Whitcroft 298034d99219SJoe Perches CHK("INVALID_UTF8", 2981000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 298200df344fSAndy Whitcroft } 29830a920b5bSAndy Whitcroft 298415662b3eSJoe Perches# Check if it's the start of a commit log 298515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 298615662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2987eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2988eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 298915662b3eSJoe Perches $in_header_lines = 0; 299015662b3eSJoe Perches $in_commit_log = 1; 2991ed43c4e5SAllen Hubbe $has_commit_log = 1; 299215662b3eSJoe Perches } 299315662b3eSJoe Perches 2994fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2995fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2996fa64205dSPasi Savanainen if ($in_header_lines && 2997fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2998fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2999fa64205dSPasi Savanainen $non_utf8_charset = 1; 3000fa64205dSPasi Savanainen } 3001fa64205dSPasi Savanainen 3002fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 300315662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 3004fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 300515662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 300615662b3eSJoe Perches } 300715662b3eSJoe Perches 3008d6430f71SJoe Perches# Check for absolute kernel paths in commit message 3009d6430f71SJoe Perches if ($tree && $in_commit_log) { 3010d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 3011d6430f71SJoe Perches my $file = $1; 3012d6430f71SJoe Perches 3013d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 3014d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 3015d6430f71SJoe Perches # 3016d6430f71SJoe Perches } else { 3017d6430f71SJoe Perches check_absolute_file($file, $herecurr); 3018d6430f71SJoe Perches } 3019d6430f71SJoe Perches } 3020d6430f71SJoe Perches } 3021d6430f71SJoe Perches 302266b47b4aSKees Cook# Check for various typo / spelling mistakes 302366d7a382SJoe Perches if (defined($misspellings) && 302466d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 3025ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 302666b47b4aSKees Cook my $typo = $1; 302766b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 302866b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 302966b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 30300675a8fbSJean Delvare my $msg_level = \&WARN; 30310675a8fbSJean Delvare $msg_level = \&CHK if ($file); 30320675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 303366b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 303466b47b4aSKees Cook $fix) { 303566b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 303666b47b4aSKees Cook } 303766b47b4aSKees Cook } 303866b47b4aSKees Cook } 303966b47b4aSKees Cook 3040a8dd86bfSMatteo Croce# check for invalid commit id 3041a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 3042a8dd86bfSMatteo Croce my $id; 3043a8dd86bfSMatteo Croce my $description; 3044a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 3045a8dd86bfSMatteo Croce if (!defined($id)) { 3046a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 3047a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 3048a8dd86bfSMatteo Croce } 3049a8dd86bfSMatteo Croce } 3050a8dd86bfSMatteo Croce 3051310cd06bSJoe Perches# check for repeated words separated by a single space 3052310cd06bSJoe Perches if ($rawline =~ /^\+/ || $in_commit_log) { 3053310cd06bSJoe Perches while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { 3054310cd06bSJoe Perches 3055310cd06bSJoe Perches my $first = $1; 3056310cd06bSJoe Perches my $second = $2; 3057310cd06bSJoe Perches 3058310cd06bSJoe Perches if ($first =~ /(?:struct|union|enum)/) { 3059310cd06bSJoe Perches pos($rawline) += length($first) + length($second) + 1; 3060310cd06bSJoe Perches next; 3061310cd06bSJoe Perches } 3062310cd06bSJoe Perches 3063310cd06bSJoe Perches next if ($first ne $second); 3064310cd06bSJoe Perches next if ($first eq 'long'); 3065310cd06bSJoe Perches 3066310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3067310cd06bSJoe Perches "Possible repeated word: '$first'\n" . $herecurr) && 3068310cd06bSJoe Perches $fix) { 3069310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; 3070310cd06bSJoe Perches } 3071310cd06bSJoe Perches } 3072310cd06bSJoe Perches 3073310cd06bSJoe Perches # if it's a repeated word on consecutive lines in a comment block 3074310cd06bSJoe Perches if ($prevline =~ /$;+\s*$/ && 3075310cd06bSJoe Perches $prevrawline =~ /($word_pattern)\s*$/) { 3076310cd06bSJoe Perches my $last_word = $1; 3077310cd06bSJoe Perches if ($rawline =~ /^\+\s*\*\s*$last_word /) { 3078310cd06bSJoe Perches if (WARN("REPEATED_WORD", 3079310cd06bSJoe Perches "Possible repeated word: '$last_word'\n" . $hereprev) && 3080310cd06bSJoe Perches $fix) { 3081310cd06bSJoe Perches $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; 3082310cd06bSJoe Perches } 3083310cd06bSJoe Perches } 3084310cd06bSJoe Perches } 3085310cd06bSJoe Perches } 3086310cd06bSJoe Perches 308730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 308830670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 308900df344fSAndy Whitcroft 30900a920b5bSAndy Whitcroft#trailing whitespace 30919c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 3092c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3093d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 3094d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 3095d5e616fcSJoe Perches $fix) { 3096194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 3097d5e616fcSJoe Perches } 3098c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 3099c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 31003705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 31013705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 31023705ce5bSJoe Perches $fix) { 3103194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 31043705ce5bSJoe Perches } 31053705ce5bSJoe Perches 3106d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 31070a920b5bSAndy Whitcroft } 31085368df20SAndy Whitcroft 31094783f894SJosh Triplett# Check for FSF mailing addresses. 3110109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 31111bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 31123e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 31133e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 31144783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 31150675a8fbSJean Delvare my $msg_level = \&ERROR; 31160675a8fbSJean Delvare $msg_level = \&CHK if ($file); 31170675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 31184783f894SJosh 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) 31194783f894SJosh Triplett } 31204783f894SJosh Triplett 31213354957aSAndi Kleen# check for Kconfig help text having a real description 31229fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 31239fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 31243354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 3125678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3126678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3127678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3128678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 31293354957aSAndi Kleen my $length = 0; 31309fe287d7SAndy Whitcroft my $cnt = $realcnt; 31319fe287d7SAndy Whitcroft my $ln = $linenr + 1; 31329fe287d7SAndy Whitcroft my $f; 3133a1385803SAndy Whitcroft my $is_start = 0; 31349fe287d7SAndy Whitcroft my $is_end = 0; 3135a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 31369fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 31379fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 31389fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 31399fe287d7SAndy Whitcroft 31409fe287d7SAndy Whitcroft next if ($f =~ /^-/); 31418d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3142a1385803SAndy Whitcroft 314386adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3144a1385803SAndy Whitcroft $is_start = 1; 314522a4ac02SMasahiro Yamada } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 3146a1385803SAndy Whitcroft $length = -1; 3147a1385803SAndy Whitcroft } 3148a1385803SAndy Whitcroft 31499fe287d7SAndy Whitcroft $f =~ s/^.//; 31503354957aSAndi Kleen $f =~ s/#.*//; 31513354957aSAndi Kleen $f =~ s/^\s+//; 31523354957aSAndi Kleen next if ($f =~ /^$/); 3153678ae162SUlf Magnusson 3154678ae162SUlf Magnusson # This only checks context lines in the patch 3155678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3156678ae162SUlf Magnusson # positives, even though some of these are 3157678ae162SUlf Magnusson # common words in help texts 3158678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3159678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 31609fe287d7SAndy Whitcroft $is_end = 1; 31619fe287d7SAndy Whitcroft last; 31629fe287d7SAndy Whitcroft } 31633354957aSAndi Kleen $length++; 31643354957aSAndi Kleen } 316556193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3166000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 316756193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 316856193274SVadim Bendebury } 3169a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 31703354957aSAndi Kleen } 31713354957aSAndi Kleen 31727ccf41a8SJoe Perches# check MAINTAINERS entries 31737ccf41a8SJoe Perches if ($realfile =~ /^MAINTAINERS$/) { 31747ccf41a8SJoe Perches# check MAINTAINERS entries for the right form 31757ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 3176628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3177628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3178628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3179628f91a2SJoe Perches $fix) { 3180628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3181628f91a2SJoe Perches } 3182628f91a2SJoe Perches } 31837ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too 31847ccf41a8SJoe Perches my $preferred_order = 'MRLSWQBCPTFXNK'; 31857ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 31867ccf41a8SJoe Perches $prevrawline =~ /^[\+ ][A-Z]:/) { 31877ccf41a8SJoe Perches $rawline =~ /^\+([A-Z]):\s*(.*)/; 31887ccf41a8SJoe Perches my $cur = $1; 31897ccf41a8SJoe Perches my $curval = $2; 31907ccf41a8SJoe Perches $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; 31917ccf41a8SJoe Perches my $prev = $1; 31927ccf41a8SJoe Perches my $prevval = $2; 31937ccf41a8SJoe Perches my $curindex = index($preferred_order, $cur); 31947ccf41a8SJoe Perches my $previndex = index($preferred_order, $prev); 31957ccf41a8SJoe Perches if ($curindex < 0) { 31967ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 31977ccf41a8SJoe Perches "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); 31987ccf41a8SJoe Perches } else { 31997ccf41a8SJoe Perches if ($previndex >= 0 && $curindex < $previndex) { 32007ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 32017ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); 32027ccf41a8SJoe Perches } elsif ((($prev eq 'F' && $cur eq 'F') || 32037ccf41a8SJoe Perches ($prev eq 'X' && $cur eq 'X')) && 32047ccf41a8SJoe Perches ($prevval cmp $curval) > 0) { 32057ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 32067ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); 32077ccf41a8SJoe Perches } 32087ccf41a8SJoe Perches } 32097ccf41a8SJoe Perches } 32107ccf41a8SJoe Perches } 3211628f91a2SJoe Perches 3212327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3213327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3214327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3215327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3216327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3217327953e9SChristoph Jaeger } 3218327953e9SChristoph Jaeger 3219c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3220c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3221c68e5878SArnaud Lacombe my $flag = $1; 3222c68e5878SArnaud Lacombe my $replacement = { 3223c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3224c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3225c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3226c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3227c68e5878SArnaud Lacombe }; 3228c68e5878SArnaud Lacombe 3229c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3230c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3231c68e5878SArnaud Lacombe } 3232c68e5878SArnaud Lacombe 3233bff5da43SRob Herring# check for DT compatible documentation 32347dd05b38SFlorian Vaussard if (defined $root && 32357dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 32367dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 32377dd05b38SFlorian Vaussard 3238bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3239bff5da43SRob Herring 3240cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3241852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3242cc93319bSFlorian Vaussard 3243bff5da43SRob Herring foreach my $compat (@compats) { 3244bff5da43SRob Herring my $compat2 = $compat; 3245185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3246185d566bSRob Herring my $compat3 = $compat; 3247185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3248185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3249bff5da43SRob Herring if ( $? >> 8 ) { 3250bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3251bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3252bff5da43SRob Herring } 3253bff5da43SRob Herring 32544fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 32554fbf32a6SFlorian Vaussard my $vendor = $1; 3256852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3257bff5da43SRob Herring if ( $? >> 8 ) { 3258bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3259cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3260bff5da43SRob Herring } 3261bff5da43SRob Herring } 3262bff5da43SRob Herring } 3263bff5da43SRob Herring 32649f3a8992SRob Herring# check for using SPDX license tag at beginning of files 32659f3a8992SRob Herring if ($realline == $checklicenseline) { 32669f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 32679f3a8992SRob Herring $checklicenseline = 2; 32689f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 32699f3a8992SRob Herring my $comment = ""; 32709f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 32719f3a8992SRob Herring $comment = '/*'; 32729f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 32739f3a8992SRob Herring $comment = '//'; 3274c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 32759f3a8992SRob Herring $comment = '#'; 32769f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 32779f3a8992SRob Herring $comment = '..'; 32789f3a8992SRob Herring } 32799f3a8992SRob Herring 3280fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3281fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3282fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3283ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3284fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3285fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3286fdf13693SJoe Perches } 3287fdf13693SJoe Perches 32889f3a8992SRob Herring if ($comment !~ /^$/ && 3289ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 32909f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 32919f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 32923b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 32933b6e8ac9SJoe Perches my $spdx_license = $1; 32943b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 32953b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 32963b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 32973b6e8ac9SJoe Perches } 329850c92900SLubomir Rintel if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 329950c92900SLubomir Rintel not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 330050c92900SLubomir Rintel my $msg_level = \&WARN; 330150c92900SLubomir Rintel $msg_level = \&CHK if ($file); 330250c92900SLubomir Rintel if (&{$msg_level}("SPDX_LICENSE_TAG", 330350c92900SLubomir Rintel 330450c92900SLubomir Rintel "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 330550c92900SLubomir Rintel $fix) { 330650c92900SLubomir Rintel $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 330750c92900SLubomir Rintel } 330850c92900SLubomir Rintel } 33099f3a8992SRob Herring } 33109f3a8992SRob Herring } 33119f3a8992SRob Herring } 33129f3a8992SRob Herring 3313a0154cdbSJoe Perches# check for embedded filenames 3314a0154cdbSJoe Perches if ($rawline =~ /^\+.*\Q$realfile\E/) { 3315a0154cdbSJoe Perches WARN("EMBEDDED_FILENAME", 3316a0154cdbSJoe Perches "It's generally not useful to have the filename in the file\n" . $herecurr); 3317a0154cdbSJoe Perches } 3318a0154cdbSJoe Perches 33195368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3320d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 33215368df20SAndy Whitcroft 3322a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3323a8da38a9SJoe Perches if ($realline != $checklicenseline && 3324a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3325a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3326a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3327a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3328a8da38a9SJoe Perches } 3329a8da38a9SJoe Perches 333047e0c88bSJoe Perches# line length limit (with some exclusions) 333147e0c88bSJoe Perches# 333247e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 333347e0c88bSJoe Perches# logging functions like pr_info that end in a string 333447e0c88bSJoe Perches# lines with a single string 333547e0c88bSJoe Perches# #defines that are a single string 33362e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 333747e0c88bSJoe Perches# 333847e0c88bSJoe Perches# There are 3 different line length message types: 3339ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 334047e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 334147e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 334247e0c88bSJoe Perches# 334347e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 334447e0c88bSJoe Perches# 334547e0c88bSJoe Perches 3346b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 334747e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 334847e0c88bSJoe Perches 334947e0c88bSJoe Perches # Check the allowed long line types first 335047e0c88bSJoe Perches 335147e0c88bSJoe Perches # logging functions that end in a string that starts 335247e0c88bSJoe Perches # before $max_line_length 335347e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 335447e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 335547e0c88bSJoe Perches $msg_type = ""; 335647e0c88bSJoe Perches 335747e0c88bSJoe Perches # lines with only strings (w/ possible termination) 335847e0c88bSJoe Perches # #defines with only strings 335947e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 336047e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 336147e0c88bSJoe Perches $msg_type = ""; 336247e0c88bSJoe Perches 3363cc147506SJoe Perches # More special cases 3364cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3365cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3366d560a5f8SJoe Perches $msg_type = ""; 3367d560a5f8SJoe Perches 33682e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 33692e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 33702e4bbbc5SAndreas Brauchli $msg_type = ""; 33712e4bbbc5SAndreas Brauchli 337247e0c88bSJoe Perches # Otherwise set the alternate message types 337347e0c88bSJoe Perches 337447e0c88bSJoe Perches # a comment starts before $max_line_length 337547e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 337647e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 337747e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 337847e0c88bSJoe Perches 337947e0c88bSJoe Perches # a quoted string starts before $max_line_length 338047e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 338147e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 338247e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 338347e0c88bSJoe Perches } 338447e0c88bSJoe Perches 338547e0c88bSJoe Perches if ($msg_type ne "" && 338647e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 3387bdc48fa1SJoe Perches my $msg_level = \&WARN; 3388bdc48fa1SJoe Perches $msg_level = \&CHK if ($file); 3389bdc48fa1SJoe Perches &{$msg_level}($msg_type, 3390bdc48fa1SJoe Perches "line length of $length exceeds $max_line_length columns\n" . $herecurr); 33910a920b5bSAndy Whitcroft } 339247e0c88bSJoe Perches } 33930a920b5bSAndy Whitcroft 33948905a67cSAndy Whitcroft# check for adding lines without a newline. 33958905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3396000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3397000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 33988905a67cSAndy Whitcroft } 33998905a67cSAndy Whitcroft 3400b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3401de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 34020a920b5bSAndy Whitcroft 34030a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3404713a09deSAntonio Borneo# more than $tabsize must use tabs. 3405c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3406c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3407c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3408d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 34093705ce5bSJoe Perches if (ERROR("CODE_INDENT", 34103705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 34113705ce5bSJoe Perches $fix) { 3412194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 34133705ce5bSJoe Perches } 34140a920b5bSAndy Whitcroft } 34150a920b5bSAndy Whitcroft 341608e44365SAlberto Panizzo# check for space before tabs. 341708e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 341808e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 34193705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 34203705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 34213705ce5bSJoe Perches $fix) { 3422194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3423713a09deSAntonio Borneo s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3424194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3425c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 34263705ce5bSJoe Perches } 342708e44365SAlberto Panizzo } 342808e44365SAlberto Panizzo 34296a487211SJoe Perches# check for assignments on the start of a line 34306a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 34316a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 34326a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 34336a487211SJoe Perches } 34346a487211SJoe Perches 3435d1fe9c09SJoe Perches# check for && or || at the start of a line 3436d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3437d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3438d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3439d1fe9c09SJoe Perches } 3440d1fe9c09SJoe Perches 3441a91e8994SJoe Perches# check indentation starts on a tab stop 34425b57980dSJoe Perches if ($perl_version_ok && 3443bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3444a91e8994SJoe Perches my $indent = length($1); 3445713a09deSAntonio Borneo if ($indent % $tabsize) { 3446a91e8994SJoe Perches if (WARN("TABSTOP", 3447a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3448a91e8994SJoe Perches $fix) { 3449713a09deSAntonio Borneo $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3450a91e8994SJoe Perches } 3451a91e8994SJoe Perches } 3452a91e8994SJoe Perches } 3453a91e8994SJoe Perches 3454d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 34555b57980dSJoe Perches if ($perl_version_ok && 3456fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3457d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3458d1fe9c09SJoe Perches my $oldindent = $1; 3459d1fe9c09SJoe Perches my $rest = $2; 3460d1fe9c09SJoe Perches 3461d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3462d1fe9c09SJoe Perches if ($pos >= 0) { 3463b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3464b34a26f3SJoe Perches my $newindent = $2; 3465d1fe9c09SJoe Perches 3466d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3467713a09deSAntonio Borneo "\t" x ($pos / $tabsize) . 3468713a09deSAntonio Borneo " " x ($pos % $tabsize); 3469d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3470d1fe9c09SJoe Perches 3471d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3472d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 34733705ce5bSJoe Perches 34743705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 34753705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 34763705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3477194f66fcSJoe Perches $fixed[$fixlinenr] =~ 34783705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 34793705ce5bSJoe Perches } 3480d1fe9c09SJoe Perches } 3481d1fe9c09SJoe Perches } 3482d1fe9c09SJoe Perches } 3483d1fe9c09SJoe Perches 34846ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 34856ab3a970SJoe Perches# avoid checking a few false positives: 34866ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 34876ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 34886ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 34896ab3a970SJoe Perches# multiline macros that define functions 34906ab3a970SJoe Perches# known attributes or the __attribute__ keyword 34916ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 34926ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 34933705ce5bSJoe Perches if (CHK("SPACING", 3494f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 34953705ce5bSJoe Perches $fix) { 3496194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3497f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 34983705ce5bSJoe Perches } 3499aad4f614SJoe Perches } 3500aad4f614SJoe Perches 350186406b1cSJoe Perches# Block comment styles 350286406b1cSJoe Perches# Networking with an initial /* 350305880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3504fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 350585ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 3506c70735c2SŁukasz Stelmach $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier 350705880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 350805880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 350905880600SJoe Perches } 351005880600SJoe Perches 351186406b1cSJoe Perches# Block comments use * on subsequent lines 351286406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 351386406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3514a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 351561135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3516a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 351786406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 351886406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3519a605e32eSJoe Perches } 3520a605e32eSJoe Perches 352186406b1cSJoe Perches# Block comments use */ on trailing lines 352286406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3523c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3524c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3525c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 352686406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 352786406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 352805880600SJoe Perches } 352905880600SJoe Perches 353008eb9b80SJoe Perches# Block comment * alignment 353108eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3532af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3533af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3534af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 353508eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3536af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3537af207524SJoe Perches my $oldindent; 353808eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3539af207524SJoe Perches if (defined($1)) { 3540af207524SJoe Perches $oldindent = expand_tabs($1); 3541af207524SJoe Perches } else { 3542af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3543af207524SJoe Perches $oldindent = expand_tabs($1); 3544af207524SJoe Perches } 354508eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 354608eb9b80SJoe Perches my $newindent = $1; 354708eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3548af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 354908eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 355008eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 355108eb9b80SJoe Perches } 355208eb9b80SJoe Perches } 355308eb9b80SJoe Perches 35547f619191SJoe Perches# check for missing blank lines after struct/union declarations 35557f619191SJoe Perches# with exceptions for various attributes and macros 35567f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 35577f619191SJoe Perches $line =~ /^\+/ && 35587f619191SJoe Perches !($line =~ /^\+\s*$/ || 35597f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 35607f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 35617f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 35627f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 35637f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 35647f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 35650bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 35667f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3567d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3568d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3569d752fcc8SJoe Perches $fix) { 3570f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3571d752fcc8SJoe Perches } 35727f619191SJoe Perches } 35737f619191SJoe Perches 3574365dd4eaSJoe Perches# check for multiple consecutive blank lines 3575365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3576365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3577365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3578d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3579d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3580d752fcc8SJoe Perches $fix) { 3581f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3582d752fcc8SJoe Perches } 3583d752fcc8SJoe Perches 3584365dd4eaSJoe Perches $last_blank_line = $linenr; 3585365dd4eaSJoe Perches } 3586365dd4eaSJoe Perches 35873b617e3bSJoe Perches# check for missing blank lines after declarations 35883f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 35893f7bac03SJoe Perches # actual declarations 35903f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 35915a4e1fd3SJoe Perches # function pointer declarations 35925a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 35933f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 35943f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 35953f7bac03SJoe Perches # known declaration macros 35963f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 35973f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 35983f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 35993f7bac03SJoe Perches # other possible extensions of declaration lines 36003f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 36013f7bac03SJoe Perches # not starting a section or a macro "\" extended line 36023f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 36033f7bac03SJoe Perches # looks like a declaration 36043f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 36055a4e1fd3SJoe Perches # function pointer declarations 36065a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 36073f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 36083f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 36093f7bac03SJoe Perches # known declaration macros 36103f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 36113f7bac03SJoe Perches # start of struct or union or enum 3612328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 36133f7bac03SJoe Perches # start or end of block or continuation of declaration 36143f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 36153f7bac03SJoe Perches # bitfield continuation 36163f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 36173f7bac03SJoe Perches # other possible extensions of declaration lines 36183f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 36193f7bac03SJoe Perches # indentation of previous and current line are the same 36203f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3621d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3622d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3623d752fcc8SJoe Perches $fix) { 3624f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3625d752fcc8SJoe Perches } 36263b617e3bSJoe Perches } 36273b617e3bSJoe Perches 36285f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 36296b4c5bebSAndy Whitcroft# Exceptions: 36306b4c5bebSAndy Whitcroft# 1) within comments 36316b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 36326b4c5bebSAndy Whitcroft# 3) hanging labels 36333705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 36345f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 36353705ce5bSJoe Perches if (WARN("LEADING_SPACE", 36363705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 36373705ce5bSJoe Perches $fix) { 3638194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 36393705ce5bSJoe Perches } 36405f7ddae6SRaffaele Recalcati } 36415f7ddae6SRaffaele Recalcati 3642b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3643b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3644b9ea10d6SAndy Whitcroft 36455751a24eSJoe Perches# check for unusual line ending [ or ( 36465751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 36475751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 36485751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 36495751a24eSJoe Perches } 36505751a24eSJoe Perches 36514dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 36524dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 36534dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 36544dbed76fSJoe Perches $context_function = $1; 36554dbed76fSJoe Perches } 36564dbed76fSJoe Perches 36574dbed76fSJoe Perches# check if this appears to be the end of function declaration 36584dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 36594dbed76fSJoe Perches undef $context_function; 36604dbed76fSJoe Perches } 36614dbed76fSJoe Perches 3662032a4c0fSJoe Perches# check indentation of any line with a bare else 3663840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3664032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3665032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3666032a4c0fSJoe Perches my $tabs = length($1) + 1; 3667840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3668840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3669840080a0SJoe Perches defined $lines[$linenr] && 3670840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3671032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3672032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3673032a4c0fSJoe Perches } 3674032a4c0fSJoe Perches } 3675032a4c0fSJoe Perches 3676c00df19aSJoe Perches# check indentation of a line with a break; 3677c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3678c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3679c00df19aSJoe Perches my $tabs = $1; 3680c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3681c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3682c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3683c00df19aSJoe Perches } 3684c00df19aSJoe Perches } 3685c00df19aSJoe Perches 3686c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3687cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3688000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3689000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3690c2fdda0dSAndy Whitcroft } 369122f2a2efSAndy Whitcroft 369256e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 369356e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 369456e77d70SJoe Perches WARN("HOTPLUG_SECTION", 369556e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 369656e77d70SJoe Perches } 369756e77d70SJoe Perches 36989c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 36992b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 37002b474a1aSAndy Whitcroft $realline_next); 37013e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3702ca819864SJoe Perches if ($linenr > $suppress_statement && 37031b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3704170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3705f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3706171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3707171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3708171ae1a4SAndy Whitcroft 37093e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 37103e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 37113e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 37123e469cdcSAndy Whitcroft # until we hit end of it. 37133e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 37143e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 37153e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 37163e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 37173e469cdcSAndy Whitcroft } 3718f74bd194SAndy Whitcroft 37192b474a1aSAndy Whitcroft # Find the real next line. 37202b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 37212b474a1aSAndy Whitcroft if (defined $realline_next && 37222b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 37232b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 37242b474a1aSAndy Whitcroft $realline_next++; 37252b474a1aSAndy Whitcroft } 37262b474a1aSAndy Whitcroft 3727171ae1a4SAndy Whitcroft my $s = $stat; 3728171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3729cf655043SAndy Whitcroft 3730c2fdda0dSAndy Whitcroft # Ignore goto labels. 3731171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3732c2fdda0dSAndy Whitcroft 3733c2fdda0dSAndy Whitcroft # Ignore functions being called 3734171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3735c2fdda0dSAndy Whitcroft 3736463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3737463f2864SAndy Whitcroft 3738c45dcabdSAndy Whitcroft # declarations always start with types 3739d2506586SAndy 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) { 3740c45dcabdSAndy Whitcroft my $type = $1; 3741c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3742c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3743c45dcabdSAndy Whitcroft 37446c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3745a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3746c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3747c2fdda0dSAndy Whitcroft } 37488905a67cSAndy Whitcroft 37496c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 375065863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3751c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 37529c0ca6f9SAndy Whitcroft } 37538905a67cSAndy Whitcroft 37548905a67cSAndy Whitcroft # Check for any sort of function declaration. 37558905a67cSAndy Whitcroft # int foo(something bar, other baz); 37568905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3757171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 37588905a67cSAndy Whitcroft my ($name_len) = length($1); 37598905a67cSAndy Whitcroft 3760cf655043SAndy Whitcroft my $ctx = $s; 3761773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 37628905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3763cf655043SAndy Whitcroft 37648905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3765c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 37668905a67cSAndy Whitcroft 3767c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 37688905a67cSAndy Whitcroft } 37698905a67cSAndy Whitcroft } 37708905a67cSAndy Whitcroft } 37718905a67cSAndy Whitcroft 37729c0ca6f9SAndy Whitcroft } 37739c0ca6f9SAndy Whitcroft 377400df344fSAndy Whitcroft# 377500df344fSAndy Whitcroft# Checks which may be anchored in the context. 377600df344fSAndy Whitcroft# 377700df344fSAndy Whitcroft 377800df344fSAndy Whitcroft# Check for switch () and associated case and default 377900df344fSAndy Whitcroft# statements should be at the same indent. 378000df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 378100df344fSAndy Whitcroft my $err = ''; 378200df344fSAndy Whitcroft my $sep = ''; 378300df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 378400df344fSAndy Whitcroft shift(@ctx); 378500df344fSAndy Whitcroft for my $ctx (@ctx) { 378600df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 378700df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 378800df344fSAndy Whitcroft $indent != $cindent) { 378900df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 379000df344fSAndy Whitcroft $sep = ''; 379100df344fSAndy Whitcroft } else { 379200df344fSAndy Whitcroft $sep = "[...]\n"; 379300df344fSAndy Whitcroft } 379400df344fSAndy Whitcroft } 379500df344fSAndy Whitcroft if ($err ne '') { 3796000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3797000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3798de7d4f0eSAndy Whitcroft } 3799de7d4f0eSAndy Whitcroft } 3800de7d4f0eSAndy Whitcroft 3801de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3802de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 38030fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3804773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3805773647a0SAndy Whitcroft 38069c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 38078eef05ddSJoe Perches 38088eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 38098eef05ddSJoe Perches WARN("DEEP_INDENTATION", 38108eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 38118eef05ddSJoe Perches } 38128eef05ddSJoe Perches 3813de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3814de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3815de7d4f0eSAndy Whitcroft 3816548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3817548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3818de7d4f0eSAndy Whitcroft 3819548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3820548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3821548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3822548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3823548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3824773647a0SAndy Whitcroft $ctx_ln++; 3825773647a0SAndy Whitcroft } 3826548596d5SAndy Whitcroft 382753210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 382853210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3829773647a0SAndy Whitcroft 3830773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3831000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3832000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 383301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 383400df344fSAndy Whitcroft } 3835773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3836773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3837773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3838773647a0SAndy Whitcroft { 38399c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 38409c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3841000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3842000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 384301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 38449c0ca6f9SAndy Whitcroft } 38459c0ca6f9SAndy Whitcroft } 384600df344fSAndy Whitcroft } 384700df344fSAndy Whitcroft 38484d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3849f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 38503e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 38513e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 38523e469cdcSAndy Whitcroft if (!defined $stat); 38534d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 38544d001e4dSAndy Whitcroft 38554d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 38564d001e4dSAndy Whitcroft 38579f5af480SJoe Perches # remove inline comments 38589f5af480SJoe Perches $s =~ s/$;/ /g; 38599f5af480SJoe Perches $c =~ s/$;/ /g; 38604d001e4dSAndy Whitcroft 38614d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 38626f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 38636f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 38644d001e4dSAndy Whitcroft 38659f5af480SJoe Perches # Make sure we remove the line prefixes as we have 38669f5af480SJoe Perches # none on the first line, and are going to readd them 38679f5af480SJoe Perches # where necessary. 38689f5af480SJoe Perches $s =~ s/\n./\n/gs; 38699f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 38709f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 38719f5af480SJoe Perches } 38729f5af480SJoe Perches 38734d001e4dSAndy Whitcroft # We want to check the first line inside the block 38744d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 38754d001e4dSAndy Whitcroft # 1) any blank line termination 38764d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 38774d001e4dSAndy Whitcroft # 3) any do (...) { 38784d001e4dSAndy Whitcroft my $continuation = 0; 38794d001e4dSAndy Whitcroft my $check = 0; 38804d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 38814d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 38824d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 38834d001e4dSAndy Whitcroft $continuation = 1; 38844d001e4dSAndy Whitcroft } 38859bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 38864d001e4dSAndy Whitcroft $check = 1; 38874d001e4dSAndy Whitcroft $cond_lines++; 38884d001e4dSAndy Whitcroft } 38894d001e4dSAndy Whitcroft 38904d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 38914d001e4dSAndy Whitcroft # preprocessor statement. 38924d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 38934d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 38944d001e4dSAndy Whitcroft $check = 0; 38954d001e4dSAndy Whitcroft } 38964d001e4dSAndy Whitcroft 38979bd49efeSAndy Whitcroft my $cond_ptr = -1; 3898740504c6SAndy Whitcroft $continuation = 0; 38999bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 39009bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 39014d001e4dSAndy Whitcroft 3902f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3903f16fa28fSAndy Whitcroft # is not linear. 3904f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3905f16fa28fSAndy Whitcroft $check = 0; 3906f16fa28fSAndy Whitcroft } 3907f16fa28fSAndy Whitcroft 39089bd49efeSAndy Whitcroft # Ignore: 39099bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 39109bd49efeSAndy Whitcroft # 2) preprocessor lines, and 39119bd49efeSAndy Whitcroft # 3) labels. 3912740504c6SAndy Whitcroft if ($continuation || 3913740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 39149bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 39159bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3916740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 391730dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 39189bd49efeSAndy Whitcroft $cond_lines++; 39199bd49efeSAndy Whitcroft } 39204d001e4dSAndy Whitcroft } 392130dad6ebSAndy Whitcroft } 39224d001e4dSAndy Whitcroft 39234d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 39244d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 39254d001e4dSAndy Whitcroft 39264d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 39274d001e4dSAndy Whitcroft # this is not this patch's fault. 39284d001e4dSAndy Whitcroft if (!defined($stat_real) || 39294d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 39304d001e4dSAndy Whitcroft $check = 0; 39314d001e4dSAndy Whitcroft } 39324d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 39334d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 39344d001e4dSAndy Whitcroft } 39354d001e4dSAndy Whitcroft 39369bd49efeSAndy 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"; 39374d001e4dSAndy Whitcroft 39389f5af480SJoe Perches if ($check && $s ne '' && 3939713a09deSAntonio Borneo (($sindent % $tabsize) != 0 || 39409f5af480SJoe Perches ($sindent < $indent) || 3941f6950a73SJoe Perches ($sindent == $indent && 3942f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 3943713a09deSAntonio Borneo ($sindent > $indent + $tabsize))) { 3944000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3945000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 39464d001e4dSAndy Whitcroft } 39474d001e4dSAndy Whitcroft } 39484d001e4dSAndy Whitcroft 39496c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 39506c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 39511f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 39521f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 39536c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3954c2fdda0dSAndy Whitcroft if ($dbg_values) { 3955c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3956cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3957cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 39581f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3959c2fdda0dSAndy Whitcroft } 39606c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 39616c72ffaaSAndy Whitcroft 396200df344fSAndy Whitcroft#ignore lines not being added 39633705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 396400df344fSAndy Whitcroft 396599ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings 396699ca38c2SJoe Perches# e.g.: int foo = foo, *bar = NULL; 396799ca38c2SJoe Perches# struct foo bar = *(&(bar)); 396899ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { 396999ca38c2SJoe Perches my $var = $1; 397099ca38c2SJoe Perches if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { 397199ca38c2SJoe Perches WARN("SELF_ASSIGNMENT", 397299ca38c2SJoe Perches "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); 397399ca38c2SJoe Perches } 397499ca38c2SJoe Perches } 397599ca38c2SJoe Perches 397611ca40a0SJoe Perches# check for dereferences that span multiple lines 397711ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 397811ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 397911ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 398011ca40a0SJoe Perches my $ref = $1; 398111ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 398211ca40a0SJoe Perches $ref .= $1; 398311ca40a0SJoe Perches $ref =~ s/\s//g; 398411ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 398511ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 398611ca40a0SJoe Perches } 398711ca40a0SJoe Perches 3988a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3989c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3990a1ce18e4SJoe Perches my $type = $1; 3991a1ce18e4SJoe Perches my $var = $2; 3992207a8e84SJoe Perches $var = "" if (!defined $var); 3993207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3994a1ce18e4SJoe Perches my $sign = $1; 3995a1ce18e4SJoe Perches my $pointer = $2; 3996a1ce18e4SJoe Perches 3997a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3998a1ce18e4SJoe Perches 3999a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 4000a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 4001a1ce18e4SJoe Perches $fix) { 4002a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 4003207a8e84SJoe Perches my $comp_pointer = $pointer; 4004207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 4005207a8e84SJoe Perches $decl .= $comp_pointer; 4006207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 4007207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 4008a1ce18e4SJoe Perches } 4009a1ce18e4SJoe Perches } 4010a1ce18e4SJoe Perches } 4011a1ce18e4SJoe Perches 4012653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 40137429c690SAndy Whitcroft if ($dbg_type) { 40147429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 4015000d1cc1SJoe Perches ERROR("TEST_TYPE", 4016000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 40177429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 4018000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 4019000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 40207429c690SAndy Whitcroft } 4021653d4876SAndy Whitcroft next; 4022653d4876SAndy Whitcroft } 4023a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 4024a1ef277eSAndy Whitcroft if ($dbg_attr) { 40259360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 4026000d1cc1SJoe Perches ERROR("TEST_ATTR", 4027000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 40289360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 4029000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 4030000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 4031a1ef277eSAndy Whitcroft } 4032a1ef277eSAndy Whitcroft next; 4033a1ef277eSAndy Whitcroft } 4034653d4876SAndy Whitcroft 4035f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 403699423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 403799423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 4038d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 4039d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 4040f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 4041f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4042f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 4043d752fcc8SJoe Perches my $fixedline = $prevrawline; 4044d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 4045f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4046d752fcc8SJoe Perches $fixedline = $line; 40478d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 4048f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 4049d752fcc8SJoe Perches } 4050f0a594c1SAndy Whitcroft } 4051f0a594c1SAndy Whitcroft 405200df344fSAndy Whitcroft# 405300df344fSAndy Whitcroft# Checks which are anchored on the added line. 405400df344fSAndy Whitcroft# 405500df344fSAndy Whitcroft 4056653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 4057c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 4058653d4876SAndy Whitcroft my $path = $1; 4059653d4876SAndy Whitcroft if ($path =~ m{//}) { 4060000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 4061495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 4062495e9d84SJoe Perches } 4063495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 4064495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 4065495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 4066653d4876SAndy Whitcroft } 4067653d4876SAndy Whitcroft } 4068653d4876SAndy Whitcroft 406900df344fSAndy Whitcroft# no C99 // comments 407000df344fSAndy Whitcroft if ($line =~ m{//}) { 40713705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 40723705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 40733705ce5bSJoe Perches $fix) { 4074194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 40753705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 40763705ce5bSJoe Perches my $comment = trim($1); 4077194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 40783705ce5bSJoe Perches } 40793705ce5bSJoe Perches } 408000df344fSAndy Whitcroft } 408100df344fSAndy Whitcroft # Remove C99 comments. 40820a920b5bSAndy Whitcroft $line =~ s@//.*@@; 40836c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 40840a920b5bSAndy Whitcroft 40852b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 40862b474a1aSAndy Whitcroft# the whole statement. 40872b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 40882b474a1aSAndy Whitcroft if (defined $realline_next && 40892b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 40902b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 40912b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 40922b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 40933cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 40943cbf62dfSAndy Whitcroft # a prefix: 40953cbf62dfSAndy Whitcroft # XXX(foo); 40963cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 4097653d4876SAndy Whitcroft my $name = $1; 409887a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 40993cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 41003cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 41013cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 41023cbf62dfSAndy Whitcroft 41033cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 41042b474a1aSAndy Whitcroft \n.}\s*$| 410548012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 410648012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 410748012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 41082b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 41092b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 411048012058SAndy Whitcroft )/x) { 41112b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 41122b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 41132b474a1aSAndy Whitcroft } else { 41142b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 41150a920b5bSAndy Whitcroft } 41160a920b5bSAndy Whitcroft } 41172b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 41182b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 41192b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 41202b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 41212b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 41222b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 41232b474a1aSAndy Whitcroft } 41242b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 41252b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 4126000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 4127000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 41282b474a1aSAndy Whitcroft } 41290a920b5bSAndy Whitcroft 41305150bda4SJoe Eloff# check for global initialisers. 41316d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 4132d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 41336d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 4134d5e616fcSJoe Perches $fix) { 41356d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 4136d5e616fcSJoe Perches } 4137f0a594c1SAndy Whitcroft } 41380a920b5bSAndy Whitcroft# check for static initialisers. 41396d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 4140d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 41416d32f7a3SJoe Perches "do not initialise statics to $1\n" . 4142d5e616fcSJoe Perches $herecurr) && 4143d5e616fcSJoe Perches $fix) { 41446d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 4145d5e616fcSJoe Perches } 41460a920b5bSAndy Whitcroft } 41470a920b5bSAndy Whitcroft 41481813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 41491813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 41501813087dSJoe Perches my $tmp = trim($1); 41511813087dSJoe Perches WARN("MISORDERED_TYPE", 41521813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 41531813087dSJoe Perches } 41541813087dSJoe Perches 4155809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 4156809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 4157809e082eSJoe Perches my $type = trim($1); 4158809e082eSJoe Perches next if ($type !~ /\bint\b/); 4159809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 4160809e082eSJoe Perches my $new_type = $type; 4161809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 4162809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 4163809e082eSJoe Perches $new_type =~ s/^const\s+//; 4164809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 4165809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 4166809e082eSJoe Perches $new_type =~ s/\s+/ /g; 4167809e082eSJoe Perches $new_type = trim($new_type); 4168809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 4169809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 4170809e082eSJoe Perches $fix) { 4171809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 4172809e082eSJoe Perches } 4173809e082eSJoe Perches } 4174809e082eSJoe Perches 4175cb710ecaSJoe Perches# check for static const char * arrays. 4176cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 4177000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4178000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 4179cb710ecaSJoe Perches $herecurr); 4180cb710ecaSJoe Perches } 4181cb710ecaSJoe Perches 418277b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 418377b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 418477b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 418577b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 418677b8c0a8SJoe Perches $fix) { 418777b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 418877b8c0a8SJoe Perches } 418977b8c0a8SJoe Perches } 419077b8c0a8SJoe Perches 4191cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 4192cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4193000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4194000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4195cb710ecaSJoe Perches $herecurr); 4196cb710ecaSJoe Perches } 4197cb710ecaSJoe Perches 4198ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4199ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4200ab7e23f3SJoe Perches my $found = $1; 4201ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4202ab7e23f3SJoe Perches WARN("CONST_CONST", 4203ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4204ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4205ab7e23f3SJoe Perches WARN("CONST_CONST", 4206ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4207ab7e23f3SJoe Perches } 4208ab7e23f3SJoe Perches } 4209ab7e23f3SJoe Perches 42109b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 42119b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 42129b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 42139b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 42149b0fa60dSJoe Perches $herecurr); 42159b0fa60dSJoe Perches } 42169b0fa60dSJoe Perches 4217b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4218b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4219b598b670SJoe Perches my $array = $1; 4220b598b670SJoe 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*\))@) { 4221b598b670SJoe Perches my $array_div = $1; 4222b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4223b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4224b598b670SJoe Perches $fix) { 4225b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4226b598b670SJoe Perches } 4227b598b670SJoe Perches } 4228b598b670SJoe Perches } 4229b598b670SJoe Perches 4230b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 423116b7f3c8SJoe Perches if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { 4232b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4233b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4234b36190c5SJoe Perches $fix) { 4235194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4236b36190c5SJoe Perches } 4237b36190c5SJoe Perches } 4238b36190c5SJoe Perches 4239653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4240653d4876SAndy Whitcroft# make sense. 4241653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 42428054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4243c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 42448ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 424546d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4246000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4247000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 42480a920b5bSAndy Whitcroft } 42490a920b5bSAndy Whitcroft 42500a920b5bSAndy Whitcroft# * goes on variable not on type 425165863862SAndy Whitcroft # (char*[ const]) 4252bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4253bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 42543705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4255d8aaf121SAndy Whitcroft 425665863862SAndy Whitcroft # Should start with a space. 425765863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 425865863862SAndy Whitcroft # Should not end with a space. 425965863862SAndy Whitcroft $to =~ s/\s+$//; 426065863862SAndy Whitcroft # '*'s should not have spaces between. 4261f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 426265863862SAndy Whitcroft } 4263d8aaf121SAndy Whitcroft 42643705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 426565863862SAndy Whitcroft if ($from ne $to) { 42663705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 42673705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 42683705ce5bSJoe Perches $fix) { 42693705ce5bSJoe Perches my $sub_from = $ident; 42703705ce5bSJoe Perches my $sub_to = $ident; 42713705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4272194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42733705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 42743705ce5bSJoe Perches } 427565863862SAndy Whitcroft } 4276bfcb2cc7SAndy Whitcroft } 4277bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4278bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 42793705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4280d8aaf121SAndy Whitcroft 428165863862SAndy Whitcroft # Should start with a space. 428265863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 428365863862SAndy Whitcroft # Should not end with a space. 428465863862SAndy Whitcroft $to =~ s/\s+$//; 428565863862SAndy Whitcroft # '*'s should not have spaces between. 4286f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 428765863862SAndy Whitcroft } 428865863862SAndy Whitcroft # Modifiers should have spaces. 428965863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 429065863862SAndy Whitcroft 42913705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4292667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 42933705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 42943705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 42953705ce5bSJoe Perches $fix) { 42963705ce5bSJoe Perches 42973705ce5bSJoe Perches my $sub_from = $match; 42983705ce5bSJoe Perches my $sub_to = $match; 42993705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4300194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43013705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 43023705ce5bSJoe Perches } 430365863862SAndy Whitcroft } 43040a920b5bSAndy Whitcroft } 43050a920b5bSAndy Whitcroft 43069d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 43079d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 43080675a8fbSJean Delvare my $msg_level = \&WARN; 43090675a8fbSJean Delvare $msg_level = \&CHK if ($file); 43100675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 43119d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 43129d3e3c70SJoe Perches } 43130a920b5bSAndy Whitcroft 43149d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 43158905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4316000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4317000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 43188905a67cSAndy Whitcroft } 43198905a67cSAndy Whitcroft 432017441227SJoe Perches# check for uses of printk_ratelimit 432117441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4322000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4323000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 432417441227SJoe Perches } 432517441227SJoe Perches 4326eeef5733SJoe Perches# printk should use KERN_* levels 4327eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4328000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4329eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 433000df344fSAndy Whitcroft } 43310a920b5bSAndy Whitcroft 4332243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4333243f3803SJoe Perches my $orig = $1; 4334243f3803SJoe Perches my $level = lc($orig); 4335243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 43368f26b837SJoe Perches my $level2 = $level; 43378f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4338243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4339daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4340243f3803SJoe Perches } 4341243f3803SJoe Perches 4342dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4343dc139313SJoe Perches my $orig = $1; 4344dc139313SJoe Perches my $level = lc($orig); 4345dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4346dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4347dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4348dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4349dc139313SJoe Perches } 4350dc139313SJoe Perches 43518020b253SNicolas Boichat# trace_printk should not be used in production code. 43528020b253SNicolas Boichat if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { 43538020b253SNicolas Boichat WARN("TRACE_PRINTK", 43548020b253SNicolas Boichat "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); 43558020b253SNicolas Boichat } 43568020b253SNicolas Boichat 435791c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 435891c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 435991c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 436091c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 436191c9afafSAndy Lutomirski WARN("ENOSYS", 436291c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 436391c9afafSAndy Lutomirski } 436491c9afafSAndy Lutomirski 43656b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches. 43666b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 43676b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected. 43686b9ea5ffSJakub Kicinski if (!$file && $line =~ /\bENOTSUPP\b/) { 43696b9ea5ffSJakub Kicinski if (WARN("ENOTSUPP", 43706b9ea5ffSJakub Kicinski "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 43716b9ea5ffSJakub Kicinski $fix) { 43726b9ea5ffSJakub Kicinski $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 43736b9ea5ffSJakub Kicinski } 43746b9ea5ffSJakub Kicinski } 43756b9ea5ffSJakub Kicinski 4376653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4377653d4876SAndy Whitcroft# or if closed on same line 43785b57980dSJoe Perches if ($perl_version_ok && 43792d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 43802d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 43812d453e3bSJoe Perches $sline !~ /}/) { 43828d182478SJoe Perches if (ERROR("OPEN_BRACE", 43832d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 43848d182478SJoe Perches $fix) { 43858d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 43868d182478SJoe Perches my $fixed_line = $rawline; 43878d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 43888d182478SJoe Perches my $line1 = $1; 43898d182478SJoe Perches my $line2 = $2; 43908d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 43918d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 43928d182478SJoe Perches if ($line2 !~ /^\s*$/) { 43938d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 43948d182478SJoe Perches } 43958d182478SJoe Perches } 43960a920b5bSAndy Whitcroft } 4397653d4876SAndy Whitcroft 43988905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 43998905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 44008905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 44018d182478SJoe Perches if (ERROR("OPEN_BRACE", 44028d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 44038d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 44048d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 44058d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 44068d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 44078d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 44088d182478SJoe Perches $fixedline = $rawline; 44098d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 44108d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 44118d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 44128d182478SJoe Perches } 44138d182478SJoe Perches } 44148905a67cSAndy Whitcroft } 44158905a67cSAndy Whitcroft 44160c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 44173705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 44183705ce5bSJoe Perches if (WARN("SPACING", 44193705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 44203705ce5bSJoe Perches $fix) { 4421194f66fcSJoe Perches $fixed[$fixlinenr] =~ 44223705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 44233705ce5bSJoe Perches } 44240c73b4ebSAndy Whitcroft } 44250c73b4ebSAndy Whitcroft 442631070b5dSJoe Perches# Function pointer declarations 442731070b5dSJoe Perches# check spacing between type, funcptr, and args 442831070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 442991f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 443031070b5dSJoe Perches my $declare = $1; 443131070b5dSJoe Perches my $pre_pointer_space = $2; 443231070b5dSJoe Perches my $post_pointer_space = $3; 443331070b5dSJoe Perches my $funcname = $4; 443431070b5dSJoe Perches my $post_funcname_space = $5; 443531070b5dSJoe Perches my $pre_args_space = $6; 443631070b5dSJoe Perches 443791f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 443891f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 443991f72e9cSJoe Perches# don't need a space so don't warn for those. 444091f72e9cSJoe Perches my $post_declare_space = ""; 444191f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 444291f72e9cSJoe Perches $post_declare_space = $1; 444391f72e9cSJoe Perches $declare = rtrim($declare); 444491f72e9cSJoe Perches } 444591f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 444631070b5dSJoe Perches WARN("SPACING", 444731070b5dSJoe Perches "missing space after return type\n" . $herecurr); 444891f72e9cSJoe Perches $post_declare_space = " "; 444931070b5dSJoe Perches } 445031070b5dSJoe Perches 445131070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 445291f72e9cSJoe Perches# This test is not currently implemented because these declarations are 445391f72e9cSJoe Perches# equivalent to 445491f72e9cSJoe Perches# int foo(int bar, ...) 445591f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 445691f72e9cSJoe Perches# 445791f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 445891f72e9cSJoe Perches# WARN("SPACING", 445991f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 446091f72e9cSJoe Perches# } 446131070b5dSJoe Perches 446231070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 446331070b5dSJoe Perches if (defined $pre_pointer_space && 446431070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 446531070b5dSJoe Perches WARN("SPACING", 446631070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 446731070b5dSJoe Perches } 446831070b5dSJoe Perches 446931070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 447031070b5dSJoe Perches if (defined $post_pointer_space && 447131070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 447231070b5dSJoe Perches WARN("SPACING", 447331070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 447431070b5dSJoe Perches } 447531070b5dSJoe Perches 447631070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 447731070b5dSJoe Perches if (defined $post_funcname_space && 447831070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 447931070b5dSJoe Perches WARN("SPACING", 448031070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 448131070b5dSJoe Perches } 448231070b5dSJoe Perches 448331070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 448431070b5dSJoe Perches if (defined $pre_args_space && 448531070b5dSJoe Perches $pre_args_space =~ /^\s/) { 448631070b5dSJoe Perches WARN("SPACING", 448731070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 448831070b5dSJoe Perches } 448931070b5dSJoe Perches 449031070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4491194f66fcSJoe Perches $fixed[$fixlinenr] =~ 449291f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 449331070b5dSJoe Perches } 449431070b5dSJoe Perches } 449531070b5dSJoe Perches 44968d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 44978d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4498fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4499fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 45008d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 45018d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 45028d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4503fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 450438dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 45053705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 45063705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 45073705ce5bSJoe Perches $fix) { 4508194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45093705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 45103705ce5bSJoe Perches } 45118d31cfceSAndy Whitcroft } 45128d31cfceSAndy Whitcroft } 45138d31cfceSAndy Whitcroft 4514f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 45156c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4516c2fdda0dSAndy Whitcroft my $name = $1; 4517773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4518773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4519c2fdda0dSAndy Whitcroft 4520c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4521773647a0SAndy Whitcroft if ($name =~ /^(?: 4522773647a0SAndy Whitcroft if|for|while|switch|return|case| 4523773647a0SAndy Whitcroft volatile|__volatile__| 4524773647a0SAndy Whitcroft __attribute__|format|__extension__| 4525773647a0SAndy Whitcroft asm|__asm__)$/x) 4526773647a0SAndy Whitcroft { 4527c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4528c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4529c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4530c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4531773647a0SAndy Whitcroft 4532773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4533c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4534c2fdda0dSAndy Whitcroft 4535c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4536c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4537773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4538c2fdda0dSAndy Whitcroft 4539c2fdda0dSAndy Whitcroft } else { 45403705ce5bSJoe Perches if (WARN("SPACING", 45413705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 45423705ce5bSJoe Perches $fix) { 4543194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45443705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 45453705ce5bSJoe Perches } 4546f0a594c1SAndy Whitcroft } 45476c72ffaaSAndy Whitcroft } 45489a4cad4eSEric Nelson 4549653d4876SAndy Whitcroft# Check operator spacing. 45500a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 45513705ce5bSJoe Perches my $fixed_line = ""; 45523705ce5bSJoe Perches my $line_fixed = 0; 45533705ce5bSJoe Perches 45549c0ca6f9SAndy Whitcroft my $ops = qr{ 45559c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 45569c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 45579c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 45581f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 455984731623SJoe Perches \?:|\?|: 45609c0ca6f9SAndy Whitcroft }x; 4561cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 45623705ce5bSJoe Perches 45633705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 45643705ce5bSJoe Perches## foreach my $el (@elements) { 45653705ce5bSJoe Perches## print("el: <$el>\n"); 45663705ce5bSJoe Perches## } 45673705ce5bSJoe Perches 45683705ce5bSJoe Perches my @fix_elements = (); 456900df344fSAndy Whitcroft my $off = 0; 45706c72ffaaSAndy Whitcroft 45713705ce5bSJoe Perches foreach my $el (@elements) { 45723705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 45733705ce5bSJoe Perches $off += length($el); 45743705ce5bSJoe Perches } 45753705ce5bSJoe Perches 45763705ce5bSJoe Perches $off = 0; 45773705ce5bSJoe Perches 45786c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4579b34c648bSJoe Perches my $last_after = -1; 45806c72ffaaSAndy Whitcroft 45810a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 45823705ce5bSJoe Perches 45833705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 45843705ce5bSJoe Perches 45853705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 45863705ce5bSJoe Perches 45874a0df2efSAndy Whitcroft $off += length($elements[$n]); 45884a0df2efSAndy Whitcroft 458925985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4590773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4591773647a0SAndy Whitcroft my $cc = ''; 4592773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4593773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4594773647a0SAndy Whitcroft } 4595773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4596773647a0SAndy Whitcroft 45974a0df2efSAndy Whitcroft my $a = ''; 45984a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 45994a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4600cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 46014a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 46024a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4603773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 46044a0df2efSAndy Whitcroft 46050a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 46064a0df2efSAndy Whitcroft 46074a0df2efSAndy Whitcroft my $c = ''; 46080a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 46094a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 46104a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4611cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 46124a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 46134a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 46148b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 46154a0df2efSAndy Whitcroft } else { 46164a0df2efSAndy Whitcroft $c = 'E'; 46170a920b5bSAndy Whitcroft } 46180a920b5bSAndy Whitcroft 46194a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 46204a0df2efSAndy Whitcroft 46214a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 46224a0df2efSAndy Whitcroft 46236c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4624de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 46250a920b5bSAndy Whitcroft 462674048ed8SAndy Whitcroft # Pull out the value of this operator. 46276c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 46280a920b5bSAndy Whitcroft 46291f65f947SAndy Whitcroft # Get the full operator variant. 46301f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 46311f65f947SAndy Whitcroft 463213214adfSAndy Whitcroft # Ignore operators passed as parameters. 463313214adfSAndy Whitcroft if ($op_type ne 'V' && 4634d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 463513214adfSAndy Whitcroft 4636cf655043SAndy Whitcroft# # Ignore comments 4637cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 463813214adfSAndy Whitcroft 4639d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 464013214adfSAndy Whitcroft } elsif ($op eq ';') { 4641cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4642cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 46433705ce5bSJoe Perches if (ERROR("SPACING", 46443705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4645b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 46463705ce5bSJoe Perches $line_fixed = 1; 46473705ce5bSJoe Perches } 4648d8aaf121SAndy Whitcroft } 4649d8aaf121SAndy Whitcroft 4650d8aaf121SAndy Whitcroft # // is a comment 4651d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 46520a920b5bSAndy Whitcroft 4653b00e4814SJoe Perches # : when part of a bitfield 4654b00e4814SJoe Perches } elsif ($opv eq ':B') { 4655b00e4814SJoe Perches # skip the bitfield test for now 4656b00e4814SJoe Perches 46571f65f947SAndy Whitcroft # No spaces for: 46581f65f947SAndy Whitcroft # -> 4659b00e4814SJoe Perches } elsif ($op eq '->') { 46604a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 46613705ce5bSJoe Perches if (ERROR("SPACING", 46623705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4663b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 46643705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 46653705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 46663705ce5bSJoe Perches } 4667b34c648bSJoe Perches $line_fixed = 1; 46683705ce5bSJoe Perches } 46690a920b5bSAndy Whitcroft } 46700a920b5bSAndy Whitcroft 46712381097bSJoe Perches # , must not have a space before and must have a space on the right. 46720a920b5bSAndy Whitcroft } elsif ($op eq ',') { 46732381097bSJoe Perches my $rtrim_before = 0; 46742381097bSJoe Perches my $space_after = 0; 46752381097bSJoe Perches if ($ctx =~ /Wx./) { 46762381097bSJoe Perches if (ERROR("SPACING", 46772381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 46782381097bSJoe Perches $line_fixed = 1; 46792381097bSJoe Perches $rtrim_before = 1; 46802381097bSJoe Perches } 46812381097bSJoe Perches } 4682cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 46833705ce5bSJoe Perches if (ERROR("SPACING", 46843705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 46853705ce5bSJoe Perches $line_fixed = 1; 4686b34c648bSJoe Perches $last_after = $n; 46872381097bSJoe Perches $space_after = 1; 46882381097bSJoe Perches } 46892381097bSJoe Perches } 46902381097bSJoe Perches if ($rtrim_before || $space_after) { 46912381097bSJoe Perches if ($rtrim_before) { 46922381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 46932381097bSJoe Perches } else { 46942381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 46952381097bSJoe Perches } 46962381097bSJoe Perches if ($space_after) { 46972381097bSJoe Perches $good .= " "; 46983705ce5bSJoe Perches } 46990a920b5bSAndy Whitcroft } 47000a920b5bSAndy Whitcroft 47019c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 470274048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 47039c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 47049c0ca6f9SAndy Whitcroft 47059c0ca6f9SAndy Whitcroft # unary operators should have a space before and 47069c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 47079c0ca6f9SAndy Whitcroft # unary operator, or a cast 47089c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 470974048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 47100d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4711cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 47123705ce5bSJoe Perches if (ERROR("SPACING", 47133705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4714b34c648bSJoe Perches if ($n != $last_after + 2) { 4715b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 47163705ce5bSJoe Perches $line_fixed = 1; 47173705ce5bSJoe Perches } 47180a920b5bSAndy Whitcroft } 4719b34c648bSJoe Perches } 4720a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4721171ae1a4SAndy Whitcroft # A unary '*' may be const 4722171ae1a4SAndy Whitcroft 4723171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 47243705ce5bSJoe Perches if (ERROR("SPACING", 47253705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4726b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 47273705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 47283705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 47293705ce5bSJoe Perches } 4730b34c648bSJoe Perches $line_fixed = 1; 47313705ce5bSJoe Perches } 47320a920b5bSAndy Whitcroft } 47330a920b5bSAndy Whitcroft 47340a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 47350a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4736773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 47373705ce5bSJoe Perches if (ERROR("SPACING", 47383705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4739b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 47403705ce5bSJoe Perches $line_fixed = 1; 47413705ce5bSJoe Perches } 47420a920b5bSAndy Whitcroft } 4743773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4744773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 47453705ce5bSJoe Perches if (ERROR("SPACING", 47463705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4747b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 47483705ce5bSJoe Perches $line_fixed = 1; 47493705ce5bSJoe Perches } 4750653d4876SAndy Whitcroft } 4751773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 47523705ce5bSJoe Perches if (ERROR("SPACING", 47533705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4754b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 47553705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 47563705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4757773647a0SAndy Whitcroft } 4758b34c648bSJoe Perches $line_fixed = 1; 47593705ce5bSJoe Perches } 47603705ce5bSJoe Perches } 47610a920b5bSAndy Whitcroft 47620a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 47639c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 47649c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 47659c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4766c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4767c2fdda0dSAndy Whitcroft $op eq '%') 47680a920b5bSAndy Whitcroft { 4769d2e025f3SJoe Perches if ($check) { 4770d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4771d2e025f3SJoe Perches if (CHK("SPACING", 4772d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4773d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4774d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4775d2e025f3SJoe Perches $line_fixed = 1; 4776d2e025f3SJoe Perches } 4777d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4778d2e025f3SJoe Perches if (CHK("SPACING", 4779d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4780d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4781d2e025f3SJoe Perches $line_fixed = 1; 4782d2e025f3SJoe Perches } 4783d2e025f3SJoe Perches } 4784d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 47853705ce5bSJoe Perches if (ERROR("SPACING", 47863705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4787b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4788b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4789b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4790b34c648bSJoe Perches } 47913705ce5bSJoe Perches $line_fixed = 1; 47923705ce5bSJoe Perches } 47930a920b5bSAndy Whitcroft } 47940a920b5bSAndy Whitcroft 47951f65f947SAndy Whitcroft # A colon needs no spaces before when it is 47961f65f947SAndy Whitcroft # terminating a case value or a label. 47971f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 47981f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 47993705ce5bSJoe Perches if (ERROR("SPACING", 48003705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4801b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 48023705ce5bSJoe Perches $line_fixed = 1; 48033705ce5bSJoe Perches } 48041f65f947SAndy Whitcroft } 48051f65f947SAndy Whitcroft 48060a920b5bSAndy Whitcroft # All the others need spaces both sides. 4807cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 48081f65f947SAndy Whitcroft my $ok = 0; 48091f65f947SAndy Whitcroft 481022f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 48111f65f947SAndy Whitcroft if (($op eq '<' && 48121f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 48131f65f947SAndy Whitcroft ($op eq '>' && 48141f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 48151f65f947SAndy Whitcroft { 48161f65f947SAndy Whitcroft $ok = 1; 48171f65f947SAndy Whitcroft } 48181f65f947SAndy Whitcroft 4819e0df7e1fSJoe Perches # for asm volatile statements 4820e0df7e1fSJoe Perches # ignore a colon with another 4821e0df7e1fSJoe Perches # colon immediately before or after 4822e0df7e1fSJoe Perches if (($op eq ':') && 4823e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4824e0df7e1fSJoe Perches $ok = 1; 4825e0df7e1fSJoe Perches } 4826e0df7e1fSJoe Perches 482784731623SJoe Perches # messages are ERROR, but ?: are CHK 48281f65f947SAndy Whitcroft if ($ok == 0) { 48290675a8fbSJean Delvare my $msg_level = \&ERROR; 48300675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 483184731623SJoe Perches 48320675a8fbSJean Delvare if (&{$msg_level}("SPACING", 48333705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4834b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4835b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4836b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4837b34c648bSJoe Perches } 48383705ce5bSJoe Perches $line_fixed = 1; 48393705ce5bSJoe Perches } 48400a920b5bSAndy Whitcroft } 484122f2a2efSAndy Whitcroft } 48424a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 48433705ce5bSJoe Perches 48443705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 48453705ce5bSJoe Perches 48463705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 48470a920b5bSAndy Whitcroft } 48483705ce5bSJoe Perches 48493705ce5bSJoe Perches if (($#elements % 2) == 0) { 48503705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 48513705ce5bSJoe Perches } 48523705ce5bSJoe Perches 4853194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4854194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 48553705ce5bSJoe Perches } 48563705ce5bSJoe Perches 48573705ce5bSJoe Perches 48580a920b5bSAndy Whitcroft } 48590a920b5bSAndy Whitcroft 4860786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4861d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4862786b6326SJoe Perches if (WARN("SPACING", 4863786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4864786b6326SJoe Perches $fix) { 4865194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4866786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4867786b6326SJoe Perches } 4868786b6326SJoe Perches } 4869786b6326SJoe Perches 4870f0a594c1SAndy Whitcroft# check for multiple assignments 4871f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4872000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4873000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4874f0a594c1SAndy Whitcroft } 4875f0a594c1SAndy Whitcroft 487622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 487722f2a2efSAndy Whitcroft## # continuation. 487822f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 487922f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 488022f2a2efSAndy Whitcroft## 488122f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 488222f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 488322f2a2efSAndy Whitcroft## my $ln = $line; 488422f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 488522f2a2efSAndy Whitcroft## } 488622f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4887000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4888000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 488922f2a2efSAndy Whitcroft## } 489022f2a2efSAndy Whitcroft## } 4891f0a594c1SAndy Whitcroft 48920a920b5bSAndy Whitcroft#need space before brace following if, while, etc 48936b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 48946ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 48953705ce5bSJoe Perches if (ERROR("SPACING", 48963705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 48973705ce5bSJoe Perches $fix) { 48986ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 48993705ce5bSJoe Perches } 4900de7d4f0eSAndy Whitcroft } 4901de7d4f0eSAndy Whitcroft 4902c4a62ef9SJoe Perches## # check for blank lines before declarations 4903c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4904c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4905c4a62ef9SJoe Perches## WARN("SPACING", 4906c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4907c4a62ef9SJoe Perches## } 4908c4a62ef9SJoe Perches## 4909c4a62ef9SJoe Perches 4910de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4911de7d4f0eSAndy Whitcroft# on the line 491294fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 4913d5e616fcSJoe Perches if (ERROR("SPACING", 4914d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4915d5e616fcSJoe Perches $fix) { 4916194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4917d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4918d5e616fcSJoe Perches } 49190a920b5bSAndy Whitcroft } 49200a920b5bSAndy Whitcroft 492122f2a2efSAndy Whitcroft# check spacing on square brackets 492222f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 49233705ce5bSJoe Perches if (ERROR("SPACING", 49243705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 49253705ce5bSJoe Perches $fix) { 4926194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49273705ce5bSJoe Perches s/\[\s+/\[/; 49283705ce5bSJoe Perches } 492922f2a2efSAndy Whitcroft } 493022f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 49313705ce5bSJoe Perches if (ERROR("SPACING", 49323705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 49333705ce5bSJoe Perches $fix) { 4934194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49353705ce5bSJoe Perches s/\s+\]/\]/; 49363705ce5bSJoe Perches } 493722f2a2efSAndy Whitcroft } 493822f2a2efSAndy Whitcroft 4939c45dcabdSAndy Whitcroft# check spacing on parentheses 49409c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 49419c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 49423705ce5bSJoe Perches if (ERROR("SPACING", 49433705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 49443705ce5bSJoe Perches $fix) { 4945194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49463705ce5bSJoe Perches s/\(\s+/\(/; 49473705ce5bSJoe Perches } 494822f2a2efSAndy Whitcroft } 494913214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4950c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4951c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 49523705ce5bSJoe Perches if (ERROR("SPACING", 49533705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 49543705ce5bSJoe Perches $fix) { 4955194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49563705ce5bSJoe Perches s/\s+\)/\)/; 49573705ce5bSJoe Perches } 495822f2a2efSAndy Whitcroft } 495922f2a2efSAndy Whitcroft 4960e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4961e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4962e2826fd0SJoe Perches 4963e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4964ea4acbb1SJoe Perches my $var = $1; 4965ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4966ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4967ea4acbb1SJoe Perches $fix) { 4968ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4969ea4acbb1SJoe Perches } 4970ea4acbb1SJoe Perches } 4971ea4acbb1SJoe Perches 4972ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4973ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4974ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4975ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4976ea4acbb1SJoe Perches my $var = $2; 4977ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4978ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4979ea4acbb1SJoe Perches $fix) { 4980ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4981ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4982ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4983ea4acbb1SJoe Perches } 4984e2826fd0SJoe Perches } 4985e2826fd0SJoe Perches 498663b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4987a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4988a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 49895b57980dSJoe Perches $perl_version_ok && defined($stat) && 499063b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 499163b7c73eSJoe Perches my $if_stat = $1; 499263b7c73eSJoe Perches my $test = substr($2, 1, -1); 499363b7c73eSJoe Perches my $herectx; 499463b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 499563b7c73eSJoe Perches my $match = $1; 499663b7c73eSJoe Perches # avoid parentheses around potential macro args 499763b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 499863b7c73eSJoe Perches if (!defined($herectx)) { 499963b7c73eSJoe Perches $herectx = $here . "\n"; 500063b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 500163b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 500263b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 500363b7c73eSJoe Perches $herectx .= $rl . "\n"; 500463b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 500563b7c73eSJoe Perches } 500663b7c73eSJoe Perches } 500763b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 500863b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 500963b7c73eSJoe Perches } 501063b7c73eSJoe Perches } 501163b7c73eSJoe Perches 50120a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 50134a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 50140a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 50153705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 50163705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 50173705ce5bSJoe Perches $fix) { 5018194f66fcSJoe Perches $fixed[$fixlinenr] =~ 50193705ce5bSJoe Perches s/^(.)\s+/$1/; 50203705ce5bSJoe Perches } 50210a920b5bSAndy Whitcroft } 50220a920b5bSAndy Whitcroft 502340873abaSJoe Perches# check if a statement with a comma should be two statements like: 502440873abaSJoe Perches# foo = bar(), /* comma should be semicolon */ 502540873abaSJoe Perches# bar = baz(); 502640873abaSJoe Perches if (defined($stat) && 502740873abaSJoe Perches $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { 502840873abaSJoe Perches my $cnt = statement_rawlines($stat); 502940873abaSJoe Perches my $herectx = get_stat_here($linenr, $cnt, $here); 503040873abaSJoe Perches WARN("SUSPECT_COMMA_SEMICOLON", 503140873abaSJoe Perches "Possible comma where semicolon could be used\n" . $herectx); 503240873abaSJoe Perches } 503340873abaSJoe Perches 50345b9553abSJoe Perches# return is not a function 5035507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 5036c45dcabdSAndy Whitcroft my $spacing = $1; 50375b57980dSJoe Perches if ($perl_version_ok && 50385b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 50395b9553abSJoe Perches my $value = $1; 50405b9553abSJoe Perches $value = deparenthesize($value); 50415b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 5042000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 5043000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 50445b9553abSJoe Perches } 5045c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 5046000d1cc1SJoe Perches ERROR("SPACING", 5047000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 5048c45dcabdSAndy Whitcroft } 5049c45dcabdSAndy Whitcroft } 5050507e5141SJoe Perches 5051b43ae21bSJoe Perches# unnecessary return in a void function 5052b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 5053b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 5054b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 5055b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 5056b43ae21bSJoe Perches $linenr >= 3 && 5057b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 5058b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 50599819cf25SJoe Perches WARN("RETURN_VOID", 5060b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 50619819cf25SJoe Perches } 50629819cf25SJoe Perches 5063189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 50645b57980dSJoe Perches if ($perl_version_ok && 5065189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 5066189248d8SJoe Perches my $openparens = $1; 5067189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 5068189248d8SJoe Perches my $msg = ""; 5069189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 5070189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 5071189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 5072189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 5073189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 5074189248d8SJoe Perches } 5075189248d8SJoe Perches } 5076189248d8SJoe Perches 5077c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 5078c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 5079c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 5080c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 50815b57980dSJoe Perches if ($perl_version_ok && 5082c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 5083c5595fa2SJoe Perches my $lead = $1; 5084c5595fa2SJoe Perches my $const = $2; 5085c5595fa2SJoe Perches my $comp = $3; 5086c5595fa2SJoe Perches my $to = $4; 5087c5595fa2SJoe Perches my $newcomp = $comp; 5088f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 5089c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 5090c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 5091c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 5092c5595fa2SJoe Perches $fix) { 5093c5595fa2SJoe Perches if ($comp eq "<") { 5094c5595fa2SJoe Perches $newcomp = ">"; 5095c5595fa2SJoe Perches } elsif ($comp eq "<=") { 5096c5595fa2SJoe Perches $newcomp = ">="; 5097c5595fa2SJoe Perches } elsif ($comp eq ">") { 5098c5595fa2SJoe Perches $newcomp = "<"; 5099c5595fa2SJoe Perches } elsif ($comp eq ">=") { 5100c5595fa2SJoe Perches $newcomp = "<="; 5101c5595fa2SJoe Perches } 5102c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 5103c5595fa2SJoe Perches } 5104c5595fa2SJoe Perches } 5105c5595fa2SJoe Perches 5106f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 5107f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 510853a3c448SAndy Whitcroft my $name = $1; 510953a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 5110000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 5111f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 511253a3c448SAndy Whitcroft } 511353a3c448SAndy Whitcroft } 5114c45dcabdSAndy Whitcroft 51150a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 51164a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 51173705ce5bSJoe Perches if (ERROR("SPACING", 51183705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 51193705ce5bSJoe Perches $fix) { 5120194f66fcSJoe Perches $fixed[$fixlinenr] =~ 51213705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 51223705ce5bSJoe Perches } 51230a920b5bSAndy Whitcroft } 51240a920b5bSAndy Whitcroft 5125f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 5126f5fe35ddSAndy Whitcroft# statements after the conditional. 5127170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 51283e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 51293e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 51303e469cdcSAndy Whitcroft if (!defined $stat); 5131170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 5132170d3a22SAndy Whitcroft $remain_next, $off_next); 5133170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 5134170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 5135170d3a22SAndy Whitcroft 5136170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 5137170d3a22SAndy Whitcroft # If the statement carries leading newlines, 5138170d3a22SAndy Whitcroft # then count those as offsets. 5139170d3a22SAndy Whitcroft my ($whitespace) = 5140170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 5141170d3a22SAndy Whitcroft my $offset = 5142170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 5143170d3a22SAndy Whitcroft 5144170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 5145170d3a22SAndy Whitcroft $offset} = 1; 5146170d3a22SAndy Whitcroft } 5147170d3a22SAndy Whitcroft } 5148170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 5149c11230f4SJoe Perches defined($stat) && defined($cond) && 5150170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 5151171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 51528905a67cSAndy Whitcroft 5153b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 515465b64b3bSJoe Perches if (ERROR("ASSIGN_IN_IF", 515565b64b3bSJoe Perches "do not use assignment in if condition\n" . $herecurr) && 515665b64b3bSJoe Perches $fix && $perl_version_ok) { 515765b64b3bSJoe Perches if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { 515865b64b3bSJoe Perches my $space = $1; 515965b64b3bSJoe Perches my $not = $2; 516065b64b3bSJoe Perches my $statement = $3; 516165b64b3bSJoe Perches my $assigned = $4; 516265b64b3bSJoe Perches my $test = $8; 516365b64b3bSJoe Perches my $against = $9; 516465b64b3bSJoe Perches my $brace = $15; 516565b64b3bSJoe Perches fix_delete_line($fixlinenr, $rawline); 516665b64b3bSJoe Perches fix_insert_line($fixlinenr, "$space$statement;"); 516765b64b3bSJoe Perches my $newline = "${space}if ("; 516865b64b3bSJoe Perches $newline .= '!' if defined($not); 516965b64b3bSJoe Perches $newline .= '(' if (defined $not && defined($test) && defined($against)); 517065b64b3bSJoe Perches $newline .= "$assigned"; 517165b64b3bSJoe Perches $newline .= " $test $against" if (defined($test) && defined($against)); 517265b64b3bSJoe Perches $newline .= ')' if (defined $not && defined($test) && defined($against)); 517365b64b3bSJoe Perches $newline .= ')'; 517465b64b3bSJoe Perches $newline .= " {" if (defined($brace)); 517565b64b3bSJoe Perches fix_insert_line($fixlinenr + 1, $newline); 517665b64b3bSJoe Perches } 517765b64b3bSJoe Perches } 51788905a67cSAndy Whitcroft } 51798905a67cSAndy Whitcroft 51808905a67cSAndy Whitcroft # Find out what is on the end of the line after the 51818905a67cSAndy Whitcroft # conditional. 5182773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 51838905a67cSAndy Whitcroft $s =~ s/\n.*//g; 518413214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 518553210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 518653210168SAndy Whitcroft $c !~ /}\s*while\s*/) 5187773647a0SAndy Whitcroft { 5188bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 5189bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 5190bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 519142bdf74cSHidetoshi Seto my $stat_real = ''; 5192bb44ad39SAndy Whitcroft 519342bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 519442bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 5195bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 5196bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 5197bb44ad39SAndy Whitcroft } 5198bb44ad39SAndy Whitcroft 5199000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5200000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 52018905a67cSAndy Whitcroft } 52028905a67cSAndy Whitcroft } 52038905a67cSAndy Whitcroft 520413214adfSAndy Whitcroft# Check for bitwise tests written as boolean 520513214adfSAndy Whitcroft if ($line =~ / 520613214adfSAndy Whitcroft (?: 520713214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 520813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 520913214adfSAndy Whitcroft (?:\&\&|\|\|) 521013214adfSAndy Whitcroft | 521113214adfSAndy Whitcroft (?:\&\&|\|\|) 521213214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 521313214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 521413214adfSAndy Whitcroft )/x) 521513214adfSAndy Whitcroft { 5216000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 5217000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 521813214adfSAndy Whitcroft } 521913214adfSAndy Whitcroft 52208905a67cSAndy Whitcroft# if and else should not have general statements after it 522113214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 522213214adfSAndy Whitcroft my $s = $1; 522313214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 522413214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5225000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5226000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 52270a920b5bSAndy Whitcroft } 522813214adfSAndy Whitcroft } 522939667782SAndy Whitcroft# if should not continue a brace 523039667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5231000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5232048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 523339667782SAndy Whitcroft $herecurr); 523439667782SAndy Whitcroft } 5235a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5236a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5237a1080bf8SAndy Whitcroft $line !~ /\G(?: 52383fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5239a1080bf8SAndy Whitcroft \s*return\s+ 5240a1080bf8SAndy Whitcroft )/xg) 5241a1080bf8SAndy Whitcroft { 5242000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5243000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5244a1080bf8SAndy Whitcroft } 52450a920b5bSAndy Whitcroft 52460a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 52470a920b5bSAndy Whitcroft # indent level to be relevant to each other. 52488b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 52490a920b5bSAndy Whitcroft $previndent == $indent) { 52508b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 52518b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 52528b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 52538b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 52548b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 52558b8856f4SJoe Perches my $fixedline = $prevrawline; 52568b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 52578b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 52588b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 52598b8856f4SJoe Perches } 52608b8856f4SJoe Perches $fixedline = $rawline; 52618b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 52628b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 52638b8856f4SJoe Perches } 52640a920b5bSAndy Whitcroft } 52650a920b5bSAndy Whitcroft 52668b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5267c2fdda0dSAndy Whitcroft $previndent == $indent) { 5268c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5269c2fdda0dSAndy Whitcroft 5270c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5271c2fdda0dSAndy Whitcroft # conditional. 5272773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5273c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5274c2fdda0dSAndy Whitcroft 5275c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 52768b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 52778b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 52788b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 52798b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 52808b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 52818b8856f4SJoe Perches my $fixedline = $prevrawline; 52828b8856f4SJoe Perches my $trailing = $rawline; 52838b8856f4SJoe Perches $trailing =~ s/^\+//; 52848b8856f4SJoe Perches $trailing = trim($trailing); 52858b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 52868b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 52878b8856f4SJoe Perches } 5288c2fdda0dSAndy Whitcroft } 5289c2fdda0dSAndy Whitcroft } 5290c2fdda0dSAndy Whitcroft 529195e2c602SJoe Perches#Specific variable tests 5292323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5293323c1260SJoe Perches my $var = $1; 529495e2c602SJoe Perches 529595e2c602SJoe Perches#CamelCase 5296807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5297be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 529822735ce8SJoe Perches#Ignore Page<foo> variants 5299807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5300d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5301d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5302d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5303f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5304f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 53057e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 53067e781f67SJoe Perches my $word = $1; 53077e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5308d8b07710SJoe Perches if ($check) { 5309d8b07710SJoe Perches seed_camelcase_includes(); 5310d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5311d8b07710SJoe Perches seed_camelcase_file($realfile); 5312d8b07710SJoe Perches $camelcase_file_seeded = 1; 5313d8b07710SJoe Perches } 5314d8b07710SJoe Perches } 53157e781f67SJoe Perches if (!defined $camelcase{$word}) { 53167e781f67SJoe Perches $camelcase{$word} = 1; 5317be79794bSJoe Perches CHK("CAMELCASE", 53187e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 53197e781f67SJoe Perches } 5320323c1260SJoe Perches } 5321323c1260SJoe Perches } 53223445686aSJoe Perches } 53230a920b5bSAndy Whitcroft 53240a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5325d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5326d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5327d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5328d5e616fcSJoe Perches $fix) { 5329194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5330d5e616fcSJoe Perches } 53310a920b5bSAndy Whitcroft } 53320a920b5bSAndy Whitcroft 53330e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 53340e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5335c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5336e09dec48SAndy Whitcroft my $file = "$1.h"; 5337e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5338e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5339e09dec48SAndy Whitcroft $realfile ne $checkfile && 53407840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5341c45dcabdSAndy Whitcroft { 53420e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 53430e212e0aSFabian Frederick if ($asminclude > 0) { 5344e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5345000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5346000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5347e09dec48SAndy Whitcroft } else { 5348000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5349000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5350e09dec48SAndy Whitcroft } 53510a920b5bSAndy Whitcroft } 53520a920b5bSAndy Whitcroft } 53530e212e0aSFabian Frederick } 53540a920b5bSAndy Whitcroft 5355653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5356653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5357cf655043SAndy Whitcroft# in a known good container 5358b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5359b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5360d8aaf121SAndy Whitcroft my $ln = $linenr; 5361d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5362c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5363c45dcabdSAndy Whitcroft my $ctx = ''; 536408a2843eSJoe Perches my $has_flow_statement = 0; 536508a2843eSJoe Perches my $has_arg_concat = 0; 5366c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5367f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5368f74bd194SAndy Whitcroft $ctx = $dstat; 5369c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5370a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5371c45dcabdSAndy Whitcroft 537208a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 537362e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 537408a2843eSJoe Perches 5375f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5376f59b64bfSJoe Perches my $define_args = $1; 5377f59b64bfSJoe Perches my $define_stmt = $dstat; 5378f59b64bfSJoe Perches my @def_args = (); 5379f59b64bfSJoe Perches 5380f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5381f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5382f59b64bfSJoe Perches $define_args =~ s/\s*//g; 53838c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5384f59b64bfSJoe Perches @def_args = split(",", $define_args); 5385f59b64bfSJoe Perches } 5386f59b64bfSJoe Perches 5387292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5388c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5389c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5390c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5391c45dcabdSAndy Whitcroft 5392c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 53932e44e803SDwaipayan Ray while ($dstat =~ s/\([^\(\)]*\)/1u/ || 53942e44e803SDwaipayan Ray $dstat =~ s/\{[^\{\}]*\}/1u/ || 53952e44e803SDwaipayan Ray $dstat =~ s/.\[[^\[\]]*\]/1u/) 5396bf30d6edSAndy Whitcroft { 5397c45dcabdSAndy Whitcroft } 5398c45dcabdSAndy Whitcroft 5399342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 540033acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 540133acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5402e45bab8eSAndy Whitcroft { 5403e45bab8eSAndy Whitcroft } 5404e45bab8eSAndy Whitcroft 540542e15293SJoe Perches # Make asm volatile uses seem like a generic function 540642e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 540742e15293SJoe Perches 5408c45dcabdSAndy Whitcroft my $exceptions = qr{ 5409c45dcabdSAndy Whitcroft $Declare| 5410c45dcabdSAndy Whitcroft module_param_named| 5411a0a0a7a9SKees Cook MODULE_PARM_DESC| 5412c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5413c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5414383099fdSAndy Whitcroft __typeof__\(| 541522fd2d3eSStefani Seibold union| 541622fd2d3eSStefani Seibold struct| 5417ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 54186b10df42SVladimir Zapolskiy ^\"|\"$| 54196b10df42SVladimir Zapolskiy ^\[ 5420c45dcabdSAndy Whitcroft }x; 54215eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5422f59b64bfSJoe Perches 5423f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5424f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5425e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5426f59b64bfSJoe Perches 5427f74bd194SAndy Whitcroft if ($dstat ne '' && 5428f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5429f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 54303cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5431356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5432f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5433f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5434e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 543572f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 54362e44e803SDwaipayan Ray $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} 5437f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5438f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5439f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 54404e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5441f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5442c45dcabdSAndy Whitcroft { 5443e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5444e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5445e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5446e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5447f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5448f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5449f74bd194SAndy Whitcroft } else { 5450000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5451388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5452d8aaf121SAndy Whitcroft } 5453f59b64bfSJoe Perches 5454f59b64bfSJoe Perches } 54555207649bSJoe Perches 54565207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 54575207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 54585207649bSJoe Perches my $first = 1; 54595207649bSJoe Perches $define_stmt = ""; 54605207649bSJoe Perches foreach my $l (@stmt_array) { 54615207649bSJoe Perches $l =~ s/\\$//; 54625207649bSJoe Perches if ($first) { 54635207649bSJoe Perches $define_stmt = $l; 54645207649bSJoe Perches $first = 0; 54655207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 54665207649bSJoe Perches $define_stmt .= substr($l, 1); 54675207649bSJoe Perches } 54685207649bSJoe Perches } 54695207649bSJoe Perches $define_stmt =~ s/$;//g; 54705207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 54715207649bSJoe Perches $define_stmt = trim($define_stmt); 54725207649bSJoe Perches 5473f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5474f59b64bfSJoe Perches foreach my $arg (@def_args) { 5475f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 54769192d41aSJoe Perches next if ($arg =~ /^type$/i); 54777fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 54786dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 54797fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 54807fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5481d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5482f59b64bfSJoe Perches if ($use_cnt > 1) { 5483f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5484f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5485f59b64bfSJoe Perches } 54869192d41aSJoe Perches# check if any macro arguments may have other precedence issues 54877fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 54889192d41aSJoe Perches ((defined($1) && $1 ne ',') || 54899192d41aSJoe Perches (defined($2) && $2 ne ','))) { 54909192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 54919192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 54929192d41aSJoe Perches } 54930a920b5bSAndy Whitcroft } 54945023d347SJoe Perches 549508a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 549608a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 549708a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 549808a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5499e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 550008a2843eSJoe Perches 550108a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 550208a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 550308a2843eSJoe Perches } 550408a2843eSJoe Perches 5505481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 55065023d347SJoe Perches 55075023d347SJoe Perches } else { 55085023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5509481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5510481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 55115023d347SJoe Perches $line =~ /^\+.*\\$/) { 55125023d347SJoe Perches WARN("LINE_CONTINUATIONS", 55135023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 55145023d347SJoe Perches } 5515653d4876SAndy Whitcroft } 55160a920b5bSAndy Whitcroft 5517b13edf7fSJoe Perches# do {} while (0) macro tests: 5518b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5519b13edf7fSJoe Perches# macro should not end with a semicolon 55205b57980dSJoe Perches if ($perl_version_ok && 5521b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5522b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5523b13edf7fSJoe Perches my $ln = $linenr; 5524b13edf7fSJoe Perches my $cnt = $realcnt; 5525b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5526b13edf7fSJoe Perches my $ctx = ''; 5527b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5528b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5529b13edf7fSJoe Perches $ctx = $dstat; 5530b13edf7fSJoe Perches 5531b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 55321b36b201SJoe Perches $dstat =~ s/$;/ /g; 5533b13edf7fSJoe Perches 5534b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5535b13edf7fSJoe Perches my $stmts = $2; 5536b13edf7fSJoe Perches my $semis = $3; 5537b13edf7fSJoe Perches 5538b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5539b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5540e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5541b13edf7fSJoe Perches 5542ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5543ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5544b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5545b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5546b13edf7fSJoe Perches } 5547b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5548b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5549b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5550b13edf7fSJoe Perches } 5551f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5552f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5553f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5554e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5555f5ef95b1SJoe Perches 5556f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5557f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5558b13edf7fSJoe Perches } 5559b13edf7fSJoe Perches } 5560b13edf7fSJoe Perches 5561f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 556213214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 556313214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5564cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 556513214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5566cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5567cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5568aad4f614SJoe Perches my @allowed = (); 5569aad4f614SJoe Perches my $allow = 0; 557013214adfSAndy Whitcroft my $seen = 0; 5571773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5572cf655043SAndy Whitcroft my $ln = $linenr - 1; 557313214adfSAndy Whitcroft for my $chunk (@chunks) { 557413214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 557513214adfSAndy Whitcroft 5576773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5577773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5578773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5579773647a0SAndy Whitcroft 5580aad4f614SJoe Perches $allowed[$allow] = 0; 5581773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5582773647a0SAndy Whitcroft 5583773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5584773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5585773647a0SAndy Whitcroft 5586773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5587cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5588cf655043SAndy Whitcroft 5589773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 559013214adfSAndy Whitcroft 559113214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 559213214adfSAndy Whitcroft 5593aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5594cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5595cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5596aad4f614SJoe Perches $allowed[$allow] = 1; 559713214adfSAndy Whitcroft } 559813214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5599cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5600aad4f614SJoe Perches $allowed[$allow] = 1; 560113214adfSAndy Whitcroft } 5602cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5603cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5604aad4f614SJoe Perches $allowed[$allow] = 1; 560513214adfSAndy Whitcroft } 5606aad4f614SJoe Perches $allow++; 560713214adfSAndy Whitcroft } 5608aad4f614SJoe Perches if ($seen) { 5609aad4f614SJoe Perches my $sum_allowed = 0; 5610aad4f614SJoe Perches foreach (@allowed) { 5611aad4f614SJoe Perches $sum_allowed += $_; 5612aad4f614SJoe Perches } 5613aad4f614SJoe Perches if ($sum_allowed == 0) { 5614000d1cc1SJoe Perches WARN("BRACES", 5615000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5616aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5617aad4f614SJoe Perches $seen != $allow) { 5618aad4f614SJoe Perches CHK("BRACES", 5619aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5620aad4f614SJoe Perches } 562113214adfSAndy Whitcroft } 562213214adfSAndy Whitcroft } 562313214adfSAndy Whitcroft } 5624773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 562513214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5626cf655043SAndy Whitcroft my $allowed = 0; 5627f0a594c1SAndy Whitcroft 5628cf655043SAndy Whitcroft # Check the pre-context. 5629cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5630cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5631cf655043SAndy Whitcroft $allowed = 1; 5632f0a594c1SAndy Whitcroft } 5633773647a0SAndy Whitcroft 5634773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5635773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5636773647a0SAndy Whitcroft 5637cf655043SAndy Whitcroft # Check the condition. 5638cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5639773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5640cf655043SAndy Whitcroft if (defined $cond) { 5641773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5642cf655043SAndy Whitcroft } 5643cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5644cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5645cf655043SAndy Whitcroft $allowed = 1; 5646cf655043SAndy Whitcroft } 5647cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5648cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5649cf655043SAndy Whitcroft $allowed = 1; 5650cf655043SAndy Whitcroft } 5651cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5652cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5653cf655043SAndy Whitcroft $allowed = 1; 5654cf655043SAndy Whitcroft } 5655cf655043SAndy Whitcroft # Check the post-context. 5656cf655043SAndy Whitcroft if (defined $chunks[1]) { 5657cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5658cf655043SAndy Whitcroft if (defined $cond) { 5659773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5660cf655043SAndy Whitcroft } 5661cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5662cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5663cf655043SAndy Whitcroft $allowed = 1; 5664cf655043SAndy Whitcroft } 5665cf655043SAndy Whitcroft } 5666cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5667f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5668e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5669cf655043SAndy Whitcroft 5670000d1cc1SJoe Perches WARN("BRACES", 5671000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5672f0a594c1SAndy Whitcroft } 5673f0a594c1SAndy Whitcroft } 5674f0a594c1SAndy Whitcroft 5675e4c5babdSJoe Perches# check for single line unbalanced braces 567695330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 567795330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5678e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5679e4c5babdSJoe Perches } 5680e4c5babdSJoe Perches 56810979ae66SJoe Perches# check for unnecessary blank lines around braces 568277b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5683f8e58219SJoe Perches if (CHK("BRACES", 5684f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5685f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5686f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5687f8e58219SJoe Perches } 56880979ae66SJoe Perches } 568977b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5690f8e58219SJoe Perches if (CHK("BRACES", 5691f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5692f8e58219SJoe Perches $fix) { 5693f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5694f8e58219SJoe Perches } 56950979ae66SJoe Perches } 56960979ae66SJoe Perches 56974a0df2efSAndy Whitcroft# no volatiles please 56986c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 56996c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5700000d1cc1SJoe Perches WARN("VOLATILE", 57018c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 57024a0df2efSAndy Whitcroft } 57034a0df2efSAndy Whitcroft 57045e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 57055e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 57065e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 57075e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 570833acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 57095e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 57105e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 57115e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 57125e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 57135e4f6ba5SJoe Perches $fix && 57145e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 57155e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 57165e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 57175e4f6ba5SJoe Perches my $comma_close = ""; 57185e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 57195e4f6ba5SJoe Perches $comma_close = $1; 57205e4f6ba5SJoe Perches } 57215e4f6ba5SJoe Perches 57225e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 57235e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 57245e4f6ba5SJoe Perches my $fixedline = $prevrawline; 57255e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 57265e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 57275e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 57285e4f6ba5SJoe Perches $fixedline = $rawline; 57295e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 57305e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 57315e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 57325e4f6ba5SJoe Perches } 57335e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 57345e4f6ba5SJoe Perches } 57355e4f6ba5SJoe Perches } 57365e4f6ba5SJoe Perches 57375e4f6ba5SJoe Perches# check for missing a space in a string concatenation 57385e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 57395e4f6ba5SJoe Perches WARN('MISSING_SPACE', 57405e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 57415e4f6ba5SJoe Perches } 57425e4f6ba5SJoe Perches 574377cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5744e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5745e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5746e4b7d309SJoe Perches# function declarations 574777cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 574877cb8546SJoe Perches defined($context_function) && 5749e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5750e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 575177cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5752e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 575377cb8546SJoe Perches } 575477cb8546SJoe Perches 57555e4f6ba5SJoe Perches# check for spaces before a quoted newline 57565e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 57575e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 57585e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 57595e4f6ba5SJoe Perches $fix) { 57605e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 57615e4f6ba5SJoe Perches } 57625e4f6ba5SJoe Perches 57635e4f6ba5SJoe Perches } 57645e4f6ba5SJoe Perches 5765f17dba4fSJoe Perches# concatenated string without spaces between elements 576679682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 576779682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 576879682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 576979682c0cSJoe Perches $fix) { 577079682c0cSJoe Perches while ($line =~ /($String)/g) { 577179682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 577279682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 577379682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 577479682c0cSJoe Perches } 577579682c0cSJoe Perches } 5776f17dba4fSJoe Perches } 5777f17dba4fSJoe Perches 577890ad30e5SJoe Perches# uncoalesced string fragments 577933acb54aSJoe Perches if ($line =~ /$String\s*"/) { 578079682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 578179682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 578279682c0cSJoe Perches $fix) { 578379682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 578479682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 578579682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 578679682c0cSJoe Perches } 578779682c0cSJoe Perches } 578890ad30e5SJoe Perches } 578990ad30e5SJoe Perches 5790522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5791522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5792522b837cSAlexey Dobriyan my $show_Z = 1; 57935e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5794522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 57955e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5796522b837cSAlexey Dobriyan # check for %L 5797522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 57985e4f6ba5SJoe Perches WARN("PRINTF_L", 5799522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5800522b837cSAlexey Dobriyan $show_L = 0; 58015e4f6ba5SJoe Perches } 5802522b837cSAlexey Dobriyan # check for %Z 5803522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5804522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5805522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5806522b837cSAlexey Dobriyan $show_Z = 0; 5807522b837cSAlexey Dobriyan } 5808522b837cSAlexey Dobriyan # check for 0x<decimal> 5809522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5810522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 58116e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 58126e300757SJoe Perches } 58135e4f6ba5SJoe Perches } 58145e4f6ba5SJoe Perches 58155e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 58163f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 58175e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 58185e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 58195e4f6ba5SJoe Perches } 58205e4f6ba5SJoe Perches 582100df344fSAndy Whitcroft# warn about #if 0 5822c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 582360f89010SPrakruthi Deepak Heragu WARN("IF_0", 582460f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 582560f89010SPrakruthi Deepak Heragu } 582660f89010SPrakruthi Deepak Heragu 582760f89010SPrakruthi Deepak Heragu# warn about #if 1 582860f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 582960f89010SPrakruthi Deepak Heragu WARN("IF_1", 583060f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 58314a0df2efSAndy Whitcroft } 58324a0df2efSAndy Whitcroft 583303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 583403df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5835100425deSJoe Perches my $tested = quotemeta($1); 5836100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5837100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5838100425deSJoe Perches my $func = $1; 5839100425deSJoe Perches if (WARN('NEEDLESS_IF', 5840100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5841100425deSJoe Perches $fix) { 5842100425deSJoe Perches my $do_fix = 1; 5843100425deSJoe Perches my $leading_tabs = ""; 5844100425deSJoe Perches my $new_leading_tabs = ""; 5845100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5846100425deSJoe Perches $leading_tabs = $1; 5847100425deSJoe Perches } else { 5848100425deSJoe Perches $do_fix = 0; 5849100425deSJoe Perches } 5850100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5851100425deSJoe Perches $new_leading_tabs = $1; 5852100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5853100425deSJoe Perches $do_fix = 0; 5854100425deSJoe Perches } 5855100425deSJoe Perches } else { 5856100425deSJoe Perches $do_fix = 0; 5857100425deSJoe Perches } 5858100425deSJoe Perches if ($do_fix) { 5859100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5860100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5861100425deSJoe Perches } 5862100425deSJoe Perches } 58634c432a8fSGreg Kroah-Hartman } 58644c432a8fSGreg Kroah-Hartman } 5865f0a594c1SAndy Whitcroft 5866ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5867ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5868ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5869ebfdc409SJoe Perches (defined $1 || defined $3) && 5870ebfdc409SJoe Perches $linenr > 3) { 5871ebfdc409SJoe Perches my $testval = $2; 5872ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5873ebfdc409SJoe Perches 5874ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5875ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5876ebfdc409SJoe Perches 5877e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 5878e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 5879ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5880ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5881ebfdc409SJoe Perches } 5882ebfdc409SJoe Perches } 5883ebfdc409SJoe Perches 5884f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5885dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5886f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5887f78d98f6SJoe Perches my $level = $1; 5888f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5889f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5890f78d98f6SJoe Perches $fix) { 5891f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5892f78d98f6SJoe Perches } 5893f78d98f6SJoe Perches } 5894f78d98f6SJoe Perches 589545c55e92SJoe Perches# check for logging continuations 589645c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 589745c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 589845c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 589945c55e92SJoe Perches } 590045c55e92SJoe Perches 5901abb08a53SJoe Perches# check for mask then right shift without a parentheses 59025b57980dSJoe Perches if ($perl_version_ok && 5903abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5904abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5905abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5906abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5907abb08a53SJoe Perches } 5908abb08a53SJoe Perches 5909b75ac618SJoe Perches# check for pointer comparisons to NULL 59105b57980dSJoe Perches if ($perl_version_ok) { 5911b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5912b75ac618SJoe Perches my $val = $1; 5913b75ac618SJoe Perches my $equal = "!"; 5914b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5915b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5916b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5917b75ac618SJoe Perches $fix) { 5918b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5919b75ac618SJoe Perches } 5920b75ac618SJoe Perches } 5921b75ac618SJoe Perches } 5922b75ac618SJoe Perches 59238716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 59248716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 59258716de38SJoe Perches my $attr = $1; 59268716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 59278716de38SJoe Perches my $ptr = $1; 59288716de38SJoe Perches my $var = $2; 59298716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 59308716de38SJoe Perches ERROR("MISPLACED_INIT", 59318716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 59328716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 59338716de38SJoe Perches WARN("MISPLACED_INIT", 59348716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 59358716de38SJoe Perches $fix) { 5936194f66fcSJoe 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; 59378716de38SJoe Perches } 59388716de38SJoe Perches } 59398716de38SJoe Perches } 59408716de38SJoe Perches 5941e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5942e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5943e970b884SJoe Perches my $attr = $1; 5944e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5945e970b884SJoe Perches my $attr_prefix = $1; 5946e970b884SJoe Perches my $attr_type = $2; 5947e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5948e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5949e970b884SJoe Perches $fix) { 5950194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5951e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5952e970b884SJoe Perches } 5953e970b884SJoe Perches } 5954e970b884SJoe Perches 5955e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5956e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5957e970b884SJoe Perches my $attr = $1; 5958e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5959e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5960e970b884SJoe Perches $fix) { 5961194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5962e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5963e970b884SJoe Perches $lead = rtrim($1); 5964e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5965e970b884SJoe Perches $lead = "${lead}const "; 5966194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5967e970b884SJoe Perches } 5968e970b884SJoe Perches } 5969e970b884SJoe Perches 5970c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5971c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5972c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5973c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5974c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5975c17893c7SJoe Perches $fix) { 5976c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5977c17893c7SJoe Perches } 5978c17893c7SJoe Perches } 5979c17893c7SJoe Perches 5980fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5981fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5982fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5983fbdb8138SJoe Perches my $constant_func = $1; 5984fbdb8138SJoe Perches my $func = $constant_func; 5985fbdb8138SJoe Perches $func =~ s/^__constant_//; 5986fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5987fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5988fbdb8138SJoe Perches $fix) { 5989194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5990fbdb8138SJoe Perches } 5991fbdb8138SJoe Perches } 5992fbdb8138SJoe Perches 59931a15a250SPatrick Pannuto# prefer usleep_range over udelay 599437581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 599543c1d77cSJoe Perches my $delay = $1; 59961a15a250SPatrick Pannuto # ignore udelay's < 10, however 599743c1d77cSJoe Perches if (! ($delay < 10) ) { 5998000d1cc1SJoe Perches CHK("USLEEP_RANGE", 5999458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 600043c1d77cSJoe Perches } 600143c1d77cSJoe Perches if ($delay > 2000) { 600243c1d77cSJoe Perches WARN("LONG_UDELAY", 600343c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 60041a15a250SPatrick Pannuto } 60051a15a250SPatrick Pannuto } 60061a15a250SPatrick Pannuto 600709ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 600809ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 600909ef8725SPatrick Pannuto if ($1 < 20) { 6010000d1cc1SJoe Perches WARN("MSLEEP", 6011458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 601209ef8725SPatrick Pannuto } 601309ef8725SPatrick Pannuto } 601409ef8725SPatrick Pannuto 601536ec1939SJoe Perches# check for comparisons of jiffies 601636ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 601736ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 601836ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 601936ec1939SJoe Perches } 602036ec1939SJoe Perches 60219d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 60229d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 60239d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 60249d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 60259d7a34a5SJoe Perches } 60269d7a34a5SJoe Perches 602700df344fSAndy Whitcroft# warn about #ifdefs in C files 6028c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 602900df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 603000df344fSAndy Whitcroft# print "$herecurr"; 603100df344fSAndy Whitcroft# $clean = 0; 603200df344fSAndy Whitcroft# } 603300df344fSAndy Whitcroft 603422f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 6035c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 60363705ce5bSJoe Perches if (ERROR("SPACING", 60373705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 60383705ce5bSJoe Perches $fix) { 6039194f66fcSJoe Perches $fixed[$fixlinenr] =~ 60403705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 60413705ce5bSJoe Perches } 60423705ce5bSJoe Perches 604322f2a2efSAndy Whitcroft } 604422f2a2efSAndy Whitcroft 60454a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 6046171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 6047171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 60484a0df2efSAndy Whitcroft my $which = $1; 60494a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6050000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 6051000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 60524a0df2efSAndy Whitcroft } 60534a0df2efSAndy Whitcroft } 60544a0df2efSAndy Whitcroft# check for memory barriers without a comment. 6055402c2553SMichael S. Tsirkin 6056402c2553SMichael S. Tsirkin my $barriers = qr{ 6057402c2553SMichael S. Tsirkin mb| 6058402c2553SMichael S. Tsirkin rmb| 6059ad83ec6cSWill Deacon wmb 6060402c2553SMichael S. Tsirkin }x; 6061402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 6062402c2553SMichael S. Tsirkin mb__before_atomic| 6063402c2553SMichael S. Tsirkin mb__after_atomic| 6064402c2553SMichael S. Tsirkin store_release| 6065402c2553SMichael S. Tsirkin load_acquire| 6066402c2553SMichael S. Tsirkin store_mb| 6067402c2553SMichael S. Tsirkin (?:$barriers) 6068402c2553SMichael S. Tsirkin }x; 6069402c2553SMichael S. Tsirkin my $all_barriers = qr{ 6070402c2553SMichael S. Tsirkin (?:$barriers)| 607143e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 607243e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 6073402c2553SMichael S. Tsirkin }x; 6074402c2553SMichael S. Tsirkin 6075402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 60764a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 6077c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 6078000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 60794a0df2efSAndy Whitcroft } 60804a0df2efSAndy Whitcroft } 60813ad81779SPaul E. McKenney 6082f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 6083f4073b0fSMichael S. Tsirkin 6084f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 6085f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 6086f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 6087f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 6088f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 6089f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 6090f4073b0fSMichael S. Tsirkin } 6091f4073b0fSMichael S. Tsirkin 6092cb426e99SJoe Perches# check for waitqueue_active without a comment. 6093cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 6094cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 6095cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 6096cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 6097cb426e99SJoe Perches } 6098cb426e99SJoe Perches } 60993ad81779SPaul E. McKenney 61005099a722SMarco Elver# check for data_race without a comment. 61015099a722SMarco Elver if ($line =~ /\bdata_race\s*\(/) { 61025099a722SMarco Elver if (!ctx_has_comment($first_line, $linenr)) { 61035099a722SMarco Elver WARN("DATA_RACE", 61045099a722SMarco Elver "data_race without comment\n" . $herecurr); 61055099a722SMarco Elver } 61065099a722SMarco Elver } 61075099a722SMarco Elver 61084a0df2efSAndy Whitcroft# check of hardware specific defines 6109c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 6110000d1cc1SJoe Perches CHK("ARCH_DEFINES", 6111000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 61120a920b5bSAndy Whitcroft } 6113653d4876SAndy Whitcroft 6114596ed45bSJoe Perches# check that the storage class is not after a type 6115596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 6116000d1cc1SJoe Perches WARN("STORAGE_CLASS", 6117596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 6118596ed45bSJoe Perches } 6119596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 6120596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 6121596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 6122596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 6123596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 6124596ed45bSJoe Perches WARN("STORAGE_CLASS", 6125596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 6126d4977c78STobias Klauser } 6127d4977c78STobias Klauser 6128de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 6129de7d4f0eSAndy Whitcroft# storage class and type. 61309c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 61319c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 6132000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 6133000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 6134de7d4f0eSAndy Whitcroft } 6135de7d4f0eSAndy Whitcroft 61368905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 61372b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61382b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 6139d5e616fcSJoe Perches if (WARN("INLINE", 6140d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 6141d5e616fcSJoe Perches $fix) { 6142194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 6143d5e616fcSJoe Perches 6144d5e616fcSJoe Perches } 61458905a67cSAndy Whitcroft } 61468905a67cSAndy Whitcroft 61473d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 61482b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61492b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 6150000d1cc1SJoe Perches WARN("PREFER_PACKED", 6151000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 61523d130fd0SJoe Perches } 61533d130fd0SJoe Perches 615439b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 61552b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61562b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 6157000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 6158000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 615939b7e287SJoe Perches } 616039b7e287SJoe Perches 6161462811d9SJoe Perches# Check for __attribute__ section, prefer __section 6162462811d9SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6163462811d9SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { 6164462811d9SJoe Perches my $old = substr($rawline, $-[1], $+[1] - $-[1]); 6165462811d9SJoe Perches my $new = substr($old, 1, -1); 6166462811d9SJoe Perches if (WARN("PREFER_SECTION", 6167462811d9SJoe Perches "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && 6168462811d9SJoe Perches $fix) { 6169462811d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; 6170462811d9SJoe Perches } 6171462811d9SJoe Perches } 6172462811d9SJoe Perches 61735f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 61742b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61752b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 6176d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 6177d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 6178d5e616fcSJoe Perches $fix) { 6179194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 6180d5e616fcSJoe Perches 6181d5e616fcSJoe Perches } 61825f14d3bdSJoe Perches } 61835f14d3bdSJoe Perches 61846061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 61852b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 61862b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 6187d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 6188d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 6189d5e616fcSJoe Perches $fix) { 6190194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 6191d5e616fcSJoe Perches } 61926061d949SJoe Perches } 61936061d949SJoe Perches 6194619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 61955b57980dSJoe Perches if ($perl_version_ok && 6196619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 6197619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6198619a908aSJoe Perches $line =~ /\b__weak\b/)) { 6199619a908aSJoe Perches ERROR("WEAK_DECLARATION", 6200619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 6201619a908aSJoe Perches } 6202619a908aSJoe Perches 6203fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 6204e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6205fd39f904STomas Winkler $realfile !~ m@\btools/@ && 6206e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6207e6176fa4SJoe Perches my $type = $1; 6208e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 6209e6176fa4SJoe Perches $type = $1; 6210e6176fa4SJoe Perches my $kernel_type = 'u'; 6211e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 6212e6176fa4SJoe Perches $type =~ /(\d+)/; 6213e6176fa4SJoe Perches $kernel_type .= $1; 6214e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 6215e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6216e6176fa4SJoe Perches $fix) { 6217e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6218e6176fa4SJoe Perches } 6219e6176fa4SJoe Perches } 6220e6176fa4SJoe Perches } 6221e6176fa4SJoe Perches 6222938224b5SJoe Perches# check for cast of C90 native int or longer types constants 6223938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6224938224b5SJoe Perches my $cast = $1; 6225938224b5SJoe Perches my $const = $2; 6226938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 6227938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 6228938224b5SJoe Perches $fix) { 6229938224b5SJoe Perches my $suffix = ""; 6230938224b5SJoe Perches my $newconst = $const; 6231938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 6232938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6233938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6234938224b5SJoe Perches $suffix .= 'LL'; 6235938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6236938224b5SJoe Perches $suffix .= 'L'; 6237938224b5SJoe Perches } 6238938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6239938224b5SJoe Perches } 6240938224b5SJoe Perches } 6241938224b5SJoe Perches 62428f53a9b8SJoe Perches# check for sizeof(&) 62438f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6244000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6245000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 62468f53a9b8SJoe Perches } 62478f53a9b8SJoe Perches 624866c80b60SJoe Perches# check for sizeof without parenthesis 624966c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6250d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6251d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6252d5e616fcSJoe Perches $fix) { 6253194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6254d5e616fcSJoe Perches } 625566c80b60SJoe Perches } 625666c80b60SJoe Perches 625788982feaSJoe Perches# check for struct spinlock declarations 625888982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 625988982feaSJoe Perches WARN("USE_SPINLOCK_T", 626088982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 626188982feaSJoe Perches } 626288982feaSJoe Perches 6263a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 626406668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6265a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6266caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6267caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6268d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6269d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6270d5e616fcSJoe Perches $fix) { 6271194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6272d5e616fcSJoe Perches } 6273a6962d72SJoe Perches } 6274a6962d72SJoe Perches } 6275a6962d72SJoe Perches 62760b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 62775b57980dSJoe Perches if ($perl_version_ok && 62780b523769SJoe Perches defined $stat && 62790b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 62800b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6281e3c6bc95STobin C. Harding my $stat_real; 6282e3c6bc95STobin C. Harding 62830b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 62840b523769SJoe Perches $lc = $lc + $linenr; 62850b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6286ffe07513SJoe Perches my $specifier; 6287ffe07513SJoe Perches my $extension; 62883bd32d6aSSakari Ailus my $qualifier; 6289ffe07513SJoe Perches my $bad_specifier = ""; 62900b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 62910b523769SJoe Perches $fmt =~ s/%%//g; 6292e3c6bc95STobin C. Harding 62933bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6294e3c6bc95STobin C. Harding $specifier = $1; 6295e3c6bc95STobin C. Harding $extension = $2; 62963bd32d6aSSakari Ailus $qualifier = $3; 6297361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 62983bd32d6aSSakari Ailus ($extension eq "f" && 62993bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6300e3c6bc95STobin C. Harding $bad_specifier = $specifier; 63010b523769SJoe Perches last; 63020b523769SJoe Perches } 6303e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6304e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6305e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 63060b523769SJoe Perches } 6307e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6308e3c6bc95STobin 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"); 6309e3c6bc95STobin C. Harding } 6310e3c6bc95STobin C. Harding } 6311e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 63122a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 63131df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 63141df7338aSSergey Senozhatsky my $use = ""; 6315e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 63161df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6317e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 63181df7338aSSergey Senozhatsky } 63192a9f9d85STobin C. Harding 63200b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6321e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6322e3c6bc95STobin C. Harding } 63230b523769SJoe Perches } 63240b523769SJoe Perches } 63250b523769SJoe Perches 6326554e165cSAndy Whitcroft# Check for misused memsets 63275b57980dSJoe Perches if ($perl_version_ok && 6328d1fe9c09SJoe Perches defined $stat && 63299e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6330554e165cSAndy Whitcroft 6331d7c76ba7SJoe Perches my $ms_addr = $2; 6332d1fe9c09SJoe Perches my $ms_val = $7; 6333d1fe9c09SJoe Perches my $ms_size = $12; 6334d7c76ba7SJoe Perches 6335554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6336554e165cSAndy Whitcroft ERROR("MEMSET", 6337d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6338554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6339554e165cSAndy Whitcroft WARN("MEMSET", 6340d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6341d7c76ba7SJoe Perches } 6342d7c76ba7SJoe Perches } 6343d7c76ba7SJoe Perches 634498a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 63455b57980dSJoe Perches# if ($perl_version_ok && 6346f333195dSJoe Perches# defined $stat && 6347f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6348f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6349f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6350f333195dSJoe Perches# $fix) { 6351f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6352f333195dSJoe Perches# } 6353f333195dSJoe Perches# } 635498a9bba5SJoe Perches 6355b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 63565b57980dSJoe Perches# if ($perl_version_ok && 6357f333195dSJoe Perches# defined $stat && 6358f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6359f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6360f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6361f333195dSJoe Perches# } 6362b6117d17SMateusz Kulikowski 63638617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 63648617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 63655b57980dSJoe Perches# if ($perl_version_ok && 6366f333195dSJoe Perches# defined $stat && 6367f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6368f333195dSJoe Perches# 6369f333195dSJoe Perches# my $ms_val = $7; 6370f333195dSJoe Perches# 6371f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6372f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6373f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6374f333195dSJoe Perches# $fix) { 6375f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6376f333195dSJoe Perches# } 6377f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6378f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6379f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6380f333195dSJoe Perches# $fix) { 6381f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6382f333195dSJoe Perches# } 6383f333195dSJoe Perches# } 6384f333195dSJoe Perches# } 63858617cd09SMateusz Kulikowski 6386d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 63875b57980dSJoe Perches if ($perl_version_ok && 6388d1fe9c09SJoe Perches defined $stat && 6389d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6390d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6391d7c76ba7SJoe Perches my $call = $1; 6392d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6393d7c76ba7SJoe Perches my $arg1 = $3; 6394d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6395d1fe9c09SJoe Perches my $arg2 = $8; 6396d7c76ba7SJoe Perches my $cast; 6397d7c76ba7SJoe Perches 6398d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6399d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6400d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6401d7c76ba7SJoe Perches $cast = $cast1; 6402d7c76ba7SJoe Perches } else { 6403d7c76ba7SJoe Perches $cast = $cast2; 6404d7c76ba7SJoe Perches } 6405d7c76ba7SJoe Perches WARN("MINMAX", 6406d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6407554e165cSAndy Whitcroft } 6408554e165cSAndy Whitcroft } 6409554e165cSAndy Whitcroft 64104a273195SJoe Perches# check usleep_range arguments 64115b57980dSJoe Perches if ($perl_version_ok && 64124a273195SJoe Perches defined $stat && 64134a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 64144a273195SJoe Perches my $min = $1; 64154a273195SJoe Perches my $max = $7; 64164a273195SJoe Perches if ($min eq $max) { 64174a273195SJoe Perches WARN("USLEEP_RANGE", 6418458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 64194a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 64204a273195SJoe Perches $min > $max) { 64214a273195SJoe Perches WARN("USLEEP_RANGE", 6422458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 64234a273195SJoe Perches } 64244a273195SJoe Perches } 64254a273195SJoe Perches 6426823b794cSJoe Perches# check for naked sscanf 64275b57980dSJoe Perches if ($perl_version_ok && 6428823b794cSJoe Perches defined $stat && 64296c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6430823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6431823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6432823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6433823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6434823b794cSJoe Perches $lc = $lc + $linenr; 64352a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6436823b794cSJoe Perches WARN("NAKED_SSCANF", 6437823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6438823b794cSJoe Perches } 6439823b794cSJoe Perches 6440afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 64415b57980dSJoe Perches if ($perl_version_ok && 6442afc819abSJoe Perches defined $stat && 6443afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6444afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6445afc819abSJoe Perches $lc = $lc + $linenr; 64462a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6447afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6448afc819abSJoe Perches my $format = $6; 6449afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6450afc819abSJoe Perches if ($count == 1 && 6451afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6452afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6453afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6454afc819abSJoe Perches } 6455afc819abSJoe Perches } 6456afc819abSJoe Perches } 6457afc819abSJoe Perches 645870dc8a48SJoe Perches# check for new externs in .h files. 645970dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 646070dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6461d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 646270dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 646370dc8a48SJoe Perches $fix) { 6464194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 646570dc8a48SJoe Perches } 646670dc8a48SJoe Perches } 646770dc8a48SJoe Perches 6468de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6469171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6470c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6471171ae1a4SAndy Whitcroft { 6472c45dcabdSAndy Whitcroft my $function_name = $1; 6473c45dcabdSAndy Whitcroft my $paren_space = $2; 6474171ae1a4SAndy Whitcroft 6475171ae1a4SAndy Whitcroft my $s = $stat; 6476171ae1a4SAndy Whitcroft if (defined $cond) { 6477171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6478171ae1a4SAndy Whitcroft } 6479d8b44b58SKees Cook if ($s =~ /^\s*;/) 6480c45dcabdSAndy Whitcroft { 6481000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6482000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6483de7d4f0eSAndy Whitcroft } 6484de7d4f0eSAndy Whitcroft 6485171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6486000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6487000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6488171ae1a4SAndy Whitcroft } 64899c9ba34eSAndy Whitcroft 64909c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 64919c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 64929c9ba34eSAndy Whitcroft { 6493000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6494000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6495171ae1a4SAndy Whitcroft } 6496171ae1a4SAndy Whitcroft 6497a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6498a0ad7596SJoe Perches if (defined $stat && 6499d8b44b58SKees Cook $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6500d8b44b58SKees Cook $1 ne "void") { 6501d8b44b58SKees Cook my $args = trim($1); 6502ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6503ca0d8929SJoe Perches my $arg = trim($1); 6504d8b44b58SKees Cook if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6505ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6506ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6507ca0d8929SJoe Perches } 6508ca0d8929SJoe Perches } 6509ca0d8929SJoe Perches } 6510ca0d8929SJoe Perches 6511a0ad7596SJoe Perches# check for function definitions 65125b57980dSJoe Perches if ($perl_version_ok && 6513a0ad7596SJoe Perches defined $stat && 6514a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6515a0ad7596SJoe Perches $context_function = $1; 6516a0ad7596SJoe Perches 6517a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6518a0ad7596SJoe Perches my $ok = 0; 6519a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6520a0ad7596SJoe Perches my $herectx = $here . "\n"; 6521a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6522a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6523a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6524a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6525a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6526a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6527a0ad7596SJoe Perches } 6528a0ad7596SJoe Perches if (!$ok) { 6529a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6530a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6531a0ad7596SJoe Perches } 6532a0ad7596SJoe Perches } 6533a0ad7596SJoe Perches 6534de7d4f0eSAndy Whitcroft# checks for new __setup's 6535de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6536de7d4f0eSAndy Whitcroft my $name = $1; 6537de7d4f0eSAndy Whitcroft 6538de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6539000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 65402581ac7cSTim Froidcoeur "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); 6541de7d4f0eSAndy Whitcroft } 6542653d4876SAndy Whitcroft } 65439c0ca6f9SAndy Whitcroft 6544e29a70f1SJoe Perches# check for pointless casting of alloc functions 6545e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6546000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6547000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 65489c0ca6f9SAndy Whitcroft } 654913214adfSAndy Whitcroft 6550a640d25cSJoe Perches# alloc style 6551a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 65525b57980dSJoe Perches if ($perl_version_ok && 6553e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6554a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6555a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6556a640d25cSJoe Perches } 6557a640d25cSJoe Perches 655860a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 65595b57980dSJoe Perches if ($perl_version_ok && 65601b4a2ed4SJoe Perches defined $stat && 65611b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 656260a55369SJoe Perches my $oldfunc = $3; 656360a55369SJoe Perches my $a1 = $4; 656460a55369SJoe Perches my $a2 = $10; 656560a55369SJoe Perches my $newfunc = "kmalloc_array"; 656660a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 656760a55369SJoe Perches my $r1 = $a1; 656860a55369SJoe Perches my $r2 = $a2; 656960a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 657060a55369SJoe Perches $r1 = $a2; 657160a55369SJoe Perches $r2 = $a1; 657260a55369SJoe Perches } 6573e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6574e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 65751b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6576e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6577e3d95a2aSTobin C. Harding 6578e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 65791b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 65801b4a2ed4SJoe Perches $cnt == 1 && 6581e367455aSJoe Perches $fix) { 6582194f66fcSJoe 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; 658360a55369SJoe Perches } 658460a55369SJoe Perches } 658560a55369SJoe Perches } 658660a55369SJoe Perches 6587972fdea2SJoe Perches# check for krealloc arg reuse 65885b57980dSJoe Perches if ($perl_version_ok && 65894cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 65904cab63ceSJoe Perches $1 eq $3) { 6591972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6592972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6593972fdea2SJoe Perches } 6594972fdea2SJoe Perches 65955ce59ae0SJoe Perches# check for alloc argument mismatch 65965ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 65975ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 65985ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 65995ce59ae0SJoe Perches } 66005ce59ae0SJoe Perches 6601caf2a54fSJoe Perches# check for multiple semicolons 6602caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6603d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6604d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6605d5e616fcSJoe Perches $fix) { 6606194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6607d5e616fcSJoe Perches } 6608d1e2ad07SJoe Perches } 6609d1e2ad07SJoe Perches 6610cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6611cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6612cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 66130ab90191SJoe Perches my $ull = ""; 66140ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 66150ab90191SJoe Perches if (CHK("BIT_MACRO", 66160ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 66170ab90191SJoe Perches $fix) { 66180ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 66190ab90191SJoe Perches } 66200ab90191SJoe Perches } 66210ab90191SJoe Perches 662250161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) 66233e89ad85SJerome Forissier if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { 662450161266SJoe Perches WARN("IS_ENABLED_CONFIG", 66253e89ad85SJerome Forissier "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); 662650161266SJoe Perches } 662750161266SJoe Perches 66282d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 66293e89ad85SJerome 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*$/) { 66302d632745SJoe Perches my $config = $1; 66312d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 66323e89ad85SJerome Forissier "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && 66332d632745SJoe Perches $fix) { 66342d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 66352d632745SJoe Perches } 66362d632745SJoe Perches } 66372d632745SJoe Perches 6638f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 6639f36d3eb8SJoe Perches my @fallthroughs = ( 6640f36d3eb8SJoe Perches 'fallthrough', 6641f36d3eb8SJoe Perches '@fallthrough@', 6642f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 6643f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 6644f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 6645f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6646f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6647f36d3eb8SJoe Perches ); 6648f36d3eb8SJoe Perches if ($raw_comment ne '') { 6649f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 6650f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 6651f36d3eb8SJoe Perches my $msg_level = \&WARN; 6652f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 6653f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 6654f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 6655f36d3eb8SJoe Perches last; 6656f36d3eb8SJoe Perches } 6657f36d3eb8SJoe Perches } 6658f36d3eb8SJoe Perches } 6659f36d3eb8SJoe Perches 6660d1e2ad07SJoe Perches# check for switch/default statements without a break; 66615b57980dSJoe Perches if ($perl_version_ok && 6662d1e2ad07SJoe Perches defined $stat && 6663d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6664d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6665e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6666e3d95a2aSTobin C. Harding 6667d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6668d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6669caf2a54fSJoe Perches } 6670caf2a54fSJoe Perches 667113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6672d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6673d5e616fcSJoe Perches if (WARN("USE_FUNC", 6674d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6675d5e616fcSJoe Perches $fix) { 6676194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6677d5e616fcSJoe Perches } 667813214adfSAndy Whitcroft } 6679773647a0SAndy Whitcroft 668062ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 668162ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 668262ec818fSJoe Perches ERROR("DATE_TIME", 668362ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 668462ec818fSJoe Perches } 668562ec818fSJoe Perches 66862c92488aSJoe Perches# check for use of yield() 66872c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 66882c92488aSJoe Perches WARN("YIELD", 66892c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 66902c92488aSJoe Perches } 66912c92488aSJoe Perches 6692179f8f40SJoe Perches# check for comparisons against true and false 6693179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6694179f8f40SJoe Perches my $lead = $1; 6695179f8f40SJoe Perches my $arg = $2; 6696179f8f40SJoe Perches my $test = $3; 6697179f8f40SJoe Perches my $otype = $4; 6698179f8f40SJoe Perches my $trail = $5; 6699179f8f40SJoe Perches my $op = "!"; 6700179f8f40SJoe Perches 6701179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6702179f8f40SJoe Perches 6703179f8f40SJoe Perches my $type = lc($otype); 6704179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6705179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6706179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6707179f8f40SJoe Perches $op = ""; 6708179f8f40SJoe Perches } 6709179f8f40SJoe Perches 6710179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6711179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6712179f8f40SJoe Perches 6713179f8f40SJoe Perches## maybe suggesting a correct construct would better 6714179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6715179f8f40SJoe Perches 6716179f8f40SJoe Perches } 6717179f8f40SJoe Perches } 6718179f8f40SJoe Perches 67194882720bSThomas Gleixner# check for semaphores initialized locked 67204882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6721000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6722000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6723773647a0SAndy Whitcroft } 67246712d858SJoe Perches 672567d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 672667d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6727000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 672867d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6729773647a0SAndy Whitcroft } 67306712d858SJoe Perches 6731ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6732f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6733000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6734ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6735f3db6639SMichael Ellerman } 67366712d858SJoe Perches 67373d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 67383d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 67393d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 67403d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 67413d709ab5SPaul E. McKenney } 67423d709ab5SPaul E. McKenney 67439189c7e7SJoe Perches# check for deprecated apis 67449189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 67459189c7e7SJoe Perches my $deprecated_api = $1; 67469189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 67479189c7e7SJoe Perches WARN("DEPRECATED_API", 67489189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 67499189c7e7SJoe Perches } 67509189c7e7SJoe Perches 67510f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6752d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 6753ced69da1SQuentin Monnet if (defined($const_structs) && 6754ced69da1SQuentin Monnet $line !~ /\bconst\b/ && 6755d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6756000d1cc1SJoe Perches WARN("CONST_STRUCT", 6757d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 67582b6db5cbSAndy Whitcroft } 6759773647a0SAndy Whitcroft 6760773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6761773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6762773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6763c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6764c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6765171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6766171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6767171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6768773647a0SAndy Whitcroft { 6769000d1cc1SJoe Perches WARN("NR_CPUS", 6770000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6771773647a0SAndy Whitcroft } 67729c9ba34eSAndy Whitcroft 677352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 677452ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 677552ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 677652ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 677752ea8506SJoe Perches } 677852ea8506SJoe Perches 6779acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 67805b57980dSJoe Perches if ($perl_version_ok && 6781acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6782acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6783acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6784acd9362cSJoe Perches } 6785acd9362cSJoe Perches 6786de3f186fSDenis Efremov# nested likely/unlikely calls 6787de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 6788de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 6789de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 6790de3f186fSDenis Efremov } 6791de3f186fSDenis Efremov 6792691d77b6SAndy Whitcroft# whine mightly about in_atomic 6793691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6794691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6795000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6796000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6797f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6798000d1cc1SJoe Perches WARN("IN_ATOMIC", 6799000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6800691d77b6SAndy Whitcroft } 6801691d77b6SAndy Whitcroft } 68021704f47bSPeter Zijlstra 68030f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 68040f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 68050f5225b0SPeter Zijlstra ERROR("LOCKING", 68060f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 68070f5225b0SPeter Zijlstra } 68080f5225b0SPeter Zijlstra 68091704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 68101704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 68111704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 68121704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 68131704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 68141704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6815000d1cc1SJoe Perches ERROR("LOCKDEP", 6816000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 68171704f47bSPeter Zijlstra } 68181704f47bSPeter Zijlstra } 681988f8831cSDave Jones 6820b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6821b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6822000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6823000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 682488f8831cSDave Jones } 68252435880fSJoe Perches 682600180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 682700180468SJoe Perches# and whether or not function naming is typical and if 682800180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 68295b57980dSJoe Perches if ($perl_version_ok && 683000180468SJoe Perches defined $stat && 683100180468SJoe 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*\)/) { 683200180468SJoe Perches my $var = $1; 683300180468SJoe Perches my $perms = $2; 683400180468SJoe Perches my $show = $3; 683500180468SJoe Perches my $store = $4; 683600180468SJoe Perches my $octal_perms = perms_to_octal($perms); 683700180468SJoe Perches if ($show =~ /^${var}_show$/ && 683800180468SJoe Perches $store =~ /^${var}_store$/ && 683900180468SJoe Perches $octal_perms eq "0644") { 684000180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 684100180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 684200180468SJoe Perches $fix) { 684300180468SJoe 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})/; 684400180468SJoe Perches } 684500180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 684600180468SJoe Perches $store =~ /^NULL$/ && 684700180468SJoe Perches $octal_perms eq "0444") { 684800180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 684900180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 685000180468SJoe Perches $fix) { 685100180468SJoe 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})/; 685200180468SJoe Perches } 685300180468SJoe Perches } elsif ($show =~ /^NULL$/ && 685400180468SJoe Perches $store =~ /^${var}_store$/ && 685500180468SJoe Perches $octal_perms eq "0200") { 685600180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 685700180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 685800180468SJoe Perches $fix) { 685900180468SJoe 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})/; 686000180468SJoe Perches } 686100180468SJoe Perches } elsif ($octal_perms eq "0644" || 686200180468SJoe Perches $octal_perms eq "0444" || 686300180468SJoe Perches $octal_perms eq "0200") { 686400180468SJoe Perches my $newshow = "$show"; 686500180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 686600180468SJoe Perches my $newstore = $store; 686700180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 686800180468SJoe Perches my $rename = ""; 686900180468SJoe Perches if ($show ne $newshow) { 687000180468SJoe Perches $rename .= " '$show' to '$newshow'"; 687100180468SJoe Perches } 687200180468SJoe Perches if ($store ne $newstore) { 687300180468SJoe Perches $rename .= " '$store' to '$newstore'"; 687400180468SJoe Perches } 687500180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 687600180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 687700180468SJoe Perches } else { 687800180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 687900180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 688000180468SJoe Perches } 688100180468SJoe Perches } 688200180468SJoe Perches 6883515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6884515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 688573121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 688673121534SJoe Perches# specific definition of not visible in sysfs. 688773121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 688873121534SJoe Perches# use the default permissions 68895b57980dSJoe Perches if ($perl_version_ok && 6890459cf0aeSJoe Perches defined $stat && 6891515a235eSJoe Perches $line =~ /$mode_perms_search/) { 68922435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 68932435880fSJoe Perches my $func = $entry->[0]; 68942435880fSJoe Perches my $arg_pos = $entry->[1]; 68952435880fSJoe Perches 6896459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6897459cf0aeSJoe Perches $lc = $lc + $linenr; 68982a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6899459cf0aeSJoe Perches 69002435880fSJoe Perches my $skip_args = ""; 69012435880fSJoe Perches if ($arg_pos > 1) { 69022435880fSJoe Perches $arg_pos--; 69032435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 69042435880fSJoe Perches } 6905f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6906459cf0aeSJoe Perches if ($stat =~ /$test/) { 69072435880fSJoe Perches my $val = $1; 69082435880fSJoe Perches $val = $6 if ($skip_args ne ""); 690973121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 691073121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 691173121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 69122435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6913459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6914f90774e1SJoe Perches } 6915f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6916c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6917459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 69182435880fSJoe Perches } 6919459cf0aeSJoe Perches } 6920459cf0aeSJoe Perches } 6921459cf0aeSJoe Perches } 6922459cf0aeSJoe Perches 6923459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6924bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 692500180468SJoe Perches my $oval = $1; 692600180468SJoe Perches my $octal = perms_to_octal($oval); 6927f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6928459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6929f90774e1SJoe Perches $fix) { 693000180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 69312435880fSJoe Perches } 693213214adfSAndy Whitcroft } 69335a6d20ceSBjorn Andersson 69345a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 69355a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 69365a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 69375a6d20ceSBjorn Andersson my $valid_licenses = qr{ 69385a6d20ceSBjorn Andersson GPL| 69395a6d20ceSBjorn Andersson GPL\ v2| 69405a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 69415a6d20ceSBjorn Andersson Dual\ BSD/GPL| 69425a6d20ceSBjorn Andersson Dual\ MIT/GPL| 69435a6d20ceSBjorn Andersson Dual\ MPL/GPL| 69445a6d20ceSBjorn Andersson Proprietary 69455a6d20ceSBjorn Andersson }x; 69465a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 69475a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 69485a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 69495a6d20ceSBjorn Andersson } 69505a6d20ceSBjorn Andersson } 69516a8d76cbSMatteo Croce 69526a8d76cbSMatteo Croce# check for sysctl duplicate constants 69536a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 69546a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 69556a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 69566a8d76cbSMatteo Croce } 6957515a235eSJoe Perches } 695813214adfSAndy Whitcroft 695913214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 696013214adfSAndy Whitcroft # so just keep quiet. 696113214adfSAndy Whitcroft if ($#rawlines == -1) { 696213214adfSAndy Whitcroft exit(0); 69630a920b5bSAndy Whitcroft } 69640a920b5bSAndy Whitcroft 69658905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 69668905a67cSAndy Whitcroft # things that appear to be patches. 69678905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 69688905a67cSAndy Whitcroft exit(0); 69698905a67cSAndy Whitcroft } 69708905a67cSAndy Whitcroft 69718905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 69728905a67cSAndy Whitcroft # just keep quiet. 69738905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 69748905a67cSAndy Whitcroft exit(0); 69758905a67cSAndy Whitcroft } 69768905a67cSAndy Whitcroft 6977a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6978000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6979000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 69800a920b5bSAndy Whitcroft } 6981cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6982cd261496SGeert Uytterhoeven if ($signoff == 0) { 6983000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6984000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 698548ca2d8aSDwaipayan Ray } elsif ($authorsignoff != 1) { 698648ca2d8aSDwaipayan Ray # authorsignoff values: 698748ca2d8aSDwaipayan Ray # 0 -> missing sign off 698848ca2d8aSDwaipayan Ray # 1 -> sign off identical 698948ca2d8aSDwaipayan Ray # 2 -> names and addresses match, comments mismatch 699048ca2d8aSDwaipayan Ray # 3 -> addresses match, names different 699148ca2d8aSDwaipayan Ray # 4 -> names match, addresses different 699248ca2d8aSDwaipayan Ray # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match 699348ca2d8aSDwaipayan Ray 699448ca2d8aSDwaipayan Ray my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; 699548ca2d8aSDwaipayan Ray 699648ca2d8aSDwaipayan Ray if ($authorsignoff == 0) { 699748ca2d8aSDwaipayan Ray ERROR("NO_AUTHOR_SIGN_OFF", 6998cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 699948ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 2) { 700048ca2d8aSDwaipayan Ray CHK("FROM_SIGN_OFF_MISMATCH", 700148ca2d8aSDwaipayan Ray "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); 700248ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 3) { 700348ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 700448ca2d8aSDwaipayan Ray "From:/Signed-off-by: email name mismatch: $sob_msg\n"); 700548ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 4) { 700648ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 700748ca2d8aSDwaipayan Ray "From:/Signed-off-by: email address mismatch: $sob_msg\n"); 700848ca2d8aSDwaipayan Ray } elsif ($authorsignoff == 5) { 700948ca2d8aSDwaipayan Ray WARN("FROM_SIGN_OFF_MISMATCH", 701048ca2d8aSDwaipayan Ray "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); 701148ca2d8aSDwaipayan Ray } 7012cd261496SGeert Uytterhoeven } 70130a920b5bSAndy Whitcroft } 70140a920b5bSAndy Whitcroft 7015f0a594c1SAndy Whitcroft print report_dump(); 701613214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 701713214adfSAndy Whitcroft print "$filename " if ($summary_file); 70186c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 70196c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 70206c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 70216c72ffaaSAndy Whitcroft } 70228905a67cSAndy Whitcroft 7023d2c0a235SAndy Whitcroft if ($quiet == 0) { 7024ef212196SJoe Perches # If there were any defects found and not already fixing them 7025ef212196SJoe Perches if (!$clean and !$fix) { 7026ef212196SJoe Perches print << "EOM" 7027ef212196SJoe Perches 7028ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 7029ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 7030ef212196SJoe PerchesEOM 7031ef212196SJoe Perches } 7032d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 7033d2c0a235SAndy Whitcroft # then suggest that. 7034d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 7035b0781216SMike Frysinger $rpt_cleaners = 0; 7036d8469f16SJoe Perches print << "EOM" 7037d8469f16SJoe Perches 7038d8469f16SJoe PerchesNOTE: Whitespace errors detected. 7039d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 7040d8469f16SJoe PerchesEOM 7041d2c0a235SAndy Whitcroft } 7042d2c0a235SAndy Whitcroft } 7043d2c0a235SAndy Whitcroft 7044d752fcc8SJoe Perches if ($clean == 0 && $fix && 7045d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 7046d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 70479624b8d6SJoe Perches my $newfile = $filename; 70489624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 70493705ce5bSJoe Perches my $linecount = 0; 70503705ce5bSJoe Perches my $f; 70513705ce5bSJoe Perches 7052d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 7053d752fcc8SJoe Perches 70543705ce5bSJoe Perches open($f, '>', $newfile) 70553705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 70563705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 70573705ce5bSJoe Perches $linecount++; 70583705ce5bSJoe Perches if ($file) { 70593705ce5bSJoe Perches if ($linecount > 3) { 70603705ce5bSJoe Perches $fixed_line =~ s/^\+//; 70613705ce5bSJoe Perches print $f $fixed_line . "\n"; 70623705ce5bSJoe Perches } 70633705ce5bSJoe Perches } else { 70643705ce5bSJoe Perches print $f $fixed_line . "\n"; 70653705ce5bSJoe Perches } 70663705ce5bSJoe Perches } 70673705ce5bSJoe Perches close($f); 70683705ce5bSJoe Perches 70693705ce5bSJoe Perches if (!$quiet) { 70703705ce5bSJoe Perches print << "EOM"; 7071d8469f16SJoe Perches 70723705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 70733705ce5bSJoe Perches 70743705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 70753705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 70763705ce5bSJoe Perches 70773705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 70783705ce5bSJoe PerchesNo warranties, expressed or implied... 70793705ce5bSJoe PerchesEOM 70803705ce5bSJoe Perches } 70813705ce5bSJoe Perches } 70823705ce5bSJoe Perches 7083d8469f16SJoe Perches if ($quiet == 0) { 7084d8469f16SJoe Perches print "\n"; 7085d8469f16SJoe Perches if ($clean == 1) { 7086d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 7087d8469f16SJoe Perches } else { 7088d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 70890a920b5bSAndy Whitcroft } 70900a920b5bSAndy Whitcroft } 70910a920b5bSAndy Whitcroft return $clean; 70920a920b5bSAndy Whitcroft} 7093