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; 46c2fdda0dSAndy Whitcroftmy %debug; 473445686aSJoe Perchesmy %camelcase = (); 4891bfe484SJoe Perchesmy %use_type = (); 4991bfe484SJoe Perchesmy @use = (); 5091bfe484SJoe Perchesmy %ignore_type = (); 51000d1cc1SJoe Perchesmy @ignore = (); 5277f5b10aSHannes Edermy $help = 0; 53000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 54bdc48fa1SJoe Perchesmy $max_line_length = 100; 55d62a201fSDave Hansenmy $ignore_perl_version = 0; 56d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5756193274SVadim Bendeburymy $min_conf_desc_length = 4; 5866b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 59ebfd7d62SJoe Perchesmy $codespell = 0; 60f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 61bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch"; 6275ad8c57SJerome Forissiermy $typedefsfile = ""; 63737c0767SJohn Brooksmy $color = "auto"; 6498005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE 65dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE 66dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git'; 67713a09deSAntonio Borneomy $tabsize = 8; 6877f5b10aSHannes Eder 6977f5b10aSHannes Edersub help { 7077f5b10aSHannes Eder my ($exitcode) = @_; 7177f5b10aSHannes Eder 7277f5b10aSHannes Eder print << "EOM"; 7377f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 7477f5b10aSHannes EderVersion: $V 7577f5b10aSHannes Eder 7677f5b10aSHannes EderOptions: 7777f5b10aSHannes Eder -q, --quiet quiet 7877f5b10aSHannes Eder --no-tree run without a kernel tree 7977f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 8077f5b10aSHannes Eder --patch treat FILE as patchfile (default) 8177f5b10aSHannes Eder --emacs emacs compile window format 8277f5b10aSHannes Eder --terse one line per report 8334d8815fSJoe Perches --showfile emit diffed file position, not input file position 844a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 854a593c34SDu, Changbin single git commit with: 864a593c34SDu, Changbin <rev> 874a593c34SDu, Changbin <rev>^ 884a593c34SDu, Changbin <rev>~n 894a593c34SDu, Changbin multiple git commits with: 904a593c34SDu, Changbin <rev1>..<rev2> 914a593c34SDu, Changbin <rev1>...<rev2> 924a593c34SDu, Changbin <rev>-<count> 934a593c34SDu, Changbin git merges are ignored 9477f5b10aSHannes Eder -f, --file treat FILE as regular source file 9577f5b10aSHannes Eder --subjective, --strict enable more subjective tests 963beb42ecSJoe Perches --list-types list the possible message types 9791bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 98000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 993beb42ecSJoe Perches --show-types show the specific message type in the output 100bdc48fa1SJoe Perches --max-line-length=n set the maximum line length, (default $max_line_length) 101bdc48fa1SJoe Perches if exceeded, warn on patches 102bdc48fa1SJoe Perches requires --strict for use with --file 10356193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 104bdc48fa1SJoe Perches --tab-size=n set the number of spaces for tab (default $tabsize) 10577f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10677f5b10aSHannes Eder --no-summary suppress the per-file summary 10777f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 10877f5b10aSHannes Eder --summary-file include the filename in summary 10977f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 11077f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 11177f5b10aSHannes Eder is all off) 11277f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 11377f5b10aSHannes Eder literally 1143705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1153705ce5bSJoe Perches If correctable single-line errors exist, create 1163705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1173705ce5bSJoe Perches with potential errors corrected to the preferred 1183705ce5bSJoe Perches checkpatch style 1199624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1209624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1219624b8d6SJoe Perches file. It's your fault if there's no backup or git 122d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 123d62a201fSDave Hansen runtime errors. 124ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 125f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 126ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 12775ad8c57SJerome Forissier --typedefsfile Read additional types from this file 128737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 129737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 13077f5b10aSHannes Eder -h, --help, --version display this help and exit 13177f5b10aSHannes Eder 13277f5b10aSHannes EderWhen FILE is - read standard input. 13377f5b10aSHannes EderEOM 13477f5b10aSHannes Eder 13577f5b10aSHannes Eder exit($exitcode); 13677f5b10aSHannes Eder} 13777f5b10aSHannes Eder 1383beb42ecSJoe Perchessub uniq { 1393beb42ecSJoe Perches my %seen; 1403beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1413beb42ecSJoe Perches} 1423beb42ecSJoe Perches 1433beb42ecSJoe Perchessub list_types { 1443beb42ecSJoe Perches my ($exitcode) = @_; 1453beb42ecSJoe Perches 1463beb42ecSJoe Perches my $count = 0; 1473beb42ecSJoe Perches 1483beb42ecSJoe Perches local $/ = undef; 1493beb42ecSJoe Perches 1503beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1513beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1523beb42ecSJoe Perches 1533beb42ecSJoe Perches my $text = <$script>; 1543beb42ecSJoe Perches close($script); 1553beb42ecSJoe Perches 1563beb42ecSJoe Perches my @types = (); 1570547fa58SJean Delvare # Also catch when type or level is passed through a variable 1580547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1593beb42ecSJoe Perches push (@types, $_); 1603beb42ecSJoe Perches } 1613beb42ecSJoe Perches @types = sort(uniq(@types)); 1623beb42ecSJoe Perches print("#\tMessage type\n\n"); 1633beb42ecSJoe Perches foreach my $type (@types) { 1643beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1653beb42ecSJoe Perches } 1663beb42ecSJoe Perches 1673beb42ecSJoe Perches exit($exitcode); 1683beb42ecSJoe Perches} 1693beb42ecSJoe Perches 170000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 171000d1cc1SJoe Perchesif (-f $conf) { 172000d1cc1SJoe Perches my @conf_args; 173000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 174000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 175000d1cc1SJoe Perches 176000d1cc1SJoe Perches while (<$conffile>) { 177000d1cc1SJoe Perches my $line = $_; 178000d1cc1SJoe Perches 179000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 180000d1cc1SJoe Perches $line =~ s/^\s*//g; 181000d1cc1SJoe Perches $line =~ s/\s+/ /g; 182000d1cc1SJoe Perches 183000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 184000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 185000d1cc1SJoe Perches 186000d1cc1SJoe Perches my @words = split(" ", $line); 187000d1cc1SJoe Perches foreach my $word (@words) { 188000d1cc1SJoe Perches last if ($word =~ m/^#/); 189000d1cc1SJoe Perches push (@conf_args, $word); 190000d1cc1SJoe Perches } 191000d1cc1SJoe Perches } 192000d1cc1SJoe Perches close($conffile); 193000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 194000d1cc1SJoe Perches} 195000d1cc1SJoe Perches 196737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 197737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 198737c0767SJohn Brooksforeach (@ARGV) { 199737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 200737c0767SJohn Brooks $_ = "--color=$color"; 201737c0767SJohn Brooks } 202737c0767SJohn Brooks} 203737c0767SJohn Brooks 2040a920b5bSAndy WhitcroftGetOptions( 2056c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2060a920b5bSAndy Whitcroft 'tree!' => \$tree, 2070a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2080a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2096c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2108905a67cSAndy Whitcroft 'terse!' => \$terse, 21134d8815fSJoe Perches 'showfile!' => \$showfile, 21277f5b10aSHannes Eder 'f|file!' => \$file, 2134a593c34SDu, Changbin 'g|git!' => \$git, 2146c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2156c72ffaaSAndy Whitcroft 'strict!' => \$check, 216000d1cc1SJoe Perches 'ignore=s' => \@ignore, 21791bfe484SJoe Perches 'types=s' => \@use, 218000d1cc1SJoe Perches 'show-types!' => \$show_types, 2193beb42ecSJoe Perches 'list-types!' => \$list_types, 2206cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 22156193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 222713a09deSAntonio Borneo 'tab-size=i' => \$tabsize, 2236c72ffaaSAndy Whitcroft 'root=s' => \$root, 2248905a67cSAndy Whitcroft 'summary!' => \$summary, 2258905a67cSAndy Whitcroft 'mailback!' => \$mailback, 22613214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2273705ce5bSJoe Perches 'fix!' => \$fix, 2289624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 229d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 230c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 231773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 232ebfd7d62SJoe Perches 'codespell!' => \$codespell, 233ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 23475ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 235737c0767SJohn Brooks 'color=s' => \$color, 236737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 237737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 23877f5b10aSHannes Eder 'h|help' => \$help, 23977f5b10aSHannes Eder 'version' => \$help 24077f5b10aSHannes Eder) or help(1); 24177f5b10aSHannes Eder 24277f5b10aSHannes Ederhelp(0) if ($help); 2430a920b5bSAndy Whitcroft 2443beb42ecSJoe Percheslist_types(0) if ($list_types); 2453beb42ecSJoe Perches 2469624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2472ac73b4fSJoe Perches$check_orig = $check; 2489624b8d6SJoe Perches 24932f30ca9SJoe Perchesdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); 25032f30ca9SJoe Perches 2510a920b5bSAndy Whitcroftmy $exit = 0; 2520a920b5bSAndy Whitcroft 2535b57980dSJoe Perchesmy $perl_version_ok = 1; 254d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2555b57980dSJoe Perches $perl_version_ok = 0; 256d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2575b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 258d62a201fSDave Hansen} 259d62a201fSDave Hansen 26045107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2610a920b5bSAndy Whitcroftif ($#ARGV < 0) { 26245107ff6SAllen Hubbe push(@ARGV, '-'); 2630a920b5bSAndy Whitcroft} 2640a920b5bSAndy Whitcroft 265737c0767SJohn Brooksif ($color =~ /^[01]$/) { 266737c0767SJohn Brooks $color = !$color; 267737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 268737c0767SJohn Brooks $color = 1; 269737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 270737c0767SJohn Brooks $color = 0; 271737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 272737c0767SJohn Brooks $color = (-t STDOUT); 273737c0767SJohn Brooks} else { 27432f30ca9SJoe Perches die "$P: Invalid color mode: $color\n"; 275737c0767SJohn Brooks} 276737c0767SJohn Brooks 277713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1 27832f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); 279713a09deSAntonio Borneo 28091bfe484SJoe Perchessub hash_save_array_words { 28191bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 28291bfe484SJoe Perches 28391bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 28491bfe484SJoe Perches foreach my $word (@array) { 285000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 286000d1cc1SJoe Perches $word =~ s/^\s*//g; 287000d1cc1SJoe Perches $word =~ s/\s+/ /g; 288000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 289000d1cc1SJoe Perches 290000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 291000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 292000d1cc1SJoe Perches 29391bfe484SJoe Perches $hashRef->{$word}++; 294000d1cc1SJoe Perches } 29591bfe484SJoe Perches} 29691bfe484SJoe Perches 29791bfe484SJoe Perchessub hash_show_words { 29891bfe484SJoe Perches my ($hashRef, $prefix) = @_; 29991bfe484SJoe Perches 3003c816e49SJoe Perches if (keys %$hashRef) { 301d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 30258cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 30391bfe484SJoe Perches print " $word"; 30491bfe484SJoe Perches } 305d8469f16SJoe Perches print "\n"; 30691bfe484SJoe Perches } 30791bfe484SJoe Perches} 30891bfe484SJoe Perches 30991bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 31091bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 311000d1cc1SJoe Perches 312c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 313c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3147429c690SAndy Whitcroftmy $dbg_type = 0; 315a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 316c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 31721caa13cSAndy Whitcroft ## no critic 31821caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 31921caa13cSAndy Whitcroft die "$@" if ($@); 320c2fdda0dSAndy Whitcroft} 321c2fdda0dSAndy Whitcroft 322d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 323d2c0a235SAndy Whitcroft 3248905a67cSAndy Whitcroftif ($terse) { 3258905a67cSAndy Whitcroft $emacs = 1; 3268905a67cSAndy Whitcroft $quiet++; 3278905a67cSAndy Whitcroft} 3288905a67cSAndy Whitcroft 3296c72ffaaSAndy Whitcroftif ($tree) { 3306c72ffaaSAndy Whitcroft if (defined $root) { 3316c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3326c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3336c72ffaaSAndy Whitcroft } 3346c72ffaaSAndy Whitcroft } else { 3356c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3366c72ffaaSAndy Whitcroft $root = '.'; 3376c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3386c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3396c72ffaaSAndy Whitcroft $root = $1; 3406c72ffaaSAndy Whitcroft } 3416c72ffaaSAndy Whitcroft } 3426c72ffaaSAndy Whitcroft 3436c72ffaaSAndy Whitcroft if (!defined $root) { 3440a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3450a920b5bSAndy Whitcroft exit(2); 3460a920b5bSAndy Whitcroft } 3476c72ffaaSAndy Whitcroft} 3486c72ffaaSAndy Whitcroft 3496c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3506c72ffaaSAndy Whitcroft 3512ceb532bSAndy Whitcroftour $Ident = qr{ 3522ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3532ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3542ceb532bSAndy Whitcroft }x; 3556c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3566c72ffaaSAndy Whitcroftour $Sparse = qr{ 3576c72ffaaSAndy Whitcroft __user| 3586c72ffaaSAndy Whitcroft __kernel| 3596c72ffaaSAndy Whitcroft __force| 3606c72ffaaSAndy Whitcroft __iomem| 3616c72ffaaSAndy Whitcroft __must_check| 362417495edSAndy Whitcroft __kprobes| 363165e72a6SSven Eckelmann __ref| 36433aa4597SGeert Uytterhoeven __refconst| 36533aa4597SGeert Uytterhoeven __refdata| 366ad315455SBoqun Feng __rcu| 367ad315455SBoqun Feng __private 3686c72ffaaSAndy Whitcroft }x; 369e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 370e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 371e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 372e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 373e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3748716de38SJoe Perches 37552131292SWolfram Sang# Notes to $Attribute: 37652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3776c72ffaaSAndy Whitcroftour $Attribute = qr{ 3786c72ffaaSAndy Whitcroft const| 37903f1df7dSJoe Perches __percpu| 38003f1df7dSJoe Perches __nocast| 38103f1df7dSJoe Perches __safe| 38246d832f5SMichael S. Tsirkin __bitwise| 38303f1df7dSJoe Perches __packed__| 38403f1df7dSJoe Perches __packed2__| 38503f1df7dSJoe Perches __naked| 38603f1df7dSJoe Perches __maybe_unused| 38703f1df7dSJoe Perches __always_unused| 38803f1df7dSJoe Perches __noreturn| 38903f1df7dSJoe Perches __used| 39003f1df7dSJoe Perches __cold| 391e23ef1f3SJoe Perches __pure| 39203f1df7dSJoe Perches __noclone| 39303f1df7dSJoe Perches __deprecated| 3946c72ffaaSAndy Whitcroft __read_mostly| 395c5967e98SJoe Perches __ro_after_init| 3966c72ffaaSAndy Whitcroft __kprobes| 3978716de38SJoe Perches $InitAttribute| 39824e1d81aSAndy Whitcroft ____cacheline_aligned| 39924e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 4005fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 4015fe3af11SAndy Whitcroft __weak 4026c72ffaaSAndy Whitcroft }x; 403c45dcabdSAndy Whitcroftour $Modifier; 40491cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 4056c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 4066c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 4076c72ffaaSAndy Whitcroft 40895e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 40995e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 41095e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 41195e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4122435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 413c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 414326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 415326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 416326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 41774349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4182435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 419326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 420447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 42123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4226c72ffaaSAndy Whitcroftour $Operators = qr{ 4236c72ffaaSAndy Whitcroft <=|>=|==|!=| 4246c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 42523f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4266c72ffaaSAndy Whitcroft }x; 4276c72ffaaSAndy Whitcroft 42891cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 42991cb5195SJoe Perches 430ab7e23f3SJoe Perchesour $BasicType; 4318905a67cSAndy Whitcroftour $NonptrType; 4321813087dSJoe Perchesour $NonptrTypeMisordered; 4338716de38SJoe Perchesour $NonptrTypeWithAttr; 4348905a67cSAndy Whitcroftour $Type; 4351813087dSJoe Perchesour $TypeMisordered; 4368905a67cSAndy Whitcroftour $Declare; 4371813087dSJoe Perchesour $DeclareMisordered; 4388905a67cSAndy Whitcroft 43915662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 44015662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 441171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 442171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 443171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 444171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 445171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 446171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 447171ae1a4SAndy Whitcroft}x; 448171ae1a4SAndy Whitcroft 44915662b3eSJoe Perchesour $UTF8 = qr{ 45015662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 45115662b3eSJoe Perches | $NON_ASCII_UTF8 45215662b3eSJoe Perches}x; 45315662b3eSJoe Perches 454e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 455021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 456021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 457021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 458021158b4SJoe Perches)}; 459e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 460fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4618ed22cadSAndy Whitcroft atomic_t 4628ed22cadSAndy Whitcroft)}; 463e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 464e6176fa4SJoe Perches $typeC99Typedefs\b| 465e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 466e6176fa4SJoe Perches $typeKernelTypedefs\b 467e6176fa4SJoe Perches)}; 4688ed22cadSAndy Whitcroft 4696d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4706d32f7a3SJoe Perches 471691e669bSJoe Perchesour $logFunctions = qr{(?x: 472758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4737d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 47487bd499aSJoe Perches TP_printk| 4756e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 476b0531722SJoe Perches panic| 47706668727SJoe Perches MODULE_[A-Z_]+| 47806668727SJoe Perches seq_vprintf|seq_printf|seq_puts 479691e669bSJoe Perches)}; 480691e669bSJoe Perches 481e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 482e29a70f1SJoe Perches (?:(?:devm_)? 483e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 484e29a70f1SJoe Perches kstrdup(?:_const)? | 485e29a70f1SJoe Perches kmemdup(?:_nul)?) | 486461e1565SChristophe JAILLET (?:\w+)?alloc_skb(?:_ip_align)? | 487e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 488e29a70f1SJoe Perches dma_alloc_coherent 489e29a70f1SJoe Perches)}; 490e29a70f1SJoe Perches 49120112475SJoe Perchesour $signature_tags = qr{(?xi: 49220112475SJoe Perches Signed-off-by:| 493d499480cSJorge Ramirez-Ortiz Co-developed-by:| 49420112475SJoe Perches Acked-by:| 49520112475SJoe Perches Tested-by:| 49620112475SJoe Perches Reviewed-by:| 49720112475SJoe Perches Reported-by:| 4988543ae12SMugunthan V N Suggested-by:| 49920112475SJoe Perches To:| 50020112475SJoe Perches Cc: 50120112475SJoe Perches)}; 50220112475SJoe Perches 5031813087dSJoe Perchesour @typeListMisordered = ( 5041813087dSJoe Perches qr{char\s+(?:un)?signed}, 5051813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 5061813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 5071813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 5081813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 5091813087dSJoe Perches qr{short\s+(?:un)?signed}, 5101813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5111813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5121813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5131813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5141813087dSJoe Perches qr{int\s+(?:un)?signed}, 5151813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5161813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5171813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5181813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5191813087dSJoe Perches qr{long\s+(?:un)?signed}, 5201813087dSJoe Perches); 5211813087dSJoe Perches 5228905a67cSAndy Whitcroftour @typeList = ( 5238905a67cSAndy Whitcroft qr{void}, 5240c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5250c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5260c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5270c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5280c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5290c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5300c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5310c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5320c773d9dSJoe Perches qr{(?:un)?signed}, 5338905a67cSAndy Whitcroft qr{float}, 5348905a67cSAndy Whitcroft qr{double}, 5358905a67cSAndy Whitcroft qr{bool}, 5368905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5378905a67cSAndy Whitcroft qr{union\s+$Ident}, 5388905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5398905a67cSAndy Whitcroft qr{${Ident}_t}, 5408905a67cSAndy Whitcroft qr{${Ident}_handler}, 5418905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5421813087dSJoe Perches @typeListMisordered, 5438905a67cSAndy Whitcroft); 544938224b5SJoe Perches 545938224b5SJoe Perchesour $C90_int_types = qr{(?x: 546938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 547938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 548938224b5SJoe Perches long\s+long\s+(?:un)?signed| 549938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 550938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 551938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 552938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 553938224b5SJoe Perches 554938224b5SJoe Perches long\s+int\s+(?:un)?signed| 555938224b5SJoe Perches long\s+(?:un)?signed\s+int| 556938224b5SJoe Perches long\s+(?:un)?signed| 557938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 558938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 559938224b5SJoe Perches int\s+long\s+(?:un)?signed| 560938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 561938224b5SJoe Perches 562938224b5SJoe Perches int\s+(?:un)?signed| 563938224b5SJoe Perches (?:(?:un)?signed\s+)?int 564938224b5SJoe Perches)}; 565938224b5SJoe Perches 566485ff23eSAlex Dowadour @typeListFile = (); 5678716de38SJoe Perchesour @typeListWithAttr = ( 5688716de38SJoe Perches @typeList, 5698716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5708716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5718716de38SJoe Perches); 5728716de38SJoe Perches 573c45dcabdSAndy Whitcroftour @modifierList = ( 574c45dcabdSAndy Whitcroft qr{fastcall}, 575c45dcabdSAndy Whitcroft); 576485ff23eSAlex Dowadour @modifierListFile = (); 5778905a67cSAndy Whitcroft 5782435880fSJoe Perchesour @mode_permission_funcs = ( 5792435880fSJoe Perches ["module_param", 3], 5802435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5812435880fSJoe Perches ["module_param_array_named", 5], 5822435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5832435880fSJoe Perches ["proc_create(?:_data|)", 2], 584459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 585459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 586459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 587459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 588459cf0aeSJoe Perches ["__ATTR", 2], 5892435880fSJoe Perches); 5902435880fSJoe Perches 591515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 592515a235eSJoe Perchesour $mode_perms_search = ""; 593515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 594515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 595515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 596515a235eSJoe Perches} 59700180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 598515a235eSJoe Perches 5999189c7e7SJoe Perchesour %deprecated_apis = ( 6009189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 6019189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 6029189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 6039189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 6049189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 6059189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 6069189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 6079189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 6089189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 6099189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6109189c7e7SJoe Perches); 6119189c7e7SJoe Perches 6129189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6139189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6149189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6159189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6169189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6179189c7e7SJoe Perches} 6189189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6199189c7e7SJoe Perches 620b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 621b392c64fSJoe Perches S_IWUGO | 622b392c64fSJoe Perches S_IWOTH | 623b392c64fSJoe Perches S_IRWXUGO | 624b392c64fSJoe Perches S_IALLUGO | 625b392c64fSJoe Perches 0[0-7][0-7][2367] 626b392c64fSJoe Perches}x; 627b392c64fSJoe Perches 628f90774e1SJoe Perchesour %mode_permission_string_types = ( 629f90774e1SJoe Perches "S_IRWXU" => 0700, 630f90774e1SJoe Perches "S_IRUSR" => 0400, 631f90774e1SJoe Perches "S_IWUSR" => 0200, 632f90774e1SJoe Perches "S_IXUSR" => 0100, 633f90774e1SJoe Perches "S_IRWXG" => 0070, 634f90774e1SJoe Perches "S_IRGRP" => 0040, 635f90774e1SJoe Perches "S_IWGRP" => 0020, 636f90774e1SJoe Perches "S_IXGRP" => 0010, 637f90774e1SJoe Perches "S_IRWXO" => 0007, 638f90774e1SJoe Perches "S_IROTH" => 0004, 639f90774e1SJoe Perches "S_IWOTH" => 0002, 640f90774e1SJoe Perches "S_IXOTH" => 0001, 641f90774e1SJoe Perches "S_IRWXUGO" => 0777, 642f90774e1SJoe Perches "S_IRUGO" => 0444, 643f90774e1SJoe Perches "S_IWUGO" => 0222, 644f90774e1SJoe Perches "S_IXUGO" => 0111, 645f90774e1SJoe Perches); 646f90774e1SJoe Perches 647f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 648f90774e1SJoe Perchesour $mode_perms_string_search = ""; 649f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 650f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 651f90774e1SJoe Perches $mode_perms_string_search .= $entry; 652f90774e1SJoe Perches} 65300180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 65400180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 65500180468SJoe Perches ${single_mode_perms_string_search} 65600180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 65700180468SJoe Perches}x; 65800180468SJoe Perches 65900180468SJoe Perchessub perms_to_octal { 66000180468SJoe Perches my ($string) = @_; 66100180468SJoe Perches 66200180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 66300180468SJoe Perches 66400180468SJoe Perches my $val = ""; 66500180468SJoe Perches my $oval = ""; 66600180468SJoe Perches my $to = 0; 66700180468SJoe Perches my $curpos = 0; 66800180468SJoe Perches my $lastpos = 0; 66900180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 67000180468SJoe Perches $curpos = pos($string); 67100180468SJoe Perches my $match = $2; 67200180468SJoe Perches my $omatch = $1; 67300180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 67400180468SJoe Perches $lastpos = $curpos; 67500180468SJoe Perches $to |= $mode_permission_string_types{$match}; 67600180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 67700180468SJoe Perches $val .= $match; 67800180468SJoe Perches $oval .= $omatch; 67900180468SJoe Perches } 68000180468SJoe Perches $oval =~ s/^\s*\|\s*//; 68100180468SJoe Perches $oval =~ s/\s*\|\s*$//; 68200180468SJoe Perches return sprintf("%04o", $to); 68300180468SJoe Perches} 684f90774e1SJoe Perches 6857840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6867840a94cSWolfram Sang irq| 687cdcee686SSergey Ryazanov memory| 688cdcee686SSergey Ryazanov time| 689cdcee686SSergey Ryazanov reboot 6907840a94cSWolfram Sang)}; 6917840a94cSWolfram Sang# memory.h: ARM has a custom one 6927840a94cSWolfram Sang 69366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 69466b47b4aSKees Cookmy $misspellings; 69566b47b4aSKees Cookmy %spelling_fix; 69636061e38SJoe Perches 69736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 69866b47b4aSKees Cook while (<$spelling>) { 69966b47b4aSKees Cook my $line = $_; 70066b47b4aSKees Cook 70166b47b4aSKees Cook $line =~ s/\s*\n?$//g; 70266b47b4aSKees Cook $line =~ s/^\s*//g; 70366b47b4aSKees Cook 70466b47b4aSKees Cook next if ($line =~ m/^\s*#/); 70566b47b4aSKees Cook next if ($line =~ m/^\s*$/); 70666b47b4aSKees Cook 70766b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 70866b47b4aSKees Cook 70966b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 71066b47b4aSKees Cook } 71166b47b4aSKees Cook close($spelling); 71236061e38SJoe Perches} else { 71336061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 71436061e38SJoe Perches} 71566b47b4aSKees Cook 716ebfd7d62SJoe Perchesif ($codespell) { 717ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 718ebfd7d62SJoe Perches while (<$spelling>) { 719ebfd7d62SJoe Perches my $line = $_; 720ebfd7d62SJoe Perches 721ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 722ebfd7d62SJoe Perches $line =~ s/^\s*//g; 723ebfd7d62SJoe Perches 724ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 725ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 726ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 727ebfd7d62SJoe Perches 728ebfd7d62SJoe Perches $line =~ s/,.*$//; 729ebfd7d62SJoe Perches 730ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 731ebfd7d62SJoe Perches 732ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 733ebfd7d62SJoe Perches } 734ebfd7d62SJoe Perches close($spelling); 735ebfd7d62SJoe Perches } else { 736ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 737ebfd7d62SJoe Perches } 738ebfd7d62SJoe Perches} 739ebfd7d62SJoe Perches 740ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 741ebfd7d62SJoe Perches 74275ad8c57SJerome Forissiersub read_words { 74375ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 74475ad8c57SJerome Forissier 74575ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 74675ad8c57SJerome Forissier while (<$words>) { 747bf1fa1daSJoe Perches my $line = $_; 748bf1fa1daSJoe Perches 749bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 750bf1fa1daSJoe Perches $line =~ s/^\s*//g; 751bf1fa1daSJoe Perches 752bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 753bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 754bf1fa1daSJoe Perches if ($line =~ /\s/) { 75575ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 756bf1fa1daSJoe Perches next; 757bf1fa1daSJoe Perches } 758bf1fa1daSJoe Perches 75975ad8c57SJerome Forissier $$wordsRef .= '|' if ($$wordsRef ne ""); 76075ad8c57SJerome Forissier $$wordsRef .= $line; 761bf1fa1daSJoe Perches } 76275ad8c57SJerome Forissier close($file); 76375ad8c57SJerome Forissier return 1; 764bf1fa1daSJoe Perches } 765bf1fa1daSJoe Perches 76675ad8c57SJerome Forissier return 0; 76775ad8c57SJerome Forissier} 76875ad8c57SJerome Forissier 76975ad8c57SJerome Forissiermy $const_structs = ""; 77075ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile) 77175ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 77275ad8c57SJerome Forissier 77375ad8c57SJerome Forissiermy $typeOtherTypedefs = ""; 77475ad8c57SJerome Forissierif (length($typedefsfile)) { 77575ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 77675ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 77775ad8c57SJerome Forissier} 77875ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); 77975ad8c57SJerome Forissier 7808905a67cSAndy Whitcroftsub build_types { 781485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 782485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7831813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7848716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 785c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 786ab7e23f3SJoe Perches $BasicType = qr{ 787ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 788ab7e23f3SJoe Perches (?:${all}\b) 789ab7e23f3SJoe Perches }x; 7908905a67cSAndy Whitcroft $NonptrType = qr{ 791d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 792cf655043SAndy Whitcroft (?: 7936b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 7948ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 795c45dcabdSAndy Whitcroft (?:${all}\b) 796cf655043SAndy Whitcroft ) 797c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 7988905a67cSAndy Whitcroft }x; 7991813087dSJoe Perches $NonptrTypeMisordered = qr{ 8001813087dSJoe Perches (?:$Modifier\s+|const\s+)* 8011813087dSJoe Perches (?: 8021813087dSJoe Perches (?:${Misordered}\b) 8031813087dSJoe Perches ) 8041813087dSJoe Perches (?:\s+$Modifier|\s+const)* 8051813087dSJoe Perches }x; 8068716de38SJoe Perches $NonptrTypeWithAttr = qr{ 8078716de38SJoe Perches (?:$Modifier\s+|const\s+)* 8088716de38SJoe Perches (?: 8098716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8108716de38SJoe Perches (?:$typeTypedefs\b)| 8118716de38SJoe Perches (?:${allWithAttr}\b) 8128716de38SJoe Perches ) 8138716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8148716de38SJoe Perches }x; 8158905a67cSAndy Whitcroft $Type = qr{ 816c45dcabdSAndy Whitcroft $NonptrType 8177b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 818c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8198905a67cSAndy Whitcroft }x; 8201813087dSJoe Perches $TypeMisordered = qr{ 8211813087dSJoe Perches $NonptrTypeMisordered 8227b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 8231813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8241813087dSJoe Perches }x; 82591cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8261813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8278905a67cSAndy Whitcroft} 8288905a67cSAndy Whitcroftbuild_types(); 8296c72ffaaSAndy Whitcroft 8307d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 831d1fe9c09SJoe Perches 832d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 833d1fe9c09SJoe Perches# requires at least perl version v5.10.0 834d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 835d1fe9c09SJoe Perches 836d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 8372435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 838c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 8397d2367afSJoe Perches 840f8422308SJoe Perchesour $declaration_macros = qr{(?x: 8413e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 842fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 8433d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 844f8422308SJoe Perches)}; 845f8422308SJoe Perches 8467d2367afSJoe Perchessub deparenthesize { 8477d2367afSJoe Perches my ($string) = @_; 8487d2367afSJoe Perches return "" if (!defined($string)); 8495b9553abSJoe Perches 8505b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8515b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8525b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8535b9553abSJoe Perches } 8545b9553abSJoe Perches 8557d2367afSJoe Perches $string =~ s@\s+@ @g; 8565b9553abSJoe Perches 8577d2367afSJoe Perches return $string; 8587d2367afSJoe Perches} 8597d2367afSJoe Perches 8603445686aSJoe Perchessub seed_camelcase_file { 8613445686aSJoe Perches my ($file) = @_; 8623445686aSJoe Perches 8633445686aSJoe Perches return if (!(-f $file)); 8643445686aSJoe Perches 8653445686aSJoe Perches local $/; 8663445686aSJoe Perches 8673445686aSJoe Perches open(my $include_file, '<', "$file") 8683445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8693445686aSJoe Perches my $text = <$include_file>; 8703445686aSJoe Perches close($include_file); 8713445686aSJoe Perches 8723445686aSJoe Perches my @lines = split('\n', $text); 8733445686aSJoe Perches 8743445686aSJoe Perches foreach my $line (@lines) { 8753445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8763445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8773445686aSJoe Perches $camelcase{$1} = 1; 87811ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 87911ea516aSJoe Perches $camelcase{$1} = 1; 88011ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8813445686aSJoe Perches $camelcase{$1} = 1; 8823445686aSJoe Perches } 8833445686aSJoe Perches } 8843445686aSJoe Perches} 8853445686aSJoe Perches 886cd28b119SJoe Perchesour %maintained_status = (); 887cd28b119SJoe Perches 88885b0ee18SJoe Perchessub is_maintained_obsolete { 88985b0ee18SJoe Perches my ($filename) = @_; 89085b0ee18SJoe Perches 891f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 89285b0ee18SJoe Perches 893cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 894cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 895cd28b119SJoe Perches } 89685b0ee18SJoe Perches 897cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 89885b0ee18SJoe Perches} 89985b0ee18SJoe Perches 9003b6e8ac9SJoe Perchessub is_SPDX_License_valid { 9013b6e8ac9SJoe Perches my ($license) = @_; 9023b6e8ac9SJoe Perches 90356294112SJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git")); 9043b6e8ac9SJoe Perches 90556294112SJoe Perches my $root_path = abs_path($root); 90656294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 9073b6e8ac9SJoe Perches return 0 if ($status ne ""); 9083b6e8ac9SJoe Perches return 1; 9093b6e8ac9SJoe Perches} 9103b6e8ac9SJoe Perches 9113445686aSJoe Perchesmy $camelcase_seeded = 0; 9123445686aSJoe Perchessub seed_camelcase_includes { 9133445686aSJoe Perches return if ($camelcase_seeded); 9143445686aSJoe Perches 9153445686aSJoe Perches my $files; 916c707a81dSJoe Perches my $camelcase_cache = ""; 917c707a81dSJoe Perches my @include_files = (); 918c707a81dSJoe Perches 919c707a81dSJoe Perches $camelcase_seeded = 1; 920351b2a1fSJoe Perches 9213645e328SRichard Genoud if (-e ".git") { 922dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 923351b2a1fSJoe Perches chomp $git_last_include_commit; 924c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 925c707a81dSJoe Perches } else { 926c707a81dSJoe Perches my $last_mod_date = 0; 927c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 928c707a81dSJoe Perches @include_files = split('\n', $files); 929c707a81dSJoe Perches foreach my $file (@include_files) { 930c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 931c707a81dSJoe Perches localtime((stat $file)[9])); 932c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 933c707a81dSJoe Perches } 934c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 935c707a81dSJoe Perches } 936c707a81dSJoe Perches 937c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 938c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 939c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 940351b2a1fSJoe Perches while (<$camelcase_file>) { 941351b2a1fSJoe Perches chomp; 942351b2a1fSJoe Perches $camelcase{$_} = 1; 943351b2a1fSJoe Perches } 944351b2a1fSJoe Perches close($camelcase_file); 945351b2a1fSJoe Perches 946351b2a1fSJoe Perches return; 947351b2a1fSJoe Perches } 948c707a81dSJoe Perches 9493645e328SRichard Genoud if (-e ".git") { 950dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 951c707a81dSJoe Perches @include_files = split('\n', $files); 9523445686aSJoe Perches } 953c707a81dSJoe Perches 9543445686aSJoe Perches foreach my $file (@include_files) { 9553445686aSJoe Perches seed_camelcase_file($file); 9563445686aSJoe Perches } 957351b2a1fSJoe Perches 958c707a81dSJoe Perches if ($camelcase_cache ne "") { 959351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 960c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 961c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 962351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 963351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 964351b2a1fSJoe Perches } 965351b2a1fSJoe Perches close($camelcase_file); 966351b2a1fSJoe Perches } 9673445686aSJoe Perches} 9683445686aSJoe Perches 969d311cd44SJoe Perchessub git_commit_info { 970d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 971d311cd44SJoe Perches 972d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 973d311cd44SJoe Perches 974dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 975d311cd44SJoe Perches $output =~ s/^\s*//gm; 976d311cd44SJoe Perches my @lines = split("\n", $output); 977d311cd44SJoe Perches 9780d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9790d7835fcSJoe Perches 9805a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 981d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 982d311cd44SJoe Perches# all matching commit ids, but it's very slow... 983d311cd44SJoe Perches# 984d311cd44SJoe Perches# echo "checking commits $1..." 985d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 986d311cd44SJoe Perches# while read line ; do 987d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 988d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 989d311cd44SJoe Perches# done 990d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 991948b133aSHeinrich Schuchardt $id = undef; 992d311cd44SJoe Perches } else { 993d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 994d311cd44SJoe Perches $desc = substr($lines[0], 41); 995d311cd44SJoe Perches } 996d311cd44SJoe Perches 997d311cd44SJoe Perches return ($id, $desc); 998d311cd44SJoe Perches} 999d311cd44SJoe Perches 10006c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 10010a920b5bSAndy Whitcroft 100200df344fSAndy Whitcroftmy @rawlines = (); 1003c2fdda0dSAndy Whitcroftmy @lines = (); 10043705ce5bSJoe Perchesmy @fixed = (); 1005d752fcc8SJoe Perchesmy @fixed_inserted = (); 1006d752fcc8SJoe Perchesmy @fixed_deleted = (); 1007194f66fcSJoe Perchesmy $fixlinenr = -1; 1008194f66fcSJoe Perches 10094a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10104a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 10114a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 10124a593c34SDu, Changbin 10134a593c34SDu, Changbinif ($git) { 10144a593c34SDu, Changbin my @commits = (); 10150dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 10164a593c34SDu, Changbin my $git_range; 101728898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 101828898fd1SJoe Perches $git_range = "-$2 $1"; 10194a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 10204a593c34SDu, Changbin $git_range = "$commit_expr"; 10214a593c34SDu, Changbin } else { 10220dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 10230dea9f1eSJoe Perches } 1024dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 10250dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 102628898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 102728898fd1SJoe Perches next if (!defined($1) || !defined($2)); 10280dea9f1eSJoe Perches my $sha1 = $1; 10290dea9f1eSJoe Perches my $subject = $2; 10300dea9f1eSJoe Perches unshift(@commits, $sha1); 10310dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 10324a593c34SDu, Changbin } 10334a593c34SDu, Changbin } 10344a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 10354a593c34SDu, Changbin @ARGV = @commits; 10364a593c34SDu, Changbin} 10374a593c34SDu, Changbin 1038c2fdda0dSAndy Whitcroftmy $vname; 103998005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 10406c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 104121caa13cSAndy Whitcroft my $FILE; 10424a593c34SDu, Changbin if ($git) { 10434a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 10444a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 10454a593c34SDu, Changbin } elsif ($file) { 104621caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 10476c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 104821caa13cSAndy Whitcroft } elsif ($filename eq '-') { 104921caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 10506c72ffaaSAndy Whitcroft } else { 105121caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 10526c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 10536c72ffaaSAndy Whitcroft } 1054c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1055c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 10564a593c34SDu, Changbin } elsif ($git) { 10570dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1058c2fdda0dSAndy Whitcroft } else { 1059c2fdda0dSAndy Whitcroft $vname = $filename; 1060c2fdda0dSAndy Whitcroft } 106121caa13cSAndy Whitcroft while (<$FILE>) { 10620a920b5bSAndy Whitcroft chomp; 106300df344fSAndy Whitcroft push(@rawlines, $_); 1064c7f574d0SGeert Uytterhoeven $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); 10656c72ffaaSAndy Whitcroft } 106621caa13cSAndy Whitcroft close($FILE); 1067d8469f16SJoe Perches 1068d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1069d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1070d8469f16SJoe Perches print "$vname\n"; 1071d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1072d8469f16SJoe Perches } 1073d8469f16SJoe Perches 1074c2fdda0dSAndy Whitcroft if (!process($filename)) { 10750a920b5bSAndy Whitcroft $exit = 1; 10760a920b5bSAndy Whitcroft } 107700df344fSAndy Whitcroft @rawlines = (); 107813214adfSAndy Whitcroft @lines = (); 10793705ce5bSJoe Perches @fixed = (); 1080d752fcc8SJoe Perches @fixed_inserted = (); 1081d752fcc8SJoe Perches @fixed_deleted = (); 1082194f66fcSJoe Perches $fixlinenr = -1; 1083485ff23eSAlex Dowad @modifierListFile = (); 1084485ff23eSAlex Dowad @typeListFile = (); 1085485ff23eSAlex Dowad build_types(); 10860a920b5bSAndy Whitcroft} 10870a920b5bSAndy Whitcroft 1088d8469f16SJoe Perchesif (!$quiet) { 10893c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 10903c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 10913c816e49SJoe Perches 10925b57980dSJoe Perches if (!$perl_version_ok) { 1093d8469f16SJoe Perches print << "EOM" 1094d8469f16SJoe Perches 1095d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 10965b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1097d8469f16SJoe PerchesEOM 1098d8469f16SJoe Perches } 1099d8469f16SJoe Perches if ($exit) { 1100d8469f16SJoe Perches print << "EOM" 1101d8469f16SJoe Perches 1102d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1103d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1104d8469f16SJoe PerchesEOM 1105d8469f16SJoe Perches } 1106d8469f16SJoe Perches} 1107d8469f16SJoe Perches 11080a920b5bSAndy Whitcroftexit($exit); 11090a920b5bSAndy Whitcroft 11100a920b5bSAndy Whitcroftsub top_of_kernel_tree { 11116c72ffaaSAndy Whitcroft my ($root) = @_; 11126c72ffaaSAndy Whitcroft 11136c72ffaaSAndy Whitcroft my @tree_check = ( 11146c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 11156c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 11166c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 11176c72ffaaSAndy Whitcroft ); 11186c72ffaaSAndy Whitcroft 11196c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 11206c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 11210a920b5bSAndy Whitcroft return 0; 11220a920b5bSAndy Whitcroft } 11236c72ffaaSAndy Whitcroft } 11246c72ffaaSAndy Whitcroft return 1; 11256c72ffaaSAndy Whitcroft} 11260a920b5bSAndy Whitcroft 112720112475SJoe Perchessub parse_email { 112820112475SJoe Perches my ($formatted_email) = @_; 112920112475SJoe Perches 113020112475SJoe Perches my $name = ""; 1131dfa05c28SJoe Perches my $name_comment = ""; 113220112475SJoe Perches my $address = ""; 113320112475SJoe Perches my $comment = ""; 113420112475SJoe Perches 113520112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 113620112475SJoe Perches $name = $1; 113720112475SJoe Perches $address = $2; 113820112475SJoe Perches $comment = $3 if defined $3; 113920112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 114020112475SJoe Perches $address = $1; 114120112475SJoe Perches $comment = $2 if defined $2; 114220112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 114320112475SJoe Perches $address = $1; 114420112475SJoe Perches $comment = $2 if defined $2; 114585e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 114620112475SJoe Perches $name = $formatted_email; 11473705ce5bSJoe Perches $name = trim($name); 114820112475SJoe Perches $name =~ s/^\"|\"$//g; 114920112475SJoe Perches # If there's a name left after stripping spaces and 115020112475SJoe Perches # leading quotes, and the address doesn't have both 115120112475SJoe Perches # leading and trailing angle brackets, the address 115220112475SJoe Perches # is invalid. ie: 115320112475SJoe Perches # "joe smith [email protected]" bad 115420112475SJoe Perches # "joe smith <[email protected]" bad 115520112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 115620112475SJoe Perches $name = ""; 115720112475SJoe Perches $address = ""; 115820112475SJoe Perches $comment = ""; 115920112475SJoe Perches } 116020112475SJoe Perches } 116120112475SJoe Perches 11623705ce5bSJoe Perches $name = trim($name); 116320112475SJoe Perches $name =~ s/^\"|\"$//g; 1164dfa05c28SJoe Perches $name =~ s/(\s*\([^\)]+\))\s*//; 1165dfa05c28SJoe Perches if (defined($1)) { 1166dfa05c28SJoe Perches $name_comment = trim($1); 1167dfa05c28SJoe Perches } 11683705ce5bSJoe Perches $address = trim($address); 116920112475SJoe Perches $address =~ s/^\<|\>$//g; 117020112475SJoe Perches 117120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 117220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 117320112475SJoe Perches $name = "\"$name\""; 117420112475SJoe Perches } 117520112475SJoe Perches 1176dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 117720112475SJoe Perches} 117820112475SJoe Perches 117920112475SJoe Perchessub format_email { 118020112475SJoe Perches my ($name, $address) = @_; 118120112475SJoe Perches 118220112475SJoe Perches my $formatted_email; 118320112475SJoe Perches 11843705ce5bSJoe Perches $name = trim($name); 118520112475SJoe Perches $name =~ s/^\"|\"$//g; 11863705ce5bSJoe Perches $address = trim($address); 118720112475SJoe Perches 118820112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 118920112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 119020112475SJoe Perches $name = "\"$name\""; 119120112475SJoe Perches } 119220112475SJoe Perches 119320112475SJoe Perches if ("$name" eq "") { 119420112475SJoe Perches $formatted_email = "$address"; 119520112475SJoe Perches } else { 119620112475SJoe Perches $formatted_email = "$name <$address>"; 119720112475SJoe Perches } 119820112475SJoe Perches 119920112475SJoe Perches return $formatted_email; 120020112475SJoe Perches} 120120112475SJoe Perches 1202dfa05c28SJoe Perchessub reformat_email { 1203dfa05c28SJoe Perches my ($email) = @_; 1204dfa05c28SJoe Perches 1205dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 1206dfa05c28SJoe Perches return format_email($email_name, $email_address); 1207dfa05c28SJoe Perches} 1208dfa05c28SJoe Perches 1209dfa05c28SJoe Perchessub same_email_addresses { 1210dfa05c28SJoe Perches my ($email1, $email2) = @_; 1211dfa05c28SJoe Perches 1212dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1213dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1214dfa05c28SJoe Perches 1215dfa05c28SJoe Perches return $email1_name eq $email2_name && 1216dfa05c28SJoe Perches $email1_address eq $email2_address; 1217dfa05c28SJoe Perches} 1218dfa05c28SJoe Perches 1219d311cd44SJoe Perchessub which { 1220d311cd44SJoe Perches my ($bin) = @_; 1221d311cd44SJoe Perches 1222d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1223d311cd44SJoe Perches if (-e "$path/$bin") { 1224d311cd44SJoe Perches return "$path/$bin"; 1225d311cd44SJoe Perches } 1226d311cd44SJoe Perches } 1227d311cd44SJoe Perches 1228d311cd44SJoe Perches return ""; 1229d311cd44SJoe Perches} 1230d311cd44SJoe Perches 1231000d1cc1SJoe Perchessub which_conf { 1232000d1cc1SJoe Perches my ($conf) = @_; 1233000d1cc1SJoe Perches 1234000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1235000d1cc1SJoe Perches if (-e "$path/$conf") { 1236000d1cc1SJoe Perches return "$path/$conf"; 1237000d1cc1SJoe Perches } 1238000d1cc1SJoe Perches } 1239000d1cc1SJoe Perches 1240000d1cc1SJoe Perches return ""; 1241000d1cc1SJoe Perches} 1242000d1cc1SJoe Perches 12430a920b5bSAndy Whitcroftsub expand_tabs { 12440a920b5bSAndy Whitcroft my ($str) = @_; 12450a920b5bSAndy Whitcroft 12460a920b5bSAndy Whitcroft my $res = ''; 12470a920b5bSAndy Whitcroft my $n = 0; 12480a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 12490a920b5bSAndy Whitcroft if ($c eq "\t") { 12500a920b5bSAndy Whitcroft $res .= ' '; 12510a920b5bSAndy Whitcroft $n++; 1252713a09deSAntonio Borneo for (; ($n % $tabsize) != 0; $n++) { 12530a920b5bSAndy Whitcroft $res .= ' '; 12540a920b5bSAndy Whitcroft } 12550a920b5bSAndy Whitcroft next; 12560a920b5bSAndy Whitcroft } 12570a920b5bSAndy Whitcroft $res .= $c; 12580a920b5bSAndy Whitcroft $n++; 12590a920b5bSAndy Whitcroft } 12600a920b5bSAndy Whitcroft 12610a920b5bSAndy Whitcroft return $res; 12620a920b5bSAndy Whitcroft} 12636c72ffaaSAndy Whitcroftsub copy_spacing { 1264773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 12656c72ffaaSAndy Whitcroft return $res; 12666c72ffaaSAndy Whitcroft} 12670a920b5bSAndy Whitcroft 12684a0df2efSAndy Whitcroftsub line_stats { 12694a0df2efSAndy Whitcroft my ($line) = @_; 12704a0df2efSAndy Whitcroft 12714a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 12724a0df2efSAndy Whitcroft $line =~ s/^.//; 12734a0df2efSAndy Whitcroft $line = expand_tabs($line); 12744a0df2efSAndy Whitcroft 12754a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 12764a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 12774a0df2efSAndy Whitcroft 12784a0df2efSAndy Whitcroft return (length($line), length($white)); 12794a0df2efSAndy Whitcroft} 12804a0df2efSAndy Whitcroft 1281773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1282773647a0SAndy Whitcroft 1283773647a0SAndy Whitcroftsub sanitise_line_reset { 1284773647a0SAndy Whitcroft my ($in_comment) = @_; 1285773647a0SAndy Whitcroft 1286773647a0SAndy Whitcroft if ($in_comment) { 1287773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1288773647a0SAndy Whitcroft } else { 1289773647a0SAndy Whitcroft $sanitise_quote = ''; 1290773647a0SAndy Whitcroft } 1291773647a0SAndy Whitcroft} 129200df344fSAndy Whitcroftsub sanitise_line { 129300df344fSAndy Whitcroft my ($line) = @_; 129400df344fSAndy Whitcroft 129500df344fSAndy Whitcroft my $res = ''; 129600df344fSAndy Whitcroft my $l = ''; 129700df344fSAndy Whitcroft 1298c2fdda0dSAndy Whitcroft my $qlen = 0; 1299773647a0SAndy Whitcroft my $off = 0; 1300773647a0SAndy Whitcroft my $c; 130100df344fSAndy Whitcroft 1302773647a0SAndy Whitcroft # Always copy over the diff marker. 1303773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1304773647a0SAndy Whitcroft 1305773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1306773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1307773647a0SAndy Whitcroft 13088d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1309773647a0SAndy Whitcroft # and end, all to $;. 1310773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1311773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1312773647a0SAndy Whitcroft 1313773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1314773647a0SAndy Whitcroft $off++; 131500df344fSAndy Whitcroft next; 1316773647a0SAndy Whitcroft } 131781bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1318773647a0SAndy Whitcroft $sanitise_quote = ''; 1319773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1320773647a0SAndy Whitcroft $off++; 1321773647a0SAndy Whitcroft next; 1322773647a0SAndy Whitcroft } 1323113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1324113f04a8SDaniel Walker $sanitise_quote = '//'; 1325113f04a8SDaniel Walker 1326113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1327113f04a8SDaniel Walker $off++; 1328113f04a8SDaniel Walker next; 1329113f04a8SDaniel Walker } 1330773647a0SAndy Whitcroft 1331773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1332773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1333773647a0SAndy Whitcroft $c eq "\\") { 1334773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1335773647a0SAndy Whitcroft $off++; 1336773647a0SAndy Whitcroft next; 1337773647a0SAndy Whitcroft } 1338773647a0SAndy Whitcroft # Regular quotes. 1339773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1340773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1341773647a0SAndy Whitcroft $sanitise_quote = $c; 1342773647a0SAndy Whitcroft 1343773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1344773647a0SAndy Whitcroft next; 1345773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1346773647a0SAndy Whitcroft $sanitise_quote = ''; 134700df344fSAndy Whitcroft } 134800df344fSAndy Whitcroft } 1349773647a0SAndy Whitcroft 1350fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1351773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1352773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1353113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1354113f04a8SDaniel Walker substr($res, $off, 1, $;); 1355773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1356773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 135700df344fSAndy Whitcroft } else { 1358773647a0SAndy Whitcroft substr($res, $off, 1, $c); 135900df344fSAndy Whitcroft } 1360c2fdda0dSAndy Whitcroft } 1361c2fdda0dSAndy Whitcroft 1362113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1363113f04a8SDaniel Walker $sanitise_quote = ''; 1364113f04a8SDaniel Walker } 1365113f04a8SDaniel Walker 1366c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1367c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1368c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1369c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1370c2fdda0dSAndy Whitcroft 1371c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1372c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1373c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1374c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1375c2fdda0dSAndy Whitcroft } 1376c2fdda0dSAndy Whitcroft 1377dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1378dadf680dSJoe Perches my $match = $1; 1379dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1380dadf680dSJoe Perches } 1381dadf680dSJoe Perches 138200df344fSAndy Whitcroft return $res; 138300df344fSAndy Whitcroft} 138400df344fSAndy Whitcroft 1385a6962d72SJoe Perchessub get_quoted_string { 1386a6962d72SJoe Perches my ($line, $rawline) = @_; 1387a6962d72SJoe Perches 1388478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 138933acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1390a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1391a6962d72SJoe Perches} 1392a6962d72SJoe Perches 13938905a67cSAndy Whitcroftsub ctx_statement_block { 13948905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 13958905a67cSAndy Whitcroft my $line = $linenr - 1; 13968905a67cSAndy Whitcroft my $blk = ''; 13978905a67cSAndy Whitcroft my $soff = $off; 13988905a67cSAndy Whitcroft my $coff = $off - 1; 1399773647a0SAndy Whitcroft my $coff_set = 0; 14008905a67cSAndy Whitcroft 140113214adfSAndy Whitcroft my $loff = 0; 140213214adfSAndy Whitcroft 14038905a67cSAndy Whitcroft my $type = ''; 14048905a67cSAndy Whitcroft my $level = 0; 1405a2750645SAndy Whitcroft my @stack = (); 1406cf655043SAndy Whitcroft my $p; 14078905a67cSAndy Whitcroft my $c; 14088905a67cSAndy Whitcroft my $len = 0; 140913214adfSAndy Whitcroft 141013214adfSAndy Whitcroft my $remainder; 14118905a67cSAndy Whitcroft while (1) { 1412a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1413a2750645SAndy Whitcroft 1414773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 14158905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 14168905a67cSAndy Whitcroft # context. 14178905a67cSAndy Whitcroft if ($off >= $len) { 14188905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1419dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1420c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 14218905a67cSAndy Whitcroft $remain--; 142213214adfSAndy Whitcroft $loff = $len; 1423c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 14248905a67cSAndy Whitcroft $len = length($blk); 14258905a67cSAndy Whitcroft $line++; 14268905a67cSAndy Whitcroft last; 14278905a67cSAndy Whitcroft } 14288905a67cSAndy Whitcroft # Bail if there is no further context. 14298905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 143013214adfSAndy Whitcroft if ($off >= $len) { 14318905a67cSAndy Whitcroft last; 14328905a67cSAndy Whitcroft } 1433f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1434f74bd194SAndy Whitcroft $level++; 1435f74bd194SAndy Whitcroft $type = '#'; 1436f74bd194SAndy Whitcroft } 14378905a67cSAndy Whitcroft } 1438cf655043SAndy Whitcroft $p = $c; 14398905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 144013214adfSAndy Whitcroft $remainder = substr($blk, $off); 14418905a67cSAndy Whitcroft 1442773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 14434635f4fbSAndy Whitcroft 14444635f4fbSAndy Whitcroft # Handle nested #if/#else. 14454635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 14464635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 14474635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 14484635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 14494635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 14504635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 14514635f4fbSAndy Whitcroft } 14524635f4fbSAndy Whitcroft 14538905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 14548905a67cSAndy Whitcroft # outermost level. 14558905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 14568905a67cSAndy Whitcroft last; 14578905a67cSAndy Whitcroft } 14588905a67cSAndy Whitcroft 145913214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1460773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1461773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1462773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1463773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1464773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1465773647a0SAndy Whitcroft $coff_set = 1; 1466773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1467773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 146813214adfSAndy Whitcroft } 146913214adfSAndy Whitcroft 14708905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 14718905a67cSAndy Whitcroft $level++; 14728905a67cSAndy Whitcroft $type = '('; 14738905a67cSAndy Whitcroft } 14748905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 14758905a67cSAndy Whitcroft $level--; 14768905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 14778905a67cSAndy Whitcroft 14788905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 14798905a67cSAndy Whitcroft $coff = $off; 1480773647a0SAndy Whitcroft $coff_set = 1; 1481773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 14828905a67cSAndy Whitcroft } 14838905a67cSAndy Whitcroft } 14848905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 14858905a67cSAndy Whitcroft $level++; 14868905a67cSAndy Whitcroft $type = '{'; 14878905a67cSAndy Whitcroft } 14888905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 14898905a67cSAndy Whitcroft $level--; 14908905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 14918905a67cSAndy Whitcroft 14928905a67cSAndy Whitcroft if ($level == 0) { 1493b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1494b998e001SPatrick Pannuto $off++; 1495b998e001SPatrick Pannuto } 14968905a67cSAndy Whitcroft last; 14978905a67cSAndy Whitcroft } 14988905a67cSAndy Whitcroft } 1499f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1500f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1501f74bd194SAndy Whitcroft $level--; 1502f74bd194SAndy Whitcroft $type = ''; 1503f74bd194SAndy Whitcroft $off++; 1504f74bd194SAndy Whitcroft last; 1505f74bd194SAndy Whitcroft } 15068905a67cSAndy Whitcroft $off++; 15078905a67cSAndy Whitcroft } 1508a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 150913214adfSAndy Whitcroft if ($off == $len) { 1510a3bb97a7SAndy Whitcroft $loff = $len + 1; 151113214adfSAndy Whitcroft $line++; 151213214adfSAndy Whitcroft $remain--; 151313214adfSAndy Whitcroft } 15148905a67cSAndy Whitcroft 15158905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 15168905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 15178905a67cSAndy Whitcroft 15188905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 15198905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 15208905a67cSAndy Whitcroft 1521773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 152213214adfSAndy Whitcroft 152313214adfSAndy Whitcroft return ($statement, $condition, 152413214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 152513214adfSAndy Whitcroft} 152613214adfSAndy Whitcroft 1527cf655043SAndy Whitcroftsub statement_lines { 1528cf655043SAndy Whitcroft my ($stmt) = @_; 1529cf655043SAndy Whitcroft 1530cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1531cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1532cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1533cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1534cf655043SAndy Whitcroft 1535cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1536cf655043SAndy Whitcroft 1537cf655043SAndy Whitcroft return $#stmt_lines + 2; 1538cf655043SAndy Whitcroft} 1539cf655043SAndy Whitcroft 1540cf655043SAndy Whitcroftsub statement_rawlines { 1541cf655043SAndy Whitcroft my ($stmt) = @_; 1542cf655043SAndy Whitcroft 1543cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1544cf655043SAndy Whitcroft 1545cf655043SAndy Whitcroft return $#stmt_lines + 2; 1546cf655043SAndy Whitcroft} 1547cf655043SAndy Whitcroft 1548cf655043SAndy Whitcroftsub statement_block_size { 1549cf655043SAndy Whitcroft my ($stmt) = @_; 1550cf655043SAndy Whitcroft 1551cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1552cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1553cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1554cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1555cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1556cf655043SAndy Whitcroft 1557cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1558cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1559cf655043SAndy Whitcroft 1560cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1561cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1562cf655043SAndy Whitcroft 1563cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1564cf655043SAndy Whitcroft return $stmt_lines; 1565cf655043SAndy Whitcroft } else { 1566cf655043SAndy Whitcroft return $stmt_statements; 1567cf655043SAndy Whitcroft } 1568cf655043SAndy Whitcroft} 1569cf655043SAndy Whitcroft 157013214adfSAndy Whitcroftsub ctx_statement_full { 157113214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 157213214adfSAndy Whitcroft my ($statement, $condition, $level); 157313214adfSAndy Whitcroft 157413214adfSAndy Whitcroft my (@chunks); 157513214adfSAndy Whitcroft 1576cf655043SAndy Whitcroft # Grab the first conditional/block pair. 157713214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 157813214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1579773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 158013214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1581cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1582cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1583cf655043SAndy Whitcroft } 1584cf655043SAndy Whitcroft 1585cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1586cf655043SAndy Whitcroft # could continue the statement. 1587cf655043SAndy Whitcroft for (;;) { 158813214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 158913214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1590cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1591773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1592cf655043SAndy Whitcroft #print "C: push\n"; 1593cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 159413214adfSAndy Whitcroft } 159513214adfSAndy Whitcroft 159613214adfSAndy Whitcroft return ($level, $linenr, @chunks); 15978905a67cSAndy Whitcroft} 15988905a67cSAndy Whitcroft 15994a0df2efSAndy Whitcroftsub ctx_block_get { 1600f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 16014a0df2efSAndy Whitcroft my $line; 16024a0df2efSAndy Whitcroft my $start = $linenr - 1; 16034a0df2efSAndy Whitcroft my $blk = ''; 16044a0df2efSAndy Whitcroft my @o; 16054a0df2efSAndy Whitcroft my @c; 16064a0df2efSAndy Whitcroft my @res = (); 16074a0df2efSAndy Whitcroft 1608f0a594c1SAndy Whitcroft my $level = 0; 16094635f4fbSAndy Whitcroft my @stack = ($level); 161000df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 161100df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 161200df344fSAndy Whitcroft $remain--; 161300df344fSAndy Whitcroft 161400df344fSAndy Whitcroft $blk .= $rawlines[$line]; 16154635f4fbSAndy Whitcroft 16164635f4fbSAndy Whitcroft # Handle nested #if/#else. 161701464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 16184635f4fbSAndy Whitcroft push(@stack, $level); 161901464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 16204635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 162101464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 16224635f4fbSAndy Whitcroft $level = pop(@stack); 16234635f4fbSAndy Whitcroft } 16244635f4fbSAndy Whitcroft 162501464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1626f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1627f0a594c1SAndy Whitcroft if ($off > 0) { 1628f0a594c1SAndy Whitcroft $off--; 1629f0a594c1SAndy Whitcroft next; 1630f0a594c1SAndy Whitcroft } 16314a0df2efSAndy Whitcroft 1632f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1633f0a594c1SAndy Whitcroft $level--; 1634f0a594c1SAndy Whitcroft last if ($level == 0); 1635f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1636f0a594c1SAndy Whitcroft $level++; 1637f0a594c1SAndy Whitcroft } 1638f0a594c1SAndy Whitcroft } 16394a0df2efSAndy Whitcroft 1640f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 164100df344fSAndy Whitcroft push(@res, $rawlines[$line]); 16424a0df2efSAndy Whitcroft } 16434a0df2efSAndy Whitcroft 1644f0a594c1SAndy Whitcroft last if ($level == 0); 16454a0df2efSAndy Whitcroft } 16464a0df2efSAndy Whitcroft 1647f0a594c1SAndy Whitcroft return ($level, @res); 16484a0df2efSAndy Whitcroft} 16494a0df2efSAndy Whitcroftsub ctx_block_outer { 16504a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16514a0df2efSAndy Whitcroft 1652f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1653f0a594c1SAndy Whitcroft return @r; 16544a0df2efSAndy Whitcroft} 16554a0df2efSAndy Whitcroftsub ctx_block { 16564a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16574a0df2efSAndy Whitcroft 1658f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1659f0a594c1SAndy Whitcroft return @r; 1660653d4876SAndy Whitcroft} 1661653d4876SAndy Whitcroftsub ctx_statement { 1662f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1663f0a594c1SAndy Whitcroft 1664f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1665f0a594c1SAndy Whitcroft return @r; 1666f0a594c1SAndy Whitcroft} 1667f0a594c1SAndy Whitcroftsub ctx_block_level { 1668653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1669653d4876SAndy Whitcroft 1670f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 16714a0df2efSAndy Whitcroft} 16729c0ca6f9SAndy Whitcroftsub ctx_statement_level { 16739c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 16749c0ca6f9SAndy Whitcroft 16759c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 16769c0ca6f9SAndy Whitcroft} 16774a0df2efSAndy Whitcroft 16784a0df2efSAndy Whitcroftsub ctx_locate_comment { 16794a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16804a0df2efSAndy Whitcroft 1681a55ee0ccSJoe Perches # If c99 comment on the current line, or the line before or after 1682a55ee0ccSJoe Perches my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); 1683a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1684a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); 1685a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1686a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); 1687a55ee0ccSJoe Perches return $current_comment if (defined $current_comment); 1688a55ee0ccSJoe Perches 16894a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1690a55ee0ccSJoe Perches ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 16914a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 16924a0df2efSAndy Whitcroft 16934a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 16944a0df2efSAndy Whitcroft # comment. 16954a0df2efSAndy Whitcroft my $in_comment = 0; 16964a0df2efSAndy Whitcroft $current_comment = ''; 16974a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 169800df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 169900df344fSAndy Whitcroft #warn " $line\n"; 17004a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 17014a0df2efSAndy Whitcroft $in_comment = 1; 17024a0df2efSAndy Whitcroft } 17034a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 17044a0df2efSAndy Whitcroft $in_comment = 1; 17054a0df2efSAndy Whitcroft } 17064a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 17074a0df2efSAndy Whitcroft $current_comment = ''; 17084a0df2efSAndy Whitcroft } 17094a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 17104a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 17114a0df2efSAndy Whitcroft $in_comment = 0; 17124a0df2efSAndy Whitcroft } 17134a0df2efSAndy Whitcroft } 17144a0df2efSAndy Whitcroft 17154a0df2efSAndy Whitcroft chomp($current_comment); 17164a0df2efSAndy Whitcroft return($current_comment); 17174a0df2efSAndy Whitcroft} 17184a0df2efSAndy Whitcroftsub ctx_has_comment { 17194a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17204a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 17214a0df2efSAndy Whitcroft 172200df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 17234a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 17244a0df2efSAndy Whitcroft 17254a0df2efSAndy Whitcroft return ($cmt ne ''); 17264a0df2efSAndy Whitcroft} 17274a0df2efSAndy Whitcroft 17284d001e4dSAndy Whitcroftsub raw_line { 17294d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 17304d001e4dSAndy Whitcroft 17314d001e4dSAndy Whitcroft my $offset = $linenr - 1; 17324d001e4dSAndy Whitcroft $cnt++; 17334d001e4dSAndy Whitcroft 17344d001e4dSAndy Whitcroft my $line; 17354d001e4dSAndy Whitcroft while ($cnt) { 17364d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 17374d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 17384d001e4dSAndy Whitcroft $cnt--; 17394d001e4dSAndy Whitcroft } 17404d001e4dSAndy Whitcroft 17414d001e4dSAndy Whitcroft return $line; 17424d001e4dSAndy Whitcroft} 17434d001e4dSAndy Whitcroft 17442a9f9d85STobin C. Hardingsub get_stat_real { 17452a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 17462a9f9d85STobin C. Harding 17472a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 17482a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 17492a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 17502a9f9d85STobin C. Harding } 17512a9f9d85STobin C. Harding 17522a9f9d85STobin C. Harding return $stat_real; 17532a9f9d85STobin C. Harding} 17542a9f9d85STobin C. Harding 1755e3d95a2aSTobin C. Hardingsub get_stat_here { 1756e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1757e3d95a2aSTobin C. Harding 1758e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1759e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1760e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1761e3d95a2aSTobin C. Harding } 1762e3d95a2aSTobin C. Harding 1763e3d95a2aSTobin C. Harding return $herectx; 1764e3d95a2aSTobin C. Harding} 1765e3d95a2aSTobin C. Harding 17660a920b5bSAndy Whitcroftsub cat_vet { 17670a920b5bSAndy Whitcroft my ($vet) = @_; 17689c0ca6f9SAndy Whitcroft my ($res, $coded); 17690a920b5bSAndy Whitcroft 17709c0ca6f9SAndy Whitcroft $res = ''; 17716c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 17726c72ffaaSAndy Whitcroft $res .= $1; 17736c72ffaaSAndy Whitcroft if ($2 ne '') { 17749c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 17756c72ffaaSAndy Whitcroft $res .= $coded; 17766c72ffaaSAndy Whitcroft } 17779c0ca6f9SAndy Whitcroft } 17789c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 17790a920b5bSAndy Whitcroft 17809c0ca6f9SAndy Whitcroft return $res; 17810a920b5bSAndy Whitcroft} 17820a920b5bSAndy Whitcroft 1783c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1784cf655043SAndy Whitcroftmy $av_pending; 1785c2fdda0dSAndy Whitcroftmy @av_paren_type; 17861f65f947SAndy Whitcroftmy $av_pend_colon; 1787c2fdda0dSAndy Whitcroft 1788c2fdda0dSAndy Whitcroftsub annotate_reset { 1789c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1790cf655043SAndy Whitcroft $av_pending = '_'; 1791cf655043SAndy Whitcroft @av_paren_type = ('E'); 17921f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1793c2fdda0dSAndy Whitcroft} 1794c2fdda0dSAndy Whitcroft 17956c72ffaaSAndy Whitcroftsub annotate_values { 17966c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 17976c72ffaaSAndy Whitcroft 17986c72ffaaSAndy Whitcroft my $res; 17991f65f947SAndy Whitcroft my $var = '_' x length($stream); 18006c72ffaaSAndy Whitcroft my $cur = $stream; 18016c72ffaaSAndy Whitcroft 1802c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 18036c72ffaaSAndy Whitcroft 18046c72ffaaSAndy Whitcroft while (length($cur)) { 1805773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1806cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1807171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 18086c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1809c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1810c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1811cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1812c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 18136c72ffaaSAndy Whitcroft } 18146c72ffaaSAndy Whitcroft 1815c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 18169446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 18179446ef56SAndy Whitcroft push(@av_paren_type, $type); 1818addcdceaSAndy Whitcroft $type = 'c'; 18199446ef56SAndy Whitcroft 1820e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1821c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 18226c72ffaaSAndy Whitcroft $type = 'T'; 18236c72ffaaSAndy Whitcroft 1824389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1825389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1826389a2fe5SAndy Whitcroft $type = 'T'; 1827389a2fe5SAndy Whitcroft 1828c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1829171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1830c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1831171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1832171ae1a4SAndy Whitcroft if ($2 ne '') { 1833cf655043SAndy Whitcroft $av_pending = 'N'; 1834171ae1a4SAndy Whitcroft } 1835171ae1a4SAndy Whitcroft $type = 'E'; 1836171ae1a4SAndy Whitcroft 1837c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1838171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1839171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1840171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 18416c72ffaaSAndy Whitcroft 1842c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1843cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1844c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1845cf655043SAndy Whitcroft 1846cf655043SAndy Whitcroft push(@av_paren_type, $type); 1847cf655043SAndy Whitcroft push(@av_paren_type, $type); 1848171ae1a4SAndy Whitcroft $type = 'E'; 1849cf655043SAndy Whitcroft 1850c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1851cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1852cf655043SAndy Whitcroft $av_preprocessor = 1; 1853cf655043SAndy Whitcroft 1854cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1855cf655043SAndy Whitcroft 1856171ae1a4SAndy Whitcroft $type = 'E'; 1857cf655043SAndy Whitcroft 1858c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1859cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1860cf655043SAndy Whitcroft 1861cf655043SAndy Whitcroft $av_preprocessor = 1; 1862cf655043SAndy Whitcroft 1863cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1864cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1865cf655043SAndy Whitcroft pop(@av_paren_type); 1866cf655043SAndy Whitcroft push(@av_paren_type, $type); 1867171ae1a4SAndy Whitcroft $type = 'E'; 18686c72ffaaSAndy Whitcroft 18696c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1870c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 18716c72ffaaSAndy Whitcroft 1872171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1873171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1874171ae1a4SAndy Whitcroft $av_pending = $type; 1875171ae1a4SAndy Whitcroft $type = 'N'; 1876171ae1a4SAndy Whitcroft 18776c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1878c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 18796c72ffaaSAndy Whitcroft if (defined $2) { 1880cf655043SAndy Whitcroft $av_pending = 'V'; 18816c72ffaaSAndy Whitcroft } 18826c72ffaaSAndy Whitcroft $type = 'N'; 18836c72ffaaSAndy Whitcroft 188414b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1885c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 188614b111c1SAndy Whitcroft $av_pending = 'E'; 18876c72ffaaSAndy Whitcroft $type = 'N'; 18886c72ffaaSAndy Whitcroft 18891f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 18901f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 18911f65f947SAndy Whitcroft $av_pend_colon = 'C'; 18921f65f947SAndy Whitcroft $type = 'N'; 18931f65f947SAndy Whitcroft 189414b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1895c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 18966c72ffaaSAndy Whitcroft $type = 'N'; 18976c72ffaaSAndy Whitcroft 18986c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1899c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1900cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1901cf655043SAndy Whitcroft $av_pending = '_'; 19026c72ffaaSAndy Whitcroft $type = 'N'; 19036c72ffaaSAndy Whitcroft 19046c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1905cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1906cf655043SAndy Whitcroft if ($new_type ne '_') { 1907cf655043SAndy Whitcroft $type = $new_type; 1908c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1909c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 19106c72ffaaSAndy Whitcroft } else { 1911c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 19126c72ffaaSAndy Whitcroft } 19136c72ffaaSAndy Whitcroft 1914c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1915c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1916c8cb2ca3SAndy Whitcroft $type = 'V'; 1917cf655043SAndy Whitcroft $av_pending = 'V'; 19186c72ffaaSAndy Whitcroft 19198e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 19208e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 19211f65f947SAndy Whitcroft $av_pend_colon = 'B'; 19228e761b04SAndy Whitcroft } elsif ($type eq 'E') { 19238e761b04SAndy Whitcroft $av_pend_colon = 'L'; 19241f65f947SAndy Whitcroft } 19251f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 19261f65f947SAndy Whitcroft $type = 'V'; 19271f65f947SAndy Whitcroft 19286c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1929c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 19306c72ffaaSAndy Whitcroft $type = 'V'; 19316c72ffaaSAndy Whitcroft 19326c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1933c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 19346c72ffaaSAndy Whitcroft $type = 'N'; 19356c72ffaaSAndy Whitcroft 1936cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1937c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 193813214adfSAndy Whitcroft $type = 'E'; 19391f65f947SAndy Whitcroft $av_pend_colon = 'O'; 194013214adfSAndy Whitcroft 19418e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 19428e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 19438e761b04SAndy Whitcroft $type = 'C'; 19448e761b04SAndy Whitcroft 19451f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 19461f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 19471f65f947SAndy Whitcroft $type = 'N'; 19481f65f947SAndy Whitcroft 19491f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 19501f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 19511f65f947SAndy Whitcroft 19521f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 19531f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 19541f65f947SAndy Whitcroft $type = 'E'; 19551f65f947SAndy Whitcroft } else { 19561f65f947SAndy Whitcroft $type = 'N'; 19571f65f947SAndy Whitcroft } 19581f65f947SAndy Whitcroft $av_pend_colon = 'O'; 19591f65f947SAndy Whitcroft 19608e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 196113214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 19626c72ffaaSAndy Whitcroft $type = 'N'; 19636c72ffaaSAndy Whitcroft 19640d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 196574048ed8SAndy Whitcroft my $variant; 196674048ed8SAndy Whitcroft 196774048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 196874048ed8SAndy Whitcroft if ($type eq 'V') { 196974048ed8SAndy Whitcroft $variant = 'B'; 197074048ed8SAndy Whitcroft } else { 197174048ed8SAndy Whitcroft $variant = 'U'; 197274048ed8SAndy Whitcroft } 197374048ed8SAndy Whitcroft 197474048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 197574048ed8SAndy Whitcroft $type = 'N'; 197674048ed8SAndy Whitcroft 19776c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1978c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 19796c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 19806c72ffaaSAndy Whitcroft $type = 'N'; 19816c72ffaaSAndy Whitcroft } 19826c72ffaaSAndy Whitcroft 19836c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1984c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 19856c72ffaaSAndy Whitcroft } 19866c72ffaaSAndy Whitcroft if (defined $1) { 19876c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 19886c72ffaaSAndy Whitcroft $res .= $type x length($1); 19896c72ffaaSAndy Whitcroft } 19906c72ffaaSAndy Whitcroft } 19916c72ffaaSAndy Whitcroft 19921f65f947SAndy Whitcroft return ($res, $var); 19936c72ffaaSAndy Whitcroft} 19946c72ffaaSAndy Whitcroft 19958905a67cSAndy Whitcroftsub possible { 199613214adfSAndy Whitcroft my ($possible, $line) = @_; 19979a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 19980776e594SAndy Whitcroft ^(?: 19990776e594SAndy Whitcroft $Modifier| 20000776e594SAndy Whitcroft $Storage| 20010776e594SAndy Whitcroft $Type| 20029a974fdbSAndy Whitcroft DEFINE_\S+ 20039a974fdbSAndy Whitcroft )$| 20049a974fdbSAndy Whitcroft ^(?: 20050776e594SAndy Whitcroft goto| 20060776e594SAndy Whitcroft return| 20070776e594SAndy Whitcroft case| 20080776e594SAndy Whitcroft else| 20090776e594SAndy Whitcroft asm|__asm__| 201089a88353SAndy Whitcroft do| 201189a88353SAndy Whitcroft \#| 201289a88353SAndy Whitcroft \#\#| 20139a974fdbSAndy Whitcroft )(?:\s|$)| 20140776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 20159a974fdbSAndy Whitcroft )}x; 20169a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 20179a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2018c45dcabdSAndy Whitcroft # Check for modifiers. 2019c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2020c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2021c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2022c45dcabdSAndy Whitcroft 2023c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2024c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2025d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 20269a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2027d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2028485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2029d2506586SAndy Whitcroft } 20309a974fdbSAndy Whitcroft } 2031c45dcabdSAndy Whitcroft 2032c45dcabdSAndy Whitcroft } else { 203313214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2034485ff23eSAlex Dowad push(@typeListFile, $possible); 2035c45dcabdSAndy Whitcroft } 20368905a67cSAndy Whitcroft build_types(); 20370776e594SAndy Whitcroft } else { 20380776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 20398905a67cSAndy Whitcroft } 20408905a67cSAndy Whitcroft} 20418905a67cSAndy Whitcroft 20426c72ffaaSAndy Whitcroftmy $prefix = ''; 20436c72ffaaSAndy Whitcroft 2044000d1cc1SJoe Perchessub show_type { 2045cbec18afSJoe Perches my ($type) = @_; 204691bfe484SJoe Perches 2047522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2048522b837cSAlexey Dobriyan 2049cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2050cbec18afSJoe Perches 2051cbec18afSJoe Perches return !defined $ignore_type{$type}; 2052000d1cc1SJoe Perches} 2053000d1cc1SJoe Perches 2054f0a594c1SAndy Whitcroftsub report { 2055cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2056cbec18afSJoe Perches 2057cbec18afSJoe Perches if (!show_type($type) || 2058cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2059773647a0SAndy Whitcroft return 0; 2060773647a0SAndy Whitcroft } 206157230297SJoe Perches my $output = ''; 2062737c0767SJohn Brooks if ($color) { 206357230297SJoe Perches if ($level eq 'ERROR') { 206457230297SJoe Perches $output .= RED; 206557230297SJoe Perches } elsif ($level eq 'WARNING') { 206657230297SJoe Perches $output .= YELLOW; 2067000d1cc1SJoe Perches } else { 206857230297SJoe Perches $output .= GREEN; 2069000d1cc1SJoe Perches } 207057230297SJoe Perches } 207157230297SJoe Perches $output .= $prefix . $level . ':'; 207257230297SJoe Perches if ($show_types) { 2073737c0767SJohn Brooks $output .= BLUE if ($color); 207457230297SJoe Perches $output .= "$type:"; 207557230297SJoe Perches } 2076737c0767SJohn Brooks $output .= RESET if ($color); 207757230297SJoe Perches $output .= ' ' . $msg . "\n"; 207834d8815fSJoe Perches 207934d8815fSJoe Perches if ($showfile) { 208034d8815fSJoe Perches my @lines = split("\n", $output, -1); 208134d8815fSJoe Perches splice(@lines, 1, 1); 208234d8815fSJoe Perches $output = join("\n", @lines); 208334d8815fSJoe Perches } 208457230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 20858905a67cSAndy Whitcroft 208657230297SJoe Perches push(our @report, $output); 2087773647a0SAndy Whitcroft 2088773647a0SAndy Whitcroft return 1; 2089f0a594c1SAndy Whitcroft} 2090cbec18afSJoe Perches 2091f0a594c1SAndy Whitcroftsub report_dump { 209213214adfSAndy Whitcroft our @report; 2093f0a594c1SAndy Whitcroft} 2094000d1cc1SJoe Perches 2095d752fcc8SJoe Perchessub fixup_current_range { 2096d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2097d752fcc8SJoe Perches 2098d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2099d752fcc8SJoe Perches my $o = $1; 2100d752fcc8SJoe Perches my $l = $2; 2101d752fcc8SJoe Perches my $no = $o + $offset; 2102d752fcc8SJoe Perches my $nl = $l + $length; 2103d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2104d752fcc8SJoe Perches } 2105d752fcc8SJoe Perches} 2106d752fcc8SJoe Perches 2107d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2108d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2109d752fcc8SJoe Perches 2110d752fcc8SJoe Perches my $range_last_linenr = 0; 2111d752fcc8SJoe Perches my $delta_offset = 0; 2112d752fcc8SJoe Perches 2113d752fcc8SJoe Perches my $old_linenr = 0; 2114d752fcc8SJoe Perches my $new_linenr = 0; 2115d752fcc8SJoe Perches 2116d752fcc8SJoe Perches my $next_insert = 0; 2117d752fcc8SJoe Perches my $next_delete = 0; 2118d752fcc8SJoe Perches 2119d752fcc8SJoe Perches my @lines = (); 2120d752fcc8SJoe Perches 2121d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2122d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2123d752fcc8SJoe Perches 2124d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2125d752fcc8SJoe Perches my $save_line = 1; 2126d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2127323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2128d752fcc8SJoe Perches $delta_offset = 0; 2129d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2130d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2131d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2132d752fcc8SJoe Perches } 2133d752fcc8SJoe Perches 2134d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2135d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2136d752fcc8SJoe Perches $save_line = 0; 2137d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2138d752fcc8SJoe Perches } 2139d752fcc8SJoe Perches 2140d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2141d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2142d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2143d752fcc8SJoe Perches $new_linenr++; 2144d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2145d752fcc8SJoe Perches } 2146d752fcc8SJoe Perches 2147d752fcc8SJoe Perches if ($save_line) { 2148d752fcc8SJoe Perches push(@lines, $line); 2149d752fcc8SJoe Perches $new_linenr++; 2150d752fcc8SJoe Perches } 2151d752fcc8SJoe Perches 2152d752fcc8SJoe Perches $old_linenr++; 2153d752fcc8SJoe Perches } 2154d752fcc8SJoe Perches 2155d752fcc8SJoe Perches return @lines; 2156d752fcc8SJoe Perches} 2157d752fcc8SJoe Perches 2158f2d7e4d4SJoe Perchessub fix_insert_line { 2159f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2160f2d7e4d4SJoe Perches 2161f2d7e4d4SJoe Perches my $inserted = { 2162f2d7e4d4SJoe Perches LINENR => $linenr, 2163f2d7e4d4SJoe Perches LINE => $line, 2164f2d7e4d4SJoe Perches }; 2165f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2166f2d7e4d4SJoe Perches} 2167f2d7e4d4SJoe Perches 2168f2d7e4d4SJoe Perchessub fix_delete_line { 2169f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2170f2d7e4d4SJoe Perches 2171f2d7e4d4SJoe Perches my $deleted = { 2172f2d7e4d4SJoe Perches LINENR => $linenr, 2173f2d7e4d4SJoe Perches LINE => $line, 2174f2d7e4d4SJoe Perches }; 2175f2d7e4d4SJoe Perches 2176f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2177f2d7e4d4SJoe Perches} 2178f2d7e4d4SJoe Perches 2179de7d4f0eSAndy Whitcroftsub ERROR { 2180cbec18afSJoe Perches my ($type, $msg) = @_; 2181cbec18afSJoe Perches 2182cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2183de7d4f0eSAndy Whitcroft our $clean = 0; 21846c72ffaaSAndy Whitcroft our $cnt_error++; 21853705ce5bSJoe Perches return 1; 2186de7d4f0eSAndy Whitcroft } 21873705ce5bSJoe Perches return 0; 2188773647a0SAndy Whitcroft} 2189de7d4f0eSAndy Whitcroftsub WARN { 2190cbec18afSJoe Perches my ($type, $msg) = @_; 2191cbec18afSJoe Perches 2192cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2193de7d4f0eSAndy Whitcroft our $clean = 0; 21946c72ffaaSAndy Whitcroft our $cnt_warn++; 21953705ce5bSJoe Perches return 1; 2196de7d4f0eSAndy Whitcroft } 21973705ce5bSJoe Perches return 0; 2198773647a0SAndy Whitcroft} 2199de7d4f0eSAndy Whitcroftsub CHK { 2200cbec18afSJoe Perches my ($type, $msg) = @_; 2201cbec18afSJoe Perches 2202cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2203de7d4f0eSAndy Whitcroft our $clean = 0; 22046c72ffaaSAndy Whitcroft our $cnt_chk++; 22053705ce5bSJoe Perches return 1; 22066c72ffaaSAndy Whitcroft } 22073705ce5bSJoe Perches return 0; 2208de7d4f0eSAndy Whitcroft} 2209de7d4f0eSAndy Whitcroft 22106ecd9674SAndy Whitcroftsub check_absolute_file { 22116ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 22126ecd9674SAndy Whitcroft my $file = $absolute; 22136ecd9674SAndy Whitcroft 22146ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 22156ecd9674SAndy Whitcroft 22166ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 22176ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 22186ecd9674SAndy Whitcroft if (-f "$root/$file") { 22196ecd9674SAndy Whitcroft ##print "file<$file>\n"; 22206ecd9674SAndy Whitcroft last; 22216ecd9674SAndy Whitcroft } 22226ecd9674SAndy Whitcroft } 22236ecd9674SAndy Whitcroft if (! -f _) { 22246ecd9674SAndy Whitcroft return 0; 22256ecd9674SAndy Whitcroft } 22266ecd9674SAndy Whitcroft 22276ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 22286ecd9674SAndy Whitcroft my $prefix = $absolute; 22296ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 22306ecd9674SAndy Whitcroft 22316ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 22326ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2233000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2234000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 22356ecd9674SAndy Whitcroft } 22366ecd9674SAndy Whitcroft} 22376ecd9674SAndy Whitcroft 22383705ce5bSJoe Perchessub trim { 22393705ce5bSJoe Perches my ($string) = @_; 22403705ce5bSJoe Perches 2241b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2242b34c648bSJoe Perches 2243b34c648bSJoe Perches return $string; 2244b34c648bSJoe Perches} 2245b34c648bSJoe Perches 2246b34c648bSJoe Perchessub ltrim { 2247b34c648bSJoe Perches my ($string) = @_; 2248b34c648bSJoe Perches 2249b34c648bSJoe Perches $string =~ s/^\s+//; 2250b34c648bSJoe Perches 2251b34c648bSJoe Perches return $string; 2252b34c648bSJoe Perches} 2253b34c648bSJoe Perches 2254b34c648bSJoe Perchessub rtrim { 2255b34c648bSJoe Perches my ($string) = @_; 2256b34c648bSJoe Perches 2257b34c648bSJoe Perches $string =~ s/\s+$//; 22583705ce5bSJoe Perches 22593705ce5bSJoe Perches return $string; 22603705ce5bSJoe Perches} 22613705ce5bSJoe Perches 226252ea8506SJoe Perchessub string_find_replace { 226352ea8506SJoe Perches my ($string, $find, $replace) = @_; 226452ea8506SJoe Perches 226552ea8506SJoe Perches $string =~ s/$find/$replace/g; 226652ea8506SJoe Perches 226752ea8506SJoe Perches return $string; 226852ea8506SJoe Perches} 226952ea8506SJoe Perches 22703705ce5bSJoe Perchessub tabify { 22713705ce5bSJoe Perches my ($leading) = @_; 22723705ce5bSJoe Perches 2273713a09deSAntonio Borneo my $source_indent = $tabsize; 22743705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 22753705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 22763705ce5bSJoe Perches 22773705ce5bSJoe Perches #convert leading spaces to tabs 22783705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 22793705ce5bSJoe Perches #Remove spaces before a tab 22803705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 22813705ce5bSJoe Perches 22823705ce5bSJoe Perches return "$leading"; 22833705ce5bSJoe Perches} 22843705ce5bSJoe Perches 2285d1fe9c09SJoe Perchessub pos_last_openparen { 2286d1fe9c09SJoe Perches my ($line) = @_; 2287d1fe9c09SJoe Perches 2288d1fe9c09SJoe Perches my $pos = 0; 2289d1fe9c09SJoe Perches 2290d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2291d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2292d1fe9c09SJoe Perches 2293d1fe9c09SJoe Perches my $last_openparen = 0; 2294d1fe9c09SJoe Perches 2295d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2296d1fe9c09SJoe Perches return -1; 2297d1fe9c09SJoe Perches } 2298d1fe9c09SJoe Perches 2299d1fe9c09SJoe Perches my $len = length($line); 2300d1fe9c09SJoe Perches 2301d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2302d1fe9c09SJoe Perches my $string = substr($line, $pos); 2303d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2304d1fe9c09SJoe Perches $pos += length($1) - 1; 2305d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2306d1fe9c09SJoe Perches $last_openparen = $pos; 2307d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2308d1fe9c09SJoe Perches last; 2309d1fe9c09SJoe Perches } 2310d1fe9c09SJoe Perches } 2311d1fe9c09SJoe Perches 231291cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2313d1fe9c09SJoe Perches} 2314d1fe9c09SJoe Perches 2315f36d3eb8SJoe Perchessub get_raw_comment { 2316f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2317f36d3eb8SJoe Perches my $comment = ''; 2318f36d3eb8SJoe Perches 2319f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2320f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2321f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2322f36d3eb8SJoe Perches } 2323f36d3eb8SJoe Perches } 2324f36d3eb8SJoe Perches 2325f36d3eb8SJoe Perches return $comment; 2326f36d3eb8SJoe Perches} 2327f36d3eb8SJoe Perches 23280a920b5bSAndy Whitcroftsub process { 23290a920b5bSAndy Whitcroft my $filename = shift; 23300a920b5bSAndy Whitcroft 23310a920b5bSAndy Whitcroft my $linenr=0; 23320a920b5bSAndy Whitcroft my $prevline=""; 2333c2fdda0dSAndy Whitcroft my $prevrawline=""; 23340a920b5bSAndy Whitcroft my $stashline=""; 2335c2fdda0dSAndy Whitcroft my $stashrawline=""; 23360a920b5bSAndy Whitcroft 23374a0df2efSAndy Whitcroft my $length; 23380a920b5bSAndy Whitcroft my $indent; 23390a920b5bSAndy Whitcroft my $previndent=0; 23400a920b5bSAndy Whitcroft my $stashindent=0; 23410a920b5bSAndy Whitcroft 2342de7d4f0eSAndy Whitcroft our $clean = 1; 23430a920b5bSAndy Whitcroft my $signoff = 0; 2344cd261496SGeert Uytterhoeven my $author = ''; 2345cd261496SGeert Uytterhoeven my $authorsignoff = 0; 23460a920b5bSAndy Whitcroft my $is_patch = 0; 2347133712a2SRob Herring my $is_binding_patch = -1; 234829ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 234915662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 235044d303ebSJoe Perches my $has_patch_separator = 0; #Found a --- line 2351ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2352490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2353bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 23542a076f40SJoe Perches my $commit_log_long_line = 0; 2355e518e9a5SJoe Perches my $commit_log_has_diff = 0; 235613f1937eSJoe Perches my $reported_maintainer_file = 0; 2357fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2358fa64205dSPasi Savanainen 2359365dd4eaSJoe Perches my $last_blank_line = 0; 23605e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2361365dd4eaSJoe Perches 236213214adfSAndy Whitcroft our @report = (); 23636c72ffaaSAndy Whitcroft our $cnt_lines = 0; 23646c72ffaaSAndy Whitcroft our $cnt_error = 0; 23656c72ffaaSAndy Whitcroft our $cnt_warn = 0; 23666c72ffaaSAndy Whitcroft our $cnt_chk = 0; 23676c72ffaaSAndy Whitcroft 23680a920b5bSAndy Whitcroft # Trace the real file/line as we go. 23690a920b5bSAndy Whitcroft my $realfile = ''; 23700a920b5bSAndy Whitcroft my $realline = 0; 23710a920b5bSAndy Whitcroft my $realcnt = 0; 23720a920b5bSAndy Whitcroft my $here = ''; 237377cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 23740a920b5bSAndy Whitcroft my $in_comment = 0; 2375c2fdda0dSAndy Whitcroft my $comment_edge = 0; 23760a920b5bSAndy Whitcroft my $first_line = 0; 23771e855726SWolfram Sang my $p1_prefix = ''; 23780a920b5bSAndy Whitcroft 237913214adfSAndy Whitcroft my $prev_values = 'E'; 238013214adfSAndy Whitcroft 238113214adfSAndy Whitcroft # suppression flags 2382773647a0SAndy Whitcroft my %suppress_ifbraces; 2383170d3a22SAndy Whitcroft my %suppress_whiletrailers; 23842b474a1aSAndy Whitcroft my %suppress_export; 23853e469cdcSAndy Whitcroft my $suppress_statement = 0; 2386653d4876SAndy Whitcroft 23877e51f197SJoe Perches my %signatures = (); 2388323c1260SJoe Perches 2389c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2390de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2391c2fdda0dSAndy Whitcroft # 2392de7d4f0eSAndy Whitcroft my @setup_docs = (); 2393de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2394773647a0SAndy Whitcroft 2395d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2396d8b07710SJoe Perches 23979f3a8992SRob Herring my $checklicenseline = 1; 23989f3a8992SRob Herring 2399773647a0SAndy Whitcroft sanitise_line_reset(); 2400c2fdda0dSAndy Whitcroft my $line; 2401c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2402773647a0SAndy Whitcroft $linenr++; 2403773647a0SAndy Whitcroft $line = $rawline; 2404c2fdda0dSAndy Whitcroft 24053705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 24063705ce5bSJoe Perches 2407773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2408de7d4f0eSAndy Whitcroft $setup_docs = 0; 24092581ac7cSTim Froidcoeur if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { 2410de7d4f0eSAndy Whitcroft $setup_docs = 1; 2411de7d4f0eSAndy Whitcroft } 2412773647a0SAndy Whitcroft #next; 2413de7d4f0eSAndy Whitcroft } 241474fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2415773647a0SAndy Whitcroft $realline=$1-1; 2416773647a0SAndy Whitcroft if (defined $2) { 2417773647a0SAndy Whitcroft $realcnt=$3+1; 2418773647a0SAndy Whitcroft } else { 2419773647a0SAndy Whitcroft $realcnt=1+1; 2420773647a0SAndy Whitcroft } 2421c45dcabdSAndy Whitcroft $in_comment = 0; 2422773647a0SAndy Whitcroft 2423773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2424773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2425773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2426773647a0SAndy Whitcroft # at context start. 2427773647a0SAndy Whitcroft my $edge; 242801fa9147SAndy Whitcroft my $cnt = $realcnt; 242901fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 243001fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 243101fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 243201fa9147SAndy Whitcroft $cnt--; 243301fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2434721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2435fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2436fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2437fae17daeSAndy Whitcroft ($edge) = $1; 2438fae17daeSAndy Whitcroft last; 2439fae17daeSAndy Whitcroft } 2440773647a0SAndy Whitcroft } 2441773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2442773647a0SAndy Whitcroft $in_comment = 1; 2443773647a0SAndy Whitcroft } 2444773647a0SAndy Whitcroft 2445773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2446773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2447773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2448773647a0SAndy Whitcroft if (!defined $edge && 244983242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2450773647a0SAndy Whitcroft { 2451773647a0SAndy Whitcroft $in_comment = 1; 2452773647a0SAndy Whitcroft } 2453773647a0SAndy Whitcroft 2454773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2455773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2456773647a0SAndy Whitcroft 2457171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2458773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2459171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2460773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2461773647a0SAndy Whitcroft } 2462773647a0SAndy Whitcroft push(@lines, $line); 2463773647a0SAndy Whitcroft 2464773647a0SAndy Whitcroft if ($realcnt > 1) { 2465773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2466773647a0SAndy Whitcroft } else { 2467773647a0SAndy Whitcroft $realcnt = 0; 2468773647a0SAndy Whitcroft } 2469773647a0SAndy Whitcroft 2470773647a0SAndy Whitcroft #print "==>$rawline\n"; 2471773647a0SAndy Whitcroft #print "-->$line\n"; 2472de7d4f0eSAndy Whitcroft 2473de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2474de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2475de7d4f0eSAndy Whitcroft } 2476de7d4f0eSAndy Whitcroft } 2477de7d4f0eSAndy Whitcroft 24786c72ffaaSAndy Whitcroft $prefix = ''; 24796c72ffaaSAndy Whitcroft 2480773647a0SAndy Whitcroft $realcnt = 0; 2481773647a0SAndy Whitcroft $linenr = 0; 2482194f66fcSJoe Perches $fixlinenr = -1; 24830a920b5bSAndy Whitcroft foreach my $line (@lines) { 24840a920b5bSAndy Whitcroft $linenr++; 2485194f66fcSJoe Perches $fixlinenr++; 24861b5539b1SJoe Perches my $sline = $line; #copy of $line 24871b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 24880a920b5bSAndy Whitcroft 2489c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2490f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 24916c72ffaaSAndy Whitcroft 249212c253abSJoe Perches# check if it's a mode change, rename or start of a patch 249312c253abSJoe Perches if (!$in_commit_log && 249412c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 249512c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 249612c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 249712c253abSJoe Perches $is_patch = 1; 249812c253abSJoe Perches } 249912c253abSJoe Perches 25000a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2501e518e9a5SJoe Perches if (!$in_commit_log && 250274fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 250374fd4f34SJoe Perches my $context = $4; 25040a920b5bSAndy Whitcroft $is_patch = 1; 25054a0df2efSAndy Whitcroft $first_line = $linenr + 1; 25060a920b5bSAndy Whitcroft $realline=$1-1; 25070a920b5bSAndy Whitcroft if (defined $2) { 25080a920b5bSAndy Whitcroft $realcnt=$3+1; 25090a920b5bSAndy Whitcroft } else { 25100a920b5bSAndy Whitcroft $realcnt=1+1; 25110a920b5bSAndy Whitcroft } 2512c2fdda0dSAndy Whitcroft annotate_reset(); 251313214adfSAndy Whitcroft $prev_values = 'E'; 251413214adfSAndy Whitcroft 2515773647a0SAndy Whitcroft %suppress_ifbraces = (); 2516170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 25172b474a1aSAndy Whitcroft %suppress_export = (); 25183e469cdcSAndy Whitcroft $suppress_statement = 0; 251974fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 252074fd4f34SJoe Perches $context_function = $1; 252174fd4f34SJoe Perches } else { 252274fd4f34SJoe Perches undef $context_function; 252374fd4f34SJoe Perches } 25240a920b5bSAndy Whitcroft next; 25250a920b5bSAndy Whitcroft 25264a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 25274a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 25284a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2529773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 25300a920b5bSAndy Whitcroft $realline++; 2531d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 25320a920b5bSAndy Whitcroft 25334a0df2efSAndy Whitcroft # Measure the line length and indent. 2534c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 25350a920b5bSAndy Whitcroft 25360a920b5bSAndy Whitcroft # Track the previous line. 25370a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 25380a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2539c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2540c2fdda0dSAndy Whitcroft 2541773647a0SAndy Whitcroft #warn "line<$line>\n"; 25426c72ffaaSAndy Whitcroft 2543d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2544d8aaf121SAndy Whitcroft $realcnt--; 25450a920b5bSAndy Whitcroft } 25460a920b5bSAndy Whitcroft 2547cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2548cc77cdcaSAndy Whitcroft 25496c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 25506c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2551773647a0SAndy Whitcroft 25522ac73b4fSJoe Perches my $found_file = 0; 2553773647a0SAndy Whitcroft # extract the filename as it passes 25543bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 25553bf9a009SRabin Vincent $realfile = $1; 25562b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2557270c49a0SJoe Perches $in_commit_log = 0; 25582ac73b4fSJoe Perches $found_file = 1; 25593bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2560773647a0SAndy Whitcroft $realfile = $1; 25612b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2562270c49a0SJoe Perches $in_commit_log = 0; 25631e855726SWolfram Sang 25641e855726SWolfram Sang $p1_prefix = $1; 2565e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2566e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2567000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2568000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 25691e855726SWolfram Sang } 2570773647a0SAndy Whitcroft 2571c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2572000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2573000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2574773647a0SAndy Whitcroft } 25752ac73b4fSJoe Perches $found_file = 1; 25762ac73b4fSJoe Perches } 25772ac73b4fSJoe Perches 257834d8815fSJoe Perches#make up the handle for any error we report on this line 257934d8815fSJoe Perches if ($showfile) { 258034d8815fSJoe Perches $prefix = "$realfile:$realline: " 258134d8815fSJoe Perches } elsif ($emacs) { 25827d3a9f67SJoe Perches if ($file) { 25837d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 25847d3a9f67SJoe Perches } else { 258534d8815fSJoe Perches $prefix = "$filename:$linenr: "; 258634d8815fSJoe Perches } 25877d3a9f67SJoe Perches } 258834d8815fSJoe Perches 25892ac73b4fSJoe Perches if ($found_file) { 259085b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 259185b0ee18SJoe Perches WARN("OBSOLETE", 259285b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 259385b0ee18SJoe Perches } 25947bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 25952ac73b4fSJoe Perches $check = 1; 25962ac73b4fSJoe Perches } else { 25972ac73b4fSJoe Perches $check = $check_orig; 25982ac73b4fSJoe Perches } 25999f3a8992SRob Herring $checklicenseline = 1; 2600133712a2SRob Herring 2601133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2602133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2603133712a2SRob Herring 2604133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2605133712a2SRob Herring 2606133712a2SRob Herring if (($last_binding_patch != -1) && 2607133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2608133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2609858e6845SMauro Carvalho Chehab "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); 2610133712a2SRob Herring } 2611133712a2SRob Herring } 2612133712a2SRob Herring 2613773647a0SAndy Whitcroft next; 2614773647a0SAndy Whitcroft } 2615773647a0SAndy Whitcroft 2616389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 26170a920b5bSAndy Whitcroft 2618c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2619c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2620c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 26210a920b5bSAndy Whitcroft 26226c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 26236c72ffaaSAndy Whitcroft 2624490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2625490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2626490b292cSJoe Perches if ($in_commit_log) { 2627490b292cSJoe Perches if ($line !~ /^\s*$/) { 2628490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2629490b292cSJoe Perches } 2630490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2631490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2632490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2633490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2634490b292cSJoe Perches } 2635490b292cSJoe Perches 2636e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2637e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2638e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2639e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2640e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2641e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2642e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2643e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2644e518e9a5SJoe Perches $commit_log_has_diff = 1; 2645e518e9a5SJoe Perches } 2646e518e9a5SJoe Perches 26473bf9a009SRabin Vincent# Check for incorrect file permissions 26483bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 26493bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 265004db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 265104db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2652000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2653000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 26543bf9a009SRabin Vincent } 26553bf9a009SRabin Vincent } 26563bf9a009SRabin Vincent 2657cd261496SGeert Uytterhoeven# Check the patch for a From: 2658cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2659cd261496SGeert Uytterhoeven $author = $1; 2660cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2661cd261496SGeert Uytterhoeven $author =~ s/"//g; 2662dfa05c28SJoe Perches $author = reformat_email($author); 2663cd261496SGeert Uytterhoeven } 2664cd261496SGeert Uytterhoeven 266520112475SJoe Perches# Check the patch for a signoff: 2666dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 26674a0df2efSAndy Whitcroft $signoff++; 266815662b3eSJoe Perches $in_commit_log = 0; 2669cd261496SGeert Uytterhoeven if ($author ne '') { 2670dfa05c28SJoe Perches if (same_email_addresses($1, $author)) { 2671cd261496SGeert Uytterhoeven $authorsignoff = 1; 2672cd261496SGeert Uytterhoeven } 2673cd261496SGeert Uytterhoeven } 26740a920b5bSAndy Whitcroft } 267520112475SJoe Perches 267644d303ebSJoe Perches# Check for patch separator 267744d303ebSJoe Perches if ($line =~ /^---$/) { 267844d303ebSJoe Perches $has_patch_separator = 1; 267944d303ebSJoe Perches $in_commit_log = 0; 268044d303ebSJoe Perches } 268144d303ebSJoe Perches 2682e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2683e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2684e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2685e0d975b1SJoe Perches $reported_maintainer_file = 1; 2686e0d975b1SJoe Perches } 2687e0d975b1SJoe Perches 268820112475SJoe Perches# Check signature styles 2689270c49a0SJoe Perches if (!$in_header_lines && 2690ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 269120112475SJoe Perches my $space_before = $1; 269220112475SJoe Perches my $sign_off = $2; 269320112475SJoe Perches my $space_after = $3; 269420112475SJoe Perches my $email = $4; 269520112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 269620112475SJoe Perches 2697ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2698ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2699ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2700ce0338dfSJoe Perches } 270120112475SJoe Perches if (defined $space_before && $space_before ne "") { 27023705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27033705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 27043705ce5bSJoe Perches $fix) { 2705194f66fcSJoe Perches $fixed[$fixlinenr] = 27063705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27073705ce5bSJoe Perches } 270820112475SJoe Perches } 270920112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 27103705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27113705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 27123705ce5bSJoe Perches $fix) { 2713194f66fcSJoe Perches $fixed[$fixlinenr] = 27143705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27153705ce5bSJoe Perches } 27163705ce5bSJoe Perches 271720112475SJoe Perches } 271820112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 27193705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27203705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 27213705ce5bSJoe Perches $fix) { 2722194f66fcSJoe Perches $fixed[$fixlinenr] = 27233705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27243705ce5bSJoe Perches } 272520112475SJoe Perches } 272620112475SJoe Perches 2727dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 272820112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 272920112475SJoe Perches if ($suggested_email eq "") { 2730000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2731000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 273220112475SJoe Perches } else { 273320112475SJoe Perches my $dequoted = $suggested_email; 273420112475SJoe Perches $dequoted =~ s/^"//; 273520112475SJoe Perches $dequoted =~ s/" </ </; 273620112475SJoe Perches # Don't force email to have quotes 273720112475SJoe Perches # Allow just an angle bracketed address 2738dfa05c28SJoe Perches if (!same_email_addresses($email, $suggested_email)) { 2739000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2740000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 274120112475SJoe Perches } 27420a920b5bSAndy Whitcroft } 27437e51f197SJoe Perches 27447e51f197SJoe Perches# Check for duplicate signatures 27457e51f197SJoe Perches my $sig_nospace = $line; 27467e51f197SJoe Perches $sig_nospace =~ s/\s//g; 27477e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 27487e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 27497e51f197SJoe Perches WARN("BAD_SIGN_OFF", 27507e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 27517e51f197SJoe Perches } else { 27527e51f197SJoe Perches $signatures{$sig_nospace} = 1; 27537e51f197SJoe Perches } 27546c5d24eeSSean Christopherson 27556c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 27566c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 27576c5d24eeSSean Christopherson if ($email eq $author) { 27586c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27596c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 27606c5d24eeSSean Christopherson } 27616c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 27626c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27636c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 27646c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 27656c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27666c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27676c5d24eeSSean Christopherson } elsif ($1 ne $email) { 27686c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27696c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27706c5d24eeSSean Christopherson } 27716c5d24eeSSean Christopherson } 27720a920b5bSAndy Whitcroft } 27730a920b5bSAndy Whitcroft 2774a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2775a2fe16b9SJoe Perches if ($in_header_lines && 2776a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2777a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2778a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2779a2fe16b9SJoe Perches } 2780a2fe16b9SJoe Perches 278144d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context 278244d303ebSJoe Perches if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 27837ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 278444d303ebSJoe Perches "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr); 27857ebd05efSChristopher Covington } 27867ebd05efSChristopher Covington 2787369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2788369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2789369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2790369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2791369c8dd3SJoe Perches # timestamp 2792634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 2793634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 2794634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 2795634cffccSJoe Perches # stack dump address styles 2796369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2797369c8dd3SJoe Perches } 2798369c8dd3SJoe Perches 27992a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 28002a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2801bf4daf12SJoe Perches length($line) > 75 && 2802bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2803bf4daf12SJoe Perches # file delta changes 2804bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2805bf4daf12SJoe Perches # filename then : 2806bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2807bf4daf12SJoe Perches # A Fixes: or Link: line 2808bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 28092a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 28102a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 28112a076f40SJoe Perches $commit_log_long_line = 1; 28122a076f40SJoe Perches } 28132a076f40SJoe Perches 2814bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2815bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2816bf4daf12SJoe Perches $line =~ /^\s*$/) { 2817bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2818bf4daf12SJoe Perches } 2819bf4daf12SJoe Perches 28200d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2821369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2822a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 2823e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2824fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2825aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2826369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2827bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2828fe043ea1SJoe Perches my $init_char = "c"; 2829fe043ea1SJoe Perches my $orig_commit = ""; 28300d7835fcSJoe Perches my $short = 1; 28310d7835fcSJoe Perches my $long = 0; 28320d7835fcSJoe Perches my $case = 1; 28330d7835fcSJoe Perches my $space = 1; 28340d7835fcSJoe Perches my $hasdesc = 0; 283519c146a6SJoe Perches my $hasparens = 0; 28360d7835fcSJoe Perches my $id = '0123456789ab'; 28370d7835fcSJoe Perches my $orig_desc = "commit description"; 28380d7835fcSJoe Perches my $description = ""; 28390d7835fcSJoe Perches 2840fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2841fe043ea1SJoe Perches $init_char = $1; 2842fe043ea1SJoe Perches $orig_commit = lc($2); 2843fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2844fe043ea1SJoe Perches $orig_commit = lc($1); 2845fe043ea1SJoe Perches } 2846fe043ea1SJoe Perches 28470d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 28480d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 28490d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 28500d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 28510d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 28520d7835fcSJoe Perches $orig_desc = $1; 285319c146a6SJoe Perches $hasparens = 1; 28540d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 28550d7835fcSJoe Perches defined $rawlines[$linenr] && 28560d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 28570d7835fcSJoe Perches $orig_desc = $1; 285819c146a6SJoe Perches $hasparens = 1; 2859b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2860b671fde0SJoe Perches defined $rawlines[$linenr] && 2861b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2862b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2863b671fde0SJoe Perches $orig_desc = $1; 2864b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2865b671fde0SJoe Perches $orig_desc .= " " . $1; 286619c146a6SJoe Perches $hasparens = 1; 28670d7835fcSJoe Perches } 28680d7835fcSJoe Perches 28690d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 28700d7835fcSJoe Perches $id, $orig_desc); 28710d7835fcSJoe Perches 2872948b133aSHeinrich Schuchardt if (defined($id) && 2873948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2874d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 28750d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 28760d7835fcSJoe Perches } 2877d311cd44SJoe Perches } 2878d311cd44SJoe Perches 287913f1937eSJoe Perches# Check for added, moved or deleted files 288013f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 288113f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 288213f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 288313f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 288413f1937eSJoe Perches (defined($1) || defined($2))))) { 2885a82603a8SAndrew Jeffery $is_patch = 1; 288613f1937eSJoe Perches $reported_maintainer_file = 1; 288713f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 288813f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 288913f1937eSJoe Perches } 289013f1937eSJoe Perches 2891e400edb1SRob Herring# Check for adding new DT bindings not in schema format 2892e400edb1SRob Herring if (!$in_commit_log && 2893e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 2894e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 2895e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 2896e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 2897e400edb1SRob Herring } 2898e400edb1SRob Herring 289900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 29008905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2901000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2902000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 29036c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2904de7d4f0eSAndy Whitcroft } 2905de7d4f0eSAndy Whitcroft 2906de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2907de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2908171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2909171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2910171ae1a4SAndy Whitcroft 2911171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2912171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2913171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2914171ae1a4SAndy Whitcroft 291534d99219SJoe Perches CHK("INVALID_UTF8", 2916000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 291700df344fSAndy Whitcroft } 29180a920b5bSAndy Whitcroft 291915662b3eSJoe Perches# Check if it's the start of a commit log 292015662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 292115662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2922eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2923eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 292415662b3eSJoe Perches $in_header_lines = 0; 292515662b3eSJoe Perches $in_commit_log = 1; 2926ed43c4e5SAllen Hubbe $has_commit_log = 1; 292715662b3eSJoe Perches } 292815662b3eSJoe Perches 2929fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2930fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2931fa64205dSPasi Savanainen if ($in_header_lines && 2932fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2933fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2934fa64205dSPasi Savanainen $non_utf8_charset = 1; 2935fa64205dSPasi Savanainen } 2936fa64205dSPasi Savanainen 2937fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 293815662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2939fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 294015662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 294115662b3eSJoe Perches } 294215662b3eSJoe Perches 2943d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2944d6430f71SJoe Perches if ($tree && $in_commit_log) { 2945d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2946d6430f71SJoe Perches my $file = $1; 2947d6430f71SJoe Perches 2948d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2949d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2950d6430f71SJoe Perches # 2951d6430f71SJoe Perches } else { 2952d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2953d6430f71SJoe Perches } 2954d6430f71SJoe Perches } 2955d6430f71SJoe Perches } 2956d6430f71SJoe Perches 295766b47b4aSKees Cook# Check for various typo / spelling mistakes 295866d7a382SJoe Perches if (defined($misspellings) && 295966d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2960ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 296166b47b4aSKees Cook my $typo = $1; 296266b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 296366b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 296466b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 29650675a8fbSJean Delvare my $msg_level = \&WARN; 29660675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29670675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 296866b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 296966b47b4aSKees Cook $fix) { 297066b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 297166b47b4aSKees Cook } 297266b47b4aSKees Cook } 297366b47b4aSKees Cook } 297466b47b4aSKees Cook 2975a8dd86bfSMatteo Croce# check for invalid commit id 2976a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 2977a8dd86bfSMatteo Croce my $id; 2978a8dd86bfSMatteo Croce my $description; 2979a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 2980a8dd86bfSMatteo Croce if (!defined($id)) { 2981a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 2982a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 2983a8dd86bfSMatteo Croce } 2984a8dd86bfSMatteo Croce } 2985a8dd86bfSMatteo Croce 298630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 298730670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 298800df344fSAndy Whitcroft 29890a920b5bSAndy Whitcroft#trailing whitespace 29909c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2991c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2992d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2993d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2994d5e616fcSJoe Perches $fix) { 2995194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2996d5e616fcSJoe Perches } 2997c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2998c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29993705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 30003705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 30013705ce5bSJoe Perches $fix) { 3002194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 30033705ce5bSJoe Perches } 30043705ce5bSJoe Perches 3005d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 30060a920b5bSAndy Whitcroft } 30075368df20SAndy Whitcroft 30084783f894SJosh Triplett# Check for FSF mailing addresses. 3009109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 30101bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 30113e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 30123e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 30134783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 30140675a8fbSJean Delvare my $msg_level = \&ERROR; 30150675a8fbSJean Delvare $msg_level = \&CHK if ($file); 30160675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 30174783f894SJosh 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) 30184783f894SJosh Triplett } 30194783f894SJosh Triplett 30203354957aSAndi Kleen# check for Kconfig help text having a real description 30219fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 30229fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 30233354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 3024678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3025678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3026678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3027678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 30283354957aSAndi Kleen my $length = 0; 30299fe287d7SAndy Whitcroft my $cnt = $realcnt; 30309fe287d7SAndy Whitcroft my $ln = $linenr + 1; 30319fe287d7SAndy Whitcroft my $f; 3032a1385803SAndy Whitcroft my $is_start = 0; 30339fe287d7SAndy Whitcroft my $is_end = 0; 3034a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 30359fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 30369fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 30379fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 30389fe287d7SAndy Whitcroft 30399fe287d7SAndy Whitcroft next if ($f =~ /^-/); 30408d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3041a1385803SAndy Whitcroft 304286adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3043a1385803SAndy Whitcroft $is_start = 1; 304484af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 304584af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 304684af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 304784af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 304884af7a61SUlf Magnusson } 3049a1385803SAndy Whitcroft $length = -1; 3050a1385803SAndy Whitcroft } 3051a1385803SAndy Whitcroft 30529fe287d7SAndy Whitcroft $f =~ s/^.//; 30533354957aSAndi Kleen $f =~ s/#.*//; 30543354957aSAndi Kleen $f =~ s/^\s+//; 30553354957aSAndi Kleen next if ($f =~ /^$/); 3056678ae162SUlf Magnusson 3057678ae162SUlf Magnusson # This only checks context lines in the patch 3058678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3059678ae162SUlf Magnusson # positives, even though some of these are 3060678ae162SUlf Magnusson # common words in help texts 3061678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3062678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 30639fe287d7SAndy Whitcroft $is_end = 1; 30649fe287d7SAndy Whitcroft last; 30659fe287d7SAndy Whitcroft } 30663354957aSAndi Kleen $length++; 30673354957aSAndi Kleen } 306856193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3069000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 307056193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 307156193274SVadim Bendebury } 3072a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 30733354957aSAndi Kleen } 30743354957aSAndi Kleen 30757ccf41a8SJoe Perches# check MAINTAINERS entries 30767ccf41a8SJoe Perches if ($realfile =~ /^MAINTAINERS$/) { 30777ccf41a8SJoe Perches# check MAINTAINERS entries for the right form 30787ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 3079628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3080628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3081628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3082628f91a2SJoe Perches $fix) { 3083628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3084628f91a2SJoe Perches } 3085628f91a2SJoe Perches } 30867ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too 30877ccf41a8SJoe Perches my $preferred_order = 'MRLSWQBCPTFXNK'; 30887ccf41a8SJoe Perches if ($rawline =~ /^\+[A-Z]:/ && 30897ccf41a8SJoe Perches $prevrawline =~ /^[\+ ][A-Z]:/) { 30907ccf41a8SJoe Perches $rawline =~ /^\+([A-Z]):\s*(.*)/; 30917ccf41a8SJoe Perches my $cur = $1; 30927ccf41a8SJoe Perches my $curval = $2; 30937ccf41a8SJoe Perches $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; 30947ccf41a8SJoe Perches my $prev = $1; 30957ccf41a8SJoe Perches my $prevval = $2; 30967ccf41a8SJoe Perches my $curindex = index($preferred_order, $cur); 30977ccf41a8SJoe Perches my $previndex = index($preferred_order, $prev); 30987ccf41a8SJoe Perches if ($curindex < 0) { 30997ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 31007ccf41a8SJoe Perches "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); 31017ccf41a8SJoe Perches } else { 31027ccf41a8SJoe Perches if ($previndex >= 0 && $curindex < $previndex) { 31037ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 31047ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); 31057ccf41a8SJoe Perches } elsif ((($prev eq 'F' && $cur eq 'F') || 31067ccf41a8SJoe Perches ($prev eq 'X' && $cur eq 'X')) && 31077ccf41a8SJoe Perches ($prevval cmp $curval) > 0) { 31087ccf41a8SJoe Perches WARN("MAINTAINERS_STYLE", 31097ccf41a8SJoe Perches "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); 31107ccf41a8SJoe Perches } 31117ccf41a8SJoe Perches } 31127ccf41a8SJoe Perches } 31137ccf41a8SJoe Perches } 3114628f91a2SJoe Perches 3115327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3116327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3117327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3118327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3119327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3120327953e9SChristoph Jaeger } 3121327953e9SChristoph Jaeger 3122c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3123c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3124c68e5878SArnaud Lacombe my $flag = $1; 3125c68e5878SArnaud Lacombe my $replacement = { 3126c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3127c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3128c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3129c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3130c68e5878SArnaud Lacombe }; 3131c68e5878SArnaud Lacombe 3132c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3133c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3134c68e5878SArnaud Lacombe } 3135c68e5878SArnaud Lacombe 3136bff5da43SRob Herring# check for DT compatible documentation 31377dd05b38SFlorian Vaussard if (defined $root && 31387dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 31397dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 31407dd05b38SFlorian Vaussard 3141bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3142bff5da43SRob Herring 3143cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3144852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3145cc93319bSFlorian Vaussard 3146bff5da43SRob Herring foreach my $compat (@compats) { 3147bff5da43SRob Herring my $compat2 = $compat; 3148185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3149185d566bSRob Herring my $compat3 = $compat; 3150185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3151185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3152bff5da43SRob Herring if ( $? >> 8 ) { 3153bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3154bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3155bff5da43SRob Herring } 3156bff5da43SRob Herring 31574fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 31584fbf32a6SFlorian Vaussard my $vendor = $1; 3159852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3160bff5da43SRob Herring if ( $? >> 8 ) { 3161bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3162cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3163bff5da43SRob Herring } 3164bff5da43SRob Herring } 3165bff5da43SRob Herring } 3166bff5da43SRob Herring 31679f3a8992SRob Herring# check for using SPDX license tag at beginning of files 31689f3a8992SRob Herring if ($realline == $checklicenseline) { 31699f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 31709f3a8992SRob Herring $checklicenseline = 2; 31719f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 31729f3a8992SRob Herring my $comment = ""; 31739f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 31749f3a8992SRob Herring $comment = '/*'; 31759f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 31769f3a8992SRob Herring $comment = '//'; 3177c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 31789f3a8992SRob Herring $comment = '#'; 31799f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 31809f3a8992SRob Herring $comment = '..'; 31819f3a8992SRob Herring } 31829f3a8992SRob Herring 3183fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3184fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3185fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3186ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3187fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3188fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3189fdf13693SJoe Perches } 3190fdf13693SJoe Perches 31919f3a8992SRob Herring if ($comment !~ /^$/ && 3192ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 31939f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 31949f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 31953b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 31963b6e8ac9SJoe Perches my $spdx_license = $1; 31973b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 31983b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 31993b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 32003b6e8ac9SJoe Perches } 320150c92900SLubomir Rintel if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 320250c92900SLubomir Rintel not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 320350c92900SLubomir Rintel my $msg_level = \&WARN; 320450c92900SLubomir Rintel $msg_level = \&CHK if ($file); 320550c92900SLubomir Rintel if (&{$msg_level}("SPDX_LICENSE_TAG", 320650c92900SLubomir Rintel 320750c92900SLubomir Rintel "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 320850c92900SLubomir Rintel $fix) { 320950c92900SLubomir Rintel $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 321050c92900SLubomir Rintel } 321150c92900SLubomir Rintel } 32129f3a8992SRob Herring } 32139f3a8992SRob Herring } 32149f3a8992SRob Herring } 32159f3a8992SRob Herring 32165368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3217d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 32185368df20SAndy Whitcroft 3219a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3220a8da38a9SJoe Perches if ($realline != $checklicenseline && 3221a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3222a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3223a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3224a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3225a8da38a9SJoe Perches } 3226a8da38a9SJoe Perches 322747e0c88bSJoe Perches# line length limit (with some exclusions) 322847e0c88bSJoe Perches# 322947e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 323047e0c88bSJoe Perches# logging functions like pr_info that end in a string 323147e0c88bSJoe Perches# lines with a single string 323247e0c88bSJoe Perches# #defines that are a single string 32332e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 323447e0c88bSJoe Perches# 323547e0c88bSJoe Perches# There are 3 different line length message types: 3236ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 323747e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 323847e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 323947e0c88bSJoe Perches# 324047e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 324147e0c88bSJoe Perches# 324247e0c88bSJoe Perches 3243b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 324447e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 324547e0c88bSJoe Perches 324647e0c88bSJoe Perches # Check the allowed long line types first 324747e0c88bSJoe Perches 324847e0c88bSJoe Perches # logging functions that end in a string that starts 324947e0c88bSJoe Perches # before $max_line_length 325047e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 325147e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 325247e0c88bSJoe Perches $msg_type = ""; 325347e0c88bSJoe Perches 325447e0c88bSJoe Perches # lines with only strings (w/ possible termination) 325547e0c88bSJoe Perches # #defines with only strings 325647e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 325747e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 325847e0c88bSJoe Perches $msg_type = ""; 325947e0c88bSJoe Perches 3260cc147506SJoe Perches # More special cases 3261cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3262cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3263d560a5f8SJoe Perches $msg_type = ""; 3264d560a5f8SJoe Perches 32652e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 32662e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 32672e4bbbc5SAndreas Brauchli $msg_type = ""; 32682e4bbbc5SAndreas Brauchli 326947e0c88bSJoe Perches # Otherwise set the alternate message types 327047e0c88bSJoe Perches 327147e0c88bSJoe Perches # a comment starts before $max_line_length 327247e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 327347e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 327447e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 327547e0c88bSJoe Perches 327647e0c88bSJoe Perches # a quoted string starts before $max_line_length 327747e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 327847e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 327947e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 328047e0c88bSJoe Perches } 328147e0c88bSJoe Perches 328247e0c88bSJoe Perches if ($msg_type ne "" && 328347e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 3284bdc48fa1SJoe Perches my $msg_level = \&WARN; 3285bdc48fa1SJoe Perches $msg_level = \&CHK if ($file); 3286bdc48fa1SJoe Perches &{$msg_level}($msg_type, 3287bdc48fa1SJoe Perches "line length of $length exceeds $max_line_length columns\n" . $herecurr); 32880a920b5bSAndy Whitcroft } 328947e0c88bSJoe Perches } 32900a920b5bSAndy Whitcroft 32918905a67cSAndy Whitcroft# check for adding lines without a newline. 32928905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3293000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3294000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 32958905a67cSAndy Whitcroft } 32968905a67cSAndy Whitcroft 3297b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3298de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 32990a920b5bSAndy Whitcroft 33000a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3301713a09deSAntonio Borneo# more than $tabsize must use tabs. 3302c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3303c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3304c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3305d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 33063705ce5bSJoe Perches if (ERROR("CODE_INDENT", 33073705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 33083705ce5bSJoe Perches $fix) { 3309194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 33103705ce5bSJoe Perches } 33110a920b5bSAndy Whitcroft } 33120a920b5bSAndy Whitcroft 331308e44365SAlberto Panizzo# check for space before tabs. 331408e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 331508e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 33163705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 33173705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 33183705ce5bSJoe Perches $fix) { 3319194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3320713a09deSAntonio Borneo s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3321194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3322c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 33233705ce5bSJoe Perches } 332408e44365SAlberto Panizzo } 332508e44365SAlberto Panizzo 33266a487211SJoe Perches# check for assignments on the start of a line 33276a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 33286a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 33296a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 33306a487211SJoe Perches } 33316a487211SJoe Perches 3332d1fe9c09SJoe Perches# check for && or || at the start of a line 3333d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3334d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3335d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3336d1fe9c09SJoe Perches } 3337d1fe9c09SJoe Perches 3338a91e8994SJoe Perches# check indentation starts on a tab stop 33395b57980dSJoe Perches if ($perl_version_ok && 3340bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3341a91e8994SJoe Perches my $indent = length($1); 3342713a09deSAntonio Borneo if ($indent % $tabsize) { 3343a91e8994SJoe Perches if (WARN("TABSTOP", 3344a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3345a91e8994SJoe Perches $fix) { 3346713a09deSAntonio Borneo $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3347a91e8994SJoe Perches } 3348a91e8994SJoe Perches } 3349a91e8994SJoe Perches } 3350a91e8994SJoe Perches 3351d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 33525b57980dSJoe Perches if ($perl_version_ok && 3353fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3354d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3355d1fe9c09SJoe Perches my $oldindent = $1; 3356d1fe9c09SJoe Perches my $rest = $2; 3357d1fe9c09SJoe Perches 3358d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3359d1fe9c09SJoe Perches if ($pos >= 0) { 3360b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3361b34a26f3SJoe Perches my $newindent = $2; 3362d1fe9c09SJoe Perches 3363d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3364713a09deSAntonio Borneo "\t" x ($pos / $tabsize) . 3365713a09deSAntonio Borneo " " x ($pos % $tabsize); 3366d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3367d1fe9c09SJoe Perches 3368d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3369d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 33703705ce5bSJoe Perches 33713705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 33723705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 33733705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3374194f66fcSJoe Perches $fixed[$fixlinenr] =~ 33753705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 33763705ce5bSJoe Perches } 3377d1fe9c09SJoe Perches } 3378d1fe9c09SJoe Perches } 3379d1fe9c09SJoe Perches } 3380d1fe9c09SJoe Perches 33816ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 33826ab3a970SJoe Perches# avoid checking a few false positives: 33836ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 33846ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 33856ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 33866ab3a970SJoe Perches# multiline macros that define functions 33876ab3a970SJoe Perches# known attributes or the __attribute__ keyword 33886ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 33896ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 33903705ce5bSJoe Perches if (CHK("SPACING", 3391f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 33923705ce5bSJoe Perches $fix) { 3393194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3394f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 33953705ce5bSJoe Perches } 3396aad4f614SJoe Perches } 3397aad4f614SJoe Perches 339886406b1cSJoe Perches# Block comment styles 339986406b1cSJoe Perches# Networking with an initial /* 340005880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3401fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 340285ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 340385ad978cSJoe Perches $realline > 2) { 340405880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 340505880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 340605880600SJoe Perches } 340705880600SJoe Perches 340886406b1cSJoe Perches# Block comments use * on subsequent lines 340986406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 341086406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3411a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 341261135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3413a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 341486406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 341586406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3416a605e32eSJoe Perches } 3417a605e32eSJoe Perches 341886406b1cSJoe Perches# Block comments use */ on trailing lines 341986406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3420c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3421c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3422c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 342386406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 342486406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 342505880600SJoe Perches } 342605880600SJoe Perches 342708eb9b80SJoe Perches# Block comment * alignment 342808eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3429af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3430af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3431af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 343208eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3433af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3434af207524SJoe Perches my $oldindent; 343508eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3436af207524SJoe Perches if (defined($1)) { 3437af207524SJoe Perches $oldindent = expand_tabs($1); 3438af207524SJoe Perches } else { 3439af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3440af207524SJoe Perches $oldindent = expand_tabs($1); 3441af207524SJoe Perches } 344208eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 344308eb9b80SJoe Perches my $newindent = $1; 344408eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3445af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 344608eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 344708eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 344808eb9b80SJoe Perches } 344908eb9b80SJoe Perches } 345008eb9b80SJoe Perches 34517f619191SJoe Perches# check for missing blank lines after struct/union declarations 34527f619191SJoe Perches# with exceptions for various attributes and macros 34537f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 34547f619191SJoe Perches $line =~ /^\+/ && 34557f619191SJoe Perches !($line =~ /^\+\s*$/ || 34567f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 34577f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 34587f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 34597f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 34607f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 34617f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 34620bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 34637f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3464d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3465d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3466d752fcc8SJoe Perches $fix) { 3467f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3468d752fcc8SJoe Perches } 34697f619191SJoe Perches } 34707f619191SJoe Perches 3471365dd4eaSJoe Perches# check for multiple consecutive blank lines 3472365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3473365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3474365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3475d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3476d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3477d752fcc8SJoe Perches $fix) { 3478f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3479d752fcc8SJoe Perches } 3480d752fcc8SJoe Perches 3481365dd4eaSJoe Perches $last_blank_line = $linenr; 3482365dd4eaSJoe Perches } 3483365dd4eaSJoe Perches 34843b617e3bSJoe Perches# check for missing blank lines after declarations 34853f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 34863f7bac03SJoe Perches # actual declarations 34873f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34885a4e1fd3SJoe Perches # function pointer declarations 34895a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34903f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34913f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34923f7bac03SJoe Perches # known declaration macros 34933f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 34943f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 34953f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 34963f7bac03SJoe Perches # other possible extensions of declaration lines 34973f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 34983f7bac03SJoe Perches # not starting a section or a macro "\" extended line 34993f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 35003f7bac03SJoe Perches # looks like a declaration 35013f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 35025a4e1fd3SJoe Perches # function pointer declarations 35035a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 35043f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 35053f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 35063f7bac03SJoe Perches # known declaration macros 35073f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 35083f7bac03SJoe Perches # start of struct or union or enum 3509328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 35103f7bac03SJoe Perches # start or end of block or continuation of declaration 35113f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 35123f7bac03SJoe Perches # bitfield continuation 35133f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 35143f7bac03SJoe Perches # other possible extensions of declaration lines 35153f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 35163f7bac03SJoe Perches # indentation of previous and current line are the same 35173f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3518d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3519d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3520d752fcc8SJoe Perches $fix) { 3521f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3522d752fcc8SJoe Perches } 35233b617e3bSJoe Perches } 35243b617e3bSJoe Perches 35255f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 35266b4c5bebSAndy Whitcroft# Exceptions: 35276b4c5bebSAndy Whitcroft# 1) within comments 35286b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 35296b4c5bebSAndy Whitcroft# 3) hanging labels 35303705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 35315f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 35323705ce5bSJoe Perches if (WARN("LEADING_SPACE", 35333705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 35343705ce5bSJoe Perches $fix) { 3535194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 35363705ce5bSJoe Perches } 35375f7ddae6SRaffaele Recalcati } 35385f7ddae6SRaffaele Recalcati 3539b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3540b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3541b9ea10d6SAndy Whitcroft 35425751a24eSJoe Perches# check for unusual line ending [ or ( 35435751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 35445751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 35455751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 35465751a24eSJoe Perches } 35475751a24eSJoe Perches 35484dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 35494dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 35504dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 35514dbed76fSJoe Perches $context_function = $1; 35524dbed76fSJoe Perches } 35534dbed76fSJoe Perches 35544dbed76fSJoe Perches# check if this appears to be the end of function declaration 35554dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 35564dbed76fSJoe Perches undef $context_function; 35574dbed76fSJoe Perches } 35584dbed76fSJoe Perches 3559032a4c0fSJoe Perches# check indentation of any line with a bare else 3560840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3561032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3562032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3563032a4c0fSJoe Perches my $tabs = length($1) + 1; 3564840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3565840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3566840080a0SJoe Perches defined $lines[$linenr] && 3567840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3568032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3569032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3570032a4c0fSJoe Perches } 3571032a4c0fSJoe Perches } 3572032a4c0fSJoe Perches 3573c00df19aSJoe Perches# check indentation of a line with a break; 3574c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3575c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3576c00df19aSJoe Perches my $tabs = $1; 3577c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3578c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3579c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3580c00df19aSJoe Perches } 3581c00df19aSJoe Perches } 3582c00df19aSJoe Perches 3583c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3584cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3585000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3586000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3587c2fdda0dSAndy Whitcroft } 358822f2a2efSAndy Whitcroft 358956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 359056e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 359156e77d70SJoe Perches WARN("HOTPLUG_SECTION", 359256e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 359356e77d70SJoe Perches } 359456e77d70SJoe Perches 35959c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 35962b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 35972b474a1aSAndy Whitcroft $realline_next); 35983e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3599ca819864SJoe Perches if ($linenr > $suppress_statement && 36001b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3601170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3602f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3603171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3604171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3605171ae1a4SAndy Whitcroft 36063e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 36073e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 36083e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 36093e469cdcSAndy Whitcroft # until we hit end of it. 36103e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 36113e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 36123e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 36133e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 36143e469cdcSAndy Whitcroft } 3615f74bd194SAndy Whitcroft 36162b474a1aSAndy Whitcroft # Find the real next line. 36172b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 36182b474a1aSAndy Whitcroft if (defined $realline_next && 36192b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 36202b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 36212b474a1aSAndy Whitcroft $realline_next++; 36222b474a1aSAndy Whitcroft } 36232b474a1aSAndy Whitcroft 3624171ae1a4SAndy Whitcroft my $s = $stat; 3625171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3626cf655043SAndy Whitcroft 3627c2fdda0dSAndy Whitcroft # Ignore goto labels. 3628171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3629c2fdda0dSAndy Whitcroft 3630c2fdda0dSAndy Whitcroft # Ignore functions being called 3631171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3632c2fdda0dSAndy Whitcroft 3633463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3634463f2864SAndy Whitcroft 3635c45dcabdSAndy Whitcroft # declarations always start with types 3636d2506586SAndy 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) { 3637c45dcabdSAndy Whitcroft my $type = $1; 3638c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3639c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3640c45dcabdSAndy Whitcroft 36416c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3642a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3643c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3644c2fdda0dSAndy Whitcroft } 36458905a67cSAndy Whitcroft 36466c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 364765863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3648c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 36499c0ca6f9SAndy Whitcroft } 36508905a67cSAndy Whitcroft 36518905a67cSAndy Whitcroft # Check for any sort of function declaration. 36528905a67cSAndy Whitcroft # int foo(something bar, other baz); 36538905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3654171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 36558905a67cSAndy Whitcroft my ($name_len) = length($1); 36568905a67cSAndy Whitcroft 3657cf655043SAndy Whitcroft my $ctx = $s; 3658773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 36598905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3660cf655043SAndy Whitcroft 36618905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3662c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 36638905a67cSAndy Whitcroft 3664c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 36658905a67cSAndy Whitcroft } 36668905a67cSAndy Whitcroft } 36678905a67cSAndy Whitcroft } 36688905a67cSAndy Whitcroft 36699c0ca6f9SAndy Whitcroft } 36709c0ca6f9SAndy Whitcroft 367100df344fSAndy Whitcroft# 367200df344fSAndy Whitcroft# Checks which may be anchored in the context. 367300df344fSAndy Whitcroft# 367400df344fSAndy Whitcroft 367500df344fSAndy Whitcroft# Check for switch () and associated case and default 367600df344fSAndy Whitcroft# statements should be at the same indent. 367700df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 367800df344fSAndy Whitcroft my $err = ''; 367900df344fSAndy Whitcroft my $sep = ''; 368000df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 368100df344fSAndy Whitcroft shift(@ctx); 368200df344fSAndy Whitcroft for my $ctx (@ctx) { 368300df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 368400df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 368500df344fSAndy Whitcroft $indent != $cindent) { 368600df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 368700df344fSAndy Whitcroft $sep = ''; 368800df344fSAndy Whitcroft } else { 368900df344fSAndy Whitcroft $sep = "[...]\n"; 369000df344fSAndy Whitcroft } 369100df344fSAndy Whitcroft } 369200df344fSAndy Whitcroft if ($err ne '') { 3693000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3694000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3695de7d4f0eSAndy Whitcroft } 3696de7d4f0eSAndy Whitcroft } 3697de7d4f0eSAndy Whitcroft 3698de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3699de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 37000fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3701773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3702773647a0SAndy Whitcroft 37039c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 37048eef05ddSJoe Perches 37058eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 37068eef05ddSJoe Perches WARN("DEEP_INDENTATION", 37078eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 37088eef05ddSJoe Perches } 37098eef05ddSJoe Perches 3710de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3711de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3712de7d4f0eSAndy Whitcroft 3713548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3714548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3715de7d4f0eSAndy Whitcroft 3716548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3717548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3718548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3719548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3720548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3721773647a0SAndy Whitcroft $ctx_ln++; 3722773647a0SAndy Whitcroft } 3723548596d5SAndy Whitcroft 372453210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 372553210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3726773647a0SAndy Whitcroft 3727773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3728000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3729000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 373001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 373100df344fSAndy Whitcroft } 3732773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3733773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3734773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3735773647a0SAndy Whitcroft { 37369c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 37379c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3738000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3739000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 374001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 37419c0ca6f9SAndy Whitcroft } 37429c0ca6f9SAndy Whitcroft } 374300df344fSAndy Whitcroft } 374400df344fSAndy Whitcroft 37454d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3746f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 37473e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 37483e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 37493e469cdcSAndy Whitcroft if (!defined $stat); 37504d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 37514d001e4dSAndy Whitcroft 37524d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 37534d001e4dSAndy Whitcroft 37549f5af480SJoe Perches # remove inline comments 37559f5af480SJoe Perches $s =~ s/$;/ /g; 37569f5af480SJoe Perches $c =~ s/$;/ /g; 37574d001e4dSAndy Whitcroft 37584d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 37596f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 37606f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 37614d001e4dSAndy Whitcroft 37629f5af480SJoe Perches # Make sure we remove the line prefixes as we have 37639f5af480SJoe Perches # none on the first line, and are going to readd them 37649f5af480SJoe Perches # where necessary. 37659f5af480SJoe Perches $s =~ s/\n./\n/gs; 37669f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 37679f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 37689f5af480SJoe Perches } 37699f5af480SJoe Perches 37704d001e4dSAndy Whitcroft # We want to check the first line inside the block 37714d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 37724d001e4dSAndy Whitcroft # 1) any blank line termination 37734d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 37744d001e4dSAndy Whitcroft # 3) any do (...) { 37754d001e4dSAndy Whitcroft my $continuation = 0; 37764d001e4dSAndy Whitcroft my $check = 0; 37774d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 37784d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 37794d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 37804d001e4dSAndy Whitcroft $continuation = 1; 37814d001e4dSAndy Whitcroft } 37829bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 37834d001e4dSAndy Whitcroft $check = 1; 37844d001e4dSAndy Whitcroft $cond_lines++; 37854d001e4dSAndy Whitcroft } 37864d001e4dSAndy Whitcroft 37874d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 37884d001e4dSAndy Whitcroft # preprocessor statement. 37894d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 37904d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 37914d001e4dSAndy Whitcroft $check = 0; 37924d001e4dSAndy Whitcroft } 37934d001e4dSAndy Whitcroft 37949bd49efeSAndy Whitcroft my $cond_ptr = -1; 3795740504c6SAndy Whitcroft $continuation = 0; 37969bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 37979bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 37984d001e4dSAndy Whitcroft 3799f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3800f16fa28fSAndy Whitcroft # is not linear. 3801f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3802f16fa28fSAndy Whitcroft $check = 0; 3803f16fa28fSAndy Whitcroft } 3804f16fa28fSAndy Whitcroft 38059bd49efeSAndy Whitcroft # Ignore: 38069bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 38079bd49efeSAndy Whitcroft # 2) preprocessor lines, and 38089bd49efeSAndy Whitcroft # 3) labels. 3809740504c6SAndy Whitcroft if ($continuation || 3810740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 38119bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 38129bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3813740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 381430dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 38159bd49efeSAndy Whitcroft $cond_lines++; 38169bd49efeSAndy Whitcroft } 38174d001e4dSAndy Whitcroft } 381830dad6ebSAndy Whitcroft } 38194d001e4dSAndy Whitcroft 38204d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 38214d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 38224d001e4dSAndy Whitcroft 38234d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 38244d001e4dSAndy Whitcroft # this is not this patch's fault. 38254d001e4dSAndy Whitcroft if (!defined($stat_real) || 38264d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 38274d001e4dSAndy Whitcroft $check = 0; 38284d001e4dSAndy Whitcroft } 38294d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 38304d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 38314d001e4dSAndy Whitcroft } 38324d001e4dSAndy Whitcroft 38339bd49efeSAndy 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"; 38344d001e4dSAndy Whitcroft 38359f5af480SJoe Perches if ($check && $s ne '' && 3836713a09deSAntonio Borneo (($sindent % $tabsize) != 0 || 38379f5af480SJoe Perches ($sindent < $indent) || 3838f6950a73SJoe Perches ($sindent == $indent && 3839f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 3840713a09deSAntonio Borneo ($sindent > $indent + $tabsize))) { 3841000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3842000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 38434d001e4dSAndy Whitcroft } 38444d001e4dSAndy Whitcroft } 38454d001e4dSAndy Whitcroft 38466c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 38476c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 38481f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 38491f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 38506c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3851c2fdda0dSAndy Whitcroft if ($dbg_values) { 3852c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3853cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3854cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 38551f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3856c2fdda0dSAndy Whitcroft } 38576c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 38586c72ffaaSAndy Whitcroft 385900df344fSAndy Whitcroft#ignore lines not being added 38603705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 386100df344fSAndy Whitcroft 386211ca40a0SJoe Perches# check for dereferences that span multiple lines 386311ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 386411ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 386511ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 386611ca40a0SJoe Perches my $ref = $1; 386711ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 386811ca40a0SJoe Perches $ref .= $1; 386911ca40a0SJoe Perches $ref =~ s/\s//g; 387011ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 387111ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 387211ca40a0SJoe Perches } 387311ca40a0SJoe Perches 3874a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3875c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3876a1ce18e4SJoe Perches my $type = $1; 3877a1ce18e4SJoe Perches my $var = $2; 3878207a8e84SJoe Perches $var = "" if (!defined $var); 3879207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3880a1ce18e4SJoe Perches my $sign = $1; 3881a1ce18e4SJoe Perches my $pointer = $2; 3882a1ce18e4SJoe Perches 3883a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3884a1ce18e4SJoe Perches 3885a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3886a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3887a1ce18e4SJoe Perches $fix) { 3888a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3889207a8e84SJoe Perches my $comp_pointer = $pointer; 3890207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3891207a8e84SJoe Perches $decl .= $comp_pointer; 3892207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3893207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3894a1ce18e4SJoe Perches } 3895a1ce18e4SJoe Perches } 3896a1ce18e4SJoe Perches } 3897a1ce18e4SJoe Perches 3898653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 38997429c690SAndy Whitcroft if ($dbg_type) { 39007429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3901000d1cc1SJoe Perches ERROR("TEST_TYPE", 3902000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 39037429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3904000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3905000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 39067429c690SAndy Whitcroft } 3907653d4876SAndy Whitcroft next; 3908653d4876SAndy Whitcroft } 3909a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3910a1ef277eSAndy Whitcroft if ($dbg_attr) { 39119360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3912000d1cc1SJoe Perches ERROR("TEST_ATTR", 3913000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 39149360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3915000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3916000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3917a1ef277eSAndy Whitcroft } 3918a1ef277eSAndy Whitcroft next; 3919a1ef277eSAndy Whitcroft } 3920653d4876SAndy Whitcroft 3921f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 392299423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 392399423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3924d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3925d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3926f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3927f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3928f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3929d752fcc8SJoe Perches my $fixedline = $prevrawline; 3930d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3931f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3932d752fcc8SJoe Perches $fixedline = $line; 39338d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3934f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3935d752fcc8SJoe Perches } 3936f0a594c1SAndy Whitcroft } 3937f0a594c1SAndy Whitcroft 393800df344fSAndy Whitcroft# 393900df344fSAndy Whitcroft# Checks which are anchored on the added line. 394000df344fSAndy Whitcroft# 394100df344fSAndy Whitcroft 3942653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3943c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3944653d4876SAndy Whitcroft my $path = $1; 3945653d4876SAndy Whitcroft if ($path =~ m{//}) { 3946000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3947495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3948495e9d84SJoe Perches } 3949495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3950495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3951495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3952653d4876SAndy Whitcroft } 3953653d4876SAndy Whitcroft } 3954653d4876SAndy Whitcroft 395500df344fSAndy Whitcroft# no C99 // comments 395600df344fSAndy Whitcroft if ($line =~ m{//}) { 39573705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 39583705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 39593705ce5bSJoe Perches $fix) { 3960194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 39613705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 39623705ce5bSJoe Perches my $comment = trim($1); 3963194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 39643705ce5bSJoe Perches } 39653705ce5bSJoe Perches } 396600df344fSAndy Whitcroft } 396700df344fSAndy Whitcroft # Remove C99 comments. 39680a920b5bSAndy Whitcroft $line =~ s@//.*@@; 39696c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 39700a920b5bSAndy Whitcroft 39712b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 39722b474a1aSAndy Whitcroft# the whole statement. 39732b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 39742b474a1aSAndy Whitcroft if (defined $realline_next && 39752b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 39762b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 39772b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 39782b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 39793cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 39803cbf62dfSAndy Whitcroft # a prefix: 39813cbf62dfSAndy Whitcroft # XXX(foo); 39823cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3983653d4876SAndy Whitcroft my $name = $1; 398487a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 39853cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 39863cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 39873cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 39883cbf62dfSAndy Whitcroft 39893cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 39902b474a1aSAndy Whitcroft \n.}\s*$| 399148012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 399248012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 399348012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 39942b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 39952b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 399648012058SAndy Whitcroft )/x) { 39972b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 39982b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 39992b474a1aSAndy Whitcroft } else { 40002b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 40010a920b5bSAndy Whitcroft } 40020a920b5bSAndy Whitcroft } 40032b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 40042b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 40052b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 40062b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 40072b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 40082b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 40092b474a1aSAndy Whitcroft } 40102b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 40112b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 4012000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 4013000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 40142b474a1aSAndy Whitcroft } 40150a920b5bSAndy Whitcroft 40165150bda4SJoe Eloff# check for global initialisers. 40176d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 4018d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 40196d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 4020d5e616fcSJoe Perches $fix) { 40216d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 4022d5e616fcSJoe Perches } 4023f0a594c1SAndy Whitcroft } 40240a920b5bSAndy Whitcroft# check for static initialisers. 40256d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 4026d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 40276d32f7a3SJoe Perches "do not initialise statics to $1\n" . 4028d5e616fcSJoe Perches $herecurr) && 4029d5e616fcSJoe Perches $fix) { 40306d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 4031d5e616fcSJoe Perches } 40320a920b5bSAndy Whitcroft } 40330a920b5bSAndy Whitcroft 40341813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 40351813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 40361813087dSJoe Perches my $tmp = trim($1); 40371813087dSJoe Perches WARN("MISORDERED_TYPE", 40381813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 40391813087dSJoe Perches } 40401813087dSJoe Perches 4041809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 4042809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 4043809e082eSJoe Perches my $type = trim($1); 4044809e082eSJoe Perches next if ($type !~ /\bint\b/); 4045809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 4046809e082eSJoe Perches my $new_type = $type; 4047809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 4048809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 4049809e082eSJoe Perches $new_type =~ s/^const\s+//; 4050809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 4051809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 4052809e082eSJoe Perches $new_type =~ s/\s+/ /g; 4053809e082eSJoe Perches $new_type = trim($new_type); 4054809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 4055809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 4056809e082eSJoe Perches $fix) { 4057809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 4058809e082eSJoe Perches } 4059809e082eSJoe Perches } 4060809e082eSJoe Perches 4061cb710ecaSJoe Perches# check for static const char * arrays. 4062cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 4063000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4064000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 4065cb710ecaSJoe Perches $herecurr); 4066cb710ecaSJoe Perches } 4067cb710ecaSJoe Perches 406877b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 406977b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 407077b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 407177b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 407277b8c0a8SJoe Perches $fix) { 407377b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 407477b8c0a8SJoe Perches } 407577b8c0a8SJoe Perches } 407677b8c0a8SJoe Perches 4077cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 4078cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4079000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4080000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4081cb710ecaSJoe Perches $herecurr); 4082cb710ecaSJoe Perches } 4083cb710ecaSJoe Perches 4084ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4085ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4086ab7e23f3SJoe Perches my $found = $1; 4087ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4088ab7e23f3SJoe Perches WARN("CONST_CONST", 4089ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4090ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4091ab7e23f3SJoe Perches WARN("CONST_CONST", 4092ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4093ab7e23f3SJoe Perches } 4094ab7e23f3SJoe Perches } 4095ab7e23f3SJoe Perches 40969b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 40979b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 40989b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 40999b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 41009b0fa60dSJoe Perches $herecurr); 41019b0fa60dSJoe Perches } 41029b0fa60dSJoe Perches 4103b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4104b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4105b598b670SJoe Perches my $array = $1; 4106b598b670SJoe 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*\))@) { 4107b598b670SJoe Perches my $array_div = $1; 4108b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4109b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4110b598b670SJoe Perches $fix) { 4111b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4112b598b670SJoe Perches } 4113b598b670SJoe Perches } 4114b598b670SJoe Perches } 4115b598b670SJoe Perches 4116b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 411716b7f3c8SJoe Perches if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { 4118b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4119b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4120b36190c5SJoe Perches $fix) { 4121194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4122b36190c5SJoe Perches } 4123b36190c5SJoe Perches } 4124b36190c5SJoe Perches 4125653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4126653d4876SAndy Whitcroft# make sense. 4127653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 41288054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4129c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 41308ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 413146d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4132000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4133000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 41340a920b5bSAndy Whitcroft } 41350a920b5bSAndy Whitcroft 41360a920b5bSAndy Whitcroft# * goes on variable not on type 413765863862SAndy Whitcroft # (char*[ const]) 4138bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4139bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 41403705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4141d8aaf121SAndy Whitcroft 414265863862SAndy Whitcroft # Should start with a space. 414365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 414465863862SAndy Whitcroft # Should not end with a space. 414565863862SAndy Whitcroft $to =~ s/\s+$//; 414665863862SAndy Whitcroft # '*'s should not have spaces between. 4147f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 414865863862SAndy Whitcroft } 4149d8aaf121SAndy Whitcroft 41503705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 415165863862SAndy Whitcroft if ($from ne $to) { 41523705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 41533705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 41543705ce5bSJoe Perches $fix) { 41553705ce5bSJoe Perches my $sub_from = $ident; 41563705ce5bSJoe Perches my $sub_to = $ident; 41573705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4158194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41593705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 41603705ce5bSJoe Perches } 416165863862SAndy Whitcroft } 4162bfcb2cc7SAndy Whitcroft } 4163bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4164bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 41653705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4166d8aaf121SAndy Whitcroft 416765863862SAndy Whitcroft # Should start with a space. 416865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 416965863862SAndy Whitcroft # Should not end with a space. 417065863862SAndy Whitcroft $to =~ s/\s+$//; 417165863862SAndy Whitcroft # '*'s should not have spaces between. 4172f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 417365863862SAndy Whitcroft } 417465863862SAndy Whitcroft # Modifiers should have spaces. 417565863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 417665863862SAndy Whitcroft 41773705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4178667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 41793705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 41803705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 41813705ce5bSJoe Perches $fix) { 41823705ce5bSJoe Perches 41833705ce5bSJoe Perches my $sub_from = $match; 41843705ce5bSJoe Perches my $sub_to = $match; 41853705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4186194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41873705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 41883705ce5bSJoe Perches } 418965863862SAndy Whitcroft } 41900a920b5bSAndy Whitcroft } 41910a920b5bSAndy Whitcroft 41929d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 41939d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 41940675a8fbSJean Delvare my $msg_level = \&WARN; 41950675a8fbSJean Delvare $msg_level = \&CHK if ($file); 41960675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 41979d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 41989d3e3c70SJoe Perches } 41990a920b5bSAndy Whitcroft 42009d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 42018905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4202000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4203000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 42048905a67cSAndy Whitcroft } 42058905a67cSAndy Whitcroft 420617441227SJoe Perches# check for uses of printk_ratelimit 420717441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4208000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4209000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 421017441227SJoe Perches } 421117441227SJoe Perches 4212eeef5733SJoe Perches# printk should use KERN_* levels 4213eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4214000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4215eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 421600df344fSAndy Whitcroft } 42170a920b5bSAndy Whitcroft 4218243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4219243f3803SJoe Perches my $orig = $1; 4220243f3803SJoe Perches my $level = lc($orig); 4221243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 42228f26b837SJoe Perches my $level2 = $level; 42238f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4224243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4225daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4226243f3803SJoe Perches } 4227243f3803SJoe Perches 4228dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4229dc139313SJoe Perches my $orig = $1; 4230dc139313SJoe Perches my $level = lc($orig); 4231dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4232dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4233dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4234dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4235dc139313SJoe Perches } 4236dc139313SJoe Perches 423791c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 423891c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 423991c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 424091c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 424191c9afafSAndy Lutomirski WARN("ENOSYS", 424291c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 424391c9afafSAndy Lutomirski } 424491c9afafSAndy Lutomirski 42456b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches. 42466b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 42476b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected. 42486b9ea5ffSJakub Kicinski if (!$file && $line =~ /\bENOTSUPP\b/) { 42496b9ea5ffSJakub Kicinski if (WARN("ENOTSUPP", 42506b9ea5ffSJakub Kicinski "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 42516b9ea5ffSJakub Kicinski $fix) { 42526b9ea5ffSJakub Kicinski $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 42536b9ea5ffSJakub Kicinski } 42546b9ea5ffSJakub Kicinski } 42556b9ea5ffSJakub Kicinski 4256653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4257653d4876SAndy Whitcroft# or if closed on same line 42585b57980dSJoe Perches if ($perl_version_ok && 42592d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 42602d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 42612d453e3bSJoe Perches $sline !~ /}/) { 42628d182478SJoe Perches if (ERROR("OPEN_BRACE", 42632d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 42648d182478SJoe Perches $fix) { 42658d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 42668d182478SJoe Perches my $fixed_line = $rawline; 42678d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 42688d182478SJoe Perches my $line1 = $1; 42698d182478SJoe Perches my $line2 = $2; 42708d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 42718d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 42728d182478SJoe Perches if ($line2 !~ /^\s*$/) { 42738d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 42748d182478SJoe Perches } 42758d182478SJoe Perches } 42760a920b5bSAndy Whitcroft } 4277653d4876SAndy Whitcroft 42788905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 42798905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 42808905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 42818d182478SJoe Perches if (ERROR("OPEN_BRACE", 42828d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 42838d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 42848d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 42858d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 42868d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 42878d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42888d182478SJoe Perches $fixedline = $rawline; 42898d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 42908d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 42918d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42928d182478SJoe Perches } 42938d182478SJoe Perches } 42948905a67cSAndy Whitcroft } 42958905a67cSAndy Whitcroft 42960c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 42973705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 42983705ce5bSJoe Perches if (WARN("SPACING", 42993705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 43003705ce5bSJoe Perches $fix) { 4301194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43023705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 43033705ce5bSJoe Perches } 43040c73b4ebSAndy Whitcroft } 43050c73b4ebSAndy Whitcroft 430631070b5dSJoe Perches# Function pointer declarations 430731070b5dSJoe Perches# check spacing between type, funcptr, and args 430831070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 430991f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 431031070b5dSJoe Perches my $declare = $1; 431131070b5dSJoe Perches my $pre_pointer_space = $2; 431231070b5dSJoe Perches my $post_pointer_space = $3; 431331070b5dSJoe Perches my $funcname = $4; 431431070b5dSJoe Perches my $post_funcname_space = $5; 431531070b5dSJoe Perches my $pre_args_space = $6; 431631070b5dSJoe Perches 431791f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 431891f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 431991f72e9cSJoe Perches# don't need a space so don't warn for those. 432091f72e9cSJoe Perches my $post_declare_space = ""; 432191f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 432291f72e9cSJoe Perches $post_declare_space = $1; 432391f72e9cSJoe Perches $declare = rtrim($declare); 432491f72e9cSJoe Perches } 432591f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 432631070b5dSJoe Perches WARN("SPACING", 432731070b5dSJoe Perches "missing space after return type\n" . $herecurr); 432891f72e9cSJoe Perches $post_declare_space = " "; 432931070b5dSJoe Perches } 433031070b5dSJoe Perches 433131070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 433291f72e9cSJoe Perches# This test is not currently implemented because these declarations are 433391f72e9cSJoe Perches# equivalent to 433491f72e9cSJoe Perches# int foo(int bar, ...) 433591f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 433691f72e9cSJoe Perches# 433791f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 433891f72e9cSJoe Perches# WARN("SPACING", 433991f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 434091f72e9cSJoe Perches# } 434131070b5dSJoe Perches 434231070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 434331070b5dSJoe Perches if (defined $pre_pointer_space && 434431070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 434531070b5dSJoe Perches WARN("SPACING", 434631070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 434731070b5dSJoe Perches } 434831070b5dSJoe Perches 434931070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 435031070b5dSJoe Perches if (defined $post_pointer_space && 435131070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 435231070b5dSJoe Perches WARN("SPACING", 435331070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 435431070b5dSJoe Perches } 435531070b5dSJoe Perches 435631070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 435731070b5dSJoe Perches if (defined $post_funcname_space && 435831070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 435931070b5dSJoe Perches WARN("SPACING", 436031070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 436131070b5dSJoe Perches } 436231070b5dSJoe Perches 436331070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 436431070b5dSJoe Perches if (defined $pre_args_space && 436531070b5dSJoe Perches $pre_args_space =~ /^\s/) { 436631070b5dSJoe Perches WARN("SPACING", 436731070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 436831070b5dSJoe Perches } 436931070b5dSJoe Perches 437031070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4371194f66fcSJoe Perches $fixed[$fixlinenr] =~ 437291f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 437331070b5dSJoe Perches } 437431070b5dSJoe Perches } 437531070b5dSJoe Perches 43768d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 43778d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4378fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4379fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 43808d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 43818d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 43828d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4383fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 438438dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 43853705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 43863705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 43873705ce5bSJoe Perches $fix) { 4388194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43893705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 43903705ce5bSJoe Perches } 43918d31cfceSAndy Whitcroft } 43928d31cfceSAndy Whitcroft } 43938d31cfceSAndy Whitcroft 4394f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 43956c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4396c2fdda0dSAndy Whitcroft my $name = $1; 4397773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4398773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4399c2fdda0dSAndy Whitcroft 4400c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4401773647a0SAndy Whitcroft if ($name =~ /^(?: 4402773647a0SAndy Whitcroft if|for|while|switch|return|case| 4403773647a0SAndy Whitcroft volatile|__volatile__| 4404773647a0SAndy Whitcroft __attribute__|format|__extension__| 4405773647a0SAndy Whitcroft asm|__asm__)$/x) 4406773647a0SAndy Whitcroft { 4407c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4408c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4409c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4410c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4411773647a0SAndy Whitcroft 4412773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4413c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4414c2fdda0dSAndy Whitcroft 4415c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4416c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4417773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4418c2fdda0dSAndy Whitcroft 4419c2fdda0dSAndy Whitcroft } else { 44203705ce5bSJoe Perches if (WARN("SPACING", 44213705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 44223705ce5bSJoe Perches $fix) { 4423194f66fcSJoe Perches $fixed[$fixlinenr] =~ 44243705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 44253705ce5bSJoe Perches } 4426f0a594c1SAndy Whitcroft } 44276c72ffaaSAndy Whitcroft } 44289a4cad4eSEric Nelson 4429653d4876SAndy Whitcroft# Check operator spacing. 44300a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 44313705ce5bSJoe Perches my $fixed_line = ""; 44323705ce5bSJoe Perches my $line_fixed = 0; 44333705ce5bSJoe Perches 44349c0ca6f9SAndy Whitcroft my $ops = qr{ 44359c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 44369c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 44379c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 44381f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 443984731623SJoe Perches \?:|\?|: 44409c0ca6f9SAndy Whitcroft }x; 4441cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 44423705ce5bSJoe Perches 44433705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 44443705ce5bSJoe Perches## foreach my $el (@elements) { 44453705ce5bSJoe Perches## print("el: <$el>\n"); 44463705ce5bSJoe Perches## } 44473705ce5bSJoe Perches 44483705ce5bSJoe Perches my @fix_elements = (); 444900df344fSAndy Whitcroft my $off = 0; 44506c72ffaaSAndy Whitcroft 44513705ce5bSJoe Perches foreach my $el (@elements) { 44523705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 44533705ce5bSJoe Perches $off += length($el); 44543705ce5bSJoe Perches } 44553705ce5bSJoe Perches 44563705ce5bSJoe Perches $off = 0; 44573705ce5bSJoe Perches 44586c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4459b34c648bSJoe Perches my $last_after = -1; 44606c72ffaaSAndy Whitcroft 44610a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 44623705ce5bSJoe Perches 44633705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 44643705ce5bSJoe Perches 44653705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 44663705ce5bSJoe Perches 44674a0df2efSAndy Whitcroft $off += length($elements[$n]); 44684a0df2efSAndy Whitcroft 446925985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4470773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4471773647a0SAndy Whitcroft my $cc = ''; 4472773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4473773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4474773647a0SAndy Whitcroft } 4475773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4476773647a0SAndy Whitcroft 44774a0df2efSAndy Whitcroft my $a = ''; 44784a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 44794a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4480cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 44814a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 44824a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4483773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 44844a0df2efSAndy Whitcroft 44850a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 44864a0df2efSAndy Whitcroft 44874a0df2efSAndy Whitcroft my $c = ''; 44880a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 44894a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 44904a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4491cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 44924a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 44934a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 44948b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 44954a0df2efSAndy Whitcroft } else { 44964a0df2efSAndy Whitcroft $c = 'E'; 44970a920b5bSAndy Whitcroft } 44980a920b5bSAndy Whitcroft 44994a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 45004a0df2efSAndy Whitcroft 45014a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 45024a0df2efSAndy Whitcroft 45036c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4504de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 45050a920b5bSAndy Whitcroft 450674048ed8SAndy Whitcroft # Pull out the value of this operator. 45076c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 45080a920b5bSAndy Whitcroft 45091f65f947SAndy Whitcroft # Get the full operator variant. 45101f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 45111f65f947SAndy Whitcroft 451213214adfSAndy Whitcroft # Ignore operators passed as parameters. 451313214adfSAndy Whitcroft if ($op_type ne 'V' && 4514d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 451513214adfSAndy Whitcroft 4516cf655043SAndy Whitcroft# # Ignore comments 4517cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 451813214adfSAndy Whitcroft 4519d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 452013214adfSAndy Whitcroft } elsif ($op eq ';') { 4521cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4522cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 45233705ce5bSJoe Perches if (ERROR("SPACING", 45243705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4525b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 45263705ce5bSJoe Perches $line_fixed = 1; 45273705ce5bSJoe Perches } 4528d8aaf121SAndy Whitcroft } 4529d8aaf121SAndy Whitcroft 4530d8aaf121SAndy Whitcroft # // is a comment 4531d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 45320a920b5bSAndy Whitcroft 4533b00e4814SJoe Perches # : when part of a bitfield 4534b00e4814SJoe Perches } elsif ($opv eq ':B') { 4535b00e4814SJoe Perches # skip the bitfield test for now 4536b00e4814SJoe Perches 45371f65f947SAndy Whitcroft # No spaces for: 45381f65f947SAndy Whitcroft # -> 4539b00e4814SJoe Perches } elsif ($op eq '->') { 45404a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 45413705ce5bSJoe Perches if (ERROR("SPACING", 45423705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4543b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45443705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45453705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 45463705ce5bSJoe Perches } 4547b34c648bSJoe Perches $line_fixed = 1; 45483705ce5bSJoe Perches } 45490a920b5bSAndy Whitcroft } 45500a920b5bSAndy Whitcroft 45512381097bSJoe Perches # , must not have a space before and must have a space on the right. 45520a920b5bSAndy Whitcroft } elsif ($op eq ',') { 45532381097bSJoe Perches my $rtrim_before = 0; 45542381097bSJoe Perches my $space_after = 0; 45552381097bSJoe Perches if ($ctx =~ /Wx./) { 45562381097bSJoe Perches if (ERROR("SPACING", 45572381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 45582381097bSJoe Perches $line_fixed = 1; 45592381097bSJoe Perches $rtrim_before = 1; 45602381097bSJoe Perches } 45612381097bSJoe Perches } 4562cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 45633705ce5bSJoe Perches if (ERROR("SPACING", 45643705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 45653705ce5bSJoe Perches $line_fixed = 1; 4566b34c648bSJoe Perches $last_after = $n; 45672381097bSJoe Perches $space_after = 1; 45682381097bSJoe Perches } 45692381097bSJoe Perches } 45702381097bSJoe Perches if ($rtrim_before || $space_after) { 45712381097bSJoe Perches if ($rtrim_before) { 45722381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45732381097bSJoe Perches } else { 45742381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 45752381097bSJoe Perches } 45762381097bSJoe Perches if ($space_after) { 45772381097bSJoe Perches $good .= " "; 45783705ce5bSJoe Perches } 45790a920b5bSAndy Whitcroft } 45800a920b5bSAndy Whitcroft 45819c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 458274048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 45839c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 45849c0ca6f9SAndy Whitcroft 45859c0ca6f9SAndy Whitcroft # unary operators should have a space before and 45869c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 45879c0ca6f9SAndy Whitcroft # unary operator, or a cast 45889c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 458974048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 45900d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4591cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 45923705ce5bSJoe Perches if (ERROR("SPACING", 45933705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4594b34c648bSJoe Perches if ($n != $last_after + 2) { 4595b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 45963705ce5bSJoe Perches $line_fixed = 1; 45973705ce5bSJoe Perches } 45980a920b5bSAndy Whitcroft } 4599b34c648bSJoe Perches } 4600a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4601171ae1a4SAndy Whitcroft # A unary '*' may be const 4602171ae1a4SAndy Whitcroft 4603171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 46043705ce5bSJoe Perches if (ERROR("SPACING", 46053705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4606b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 46073705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 46083705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 46093705ce5bSJoe Perches } 4610b34c648bSJoe Perches $line_fixed = 1; 46113705ce5bSJoe Perches } 46120a920b5bSAndy Whitcroft } 46130a920b5bSAndy Whitcroft 46140a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 46150a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4616773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 46173705ce5bSJoe Perches if (ERROR("SPACING", 46183705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4619b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 46203705ce5bSJoe Perches $line_fixed = 1; 46213705ce5bSJoe Perches } 46220a920b5bSAndy Whitcroft } 4623773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4624773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 46253705ce5bSJoe Perches if (ERROR("SPACING", 46263705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4627b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 46283705ce5bSJoe Perches $line_fixed = 1; 46293705ce5bSJoe Perches } 4630653d4876SAndy Whitcroft } 4631773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 46323705ce5bSJoe Perches if (ERROR("SPACING", 46333705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4634b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 46353705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 46363705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4637773647a0SAndy Whitcroft } 4638b34c648bSJoe Perches $line_fixed = 1; 46393705ce5bSJoe Perches } 46403705ce5bSJoe Perches } 46410a920b5bSAndy Whitcroft 46420a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 46439c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 46449c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 46459c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4646c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4647c2fdda0dSAndy Whitcroft $op eq '%') 46480a920b5bSAndy Whitcroft { 4649d2e025f3SJoe Perches if ($check) { 4650d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4651d2e025f3SJoe Perches if (CHK("SPACING", 4652d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4653d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4654d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4655d2e025f3SJoe Perches $line_fixed = 1; 4656d2e025f3SJoe Perches } 4657d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4658d2e025f3SJoe Perches if (CHK("SPACING", 4659d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4660d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4661d2e025f3SJoe Perches $line_fixed = 1; 4662d2e025f3SJoe Perches } 4663d2e025f3SJoe Perches } 4664d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 46653705ce5bSJoe Perches if (ERROR("SPACING", 46663705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4667b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4668b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4669b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4670b34c648bSJoe Perches } 46713705ce5bSJoe Perches $line_fixed = 1; 46723705ce5bSJoe Perches } 46730a920b5bSAndy Whitcroft } 46740a920b5bSAndy Whitcroft 46751f65f947SAndy Whitcroft # A colon needs no spaces before when it is 46761f65f947SAndy Whitcroft # terminating a case value or a label. 46771f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 46781f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 46793705ce5bSJoe Perches if (ERROR("SPACING", 46803705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4681b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 46823705ce5bSJoe Perches $line_fixed = 1; 46833705ce5bSJoe Perches } 46841f65f947SAndy Whitcroft } 46851f65f947SAndy Whitcroft 46860a920b5bSAndy Whitcroft # All the others need spaces both sides. 4687cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 46881f65f947SAndy Whitcroft my $ok = 0; 46891f65f947SAndy Whitcroft 469022f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 46911f65f947SAndy Whitcroft if (($op eq '<' && 46921f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 46931f65f947SAndy Whitcroft ($op eq '>' && 46941f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 46951f65f947SAndy Whitcroft { 46961f65f947SAndy Whitcroft $ok = 1; 46971f65f947SAndy Whitcroft } 46981f65f947SAndy Whitcroft 4699e0df7e1fSJoe Perches # for asm volatile statements 4700e0df7e1fSJoe Perches # ignore a colon with another 4701e0df7e1fSJoe Perches # colon immediately before or after 4702e0df7e1fSJoe Perches if (($op eq ':') && 4703e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4704e0df7e1fSJoe Perches $ok = 1; 4705e0df7e1fSJoe Perches } 4706e0df7e1fSJoe Perches 470784731623SJoe Perches # messages are ERROR, but ?: are CHK 47081f65f947SAndy Whitcroft if ($ok == 0) { 47090675a8fbSJean Delvare my $msg_level = \&ERROR; 47100675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 471184731623SJoe Perches 47120675a8fbSJean Delvare if (&{$msg_level}("SPACING", 47133705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4714b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4715b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4716b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4717b34c648bSJoe Perches } 47183705ce5bSJoe Perches $line_fixed = 1; 47193705ce5bSJoe Perches } 47200a920b5bSAndy Whitcroft } 472122f2a2efSAndy Whitcroft } 47224a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 47233705ce5bSJoe Perches 47243705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 47253705ce5bSJoe Perches 47263705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 47270a920b5bSAndy Whitcroft } 47283705ce5bSJoe Perches 47293705ce5bSJoe Perches if (($#elements % 2) == 0) { 47303705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 47313705ce5bSJoe Perches } 47323705ce5bSJoe Perches 4733194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4734194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 47353705ce5bSJoe Perches } 47363705ce5bSJoe Perches 47373705ce5bSJoe Perches 47380a920b5bSAndy Whitcroft } 47390a920b5bSAndy Whitcroft 4740786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4741d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4742786b6326SJoe Perches if (WARN("SPACING", 4743786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4744786b6326SJoe Perches $fix) { 4745194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4746786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4747786b6326SJoe Perches } 4748786b6326SJoe Perches } 4749786b6326SJoe Perches 4750f0a594c1SAndy Whitcroft# check for multiple assignments 4751f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4752000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4753000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4754f0a594c1SAndy Whitcroft } 4755f0a594c1SAndy Whitcroft 475622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 475722f2a2efSAndy Whitcroft## # continuation. 475822f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 475922f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 476022f2a2efSAndy Whitcroft## 476122f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 476222f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 476322f2a2efSAndy Whitcroft## my $ln = $line; 476422f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 476522f2a2efSAndy Whitcroft## } 476622f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4767000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4768000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 476922f2a2efSAndy Whitcroft## } 477022f2a2efSAndy Whitcroft## } 4771f0a594c1SAndy Whitcroft 47720a920b5bSAndy Whitcroft#need space before brace following if, while, etc 47736b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 47746ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 47753705ce5bSJoe Perches if (ERROR("SPACING", 47763705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 47773705ce5bSJoe Perches $fix) { 47786ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 47793705ce5bSJoe Perches } 4780de7d4f0eSAndy Whitcroft } 4781de7d4f0eSAndy Whitcroft 4782c4a62ef9SJoe Perches## # check for blank lines before declarations 4783c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4784c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4785c4a62ef9SJoe Perches## WARN("SPACING", 4786c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4787c4a62ef9SJoe Perches## } 4788c4a62ef9SJoe Perches## 4789c4a62ef9SJoe Perches 4790de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4791de7d4f0eSAndy Whitcroft# on the line 479294fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 4793d5e616fcSJoe Perches if (ERROR("SPACING", 4794d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4795d5e616fcSJoe Perches $fix) { 4796194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4797d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4798d5e616fcSJoe Perches } 47990a920b5bSAndy Whitcroft } 48000a920b5bSAndy Whitcroft 480122f2a2efSAndy Whitcroft# check spacing on square brackets 480222f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 48033705ce5bSJoe Perches if (ERROR("SPACING", 48043705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 48053705ce5bSJoe Perches $fix) { 4806194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48073705ce5bSJoe Perches s/\[\s+/\[/; 48083705ce5bSJoe Perches } 480922f2a2efSAndy Whitcroft } 481022f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 48113705ce5bSJoe Perches if (ERROR("SPACING", 48123705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 48133705ce5bSJoe Perches $fix) { 4814194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48153705ce5bSJoe Perches s/\s+\]/\]/; 48163705ce5bSJoe Perches } 481722f2a2efSAndy Whitcroft } 481822f2a2efSAndy Whitcroft 4819c45dcabdSAndy Whitcroft# check spacing on parentheses 48209c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 48219c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 48223705ce5bSJoe Perches if (ERROR("SPACING", 48233705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 48243705ce5bSJoe Perches $fix) { 4825194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48263705ce5bSJoe Perches s/\(\s+/\(/; 48273705ce5bSJoe Perches } 482822f2a2efSAndy Whitcroft } 482913214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4830c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4831c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 48323705ce5bSJoe Perches if (ERROR("SPACING", 48333705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 48343705ce5bSJoe Perches $fix) { 4835194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48363705ce5bSJoe Perches s/\s+\)/\)/; 48373705ce5bSJoe Perches } 483822f2a2efSAndy Whitcroft } 483922f2a2efSAndy Whitcroft 4840e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4841e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4842e2826fd0SJoe Perches 4843e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4844ea4acbb1SJoe Perches my $var = $1; 4845ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4846ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4847ea4acbb1SJoe Perches $fix) { 4848ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4849ea4acbb1SJoe Perches } 4850ea4acbb1SJoe Perches } 4851ea4acbb1SJoe Perches 4852ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4853ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4854ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4855ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4856ea4acbb1SJoe Perches my $var = $2; 4857ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4858ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4859ea4acbb1SJoe Perches $fix) { 4860ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4861ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4862ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4863ea4acbb1SJoe Perches } 4864e2826fd0SJoe Perches } 4865e2826fd0SJoe Perches 486663b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4867a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4868a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 48695b57980dSJoe Perches $perl_version_ok && defined($stat) && 487063b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 487163b7c73eSJoe Perches my $if_stat = $1; 487263b7c73eSJoe Perches my $test = substr($2, 1, -1); 487363b7c73eSJoe Perches my $herectx; 487463b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 487563b7c73eSJoe Perches my $match = $1; 487663b7c73eSJoe Perches # avoid parentheses around potential macro args 487763b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 487863b7c73eSJoe Perches if (!defined($herectx)) { 487963b7c73eSJoe Perches $herectx = $here . "\n"; 488063b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 488163b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 488263b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 488363b7c73eSJoe Perches $herectx .= $rl . "\n"; 488463b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 488563b7c73eSJoe Perches } 488663b7c73eSJoe Perches } 488763b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 488863b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 488963b7c73eSJoe Perches } 489063b7c73eSJoe Perches } 489163b7c73eSJoe Perches 48920a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 48934a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 48940a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 48953705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 48963705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 48973705ce5bSJoe Perches $fix) { 4898194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48993705ce5bSJoe Perches s/^(.)\s+/$1/; 49003705ce5bSJoe Perches } 49010a920b5bSAndy Whitcroft } 49020a920b5bSAndy Whitcroft 49035b9553abSJoe Perches# return is not a function 4904507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4905c45dcabdSAndy Whitcroft my $spacing = $1; 49065b57980dSJoe Perches if ($perl_version_ok && 49075b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 49085b9553abSJoe Perches my $value = $1; 49095b9553abSJoe Perches $value = deparenthesize($value); 49105b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4911000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4912000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 49135b9553abSJoe Perches } 4914c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4915000d1cc1SJoe Perches ERROR("SPACING", 4916000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4917c45dcabdSAndy Whitcroft } 4918c45dcabdSAndy Whitcroft } 4919507e5141SJoe Perches 4920b43ae21bSJoe Perches# unnecessary return in a void function 4921b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4922b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4923b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4924b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4925b43ae21bSJoe Perches $linenr >= 3 && 4926b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4927b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 49289819cf25SJoe Perches WARN("RETURN_VOID", 4929b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 49309819cf25SJoe Perches } 49319819cf25SJoe Perches 4932189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 49335b57980dSJoe Perches if ($perl_version_ok && 4934189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4935189248d8SJoe Perches my $openparens = $1; 4936189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4937189248d8SJoe Perches my $msg = ""; 4938189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4939189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4940189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4941189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4942189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4943189248d8SJoe Perches } 4944189248d8SJoe Perches } 4945189248d8SJoe Perches 4946c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4947c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4948c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4949c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 49505b57980dSJoe Perches if ($perl_version_ok && 4951c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4952c5595fa2SJoe Perches my $lead = $1; 4953c5595fa2SJoe Perches my $const = $2; 4954c5595fa2SJoe Perches my $comp = $3; 4955c5595fa2SJoe Perches my $to = $4; 4956c5595fa2SJoe Perches my $newcomp = $comp; 4957f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4958c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4959c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4960c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4961c5595fa2SJoe Perches $fix) { 4962c5595fa2SJoe Perches if ($comp eq "<") { 4963c5595fa2SJoe Perches $newcomp = ">"; 4964c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4965c5595fa2SJoe Perches $newcomp = ">="; 4966c5595fa2SJoe Perches } elsif ($comp eq ">") { 4967c5595fa2SJoe Perches $newcomp = "<"; 4968c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4969c5595fa2SJoe Perches $newcomp = "<="; 4970c5595fa2SJoe Perches } 4971c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4972c5595fa2SJoe Perches } 4973c5595fa2SJoe Perches } 4974c5595fa2SJoe Perches 4975f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4976f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 497753a3c448SAndy Whitcroft my $name = $1; 497853a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4979000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4980f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 498153a3c448SAndy Whitcroft } 498253a3c448SAndy Whitcroft } 4983c45dcabdSAndy Whitcroft 49840a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 49854a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 49863705ce5bSJoe Perches if (ERROR("SPACING", 49873705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 49883705ce5bSJoe Perches $fix) { 4989194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49903705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 49913705ce5bSJoe Perches } 49920a920b5bSAndy Whitcroft } 49930a920b5bSAndy Whitcroft 4994f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4995f5fe35ddSAndy Whitcroft# statements after the conditional. 4996170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 49973e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 49983e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 49993e469cdcSAndy Whitcroft if (!defined $stat); 5000170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 5001170d3a22SAndy Whitcroft $remain_next, $off_next); 5002170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 5003170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 5004170d3a22SAndy Whitcroft 5005170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 5006170d3a22SAndy Whitcroft # If the statement carries leading newlines, 5007170d3a22SAndy Whitcroft # then count those as offsets. 5008170d3a22SAndy Whitcroft my ($whitespace) = 5009170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 5010170d3a22SAndy Whitcroft my $offset = 5011170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 5012170d3a22SAndy Whitcroft 5013170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 5014170d3a22SAndy Whitcroft $offset} = 1; 5015170d3a22SAndy Whitcroft } 5016170d3a22SAndy Whitcroft } 5017170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 5018c11230f4SJoe Perches defined($stat) && defined($cond) && 5019170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 5020171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 50218905a67cSAndy Whitcroft 5022b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 5023*65b64b3bSJoe Perches if (ERROR("ASSIGN_IN_IF", 5024*65b64b3bSJoe Perches "do not use assignment in if condition\n" . $herecurr) && 5025*65b64b3bSJoe Perches $fix && $perl_version_ok) { 5026*65b64b3bSJoe Perches if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { 5027*65b64b3bSJoe Perches my $space = $1; 5028*65b64b3bSJoe Perches my $not = $2; 5029*65b64b3bSJoe Perches my $statement = $3; 5030*65b64b3bSJoe Perches my $assigned = $4; 5031*65b64b3bSJoe Perches my $test = $8; 5032*65b64b3bSJoe Perches my $against = $9; 5033*65b64b3bSJoe Perches my $brace = $15; 5034*65b64b3bSJoe Perches fix_delete_line($fixlinenr, $rawline); 5035*65b64b3bSJoe Perches fix_insert_line($fixlinenr, "$space$statement;"); 5036*65b64b3bSJoe Perches my $newline = "${space}if ("; 5037*65b64b3bSJoe Perches $newline .= '!' if defined($not); 5038*65b64b3bSJoe Perches $newline .= '(' if (defined $not && defined($test) && defined($against)); 5039*65b64b3bSJoe Perches $newline .= "$assigned"; 5040*65b64b3bSJoe Perches $newline .= " $test $against" if (defined($test) && defined($against)); 5041*65b64b3bSJoe Perches $newline .= ')' if (defined $not && defined($test) && defined($against)); 5042*65b64b3bSJoe Perches $newline .= ')'; 5043*65b64b3bSJoe Perches $newline .= " {" if (defined($brace)); 5044*65b64b3bSJoe Perches fix_insert_line($fixlinenr + 1, $newline); 5045*65b64b3bSJoe Perches } 5046*65b64b3bSJoe Perches } 50478905a67cSAndy Whitcroft } 50488905a67cSAndy Whitcroft 50498905a67cSAndy Whitcroft # Find out what is on the end of the line after the 50508905a67cSAndy Whitcroft # conditional. 5051773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 50528905a67cSAndy Whitcroft $s =~ s/\n.*//g; 505313214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 505453210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 505553210168SAndy Whitcroft $c !~ /}\s*while\s*/) 5056773647a0SAndy Whitcroft { 5057bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 5058bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 5059bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 506042bdf74cSHidetoshi Seto my $stat_real = ''; 5061bb44ad39SAndy Whitcroft 506242bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 506342bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 5064bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 5065bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 5066bb44ad39SAndy Whitcroft } 5067bb44ad39SAndy Whitcroft 5068000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5069000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 50708905a67cSAndy Whitcroft } 50718905a67cSAndy Whitcroft } 50728905a67cSAndy Whitcroft 507313214adfSAndy Whitcroft# Check for bitwise tests written as boolean 507413214adfSAndy Whitcroft if ($line =~ / 507513214adfSAndy Whitcroft (?: 507613214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 507713214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 507813214adfSAndy Whitcroft (?:\&\&|\|\|) 507913214adfSAndy Whitcroft | 508013214adfSAndy Whitcroft (?:\&\&|\|\|) 508113214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 508213214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 508313214adfSAndy Whitcroft )/x) 508413214adfSAndy Whitcroft { 5085000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 5086000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 508713214adfSAndy Whitcroft } 508813214adfSAndy Whitcroft 50898905a67cSAndy Whitcroft# if and else should not have general statements after it 509013214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 509113214adfSAndy Whitcroft my $s = $1; 509213214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 509313214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5094000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5095000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 50960a920b5bSAndy Whitcroft } 509713214adfSAndy Whitcroft } 509839667782SAndy Whitcroft# if should not continue a brace 509939667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5100000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5101048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 510239667782SAndy Whitcroft $herecurr); 510339667782SAndy Whitcroft } 5104a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5105a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5106a1080bf8SAndy Whitcroft $line !~ /\G(?: 51073fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5108a1080bf8SAndy Whitcroft \s*return\s+ 5109a1080bf8SAndy Whitcroft )/xg) 5110a1080bf8SAndy Whitcroft { 5111000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5112000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5113a1080bf8SAndy Whitcroft } 51140a920b5bSAndy Whitcroft 51150a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 51160a920b5bSAndy Whitcroft # indent level to be relevant to each other. 51178b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 51180a920b5bSAndy Whitcroft $previndent == $indent) { 51198b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 51208b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 51218b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 51228b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 51238b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 51248b8856f4SJoe Perches my $fixedline = $prevrawline; 51258b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 51268b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 51278b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 51288b8856f4SJoe Perches } 51298b8856f4SJoe Perches $fixedline = $rawline; 51308b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 51318b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 51328b8856f4SJoe Perches } 51330a920b5bSAndy Whitcroft } 51340a920b5bSAndy Whitcroft 51358b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5136c2fdda0dSAndy Whitcroft $previndent == $indent) { 5137c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5138c2fdda0dSAndy Whitcroft 5139c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5140c2fdda0dSAndy Whitcroft # conditional. 5141773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5142c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5143c2fdda0dSAndy Whitcroft 5144c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 51458b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 51468b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 51478b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 51488b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 51498b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 51508b8856f4SJoe Perches my $fixedline = $prevrawline; 51518b8856f4SJoe Perches my $trailing = $rawline; 51528b8856f4SJoe Perches $trailing =~ s/^\+//; 51538b8856f4SJoe Perches $trailing = trim($trailing); 51548b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 51558b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 51568b8856f4SJoe Perches } 5157c2fdda0dSAndy Whitcroft } 5158c2fdda0dSAndy Whitcroft } 5159c2fdda0dSAndy Whitcroft 516095e2c602SJoe Perches#Specific variable tests 5161323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5162323c1260SJoe Perches my $var = $1; 516395e2c602SJoe Perches 516495e2c602SJoe Perches#CamelCase 5165807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5166be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 516722735ce8SJoe Perches#Ignore Page<foo> variants 5168807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5169d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5170d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5171d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5172f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5173f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 51747e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 51757e781f67SJoe Perches my $word = $1; 51767e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5177d8b07710SJoe Perches if ($check) { 5178d8b07710SJoe Perches seed_camelcase_includes(); 5179d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5180d8b07710SJoe Perches seed_camelcase_file($realfile); 5181d8b07710SJoe Perches $camelcase_file_seeded = 1; 5182d8b07710SJoe Perches } 5183d8b07710SJoe Perches } 51847e781f67SJoe Perches if (!defined $camelcase{$word}) { 51857e781f67SJoe Perches $camelcase{$word} = 1; 5186be79794bSJoe Perches CHK("CAMELCASE", 51877e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 51887e781f67SJoe Perches } 5189323c1260SJoe Perches } 5190323c1260SJoe Perches } 51913445686aSJoe Perches } 51920a920b5bSAndy Whitcroft 51930a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5194d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5195d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5196d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5197d5e616fcSJoe Perches $fix) { 5198194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5199d5e616fcSJoe Perches } 52000a920b5bSAndy Whitcroft } 52010a920b5bSAndy Whitcroft 52020e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 52030e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5204c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5205e09dec48SAndy Whitcroft my $file = "$1.h"; 5206e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5207e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5208e09dec48SAndy Whitcroft $realfile ne $checkfile && 52097840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5210c45dcabdSAndy Whitcroft { 52110e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 52120e212e0aSFabian Frederick if ($asminclude > 0) { 5213e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5214000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5215000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5216e09dec48SAndy Whitcroft } else { 5217000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5218000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5219e09dec48SAndy Whitcroft } 52200a920b5bSAndy Whitcroft } 52210a920b5bSAndy Whitcroft } 52220e212e0aSFabian Frederick } 52230a920b5bSAndy Whitcroft 5224653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5225653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5226cf655043SAndy Whitcroft# in a known good container 5227b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5228b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5229d8aaf121SAndy Whitcroft my $ln = $linenr; 5230d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5231c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5232c45dcabdSAndy Whitcroft my $ctx = ''; 523308a2843eSJoe Perches my $has_flow_statement = 0; 523408a2843eSJoe Perches my $has_arg_concat = 0; 5235c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5236f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5237f74bd194SAndy Whitcroft $ctx = $dstat; 5238c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5239a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5240c45dcabdSAndy Whitcroft 524108a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 524262e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 524308a2843eSJoe Perches 5244f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5245f59b64bfSJoe Perches my $define_args = $1; 5246f59b64bfSJoe Perches my $define_stmt = $dstat; 5247f59b64bfSJoe Perches my @def_args = (); 5248f59b64bfSJoe Perches 5249f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5250f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5251f59b64bfSJoe Perches $define_args =~ s/\s*//g; 52528c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5253f59b64bfSJoe Perches @def_args = split(",", $define_args); 5254f59b64bfSJoe Perches } 5255f59b64bfSJoe Perches 5256292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5257c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5258c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5259c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5260c45dcabdSAndy Whitcroft 5261c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 5262bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 5263bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 52646b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 5265bf30d6edSAndy Whitcroft { 5266c45dcabdSAndy Whitcroft } 5267c45dcabdSAndy Whitcroft 5268342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 526933acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 527033acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5271e45bab8eSAndy Whitcroft { 5272e45bab8eSAndy Whitcroft } 5273e45bab8eSAndy Whitcroft 527442e15293SJoe Perches # Make asm volatile uses seem like a generic function 527542e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 527642e15293SJoe Perches 5277c45dcabdSAndy Whitcroft my $exceptions = qr{ 5278c45dcabdSAndy Whitcroft $Declare| 5279c45dcabdSAndy Whitcroft module_param_named| 5280a0a0a7a9SKees Cook MODULE_PARM_DESC| 5281c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5282c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5283383099fdSAndy Whitcroft __typeof__\(| 528422fd2d3eSStefani Seibold union| 528522fd2d3eSStefani Seibold struct| 5286ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 52876b10df42SVladimir Zapolskiy ^\"|\"$| 52886b10df42SVladimir Zapolskiy ^\[ 5289c45dcabdSAndy Whitcroft }x; 52905eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5291f59b64bfSJoe Perches 5292f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5293f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5294e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5295f59b64bfSJoe Perches 5296f74bd194SAndy Whitcroft if ($dstat ne '' && 5297f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5298f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 52993cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5300356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5301f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5302f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5303e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 530472f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5305f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5306f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5307f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 53084e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5309f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5310c45dcabdSAndy Whitcroft { 5311e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5312e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5313e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5314e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5315f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5316f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5317f74bd194SAndy Whitcroft } else { 5318000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5319388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5320d8aaf121SAndy Whitcroft } 5321f59b64bfSJoe Perches 5322f59b64bfSJoe Perches } 53235207649bSJoe Perches 53245207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 53255207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 53265207649bSJoe Perches my $first = 1; 53275207649bSJoe Perches $define_stmt = ""; 53285207649bSJoe Perches foreach my $l (@stmt_array) { 53295207649bSJoe Perches $l =~ s/\\$//; 53305207649bSJoe Perches if ($first) { 53315207649bSJoe Perches $define_stmt = $l; 53325207649bSJoe Perches $first = 0; 53335207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 53345207649bSJoe Perches $define_stmt .= substr($l, 1); 53355207649bSJoe Perches } 53365207649bSJoe Perches } 53375207649bSJoe Perches $define_stmt =~ s/$;//g; 53385207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 53395207649bSJoe Perches $define_stmt = trim($define_stmt); 53405207649bSJoe Perches 5341f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5342f59b64bfSJoe Perches foreach my $arg (@def_args) { 5343f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 53449192d41aSJoe Perches next if ($arg =~ /^type$/i); 53457fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 53466dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 53477fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 53487fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5349d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5350f59b64bfSJoe Perches if ($use_cnt > 1) { 5351f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5352f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5353f59b64bfSJoe Perches } 53549192d41aSJoe Perches# check if any macro arguments may have other precedence issues 53557fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 53569192d41aSJoe Perches ((defined($1) && $1 ne ',') || 53579192d41aSJoe Perches (defined($2) && $2 ne ','))) { 53589192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 53599192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 53609192d41aSJoe Perches } 53610a920b5bSAndy Whitcroft } 53625023d347SJoe Perches 536308a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 536408a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 536508a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 536608a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5367e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 536808a2843eSJoe Perches 536908a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 537008a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 537108a2843eSJoe Perches } 537208a2843eSJoe Perches 5373481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 53745023d347SJoe Perches 53755023d347SJoe Perches } else { 53765023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5377481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5378481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 53795023d347SJoe Perches $line =~ /^\+.*\\$/) { 53805023d347SJoe Perches WARN("LINE_CONTINUATIONS", 53815023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 53825023d347SJoe Perches } 5383653d4876SAndy Whitcroft } 53840a920b5bSAndy Whitcroft 5385b13edf7fSJoe Perches# do {} while (0) macro tests: 5386b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5387b13edf7fSJoe Perches# macro should not end with a semicolon 53885b57980dSJoe Perches if ($perl_version_ok && 5389b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5390b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5391b13edf7fSJoe Perches my $ln = $linenr; 5392b13edf7fSJoe Perches my $cnt = $realcnt; 5393b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5394b13edf7fSJoe Perches my $ctx = ''; 5395b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5396b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5397b13edf7fSJoe Perches $ctx = $dstat; 5398b13edf7fSJoe Perches 5399b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 54001b36b201SJoe Perches $dstat =~ s/$;/ /g; 5401b13edf7fSJoe Perches 5402b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5403b13edf7fSJoe Perches my $stmts = $2; 5404b13edf7fSJoe Perches my $semis = $3; 5405b13edf7fSJoe Perches 5406b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5407b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5408e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5409b13edf7fSJoe Perches 5410ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5411ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5412b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5413b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5414b13edf7fSJoe Perches } 5415b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5416b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5417b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5418b13edf7fSJoe Perches } 5419f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5420f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5421f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5422e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5423f5ef95b1SJoe Perches 5424f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5425f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5426b13edf7fSJoe Perches } 5427b13edf7fSJoe Perches } 5428b13edf7fSJoe Perches 5429f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 543013214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 543113214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5432cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 543313214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5434cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5435cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5436aad4f614SJoe Perches my @allowed = (); 5437aad4f614SJoe Perches my $allow = 0; 543813214adfSAndy Whitcroft my $seen = 0; 5439773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5440cf655043SAndy Whitcroft my $ln = $linenr - 1; 544113214adfSAndy Whitcroft for my $chunk (@chunks) { 544213214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 544313214adfSAndy Whitcroft 5444773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5445773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5446773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5447773647a0SAndy Whitcroft 5448aad4f614SJoe Perches $allowed[$allow] = 0; 5449773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5450773647a0SAndy Whitcroft 5451773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5452773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5453773647a0SAndy Whitcroft 5454773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5455cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5456cf655043SAndy Whitcroft 5457773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 545813214adfSAndy Whitcroft 545913214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 546013214adfSAndy Whitcroft 5461aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5462cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5463cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5464aad4f614SJoe Perches $allowed[$allow] = 1; 546513214adfSAndy Whitcroft } 546613214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5467cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5468aad4f614SJoe Perches $allowed[$allow] = 1; 546913214adfSAndy Whitcroft } 5470cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5471cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5472aad4f614SJoe Perches $allowed[$allow] = 1; 547313214adfSAndy Whitcroft } 5474aad4f614SJoe Perches $allow++; 547513214adfSAndy Whitcroft } 5476aad4f614SJoe Perches if ($seen) { 5477aad4f614SJoe Perches my $sum_allowed = 0; 5478aad4f614SJoe Perches foreach (@allowed) { 5479aad4f614SJoe Perches $sum_allowed += $_; 5480aad4f614SJoe Perches } 5481aad4f614SJoe Perches if ($sum_allowed == 0) { 5482000d1cc1SJoe Perches WARN("BRACES", 5483000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5484aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5485aad4f614SJoe Perches $seen != $allow) { 5486aad4f614SJoe Perches CHK("BRACES", 5487aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5488aad4f614SJoe Perches } 548913214adfSAndy Whitcroft } 549013214adfSAndy Whitcroft } 549113214adfSAndy Whitcroft } 5492773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 549313214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5494cf655043SAndy Whitcroft my $allowed = 0; 5495f0a594c1SAndy Whitcroft 5496cf655043SAndy Whitcroft # Check the pre-context. 5497cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5498cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5499cf655043SAndy Whitcroft $allowed = 1; 5500f0a594c1SAndy Whitcroft } 5501773647a0SAndy Whitcroft 5502773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5503773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5504773647a0SAndy Whitcroft 5505cf655043SAndy Whitcroft # Check the condition. 5506cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5507773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5508cf655043SAndy Whitcroft if (defined $cond) { 5509773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5510cf655043SAndy Whitcroft } 5511cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5512cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5513cf655043SAndy Whitcroft $allowed = 1; 5514cf655043SAndy Whitcroft } 5515cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5516cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5517cf655043SAndy Whitcroft $allowed = 1; 5518cf655043SAndy Whitcroft } 5519cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5520cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5521cf655043SAndy Whitcroft $allowed = 1; 5522cf655043SAndy Whitcroft } 5523cf655043SAndy Whitcroft # Check the post-context. 5524cf655043SAndy Whitcroft if (defined $chunks[1]) { 5525cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5526cf655043SAndy Whitcroft if (defined $cond) { 5527773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5528cf655043SAndy Whitcroft } 5529cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5530cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5531cf655043SAndy Whitcroft $allowed = 1; 5532cf655043SAndy Whitcroft } 5533cf655043SAndy Whitcroft } 5534cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5535f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5536e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5537cf655043SAndy Whitcroft 5538000d1cc1SJoe Perches WARN("BRACES", 5539000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5540f0a594c1SAndy Whitcroft } 5541f0a594c1SAndy Whitcroft } 5542f0a594c1SAndy Whitcroft 5543e4c5babdSJoe Perches# check for single line unbalanced braces 554495330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 554595330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5546e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5547e4c5babdSJoe Perches } 5548e4c5babdSJoe Perches 55490979ae66SJoe Perches# check for unnecessary blank lines around braces 555077b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5551f8e58219SJoe Perches if (CHK("BRACES", 5552f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5553f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5554f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5555f8e58219SJoe Perches } 55560979ae66SJoe Perches } 555777b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5558f8e58219SJoe Perches if (CHK("BRACES", 5559f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5560f8e58219SJoe Perches $fix) { 5561f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5562f8e58219SJoe Perches } 55630979ae66SJoe Perches } 55640979ae66SJoe Perches 55654a0df2efSAndy Whitcroft# no volatiles please 55666c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 55676c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5568000d1cc1SJoe Perches WARN("VOLATILE", 55698c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 55704a0df2efSAndy Whitcroft } 55714a0df2efSAndy Whitcroft 55725e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 55735e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 55745e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 55755e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 557633acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 55775e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 55785e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 55795e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 55805e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 55815e4f6ba5SJoe Perches $fix && 55825e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 55835e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 55845e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 55855e4f6ba5SJoe Perches my $comma_close = ""; 55865e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 55875e4f6ba5SJoe Perches $comma_close = $1; 55885e4f6ba5SJoe Perches } 55895e4f6ba5SJoe Perches 55905e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 55915e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 55925e4f6ba5SJoe Perches my $fixedline = $prevrawline; 55935e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 55945e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 55955e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 55965e4f6ba5SJoe Perches $fixedline = $rawline; 55975e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 55985e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 55995e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 56005e4f6ba5SJoe Perches } 56015e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 56025e4f6ba5SJoe Perches } 56035e4f6ba5SJoe Perches } 56045e4f6ba5SJoe Perches 56055e4f6ba5SJoe Perches# check for missing a space in a string concatenation 56065e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 56075e4f6ba5SJoe Perches WARN('MISSING_SPACE', 56085e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 56095e4f6ba5SJoe Perches } 56105e4f6ba5SJoe Perches 561177cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5612e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5613e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5614e4b7d309SJoe Perches# function declarations 561577cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 561677cb8546SJoe Perches defined($context_function) && 5617e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5618e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 561977cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5620e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 562177cb8546SJoe Perches } 562277cb8546SJoe Perches 56235e4f6ba5SJoe Perches# check for spaces before a quoted newline 56245e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 56255e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 56265e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 56275e4f6ba5SJoe Perches $fix) { 56285e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 56295e4f6ba5SJoe Perches } 56305e4f6ba5SJoe Perches 56315e4f6ba5SJoe Perches } 56325e4f6ba5SJoe Perches 5633f17dba4fSJoe Perches# concatenated string without spaces between elements 563479682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 563579682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 563679682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 563779682c0cSJoe Perches $fix) { 563879682c0cSJoe Perches while ($line =~ /($String)/g) { 563979682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 564079682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 564179682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 564279682c0cSJoe Perches } 564379682c0cSJoe Perches } 5644f17dba4fSJoe Perches } 5645f17dba4fSJoe Perches 564690ad30e5SJoe Perches# uncoalesced string fragments 564733acb54aSJoe Perches if ($line =~ /$String\s*"/) { 564879682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 564979682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 565079682c0cSJoe Perches $fix) { 565179682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 565279682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 565379682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 565479682c0cSJoe Perches } 565579682c0cSJoe Perches } 565690ad30e5SJoe Perches } 565790ad30e5SJoe Perches 5658522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5659522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5660522b837cSAlexey Dobriyan my $show_Z = 1; 56615e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5662522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 56635e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5664522b837cSAlexey Dobriyan # check for %L 5665522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 56665e4f6ba5SJoe Perches WARN("PRINTF_L", 5667522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5668522b837cSAlexey Dobriyan $show_L = 0; 56695e4f6ba5SJoe Perches } 5670522b837cSAlexey Dobriyan # check for %Z 5671522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5672522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5673522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5674522b837cSAlexey Dobriyan $show_Z = 0; 5675522b837cSAlexey Dobriyan } 5676522b837cSAlexey Dobriyan # check for 0x<decimal> 5677522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5678522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 56796e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 56806e300757SJoe Perches } 56815e4f6ba5SJoe Perches } 56825e4f6ba5SJoe Perches 56835e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 56843f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 56855e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 56865e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 56875e4f6ba5SJoe Perches } 56885e4f6ba5SJoe Perches 568900df344fSAndy Whitcroft# warn about #if 0 5690c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 569160f89010SPrakruthi Deepak Heragu WARN("IF_0", 569260f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 569360f89010SPrakruthi Deepak Heragu } 569460f89010SPrakruthi Deepak Heragu 569560f89010SPrakruthi Deepak Heragu# warn about #if 1 569660f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 569760f89010SPrakruthi Deepak Heragu WARN("IF_1", 569860f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 56994a0df2efSAndy Whitcroft } 57004a0df2efSAndy Whitcroft 570103df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 570203df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5703100425deSJoe Perches my $tested = quotemeta($1); 5704100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5705100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5706100425deSJoe Perches my $func = $1; 5707100425deSJoe Perches if (WARN('NEEDLESS_IF', 5708100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5709100425deSJoe Perches $fix) { 5710100425deSJoe Perches my $do_fix = 1; 5711100425deSJoe Perches my $leading_tabs = ""; 5712100425deSJoe Perches my $new_leading_tabs = ""; 5713100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5714100425deSJoe Perches $leading_tabs = $1; 5715100425deSJoe Perches } else { 5716100425deSJoe Perches $do_fix = 0; 5717100425deSJoe Perches } 5718100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5719100425deSJoe Perches $new_leading_tabs = $1; 5720100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5721100425deSJoe Perches $do_fix = 0; 5722100425deSJoe Perches } 5723100425deSJoe Perches } else { 5724100425deSJoe Perches $do_fix = 0; 5725100425deSJoe Perches } 5726100425deSJoe Perches if ($do_fix) { 5727100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5728100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5729100425deSJoe Perches } 5730100425deSJoe Perches } 57314c432a8fSGreg Kroah-Hartman } 57324c432a8fSGreg Kroah-Hartman } 5733f0a594c1SAndy Whitcroft 5734ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5735ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5736ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5737ebfdc409SJoe Perches (defined $1 || defined $3) && 5738ebfdc409SJoe Perches $linenr > 3) { 5739ebfdc409SJoe Perches my $testval = $2; 5740ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5741ebfdc409SJoe Perches 5742ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5743ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5744ebfdc409SJoe Perches 5745e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 5746e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 5747ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5748ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5749ebfdc409SJoe Perches } 5750ebfdc409SJoe Perches } 5751ebfdc409SJoe Perches 5752f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5753dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5754f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5755f78d98f6SJoe Perches my $level = $1; 5756f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5757f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5758f78d98f6SJoe Perches $fix) { 5759f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5760f78d98f6SJoe Perches } 5761f78d98f6SJoe Perches } 5762f78d98f6SJoe Perches 576345c55e92SJoe Perches# check for logging continuations 576445c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 576545c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 576645c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 576745c55e92SJoe Perches } 576845c55e92SJoe Perches 5769abb08a53SJoe Perches# check for mask then right shift without a parentheses 57705b57980dSJoe Perches if ($perl_version_ok && 5771abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5772abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5773abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5774abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5775abb08a53SJoe Perches } 5776abb08a53SJoe Perches 5777b75ac618SJoe Perches# check for pointer comparisons to NULL 57785b57980dSJoe Perches if ($perl_version_ok) { 5779b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5780b75ac618SJoe Perches my $val = $1; 5781b75ac618SJoe Perches my $equal = "!"; 5782b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5783b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5784b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5785b75ac618SJoe Perches $fix) { 5786b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5787b75ac618SJoe Perches } 5788b75ac618SJoe Perches } 5789b75ac618SJoe Perches } 5790b75ac618SJoe Perches 57918716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 57928716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 57938716de38SJoe Perches my $attr = $1; 57948716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 57958716de38SJoe Perches my $ptr = $1; 57968716de38SJoe Perches my $var = $2; 57978716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 57988716de38SJoe Perches ERROR("MISPLACED_INIT", 57998716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 58008716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 58018716de38SJoe Perches WARN("MISPLACED_INIT", 58028716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 58038716de38SJoe Perches $fix) { 5804194f66fcSJoe 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; 58058716de38SJoe Perches } 58068716de38SJoe Perches } 58078716de38SJoe Perches } 58088716de38SJoe Perches 5809e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5810e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5811e970b884SJoe Perches my $attr = $1; 5812e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5813e970b884SJoe Perches my $attr_prefix = $1; 5814e970b884SJoe Perches my $attr_type = $2; 5815e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5816e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5817e970b884SJoe Perches $fix) { 5818194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5819e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5820e970b884SJoe Perches } 5821e970b884SJoe Perches } 5822e970b884SJoe Perches 5823e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5824e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5825e970b884SJoe Perches my $attr = $1; 5826e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5827e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5828e970b884SJoe Perches $fix) { 5829194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5830e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5831e970b884SJoe Perches $lead = rtrim($1); 5832e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5833e970b884SJoe Perches $lead = "${lead}const "; 5834194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5835e970b884SJoe Perches } 5836e970b884SJoe Perches } 5837e970b884SJoe Perches 5838c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5839c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5840c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5841c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5842c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5843c17893c7SJoe Perches $fix) { 5844c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5845c17893c7SJoe Perches } 5846c17893c7SJoe Perches } 5847c17893c7SJoe Perches 5848fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5849fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5850fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5851fbdb8138SJoe Perches my $constant_func = $1; 5852fbdb8138SJoe Perches my $func = $constant_func; 5853fbdb8138SJoe Perches $func =~ s/^__constant_//; 5854fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5855fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5856fbdb8138SJoe Perches $fix) { 5857194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5858fbdb8138SJoe Perches } 5859fbdb8138SJoe Perches } 5860fbdb8138SJoe Perches 58611a15a250SPatrick Pannuto# prefer usleep_range over udelay 586237581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 586343c1d77cSJoe Perches my $delay = $1; 58641a15a250SPatrick Pannuto # ignore udelay's < 10, however 586543c1d77cSJoe Perches if (! ($delay < 10) ) { 5866000d1cc1SJoe Perches CHK("USLEEP_RANGE", 5867458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 586843c1d77cSJoe Perches } 586943c1d77cSJoe Perches if ($delay > 2000) { 587043c1d77cSJoe Perches WARN("LONG_UDELAY", 587143c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 58721a15a250SPatrick Pannuto } 58731a15a250SPatrick Pannuto } 58741a15a250SPatrick Pannuto 587509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 587609ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 587709ef8725SPatrick Pannuto if ($1 < 20) { 5878000d1cc1SJoe Perches WARN("MSLEEP", 5879458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 588009ef8725SPatrick Pannuto } 588109ef8725SPatrick Pannuto } 588209ef8725SPatrick Pannuto 588336ec1939SJoe Perches# check for comparisons of jiffies 588436ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 588536ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 588636ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 588736ec1939SJoe Perches } 588836ec1939SJoe Perches 58899d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 58909d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 58919d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 58929d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 58939d7a34a5SJoe Perches } 58949d7a34a5SJoe Perches 589500df344fSAndy Whitcroft# warn about #ifdefs in C files 5896c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 589700df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 589800df344fSAndy Whitcroft# print "$herecurr"; 589900df344fSAndy Whitcroft# $clean = 0; 590000df344fSAndy Whitcroft# } 590100df344fSAndy Whitcroft 590222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5903c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 59043705ce5bSJoe Perches if (ERROR("SPACING", 59053705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 59063705ce5bSJoe Perches $fix) { 5907194f66fcSJoe Perches $fixed[$fixlinenr] =~ 59083705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 59093705ce5bSJoe Perches } 59103705ce5bSJoe Perches 591122f2a2efSAndy Whitcroft } 591222f2a2efSAndy Whitcroft 59134a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5914171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5915171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 59164a0df2efSAndy Whitcroft my $which = $1; 59174a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5918000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5919000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 59204a0df2efSAndy Whitcroft } 59214a0df2efSAndy Whitcroft } 59224a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5923402c2553SMichael S. Tsirkin 5924402c2553SMichael S. Tsirkin my $barriers = qr{ 5925402c2553SMichael S. Tsirkin mb| 5926402c2553SMichael S. Tsirkin rmb| 5927ad83ec6cSWill Deacon wmb 5928402c2553SMichael S. Tsirkin }x; 5929402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5930402c2553SMichael S. Tsirkin mb__before_atomic| 5931402c2553SMichael S. Tsirkin mb__after_atomic| 5932402c2553SMichael S. Tsirkin store_release| 5933402c2553SMichael S. Tsirkin load_acquire| 5934402c2553SMichael S. Tsirkin store_mb| 5935402c2553SMichael S. Tsirkin (?:$barriers) 5936402c2553SMichael S. Tsirkin }x; 5937402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5938402c2553SMichael S. Tsirkin (?:$barriers)| 593943e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 594043e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5941402c2553SMichael S. Tsirkin }x; 5942402c2553SMichael S. Tsirkin 5943402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 59444a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5945c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5946000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 59474a0df2efSAndy Whitcroft } 59484a0df2efSAndy Whitcroft } 59493ad81779SPaul E. McKenney 5950f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5951f4073b0fSMichael S. Tsirkin 5952f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5953f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5954f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5955f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5956f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5957f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5958f4073b0fSMichael S. Tsirkin } 5959f4073b0fSMichael S. Tsirkin 5960cb426e99SJoe Perches# check for waitqueue_active without a comment. 5961cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5962cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5963cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5964cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5965cb426e99SJoe Perches } 5966cb426e99SJoe Perches } 59673ad81779SPaul E. McKenney 59685099a722SMarco Elver# check for data_race without a comment. 59695099a722SMarco Elver if ($line =~ /\bdata_race\s*\(/) { 59705099a722SMarco Elver if (!ctx_has_comment($first_line, $linenr)) { 59715099a722SMarco Elver WARN("DATA_RACE", 59725099a722SMarco Elver "data_race without comment\n" . $herecurr); 59735099a722SMarco Elver } 59745099a722SMarco Elver } 59755099a722SMarco Elver 59764a0df2efSAndy Whitcroft# check of hardware specific defines 5977c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5978000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5979000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 59800a920b5bSAndy Whitcroft } 5981653d4876SAndy Whitcroft 5982596ed45bSJoe Perches# check that the storage class is not after a type 5983596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5984000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5985596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5986596ed45bSJoe Perches } 5987596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5988596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5989596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5990596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5991596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5992596ed45bSJoe Perches WARN("STORAGE_CLASS", 5993596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5994d4977c78STobias Klauser } 5995d4977c78STobias Klauser 5996de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5997de7d4f0eSAndy Whitcroft# storage class and type. 59989c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 59999c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 6000000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 6001000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 6002de7d4f0eSAndy Whitcroft } 6003de7d4f0eSAndy Whitcroft 60048905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 60052b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 60062b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 6007d5e616fcSJoe Perches if (WARN("INLINE", 6008d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 6009d5e616fcSJoe Perches $fix) { 6010194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 6011d5e616fcSJoe Perches 6012d5e616fcSJoe Perches } 60138905a67cSAndy Whitcroft } 60148905a67cSAndy Whitcroft 60153d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 60162b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 60172b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 6018000d1cc1SJoe Perches WARN("PREFER_PACKED", 6019000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 60203d130fd0SJoe Perches } 60213d130fd0SJoe Perches 602239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 60232b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 60242b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 6025000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 6026000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 602739b7e287SJoe Perches } 602839b7e287SJoe Perches 6029462811d9SJoe Perches# Check for __attribute__ section, prefer __section 6030462811d9SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6031462811d9SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { 6032462811d9SJoe Perches my $old = substr($rawline, $-[1], $+[1] - $-[1]); 6033462811d9SJoe Perches my $new = substr($old, 1, -1); 6034462811d9SJoe Perches if (WARN("PREFER_SECTION", 6035462811d9SJoe Perches "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && 6036462811d9SJoe Perches $fix) { 6037462811d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; 6038462811d9SJoe Perches } 6039462811d9SJoe Perches } 6040462811d9SJoe Perches 60415f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 60422b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 60432b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 6044d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 6045d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 6046d5e616fcSJoe Perches $fix) { 6047194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 6048d5e616fcSJoe Perches 6049d5e616fcSJoe Perches } 60505f14d3bdSJoe Perches } 60515f14d3bdSJoe Perches 60526061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 60532b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 60542b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 6055d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 6056d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 6057d5e616fcSJoe Perches $fix) { 6058194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 6059d5e616fcSJoe Perches } 60606061d949SJoe Perches } 60616061d949SJoe Perches 6062619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 60635b57980dSJoe Perches if ($perl_version_ok && 6064619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 6065619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6066619a908aSJoe Perches $line =~ /\b__weak\b/)) { 6067619a908aSJoe Perches ERROR("WEAK_DECLARATION", 6068619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 6069619a908aSJoe Perches } 6070619a908aSJoe Perches 6071fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 6072e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6073fd39f904STomas Winkler $realfile !~ m@\btools/@ && 6074e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6075e6176fa4SJoe Perches my $type = $1; 6076e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 6077e6176fa4SJoe Perches $type = $1; 6078e6176fa4SJoe Perches my $kernel_type = 'u'; 6079e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 6080e6176fa4SJoe Perches $type =~ /(\d+)/; 6081e6176fa4SJoe Perches $kernel_type .= $1; 6082e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 6083e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6084e6176fa4SJoe Perches $fix) { 6085e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6086e6176fa4SJoe Perches } 6087e6176fa4SJoe Perches } 6088e6176fa4SJoe Perches } 6089e6176fa4SJoe Perches 6090938224b5SJoe Perches# check for cast of C90 native int or longer types constants 6091938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6092938224b5SJoe Perches my $cast = $1; 6093938224b5SJoe Perches my $const = $2; 6094938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 6095938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 6096938224b5SJoe Perches $fix) { 6097938224b5SJoe Perches my $suffix = ""; 6098938224b5SJoe Perches my $newconst = $const; 6099938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 6100938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6101938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6102938224b5SJoe Perches $suffix .= 'LL'; 6103938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6104938224b5SJoe Perches $suffix .= 'L'; 6105938224b5SJoe Perches } 6106938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6107938224b5SJoe Perches } 6108938224b5SJoe Perches } 6109938224b5SJoe Perches 61108f53a9b8SJoe Perches# check for sizeof(&) 61118f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6112000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6113000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 61148f53a9b8SJoe Perches } 61158f53a9b8SJoe Perches 611666c80b60SJoe Perches# check for sizeof without parenthesis 611766c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6118d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6119d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6120d5e616fcSJoe Perches $fix) { 6121194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6122d5e616fcSJoe Perches } 612366c80b60SJoe Perches } 612466c80b60SJoe Perches 612588982feaSJoe Perches# check for struct spinlock declarations 612688982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 612788982feaSJoe Perches WARN("USE_SPINLOCK_T", 612888982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 612988982feaSJoe Perches } 613088982feaSJoe Perches 6131a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 613206668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6133a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6134caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6135caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6136d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6137d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6138d5e616fcSJoe Perches $fix) { 6139194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6140d5e616fcSJoe Perches } 6141a6962d72SJoe Perches } 6142a6962d72SJoe Perches } 6143a6962d72SJoe Perches 61440b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 61455b57980dSJoe Perches if ($perl_version_ok && 61460b523769SJoe Perches defined $stat && 61470b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 61480b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6149e3c6bc95STobin C. Harding my $stat_real; 6150e3c6bc95STobin C. Harding 61510b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 61520b523769SJoe Perches $lc = $lc + $linenr; 61530b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6154ffe07513SJoe Perches my $specifier; 6155ffe07513SJoe Perches my $extension; 61563bd32d6aSSakari Ailus my $qualifier; 6157ffe07513SJoe Perches my $bad_specifier = ""; 61580b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 61590b523769SJoe Perches $fmt =~ s/%%//g; 6160e3c6bc95STobin C. Harding 61613bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6162e3c6bc95STobin C. Harding $specifier = $1; 6163e3c6bc95STobin C. Harding $extension = $2; 61643bd32d6aSSakari Ailus $qualifier = $3; 6165361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 61663bd32d6aSSakari Ailus ($extension eq "f" && 61673bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6168e3c6bc95STobin C. Harding $bad_specifier = $specifier; 61690b523769SJoe Perches last; 61700b523769SJoe Perches } 6171e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6172e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6173e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 61740b523769SJoe Perches } 6175e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6176e3c6bc95STobin 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"); 6177e3c6bc95STobin C. Harding } 6178e3c6bc95STobin C. Harding } 6179e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 61802a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 61811df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 61821df7338aSSergey Senozhatsky my $use = ""; 6183e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 61841df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6185e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 61861df7338aSSergey Senozhatsky } 61872a9f9d85STobin C. Harding 61880b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6189e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6190e3c6bc95STobin C. Harding } 61910b523769SJoe Perches } 61920b523769SJoe Perches } 61930b523769SJoe Perches 6194554e165cSAndy Whitcroft# Check for misused memsets 61955b57980dSJoe Perches if ($perl_version_ok && 6196d1fe9c09SJoe Perches defined $stat && 61979e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6198554e165cSAndy Whitcroft 6199d7c76ba7SJoe Perches my $ms_addr = $2; 6200d1fe9c09SJoe Perches my $ms_val = $7; 6201d1fe9c09SJoe Perches my $ms_size = $12; 6202d7c76ba7SJoe Perches 6203554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6204554e165cSAndy Whitcroft ERROR("MEMSET", 6205d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6206554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6207554e165cSAndy Whitcroft WARN("MEMSET", 6208d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6209d7c76ba7SJoe Perches } 6210d7c76ba7SJoe Perches } 6211d7c76ba7SJoe Perches 621298a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 62135b57980dSJoe Perches# if ($perl_version_ok && 6214f333195dSJoe Perches# defined $stat && 6215f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6216f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6217f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6218f333195dSJoe Perches# $fix) { 6219f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6220f333195dSJoe Perches# } 6221f333195dSJoe Perches# } 622298a9bba5SJoe Perches 6223b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 62245b57980dSJoe Perches# if ($perl_version_ok && 6225f333195dSJoe Perches# defined $stat && 6226f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6227f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6228f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6229f333195dSJoe Perches# } 6230b6117d17SMateusz Kulikowski 62318617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 62328617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 62335b57980dSJoe Perches# if ($perl_version_ok && 6234f333195dSJoe Perches# defined $stat && 6235f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6236f333195dSJoe Perches# 6237f333195dSJoe Perches# my $ms_val = $7; 6238f333195dSJoe Perches# 6239f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6240f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6241f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6242f333195dSJoe Perches# $fix) { 6243f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6244f333195dSJoe Perches# } 6245f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6246f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6247f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6248f333195dSJoe Perches# $fix) { 6249f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6250f333195dSJoe Perches# } 6251f333195dSJoe Perches# } 6252f333195dSJoe Perches# } 62538617cd09SMateusz Kulikowski 6254d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 62555b57980dSJoe Perches if ($perl_version_ok && 6256d1fe9c09SJoe Perches defined $stat && 6257d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6258d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6259d7c76ba7SJoe Perches my $call = $1; 6260d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6261d7c76ba7SJoe Perches my $arg1 = $3; 6262d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6263d1fe9c09SJoe Perches my $arg2 = $8; 6264d7c76ba7SJoe Perches my $cast; 6265d7c76ba7SJoe Perches 6266d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6267d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6268d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6269d7c76ba7SJoe Perches $cast = $cast1; 6270d7c76ba7SJoe Perches } else { 6271d7c76ba7SJoe Perches $cast = $cast2; 6272d7c76ba7SJoe Perches } 6273d7c76ba7SJoe Perches WARN("MINMAX", 6274d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6275554e165cSAndy Whitcroft } 6276554e165cSAndy Whitcroft } 6277554e165cSAndy Whitcroft 62784a273195SJoe Perches# check usleep_range arguments 62795b57980dSJoe Perches if ($perl_version_ok && 62804a273195SJoe Perches defined $stat && 62814a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 62824a273195SJoe Perches my $min = $1; 62834a273195SJoe Perches my $max = $7; 62844a273195SJoe Perches if ($min eq $max) { 62854a273195SJoe Perches WARN("USLEEP_RANGE", 6286458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 62874a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 62884a273195SJoe Perches $min > $max) { 62894a273195SJoe Perches WARN("USLEEP_RANGE", 6290458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 62914a273195SJoe Perches } 62924a273195SJoe Perches } 62934a273195SJoe Perches 6294823b794cSJoe Perches# check for naked sscanf 62955b57980dSJoe Perches if ($perl_version_ok && 6296823b794cSJoe Perches defined $stat && 62976c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6298823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6299823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6300823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6301823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6302823b794cSJoe Perches $lc = $lc + $linenr; 63032a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6304823b794cSJoe Perches WARN("NAKED_SSCANF", 6305823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6306823b794cSJoe Perches } 6307823b794cSJoe Perches 6308afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 63095b57980dSJoe Perches if ($perl_version_ok && 6310afc819abSJoe Perches defined $stat && 6311afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6312afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6313afc819abSJoe Perches $lc = $lc + $linenr; 63142a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6315afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6316afc819abSJoe Perches my $format = $6; 6317afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6318afc819abSJoe Perches if ($count == 1 && 6319afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6320afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6321afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6322afc819abSJoe Perches } 6323afc819abSJoe Perches } 6324afc819abSJoe Perches } 6325afc819abSJoe Perches 632670dc8a48SJoe Perches# check for new externs in .h files. 632770dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 632870dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6329d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 633070dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 633170dc8a48SJoe Perches $fix) { 6332194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 633370dc8a48SJoe Perches } 633470dc8a48SJoe Perches } 633570dc8a48SJoe Perches 6336de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6337171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6338c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6339171ae1a4SAndy Whitcroft { 6340c45dcabdSAndy Whitcroft my $function_name = $1; 6341c45dcabdSAndy Whitcroft my $paren_space = $2; 6342171ae1a4SAndy Whitcroft 6343171ae1a4SAndy Whitcroft my $s = $stat; 6344171ae1a4SAndy Whitcroft if (defined $cond) { 6345171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6346171ae1a4SAndy Whitcroft } 6347d8b44b58SKees Cook if ($s =~ /^\s*;/) 6348c45dcabdSAndy Whitcroft { 6349000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6350000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6351de7d4f0eSAndy Whitcroft } 6352de7d4f0eSAndy Whitcroft 6353171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6354000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6355000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6356171ae1a4SAndy Whitcroft } 63579c9ba34eSAndy Whitcroft 63589c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 63599c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 63609c9ba34eSAndy Whitcroft { 6361000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6362000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6363171ae1a4SAndy Whitcroft } 6364171ae1a4SAndy Whitcroft 6365a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6366a0ad7596SJoe Perches if (defined $stat && 6367d8b44b58SKees Cook $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6368d8b44b58SKees Cook $1 ne "void") { 6369d8b44b58SKees Cook my $args = trim($1); 6370ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6371ca0d8929SJoe Perches my $arg = trim($1); 6372d8b44b58SKees Cook if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6373ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6374ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6375ca0d8929SJoe Perches } 6376ca0d8929SJoe Perches } 6377ca0d8929SJoe Perches } 6378ca0d8929SJoe Perches 6379a0ad7596SJoe Perches# check for function definitions 63805b57980dSJoe Perches if ($perl_version_ok && 6381a0ad7596SJoe Perches defined $stat && 6382a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6383a0ad7596SJoe Perches $context_function = $1; 6384a0ad7596SJoe Perches 6385a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6386a0ad7596SJoe Perches my $ok = 0; 6387a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6388a0ad7596SJoe Perches my $herectx = $here . "\n"; 6389a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6390a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6391a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6392a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6393a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6394a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6395a0ad7596SJoe Perches } 6396a0ad7596SJoe Perches if (!$ok) { 6397a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6398a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6399a0ad7596SJoe Perches } 6400a0ad7596SJoe Perches } 6401a0ad7596SJoe Perches 6402de7d4f0eSAndy Whitcroft# checks for new __setup's 6403de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6404de7d4f0eSAndy Whitcroft my $name = $1; 6405de7d4f0eSAndy Whitcroft 6406de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6407000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 64082581ac7cSTim Froidcoeur "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); 6409de7d4f0eSAndy Whitcroft } 6410653d4876SAndy Whitcroft } 64119c0ca6f9SAndy Whitcroft 6412e29a70f1SJoe Perches# check for pointless casting of alloc functions 6413e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6414000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6415000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 64169c0ca6f9SAndy Whitcroft } 641713214adfSAndy Whitcroft 6418a640d25cSJoe Perches# alloc style 6419a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 64205b57980dSJoe Perches if ($perl_version_ok && 6421e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6422a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6423a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6424a640d25cSJoe Perches } 6425a640d25cSJoe Perches 642660a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 64275b57980dSJoe Perches if ($perl_version_ok && 64281b4a2ed4SJoe Perches defined $stat && 64291b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 643060a55369SJoe Perches my $oldfunc = $3; 643160a55369SJoe Perches my $a1 = $4; 643260a55369SJoe Perches my $a2 = $10; 643360a55369SJoe Perches my $newfunc = "kmalloc_array"; 643460a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 643560a55369SJoe Perches my $r1 = $a1; 643660a55369SJoe Perches my $r2 = $a2; 643760a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 643860a55369SJoe Perches $r1 = $a2; 643960a55369SJoe Perches $r2 = $a1; 644060a55369SJoe Perches } 6441e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6442e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 64431b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6444e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6445e3d95a2aSTobin C. Harding 6446e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 64471b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 64481b4a2ed4SJoe Perches $cnt == 1 && 6449e367455aSJoe Perches $fix) { 6450194f66fcSJoe 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; 645160a55369SJoe Perches } 645260a55369SJoe Perches } 645360a55369SJoe Perches } 645460a55369SJoe Perches 6455972fdea2SJoe Perches# check for krealloc arg reuse 64565b57980dSJoe Perches if ($perl_version_ok && 64574cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 64584cab63ceSJoe Perches $1 eq $3) { 6459972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6460972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6461972fdea2SJoe Perches } 6462972fdea2SJoe Perches 64635ce59ae0SJoe Perches# check for alloc argument mismatch 64645ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 64655ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 64665ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 64675ce59ae0SJoe Perches } 64685ce59ae0SJoe Perches 6469caf2a54fSJoe Perches# check for multiple semicolons 6470caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6471d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6472d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6473d5e616fcSJoe Perches $fix) { 6474194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6475d5e616fcSJoe Perches } 6476d1e2ad07SJoe Perches } 6477d1e2ad07SJoe Perches 6478cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6479cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6480cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 64810ab90191SJoe Perches my $ull = ""; 64820ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 64830ab90191SJoe Perches if (CHK("BIT_MACRO", 64840ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 64850ab90191SJoe Perches $fix) { 64860ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 64870ab90191SJoe Perches } 64880ab90191SJoe Perches } 64890ab90191SJoe Perches 649050161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) 649150161266SJoe Perches if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^CONFIG_/) { 649250161266SJoe Perches WARN("IS_ENABLED_CONFIG", 649350161266SJoe Perches "IS_ENABLED($1) is normally used as IS_ENABLED(CONFIG_$1)\n" . $herecurr); 649450161266SJoe Perches } 649550161266SJoe Perches 64962d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 64972d632745SJoe Perches if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { 64982d632745SJoe Perches my $config = $1; 64992d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 65002d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 65012d632745SJoe Perches $fix) { 65022d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 65032d632745SJoe Perches } 65042d632745SJoe Perches } 65052d632745SJoe Perches 6506e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6507c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6508c34c09a8SJoe Perches my $has_break = 0; 6509c34c09a8SJoe Perches my $has_statement = 0; 6510c34c09a8SJoe Perches my $count = 0; 6511c34c09a8SJoe Perches my $prevline = $linenr; 6512e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6513c34c09a8SJoe Perches $prevline--; 6514c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6515c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6516c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6517c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6518c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6519c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6520c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6521c34c09a8SJoe Perches $has_statement = 1; 6522c34c09a8SJoe Perches $count++; 6523258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6524c34c09a8SJoe Perches } 6525c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6526c34c09a8SJoe Perches WARN("MISSING_BREAK", 6527224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6528c34c09a8SJoe Perches } 6529c34c09a8SJoe Perches } 6530c34c09a8SJoe Perches 6531f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 6532f36d3eb8SJoe Perches my @fallthroughs = ( 6533f36d3eb8SJoe Perches 'fallthrough', 6534f36d3eb8SJoe Perches '@fallthrough@', 6535f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 6536f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 6537f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 6538f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6539f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6540f36d3eb8SJoe Perches ); 6541f36d3eb8SJoe Perches if ($raw_comment ne '') { 6542f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 6543f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 6544f36d3eb8SJoe Perches my $msg_level = \&WARN; 6545f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 6546f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 6547f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 6548f36d3eb8SJoe Perches last; 6549f36d3eb8SJoe Perches } 6550f36d3eb8SJoe Perches } 6551f36d3eb8SJoe Perches } 6552f36d3eb8SJoe Perches 6553d1e2ad07SJoe Perches# check for switch/default statements without a break; 65545b57980dSJoe Perches if ($perl_version_ok && 6555d1e2ad07SJoe Perches defined $stat && 6556d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6557d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6558e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6559e3d95a2aSTobin C. Harding 6560d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6561d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6562caf2a54fSJoe Perches } 6563caf2a54fSJoe Perches 656413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6565d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6566d5e616fcSJoe Perches if (WARN("USE_FUNC", 6567d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6568d5e616fcSJoe Perches $fix) { 6569194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6570d5e616fcSJoe Perches } 657113214adfSAndy Whitcroft } 6572773647a0SAndy Whitcroft 657362ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 657462ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 657562ec818fSJoe Perches ERROR("DATE_TIME", 657662ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 657762ec818fSJoe Perches } 657862ec818fSJoe Perches 65792c92488aSJoe Perches# check for use of yield() 65802c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 65812c92488aSJoe Perches WARN("YIELD", 65822c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 65832c92488aSJoe Perches } 65842c92488aSJoe Perches 6585179f8f40SJoe Perches# check for comparisons against true and false 6586179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6587179f8f40SJoe Perches my $lead = $1; 6588179f8f40SJoe Perches my $arg = $2; 6589179f8f40SJoe Perches my $test = $3; 6590179f8f40SJoe Perches my $otype = $4; 6591179f8f40SJoe Perches my $trail = $5; 6592179f8f40SJoe Perches my $op = "!"; 6593179f8f40SJoe Perches 6594179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6595179f8f40SJoe Perches 6596179f8f40SJoe Perches my $type = lc($otype); 6597179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6598179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6599179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6600179f8f40SJoe Perches $op = ""; 6601179f8f40SJoe Perches } 6602179f8f40SJoe Perches 6603179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6604179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6605179f8f40SJoe Perches 6606179f8f40SJoe Perches## maybe suggesting a correct construct would better 6607179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6608179f8f40SJoe Perches 6609179f8f40SJoe Perches } 6610179f8f40SJoe Perches } 6611179f8f40SJoe Perches 66124882720bSThomas Gleixner# check for semaphores initialized locked 66134882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6614000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6615000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6616773647a0SAndy Whitcroft } 66176712d858SJoe Perches 661867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 661967d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6620000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 662167d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6622773647a0SAndy Whitcroft } 66236712d858SJoe Perches 6624ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6625f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6626000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6627ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6628f3db6639SMichael Ellerman } 66296712d858SJoe Perches 66303d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 66313d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 66323d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 66333d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 66343d709ab5SPaul E. McKenney } 66353d709ab5SPaul E. McKenney 66369189c7e7SJoe Perches# check for deprecated apis 66379189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 66389189c7e7SJoe Perches my $deprecated_api = $1; 66399189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 66409189c7e7SJoe Perches WARN("DEPRECATED_API", 66419189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 66429189c7e7SJoe Perches } 66439189c7e7SJoe Perches 66440f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6645d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 66466903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6647d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6648000d1cc1SJoe Perches WARN("CONST_STRUCT", 6649d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 66502b6db5cbSAndy Whitcroft } 6651773647a0SAndy Whitcroft 6652773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6653773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6654773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6655c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6656c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6657171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6658171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6659171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6660773647a0SAndy Whitcroft { 6661000d1cc1SJoe Perches WARN("NR_CPUS", 6662000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6663773647a0SAndy Whitcroft } 66649c9ba34eSAndy Whitcroft 666552ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 666652ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 666752ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 666852ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 666952ea8506SJoe Perches } 667052ea8506SJoe Perches 6671acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 66725b57980dSJoe Perches if ($perl_version_ok && 6673acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6674acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6675acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6676acd9362cSJoe Perches } 6677acd9362cSJoe Perches 6678de3f186fSDenis Efremov# nested likely/unlikely calls 6679de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 6680de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 6681de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 6682de3f186fSDenis Efremov } 6683de3f186fSDenis Efremov 6684691d77b6SAndy Whitcroft# whine mightly about in_atomic 6685691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6686691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6687000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6688000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6689f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6690000d1cc1SJoe Perches WARN("IN_ATOMIC", 6691000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6692691d77b6SAndy Whitcroft } 6693691d77b6SAndy Whitcroft } 66941704f47bSPeter Zijlstra 66950f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 66960f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 66970f5225b0SPeter Zijlstra ERROR("LOCKING", 66980f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 66990f5225b0SPeter Zijlstra } 67000f5225b0SPeter Zijlstra 67011704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 67021704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 67031704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 67041704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 67051704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 67061704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6707000d1cc1SJoe Perches ERROR("LOCKDEP", 6708000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 67091704f47bSPeter Zijlstra } 67101704f47bSPeter Zijlstra } 671188f8831cSDave Jones 6712b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6713b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6714000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6715000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 671688f8831cSDave Jones } 67172435880fSJoe Perches 671800180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 671900180468SJoe Perches# and whether or not function naming is typical and if 672000180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 67215b57980dSJoe Perches if ($perl_version_ok && 672200180468SJoe Perches defined $stat && 672300180468SJoe 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*\)/) { 672400180468SJoe Perches my $var = $1; 672500180468SJoe Perches my $perms = $2; 672600180468SJoe Perches my $show = $3; 672700180468SJoe Perches my $store = $4; 672800180468SJoe Perches my $octal_perms = perms_to_octal($perms); 672900180468SJoe Perches if ($show =~ /^${var}_show$/ && 673000180468SJoe Perches $store =~ /^${var}_store$/ && 673100180468SJoe Perches $octal_perms eq "0644") { 673200180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 673300180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 673400180468SJoe Perches $fix) { 673500180468SJoe 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})/; 673600180468SJoe Perches } 673700180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 673800180468SJoe Perches $store =~ /^NULL$/ && 673900180468SJoe Perches $octal_perms eq "0444") { 674000180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 674100180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 674200180468SJoe Perches $fix) { 674300180468SJoe 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})/; 674400180468SJoe Perches } 674500180468SJoe Perches } elsif ($show =~ /^NULL$/ && 674600180468SJoe Perches $store =~ /^${var}_store$/ && 674700180468SJoe Perches $octal_perms eq "0200") { 674800180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 674900180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 675000180468SJoe Perches $fix) { 675100180468SJoe 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})/; 675200180468SJoe Perches } 675300180468SJoe Perches } elsif ($octal_perms eq "0644" || 675400180468SJoe Perches $octal_perms eq "0444" || 675500180468SJoe Perches $octal_perms eq "0200") { 675600180468SJoe Perches my $newshow = "$show"; 675700180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 675800180468SJoe Perches my $newstore = $store; 675900180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 676000180468SJoe Perches my $rename = ""; 676100180468SJoe Perches if ($show ne $newshow) { 676200180468SJoe Perches $rename .= " '$show' to '$newshow'"; 676300180468SJoe Perches } 676400180468SJoe Perches if ($store ne $newstore) { 676500180468SJoe Perches $rename .= " '$store' to '$newstore'"; 676600180468SJoe Perches } 676700180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 676800180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 676900180468SJoe Perches } else { 677000180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 677100180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 677200180468SJoe Perches } 677300180468SJoe Perches } 677400180468SJoe Perches 6775515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6776515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 677773121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 677873121534SJoe Perches# specific definition of not visible in sysfs. 677973121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 678073121534SJoe Perches# use the default permissions 67815b57980dSJoe Perches if ($perl_version_ok && 6782459cf0aeSJoe Perches defined $stat && 6783515a235eSJoe Perches $line =~ /$mode_perms_search/) { 67842435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 67852435880fSJoe Perches my $func = $entry->[0]; 67862435880fSJoe Perches my $arg_pos = $entry->[1]; 67872435880fSJoe Perches 6788459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6789459cf0aeSJoe Perches $lc = $lc + $linenr; 67902a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6791459cf0aeSJoe Perches 67922435880fSJoe Perches my $skip_args = ""; 67932435880fSJoe Perches if ($arg_pos > 1) { 67942435880fSJoe Perches $arg_pos--; 67952435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 67962435880fSJoe Perches } 6797f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6798459cf0aeSJoe Perches if ($stat =~ /$test/) { 67992435880fSJoe Perches my $val = $1; 68002435880fSJoe Perches $val = $6 if ($skip_args ne ""); 680173121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 680273121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 680373121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 68042435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6805459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6806f90774e1SJoe Perches } 6807f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6808c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6809459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 68102435880fSJoe Perches } 6811459cf0aeSJoe Perches } 6812459cf0aeSJoe Perches } 6813459cf0aeSJoe Perches } 6814459cf0aeSJoe Perches 6815459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6816bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 681700180468SJoe Perches my $oval = $1; 681800180468SJoe Perches my $octal = perms_to_octal($oval); 6819f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6820459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6821f90774e1SJoe Perches $fix) { 682200180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 68232435880fSJoe Perches } 682413214adfSAndy Whitcroft } 68255a6d20ceSBjorn Andersson 68265a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 68275a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 68285a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 68295a6d20ceSBjorn Andersson my $valid_licenses = qr{ 68305a6d20ceSBjorn Andersson GPL| 68315a6d20ceSBjorn Andersson GPL\ v2| 68325a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 68335a6d20ceSBjorn Andersson Dual\ BSD/GPL| 68345a6d20ceSBjorn Andersson Dual\ MIT/GPL| 68355a6d20ceSBjorn Andersson Dual\ MPL/GPL| 68365a6d20ceSBjorn Andersson Proprietary 68375a6d20ceSBjorn Andersson }x; 68385a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 68395a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 68405a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 68415a6d20ceSBjorn Andersson } 68425a6d20ceSBjorn Andersson } 68436a8d76cbSMatteo Croce 68446a8d76cbSMatteo Croce# check for sysctl duplicate constants 68456a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 68466a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 68476a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 68486a8d76cbSMatteo Croce } 6849515a235eSJoe Perches } 685013214adfSAndy Whitcroft 685113214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 685213214adfSAndy Whitcroft # so just keep quiet. 685313214adfSAndy Whitcroft if ($#rawlines == -1) { 685413214adfSAndy Whitcroft exit(0); 68550a920b5bSAndy Whitcroft } 68560a920b5bSAndy Whitcroft 68578905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 68588905a67cSAndy Whitcroft # things that appear to be patches. 68598905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 68608905a67cSAndy Whitcroft exit(0); 68618905a67cSAndy Whitcroft } 68628905a67cSAndy Whitcroft 68638905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 68648905a67cSAndy Whitcroft # just keep quiet. 68658905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 68668905a67cSAndy Whitcroft exit(0); 68678905a67cSAndy Whitcroft } 68688905a67cSAndy Whitcroft 6869a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6870000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6871000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 68720a920b5bSAndy Whitcroft } 6873cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6874cd261496SGeert Uytterhoeven if ($signoff == 0) { 6875000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6876000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 6877cd261496SGeert Uytterhoeven } elsif (!$authorsignoff) { 6878cd261496SGeert Uytterhoeven WARN("NO_AUTHOR_SIGN_OFF", 6879cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 6880cd261496SGeert Uytterhoeven } 68810a920b5bSAndy Whitcroft } 68820a920b5bSAndy Whitcroft 6883f0a594c1SAndy Whitcroft print report_dump(); 688413214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 688513214adfSAndy Whitcroft print "$filename " if ($summary_file); 68866c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 68876c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 68886c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 68896c72ffaaSAndy Whitcroft } 68908905a67cSAndy Whitcroft 6891d2c0a235SAndy Whitcroft if ($quiet == 0) { 6892ef212196SJoe Perches # If there were any defects found and not already fixing them 6893ef212196SJoe Perches if (!$clean and !$fix) { 6894ef212196SJoe Perches print << "EOM" 6895ef212196SJoe Perches 6896ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6897ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6898ef212196SJoe PerchesEOM 6899ef212196SJoe Perches } 6900d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6901d2c0a235SAndy Whitcroft # then suggest that. 6902d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6903b0781216SMike Frysinger $rpt_cleaners = 0; 6904d8469f16SJoe Perches print << "EOM" 6905d8469f16SJoe Perches 6906d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6907d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6908d8469f16SJoe PerchesEOM 6909d2c0a235SAndy Whitcroft } 6910d2c0a235SAndy Whitcroft } 6911d2c0a235SAndy Whitcroft 6912d752fcc8SJoe Perches if ($clean == 0 && $fix && 6913d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6914d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 69159624b8d6SJoe Perches my $newfile = $filename; 69169624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 69173705ce5bSJoe Perches my $linecount = 0; 69183705ce5bSJoe Perches my $f; 69193705ce5bSJoe Perches 6920d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6921d752fcc8SJoe Perches 69223705ce5bSJoe Perches open($f, '>', $newfile) 69233705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 69243705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 69253705ce5bSJoe Perches $linecount++; 69263705ce5bSJoe Perches if ($file) { 69273705ce5bSJoe Perches if ($linecount > 3) { 69283705ce5bSJoe Perches $fixed_line =~ s/^\+//; 69293705ce5bSJoe Perches print $f $fixed_line . "\n"; 69303705ce5bSJoe Perches } 69313705ce5bSJoe Perches } else { 69323705ce5bSJoe Perches print $f $fixed_line . "\n"; 69333705ce5bSJoe Perches } 69343705ce5bSJoe Perches } 69353705ce5bSJoe Perches close($f); 69363705ce5bSJoe Perches 69373705ce5bSJoe Perches if (!$quiet) { 69383705ce5bSJoe Perches print << "EOM"; 6939d8469f16SJoe Perches 69403705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 69413705ce5bSJoe Perches 69423705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 69433705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 69443705ce5bSJoe Perches 69453705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 69463705ce5bSJoe PerchesNo warranties, expressed or implied... 69473705ce5bSJoe PerchesEOM 69483705ce5bSJoe Perches } 69493705ce5bSJoe Perches } 69503705ce5bSJoe Perches 6951d8469f16SJoe Perches if ($quiet == 0) { 6952d8469f16SJoe Perches print "\n"; 6953d8469f16SJoe Perches if ($clean == 1) { 6954d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6955d8469f16SJoe Perches } else { 6956d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 69570a920b5bSAndy Whitcroft } 69580a920b5bSAndy Whitcroft } 69590a920b5bSAndy Whitcroft return $clean; 69600a920b5bSAndy Whitcroft} 6961