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"; 546cd7f386SJoe Perchesmy $max_line_length = 80; 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 1006cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 10156193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 102713a09deSAntonio Borneo --tab-size=n set the number of spaces for tab (default 8) 10377f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10477f5b10aSHannes Eder --no-summary suppress the per-file summary 10577f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 10677f5b10aSHannes Eder --summary-file include the filename in summary 10777f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 10877f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 10977f5b10aSHannes Eder is all off) 11077f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 11177f5b10aSHannes Eder literally 1123705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1133705ce5bSJoe Perches If correctable single-line errors exist, create 1143705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1153705ce5bSJoe Perches with potential errors corrected to the preferred 1163705ce5bSJoe Perches checkpatch style 1179624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1189624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1199624b8d6SJoe Perches file. It's your fault if there's no backup or git 120d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 121d62a201fSDave Hansen runtime errors. 122ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 123f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 124ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 12575ad8c57SJerome Forissier --typedefsfile Read additional types from this file 126737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 127737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 12877f5b10aSHannes Eder -h, --help, --version display this help and exit 12977f5b10aSHannes Eder 13077f5b10aSHannes EderWhen FILE is - read standard input. 13177f5b10aSHannes EderEOM 13277f5b10aSHannes Eder 13377f5b10aSHannes Eder exit($exitcode); 13477f5b10aSHannes Eder} 13577f5b10aSHannes Eder 1363beb42ecSJoe Perchessub uniq { 1373beb42ecSJoe Perches my %seen; 1383beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1393beb42ecSJoe Perches} 1403beb42ecSJoe Perches 1413beb42ecSJoe Perchessub list_types { 1423beb42ecSJoe Perches my ($exitcode) = @_; 1433beb42ecSJoe Perches 1443beb42ecSJoe Perches my $count = 0; 1453beb42ecSJoe Perches 1463beb42ecSJoe Perches local $/ = undef; 1473beb42ecSJoe Perches 1483beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1493beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1503beb42ecSJoe Perches 1513beb42ecSJoe Perches my $text = <$script>; 1523beb42ecSJoe Perches close($script); 1533beb42ecSJoe Perches 1543beb42ecSJoe Perches my @types = (); 1550547fa58SJean Delvare # Also catch when type or level is passed through a variable 1560547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1573beb42ecSJoe Perches push (@types, $_); 1583beb42ecSJoe Perches } 1593beb42ecSJoe Perches @types = sort(uniq(@types)); 1603beb42ecSJoe Perches print("#\tMessage type\n\n"); 1613beb42ecSJoe Perches foreach my $type (@types) { 1623beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1633beb42ecSJoe Perches } 1643beb42ecSJoe Perches 1653beb42ecSJoe Perches exit($exitcode); 1663beb42ecSJoe Perches} 1673beb42ecSJoe Perches 168000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 169000d1cc1SJoe Perchesif (-f $conf) { 170000d1cc1SJoe Perches my @conf_args; 171000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 172000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 173000d1cc1SJoe Perches 174000d1cc1SJoe Perches while (<$conffile>) { 175000d1cc1SJoe Perches my $line = $_; 176000d1cc1SJoe Perches 177000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 178000d1cc1SJoe Perches $line =~ s/^\s*//g; 179000d1cc1SJoe Perches $line =~ s/\s+/ /g; 180000d1cc1SJoe Perches 181000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 182000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 183000d1cc1SJoe Perches 184000d1cc1SJoe Perches my @words = split(" ", $line); 185000d1cc1SJoe Perches foreach my $word (@words) { 186000d1cc1SJoe Perches last if ($word =~ m/^#/); 187000d1cc1SJoe Perches push (@conf_args, $word); 188000d1cc1SJoe Perches } 189000d1cc1SJoe Perches } 190000d1cc1SJoe Perches close($conffile); 191000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 192000d1cc1SJoe Perches} 193000d1cc1SJoe Perches 194737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 195737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 196737c0767SJohn Brooksforeach (@ARGV) { 197737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 198737c0767SJohn Brooks $_ = "--color=$color"; 199737c0767SJohn Brooks } 200737c0767SJohn Brooks} 201737c0767SJohn Brooks 2020a920b5bSAndy WhitcroftGetOptions( 2036c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2040a920b5bSAndy Whitcroft 'tree!' => \$tree, 2050a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2060a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2076c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2088905a67cSAndy Whitcroft 'terse!' => \$terse, 20934d8815fSJoe Perches 'showfile!' => \$showfile, 21077f5b10aSHannes Eder 'f|file!' => \$file, 2114a593c34SDu, Changbin 'g|git!' => \$git, 2126c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2136c72ffaaSAndy Whitcroft 'strict!' => \$check, 214000d1cc1SJoe Perches 'ignore=s' => \@ignore, 21591bfe484SJoe Perches 'types=s' => \@use, 216000d1cc1SJoe Perches 'show-types!' => \$show_types, 2173beb42ecSJoe Perches 'list-types!' => \$list_types, 2186cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 21956193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 220713a09deSAntonio Borneo 'tab-size=i' => \$tabsize, 2216c72ffaaSAndy Whitcroft 'root=s' => \$root, 2228905a67cSAndy Whitcroft 'summary!' => \$summary, 2238905a67cSAndy Whitcroft 'mailback!' => \$mailback, 22413214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2253705ce5bSJoe Perches 'fix!' => \$fix, 2269624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 227d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 228c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 229773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 230ebfd7d62SJoe Perches 'codespell!' => \$codespell, 231ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 23275ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 233737c0767SJohn Brooks 'color=s' => \$color, 234737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 235737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 23677f5b10aSHannes Eder 'h|help' => \$help, 23777f5b10aSHannes Eder 'version' => \$help 23877f5b10aSHannes Eder) or help(1); 23977f5b10aSHannes Eder 24077f5b10aSHannes Ederhelp(0) if ($help); 2410a920b5bSAndy Whitcroft 2423beb42ecSJoe Percheslist_types(0) if ($list_types); 2433beb42ecSJoe Perches 2449624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2452ac73b4fSJoe Perches$check_orig = $check; 2469624b8d6SJoe Perches 2470a920b5bSAndy Whitcroftmy $exit = 0; 2480a920b5bSAndy Whitcroft 2495b57980dSJoe Perchesmy $perl_version_ok = 1; 250d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2515b57980dSJoe Perches $perl_version_ok = 0; 252d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2535b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 254d62a201fSDave Hansen} 255d62a201fSDave Hansen 25645107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2570a920b5bSAndy Whitcroftif ($#ARGV < 0) { 25845107ff6SAllen Hubbe push(@ARGV, '-'); 2590a920b5bSAndy Whitcroft} 2600a920b5bSAndy Whitcroft 261737c0767SJohn Brooksif ($color =~ /^[01]$/) { 262737c0767SJohn Brooks $color = !$color; 263737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 264737c0767SJohn Brooks $color = 1; 265737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 266737c0767SJohn Brooks $color = 0; 267737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 268737c0767SJohn Brooks $color = (-t STDOUT); 269737c0767SJohn Brooks} else { 270737c0767SJohn Brooks die "Invalid color mode: $color\n"; 271737c0767SJohn Brooks} 272737c0767SJohn Brooks 273713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1 274713a09deSAntonio Borneodie "Invalid TAB size: $tabsize\n" if ($tabsize < 2); 275713a09deSAntonio Borneo 27691bfe484SJoe Perchessub hash_save_array_words { 27791bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 27891bfe484SJoe Perches 27991bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 28091bfe484SJoe Perches foreach my $word (@array) { 281000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 282000d1cc1SJoe Perches $word =~ s/^\s*//g; 283000d1cc1SJoe Perches $word =~ s/\s+/ /g; 284000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 285000d1cc1SJoe Perches 286000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 287000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 288000d1cc1SJoe Perches 28991bfe484SJoe Perches $hashRef->{$word}++; 290000d1cc1SJoe Perches } 29191bfe484SJoe Perches} 29291bfe484SJoe Perches 29391bfe484SJoe Perchessub hash_show_words { 29491bfe484SJoe Perches my ($hashRef, $prefix) = @_; 29591bfe484SJoe Perches 2963c816e49SJoe Perches if (keys %$hashRef) { 297d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 29858cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 29991bfe484SJoe Perches print " $word"; 30091bfe484SJoe Perches } 301d8469f16SJoe Perches print "\n"; 30291bfe484SJoe Perches } 30391bfe484SJoe Perches} 30491bfe484SJoe Perches 30591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 30691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 307000d1cc1SJoe Perches 308c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 309c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3107429c690SAndy Whitcroftmy $dbg_type = 0; 311a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 312c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 31321caa13cSAndy Whitcroft ## no critic 31421caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 31521caa13cSAndy Whitcroft die "$@" if ($@); 316c2fdda0dSAndy Whitcroft} 317c2fdda0dSAndy Whitcroft 318d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 319d2c0a235SAndy Whitcroft 3208905a67cSAndy Whitcroftif ($terse) { 3218905a67cSAndy Whitcroft $emacs = 1; 3228905a67cSAndy Whitcroft $quiet++; 3238905a67cSAndy Whitcroft} 3248905a67cSAndy Whitcroft 3256c72ffaaSAndy Whitcroftif ($tree) { 3266c72ffaaSAndy Whitcroft if (defined $root) { 3276c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3286c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3296c72ffaaSAndy Whitcroft } 3306c72ffaaSAndy Whitcroft } else { 3316c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3326c72ffaaSAndy Whitcroft $root = '.'; 3336c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3346c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3356c72ffaaSAndy Whitcroft $root = $1; 3366c72ffaaSAndy Whitcroft } 3376c72ffaaSAndy Whitcroft } 3386c72ffaaSAndy Whitcroft 3396c72ffaaSAndy Whitcroft if (!defined $root) { 3400a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3410a920b5bSAndy Whitcroft exit(2); 3420a920b5bSAndy Whitcroft } 3436c72ffaaSAndy Whitcroft} 3446c72ffaaSAndy Whitcroft 3456c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3466c72ffaaSAndy Whitcroft 3472ceb532bSAndy Whitcroftour $Ident = qr{ 3482ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3492ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3502ceb532bSAndy Whitcroft }x; 3516c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3526c72ffaaSAndy Whitcroftour $Sparse = qr{ 3536c72ffaaSAndy Whitcroft __user| 3546c72ffaaSAndy Whitcroft __kernel| 3556c72ffaaSAndy Whitcroft __force| 3566c72ffaaSAndy Whitcroft __iomem| 3576c72ffaaSAndy Whitcroft __must_check| 358417495edSAndy Whitcroft __kprobes| 359165e72a6SSven Eckelmann __ref| 36033aa4597SGeert Uytterhoeven __refconst| 36133aa4597SGeert Uytterhoeven __refdata| 362ad315455SBoqun Feng __rcu| 363ad315455SBoqun Feng __private 3646c72ffaaSAndy Whitcroft }x; 365e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 366e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 367e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 368e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 369e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3708716de38SJoe Perches 37152131292SWolfram Sang# Notes to $Attribute: 37252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3736c72ffaaSAndy Whitcroftour $Attribute = qr{ 3746c72ffaaSAndy Whitcroft const| 37503f1df7dSJoe Perches __percpu| 37603f1df7dSJoe Perches __nocast| 37703f1df7dSJoe Perches __safe| 37846d832f5SMichael S. Tsirkin __bitwise| 37903f1df7dSJoe Perches __packed__| 38003f1df7dSJoe Perches __packed2__| 38103f1df7dSJoe Perches __naked| 38203f1df7dSJoe Perches __maybe_unused| 38303f1df7dSJoe Perches __always_unused| 38403f1df7dSJoe Perches __noreturn| 38503f1df7dSJoe Perches __used| 38603f1df7dSJoe Perches __cold| 387e23ef1f3SJoe Perches __pure| 38803f1df7dSJoe Perches __noclone| 38903f1df7dSJoe Perches __deprecated| 3906c72ffaaSAndy Whitcroft __read_mostly| 391c5967e98SJoe Perches __ro_after_init| 3926c72ffaaSAndy Whitcroft __kprobes| 3938716de38SJoe Perches $InitAttribute| 39424e1d81aSAndy Whitcroft ____cacheline_aligned| 39524e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3965fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3975fe3af11SAndy Whitcroft __weak 3986c72ffaaSAndy Whitcroft }x; 399c45dcabdSAndy Whitcroftour $Modifier; 40091cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 4016c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 4026c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 4036c72ffaaSAndy Whitcroft 40495e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 40595e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 40695e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 40795e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4082435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 409c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 410326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 411326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 412326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 41374349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4142435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 415326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 416447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 41723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4186c72ffaaSAndy Whitcroftour $Operators = qr{ 4196c72ffaaSAndy Whitcroft <=|>=|==|!=| 4206c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 42123f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4226c72ffaaSAndy Whitcroft }x; 4236c72ffaaSAndy Whitcroft 42491cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 42591cb5195SJoe Perches 426ab7e23f3SJoe Perchesour $BasicType; 4278905a67cSAndy Whitcroftour $NonptrType; 4281813087dSJoe Perchesour $NonptrTypeMisordered; 4298716de38SJoe Perchesour $NonptrTypeWithAttr; 4308905a67cSAndy Whitcroftour $Type; 4311813087dSJoe Perchesour $TypeMisordered; 4328905a67cSAndy Whitcroftour $Declare; 4331813087dSJoe Perchesour $DeclareMisordered; 4348905a67cSAndy Whitcroft 43515662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 43615662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 437171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 438171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 439171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 440171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 441171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 442171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 443171ae1a4SAndy Whitcroft}x; 444171ae1a4SAndy Whitcroft 44515662b3eSJoe Perchesour $UTF8 = qr{ 44615662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 44715662b3eSJoe Perches | $NON_ASCII_UTF8 44815662b3eSJoe Perches}x; 44915662b3eSJoe Perches 450e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 451021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 452021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 453021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 454021158b4SJoe Perches)}; 455e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 456fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4578ed22cadSAndy Whitcroft atomic_t 4588ed22cadSAndy Whitcroft)}; 459e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 460e6176fa4SJoe Perches $typeC99Typedefs\b| 461e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 462e6176fa4SJoe Perches $typeKernelTypedefs\b 463e6176fa4SJoe Perches)}; 4648ed22cadSAndy Whitcroft 4656d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4666d32f7a3SJoe Perches 467691e669bSJoe Perchesour $logFunctions = qr{(?x: 468758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4697d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 47087bd499aSJoe Perches TP_printk| 4716e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 472b0531722SJoe Perches panic| 47306668727SJoe Perches MODULE_[A-Z_]+| 47406668727SJoe Perches seq_vprintf|seq_printf|seq_puts 475691e669bSJoe Perches)}; 476691e669bSJoe Perches 477e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 478e29a70f1SJoe Perches (?:(?:devm_)? 479e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 480e29a70f1SJoe Perches kstrdup(?:_const)? | 481e29a70f1SJoe Perches kmemdup(?:_nul)?) | 482461e1565SChristophe JAILLET (?:\w+)?alloc_skb(?:_ip_align)? | 483e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 484e29a70f1SJoe Perches dma_alloc_coherent 485e29a70f1SJoe Perches)}; 486e29a70f1SJoe Perches 48720112475SJoe Perchesour $signature_tags = qr{(?xi: 48820112475SJoe Perches Signed-off-by:| 489d499480cSJorge Ramirez-Ortiz Co-developed-by:| 49020112475SJoe Perches Acked-by:| 49120112475SJoe Perches Tested-by:| 49220112475SJoe Perches Reviewed-by:| 49320112475SJoe Perches Reported-by:| 4948543ae12SMugunthan V N Suggested-by:| 49520112475SJoe Perches To:| 49620112475SJoe Perches Cc: 49720112475SJoe Perches)}; 49820112475SJoe Perches 4991813087dSJoe Perchesour @typeListMisordered = ( 5001813087dSJoe Perches qr{char\s+(?:un)?signed}, 5011813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 5021813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 5031813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 5041813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 5051813087dSJoe Perches qr{short\s+(?:un)?signed}, 5061813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5071813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5081813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5091813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5101813087dSJoe Perches qr{int\s+(?:un)?signed}, 5111813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5121813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5131813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5141813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5151813087dSJoe Perches qr{long\s+(?:un)?signed}, 5161813087dSJoe Perches); 5171813087dSJoe Perches 5188905a67cSAndy Whitcroftour @typeList = ( 5198905a67cSAndy Whitcroft qr{void}, 5200c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5210c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5220c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5230c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5240c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5250c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5260c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5270c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5280c773d9dSJoe Perches qr{(?:un)?signed}, 5298905a67cSAndy Whitcroft qr{float}, 5308905a67cSAndy Whitcroft qr{double}, 5318905a67cSAndy Whitcroft qr{bool}, 5328905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5338905a67cSAndy Whitcroft qr{union\s+$Ident}, 5348905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5358905a67cSAndy Whitcroft qr{${Ident}_t}, 5368905a67cSAndy Whitcroft qr{${Ident}_handler}, 5378905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5381813087dSJoe Perches @typeListMisordered, 5398905a67cSAndy Whitcroft); 540938224b5SJoe Perches 541938224b5SJoe Perchesour $C90_int_types = qr{(?x: 542938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 543938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 544938224b5SJoe Perches long\s+long\s+(?:un)?signed| 545938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 546938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 547938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 548938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 549938224b5SJoe Perches 550938224b5SJoe Perches long\s+int\s+(?:un)?signed| 551938224b5SJoe Perches long\s+(?:un)?signed\s+int| 552938224b5SJoe Perches long\s+(?:un)?signed| 553938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 554938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 555938224b5SJoe Perches int\s+long\s+(?:un)?signed| 556938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 557938224b5SJoe Perches 558938224b5SJoe Perches int\s+(?:un)?signed| 559938224b5SJoe Perches (?:(?:un)?signed\s+)?int 560938224b5SJoe Perches)}; 561938224b5SJoe Perches 562485ff23eSAlex Dowadour @typeListFile = (); 5638716de38SJoe Perchesour @typeListWithAttr = ( 5648716de38SJoe Perches @typeList, 5658716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5668716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5678716de38SJoe Perches); 5688716de38SJoe Perches 569c45dcabdSAndy Whitcroftour @modifierList = ( 570c45dcabdSAndy Whitcroft qr{fastcall}, 571c45dcabdSAndy Whitcroft); 572485ff23eSAlex Dowadour @modifierListFile = (); 5738905a67cSAndy Whitcroft 5742435880fSJoe Perchesour @mode_permission_funcs = ( 5752435880fSJoe Perches ["module_param", 3], 5762435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5772435880fSJoe Perches ["module_param_array_named", 5], 5782435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5792435880fSJoe Perches ["proc_create(?:_data|)", 2], 580459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 581459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 582459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 583459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 584459cf0aeSJoe Perches ["__ATTR", 2], 5852435880fSJoe Perches); 5862435880fSJoe Perches 587515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 588515a235eSJoe Perchesour $mode_perms_search = ""; 589515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 590515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 591515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 592515a235eSJoe Perches} 59300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 594515a235eSJoe Perches 5959189c7e7SJoe Perchesour %deprecated_apis = ( 5969189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 5979189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 5989189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 5999189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 6009189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 6019189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 6029189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 6039189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 6049189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 6059189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6069189c7e7SJoe Perches); 6079189c7e7SJoe Perches 6089189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6099189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6109189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6119189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6129189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6139189c7e7SJoe Perches} 6149189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6159189c7e7SJoe Perches 616b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 617b392c64fSJoe Perches S_IWUGO | 618b392c64fSJoe Perches S_IWOTH | 619b392c64fSJoe Perches S_IRWXUGO | 620b392c64fSJoe Perches S_IALLUGO | 621b392c64fSJoe Perches 0[0-7][0-7][2367] 622b392c64fSJoe Perches}x; 623b392c64fSJoe Perches 624f90774e1SJoe Perchesour %mode_permission_string_types = ( 625f90774e1SJoe Perches "S_IRWXU" => 0700, 626f90774e1SJoe Perches "S_IRUSR" => 0400, 627f90774e1SJoe Perches "S_IWUSR" => 0200, 628f90774e1SJoe Perches "S_IXUSR" => 0100, 629f90774e1SJoe Perches "S_IRWXG" => 0070, 630f90774e1SJoe Perches "S_IRGRP" => 0040, 631f90774e1SJoe Perches "S_IWGRP" => 0020, 632f90774e1SJoe Perches "S_IXGRP" => 0010, 633f90774e1SJoe Perches "S_IRWXO" => 0007, 634f90774e1SJoe Perches "S_IROTH" => 0004, 635f90774e1SJoe Perches "S_IWOTH" => 0002, 636f90774e1SJoe Perches "S_IXOTH" => 0001, 637f90774e1SJoe Perches "S_IRWXUGO" => 0777, 638f90774e1SJoe Perches "S_IRUGO" => 0444, 639f90774e1SJoe Perches "S_IWUGO" => 0222, 640f90774e1SJoe Perches "S_IXUGO" => 0111, 641f90774e1SJoe Perches); 642f90774e1SJoe Perches 643f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 644f90774e1SJoe Perchesour $mode_perms_string_search = ""; 645f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 646f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 647f90774e1SJoe Perches $mode_perms_string_search .= $entry; 648f90774e1SJoe Perches} 64900180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 65000180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 65100180468SJoe Perches ${single_mode_perms_string_search} 65200180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 65300180468SJoe Perches}x; 65400180468SJoe Perches 65500180468SJoe Perchessub perms_to_octal { 65600180468SJoe Perches my ($string) = @_; 65700180468SJoe Perches 65800180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 65900180468SJoe Perches 66000180468SJoe Perches my $val = ""; 66100180468SJoe Perches my $oval = ""; 66200180468SJoe Perches my $to = 0; 66300180468SJoe Perches my $curpos = 0; 66400180468SJoe Perches my $lastpos = 0; 66500180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 66600180468SJoe Perches $curpos = pos($string); 66700180468SJoe Perches my $match = $2; 66800180468SJoe Perches my $omatch = $1; 66900180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 67000180468SJoe Perches $lastpos = $curpos; 67100180468SJoe Perches $to |= $mode_permission_string_types{$match}; 67200180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 67300180468SJoe Perches $val .= $match; 67400180468SJoe Perches $oval .= $omatch; 67500180468SJoe Perches } 67600180468SJoe Perches $oval =~ s/^\s*\|\s*//; 67700180468SJoe Perches $oval =~ s/\s*\|\s*$//; 67800180468SJoe Perches return sprintf("%04o", $to); 67900180468SJoe Perches} 680f90774e1SJoe Perches 6817840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6827840a94cSWolfram Sang irq| 683cdcee686SSergey Ryazanov memory| 684cdcee686SSergey Ryazanov time| 685cdcee686SSergey Ryazanov reboot 6867840a94cSWolfram Sang)}; 6877840a94cSWolfram Sang# memory.h: ARM has a custom one 6887840a94cSWolfram Sang 68966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 69066b47b4aSKees Cookmy $misspellings; 69166b47b4aSKees Cookmy %spelling_fix; 69236061e38SJoe Perches 69336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 69466b47b4aSKees Cook while (<$spelling>) { 69566b47b4aSKees Cook my $line = $_; 69666b47b4aSKees Cook 69766b47b4aSKees Cook $line =~ s/\s*\n?$//g; 69866b47b4aSKees Cook $line =~ s/^\s*//g; 69966b47b4aSKees Cook 70066b47b4aSKees Cook next if ($line =~ m/^\s*#/); 70166b47b4aSKees Cook next if ($line =~ m/^\s*$/); 70266b47b4aSKees Cook 70366b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 70466b47b4aSKees Cook 70566b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 70666b47b4aSKees Cook } 70766b47b4aSKees Cook close($spelling); 70836061e38SJoe Perches} else { 70936061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 71036061e38SJoe Perches} 71166b47b4aSKees Cook 712ebfd7d62SJoe Perchesif ($codespell) { 713ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 714ebfd7d62SJoe Perches while (<$spelling>) { 715ebfd7d62SJoe Perches my $line = $_; 716ebfd7d62SJoe Perches 717ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 718ebfd7d62SJoe Perches $line =~ s/^\s*//g; 719ebfd7d62SJoe Perches 720ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 721ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 722ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 723ebfd7d62SJoe Perches 724ebfd7d62SJoe Perches $line =~ s/,.*$//; 725ebfd7d62SJoe Perches 726ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 727ebfd7d62SJoe Perches 728ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 729ebfd7d62SJoe Perches } 730ebfd7d62SJoe Perches close($spelling); 731ebfd7d62SJoe Perches } else { 732ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 733ebfd7d62SJoe Perches } 734ebfd7d62SJoe Perches} 735ebfd7d62SJoe Perches 736ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 737ebfd7d62SJoe Perches 73875ad8c57SJerome Forissiersub read_words { 73975ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 74075ad8c57SJerome Forissier 74175ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 74275ad8c57SJerome Forissier while (<$words>) { 743bf1fa1daSJoe Perches my $line = $_; 744bf1fa1daSJoe Perches 745bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 746bf1fa1daSJoe Perches $line =~ s/^\s*//g; 747bf1fa1daSJoe Perches 748bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 749bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 750bf1fa1daSJoe Perches if ($line =~ /\s/) { 75175ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 752bf1fa1daSJoe Perches next; 753bf1fa1daSJoe Perches } 754bf1fa1daSJoe Perches 75575ad8c57SJerome Forissier $$wordsRef .= '|' if ($$wordsRef ne ""); 75675ad8c57SJerome Forissier $$wordsRef .= $line; 757bf1fa1daSJoe Perches } 75875ad8c57SJerome Forissier close($file); 75975ad8c57SJerome Forissier return 1; 760bf1fa1daSJoe Perches } 761bf1fa1daSJoe Perches 76275ad8c57SJerome Forissier return 0; 76375ad8c57SJerome Forissier} 76475ad8c57SJerome Forissier 76575ad8c57SJerome Forissiermy $const_structs = ""; 76675ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile) 76775ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 76875ad8c57SJerome Forissier 76975ad8c57SJerome Forissiermy $typeOtherTypedefs = ""; 77075ad8c57SJerome Forissierif (length($typedefsfile)) { 77175ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 77275ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 77375ad8c57SJerome Forissier} 77475ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); 77575ad8c57SJerome Forissier 7768905a67cSAndy Whitcroftsub build_types { 777485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 778485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7791813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7808716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 781c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 782ab7e23f3SJoe Perches $BasicType = qr{ 783ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 784ab7e23f3SJoe Perches (?:${all}\b) 785ab7e23f3SJoe Perches }x; 7868905a67cSAndy Whitcroft $NonptrType = qr{ 787d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 788cf655043SAndy Whitcroft (?: 7896b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 7908ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 791c45dcabdSAndy Whitcroft (?:${all}\b) 792cf655043SAndy Whitcroft ) 793c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 7948905a67cSAndy Whitcroft }x; 7951813087dSJoe Perches $NonptrTypeMisordered = qr{ 7961813087dSJoe Perches (?:$Modifier\s+|const\s+)* 7971813087dSJoe Perches (?: 7981813087dSJoe Perches (?:${Misordered}\b) 7991813087dSJoe Perches ) 8001813087dSJoe Perches (?:\s+$Modifier|\s+const)* 8011813087dSJoe Perches }x; 8028716de38SJoe Perches $NonptrTypeWithAttr = qr{ 8038716de38SJoe Perches (?:$Modifier\s+|const\s+)* 8048716de38SJoe Perches (?: 8058716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8068716de38SJoe Perches (?:$typeTypedefs\b)| 8078716de38SJoe Perches (?:${allWithAttr}\b) 8088716de38SJoe Perches ) 8098716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8108716de38SJoe Perches }x; 8118905a67cSAndy Whitcroft $Type = qr{ 812c45dcabdSAndy Whitcroft $NonptrType 8137b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 814c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8158905a67cSAndy Whitcroft }x; 8161813087dSJoe Perches $TypeMisordered = qr{ 8171813087dSJoe Perches $NonptrTypeMisordered 8187b18496cSAntonio Borneo (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} 8191813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8201813087dSJoe Perches }x; 82191cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8221813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8238905a67cSAndy Whitcroft} 8248905a67cSAndy Whitcroftbuild_types(); 8256c72ffaaSAndy Whitcroft 8267d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 827d1fe9c09SJoe Perches 828d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 829d1fe9c09SJoe Perches# requires at least perl version v5.10.0 830d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 831d1fe9c09SJoe Perches 832d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 8332435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 834c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 8357d2367afSJoe Perches 836f8422308SJoe Perchesour $declaration_macros = qr{(?x: 8373e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 838fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 8393d102fc0SGilad Ben-Yossef (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(| 8403d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 841f8422308SJoe Perches)}; 842f8422308SJoe Perches 8437d2367afSJoe Perchessub deparenthesize { 8447d2367afSJoe Perches my ($string) = @_; 8457d2367afSJoe Perches return "" if (!defined($string)); 8465b9553abSJoe Perches 8475b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8485b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8495b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8505b9553abSJoe Perches } 8515b9553abSJoe Perches 8527d2367afSJoe Perches $string =~ s@\s+@ @g; 8535b9553abSJoe Perches 8547d2367afSJoe Perches return $string; 8557d2367afSJoe Perches} 8567d2367afSJoe Perches 8573445686aSJoe Perchessub seed_camelcase_file { 8583445686aSJoe Perches my ($file) = @_; 8593445686aSJoe Perches 8603445686aSJoe Perches return if (!(-f $file)); 8613445686aSJoe Perches 8623445686aSJoe Perches local $/; 8633445686aSJoe Perches 8643445686aSJoe Perches open(my $include_file, '<', "$file") 8653445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8663445686aSJoe Perches my $text = <$include_file>; 8673445686aSJoe Perches close($include_file); 8683445686aSJoe Perches 8693445686aSJoe Perches my @lines = split('\n', $text); 8703445686aSJoe Perches 8713445686aSJoe Perches foreach my $line (@lines) { 8723445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8733445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8743445686aSJoe Perches $camelcase{$1} = 1; 87511ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 87611ea516aSJoe Perches $camelcase{$1} = 1; 87711ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8783445686aSJoe Perches $camelcase{$1} = 1; 8793445686aSJoe Perches } 8803445686aSJoe Perches } 8813445686aSJoe Perches} 8823445686aSJoe Perches 883cd28b119SJoe Perchesour %maintained_status = (); 884cd28b119SJoe Perches 88585b0ee18SJoe Perchessub is_maintained_obsolete { 88685b0ee18SJoe Perches my ($filename) = @_; 88785b0ee18SJoe Perches 888f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 88985b0ee18SJoe Perches 890cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 891cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 892cd28b119SJoe Perches } 89385b0ee18SJoe Perches 894cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 89585b0ee18SJoe Perches} 89685b0ee18SJoe Perches 8973b6e8ac9SJoe Perchessub is_SPDX_License_valid { 8983b6e8ac9SJoe Perches my ($license) = @_; 8993b6e8ac9SJoe Perches 90056294112SJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git")); 9013b6e8ac9SJoe Perches 90256294112SJoe Perches my $root_path = abs_path($root); 90356294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 9043b6e8ac9SJoe Perches return 0 if ($status ne ""); 9053b6e8ac9SJoe Perches return 1; 9063b6e8ac9SJoe Perches} 9073b6e8ac9SJoe Perches 9083445686aSJoe Perchesmy $camelcase_seeded = 0; 9093445686aSJoe Perchessub seed_camelcase_includes { 9103445686aSJoe Perches return if ($camelcase_seeded); 9113445686aSJoe Perches 9123445686aSJoe Perches my $files; 913c707a81dSJoe Perches my $camelcase_cache = ""; 914c707a81dSJoe Perches my @include_files = (); 915c707a81dSJoe Perches 916c707a81dSJoe Perches $camelcase_seeded = 1; 917351b2a1fSJoe Perches 9183645e328SRichard Genoud if (-e ".git") { 919dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 920351b2a1fSJoe Perches chomp $git_last_include_commit; 921c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 922c707a81dSJoe Perches } else { 923c707a81dSJoe Perches my $last_mod_date = 0; 924c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 925c707a81dSJoe Perches @include_files = split('\n', $files); 926c707a81dSJoe Perches foreach my $file (@include_files) { 927c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 928c707a81dSJoe Perches localtime((stat $file)[9])); 929c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 930c707a81dSJoe Perches } 931c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 932c707a81dSJoe Perches } 933c707a81dSJoe Perches 934c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 935c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 936c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 937351b2a1fSJoe Perches while (<$camelcase_file>) { 938351b2a1fSJoe Perches chomp; 939351b2a1fSJoe Perches $camelcase{$_} = 1; 940351b2a1fSJoe Perches } 941351b2a1fSJoe Perches close($camelcase_file); 942351b2a1fSJoe Perches 943351b2a1fSJoe Perches return; 944351b2a1fSJoe Perches } 945c707a81dSJoe Perches 9463645e328SRichard Genoud if (-e ".git") { 947dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 948c707a81dSJoe Perches @include_files = split('\n', $files); 9493445686aSJoe Perches } 950c707a81dSJoe Perches 9513445686aSJoe Perches foreach my $file (@include_files) { 9523445686aSJoe Perches seed_camelcase_file($file); 9533445686aSJoe Perches } 954351b2a1fSJoe Perches 955c707a81dSJoe Perches if ($camelcase_cache ne "") { 956351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 957c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 958c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 959351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 960351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 961351b2a1fSJoe Perches } 962351b2a1fSJoe Perches close($camelcase_file); 963351b2a1fSJoe Perches } 9643445686aSJoe Perches} 9653445686aSJoe Perches 966d311cd44SJoe Perchessub git_commit_info { 967d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 968d311cd44SJoe Perches 969d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 970d311cd44SJoe Perches 971dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 972d311cd44SJoe Perches $output =~ s/^\s*//gm; 973d311cd44SJoe Perches my @lines = split("\n", $output); 974d311cd44SJoe Perches 9750d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9760d7835fcSJoe Perches 9775a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 978d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 979d311cd44SJoe Perches# all matching commit ids, but it's very slow... 980d311cd44SJoe Perches# 981d311cd44SJoe Perches# echo "checking commits $1..." 982d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 983d311cd44SJoe Perches# while read line ; do 984d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 985d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 986d311cd44SJoe Perches# done 987d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 988948b133aSHeinrich Schuchardt $id = undef; 989d311cd44SJoe Perches } else { 990d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 991d311cd44SJoe Perches $desc = substr($lines[0], 41); 992d311cd44SJoe Perches } 993d311cd44SJoe Perches 994d311cd44SJoe Perches return ($id, $desc); 995d311cd44SJoe Perches} 996d311cd44SJoe Perches 9976c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 9980a920b5bSAndy Whitcroft 99900df344fSAndy Whitcroftmy @rawlines = (); 1000c2fdda0dSAndy Whitcroftmy @lines = (); 10013705ce5bSJoe Perchesmy @fixed = (); 1002d752fcc8SJoe Perchesmy @fixed_inserted = (); 1003d752fcc8SJoe Perchesmy @fixed_deleted = (); 1004194f66fcSJoe Perchesmy $fixlinenr = -1; 1005194f66fcSJoe Perches 10064a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10074a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 10084a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 10094a593c34SDu, Changbin 10104a593c34SDu, Changbinif ($git) { 10114a593c34SDu, Changbin my @commits = (); 10120dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 10134a593c34SDu, Changbin my $git_range; 101428898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 101528898fd1SJoe Perches $git_range = "-$2 $1"; 10164a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 10174a593c34SDu, Changbin $git_range = "$commit_expr"; 10184a593c34SDu, Changbin } else { 10190dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 10200dea9f1eSJoe Perches } 1021dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 10220dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 102328898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 102428898fd1SJoe Perches next if (!defined($1) || !defined($2)); 10250dea9f1eSJoe Perches my $sha1 = $1; 10260dea9f1eSJoe Perches my $subject = $2; 10270dea9f1eSJoe Perches unshift(@commits, $sha1); 10280dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 10294a593c34SDu, Changbin } 10304a593c34SDu, Changbin } 10314a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 10324a593c34SDu, Changbin @ARGV = @commits; 10334a593c34SDu, Changbin} 10344a593c34SDu, Changbin 1035c2fdda0dSAndy Whitcroftmy $vname; 103698005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 10376c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 103821caa13cSAndy Whitcroft my $FILE; 10394a593c34SDu, Changbin if ($git) { 10404a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 10414a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 10424a593c34SDu, Changbin } elsif ($file) { 104321caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 10446c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 104521caa13cSAndy Whitcroft } elsif ($filename eq '-') { 104621caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 10476c72ffaaSAndy Whitcroft } else { 104821caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 10496c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 10506c72ffaaSAndy Whitcroft } 1051c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1052c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 10534a593c34SDu, Changbin } elsif ($git) { 10540dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1055c2fdda0dSAndy Whitcroft } else { 1056c2fdda0dSAndy Whitcroft $vname = $filename; 1057c2fdda0dSAndy Whitcroft } 105821caa13cSAndy Whitcroft while (<$FILE>) { 10590a920b5bSAndy Whitcroft chomp; 106000df344fSAndy Whitcroft push(@rawlines, $_); 10616c72ffaaSAndy Whitcroft } 106221caa13cSAndy Whitcroft close($FILE); 1063d8469f16SJoe Perches 1064d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1065d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1066d8469f16SJoe Perches print "$vname\n"; 1067d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1068d8469f16SJoe Perches } 1069d8469f16SJoe Perches 1070c2fdda0dSAndy Whitcroft if (!process($filename)) { 10710a920b5bSAndy Whitcroft $exit = 1; 10720a920b5bSAndy Whitcroft } 107300df344fSAndy Whitcroft @rawlines = (); 107413214adfSAndy Whitcroft @lines = (); 10753705ce5bSJoe Perches @fixed = (); 1076d752fcc8SJoe Perches @fixed_inserted = (); 1077d752fcc8SJoe Perches @fixed_deleted = (); 1078194f66fcSJoe Perches $fixlinenr = -1; 1079485ff23eSAlex Dowad @modifierListFile = (); 1080485ff23eSAlex Dowad @typeListFile = (); 1081485ff23eSAlex Dowad build_types(); 10820a920b5bSAndy Whitcroft} 10830a920b5bSAndy Whitcroft 1084d8469f16SJoe Perchesif (!$quiet) { 10853c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 10863c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 10873c816e49SJoe Perches 10885b57980dSJoe Perches if (!$perl_version_ok) { 1089d8469f16SJoe Perches print << "EOM" 1090d8469f16SJoe Perches 1091d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 10925b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1093d8469f16SJoe PerchesEOM 1094d8469f16SJoe Perches } 1095d8469f16SJoe Perches if ($exit) { 1096d8469f16SJoe Perches print << "EOM" 1097d8469f16SJoe Perches 1098d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1099d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1100d8469f16SJoe PerchesEOM 1101d8469f16SJoe Perches } 1102d8469f16SJoe Perches} 1103d8469f16SJoe Perches 11040a920b5bSAndy Whitcroftexit($exit); 11050a920b5bSAndy Whitcroft 11060a920b5bSAndy Whitcroftsub top_of_kernel_tree { 11076c72ffaaSAndy Whitcroft my ($root) = @_; 11086c72ffaaSAndy Whitcroft 11096c72ffaaSAndy Whitcroft my @tree_check = ( 11106c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 11116c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 11126c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 11136c72ffaaSAndy Whitcroft ); 11146c72ffaaSAndy Whitcroft 11156c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 11166c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 11170a920b5bSAndy Whitcroft return 0; 11180a920b5bSAndy Whitcroft } 11196c72ffaaSAndy Whitcroft } 11206c72ffaaSAndy Whitcroft return 1; 11216c72ffaaSAndy Whitcroft} 11220a920b5bSAndy Whitcroft 112320112475SJoe Perchessub parse_email { 112420112475SJoe Perches my ($formatted_email) = @_; 112520112475SJoe Perches 112620112475SJoe Perches my $name = ""; 1127dfa05c28SJoe Perches my $name_comment = ""; 112820112475SJoe Perches my $address = ""; 112920112475SJoe Perches my $comment = ""; 113020112475SJoe Perches 113120112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 113220112475SJoe Perches $name = $1; 113320112475SJoe Perches $address = $2; 113420112475SJoe Perches $comment = $3 if defined $3; 113520112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 113620112475SJoe Perches $address = $1; 113720112475SJoe Perches $comment = $2 if defined $2; 113820112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 113920112475SJoe Perches $address = $1; 114020112475SJoe Perches $comment = $2 if defined $2; 114185e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 114220112475SJoe Perches $name = $formatted_email; 11433705ce5bSJoe Perches $name = trim($name); 114420112475SJoe Perches $name =~ s/^\"|\"$//g; 114520112475SJoe Perches # If there's a name left after stripping spaces and 114620112475SJoe Perches # leading quotes, and the address doesn't have both 114720112475SJoe Perches # leading and trailing angle brackets, the address 114820112475SJoe Perches # is invalid. ie: 114920112475SJoe Perches # "joe smith [email protected]" bad 115020112475SJoe Perches # "joe smith <[email protected]" bad 115120112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 115220112475SJoe Perches $name = ""; 115320112475SJoe Perches $address = ""; 115420112475SJoe Perches $comment = ""; 115520112475SJoe Perches } 115620112475SJoe Perches } 115720112475SJoe Perches 11583705ce5bSJoe Perches $name = trim($name); 115920112475SJoe Perches $name =~ s/^\"|\"$//g; 1160dfa05c28SJoe Perches $name =~ s/(\s*\([^\)]+\))\s*//; 1161dfa05c28SJoe Perches if (defined($1)) { 1162dfa05c28SJoe Perches $name_comment = trim($1); 1163dfa05c28SJoe Perches } 11643705ce5bSJoe Perches $address = trim($address); 116520112475SJoe Perches $address =~ s/^\<|\>$//g; 116620112475SJoe Perches 116720112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 116820112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 116920112475SJoe Perches $name = "\"$name\""; 117020112475SJoe Perches } 117120112475SJoe Perches 1172dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 117320112475SJoe Perches} 117420112475SJoe Perches 117520112475SJoe Perchessub format_email { 117620112475SJoe Perches my ($name, $address) = @_; 117720112475SJoe Perches 117820112475SJoe Perches my $formatted_email; 117920112475SJoe Perches 11803705ce5bSJoe Perches $name = trim($name); 118120112475SJoe Perches $name =~ s/^\"|\"$//g; 11823705ce5bSJoe Perches $address = trim($address); 118320112475SJoe Perches 118420112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 118520112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 118620112475SJoe Perches $name = "\"$name\""; 118720112475SJoe Perches } 118820112475SJoe Perches 118920112475SJoe Perches if ("$name" eq "") { 119020112475SJoe Perches $formatted_email = "$address"; 119120112475SJoe Perches } else { 119220112475SJoe Perches $formatted_email = "$name <$address>"; 119320112475SJoe Perches } 119420112475SJoe Perches 119520112475SJoe Perches return $formatted_email; 119620112475SJoe Perches} 119720112475SJoe Perches 1198dfa05c28SJoe Perchessub reformat_email { 1199dfa05c28SJoe Perches my ($email) = @_; 1200dfa05c28SJoe Perches 1201dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 1202dfa05c28SJoe Perches return format_email($email_name, $email_address); 1203dfa05c28SJoe Perches} 1204dfa05c28SJoe Perches 1205dfa05c28SJoe Perchessub same_email_addresses { 1206dfa05c28SJoe Perches my ($email1, $email2) = @_; 1207dfa05c28SJoe Perches 1208dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1209dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1210dfa05c28SJoe Perches 1211dfa05c28SJoe Perches return $email1_name eq $email2_name && 1212dfa05c28SJoe Perches $email1_address eq $email2_address; 1213dfa05c28SJoe Perches} 1214dfa05c28SJoe Perches 1215d311cd44SJoe Perchessub which { 1216d311cd44SJoe Perches my ($bin) = @_; 1217d311cd44SJoe Perches 1218d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1219d311cd44SJoe Perches if (-e "$path/$bin") { 1220d311cd44SJoe Perches return "$path/$bin"; 1221d311cd44SJoe Perches } 1222d311cd44SJoe Perches } 1223d311cd44SJoe Perches 1224d311cd44SJoe Perches return ""; 1225d311cd44SJoe Perches} 1226d311cd44SJoe Perches 1227000d1cc1SJoe Perchessub which_conf { 1228000d1cc1SJoe Perches my ($conf) = @_; 1229000d1cc1SJoe Perches 1230000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1231000d1cc1SJoe Perches if (-e "$path/$conf") { 1232000d1cc1SJoe Perches return "$path/$conf"; 1233000d1cc1SJoe Perches } 1234000d1cc1SJoe Perches } 1235000d1cc1SJoe Perches 1236000d1cc1SJoe Perches return ""; 1237000d1cc1SJoe Perches} 1238000d1cc1SJoe Perches 12390a920b5bSAndy Whitcroftsub expand_tabs { 12400a920b5bSAndy Whitcroft my ($str) = @_; 12410a920b5bSAndy Whitcroft 12420a920b5bSAndy Whitcroft my $res = ''; 12430a920b5bSAndy Whitcroft my $n = 0; 12440a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 12450a920b5bSAndy Whitcroft if ($c eq "\t") { 12460a920b5bSAndy Whitcroft $res .= ' '; 12470a920b5bSAndy Whitcroft $n++; 1248713a09deSAntonio Borneo for (; ($n % $tabsize) != 0; $n++) { 12490a920b5bSAndy Whitcroft $res .= ' '; 12500a920b5bSAndy Whitcroft } 12510a920b5bSAndy Whitcroft next; 12520a920b5bSAndy Whitcroft } 12530a920b5bSAndy Whitcroft $res .= $c; 12540a920b5bSAndy Whitcroft $n++; 12550a920b5bSAndy Whitcroft } 12560a920b5bSAndy Whitcroft 12570a920b5bSAndy Whitcroft return $res; 12580a920b5bSAndy Whitcroft} 12596c72ffaaSAndy Whitcroftsub copy_spacing { 1260773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 12616c72ffaaSAndy Whitcroft return $res; 12626c72ffaaSAndy Whitcroft} 12630a920b5bSAndy Whitcroft 12644a0df2efSAndy Whitcroftsub line_stats { 12654a0df2efSAndy Whitcroft my ($line) = @_; 12664a0df2efSAndy Whitcroft 12674a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 12684a0df2efSAndy Whitcroft $line =~ s/^.//; 12694a0df2efSAndy Whitcroft $line = expand_tabs($line); 12704a0df2efSAndy Whitcroft 12714a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 12724a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 12734a0df2efSAndy Whitcroft 12744a0df2efSAndy Whitcroft return (length($line), length($white)); 12754a0df2efSAndy Whitcroft} 12764a0df2efSAndy Whitcroft 1277773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1278773647a0SAndy Whitcroft 1279773647a0SAndy Whitcroftsub sanitise_line_reset { 1280773647a0SAndy Whitcroft my ($in_comment) = @_; 1281773647a0SAndy Whitcroft 1282773647a0SAndy Whitcroft if ($in_comment) { 1283773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1284773647a0SAndy Whitcroft } else { 1285773647a0SAndy Whitcroft $sanitise_quote = ''; 1286773647a0SAndy Whitcroft } 1287773647a0SAndy Whitcroft} 128800df344fSAndy Whitcroftsub sanitise_line { 128900df344fSAndy Whitcroft my ($line) = @_; 129000df344fSAndy Whitcroft 129100df344fSAndy Whitcroft my $res = ''; 129200df344fSAndy Whitcroft my $l = ''; 129300df344fSAndy Whitcroft 1294c2fdda0dSAndy Whitcroft my $qlen = 0; 1295773647a0SAndy Whitcroft my $off = 0; 1296773647a0SAndy Whitcroft my $c; 129700df344fSAndy Whitcroft 1298773647a0SAndy Whitcroft # Always copy over the diff marker. 1299773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1300773647a0SAndy Whitcroft 1301773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1302773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1303773647a0SAndy Whitcroft 13048d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1305773647a0SAndy Whitcroft # and end, all to $;. 1306773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1307773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1308773647a0SAndy Whitcroft 1309773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1310773647a0SAndy Whitcroft $off++; 131100df344fSAndy Whitcroft next; 1312773647a0SAndy Whitcroft } 131381bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1314773647a0SAndy Whitcroft $sanitise_quote = ''; 1315773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1316773647a0SAndy Whitcroft $off++; 1317773647a0SAndy Whitcroft next; 1318773647a0SAndy Whitcroft } 1319113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1320113f04a8SDaniel Walker $sanitise_quote = '//'; 1321113f04a8SDaniel Walker 1322113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1323113f04a8SDaniel Walker $off++; 1324113f04a8SDaniel Walker next; 1325113f04a8SDaniel Walker } 1326773647a0SAndy Whitcroft 1327773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1328773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1329773647a0SAndy Whitcroft $c eq "\\") { 1330773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1331773647a0SAndy Whitcroft $off++; 1332773647a0SAndy Whitcroft next; 1333773647a0SAndy Whitcroft } 1334773647a0SAndy Whitcroft # Regular quotes. 1335773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1336773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1337773647a0SAndy Whitcroft $sanitise_quote = $c; 1338773647a0SAndy Whitcroft 1339773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1340773647a0SAndy Whitcroft next; 1341773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1342773647a0SAndy Whitcroft $sanitise_quote = ''; 134300df344fSAndy Whitcroft } 134400df344fSAndy Whitcroft } 1345773647a0SAndy Whitcroft 1346fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1347773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1348773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1349113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1350113f04a8SDaniel Walker substr($res, $off, 1, $;); 1351773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1352773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 135300df344fSAndy Whitcroft } else { 1354773647a0SAndy Whitcroft substr($res, $off, 1, $c); 135500df344fSAndy Whitcroft } 1356c2fdda0dSAndy Whitcroft } 1357c2fdda0dSAndy Whitcroft 1358113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1359113f04a8SDaniel Walker $sanitise_quote = ''; 1360113f04a8SDaniel Walker } 1361113f04a8SDaniel Walker 1362c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1363c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1364c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1365c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1366c2fdda0dSAndy Whitcroft 1367c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1368c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1369c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1370c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1371c2fdda0dSAndy Whitcroft } 1372c2fdda0dSAndy Whitcroft 1373dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1374dadf680dSJoe Perches my $match = $1; 1375dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1376dadf680dSJoe Perches } 1377dadf680dSJoe Perches 137800df344fSAndy Whitcroft return $res; 137900df344fSAndy Whitcroft} 138000df344fSAndy Whitcroft 1381a6962d72SJoe Perchessub get_quoted_string { 1382a6962d72SJoe Perches my ($line, $rawline) = @_; 1383a6962d72SJoe Perches 1384478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 138533acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1386a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1387a6962d72SJoe Perches} 1388a6962d72SJoe Perches 13898905a67cSAndy Whitcroftsub ctx_statement_block { 13908905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 13918905a67cSAndy Whitcroft my $line = $linenr - 1; 13928905a67cSAndy Whitcroft my $blk = ''; 13938905a67cSAndy Whitcroft my $soff = $off; 13948905a67cSAndy Whitcroft my $coff = $off - 1; 1395773647a0SAndy Whitcroft my $coff_set = 0; 13968905a67cSAndy Whitcroft 139713214adfSAndy Whitcroft my $loff = 0; 139813214adfSAndy Whitcroft 13998905a67cSAndy Whitcroft my $type = ''; 14008905a67cSAndy Whitcroft my $level = 0; 1401a2750645SAndy Whitcroft my @stack = (); 1402cf655043SAndy Whitcroft my $p; 14038905a67cSAndy Whitcroft my $c; 14048905a67cSAndy Whitcroft my $len = 0; 140513214adfSAndy Whitcroft 140613214adfSAndy Whitcroft my $remainder; 14078905a67cSAndy Whitcroft while (1) { 1408a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1409a2750645SAndy Whitcroft 1410773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 14118905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 14128905a67cSAndy Whitcroft # context. 14138905a67cSAndy Whitcroft if ($off >= $len) { 14148905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1415dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1416c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 14178905a67cSAndy Whitcroft $remain--; 141813214adfSAndy Whitcroft $loff = $len; 1419c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 14208905a67cSAndy Whitcroft $len = length($blk); 14218905a67cSAndy Whitcroft $line++; 14228905a67cSAndy Whitcroft last; 14238905a67cSAndy Whitcroft } 14248905a67cSAndy Whitcroft # Bail if there is no further context. 14258905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 142613214adfSAndy Whitcroft if ($off >= $len) { 14278905a67cSAndy Whitcroft last; 14288905a67cSAndy Whitcroft } 1429f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1430f74bd194SAndy Whitcroft $level++; 1431f74bd194SAndy Whitcroft $type = '#'; 1432f74bd194SAndy Whitcroft } 14338905a67cSAndy Whitcroft } 1434cf655043SAndy Whitcroft $p = $c; 14358905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 143613214adfSAndy Whitcroft $remainder = substr($blk, $off); 14378905a67cSAndy Whitcroft 1438773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 14394635f4fbSAndy Whitcroft 14404635f4fbSAndy Whitcroft # Handle nested #if/#else. 14414635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 14424635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 14434635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 14444635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 14454635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 14464635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 14474635f4fbSAndy Whitcroft } 14484635f4fbSAndy Whitcroft 14498905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 14508905a67cSAndy Whitcroft # outermost level. 14518905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 14528905a67cSAndy Whitcroft last; 14538905a67cSAndy Whitcroft } 14548905a67cSAndy Whitcroft 145513214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1456773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1457773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1458773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1459773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1460773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1461773647a0SAndy Whitcroft $coff_set = 1; 1462773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1463773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 146413214adfSAndy Whitcroft } 146513214adfSAndy Whitcroft 14668905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 14678905a67cSAndy Whitcroft $level++; 14688905a67cSAndy Whitcroft $type = '('; 14698905a67cSAndy Whitcroft } 14708905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 14718905a67cSAndy Whitcroft $level--; 14728905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 14738905a67cSAndy Whitcroft 14748905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 14758905a67cSAndy Whitcroft $coff = $off; 1476773647a0SAndy Whitcroft $coff_set = 1; 1477773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 14788905a67cSAndy Whitcroft } 14798905a67cSAndy Whitcroft } 14808905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 14818905a67cSAndy Whitcroft $level++; 14828905a67cSAndy Whitcroft $type = '{'; 14838905a67cSAndy Whitcroft } 14848905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 14858905a67cSAndy Whitcroft $level--; 14868905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 14878905a67cSAndy Whitcroft 14888905a67cSAndy Whitcroft if ($level == 0) { 1489b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1490b998e001SPatrick Pannuto $off++; 1491b998e001SPatrick Pannuto } 14928905a67cSAndy Whitcroft last; 14938905a67cSAndy Whitcroft } 14948905a67cSAndy Whitcroft } 1495f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1496f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1497f74bd194SAndy Whitcroft $level--; 1498f74bd194SAndy Whitcroft $type = ''; 1499f74bd194SAndy Whitcroft $off++; 1500f74bd194SAndy Whitcroft last; 1501f74bd194SAndy Whitcroft } 15028905a67cSAndy Whitcroft $off++; 15038905a67cSAndy Whitcroft } 1504a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 150513214adfSAndy Whitcroft if ($off == $len) { 1506a3bb97a7SAndy Whitcroft $loff = $len + 1; 150713214adfSAndy Whitcroft $line++; 150813214adfSAndy Whitcroft $remain--; 150913214adfSAndy Whitcroft } 15108905a67cSAndy Whitcroft 15118905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 15128905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 15138905a67cSAndy Whitcroft 15148905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 15158905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 15168905a67cSAndy Whitcroft 1517773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 151813214adfSAndy Whitcroft 151913214adfSAndy Whitcroft return ($statement, $condition, 152013214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 152113214adfSAndy Whitcroft} 152213214adfSAndy Whitcroft 1523cf655043SAndy Whitcroftsub statement_lines { 1524cf655043SAndy Whitcroft my ($stmt) = @_; 1525cf655043SAndy Whitcroft 1526cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1527cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1528cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1529cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1530cf655043SAndy Whitcroft 1531cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1532cf655043SAndy Whitcroft 1533cf655043SAndy Whitcroft return $#stmt_lines + 2; 1534cf655043SAndy Whitcroft} 1535cf655043SAndy Whitcroft 1536cf655043SAndy Whitcroftsub statement_rawlines { 1537cf655043SAndy Whitcroft my ($stmt) = @_; 1538cf655043SAndy Whitcroft 1539cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1540cf655043SAndy Whitcroft 1541cf655043SAndy Whitcroft return $#stmt_lines + 2; 1542cf655043SAndy Whitcroft} 1543cf655043SAndy Whitcroft 1544cf655043SAndy Whitcroftsub statement_block_size { 1545cf655043SAndy Whitcroft my ($stmt) = @_; 1546cf655043SAndy Whitcroft 1547cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1548cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1549cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1550cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1551cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1552cf655043SAndy Whitcroft 1553cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1554cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1555cf655043SAndy Whitcroft 1556cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1557cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1558cf655043SAndy Whitcroft 1559cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1560cf655043SAndy Whitcroft return $stmt_lines; 1561cf655043SAndy Whitcroft } else { 1562cf655043SAndy Whitcroft return $stmt_statements; 1563cf655043SAndy Whitcroft } 1564cf655043SAndy Whitcroft} 1565cf655043SAndy Whitcroft 156613214adfSAndy Whitcroftsub ctx_statement_full { 156713214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 156813214adfSAndy Whitcroft my ($statement, $condition, $level); 156913214adfSAndy Whitcroft 157013214adfSAndy Whitcroft my (@chunks); 157113214adfSAndy Whitcroft 1572cf655043SAndy Whitcroft # Grab the first conditional/block pair. 157313214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 157413214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1575773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 157613214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1577cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1578cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1579cf655043SAndy Whitcroft } 1580cf655043SAndy Whitcroft 1581cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1582cf655043SAndy Whitcroft # could continue the statement. 1583cf655043SAndy Whitcroft for (;;) { 158413214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 158513214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1586cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1587773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1588cf655043SAndy Whitcroft #print "C: push\n"; 1589cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 159013214adfSAndy Whitcroft } 159113214adfSAndy Whitcroft 159213214adfSAndy Whitcroft return ($level, $linenr, @chunks); 15938905a67cSAndy Whitcroft} 15948905a67cSAndy Whitcroft 15954a0df2efSAndy Whitcroftsub ctx_block_get { 1596f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 15974a0df2efSAndy Whitcroft my $line; 15984a0df2efSAndy Whitcroft my $start = $linenr - 1; 15994a0df2efSAndy Whitcroft my $blk = ''; 16004a0df2efSAndy Whitcroft my @o; 16014a0df2efSAndy Whitcroft my @c; 16024a0df2efSAndy Whitcroft my @res = (); 16034a0df2efSAndy Whitcroft 1604f0a594c1SAndy Whitcroft my $level = 0; 16054635f4fbSAndy Whitcroft my @stack = ($level); 160600df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 160700df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 160800df344fSAndy Whitcroft $remain--; 160900df344fSAndy Whitcroft 161000df344fSAndy Whitcroft $blk .= $rawlines[$line]; 16114635f4fbSAndy Whitcroft 16124635f4fbSAndy Whitcroft # Handle nested #if/#else. 161301464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 16144635f4fbSAndy Whitcroft push(@stack, $level); 161501464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 16164635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 161701464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 16184635f4fbSAndy Whitcroft $level = pop(@stack); 16194635f4fbSAndy Whitcroft } 16204635f4fbSAndy Whitcroft 162101464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1622f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1623f0a594c1SAndy Whitcroft if ($off > 0) { 1624f0a594c1SAndy Whitcroft $off--; 1625f0a594c1SAndy Whitcroft next; 1626f0a594c1SAndy Whitcroft } 16274a0df2efSAndy Whitcroft 1628f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1629f0a594c1SAndy Whitcroft $level--; 1630f0a594c1SAndy Whitcroft last if ($level == 0); 1631f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1632f0a594c1SAndy Whitcroft $level++; 1633f0a594c1SAndy Whitcroft } 1634f0a594c1SAndy Whitcroft } 16354a0df2efSAndy Whitcroft 1636f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 163700df344fSAndy Whitcroft push(@res, $rawlines[$line]); 16384a0df2efSAndy Whitcroft } 16394a0df2efSAndy Whitcroft 1640f0a594c1SAndy Whitcroft last if ($level == 0); 16414a0df2efSAndy Whitcroft } 16424a0df2efSAndy Whitcroft 1643f0a594c1SAndy Whitcroft return ($level, @res); 16444a0df2efSAndy Whitcroft} 16454a0df2efSAndy Whitcroftsub ctx_block_outer { 16464a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16474a0df2efSAndy Whitcroft 1648f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1649f0a594c1SAndy Whitcroft return @r; 16504a0df2efSAndy Whitcroft} 16514a0df2efSAndy Whitcroftsub ctx_block { 16524a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16534a0df2efSAndy Whitcroft 1654f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1655f0a594c1SAndy Whitcroft return @r; 1656653d4876SAndy Whitcroft} 1657653d4876SAndy Whitcroftsub ctx_statement { 1658f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1659f0a594c1SAndy Whitcroft 1660f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1661f0a594c1SAndy Whitcroft return @r; 1662f0a594c1SAndy Whitcroft} 1663f0a594c1SAndy Whitcroftsub ctx_block_level { 1664653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1665653d4876SAndy Whitcroft 1666f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 16674a0df2efSAndy Whitcroft} 16689c0ca6f9SAndy Whitcroftsub ctx_statement_level { 16699c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 16709c0ca6f9SAndy Whitcroft 16719c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 16729c0ca6f9SAndy Whitcroft} 16734a0df2efSAndy Whitcroft 16744a0df2efSAndy Whitcroftsub ctx_locate_comment { 16754a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16764a0df2efSAndy Whitcroft 16774a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1678beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 16794a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 16804a0df2efSAndy Whitcroft 16814a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 16824a0df2efSAndy Whitcroft # comment. 16834a0df2efSAndy Whitcroft my $in_comment = 0; 16844a0df2efSAndy Whitcroft $current_comment = ''; 16854a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 168600df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 168700df344fSAndy Whitcroft #warn " $line\n"; 16884a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 16894a0df2efSAndy Whitcroft $in_comment = 1; 16904a0df2efSAndy Whitcroft } 16914a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 16924a0df2efSAndy Whitcroft $in_comment = 1; 16934a0df2efSAndy Whitcroft } 16944a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 16954a0df2efSAndy Whitcroft $current_comment = ''; 16964a0df2efSAndy Whitcroft } 16974a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 16984a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 16994a0df2efSAndy Whitcroft $in_comment = 0; 17004a0df2efSAndy Whitcroft } 17014a0df2efSAndy Whitcroft } 17024a0df2efSAndy Whitcroft 17034a0df2efSAndy Whitcroft chomp($current_comment); 17044a0df2efSAndy Whitcroft return($current_comment); 17054a0df2efSAndy Whitcroft} 17064a0df2efSAndy Whitcroftsub ctx_has_comment { 17074a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17084a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 17094a0df2efSAndy Whitcroft 171000df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 17114a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 17124a0df2efSAndy Whitcroft 17134a0df2efSAndy Whitcroft return ($cmt ne ''); 17144a0df2efSAndy Whitcroft} 17154a0df2efSAndy Whitcroft 17164d001e4dSAndy Whitcroftsub raw_line { 17174d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 17184d001e4dSAndy Whitcroft 17194d001e4dSAndy Whitcroft my $offset = $linenr - 1; 17204d001e4dSAndy Whitcroft $cnt++; 17214d001e4dSAndy Whitcroft 17224d001e4dSAndy Whitcroft my $line; 17234d001e4dSAndy Whitcroft while ($cnt) { 17244d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 17254d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 17264d001e4dSAndy Whitcroft $cnt--; 17274d001e4dSAndy Whitcroft } 17284d001e4dSAndy Whitcroft 17294d001e4dSAndy Whitcroft return $line; 17304d001e4dSAndy Whitcroft} 17314d001e4dSAndy Whitcroft 17322a9f9d85STobin C. Hardingsub get_stat_real { 17332a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 17342a9f9d85STobin C. Harding 17352a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 17362a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 17372a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 17382a9f9d85STobin C. Harding } 17392a9f9d85STobin C. Harding 17402a9f9d85STobin C. Harding return $stat_real; 17412a9f9d85STobin C. Harding} 17422a9f9d85STobin C. Harding 1743e3d95a2aSTobin C. Hardingsub get_stat_here { 1744e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1745e3d95a2aSTobin C. Harding 1746e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1747e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1748e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1749e3d95a2aSTobin C. Harding } 1750e3d95a2aSTobin C. Harding 1751e3d95a2aSTobin C. Harding return $herectx; 1752e3d95a2aSTobin C. Harding} 1753e3d95a2aSTobin C. Harding 17540a920b5bSAndy Whitcroftsub cat_vet { 17550a920b5bSAndy Whitcroft my ($vet) = @_; 17569c0ca6f9SAndy Whitcroft my ($res, $coded); 17570a920b5bSAndy Whitcroft 17589c0ca6f9SAndy Whitcroft $res = ''; 17596c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 17606c72ffaaSAndy Whitcroft $res .= $1; 17616c72ffaaSAndy Whitcroft if ($2 ne '') { 17629c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 17636c72ffaaSAndy Whitcroft $res .= $coded; 17646c72ffaaSAndy Whitcroft } 17659c0ca6f9SAndy Whitcroft } 17669c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 17670a920b5bSAndy Whitcroft 17689c0ca6f9SAndy Whitcroft return $res; 17690a920b5bSAndy Whitcroft} 17700a920b5bSAndy Whitcroft 1771c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1772cf655043SAndy Whitcroftmy $av_pending; 1773c2fdda0dSAndy Whitcroftmy @av_paren_type; 17741f65f947SAndy Whitcroftmy $av_pend_colon; 1775c2fdda0dSAndy Whitcroft 1776c2fdda0dSAndy Whitcroftsub annotate_reset { 1777c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1778cf655043SAndy Whitcroft $av_pending = '_'; 1779cf655043SAndy Whitcroft @av_paren_type = ('E'); 17801f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1781c2fdda0dSAndy Whitcroft} 1782c2fdda0dSAndy Whitcroft 17836c72ffaaSAndy Whitcroftsub annotate_values { 17846c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 17856c72ffaaSAndy Whitcroft 17866c72ffaaSAndy Whitcroft my $res; 17871f65f947SAndy Whitcroft my $var = '_' x length($stream); 17886c72ffaaSAndy Whitcroft my $cur = $stream; 17896c72ffaaSAndy Whitcroft 1790c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 17916c72ffaaSAndy Whitcroft 17926c72ffaaSAndy Whitcroft while (length($cur)) { 1793773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1794cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1795171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 17966c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1797c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1798c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1799cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1800c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 18016c72ffaaSAndy Whitcroft } 18026c72ffaaSAndy Whitcroft 1803c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 18049446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 18059446ef56SAndy Whitcroft push(@av_paren_type, $type); 1806addcdceaSAndy Whitcroft $type = 'c'; 18079446ef56SAndy Whitcroft 1808e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1809c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 18106c72ffaaSAndy Whitcroft $type = 'T'; 18116c72ffaaSAndy Whitcroft 1812389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1813389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1814389a2fe5SAndy Whitcroft $type = 'T'; 1815389a2fe5SAndy Whitcroft 1816c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1817171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1818c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1819171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1820171ae1a4SAndy Whitcroft if ($2 ne '') { 1821cf655043SAndy Whitcroft $av_pending = 'N'; 1822171ae1a4SAndy Whitcroft } 1823171ae1a4SAndy Whitcroft $type = 'E'; 1824171ae1a4SAndy Whitcroft 1825c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1826171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1827171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1828171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 18296c72ffaaSAndy Whitcroft 1830c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1831cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1832c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1833cf655043SAndy Whitcroft 1834cf655043SAndy Whitcroft push(@av_paren_type, $type); 1835cf655043SAndy Whitcroft push(@av_paren_type, $type); 1836171ae1a4SAndy Whitcroft $type = 'E'; 1837cf655043SAndy Whitcroft 1838c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1839cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1840cf655043SAndy Whitcroft $av_preprocessor = 1; 1841cf655043SAndy Whitcroft 1842cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1843cf655043SAndy Whitcroft 1844171ae1a4SAndy Whitcroft $type = 'E'; 1845cf655043SAndy Whitcroft 1846c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1847cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1848cf655043SAndy Whitcroft 1849cf655043SAndy Whitcroft $av_preprocessor = 1; 1850cf655043SAndy Whitcroft 1851cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1852cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1853cf655043SAndy Whitcroft pop(@av_paren_type); 1854cf655043SAndy Whitcroft push(@av_paren_type, $type); 1855171ae1a4SAndy Whitcroft $type = 'E'; 18566c72ffaaSAndy Whitcroft 18576c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1858c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 18596c72ffaaSAndy Whitcroft 1860171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1861171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1862171ae1a4SAndy Whitcroft $av_pending = $type; 1863171ae1a4SAndy Whitcroft $type = 'N'; 1864171ae1a4SAndy Whitcroft 18656c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1866c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 18676c72ffaaSAndy Whitcroft if (defined $2) { 1868cf655043SAndy Whitcroft $av_pending = 'V'; 18696c72ffaaSAndy Whitcroft } 18706c72ffaaSAndy Whitcroft $type = 'N'; 18716c72ffaaSAndy Whitcroft 187214b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1873c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 187414b111c1SAndy Whitcroft $av_pending = 'E'; 18756c72ffaaSAndy Whitcroft $type = 'N'; 18766c72ffaaSAndy Whitcroft 18771f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 18781f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 18791f65f947SAndy Whitcroft $av_pend_colon = 'C'; 18801f65f947SAndy Whitcroft $type = 'N'; 18811f65f947SAndy Whitcroft 188214b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1883c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 18846c72ffaaSAndy Whitcroft $type = 'N'; 18856c72ffaaSAndy Whitcroft 18866c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1887c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1888cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1889cf655043SAndy Whitcroft $av_pending = '_'; 18906c72ffaaSAndy Whitcroft $type = 'N'; 18916c72ffaaSAndy Whitcroft 18926c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1893cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1894cf655043SAndy Whitcroft if ($new_type ne '_') { 1895cf655043SAndy Whitcroft $type = $new_type; 1896c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1897c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 18986c72ffaaSAndy Whitcroft } else { 1899c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 19006c72ffaaSAndy Whitcroft } 19016c72ffaaSAndy Whitcroft 1902c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1903c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1904c8cb2ca3SAndy Whitcroft $type = 'V'; 1905cf655043SAndy Whitcroft $av_pending = 'V'; 19066c72ffaaSAndy Whitcroft 19078e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 19088e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 19091f65f947SAndy Whitcroft $av_pend_colon = 'B'; 19108e761b04SAndy Whitcroft } elsif ($type eq 'E') { 19118e761b04SAndy Whitcroft $av_pend_colon = 'L'; 19121f65f947SAndy Whitcroft } 19131f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 19141f65f947SAndy Whitcroft $type = 'V'; 19151f65f947SAndy Whitcroft 19166c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1917c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 19186c72ffaaSAndy Whitcroft $type = 'V'; 19196c72ffaaSAndy Whitcroft 19206c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1921c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 19226c72ffaaSAndy Whitcroft $type = 'N'; 19236c72ffaaSAndy Whitcroft 1924cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1925c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 192613214adfSAndy Whitcroft $type = 'E'; 19271f65f947SAndy Whitcroft $av_pend_colon = 'O'; 192813214adfSAndy Whitcroft 19298e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 19308e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 19318e761b04SAndy Whitcroft $type = 'C'; 19328e761b04SAndy Whitcroft 19331f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 19341f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 19351f65f947SAndy Whitcroft $type = 'N'; 19361f65f947SAndy Whitcroft 19371f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 19381f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 19391f65f947SAndy Whitcroft 19401f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 19411f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 19421f65f947SAndy Whitcroft $type = 'E'; 19431f65f947SAndy Whitcroft } else { 19441f65f947SAndy Whitcroft $type = 'N'; 19451f65f947SAndy Whitcroft } 19461f65f947SAndy Whitcroft $av_pend_colon = 'O'; 19471f65f947SAndy Whitcroft 19488e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 194913214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 19506c72ffaaSAndy Whitcroft $type = 'N'; 19516c72ffaaSAndy Whitcroft 19520d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 195374048ed8SAndy Whitcroft my $variant; 195474048ed8SAndy Whitcroft 195574048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 195674048ed8SAndy Whitcroft if ($type eq 'V') { 195774048ed8SAndy Whitcroft $variant = 'B'; 195874048ed8SAndy Whitcroft } else { 195974048ed8SAndy Whitcroft $variant = 'U'; 196074048ed8SAndy Whitcroft } 196174048ed8SAndy Whitcroft 196274048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 196374048ed8SAndy Whitcroft $type = 'N'; 196474048ed8SAndy Whitcroft 19656c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1966c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 19676c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 19686c72ffaaSAndy Whitcroft $type = 'N'; 19696c72ffaaSAndy Whitcroft } 19706c72ffaaSAndy Whitcroft 19716c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1972c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 19736c72ffaaSAndy Whitcroft } 19746c72ffaaSAndy Whitcroft if (defined $1) { 19756c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 19766c72ffaaSAndy Whitcroft $res .= $type x length($1); 19776c72ffaaSAndy Whitcroft } 19786c72ffaaSAndy Whitcroft } 19796c72ffaaSAndy Whitcroft 19801f65f947SAndy Whitcroft return ($res, $var); 19816c72ffaaSAndy Whitcroft} 19826c72ffaaSAndy Whitcroft 19838905a67cSAndy Whitcroftsub possible { 198413214adfSAndy Whitcroft my ($possible, $line) = @_; 19859a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 19860776e594SAndy Whitcroft ^(?: 19870776e594SAndy Whitcroft $Modifier| 19880776e594SAndy Whitcroft $Storage| 19890776e594SAndy Whitcroft $Type| 19909a974fdbSAndy Whitcroft DEFINE_\S+ 19919a974fdbSAndy Whitcroft )$| 19929a974fdbSAndy Whitcroft ^(?: 19930776e594SAndy Whitcroft goto| 19940776e594SAndy Whitcroft return| 19950776e594SAndy Whitcroft case| 19960776e594SAndy Whitcroft else| 19970776e594SAndy Whitcroft asm|__asm__| 199889a88353SAndy Whitcroft do| 199989a88353SAndy Whitcroft \#| 200089a88353SAndy Whitcroft \#\#| 20019a974fdbSAndy Whitcroft )(?:\s|$)| 20020776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 20039a974fdbSAndy Whitcroft )}x; 20049a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 20059a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2006c45dcabdSAndy Whitcroft # Check for modifiers. 2007c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2008c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2009c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2010c45dcabdSAndy Whitcroft 2011c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2012c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2013d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 20149a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2015d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2016485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2017d2506586SAndy Whitcroft } 20189a974fdbSAndy Whitcroft } 2019c45dcabdSAndy Whitcroft 2020c45dcabdSAndy Whitcroft } else { 202113214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2022485ff23eSAlex Dowad push(@typeListFile, $possible); 2023c45dcabdSAndy Whitcroft } 20248905a67cSAndy Whitcroft build_types(); 20250776e594SAndy Whitcroft } else { 20260776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 20278905a67cSAndy Whitcroft } 20288905a67cSAndy Whitcroft} 20298905a67cSAndy Whitcroft 20306c72ffaaSAndy Whitcroftmy $prefix = ''; 20316c72ffaaSAndy Whitcroft 2032000d1cc1SJoe Perchessub show_type { 2033cbec18afSJoe Perches my ($type) = @_; 203491bfe484SJoe Perches 2035522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2036522b837cSAlexey Dobriyan 2037cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2038cbec18afSJoe Perches 2039cbec18afSJoe Perches return !defined $ignore_type{$type}; 2040000d1cc1SJoe Perches} 2041000d1cc1SJoe Perches 2042f0a594c1SAndy Whitcroftsub report { 2043cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2044cbec18afSJoe Perches 2045cbec18afSJoe Perches if (!show_type($type) || 2046cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2047773647a0SAndy Whitcroft return 0; 2048773647a0SAndy Whitcroft } 204957230297SJoe Perches my $output = ''; 2050737c0767SJohn Brooks if ($color) { 205157230297SJoe Perches if ($level eq 'ERROR') { 205257230297SJoe Perches $output .= RED; 205357230297SJoe Perches } elsif ($level eq 'WARNING') { 205457230297SJoe Perches $output .= YELLOW; 2055000d1cc1SJoe Perches } else { 205657230297SJoe Perches $output .= GREEN; 2057000d1cc1SJoe Perches } 205857230297SJoe Perches } 205957230297SJoe Perches $output .= $prefix . $level . ':'; 206057230297SJoe Perches if ($show_types) { 2061737c0767SJohn Brooks $output .= BLUE if ($color); 206257230297SJoe Perches $output .= "$type:"; 206357230297SJoe Perches } 2064737c0767SJohn Brooks $output .= RESET if ($color); 206557230297SJoe Perches $output .= ' ' . $msg . "\n"; 206634d8815fSJoe Perches 206734d8815fSJoe Perches if ($showfile) { 206834d8815fSJoe Perches my @lines = split("\n", $output, -1); 206934d8815fSJoe Perches splice(@lines, 1, 1); 207034d8815fSJoe Perches $output = join("\n", @lines); 207134d8815fSJoe Perches } 207257230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 20738905a67cSAndy Whitcroft 207457230297SJoe Perches push(our @report, $output); 2075773647a0SAndy Whitcroft 2076773647a0SAndy Whitcroft return 1; 2077f0a594c1SAndy Whitcroft} 2078cbec18afSJoe Perches 2079f0a594c1SAndy Whitcroftsub report_dump { 208013214adfSAndy Whitcroft our @report; 2081f0a594c1SAndy Whitcroft} 2082000d1cc1SJoe Perches 2083d752fcc8SJoe Perchessub fixup_current_range { 2084d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2085d752fcc8SJoe Perches 2086d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2087d752fcc8SJoe Perches my $o = $1; 2088d752fcc8SJoe Perches my $l = $2; 2089d752fcc8SJoe Perches my $no = $o + $offset; 2090d752fcc8SJoe Perches my $nl = $l + $length; 2091d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2092d752fcc8SJoe Perches } 2093d752fcc8SJoe Perches} 2094d752fcc8SJoe Perches 2095d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2096d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2097d752fcc8SJoe Perches 2098d752fcc8SJoe Perches my $range_last_linenr = 0; 2099d752fcc8SJoe Perches my $delta_offset = 0; 2100d752fcc8SJoe Perches 2101d752fcc8SJoe Perches my $old_linenr = 0; 2102d752fcc8SJoe Perches my $new_linenr = 0; 2103d752fcc8SJoe Perches 2104d752fcc8SJoe Perches my $next_insert = 0; 2105d752fcc8SJoe Perches my $next_delete = 0; 2106d752fcc8SJoe Perches 2107d752fcc8SJoe Perches my @lines = (); 2108d752fcc8SJoe Perches 2109d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2110d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2111d752fcc8SJoe Perches 2112d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2113d752fcc8SJoe Perches my $save_line = 1; 2114d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2115323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2116d752fcc8SJoe Perches $delta_offset = 0; 2117d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2118d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2119d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2120d752fcc8SJoe Perches } 2121d752fcc8SJoe Perches 2122d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2123d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2124d752fcc8SJoe Perches $save_line = 0; 2125d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2126d752fcc8SJoe Perches } 2127d752fcc8SJoe Perches 2128d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2129d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2130d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2131d752fcc8SJoe Perches $new_linenr++; 2132d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2133d752fcc8SJoe Perches } 2134d752fcc8SJoe Perches 2135d752fcc8SJoe Perches if ($save_line) { 2136d752fcc8SJoe Perches push(@lines, $line); 2137d752fcc8SJoe Perches $new_linenr++; 2138d752fcc8SJoe Perches } 2139d752fcc8SJoe Perches 2140d752fcc8SJoe Perches $old_linenr++; 2141d752fcc8SJoe Perches } 2142d752fcc8SJoe Perches 2143d752fcc8SJoe Perches return @lines; 2144d752fcc8SJoe Perches} 2145d752fcc8SJoe Perches 2146f2d7e4d4SJoe Perchessub fix_insert_line { 2147f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2148f2d7e4d4SJoe Perches 2149f2d7e4d4SJoe Perches my $inserted = { 2150f2d7e4d4SJoe Perches LINENR => $linenr, 2151f2d7e4d4SJoe Perches LINE => $line, 2152f2d7e4d4SJoe Perches }; 2153f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2154f2d7e4d4SJoe Perches} 2155f2d7e4d4SJoe Perches 2156f2d7e4d4SJoe Perchessub fix_delete_line { 2157f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2158f2d7e4d4SJoe Perches 2159f2d7e4d4SJoe Perches my $deleted = { 2160f2d7e4d4SJoe Perches LINENR => $linenr, 2161f2d7e4d4SJoe Perches LINE => $line, 2162f2d7e4d4SJoe Perches }; 2163f2d7e4d4SJoe Perches 2164f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2165f2d7e4d4SJoe Perches} 2166f2d7e4d4SJoe Perches 2167de7d4f0eSAndy Whitcroftsub ERROR { 2168cbec18afSJoe Perches my ($type, $msg) = @_; 2169cbec18afSJoe Perches 2170cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2171de7d4f0eSAndy Whitcroft our $clean = 0; 21726c72ffaaSAndy Whitcroft our $cnt_error++; 21733705ce5bSJoe Perches return 1; 2174de7d4f0eSAndy Whitcroft } 21753705ce5bSJoe Perches return 0; 2176773647a0SAndy Whitcroft} 2177de7d4f0eSAndy Whitcroftsub WARN { 2178cbec18afSJoe Perches my ($type, $msg) = @_; 2179cbec18afSJoe Perches 2180cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2181de7d4f0eSAndy Whitcroft our $clean = 0; 21826c72ffaaSAndy Whitcroft our $cnt_warn++; 21833705ce5bSJoe Perches return 1; 2184de7d4f0eSAndy Whitcroft } 21853705ce5bSJoe Perches return 0; 2186773647a0SAndy Whitcroft} 2187de7d4f0eSAndy Whitcroftsub CHK { 2188cbec18afSJoe Perches my ($type, $msg) = @_; 2189cbec18afSJoe Perches 2190cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2191de7d4f0eSAndy Whitcroft our $clean = 0; 21926c72ffaaSAndy Whitcroft our $cnt_chk++; 21933705ce5bSJoe Perches return 1; 21946c72ffaaSAndy Whitcroft } 21953705ce5bSJoe Perches return 0; 2196de7d4f0eSAndy Whitcroft} 2197de7d4f0eSAndy Whitcroft 21986ecd9674SAndy Whitcroftsub check_absolute_file { 21996ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 22006ecd9674SAndy Whitcroft my $file = $absolute; 22016ecd9674SAndy Whitcroft 22026ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 22036ecd9674SAndy Whitcroft 22046ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 22056ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 22066ecd9674SAndy Whitcroft if (-f "$root/$file") { 22076ecd9674SAndy Whitcroft ##print "file<$file>\n"; 22086ecd9674SAndy Whitcroft last; 22096ecd9674SAndy Whitcroft } 22106ecd9674SAndy Whitcroft } 22116ecd9674SAndy Whitcroft if (! -f _) { 22126ecd9674SAndy Whitcroft return 0; 22136ecd9674SAndy Whitcroft } 22146ecd9674SAndy Whitcroft 22156ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 22166ecd9674SAndy Whitcroft my $prefix = $absolute; 22176ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 22186ecd9674SAndy Whitcroft 22196ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 22206ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2221000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2222000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 22236ecd9674SAndy Whitcroft } 22246ecd9674SAndy Whitcroft} 22256ecd9674SAndy Whitcroft 22263705ce5bSJoe Perchessub trim { 22273705ce5bSJoe Perches my ($string) = @_; 22283705ce5bSJoe Perches 2229b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2230b34c648bSJoe Perches 2231b34c648bSJoe Perches return $string; 2232b34c648bSJoe Perches} 2233b34c648bSJoe Perches 2234b34c648bSJoe Perchessub ltrim { 2235b34c648bSJoe Perches my ($string) = @_; 2236b34c648bSJoe Perches 2237b34c648bSJoe Perches $string =~ s/^\s+//; 2238b34c648bSJoe Perches 2239b34c648bSJoe Perches return $string; 2240b34c648bSJoe Perches} 2241b34c648bSJoe Perches 2242b34c648bSJoe Perchessub rtrim { 2243b34c648bSJoe Perches my ($string) = @_; 2244b34c648bSJoe Perches 2245b34c648bSJoe Perches $string =~ s/\s+$//; 22463705ce5bSJoe Perches 22473705ce5bSJoe Perches return $string; 22483705ce5bSJoe Perches} 22493705ce5bSJoe Perches 225052ea8506SJoe Perchessub string_find_replace { 225152ea8506SJoe Perches my ($string, $find, $replace) = @_; 225252ea8506SJoe Perches 225352ea8506SJoe Perches $string =~ s/$find/$replace/g; 225452ea8506SJoe Perches 225552ea8506SJoe Perches return $string; 225652ea8506SJoe Perches} 225752ea8506SJoe Perches 22583705ce5bSJoe Perchessub tabify { 22593705ce5bSJoe Perches my ($leading) = @_; 22603705ce5bSJoe Perches 2261713a09deSAntonio Borneo my $source_indent = $tabsize; 22623705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 22633705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 22643705ce5bSJoe Perches 22653705ce5bSJoe Perches #convert leading spaces to tabs 22663705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 22673705ce5bSJoe Perches #Remove spaces before a tab 22683705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 22693705ce5bSJoe Perches 22703705ce5bSJoe Perches return "$leading"; 22713705ce5bSJoe Perches} 22723705ce5bSJoe Perches 2273d1fe9c09SJoe Perchessub pos_last_openparen { 2274d1fe9c09SJoe Perches my ($line) = @_; 2275d1fe9c09SJoe Perches 2276d1fe9c09SJoe Perches my $pos = 0; 2277d1fe9c09SJoe Perches 2278d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2279d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2280d1fe9c09SJoe Perches 2281d1fe9c09SJoe Perches my $last_openparen = 0; 2282d1fe9c09SJoe Perches 2283d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2284d1fe9c09SJoe Perches return -1; 2285d1fe9c09SJoe Perches } 2286d1fe9c09SJoe Perches 2287d1fe9c09SJoe Perches my $len = length($line); 2288d1fe9c09SJoe Perches 2289d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2290d1fe9c09SJoe Perches my $string = substr($line, $pos); 2291d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2292d1fe9c09SJoe Perches $pos += length($1) - 1; 2293d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2294d1fe9c09SJoe Perches $last_openparen = $pos; 2295d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2296d1fe9c09SJoe Perches last; 2297d1fe9c09SJoe Perches } 2298d1fe9c09SJoe Perches } 2299d1fe9c09SJoe Perches 230091cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2301d1fe9c09SJoe Perches} 2302d1fe9c09SJoe Perches 2303f36d3eb8SJoe Perchessub get_raw_comment { 2304f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2305f36d3eb8SJoe Perches my $comment = ''; 2306f36d3eb8SJoe Perches 2307f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2308f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2309f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2310f36d3eb8SJoe Perches } 2311f36d3eb8SJoe Perches } 2312f36d3eb8SJoe Perches 2313f36d3eb8SJoe Perches return $comment; 2314f36d3eb8SJoe Perches} 2315f36d3eb8SJoe Perches 23160a920b5bSAndy Whitcroftsub process { 23170a920b5bSAndy Whitcroft my $filename = shift; 23180a920b5bSAndy Whitcroft 23190a920b5bSAndy Whitcroft my $linenr=0; 23200a920b5bSAndy Whitcroft my $prevline=""; 2321c2fdda0dSAndy Whitcroft my $prevrawline=""; 23220a920b5bSAndy Whitcroft my $stashline=""; 2323c2fdda0dSAndy Whitcroft my $stashrawline=""; 23240a920b5bSAndy Whitcroft 23254a0df2efSAndy Whitcroft my $length; 23260a920b5bSAndy Whitcroft my $indent; 23270a920b5bSAndy Whitcroft my $previndent=0; 23280a920b5bSAndy Whitcroft my $stashindent=0; 23290a920b5bSAndy Whitcroft 2330de7d4f0eSAndy Whitcroft our $clean = 1; 23310a920b5bSAndy Whitcroft my $signoff = 0; 2332cd261496SGeert Uytterhoeven my $author = ''; 2333cd261496SGeert Uytterhoeven my $authorsignoff = 0; 23340a920b5bSAndy Whitcroft my $is_patch = 0; 2335133712a2SRob Herring my $is_binding_patch = -1; 233629ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 233715662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 233844d303ebSJoe Perches my $has_patch_separator = 0; #Found a --- line 2339ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2340490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2341bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 23422a076f40SJoe Perches my $commit_log_long_line = 0; 2343e518e9a5SJoe Perches my $commit_log_has_diff = 0; 234413f1937eSJoe Perches my $reported_maintainer_file = 0; 2345fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2346fa64205dSPasi Savanainen 2347365dd4eaSJoe Perches my $last_blank_line = 0; 23485e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2349365dd4eaSJoe Perches 235013214adfSAndy Whitcroft our @report = (); 23516c72ffaaSAndy Whitcroft our $cnt_lines = 0; 23526c72ffaaSAndy Whitcroft our $cnt_error = 0; 23536c72ffaaSAndy Whitcroft our $cnt_warn = 0; 23546c72ffaaSAndy Whitcroft our $cnt_chk = 0; 23556c72ffaaSAndy Whitcroft 23560a920b5bSAndy Whitcroft # Trace the real file/line as we go. 23570a920b5bSAndy Whitcroft my $realfile = ''; 23580a920b5bSAndy Whitcroft my $realline = 0; 23590a920b5bSAndy Whitcroft my $realcnt = 0; 23600a920b5bSAndy Whitcroft my $here = ''; 236177cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 23620a920b5bSAndy Whitcroft my $in_comment = 0; 2363c2fdda0dSAndy Whitcroft my $comment_edge = 0; 23640a920b5bSAndy Whitcroft my $first_line = 0; 23651e855726SWolfram Sang my $p1_prefix = ''; 23660a920b5bSAndy Whitcroft 236713214adfSAndy Whitcroft my $prev_values = 'E'; 236813214adfSAndy Whitcroft 236913214adfSAndy Whitcroft # suppression flags 2370773647a0SAndy Whitcroft my %suppress_ifbraces; 2371170d3a22SAndy Whitcroft my %suppress_whiletrailers; 23722b474a1aSAndy Whitcroft my %suppress_export; 23733e469cdcSAndy Whitcroft my $suppress_statement = 0; 2374653d4876SAndy Whitcroft 23757e51f197SJoe Perches my %signatures = (); 2376323c1260SJoe Perches 2377c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2378de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2379c2fdda0dSAndy Whitcroft # 2380de7d4f0eSAndy Whitcroft my @setup_docs = (); 2381de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2382773647a0SAndy Whitcroft 2383d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2384d8b07710SJoe Perches 23859f3a8992SRob Herring my $checklicenseline = 1; 23869f3a8992SRob Herring 2387773647a0SAndy Whitcroft sanitise_line_reset(); 2388c2fdda0dSAndy Whitcroft my $line; 2389c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2390773647a0SAndy Whitcroft $linenr++; 2391773647a0SAndy Whitcroft $line = $rawline; 2392c2fdda0dSAndy Whitcroft 23933705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 23943705ce5bSJoe Perches 2395773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2396de7d4f0eSAndy Whitcroft $setup_docs = 0; 23978c27ceffSMauro Carvalho Chehab if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { 2398de7d4f0eSAndy Whitcroft $setup_docs = 1; 2399de7d4f0eSAndy Whitcroft } 2400773647a0SAndy Whitcroft #next; 2401de7d4f0eSAndy Whitcroft } 240274fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2403773647a0SAndy Whitcroft $realline=$1-1; 2404773647a0SAndy Whitcroft if (defined $2) { 2405773647a0SAndy Whitcroft $realcnt=$3+1; 2406773647a0SAndy Whitcroft } else { 2407773647a0SAndy Whitcroft $realcnt=1+1; 2408773647a0SAndy Whitcroft } 2409c45dcabdSAndy Whitcroft $in_comment = 0; 2410773647a0SAndy Whitcroft 2411773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2412773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2413773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2414773647a0SAndy Whitcroft # at context start. 2415773647a0SAndy Whitcroft my $edge; 241601fa9147SAndy Whitcroft my $cnt = $realcnt; 241701fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 241801fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 241901fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 242001fa9147SAndy Whitcroft $cnt--; 242101fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2422721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2423fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2424fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2425fae17daeSAndy Whitcroft ($edge) = $1; 2426fae17daeSAndy Whitcroft last; 2427fae17daeSAndy Whitcroft } 2428773647a0SAndy Whitcroft } 2429773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2430773647a0SAndy Whitcroft $in_comment = 1; 2431773647a0SAndy Whitcroft } 2432773647a0SAndy Whitcroft 2433773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2434773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2435773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2436773647a0SAndy Whitcroft if (!defined $edge && 243783242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2438773647a0SAndy Whitcroft { 2439773647a0SAndy Whitcroft $in_comment = 1; 2440773647a0SAndy Whitcroft } 2441773647a0SAndy Whitcroft 2442773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2443773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2444773647a0SAndy Whitcroft 2445171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2446773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2447171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2448773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2449773647a0SAndy Whitcroft } 2450773647a0SAndy Whitcroft push(@lines, $line); 2451773647a0SAndy Whitcroft 2452773647a0SAndy Whitcroft if ($realcnt > 1) { 2453773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2454773647a0SAndy Whitcroft } else { 2455773647a0SAndy Whitcroft $realcnt = 0; 2456773647a0SAndy Whitcroft } 2457773647a0SAndy Whitcroft 2458773647a0SAndy Whitcroft #print "==>$rawline\n"; 2459773647a0SAndy Whitcroft #print "-->$line\n"; 2460de7d4f0eSAndy Whitcroft 2461de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2462de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2463de7d4f0eSAndy Whitcroft } 2464de7d4f0eSAndy Whitcroft } 2465de7d4f0eSAndy Whitcroft 24666c72ffaaSAndy Whitcroft $prefix = ''; 24676c72ffaaSAndy Whitcroft 2468773647a0SAndy Whitcroft $realcnt = 0; 2469773647a0SAndy Whitcroft $linenr = 0; 2470194f66fcSJoe Perches $fixlinenr = -1; 24710a920b5bSAndy Whitcroft foreach my $line (@lines) { 24720a920b5bSAndy Whitcroft $linenr++; 2473194f66fcSJoe Perches $fixlinenr++; 24741b5539b1SJoe Perches my $sline = $line; #copy of $line 24751b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 24760a920b5bSAndy Whitcroft 2477c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2478f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 24796c72ffaaSAndy Whitcroft 248012c253abSJoe Perches# check if it's a mode change, rename or start of a patch 248112c253abSJoe Perches if (!$in_commit_log && 248212c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 248312c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 248412c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 248512c253abSJoe Perches $is_patch = 1; 248612c253abSJoe Perches } 248712c253abSJoe Perches 24880a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2489e518e9a5SJoe Perches if (!$in_commit_log && 249074fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 249174fd4f34SJoe Perches my $context = $4; 24920a920b5bSAndy Whitcroft $is_patch = 1; 24934a0df2efSAndy Whitcroft $first_line = $linenr + 1; 24940a920b5bSAndy Whitcroft $realline=$1-1; 24950a920b5bSAndy Whitcroft if (defined $2) { 24960a920b5bSAndy Whitcroft $realcnt=$3+1; 24970a920b5bSAndy Whitcroft } else { 24980a920b5bSAndy Whitcroft $realcnt=1+1; 24990a920b5bSAndy Whitcroft } 2500c2fdda0dSAndy Whitcroft annotate_reset(); 250113214adfSAndy Whitcroft $prev_values = 'E'; 250213214adfSAndy Whitcroft 2503773647a0SAndy Whitcroft %suppress_ifbraces = (); 2504170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 25052b474a1aSAndy Whitcroft %suppress_export = (); 25063e469cdcSAndy Whitcroft $suppress_statement = 0; 250774fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 250874fd4f34SJoe Perches $context_function = $1; 250974fd4f34SJoe Perches } else { 251074fd4f34SJoe Perches undef $context_function; 251174fd4f34SJoe Perches } 25120a920b5bSAndy Whitcroft next; 25130a920b5bSAndy Whitcroft 25144a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 25154a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 25164a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2517773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 25180a920b5bSAndy Whitcroft $realline++; 2519d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 25200a920b5bSAndy Whitcroft 25214a0df2efSAndy Whitcroft # Measure the line length and indent. 2522c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 25230a920b5bSAndy Whitcroft 25240a920b5bSAndy Whitcroft # Track the previous line. 25250a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 25260a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2527c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2528c2fdda0dSAndy Whitcroft 2529773647a0SAndy Whitcroft #warn "line<$line>\n"; 25306c72ffaaSAndy Whitcroft 2531d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2532d8aaf121SAndy Whitcroft $realcnt--; 25330a920b5bSAndy Whitcroft } 25340a920b5bSAndy Whitcroft 2535cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2536cc77cdcaSAndy Whitcroft 25376c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 25386c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2539773647a0SAndy Whitcroft 25402ac73b4fSJoe Perches my $found_file = 0; 2541773647a0SAndy Whitcroft # extract the filename as it passes 25423bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 25433bf9a009SRabin Vincent $realfile = $1; 25442b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2545270c49a0SJoe Perches $in_commit_log = 0; 25462ac73b4fSJoe Perches $found_file = 1; 25473bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2548773647a0SAndy Whitcroft $realfile = $1; 25492b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2550270c49a0SJoe Perches $in_commit_log = 0; 25511e855726SWolfram Sang 25521e855726SWolfram Sang $p1_prefix = $1; 2553e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2554e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2555000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2556000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 25571e855726SWolfram Sang } 2558773647a0SAndy Whitcroft 2559c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2560000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2561000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2562773647a0SAndy Whitcroft } 25632ac73b4fSJoe Perches $found_file = 1; 25642ac73b4fSJoe Perches } 25652ac73b4fSJoe Perches 256634d8815fSJoe Perches#make up the handle for any error we report on this line 256734d8815fSJoe Perches if ($showfile) { 256834d8815fSJoe Perches $prefix = "$realfile:$realline: " 256934d8815fSJoe Perches } elsif ($emacs) { 25707d3a9f67SJoe Perches if ($file) { 25717d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 25727d3a9f67SJoe Perches } else { 257334d8815fSJoe Perches $prefix = "$filename:$linenr: "; 257434d8815fSJoe Perches } 25757d3a9f67SJoe Perches } 257634d8815fSJoe Perches 25772ac73b4fSJoe Perches if ($found_file) { 257885b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 257985b0ee18SJoe Perches WARN("OBSOLETE", 258085b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 258185b0ee18SJoe Perches } 25827bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 25832ac73b4fSJoe Perches $check = 1; 25842ac73b4fSJoe Perches } else { 25852ac73b4fSJoe Perches $check = $check_orig; 25862ac73b4fSJoe Perches } 25879f3a8992SRob Herring $checklicenseline = 1; 2588133712a2SRob Herring 2589133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2590133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2591133712a2SRob Herring 2592133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2593133712a2SRob Herring 2594133712a2SRob Herring if (($last_binding_patch != -1) && 2595133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2596133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2597133712a2SRob Herring "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n"); 2598133712a2SRob Herring } 2599133712a2SRob Herring } 2600133712a2SRob Herring 2601773647a0SAndy Whitcroft next; 2602773647a0SAndy Whitcroft } 2603773647a0SAndy Whitcroft 2604389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 26050a920b5bSAndy Whitcroft 2606c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2607c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2608c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 26090a920b5bSAndy Whitcroft 26106c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 26116c72ffaaSAndy Whitcroft 2612490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2613490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2614490b292cSJoe Perches if ($in_commit_log) { 2615490b292cSJoe Perches if ($line !~ /^\s*$/) { 2616490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2617490b292cSJoe Perches } 2618490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2619490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2620490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2621490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2622490b292cSJoe Perches } 2623490b292cSJoe Perches 2624e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2625e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2626e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2627e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2628e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2629e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2630e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2631e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2632e518e9a5SJoe Perches $commit_log_has_diff = 1; 2633e518e9a5SJoe Perches } 2634e518e9a5SJoe Perches 26353bf9a009SRabin Vincent# Check for incorrect file permissions 26363bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 26373bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 263804db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 263904db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2640000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2641000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 26423bf9a009SRabin Vincent } 26433bf9a009SRabin Vincent } 26443bf9a009SRabin Vincent 2645cd261496SGeert Uytterhoeven# Check the patch for a From: 2646cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2647cd261496SGeert Uytterhoeven $author = $1; 2648cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2649cd261496SGeert Uytterhoeven $author =~ s/"//g; 2650dfa05c28SJoe Perches $author = reformat_email($author); 2651cd261496SGeert Uytterhoeven } 2652cd261496SGeert Uytterhoeven 265320112475SJoe Perches# Check the patch for a signoff: 2654dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 26554a0df2efSAndy Whitcroft $signoff++; 265615662b3eSJoe Perches $in_commit_log = 0; 2657cd261496SGeert Uytterhoeven if ($author ne '') { 2658dfa05c28SJoe Perches if (same_email_addresses($1, $author)) { 2659cd261496SGeert Uytterhoeven $authorsignoff = 1; 2660cd261496SGeert Uytterhoeven } 2661cd261496SGeert Uytterhoeven } 26620a920b5bSAndy Whitcroft } 266320112475SJoe Perches 266444d303ebSJoe Perches# Check for patch separator 266544d303ebSJoe Perches if ($line =~ /^---$/) { 266644d303ebSJoe Perches $has_patch_separator = 1; 266744d303ebSJoe Perches $in_commit_log = 0; 266844d303ebSJoe Perches } 266944d303ebSJoe Perches 2670e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2671e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2672e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2673e0d975b1SJoe Perches $reported_maintainer_file = 1; 2674e0d975b1SJoe Perches } 2675e0d975b1SJoe Perches 267620112475SJoe Perches# Check signature styles 2677270c49a0SJoe Perches if (!$in_header_lines && 2678ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 267920112475SJoe Perches my $space_before = $1; 268020112475SJoe Perches my $sign_off = $2; 268120112475SJoe Perches my $space_after = $3; 268220112475SJoe Perches my $email = $4; 268320112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 268420112475SJoe Perches 2685ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2686ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2687ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2688ce0338dfSJoe Perches } 268920112475SJoe Perches if (defined $space_before && $space_before ne "") { 26903705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26913705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 26923705ce5bSJoe Perches $fix) { 2693194f66fcSJoe Perches $fixed[$fixlinenr] = 26943705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26953705ce5bSJoe Perches } 269620112475SJoe Perches } 269720112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 26983705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26993705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 27003705ce5bSJoe Perches $fix) { 2701194f66fcSJoe Perches $fixed[$fixlinenr] = 27023705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27033705ce5bSJoe Perches } 27043705ce5bSJoe Perches 270520112475SJoe Perches } 270620112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 27073705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 27083705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 27093705ce5bSJoe Perches $fix) { 2710194f66fcSJoe Perches $fixed[$fixlinenr] = 27113705ce5bSJoe Perches "$ucfirst_sign_off $email"; 27123705ce5bSJoe Perches } 271320112475SJoe Perches } 271420112475SJoe Perches 2715dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 271620112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 271720112475SJoe Perches if ($suggested_email eq "") { 2718000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2719000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 272020112475SJoe Perches } else { 272120112475SJoe Perches my $dequoted = $suggested_email; 272220112475SJoe Perches $dequoted =~ s/^"//; 272320112475SJoe Perches $dequoted =~ s/" </ </; 272420112475SJoe Perches # Don't force email to have quotes 272520112475SJoe Perches # Allow just an angle bracketed address 2726dfa05c28SJoe Perches if (!same_email_addresses($email, $suggested_email)) { 2727000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2728000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 272920112475SJoe Perches } 27300a920b5bSAndy Whitcroft } 27317e51f197SJoe Perches 27327e51f197SJoe Perches# Check for duplicate signatures 27337e51f197SJoe Perches my $sig_nospace = $line; 27347e51f197SJoe Perches $sig_nospace =~ s/\s//g; 27357e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 27367e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 27377e51f197SJoe Perches WARN("BAD_SIGN_OFF", 27387e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 27397e51f197SJoe Perches } else { 27407e51f197SJoe Perches $signatures{$sig_nospace} = 1; 27417e51f197SJoe Perches } 27426c5d24eeSSean Christopherson 27436c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 27446c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 27456c5d24eeSSean Christopherson if ($email eq $author) { 27466c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27476c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 27486c5d24eeSSean Christopherson } 27496c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 27506c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27516c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 27526c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 27536c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27546c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27556c5d24eeSSean Christopherson } elsif ($1 ne $email) { 27566c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27576c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27586c5d24eeSSean Christopherson } 27596c5d24eeSSean Christopherson } 27600a920b5bSAndy Whitcroft } 27610a920b5bSAndy Whitcroft 2762a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2763a2fe16b9SJoe Perches if ($in_header_lines && 2764a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2765a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2766a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2767a2fe16b9SJoe Perches } 2768a2fe16b9SJoe Perches 276944d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context 277044d303ebSJoe Perches if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { 27717ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 277244d303ebSJoe Perches "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr); 27737ebd05efSChristopher Covington } 27747ebd05efSChristopher Covington 2775369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2776369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2777369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2778369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2779369c8dd3SJoe Perches # timestamp 2780634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 2781634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 2782634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 2783634cffccSJoe Perches # stack dump address styles 2784369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2785369c8dd3SJoe Perches } 2786369c8dd3SJoe Perches 27872a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 27882a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2789bf4daf12SJoe Perches length($line) > 75 && 2790bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2791bf4daf12SJoe Perches # file delta changes 2792bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2793bf4daf12SJoe Perches # filename then : 2794bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2795bf4daf12SJoe Perches # A Fixes: or Link: line 2796bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 27972a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 27982a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 27992a076f40SJoe Perches $commit_log_long_line = 1; 28002a076f40SJoe Perches } 28012a076f40SJoe Perches 2802bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2803bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2804bf4daf12SJoe Perches $line =~ /^\s*$/) { 2805bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2806bf4daf12SJoe Perches } 2807bf4daf12SJoe Perches 28080d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2809369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2810a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 2811e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2812fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2813aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2814369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2815bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2816fe043ea1SJoe Perches my $init_char = "c"; 2817fe043ea1SJoe Perches my $orig_commit = ""; 28180d7835fcSJoe Perches my $short = 1; 28190d7835fcSJoe Perches my $long = 0; 28200d7835fcSJoe Perches my $case = 1; 28210d7835fcSJoe Perches my $space = 1; 28220d7835fcSJoe Perches my $hasdesc = 0; 282319c146a6SJoe Perches my $hasparens = 0; 28240d7835fcSJoe Perches my $id = '0123456789ab'; 28250d7835fcSJoe Perches my $orig_desc = "commit description"; 28260d7835fcSJoe Perches my $description = ""; 28270d7835fcSJoe Perches 2828fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2829fe043ea1SJoe Perches $init_char = $1; 2830fe043ea1SJoe Perches $orig_commit = lc($2); 2831fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2832fe043ea1SJoe Perches $orig_commit = lc($1); 2833fe043ea1SJoe Perches } 2834fe043ea1SJoe Perches 28350d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 28360d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 28370d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 28380d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 28390d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 28400d7835fcSJoe Perches $orig_desc = $1; 284119c146a6SJoe Perches $hasparens = 1; 28420d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 28430d7835fcSJoe Perches defined $rawlines[$linenr] && 28440d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 28450d7835fcSJoe Perches $orig_desc = $1; 284619c146a6SJoe Perches $hasparens = 1; 2847b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2848b671fde0SJoe Perches defined $rawlines[$linenr] && 2849b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2850b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2851b671fde0SJoe Perches $orig_desc = $1; 2852b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2853b671fde0SJoe Perches $orig_desc .= " " . $1; 285419c146a6SJoe Perches $hasparens = 1; 28550d7835fcSJoe Perches } 28560d7835fcSJoe Perches 28570d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 28580d7835fcSJoe Perches $id, $orig_desc); 28590d7835fcSJoe Perches 2860948b133aSHeinrich Schuchardt if (defined($id) && 2861948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2862d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 28630d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 28640d7835fcSJoe Perches } 2865d311cd44SJoe Perches } 2866d311cd44SJoe Perches 286713f1937eSJoe Perches# Check for added, moved or deleted files 286813f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 286913f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 287013f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 287113f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 287213f1937eSJoe Perches (defined($1) || defined($2))))) { 2873a82603a8SAndrew Jeffery $is_patch = 1; 287413f1937eSJoe Perches $reported_maintainer_file = 1; 287513f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 287613f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 287713f1937eSJoe Perches } 287813f1937eSJoe Perches 2879e400edb1SRob Herring# Check for adding new DT bindings not in schema format 2880e400edb1SRob Herring if (!$in_commit_log && 2881e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 2882e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 2883e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 2884e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 2885e400edb1SRob Herring } 2886e400edb1SRob Herring 288700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 28888905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2889000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2890000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 28916c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2892de7d4f0eSAndy Whitcroft } 2893de7d4f0eSAndy Whitcroft 2894de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2895de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2896171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2897171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2898171ae1a4SAndy Whitcroft 2899171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2900171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2901171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2902171ae1a4SAndy Whitcroft 290334d99219SJoe Perches CHK("INVALID_UTF8", 2904000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 290500df344fSAndy Whitcroft } 29060a920b5bSAndy Whitcroft 290715662b3eSJoe Perches# Check if it's the start of a commit log 290815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 290915662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2910eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2911eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 291215662b3eSJoe Perches $in_header_lines = 0; 291315662b3eSJoe Perches $in_commit_log = 1; 2914ed43c4e5SAllen Hubbe $has_commit_log = 1; 291515662b3eSJoe Perches } 291615662b3eSJoe Perches 2917fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2918fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2919fa64205dSPasi Savanainen if ($in_header_lines && 2920fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2921fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2922fa64205dSPasi Savanainen $non_utf8_charset = 1; 2923fa64205dSPasi Savanainen } 2924fa64205dSPasi Savanainen 2925fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 292615662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2927fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 292815662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 292915662b3eSJoe Perches } 293015662b3eSJoe Perches 2931d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2932d6430f71SJoe Perches if ($tree && $in_commit_log) { 2933d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2934d6430f71SJoe Perches my $file = $1; 2935d6430f71SJoe Perches 2936d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2937d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2938d6430f71SJoe Perches # 2939d6430f71SJoe Perches } else { 2940d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2941d6430f71SJoe Perches } 2942d6430f71SJoe Perches } 2943d6430f71SJoe Perches } 2944d6430f71SJoe Perches 294566b47b4aSKees Cook# Check for various typo / spelling mistakes 294666d7a382SJoe Perches if (defined($misspellings) && 294766d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2948ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 294966b47b4aSKees Cook my $typo = $1; 295066b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 295166b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 295266b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 29530675a8fbSJean Delvare my $msg_level = \&WARN; 29540675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29550675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 295666b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 295766b47b4aSKees Cook $fix) { 295866b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 295966b47b4aSKees Cook } 296066b47b4aSKees Cook } 296166b47b4aSKees Cook } 296266b47b4aSKees Cook 2963a8dd86bfSMatteo Croce# check for invalid commit id 2964a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 2965a8dd86bfSMatteo Croce my $id; 2966a8dd86bfSMatteo Croce my $description; 2967a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 2968a8dd86bfSMatteo Croce if (!defined($id)) { 2969a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 2970a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 2971a8dd86bfSMatteo Croce } 2972a8dd86bfSMatteo Croce } 2973a8dd86bfSMatteo Croce 297430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 297530670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 297600df344fSAndy Whitcroft 29770a920b5bSAndy Whitcroft#trailing whitespace 29789c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2979c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2980d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2981d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2982d5e616fcSJoe Perches $fix) { 2983194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2984d5e616fcSJoe Perches } 2985c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2986c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29873705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 29883705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 29893705ce5bSJoe Perches $fix) { 2990194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 29913705ce5bSJoe Perches } 29923705ce5bSJoe Perches 2993d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 29940a920b5bSAndy Whitcroft } 29955368df20SAndy Whitcroft 29964783f894SJosh Triplett# Check for FSF mailing addresses. 2997109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 29981bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 29993e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 30003e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 30014783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 30020675a8fbSJean Delvare my $msg_level = \&ERROR; 30030675a8fbSJean Delvare $msg_level = \&CHK if ($file); 30040675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 30054783f894SJosh 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) 30064783f894SJosh Triplett } 30074783f894SJosh Triplett 30083354957aSAndi Kleen# check for Kconfig help text having a real description 30099fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 30109fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 30113354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 3012678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3013678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3014678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3015678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 30163354957aSAndi Kleen my $length = 0; 30179fe287d7SAndy Whitcroft my $cnt = $realcnt; 30189fe287d7SAndy Whitcroft my $ln = $linenr + 1; 30199fe287d7SAndy Whitcroft my $f; 3020a1385803SAndy Whitcroft my $is_start = 0; 30219fe287d7SAndy Whitcroft my $is_end = 0; 3022a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 30239fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 30249fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 30259fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 30269fe287d7SAndy Whitcroft 30279fe287d7SAndy Whitcroft next if ($f =~ /^-/); 30288d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3029a1385803SAndy Whitcroft 303086adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3031a1385803SAndy Whitcroft $is_start = 1; 303284af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 303384af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 303484af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 303584af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 303684af7a61SUlf Magnusson } 3037a1385803SAndy Whitcroft $length = -1; 3038a1385803SAndy Whitcroft } 3039a1385803SAndy Whitcroft 30409fe287d7SAndy Whitcroft $f =~ s/^.//; 30413354957aSAndi Kleen $f =~ s/#.*//; 30423354957aSAndi Kleen $f =~ s/^\s+//; 30433354957aSAndi Kleen next if ($f =~ /^$/); 3044678ae162SUlf Magnusson 3045678ae162SUlf Magnusson # This only checks context lines in the patch 3046678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3047678ae162SUlf Magnusson # positives, even though some of these are 3048678ae162SUlf Magnusson # common words in help texts 3049678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3050678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 30519fe287d7SAndy Whitcroft $is_end = 1; 30529fe287d7SAndy Whitcroft last; 30539fe287d7SAndy Whitcroft } 30543354957aSAndi Kleen $length++; 30553354957aSAndi Kleen } 305656193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3057000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 305856193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 305956193274SVadim Bendebury } 3060a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 30613354957aSAndi Kleen } 30623354957aSAndi Kleen 3063628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form 3064628f91a2SJoe Perches if ($realfile =~ /^MAINTAINERS$/ && 3065628f91a2SJoe Perches $rawline =~ /^\+[A-Z]:/ && 3066628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3067628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3068628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3069628f91a2SJoe Perches $fix) { 3070628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3071628f91a2SJoe Perches } 3072628f91a2SJoe Perches } 3073628f91a2SJoe Perches 3074327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3075327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3076327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3077327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3078327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3079327953e9SChristoph Jaeger } 3080327953e9SChristoph Jaeger 3081c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3082c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3083c68e5878SArnaud Lacombe my $flag = $1; 3084c68e5878SArnaud Lacombe my $replacement = { 3085c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3086c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3087c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3088c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3089c68e5878SArnaud Lacombe }; 3090c68e5878SArnaud Lacombe 3091c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3092c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3093c68e5878SArnaud Lacombe } 3094c68e5878SArnaud Lacombe 3095bff5da43SRob Herring# check for DT compatible documentation 30967dd05b38SFlorian Vaussard if (defined $root && 30977dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 30987dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 30997dd05b38SFlorian Vaussard 3100bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3101bff5da43SRob Herring 3102cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3103852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3104cc93319bSFlorian Vaussard 3105bff5da43SRob Herring foreach my $compat (@compats) { 3106bff5da43SRob Herring my $compat2 = $compat; 3107185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3108185d566bSRob Herring my $compat3 = $compat; 3109185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3110185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3111bff5da43SRob Herring if ( $? >> 8 ) { 3112bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3113bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3114bff5da43SRob Herring } 3115bff5da43SRob Herring 31164fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 31174fbf32a6SFlorian Vaussard my $vendor = $1; 3118852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3119bff5da43SRob Herring if ( $? >> 8 ) { 3120bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3121cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3122bff5da43SRob Herring } 3123bff5da43SRob Herring } 3124bff5da43SRob Herring } 3125bff5da43SRob Herring 31269f3a8992SRob Herring# check for using SPDX license tag at beginning of files 31279f3a8992SRob Herring if ($realline == $checklicenseline) { 31289f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 31299f3a8992SRob Herring $checklicenseline = 2; 31309f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 31319f3a8992SRob Herring my $comment = ""; 31329f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 31339f3a8992SRob Herring $comment = '/*'; 31349f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 31359f3a8992SRob Herring $comment = '//'; 3136c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 31379f3a8992SRob Herring $comment = '#'; 31389f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 31399f3a8992SRob Herring $comment = '..'; 31409f3a8992SRob Herring } 31419f3a8992SRob Herring 3142fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3143fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3144fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3145ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3146fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3147fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3148fdf13693SJoe Perches } 3149fdf13693SJoe Perches 31509f3a8992SRob Herring if ($comment !~ /^$/ && 3151ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 31529f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 31539f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 31543b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 31553b6e8ac9SJoe Perches my $spdx_license = $1; 31563b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 31573b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 31583b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 31593b6e8ac9SJoe Perches } 316050c92900SLubomir Rintel if ($realfile =~ m@^Documentation/devicetree/bindings/@ && 316150c92900SLubomir Rintel not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { 316250c92900SLubomir Rintel my $msg_level = \&WARN; 316350c92900SLubomir Rintel $msg_level = \&CHK if ($file); 316450c92900SLubomir Rintel if (&{$msg_level}("SPDX_LICENSE_TAG", 316550c92900SLubomir Rintel 316650c92900SLubomir Rintel "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && 316750c92900SLubomir Rintel $fix) { 316850c92900SLubomir Rintel $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; 316950c92900SLubomir Rintel } 317050c92900SLubomir Rintel } 31719f3a8992SRob Herring } 31729f3a8992SRob Herring } 31739f3a8992SRob Herring } 31749f3a8992SRob Herring 31755368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3176d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 31775368df20SAndy Whitcroft 3178a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3179a8da38a9SJoe Perches if ($realline != $checklicenseline && 3180a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3181a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3182a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3183a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3184a8da38a9SJoe Perches } 3185a8da38a9SJoe Perches 318647e0c88bSJoe Perches# line length limit (with some exclusions) 318747e0c88bSJoe Perches# 318847e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 318947e0c88bSJoe Perches# logging functions like pr_info that end in a string 319047e0c88bSJoe Perches# lines with a single string 319147e0c88bSJoe Perches# #defines that are a single string 31922e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 319347e0c88bSJoe Perches# 319447e0c88bSJoe Perches# There are 3 different line length message types: 3195ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 319647e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 319747e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 319847e0c88bSJoe Perches# 319947e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 320047e0c88bSJoe Perches# 320147e0c88bSJoe Perches 3202b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 320347e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 320447e0c88bSJoe Perches 320547e0c88bSJoe Perches # Check the allowed long line types first 320647e0c88bSJoe Perches 320747e0c88bSJoe Perches # logging functions that end in a string that starts 320847e0c88bSJoe Perches # before $max_line_length 320947e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 321047e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 321147e0c88bSJoe Perches $msg_type = ""; 321247e0c88bSJoe Perches 321347e0c88bSJoe Perches # lines with only strings (w/ possible termination) 321447e0c88bSJoe Perches # #defines with only strings 321547e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 321647e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 321747e0c88bSJoe Perches $msg_type = ""; 321847e0c88bSJoe Perches 3219cc147506SJoe Perches # More special cases 3220cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3221cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3222d560a5f8SJoe Perches $msg_type = ""; 3223d560a5f8SJoe Perches 32242e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 32252e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 32262e4bbbc5SAndreas Brauchli $msg_type = ""; 32272e4bbbc5SAndreas Brauchli 322847e0c88bSJoe Perches # Otherwise set the alternate message types 322947e0c88bSJoe Perches 323047e0c88bSJoe Perches # a comment starts before $max_line_length 323147e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 323247e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 323347e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 323447e0c88bSJoe Perches 323547e0c88bSJoe Perches # a quoted string starts before $max_line_length 323647e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 323747e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 323847e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 323947e0c88bSJoe Perches } 324047e0c88bSJoe Perches 324147e0c88bSJoe Perches if ($msg_type ne "" && 324247e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 324347e0c88bSJoe Perches WARN($msg_type, 32446cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 32450a920b5bSAndy Whitcroft } 324647e0c88bSJoe Perches } 32470a920b5bSAndy Whitcroft 32488905a67cSAndy Whitcroft# check for adding lines without a newline. 32498905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3250000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3251000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 32528905a67cSAndy Whitcroft } 32538905a67cSAndy Whitcroft 3254b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3255de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 32560a920b5bSAndy Whitcroft 32570a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3258713a09deSAntonio Borneo# more than $tabsize must use tabs. 3259c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3260c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3261c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3262d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 32633705ce5bSJoe Perches if (ERROR("CODE_INDENT", 32643705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 32653705ce5bSJoe Perches $fix) { 3266194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 32673705ce5bSJoe Perches } 32680a920b5bSAndy Whitcroft } 32690a920b5bSAndy Whitcroft 327008e44365SAlberto Panizzo# check for space before tabs. 327108e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 327208e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32733705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 32743705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 32753705ce5bSJoe Perches $fix) { 3276194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3277713a09deSAntonio Borneo s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} 3278194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3279c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 32803705ce5bSJoe Perches } 328108e44365SAlberto Panizzo } 328208e44365SAlberto Panizzo 32836a487211SJoe Perches# check for assignments on the start of a line 32846a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 32856a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 32866a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 32876a487211SJoe Perches } 32886a487211SJoe Perches 3289d1fe9c09SJoe Perches# check for && or || at the start of a line 3290d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3291d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3292d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3293d1fe9c09SJoe Perches } 3294d1fe9c09SJoe Perches 3295a91e8994SJoe Perches# check indentation starts on a tab stop 32965b57980dSJoe Perches if ($perl_version_ok && 3297bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3298a91e8994SJoe Perches my $indent = length($1); 3299713a09deSAntonio Borneo if ($indent % $tabsize) { 3300a91e8994SJoe Perches if (WARN("TABSTOP", 3301a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3302a91e8994SJoe Perches $fix) { 3303713a09deSAntonio Borneo $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; 3304a91e8994SJoe Perches } 3305a91e8994SJoe Perches } 3306a91e8994SJoe Perches } 3307a91e8994SJoe Perches 3308d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 33095b57980dSJoe Perches if ($perl_version_ok && 3310fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3311d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3312d1fe9c09SJoe Perches my $oldindent = $1; 3313d1fe9c09SJoe Perches my $rest = $2; 3314d1fe9c09SJoe Perches 3315d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3316d1fe9c09SJoe Perches if ($pos >= 0) { 3317b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3318b34a26f3SJoe Perches my $newindent = $2; 3319d1fe9c09SJoe Perches 3320d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3321713a09deSAntonio Borneo "\t" x ($pos / $tabsize) . 3322713a09deSAntonio Borneo " " x ($pos % $tabsize); 3323d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3324d1fe9c09SJoe Perches 3325d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3326d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 33273705ce5bSJoe Perches 33283705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 33293705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 33303705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3331194f66fcSJoe Perches $fixed[$fixlinenr] =~ 33323705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 33333705ce5bSJoe Perches } 3334d1fe9c09SJoe Perches } 3335d1fe9c09SJoe Perches } 3336d1fe9c09SJoe Perches } 3337d1fe9c09SJoe Perches 33386ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 33396ab3a970SJoe Perches# avoid checking a few false positives: 33406ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 33416ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 33426ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 33436ab3a970SJoe Perches# multiline macros that define functions 33446ab3a970SJoe Perches# known attributes or the __attribute__ keyword 33456ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 33466ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 33473705ce5bSJoe Perches if (CHK("SPACING", 3348f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 33493705ce5bSJoe Perches $fix) { 3350194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3351f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 33523705ce5bSJoe Perches } 3353aad4f614SJoe Perches } 3354aad4f614SJoe Perches 335586406b1cSJoe Perches# Block comment styles 335686406b1cSJoe Perches# Networking with an initial /* 335705880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3358fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 335985ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 336085ad978cSJoe Perches $realline > 2) { 336105880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 336205880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 336305880600SJoe Perches } 336405880600SJoe Perches 336586406b1cSJoe Perches# Block comments use * on subsequent lines 336686406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 336786406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3368a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 336961135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3370a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 337186406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 337286406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3373a605e32eSJoe Perches } 3374a605e32eSJoe Perches 337586406b1cSJoe Perches# Block comments use */ on trailing lines 337686406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3377c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3378c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3379c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 338086406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 338186406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 338205880600SJoe Perches } 338305880600SJoe Perches 338408eb9b80SJoe Perches# Block comment * alignment 338508eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3386af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3387af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3388af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 338908eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3390af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3391af207524SJoe Perches my $oldindent; 339208eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3393af207524SJoe Perches if (defined($1)) { 3394af207524SJoe Perches $oldindent = expand_tabs($1); 3395af207524SJoe Perches } else { 3396af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3397af207524SJoe Perches $oldindent = expand_tabs($1); 3398af207524SJoe Perches } 339908eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 340008eb9b80SJoe Perches my $newindent = $1; 340108eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3402af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 340308eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 340408eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 340508eb9b80SJoe Perches } 340608eb9b80SJoe Perches } 340708eb9b80SJoe Perches 34087f619191SJoe Perches# check for missing blank lines after struct/union declarations 34097f619191SJoe Perches# with exceptions for various attributes and macros 34107f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 34117f619191SJoe Perches $line =~ /^\+/ && 34127f619191SJoe Perches !($line =~ /^\+\s*$/ || 34137f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 34147f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 34157f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 34167f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 34177f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 34187f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 34190bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 34207f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3421d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3422d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3423d752fcc8SJoe Perches $fix) { 3424f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3425d752fcc8SJoe Perches } 34267f619191SJoe Perches } 34277f619191SJoe Perches 3428365dd4eaSJoe Perches# check for multiple consecutive blank lines 3429365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3430365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3431365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3432d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3433d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3434d752fcc8SJoe Perches $fix) { 3435f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3436d752fcc8SJoe Perches } 3437d752fcc8SJoe Perches 3438365dd4eaSJoe Perches $last_blank_line = $linenr; 3439365dd4eaSJoe Perches } 3440365dd4eaSJoe Perches 34413b617e3bSJoe Perches# check for missing blank lines after declarations 34423f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 34433f7bac03SJoe Perches # actual declarations 34443f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34455a4e1fd3SJoe Perches # function pointer declarations 34465a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34473f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34483f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34493f7bac03SJoe Perches # known declaration macros 34503f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 34513f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 34523f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 34533f7bac03SJoe Perches # other possible extensions of declaration lines 34543f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 34553f7bac03SJoe Perches # not starting a section or a macro "\" extended line 34563f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 34573f7bac03SJoe Perches # looks like a declaration 34583f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34595a4e1fd3SJoe Perches # function pointer declarations 34605a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34613f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34623f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34633f7bac03SJoe Perches # known declaration macros 34643f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 34653f7bac03SJoe Perches # start of struct or union or enum 3466328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 34673f7bac03SJoe Perches # start or end of block or continuation of declaration 34683f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 34693f7bac03SJoe Perches # bitfield continuation 34703f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 34713f7bac03SJoe Perches # other possible extensions of declaration lines 34723f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 34733f7bac03SJoe Perches # indentation of previous and current line are the same 34743f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3475d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3476d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3477d752fcc8SJoe Perches $fix) { 3478f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3479d752fcc8SJoe Perches } 34803b617e3bSJoe Perches } 34813b617e3bSJoe Perches 34825f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 34836b4c5bebSAndy Whitcroft# Exceptions: 34846b4c5bebSAndy Whitcroft# 1) within comments 34856b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 34866b4c5bebSAndy Whitcroft# 3) hanging labels 34873705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 34885f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 34893705ce5bSJoe Perches if (WARN("LEADING_SPACE", 34903705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 34913705ce5bSJoe Perches $fix) { 3492194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 34933705ce5bSJoe Perches } 34945f7ddae6SRaffaele Recalcati } 34955f7ddae6SRaffaele Recalcati 3496b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3497b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3498b9ea10d6SAndy Whitcroft 34995751a24eSJoe Perches# check for unusual line ending [ or ( 35005751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 35015751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 35025751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 35035751a24eSJoe Perches } 35045751a24eSJoe Perches 35054dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 35064dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 35074dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 35084dbed76fSJoe Perches $context_function = $1; 35094dbed76fSJoe Perches } 35104dbed76fSJoe Perches 35114dbed76fSJoe Perches# check if this appears to be the end of function declaration 35124dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 35134dbed76fSJoe Perches undef $context_function; 35144dbed76fSJoe Perches } 35154dbed76fSJoe Perches 3516032a4c0fSJoe Perches# check indentation of any line with a bare else 3517840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3518032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3519032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3520032a4c0fSJoe Perches my $tabs = length($1) + 1; 3521840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3522840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3523840080a0SJoe Perches defined $lines[$linenr] && 3524840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3525032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3526032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3527032a4c0fSJoe Perches } 3528032a4c0fSJoe Perches } 3529032a4c0fSJoe Perches 3530c00df19aSJoe Perches# check indentation of a line with a break; 3531c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3532c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3533c00df19aSJoe Perches my $tabs = $1; 3534c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3535c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3536c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3537c00df19aSJoe Perches } 3538c00df19aSJoe Perches } 3539c00df19aSJoe Perches 3540c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3541cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3542000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3543000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3544c2fdda0dSAndy Whitcroft } 354522f2a2efSAndy Whitcroft 354656e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 354756e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 354856e77d70SJoe Perches WARN("HOTPLUG_SECTION", 354956e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 355056e77d70SJoe Perches } 355156e77d70SJoe Perches 35529c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 35532b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 35542b474a1aSAndy Whitcroft $realline_next); 35553e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3556ca819864SJoe Perches if ($linenr > $suppress_statement && 35571b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3558170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3559f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3560171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3561171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3562171ae1a4SAndy Whitcroft 35633e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 35643e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 35653e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 35663e469cdcSAndy Whitcroft # until we hit end of it. 35673e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 35683e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 35693e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 35703e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 35713e469cdcSAndy Whitcroft } 3572f74bd194SAndy Whitcroft 35732b474a1aSAndy Whitcroft # Find the real next line. 35742b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 35752b474a1aSAndy Whitcroft if (defined $realline_next && 35762b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 35772b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 35782b474a1aSAndy Whitcroft $realline_next++; 35792b474a1aSAndy Whitcroft } 35802b474a1aSAndy Whitcroft 3581171ae1a4SAndy Whitcroft my $s = $stat; 3582171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3583cf655043SAndy Whitcroft 3584c2fdda0dSAndy Whitcroft # Ignore goto labels. 3585171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3586c2fdda0dSAndy Whitcroft 3587c2fdda0dSAndy Whitcroft # Ignore functions being called 3588171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3589c2fdda0dSAndy Whitcroft 3590463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3591463f2864SAndy Whitcroft 3592c45dcabdSAndy Whitcroft # declarations always start with types 3593d2506586SAndy 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) { 3594c45dcabdSAndy Whitcroft my $type = $1; 3595c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3596c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3597c45dcabdSAndy Whitcroft 35986c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3599a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3600c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3601c2fdda0dSAndy Whitcroft } 36028905a67cSAndy Whitcroft 36036c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 360465863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3605c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 36069c0ca6f9SAndy Whitcroft } 36078905a67cSAndy Whitcroft 36088905a67cSAndy Whitcroft # Check for any sort of function declaration. 36098905a67cSAndy Whitcroft # int foo(something bar, other baz); 36108905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3611171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 36128905a67cSAndy Whitcroft my ($name_len) = length($1); 36138905a67cSAndy Whitcroft 3614cf655043SAndy Whitcroft my $ctx = $s; 3615773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 36168905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3617cf655043SAndy Whitcroft 36188905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3619c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 36208905a67cSAndy Whitcroft 3621c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 36228905a67cSAndy Whitcroft } 36238905a67cSAndy Whitcroft } 36248905a67cSAndy Whitcroft } 36258905a67cSAndy Whitcroft 36269c0ca6f9SAndy Whitcroft } 36279c0ca6f9SAndy Whitcroft 362800df344fSAndy Whitcroft# 362900df344fSAndy Whitcroft# Checks which may be anchored in the context. 363000df344fSAndy Whitcroft# 363100df344fSAndy Whitcroft 363200df344fSAndy Whitcroft# Check for switch () and associated case and default 363300df344fSAndy Whitcroft# statements should be at the same indent. 363400df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 363500df344fSAndy Whitcroft my $err = ''; 363600df344fSAndy Whitcroft my $sep = ''; 363700df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 363800df344fSAndy Whitcroft shift(@ctx); 363900df344fSAndy Whitcroft for my $ctx (@ctx) { 364000df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 364100df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 364200df344fSAndy Whitcroft $indent != $cindent) { 364300df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 364400df344fSAndy Whitcroft $sep = ''; 364500df344fSAndy Whitcroft } else { 364600df344fSAndy Whitcroft $sep = "[...]\n"; 364700df344fSAndy Whitcroft } 364800df344fSAndy Whitcroft } 364900df344fSAndy Whitcroft if ($err ne '') { 3650000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3651000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3652de7d4f0eSAndy Whitcroft } 3653de7d4f0eSAndy Whitcroft } 3654de7d4f0eSAndy Whitcroft 3655de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3656de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 36570fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3658773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3659773647a0SAndy Whitcroft 36609c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 36618eef05ddSJoe Perches 36628eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 36638eef05ddSJoe Perches WARN("DEEP_INDENTATION", 36648eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 36658eef05ddSJoe Perches } 36668eef05ddSJoe Perches 3667de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3668de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3669de7d4f0eSAndy Whitcroft 3670548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3671548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3672de7d4f0eSAndy Whitcroft 3673548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3674548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3675548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3676548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3677548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3678773647a0SAndy Whitcroft $ctx_ln++; 3679773647a0SAndy Whitcroft } 3680548596d5SAndy Whitcroft 368153210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 368253210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3683773647a0SAndy Whitcroft 3684773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3685000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3686000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 368701464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 368800df344fSAndy Whitcroft } 3689773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3690773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3691773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3692773647a0SAndy Whitcroft { 36939c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 36949c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3695000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3696000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 369701464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 36989c0ca6f9SAndy Whitcroft } 36999c0ca6f9SAndy Whitcroft } 370000df344fSAndy Whitcroft } 370100df344fSAndy Whitcroft 37024d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3703f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 37043e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 37053e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 37063e469cdcSAndy Whitcroft if (!defined $stat); 37074d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 37084d001e4dSAndy Whitcroft 37094d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 37104d001e4dSAndy Whitcroft 37119f5af480SJoe Perches # remove inline comments 37129f5af480SJoe Perches $s =~ s/$;/ /g; 37139f5af480SJoe Perches $c =~ s/$;/ /g; 37144d001e4dSAndy Whitcroft 37154d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 37166f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 37176f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 37184d001e4dSAndy Whitcroft 37199f5af480SJoe Perches # Make sure we remove the line prefixes as we have 37209f5af480SJoe Perches # none on the first line, and are going to readd them 37219f5af480SJoe Perches # where necessary. 37229f5af480SJoe Perches $s =~ s/\n./\n/gs; 37239f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 37249f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 37259f5af480SJoe Perches } 37269f5af480SJoe Perches 37274d001e4dSAndy Whitcroft # We want to check the first line inside the block 37284d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 37294d001e4dSAndy Whitcroft # 1) any blank line termination 37304d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 37314d001e4dSAndy Whitcroft # 3) any do (...) { 37324d001e4dSAndy Whitcroft my $continuation = 0; 37334d001e4dSAndy Whitcroft my $check = 0; 37344d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 37354d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 37364d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 37374d001e4dSAndy Whitcroft $continuation = 1; 37384d001e4dSAndy Whitcroft } 37399bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 37404d001e4dSAndy Whitcroft $check = 1; 37414d001e4dSAndy Whitcroft $cond_lines++; 37424d001e4dSAndy Whitcroft } 37434d001e4dSAndy Whitcroft 37444d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 37454d001e4dSAndy Whitcroft # preprocessor statement. 37464d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 37474d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 37484d001e4dSAndy Whitcroft $check = 0; 37494d001e4dSAndy Whitcroft } 37504d001e4dSAndy Whitcroft 37519bd49efeSAndy Whitcroft my $cond_ptr = -1; 3752740504c6SAndy Whitcroft $continuation = 0; 37539bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 37549bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 37554d001e4dSAndy Whitcroft 3756f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3757f16fa28fSAndy Whitcroft # is not linear. 3758f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3759f16fa28fSAndy Whitcroft $check = 0; 3760f16fa28fSAndy Whitcroft } 3761f16fa28fSAndy Whitcroft 37629bd49efeSAndy Whitcroft # Ignore: 37639bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 37649bd49efeSAndy Whitcroft # 2) preprocessor lines, and 37659bd49efeSAndy Whitcroft # 3) labels. 3766740504c6SAndy Whitcroft if ($continuation || 3767740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 37689bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 37699bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3770740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 377130dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 37729bd49efeSAndy Whitcroft $cond_lines++; 37739bd49efeSAndy Whitcroft } 37744d001e4dSAndy Whitcroft } 377530dad6ebSAndy Whitcroft } 37764d001e4dSAndy Whitcroft 37774d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 37784d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 37794d001e4dSAndy Whitcroft 37804d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 37814d001e4dSAndy Whitcroft # this is not this patch's fault. 37824d001e4dSAndy Whitcroft if (!defined($stat_real) || 37834d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 37844d001e4dSAndy Whitcroft $check = 0; 37854d001e4dSAndy Whitcroft } 37864d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 37874d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 37884d001e4dSAndy Whitcroft } 37894d001e4dSAndy Whitcroft 37909bd49efeSAndy 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"; 37914d001e4dSAndy Whitcroft 37929f5af480SJoe Perches if ($check && $s ne '' && 3793713a09deSAntonio Borneo (($sindent % $tabsize) != 0 || 37949f5af480SJoe Perches ($sindent < $indent) || 3795f6950a73SJoe Perches ($sindent == $indent && 3796f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 3797713a09deSAntonio Borneo ($sindent > $indent + $tabsize))) { 3798000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3799000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 38004d001e4dSAndy Whitcroft } 38014d001e4dSAndy Whitcroft } 38024d001e4dSAndy Whitcroft 38036c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 38046c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 38051f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 38061f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 38076c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3808c2fdda0dSAndy Whitcroft if ($dbg_values) { 3809c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3810cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3811cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 38121f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3813c2fdda0dSAndy Whitcroft } 38146c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 38156c72ffaaSAndy Whitcroft 381600df344fSAndy Whitcroft#ignore lines not being added 38173705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 381800df344fSAndy Whitcroft 381911ca40a0SJoe Perches# check for dereferences that span multiple lines 382011ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 382111ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 382211ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 382311ca40a0SJoe Perches my $ref = $1; 382411ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 382511ca40a0SJoe Perches $ref .= $1; 382611ca40a0SJoe Perches $ref =~ s/\s//g; 382711ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 382811ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 382911ca40a0SJoe Perches } 383011ca40a0SJoe Perches 3831a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3832c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3833a1ce18e4SJoe Perches my $type = $1; 3834a1ce18e4SJoe Perches my $var = $2; 3835207a8e84SJoe Perches $var = "" if (!defined $var); 3836207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3837a1ce18e4SJoe Perches my $sign = $1; 3838a1ce18e4SJoe Perches my $pointer = $2; 3839a1ce18e4SJoe Perches 3840a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3841a1ce18e4SJoe Perches 3842a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3843a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3844a1ce18e4SJoe Perches $fix) { 3845a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3846207a8e84SJoe Perches my $comp_pointer = $pointer; 3847207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3848207a8e84SJoe Perches $decl .= $comp_pointer; 3849207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3850207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3851a1ce18e4SJoe Perches } 3852a1ce18e4SJoe Perches } 3853a1ce18e4SJoe Perches } 3854a1ce18e4SJoe Perches 3855653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 38567429c690SAndy Whitcroft if ($dbg_type) { 38577429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3858000d1cc1SJoe Perches ERROR("TEST_TYPE", 3859000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 38607429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3861000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3862000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 38637429c690SAndy Whitcroft } 3864653d4876SAndy Whitcroft next; 3865653d4876SAndy Whitcroft } 3866a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3867a1ef277eSAndy Whitcroft if ($dbg_attr) { 38689360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3869000d1cc1SJoe Perches ERROR("TEST_ATTR", 3870000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 38719360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3872000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3873000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3874a1ef277eSAndy Whitcroft } 3875a1ef277eSAndy Whitcroft next; 3876a1ef277eSAndy Whitcroft } 3877653d4876SAndy Whitcroft 3878f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 387999423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 388099423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3881d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3882d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3883f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3884f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3885f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3886d752fcc8SJoe Perches my $fixedline = $prevrawline; 3887d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3888f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3889d752fcc8SJoe Perches $fixedline = $line; 38908d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3891f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3892d752fcc8SJoe Perches } 3893f0a594c1SAndy Whitcroft } 3894f0a594c1SAndy Whitcroft 389500df344fSAndy Whitcroft# 389600df344fSAndy Whitcroft# Checks which are anchored on the added line. 389700df344fSAndy Whitcroft# 389800df344fSAndy Whitcroft 3899653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3900c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3901653d4876SAndy Whitcroft my $path = $1; 3902653d4876SAndy Whitcroft if ($path =~ m{//}) { 3903000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3904495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3905495e9d84SJoe Perches } 3906495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3907495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3908495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3909653d4876SAndy Whitcroft } 3910653d4876SAndy Whitcroft } 3911653d4876SAndy Whitcroft 391200df344fSAndy Whitcroft# no C99 // comments 391300df344fSAndy Whitcroft if ($line =~ m{//}) { 39143705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 39153705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 39163705ce5bSJoe Perches $fix) { 3917194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 39183705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 39193705ce5bSJoe Perches my $comment = trim($1); 3920194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 39213705ce5bSJoe Perches } 39223705ce5bSJoe Perches } 392300df344fSAndy Whitcroft } 392400df344fSAndy Whitcroft # Remove C99 comments. 39250a920b5bSAndy Whitcroft $line =~ s@//.*@@; 39266c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 39270a920b5bSAndy Whitcroft 39282b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 39292b474a1aSAndy Whitcroft# the whole statement. 39302b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 39312b474a1aSAndy Whitcroft if (defined $realline_next && 39322b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 39332b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 39342b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 39352b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 39363cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 39373cbf62dfSAndy Whitcroft # a prefix: 39383cbf62dfSAndy Whitcroft # XXX(foo); 39393cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3940653d4876SAndy Whitcroft my $name = $1; 394187a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 39423cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 39433cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 39443cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 39453cbf62dfSAndy Whitcroft 39463cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 39472b474a1aSAndy Whitcroft \n.}\s*$| 394848012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 394948012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 395048012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 39512b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 39522b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 395348012058SAndy Whitcroft )/x) { 39542b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 39552b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 39562b474a1aSAndy Whitcroft } else { 39572b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 39580a920b5bSAndy Whitcroft } 39590a920b5bSAndy Whitcroft } 39602b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 39612b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 39622b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 39632b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 39642b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 39652b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 39662b474a1aSAndy Whitcroft } 39672b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 39682b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3969000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3970000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 39712b474a1aSAndy Whitcroft } 39720a920b5bSAndy Whitcroft 39735150bda4SJoe Eloff# check for global initialisers. 39746d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3975d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 39766d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3977d5e616fcSJoe Perches $fix) { 39786d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3979d5e616fcSJoe Perches } 3980f0a594c1SAndy Whitcroft } 39810a920b5bSAndy Whitcroft# check for static initialisers. 39826d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3983d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 39846d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3985d5e616fcSJoe Perches $herecurr) && 3986d5e616fcSJoe Perches $fix) { 39876d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3988d5e616fcSJoe Perches } 39890a920b5bSAndy Whitcroft } 39900a920b5bSAndy Whitcroft 39911813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 39921813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 39931813087dSJoe Perches my $tmp = trim($1); 39941813087dSJoe Perches WARN("MISORDERED_TYPE", 39951813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 39961813087dSJoe Perches } 39971813087dSJoe Perches 3998809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 3999809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 4000809e082eSJoe Perches my $type = trim($1); 4001809e082eSJoe Perches next if ($type !~ /\bint\b/); 4002809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 4003809e082eSJoe Perches my $new_type = $type; 4004809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 4005809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 4006809e082eSJoe Perches $new_type =~ s/^const\s+//; 4007809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 4008809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 4009809e082eSJoe Perches $new_type =~ s/\s+/ /g; 4010809e082eSJoe Perches $new_type = trim($new_type); 4011809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 4012809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 4013809e082eSJoe Perches $fix) { 4014809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 4015809e082eSJoe Perches } 4016809e082eSJoe Perches } 4017809e082eSJoe Perches 4018cb710ecaSJoe Perches# check for static const char * arrays. 4019cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 4020000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4021000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 4022cb710ecaSJoe Perches $herecurr); 4023cb710ecaSJoe Perches } 4024cb710ecaSJoe Perches 402577b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 402677b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 402777b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 402877b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 402977b8c0a8SJoe Perches $fix) { 403077b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 403177b8c0a8SJoe Perches } 403277b8c0a8SJoe Perches } 403377b8c0a8SJoe Perches 4034cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 4035cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4036000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4037000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4038cb710ecaSJoe Perches $herecurr); 4039cb710ecaSJoe Perches } 4040cb710ecaSJoe Perches 4041ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4042ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4043ab7e23f3SJoe Perches my $found = $1; 4044ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4045ab7e23f3SJoe Perches WARN("CONST_CONST", 4046ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4047ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4048ab7e23f3SJoe Perches WARN("CONST_CONST", 4049ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4050ab7e23f3SJoe Perches } 4051ab7e23f3SJoe Perches } 4052ab7e23f3SJoe Perches 40539b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 40549b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 40559b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 40569b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 40579b0fa60dSJoe Perches $herecurr); 40589b0fa60dSJoe Perches } 40599b0fa60dSJoe Perches 4060b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4061b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4062b598b670SJoe Perches my $array = $1; 4063b598b670SJoe 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*\))@) { 4064b598b670SJoe Perches my $array_div = $1; 4065b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4066b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4067b598b670SJoe Perches $fix) { 4068b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4069b598b670SJoe Perches } 4070b598b670SJoe Perches } 4071b598b670SJoe Perches } 4072b598b670SJoe Perches 4073b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 407416b7f3c8SJoe Perches if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { 4075b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4076b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4077b36190c5SJoe Perches $fix) { 4078194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4079b36190c5SJoe Perches } 4080b36190c5SJoe Perches } 4081b36190c5SJoe Perches 4082653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4083653d4876SAndy Whitcroft# make sense. 4084653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 40858054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4086c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 40878ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 408846d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4089000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4090000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 40910a920b5bSAndy Whitcroft } 40920a920b5bSAndy Whitcroft 40930a920b5bSAndy Whitcroft# * goes on variable not on type 409465863862SAndy Whitcroft # (char*[ const]) 4095bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4096bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 40973705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4098d8aaf121SAndy Whitcroft 409965863862SAndy Whitcroft # Should start with a space. 410065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 410165863862SAndy Whitcroft # Should not end with a space. 410265863862SAndy Whitcroft $to =~ s/\s+$//; 410365863862SAndy Whitcroft # '*'s should not have spaces between. 4104f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 410565863862SAndy Whitcroft } 4106d8aaf121SAndy Whitcroft 41073705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 410865863862SAndy Whitcroft if ($from ne $to) { 41093705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 41103705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 41113705ce5bSJoe Perches $fix) { 41123705ce5bSJoe Perches my $sub_from = $ident; 41133705ce5bSJoe Perches my $sub_to = $ident; 41143705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4115194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41163705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 41173705ce5bSJoe Perches } 411865863862SAndy Whitcroft } 4119bfcb2cc7SAndy Whitcroft } 4120bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4121bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 41223705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4123d8aaf121SAndy Whitcroft 412465863862SAndy Whitcroft # Should start with a space. 412565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 412665863862SAndy Whitcroft # Should not end with a space. 412765863862SAndy Whitcroft $to =~ s/\s+$//; 412865863862SAndy Whitcroft # '*'s should not have spaces between. 4129f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 413065863862SAndy Whitcroft } 413165863862SAndy Whitcroft # Modifiers should have spaces. 413265863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 413365863862SAndy Whitcroft 41343705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4135667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 41363705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 41373705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 41383705ce5bSJoe Perches $fix) { 41393705ce5bSJoe Perches 41403705ce5bSJoe Perches my $sub_from = $match; 41413705ce5bSJoe Perches my $sub_to = $match; 41423705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4143194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41443705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 41453705ce5bSJoe Perches } 414665863862SAndy Whitcroft } 41470a920b5bSAndy Whitcroft } 41480a920b5bSAndy Whitcroft 41499d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 41509d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 41510675a8fbSJean Delvare my $msg_level = \&WARN; 41520675a8fbSJean Delvare $msg_level = \&CHK if ($file); 41530675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 41549d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 41559d3e3c70SJoe Perches } 41560a920b5bSAndy Whitcroft 41579d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 41588905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4159000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4160000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 41618905a67cSAndy Whitcroft } 41628905a67cSAndy Whitcroft 416317441227SJoe Perches# check for uses of printk_ratelimit 416417441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4165000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4166000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 416717441227SJoe Perches } 416817441227SJoe Perches 4169eeef5733SJoe Perches# printk should use KERN_* levels 4170eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4171000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4172eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 417300df344fSAndy Whitcroft } 41740a920b5bSAndy Whitcroft 4175243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4176243f3803SJoe Perches my $orig = $1; 4177243f3803SJoe Perches my $level = lc($orig); 4178243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 41798f26b837SJoe Perches my $level2 = $level; 41808f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4181243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4182daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4183243f3803SJoe Perches } 4184243f3803SJoe Perches 4185dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4186dc139313SJoe Perches my $orig = $1; 4187dc139313SJoe Perches my $level = lc($orig); 4188dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4189dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4190dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4191dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4192dc139313SJoe Perches } 4193dc139313SJoe Perches 419491c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 419591c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 419691c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 419791c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 419891c9afafSAndy Lutomirski WARN("ENOSYS", 419991c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 420091c9afafSAndy Lutomirski } 420191c9afafSAndy Lutomirski 4202*6b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches. 4203*6b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. 4204*6b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected. 4205*6b9ea5ffSJakub Kicinski if (!$file && $line =~ /\bENOTSUPP\b/) { 4206*6b9ea5ffSJakub Kicinski if (WARN("ENOTSUPP", 4207*6b9ea5ffSJakub Kicinski "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && 4208*6b9ea5ffSJakub Kicinski $fix) { 4209*6b9ea5ffSJakub Kicinski $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; 4210*6b9ea5ffSJakub Kicinski } 4211*6b9ea5ffSJakub Kicinski } 4212*6b9ea5ffSJakub Kicinski 4213653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4214653d4876SAndy Whitcroft# or if closed on same line 42155b57980dSJoe Perches if ($perl_version_ok && 42162d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 42172d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 42182d453e3bSJoe Perches $sline !~ /}/) { 42198d182478SJoe Perches if (ERROR("OPEN_BRACE", 42202d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 42218d182478SJoe Perches $fix) { 42228d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 42238d182478SJoe Perches my $fixed_line = $rawline; 42248d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 42258d182478SJoe Perches my $line1 = $1; 42268d182478SJoe Perches my $line2 = $2; 42278d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 42288d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 42298d182478SJoe Perches if ($line2 !~ /^\s*$/) { 42308d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 42318d182478SJoe Perches } 42328d182478SJoe Perches } 42330a920b5bSAndy Whitcroft } 4234653d4876SAndy Whitcroft 42358905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 42368905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 42378905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 42388d182478SJoe Perches if (ERROR("OPEN_BRACE", 42398d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 42408d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 42418d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 42428d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 42438d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 42448d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42458d182478SJoe Perches $fixedline = $rawline; 42468d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 42478d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 42488d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42498d182478SJoe Perches } 42508d182478SJoe Perches } 42518905a67cSAndy Whitcroft } 42528905a67cSAndy Whitcroft 42530c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 42543705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 42553705ce5bSJoe Perches if (WARN("SPACING", 42563705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 42573705ce5bSJoe Perches $fix) { 4258194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42593705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 42603705ce5bSJoe Perches } 42610c73b4ebSAndy Whitcroft } 42620c73b4ebSAndy Whitcroft 426331070b5dSJoe Perches# Function pointer declarations 426431070b5dSJoe Perches# check spacing between type, funcptr, and args 426531070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 426691f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 426731070b5dSJoe Perches my $declare = $1; 426831070b5dSJoe Perches my $pre_pointer_space = $2; 426931070b5dSJoe Perches my $post_pointer_space = $3; 427031070b5dSJoe Perches my $funcname = $4; 427131070b5dSJoe Perches my $post_funcname_space = $5; 427231070b5dSJoe Perches my $pre_args_space = $6; 427331070b5dSJoe Perches 427491f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 427591f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 427691f72e9cSJoe Perches# don't need a space so don't warn for those. 427791f72e9cSJoe Perches my $post_declare_space = ""; 427891f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 427991f72e9cSJoe Perches $post_declare_space = $1; 428091f72e9cSJoe Perches $declare = rtrim($declare); 428191f72e9cSJoe Perches } 428291f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 428331070b5dSJoe Perches WARN("SPACING", 428431070b5dSJoe Perches "missing space after return type\n" . $herecurr); 428591f72e9cSJoe Perches $post_declare_space = " "; 428631070b5dSJoe Perches } 428731070b5dSJoe Perches 428831070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 428991f72e9cSJoe Perches# This test is not currently implemented because these declarations are 429091f72e9cSJoe Perches# equivalent to 429191f72e9cSJoe Perches# int foo(int bar, ...) 429291f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 429391f72e9cSJoe Perches# 429491f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 429591f72e9cSJoe Perches# WARN("SPACING", 429691f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 429791f72e9cSJoe Perches# } 429831070b5dSJoe Perches 429931070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 430031070b5dSJoe Perches if (defined $pre_pointer_space && 430131070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 430231070b5dSJoe Perches WARN("SPACING", 430331070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 430431070b5dSJoe Perches } 430531070b5dSJoe Perches 430631070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 430731070b5dSJoe Perches if (defined $post_pointer_space && 430831070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 430931070b5dSJoe Perches WARN("SPACING", 431031070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 431131070b5dSJoe Perches } 431231070b5dSJoe Perches 431331070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 431431070b5dSJoe Perches if (defined $post_funcname_space && 431531070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 431631070b5dSJoe Perches WARN("SPACING", 431731070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 431831070b5dSJoe Perches } 431931070b5dSJoe Perches 432031070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 432131070b5dSJoe Perches if (defined $pre_args_space && 432231070b5dSJoe Perches $pre_args_space =~ /^\s/) { 432331070b5dSJoe Perches WARN("SPACING", 432431070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 432531070b5dSJoe Perches } 432631070b5dSJoe Perches 432731070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4328194f66fcSJoe Perches $fixed[$fixlinenr] =~ 432991f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 433031070b5dSJoe Perches } 433131070b5dSJoe Perches } 433231070b5dSJoe Perches 43338d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 43348d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4335fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4336fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 43378d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 43388d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 43398d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4340fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 434138dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 43423705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 43433705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 43443705ce5bSJoe Perches $fix) { 4345194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43463705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 43473705ce5bSJoe Perches } 43488d31cfceSAndy Whitcroft } 43498d31cfceSAndy Whitcroft } 43508d31cfceSAndy Whitcroft 4351f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 43526c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4353c2fdda0dSAndy Whitcroft my $name = $1; 4354773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4355773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4356c2fdda0dSAndy Whitcroft 4357c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4358773647a0SAndy Whitcroft if ($name =~ /^(?: 4359773647a0SAndy Whitcroft if|for|while|switch|return|case| 4360773647a0SAndy Whitcroft volatile|__volatile__| 4361773647a0SAndy Whitcroft __attribute__|format|__extension__| 4362773647a0SAndy Whitcroft asm|__asm__)$/x) 4363773647a0SAndy Whitcroft { 4364c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4365c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4366c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4367c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4368773647a0SAndy Whitcroft 4369773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4370c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4371c2fdda0dSAndy Whitcroft 4372c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4373c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4374773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4375c2fdda0dSAndy Whitcroft 4376c2fdda0dSAndy Whitcroft } else { 43773705ce5bSJoe Perches if (WARN("SPACING", 43783705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 43793705ce5bSJoe Perches $fix) { 4380194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43813705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 43823705ce5bSJoe Perches } 4383f0a594c1SAndy Whitcroft } 43846c72ffaaSAndy Whitcroft } 43859a4cad4eSEric Nelson 4386653d4876SAndy Whitcroft# Check operator spacing. 43870a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 43883705ce5bSJoe Perches my $fixed_line = ""; 43893705ce5bSJoe Perches my $line_fixed = 0; 43903705ce5bSJoe Perches 43919c0ca6f9SAndy Whitcroft my $ops = qr{ 43929c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 43939c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 43949c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 43951f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 439684731623SJoe Perches \?:|\?|: 43979c0ca6f9SAndy Whitcroft }x; 4398cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 43993705ce5bSJoe Perches 44003705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 44013705ce5bSJoe Perches## foreach my $el (@elements) { 44023705ce5bSJoe Perches## print("el: <$el>\n"); 44033705ce5bSJoe Perches## } 44043705ce5bSJoe Perches 44053705ce5bSJoe Perches my @fix_elements = (); 440600df344fSAndy Whitcroft my $off = 0; 44076c72ffaaSAndy Whitcroft 44083705ce5bSJoe Perches foreach my $el (@elements) { 44093705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 44103705ce5bSJoe Perches $off += length($el); 44113705ce5bSJoe Perches } 44123705ce5bSJoe Perches 44133705ce5bSJoe Perches $off = 0; 44143705ce5bSJoe Perches 44156c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4416b34c648bSJoe Perches my $last_after = -1; 44176c72ffaaSAndy Whitcroft 44180a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 44193705ce5bSJoe Perches 44203705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 44213705ce5bSJoe Perches 44223705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 44233705ce5bSJoe Perches 44244a0df2efSAndy Whitcroft $off += length($elements[$n]); 44254a0df2efSAndy Whitcroft 442625985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4427773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4428773647a0SAndy Whitcroft my $cc = ''; 4429773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4430773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4431773647a0SAndy Whitcroft } 4432773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4433773647a0SAndy Whitcroft 44344a0df2efSAndy Whitcroft my $a = ''; 44354a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 44364a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4437cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 44384a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 44394a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4440773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 44414a0df2efSAndy Whitcroft 44420a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 44434a0df2efSAndy Whitcroft 44444a0df2efSAndy Whitcroft my $c = ''; 44450a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 44464a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 44474a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4448cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 44494a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 44504a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 44518b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 44524a0df2efSAndy Whitcroft } else { 44534a0df2efSAndy Whitcroft $c = 'E'; 44540a920b5bSAndy Whitcroft } 44550a920b5bSAndy Whitcroft 44564a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 44574a0df2efSAndy Whitcroft 44584a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 44594a0df2efSAndy Whitcroft 44606c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4461de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 44620a920b5bSAndy Whitcroft 446374048ed8SAndy Whitcroft # Pull out the value of this operator. 44646c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 44650a920b5bSAndy Whitcroft 44661f65f947SAndy Whitcroft # Get the full operator variant. 44671f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 44681f65f947SAndy Whitcroft 446913214adfSAndy Whitcroft # Ignore operators passed as parameters. 447013214adfSAndy Whitcroft if ($op_type ne 'V' && 4471d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 447213214adfSAndy Whitcroft 4473cf655043SAndy Whitcroft# # Ignore comments 4474cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 447513214adfSAndy Whitcroft 4476d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 447713214adfSAndy Whitcroft } elsif ($op eq ';') { 4478cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4479cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 44803705ce5bSJoe Perches if (ERROR("SPACING", 44813705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4482b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 44833705ce5bSJoe Perches $line_fixed = 1; 44843705ce5bSJoe Perches } 4485d8aaf121SAndy Whitcroft } 4486d8aaf121SAndy Whitcroft 4487d8aaf121SAndy Whitcroft # // is a comment 4488d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 44890a920b5bSAndy Whitcroft 4490b00e4814SJoe Perches # : when part of a bitfield 4491b00e4814SJoe Perches } elsif ($opv eq ':B') { 4492b00e4814SJoe Perches # skip the bitfield test for now 4493b00e4814SJoe Perches 44941f65f947SAndy Whitcroft # No spaces for: 44951f65f947SAndy Whitcroft # -> 4496b00e4814SJoe Perches } elsif ($op eq '->') { 44974a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 44983705ce5bSJoe Perches if (ERROR("SPACING", 44993705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4500b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45013705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45023705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 45033705ce5bSJoe Perches } 4504b34c648bSJoe Perches $line_fixed = 1; 45053705ce5bSJoe Perches } 45060a920b5bSAndy Whitcroft } 45070a920b5bSAndy Whitcroft 45082381097bSJoe Perches # , must not have a space before and must have a space on the right. 45090a920b5bSAndy Whitcroft } elsif ($op eq ',') { 45102381097bSJoe Perches my $rtrim_before = 0; 45112381097bSJoe Perches my $space_after = 0; 45122381097bSJoe Perches if ($ctx =~ /Wx./) { 45132381097bSJoe Perches if (ERROR("SPACING", 45142381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 45152381097bSJoe Perches $line_fixed = 1; 45162381097bSJoe Perches $rtrim_before = 1; 45172381097bSJoe Perches } 45182381097bSJoe Perches } 4519cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 45203705ce5bSJoe Perches if (ERROR("SPACING", 45213705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 45223705ce5bSJoe Perches $line_fixed = 1; 4523b34c648bSJoe Perches $last_after = $n; 45242381097bSJoe Perches $space_after = 1; 45252381097bSJoe Perches } 45262381097bSJoe Perches } 45272381097bSJoe Perches if ($rtrim_before || $space_after) { 45282381097bSJoe Perches if ($rtrim_before) { 45292381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45302381097bSJoe Perches } else { 45312381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 45322381097bSJoe Perches } 45332381097bSJoe Perches if ($space_after) { 45342381097bSJoe Perches $good .= " "; 45353705ce5bSJoe Perches } 45360a920b5bSAndy Whitcroft } 45370a920b5bSAndy Whitcroft 45389c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 453974048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 45409c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 45419c0ca6f9SAndy Whitcroft 45429c0ca6f9SAndy Whitcroft # unary operators should have a space before and 45439c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 45449c0ca6f9SAndy Whitcroft # unary operator, or a cast 45459c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 454674048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 45470d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4548cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 45493705ce5bSJoe Perches if (ERROR("SPACING", 45503705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4551b34c648bSJoe Perches if ($n != $last_after + 2) { 4552b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 45533705ce5bSJoe Perches $line_fixed = 1; 45543705ce5bSJoe Perches } 45550a920b5bSAndy Whitcroft } 4556b34c648bSJoe Perches } 4557a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4558171ae1a4SAndy Whitcroft # A unary '*' may be const 4559171ae1a4SAndy Whitcroft 4560171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 45613705ce5bSJoe Perches if (ERROR("SPACING", 45623705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4563b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 45643705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45653705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 45663705ce5bSJoe Perches } 4567b34c648bSJoe Perches $line_fixed = 1; 45683705ce5bSJoe Perches } 45690a920b5bSAndy Whitcroft } 45700a920b5bSAndy Whitcroft 45710a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 45720a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4573773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 45743705ce5bSJoe Perches if (ERROR("SPACING", 45753705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4576b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 45773705ce5bSJoe Perches $line_fixed = 1; 45783705ce5bSJoe Perches } 45790a920b5bSAndy Whitcroft } 4580773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4581773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 45823705ce5bSJoe Perches if (ERROR("SPACING", 45833705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4584b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45853705ce5bSJoe Perches $line_fixed = 1; 45863705ce5bSJoe Perches } 4587653d4876SAndy Whitcroft } 4588773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 45893705ce5bSJoe Perches if (ERROR("SPACING", 45903705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4591b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 45923705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45933705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4594773647a0SAndy Whitcroft } 4595b34c648bSJoe Perches $line_fixed = 1; 45963705ce5bSJoe Perches } 45973705ce5bSJoe Perches } 45980a920b5bSAndy Whitcroft 45990a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 46009c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 46019c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 46029c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4603c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4604c2fdda0dSAndy Whitcroft $op eq '%') 46050a920b5bSAndy Whitcroft { 4606d2e025f3SJoe Perches if ($check) { 4607d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4608d2e025f3SJoe Perches if (CHK("SPACING", 4609d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4610d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4611d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4612d2e025f3SJoe Perches $line_fixed = 1; 4613d2e025f3SJoe Perches } 4614d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4615d2e025f3SJoe Perches if (CHK("SPACING", 4616d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4617d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4618d2e025f3SJoe Perches $line_fixed = 1; 4619d2e025f3SJoe Perches } 4620d2e025f3SJoe Perches } 4621d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 46223705ce5bSJoe Perches if (ERROR("SPACING", 46233705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4624b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4625b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4626b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4627b34c648bSJoe Perches } 46283705ce5bSJoe Perches $line_fixed = 1; 46293705ce5bSJoe Perches } 46300a920b5bSAndy Whitcroft } 46310a920b5bSAndy Whitcroft 46321f65f947SAndy Whitcroft # A colon needs no spaces before when it is 46331f65f947SAndy Whitcroft # terminating a case value or a label. 46341f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 46351f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 46363705ce5bSJoe Perches if (ERROR("SPACING", 46373705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4638b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 46393705ce5bSJoe Perches $line_fixed = 1; 46403705ce5bSJoe Perches } 46411f65f947SAndy Whitcroft } 46421f65f947SAndy Whitcroft 46430a920b5bSAndy Whitcroft # All the others need spaces both sides. 4644cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 46451f65f947SAndy Whitcroft my $ok = 0; 46461f65f947SAndy Whitcroft 464722f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 46481f65f947SAndy Whitcroft if (($op eq '<' && 46491f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 46501f65f947SAndy Whitcroft ($op eq '>' && 46511f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 46521f65f947SAndy Whitcroft { 46531f65f947SAndy Whitcroft $ok = 1; 46541f65f947SAndy Whitcroft } 46551f65f947SAndy Whitcroft 4656e0df7e1fSJoe Perches # for asm volatile statements 4657e0df7e1fSJoe Perches # ignore a colon with another 4658e0df7e1fSJoe Perches # colon immediately before or after 4659e0df7e1fSJoe Perches if (($op eq ':') && 4660e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4661e0df7e1fSJoe Perches $ok = 1; 4662e0df7e1fSJoe Perches } 4663e0df7e1fSJoe Perches 466484731623SJoe Perches # messages are ERROR, but ?: are CHK 46651f65f947SAndy Whitcroft if ($ok == 0) { 46660675a8fbSJean Delvare my $msg_level = \&ERROR; 46670675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 466884731623SJoe Perches 46690675a8fbSJean Delvare if (&{$msg_level}("SPACING", 46703705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4671b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4672b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4673b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4674b34c648bSJoe Perches } 46753705ce5bSJoe Perches $line_fixed = 1; 46763705ce5bSJoe Perches } 46770a920b5bSAndy Whitcroft } 467822f2a2efSAndy Whitcroft } 46794a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 46803705ce5bSJoe Perches 46813705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 46823705ce5bSJoe Perches 46833705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 46840a920b5bSAndy Whitcroft } 46853705ce5bSJoe Perches 46863705ce5bSJoe Perches if (($#elements % 2) == 0) { 46873705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 46883705ce5bSJoe Perches } 46893705ce5bSJoe Perches 4690194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4691194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 46923705ce5bSJoe Perches } 46933705ce5bSJoe Perches 46943705ce5bSJoe Perches 46950a920b5bSAndy Whitcroft } 46960a920b5bSAndy Whitcroft 4697786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4698d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4699786b6326SJoe Perches if (WARN("SPACING", 4700786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4701786b6326SJoe Perches $fix) { 4702194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4703786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4704786b6326SJoe Perches } 4705786b6326SJoe Perches } 4706786b6326SJoe Perches 4707f0a594c1SAndy Whitcroft# check for multiple assignments 4708f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4709000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4710000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4711f0a594c1SAndy Whitcroft } 4712f0a594c1SAndy Whitcroft 471322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 471422f2a2efSAndy Whitcroft## # continuation. 471522f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 471622f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 471722f2a2efSAndy Whitcroft## 471822f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 471922f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 472022f2a2efSAndy Whitcroft## my $ln = $line; 472122f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 472222f2a2efSAndy Whitcroft## } 472322f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4724000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4725000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 472622f2a2efSAndy Whitcroft## } 472722f2a2efSAndy Whitcroft## } 4728f0a594c1SAndy Whitcroft 47290a920b5bSAndy Whitcroft#need space before brace following if, while, etc 47306b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 47316ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 47323705ce5bSJoe Perches if (ERROR("SPACING", 47333705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 47343705ce5bSJoe Perches $fix) { 47356ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 47363705ce5bSJoe Perches } 4737de7d4f0eSAndy Whitcroft } 4738de7d4f0eSAndy Whitcroft 4739c4a62ef9SJoe Perches## # check for blank lines before declarations 4740c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4741c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4742c4a62ef9SJoe Perches## WARN("SPACING", 4743c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4744c4a62ef9SJoe Perches## } 4745c4a62ef9SJoe Perches## 4746c4a62ef9SJoe Perches 4747de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4748de7d4f0eSAndy Whitcroft# on the line 474994fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 4750d5e616fcSJoe Perches if (ERROR("SPACING", 4751d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4752d5e616fcSJoe Perches $fix) { 4753194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4754d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4755d5e616fcSJoe Perches } 47560a920b5bSAndy Whitcroft } 47570a920b5bSAndy Whitcroft 475822f2a2efSAndy Whitcroft# check spacing on square brackets 475922f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 47603705ce5bSJoe Perches if (ERROR("SPACING", 47613705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 47623705ce5bSJoe Perches $fix) { 4763194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47643705ce5bSJoe Perches s/\[\s+/\[/; 47653705ce5bSJoe Perches } 476622f2a2efSAndy Whitcroft } 476722f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 47683705ce5bSJoe Perches if (ERROR("SPACING", 47693705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 47703705ce5bSJoe Perches $fix) { 4771194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47723705ce5bSJoe Perches s/\s+\]/\]/; 47733705ce5bSJoe Perches } 477422f2a2efSAndy Whitcroft } 477522f2a2efSAndy Whitcroft 4776c45dcabdSAndy Whitcroft# check spacing on parentheses 47779c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 47789c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 47793705ce5bSJoe Perches if (ERROR("SPACING", 47803705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 47813705ce5bSJoe Perches $fix) { 4782194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47833705ce5bSJoe Perches s/\(\s+/\(/; 47843705ce5bSJoe Perches } 478522f2a2efSAndy Whitcroft } 478613214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4787c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4788c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 47893705ce5bSJoe Perches if (ERROR("SPACING", 47903705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 47913705ce5bSJoe Perches $fix) { 4792194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47933705ce5bSJoe Perches s/\s+\)/\)/; 47943705ce5bSJoe Perches } 479522f2a2efSAndy Whitcroft } 479622f2a2efSAndy Whitcroft 4797e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4798e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4799e2826fd0SJoe Perches 4800e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4801ea4acbb1SJoe Perches my $var = $1; 4802ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4803ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4804ea4acbb1SJoe Perches $fix) { 4805ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4806ea4acbb1SJoe Perches } 4807ea4acbb1SJoe Perches } 4808ea4acbb1SJoe Perches 4809ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4810ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4811ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4812ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4813ea4acbb1SJoe Perches my $var = $2; 4814ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4815ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4816ea4acbb1SJoe Perches $fix) { 4817ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4818ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4819ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4820ea4acbb1SJoe Perches } 4821e2826fd0SJoe Perches } 4822e2826fd0SJoe Perches 482363b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4824a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4825a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 48265b57980dSJoe Perches $perl_version_ok && defined($stat) && 482763b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 482863b7c73eSJoe Perches my $if_stat = $1; 482963b7c73eSJoe Perches my $test = substr($2, 1, -1); 483063b7c73eSJoe Perches my $herectx; 483163b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 483263b7c73eSJoe Perches my $match = $1; 483363b7c73eSJoe Perches # avoid parentheses around potential macro args 483463b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 483563b7c73eSJoe Perches if (!defined($herectx)) { 483663b7c73eSJoe Perches $herectx = $here . "\n"; 483763b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 483863b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 483963b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 484063b7c73eSJoe Perches $herectx .= $rl . "\n"; 484163b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 484263b7c73eSJoe Perches } 484363b7c73eSJoe Perches } 484463b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 484563b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 484663b7c73eSJoe Perches } 484763b7c73eSJoe Perches } 484863b7c73eSJoe Perches 48490a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 48504a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 48510a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 48523705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 48533705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 48543705ce5bSJoe Perches $fix) { 4855194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48563705ce5bSJoe Perches s/^(.)\s+/$1/; 48573705ce5bSJoe Perches } 48580a920b5bSAndy Whitcroft } 48590a920b5bSAndy Whitcroft 48605b9553abSJoe Perches# return is not a function 4861507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4862c45dcabdSAndy Whitcroft my $spacing = $1; 48635b57980dSJoe Perches if ($perl_version_ok && 48645b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 48655b9553abSJoe Perches my $value = $1; 48665b9553abSJoe Perches $value = deparenthesize($value); 48675b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4868000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4869000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 48705b9553abSJoe Perches } 4871c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4872000d1cc1SJoe Perches ERROR("SPACING", 4873000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4874c45dcabdSAndy Whitcroft } 4875c45dcabdSAndy Whitcroft } 4876507e5141SJoe Perches 4877b43ae21bSJoe Perches# unnecessary return in a void function 4878b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4879b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4880b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4881b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4882b43ae21bSJoe Perches $linenr >= 3 && 4883b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4884b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 48859819cf25SJoe Perches WARN("RETURN_VOID", 4886b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 48879819cf25SJoe Perches } 48889819cf25SJoe Perches 4889189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 48905b57980dSJoe Perches if ($perl_version_ok && 4891189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4892189248d8SJoe Perches my $openparens = $1; 4893189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4894189248d8SJoe Perches my $msg = ""; 4895189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4896189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4897189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4898189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4899189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4900189248d8SJoe Perches } 4901189248d8SJoe Perches } 4902189248d8SJoe Perches 4903c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4904c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4905c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4906c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 49075b57980dSJoe Perches if ($perl_version_ok && 4908c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4909c5595fa2SJoe Perches my $lead = $1; 4910c5595fa2SJoe Perches my $const = $2; 4911c5595fa2SJoe Perches my $comp = $3; 4912c5595fa2SJoe Perches my $to = $4; 4913c5595fa2SJoe Perches my $newcomp = $comp; 4914f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4915c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4916c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4917c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4918c5595fa2SJoe Perches $fix) { 4919c5595fa2SJoe Perches if ($comp eq "<") { 4920c5595fa2SJoe Perches $newcomp = ">"; 4921c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4922c5595fa2SJoe Perches $newcomp = ">="; 4923c5595fa2SJoe Perches } elsif ($comp eq ">") { 4924c5595fa2SJoe Perches $newcomp = "<"; 4925c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4926c5595fa2SJoe Perches $newcomp = "<="; 4927c5595fa2SJoe Perches } 4928c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4929c5595fa2SJoe Perches } 4930c5595fa2SJoe Perches } 4931c5595fa2SJoe Perches 4932f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4933f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 493453a3c448SAndy Whitcroft my $name = $1; 493553a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4936000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4937f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 493853a3c448SAndy Whitcroft } 493953a3c448SAndy Whitcroft } 4940c45dcabdSAndy Whitcroft 49410a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 49424a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 49433705ce5bSJoe Perches if (ERROR("SPACING", 49443705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 49453705ce5bSJoe Perches $fix) { 4946194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49473705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 49483705ce5bSJoe Perches } 49490a920b5bSAndy Whitcroft } 49500a920b5bSAndy Whitcroft 4951f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4952f5fe35ddSAndy Whitcroft# statements after the conditional. 4953170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 49543e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 49553e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 49563e469cdcSAndy Whitcroft if (!defined $stat); 4957170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4958170d3a22SAndy Whitcroft $remain_next, $off_next); 4959170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4960170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4961170d3a22SAndy Whitcroft 4962170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4963170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4964170d3a22SAndy Whitcroft # then count those as offsets. 4965170d3a22SAndy Whitcroft my ($whitespace) = 4966170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4967170d3a22SAndy Whitcroft my $offset = 4968170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4969170d3a22SAndy Whitcroft 4970170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4971170d3a22SAndy Whitcroft $offset} = 1; 4972170d3a22SAndy Whitcroft } 4973170d3a22SAndy Whitcroft } 4974170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4975c11230f4SJoe Perches defined($stat) && defined($cond) && 4976170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4977171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 49788905a67cSAndy Whitcroft 4979b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4980000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4981000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 49828905a67cSAndy Whitcroft } 49838905a67cSAndy Whitcroft 49848905a67cSAndy Whitcroft # Find out what is on the end of the line after the 49858905a67cSAndy Whitcroft # conditional. 4986773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 49878905a67cSAndy Whitcroft $s =~ s/\n.*//g; 498813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 498953210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 499053210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4991773647a0SAndy Whitcroft { 4992bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4993bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4994bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 499542bdf74cSHidetoshi Seto my $stat_real = ''; 4996bb44ad39SAndy Whitcroft 499742bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 499842bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4999bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 5000bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 5001bb44ad39SAndy Whitcroft } 5002bb44ad39SAndy Whitcroft 5003000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5004000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 50058905a67cSAndy Whitcroft } 50068905a67cSAndy Whitcroft } 50078905a67cSAndy Whitcroft 500813214adfSAndy Whitcroft# Check for bitwise tests written as boolean 500913214adfSAndy Whitcroft if ($line =~ / 501013214adfSAndy Whitcroft (?: 501113214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 501213214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 501313214adfSAndy Whitcroft (?:\&\&|\|\|) 501413214adfSAndy Whitcroft | 501513214adfSAndy Whitcroft (?:\&\&|\|\|) 501613214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 501713214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 501813214adfSAndy Whitcroft )/x) 501913214adfSAndy Whitcroft { 5020000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 5021000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 502213214adfSAndy Whitcroft } 502313214adfSAndy Whitcroft 50248905a67cSAndy Whitcroft# if and else should not have general statements after it 502513214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 502613214adfSAndy Whitcroft my $s = $1; 502713214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 502813214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 5029000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5030000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 50310a920b5bSAndy Whitcroft } 503213214adfSAndy Whitcroft } 503339667782SAndy Whitcroft# if should not continue a brace 503439667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5035000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5036048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 503739667782SAndy Whitcroft $herecurr); 503839667782SAndy Whitcroft } 5039a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5040a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5041a1080bf8SAndy Whitcroft $line !~ /\G(?: 50423fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5043a1080bf8SAndy Whitcroft \s*return\s+ 5044a1080bf8SAndy Whitcroft )/xg) 5045a1080bf8SAndy Whitcroft { 5046000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5047000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5048a1080bf8SAndy Whitcroft } 50490a920b5bSAndy Whitcroft 50500a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 50510a920b5bSAndy Whitcroft # indent level to be relevant to each other. 50528b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 50530a920b5bSAndy Whitcroft $previndent == $indent) { 50548b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 50558b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 50568b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 50578b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 50588b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 50598b8856f4SJoe Perches my $fixedline = $prevrawline; 50608b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 50618b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 50628b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50638b8856f4SJoe Perches } 50648b8856f4SJoe Perches $fixedline = $rawline; 50658b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 50668b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50678b8856f4SJoe Perches } 50680a920b5bSAndy Whitcroft } 50690a920b5bSAndy Whitcroft 50708b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5071c2fdda0dSAndy Whitcroft $previndent == $indent) { 5072c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5073c2fdda0dSAndy Whitcroft 5074c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5075c2fdda0dSAndy Whitcroft # conditional. 5076773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5077c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5078c2fdda0dSAndy Whitcroft 5079c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 50808b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 50818b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 50828b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 50838b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 50848b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 50858b8856f4SJoe Perches my $fixedline = $prevrawline; 50868b8856f4SJoe Perches my $trailing = $rawline; 50878b8856f4SJoe Perches $trailing =~ s/^\+//; 50888b8856f4SJoe Perches $trailing = trim($trailing); 50898b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 50908b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50918b8856f4SJoe Perches } 5092c2fdda0dSAndy Whitcroft } 5093c2fdda0dSAndy Whitcroft } 5094c2fdda0dSAndy Whitcroft 509595e2c602SJoe Perches#Specific variable tests 5096323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5097323c1260SJoe Perches my $var = $1; 509895e2c602SJoe Perches 509995e2c602SJoe Perches#CamelCase 5100807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5101be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 510222735ce8SJoe Perches#Ignore Page<foo> variants 5103807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5104d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5105d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5106d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5107f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5108f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 51097e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 51107e781f67SJoe Perches my $word = $1; 51117e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5112d8b07710SJoe Perches if ($check) { 5113d8b07710SJoe Perches seed_camelcase_includes(); 5114d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5115d8b07710SJoe Perches seed_camelcase_file($realfile); 5116d8b07710SJoe Perches $camelcase_file_seeded = 1; 5117d8b07710SJoe Perches } 5118d8b07710SJoe Perches } 51197e781f67SJoe Perches if (!defined $camelcase{$word}) { 51207e781f67SJoe Perches $camelcase{$word} = 1; 5121be79794bSJoe Perches CHK("CAMELCASE", 51227e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 51237e781f67SJoe Perches } 5124323c1260SJoe Perches } 5125323c1260SJoe Perches } 51263445686aSJoe Perches } 51270a920b5bSAndy Whitcroft 51280a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5129d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5130d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5131d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5132d5e616fcSJoe Perches $fix) { 5133194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5134d5e616fcSJoe Perches } 51350a920b5bSAndy Whitcroft } 51360a920b5bSAndy Whitcroft 51370e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 51380e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5139c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5140e09dec48SAndy Whitcroft my $file = "$1.h"; 5141e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5142e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5143e09dec48SAndy Whitcroft $realfile ne $checkfile && 51447840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5145c45dcabdSAndy Whitcroft { 51460e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 51470e212e0aSFabian Frederick if ($asminclude > 0) { 5148e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5149000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5150000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5151e09dec48SAndy Whitcroft } else { 5152000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5153000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5154e09dec48SAndy Whitcroft } 51550a920b5bSAndy Whitcroft } 51560a920b5bSAndy Whitcroft } 51570e212e0aSFabian Frederick } 51580a920b5bSAndy Whitcroft 5159653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5160653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5161cf655043SAndy Whitcroft# in a known good container 5162b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5163b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5164d8aaf121SAndy Whitcroft my $ln = $linenr; 5165d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5166c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5167c45dcabdSAndy Whitcroft my $ctx = ''; 516808a2843eSJoe Perches my $has_flow_statement = 0; 516908a2843eSJoe Perches my $has_arg_concat = 0; 5170c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5171f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5172f74bd194SAndy Whitcroft $ctx = $dstat; 5173c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5174a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5175c45dcabdSAndy Whitcroft 517608a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 517762e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 517808a2843eSJoe Perches 5179f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5180f59b64bfSJoe Perches my $define_args = $1; 5181f59b64bfSJoe Perches my $define_stmt = $dstat; 5182f59b64bfSJoe Perches my @def_args = (); 5183f59b64bfSJoe Perches 5184f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5185f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5186f59b64bfSJoe Perches $define_args =~ s/\s*//g; 51878c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5188f59b64bfSJoe Perches @def_args = split(",", $define_args); 5189f59b64bfSJoe Perches } 5190f59b64bfSJoe Perches 5191292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5192c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5193c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5194c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5195c45dcabdSAndy Whitcroft 5196c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 5197bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 5198bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 51996b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 5200bf30d6edSAndy Whitcroft { 5201c45dcabdSAndy Whitcroft } 5202c45dcabdSAndy Whitcroft 5203342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 520433acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 520533acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5206e45bab8eSAndy Whitcroft { 5207e45bab8eSAndy Whitcroft } 5208e45bab8eSAndy Whitcroft 520942e15293SJoe Perches # Make asm volatile uses seem like a generic function 521042e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 521142e15293SJoe Perches 5212c45dcabdSAndy Whitcroft my $exceptions = qr{ 5213c45dcabdSAndy Whitcroft $Declare| 5214c45dcabdSAndy Whitcroft module_param_named| 5215a0a0a7a9SKees Cook MODULE_PARM_DESC| 5216c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5217c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5218383099fdSAndy Whitcroft __typeof__\(| 521922fd2d3eSStefani Seibold union| 522022fd2d3eSStefani Seibold struct| 5221ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 52226b10df42SVladimir Zapolskiy ^\"|\"$| 52236b10df42SVladimir Zapolskiy ^\[ 5224c45dcabdSAndy Whitcroft }x; 52255eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5226f59b64bfSJoe Perches 5227f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5228f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5229e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5230f59b64bfSJoe Perches 5231f74bd194SAndy Whitcroft if ($dstat ne '' && 5232f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5233f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 52343cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5235356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5236f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5237f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5238e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 523972f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5240f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5241f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5242f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 52434e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5244f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5245c45dcabdSAndy Whitcroft { 5246e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5247e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5248e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5249e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5250f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5251f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5252f74bd194SAndy Whitcroft } else { 5253000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5254388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5255d8aaf121SAndy Whitcroft } 5256f59b64bfSJoe Perches 5257f59b64bfSJoe Perches } 52585207649bSJoe Perches 52595207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 52605207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 52615207649bSJoe Perches my $first = 1; 52625207649bSJoe Perches $define_stmt = ""; 52635207649bSJoe Perches foreach my $l (@stmt_array) { 52645207649bSJoe Perches $l =~ s/\\$//; 52655207649bSJoe Perches if ($first) { 52665207649bSJoe Perches $define_stmt = $l; 52675207649bSJoe Perches $first = 0; 52685207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 52695207649bSJoe Perches $define_stmt .= substr($l, 1); 52705207649bSJoe Perches } 52715207649bSJoe Perches } 52725207649bSJoe Perches $define_stmt =~ s/$;//g; 52735207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 52745207649bSJoe Perches $define_stmt = trim($define_stmt); 52755207649bSJoe Perches 5276f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5277f59b64bfSJoe Perches foreach my $arg (@def_args) { 5278f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 52799192d41aSJoe Perches next if ($arg =~ /^type$/i); 52807fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 52816dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 52827fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 52837fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5284d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5285f59b64bfSJoe Perches if ($use_cnt > 1) { 5286f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5287f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5288f59b64bfSJoe Perches } 52899192d41aSJoe Perches# check if any macro arguments may have other precedence issues 52907fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 52919192d41aSJoe Perches ((defined($1) && $1 ne ',') || 52929192d41aSJoe Perches (defined($2) && $2 ne ','))) { 52939192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 52949192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 52959192d41aSJoe Perches } 52960a920b5bSAndy Whitcroft } 52975023d347SJoe Perches 529808a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 529908a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 530008a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 530108a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5302e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 530308a2843eSJoe Perches 530408a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 530508a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 530608a2843eSJoe Perches } 530708a2843eSJoe Perches 5308481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 53095023d347SJoe Perches 53105023d347SJoe Perches } else { 53115023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5312481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5313481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 53145023d347SJoe Perches $line =~ /^\+.*\\$/) { 53155023d347SJoe Perches WARN("LINE_CONTINUATIONS", 53165023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 53175023d347SJoe Perches } 5318653d4876SAndy Whitcroft } 53190a920b5bSAndy Whitcroft 5320b13edf7fSJoe Perches# do {} while (0) macro tests: 5321b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5322b13edf7fSJoe Perches# macro should not end with a semicolon 53235b57980dSJoe Perches if ($perl_version_ok && 5324b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5325b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5326b13edf7fSJoe Perches my $ln = $linenr; 5327b13edf7fSJoe Perches my $cnt = $realcnt; 5328b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5329b13edf7fSJoe Perches my $ctx = ''; 5330b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5331b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5332b13edf7fSJoe Perches $ctx = $dstat; 5333b13edf7fSJoe Perches 5334b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 53351b36b201SJoe Perches $dstat =~ s/$;/ /g; 5336b13edf7fSJoe Perches 5337b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5338b13edf7fSJoe Perches my $stmts = $2; 5339b13edf7fSJoe Perches my $semis = $3; 5340b13edf7fSJoe Perches 5341b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5342b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5343e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5344b13edf7fSJoe Perches 5345ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5346ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5347b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5348b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5349b13edf7fSJoe Perches } 5350b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5351b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5352b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5353b13edf7fSJoe Perches } 5354f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5355f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5356f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5357e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5358f5ef95b1SJoe Perches 5359f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5360f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5361b13edf7fSJoe Perches } 5362b13edf7fSJoe Perches } 5363b13edf7fSJoe Perches 5364f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 536513214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 536613214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5367cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 536813214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5369cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5370cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5371aad4f614SJoe Perches my @allowed = (); 5372aad4f614SJoe Perches my $allow = 0; 537313214adfSAndy Whitcroft my $seen = 0; 5374773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5375cf655043SAndy Whitcroft my $ln = $linenr - 1; 537613214adfSAndy Whitcroft for my $chunk (@chunks) { 537713214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 537813214adfSAndy Whitcroft 5379773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5380773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5381773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5382773647a0SAndy Whitcroft 5383aad4f614SJoe Perches $allowed[$allow] = 0; 5384773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5385773647a0SAndy Whitcroft 5386773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5387773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5388773647a0SAndy Whitcroft 5389773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5390cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5391cf655043SAndy Whitcroft 5392773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 539313214adfSAndy Whitcroft 539413214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 539513214adfSAndy Whitcroft 5396aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5397cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5398cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5399aad4f614SJoe Perches $allowed[$allow] = 1; 540013214adfSAndy Whitcroft } 540113214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5402cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5403aad4f614SJoe Perches $allowed[$allow] = 1; 540413214adfSAndy Whitcroft } 5405cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5406cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5407aad4f614SJoe Perches $allowed[$allow] = 1; 540813214adfSAndy Whitcroft } 5409aad4f614SJoe Perches $allow++; 541013214adfSAndy Whitcroft } 5411aad4f614SJoe Perches if ($seen) { 5412aad4f614SJoe Perches my $sum_allowed = 0; 5413aad4f614SJoe Perches foreach (@allowed) { 5414aad4f614SJoe Perches $sum_allowed += $_; 5415aad4f614SJoe Perches } 5416aad4f614SJoe Perches if ($sum_allowed == 0) { 5417000d1cc1SJoe Perches WARN("BRACES", 5418000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5419aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5420aad4f614SJoe Perches $seen != $allow) { 5421aad4f614SJoe Perches CHK("BRACES", 5422aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5423aad4f614SJoe Perches } 542413214adfSAndy Whitcroft } 542513214adfSAndy Whitcroft } 542613214adfSAndy Whitcroft } 5427773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 542813214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5429cf655043SAndy Whitcroft my $allowed = 0; 5430f0a594c1SAndy Whitcroft 5431cf655043SAndy Whitcroft # Check the pre-context. 5432cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5433cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5434cf655043SAndy Whitcroft $allowed = 1; 5435f0a594c1SAndy Whitcroft } 5436773647a0SAndy Whitcroft 5437773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5438773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5439773647a0SAndy Whitcroft 5440cf655043SAndy Whitcroft # Check the condition. 5441cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5442773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5443cf655043SAndy Whitcroft if (defined $cond) { 5444773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5445cf655043SAndy Whitcroft } 5446cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5447cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5448cf655043SAndy Whitcroft $allowed = 1; 5449cf655043SAndy Whitcroft } 5450cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5451cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5452cf655043SAndy Whitcroft $allowed = 1; 5453cf655043SAndy Whitcroft } 5454cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5455cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5456cf655043SAndy Whitcroft $allowed = 1; 5457cf655043SAndy Whitcroft } 5458cf655043SAndy Whitcroft # Check the post-context. 5459cf655043SAndy Whitcroft if (defined $chunks[1]) { 5460cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5461cf655043SAndy Whitcroft if (defined $cond) { 5462773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5463cf655043SAndy Whitcroft } 5464cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5465cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5466cf655043SAndy Whitcroft $allowed = 1; 5467cf655043SAndy Whitcroft } 5468cf655043SAndy Whitcroft } 5469cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5470f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5471e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5472cf655043SAndy Whitcroft 5473000d1cc1SJoe Perches WARN("BRACES", 5474000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5475f0a594c1SAndy Whitcroft } 5476f0a594c1SAndy Whitcroft } 5477f0a594c1SAndy Whitcroft 5478e4c5babdSJoe Perches# check for single line unbalanced braces 547995330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 548095330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5481e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5482e4c5babdSJoe Perches } 5483e4c5babdSJoe Perches 54840979ae66SJoe Perches# check for unnecessary blank lines around braces 548577b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5486f8e58219SJoe Perches if (CHK("BRACES", 5487f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5488f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5489f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5490f8e58219SJoe Perches } 54910979ae66SJoe Perches } 549277b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5493f8e58219SJoe Perches if (CHK("BRACES", 5494f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5495f8e58219SJoe Perches $fix) { 5496f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5497f8e58219SJoe Perches } 54980979ae66SJoe Perches } 54990979ae66SJoe Perches 55004a0df2efSAndy Whitcroft# no volatiles please 55016c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 55026c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5503000d1cc1SJoe Perches WARN("VOLATILE", 55048c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 55054a0df2efSAndy Whitcroft } 55064a0df2efSAndy Whitcroft 55075e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 55085e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 55095e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 55105e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 551133acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 55125e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 55135e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 55145e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 55155e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 55165e4f6ba5SJoe Perches $fix && 55175e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 55185e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 55195e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 55205e4f6ba5SJoe Perches my $comma_close = ""; 55215e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 55225e4f6ba5SJoe Perches $comma_close = $1; 55235e4f6ba5SJoe Perches } 55245e4f6ba5SJoe Perches 55255e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 55265e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 55275e4f6ba5SJoe Perches my $fixedline = $prevrawline; 55285e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 55295e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 55305e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 55315e4f6ba5SJoe Perches $fixedline = $rawline; 55325e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 55335e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 55345e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 55355e4f6ba5SJoe Perches } 55365e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 55375e4f6ba5SJoe Perches } 55385e4f6ba5SJoe Perches } 55395e4f6ba5SJoe Perches 55405e4f6ba5SJoe Perches# check for missing a space in a string concatenation 55415e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 55425e4f6ba5SJoe Perches WARN('MISSING_SPACE', 55435e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 55445e4f6ba5SJoe Perches } 55455e4f6ba5SJoe Perches 554677cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5547e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5548e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5549e4b7d309SJoe Perches# function declarations 555077cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 555177cb8546SJoe Perches defined($context_function) && 5552e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5553e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 555477cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5555e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 555677cb8546SJoe Perches } 555777cb8546SJoe Perches 55585e4f6ba5SJoe Perches# check for spaces before a quoted newline 55595e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 55605e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 55615e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 55625e4f6ba5SJoe Perches $fix) { 55635e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 55645e4f6ba5SJoe Perches } 55655e4f6ba5SJoe Perches 55665e4f6ba5SJoe Perches } 55675e4f6ba5SJoe Perches 5568f17dba4fSJoe Perches# concatenated string without spaces between elements 556979682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 557079682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 557179682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 557279682c0cSJoe Perches $fix) { 557379682c0cSJoe Perches while ($line =~ /($String)/g) { 557479682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 557579682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 557679682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 557779682c0cSJoe Perches } 557879682c0cSJoe Perches } 5579f17dba4fSJoe Perches } 5580f17dba4fSJoe Perches 558190ad30e5SJoe Perches# uncoalesced string fragments 558233acb54aSJoe Perches if ($line =~ /$String\s*"/) { 558379682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 558479682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 558579682c0cSJoe Perches $fix) { 558679682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 558779682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 558879682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 558979682c0cSJoe Perches } 559079682c0cSJoe Perches } 559190ad30e5SJoe Perches } 559290ad30e5SJoe Perches 5593522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5594522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5595522b837cSAlexey Dobriyan my $show_Z = 1; 55965e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5597522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 55985e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5599522b837cSAlexey Dobriyan # check for %L 5600522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 56015e4f6ba5SJoe Perches WARN("PRINTF_L", 5602522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5603522b837cSAlexey Dobriyan $show_L = 0; 56045e4f6ba5SJoe Perches } 5605522b837cSAlexey Dobriyan # check for %Z 5606522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5607522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5608522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5609522b837cSAlexey Dobriyan $show_Z = 0; 5610522b837cSAlexey Dobriyan } 5611522b837cSAlexey Dobriyan # check for 0x<decimal> 5612522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5613522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 56146e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 56156e300757SJoe Perches } 56165e4f6ba5SJoe Perches } 56175e4f6ba5SJoe Perches 56185e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 56193f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 56205e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 56215e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 56225e4f6ba5SJoe Perches } 56235e4f6ba5SJoe Perches 562400df344fSAndy Whitcroft# warn about #if 0 5625c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 562660f89010SPrakruthi Deepak Heragu WARN("IF_0", 562760f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 562860f89010SPrakruthi Deepak Heragu } 562960f89010SPrakruthi Deepak Heragu 563060f89010SPrakruthi Deepak Heragu# warn about #if 1 563160f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 563260f89010SPrakruthi Deepak Heragu WARN("IF_1", 563360f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 56344a0df2efSAndy Whitcroft } 56354a0df2efSAndy Whitcroft 563603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 563703df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5638100425deSJoe Perches my $tested = quotemeta($1); 5639100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5640100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5641100425deSJoe Perches my $func = $1; 5642100425deSJoe Perches if (WARN('NEEDLESS_IF', 5643100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5644100425deSJoe Perches $fix) { 5645100425deSJoe Perches my $do_fix = 1; 5646100425deSJoe Perches my $leading_tabs = ""; 5647100425deSJoe Perches my $new_leading_tabs = ""; 5648100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5649100425deSJoe Perches $leading_tabs = $1; 5650100425deSJoe Perches } else { 5651100425deSJoe Perches $do_fix = 0; 5652100425deSJoe Perches } 5653100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5654100425deSJoe Perches $new_leading_tabs = $1; 5655100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5656100425deSJoe Perches $do_fix = 0; 5657100425deSJoe Perches } 5658100425deSJoe Perches } else { 5659100425deSJoe Perches $do_fix = 0; 5660100425deSJoe Perches } 5661100425deSJoe Perches if ($do_fix) { 5662100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5663100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5664100425deSJoe Perches } 5665100425deSJoe Perches } 56664c432a8fSGreg Kroah-Hartman } 56674c432a8fSGreg Kroah-Hartman } 5668f0a594c1SAndy Whitcroft 5669ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5670ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5671ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5672ebfdc409SJoe Perches (defined $1 || defined $3) && 5673ebfdc409SJoe Perches $linenr > 3) { 5674ebfdc409SJoe Perches my $testval = $2; 5675ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5676ebfdc409SJoe Perches 5677ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5678ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5679ebfdc409SJoe Perches 5680e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 5681e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 5682ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5683ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5684ebfdc409SJoe Perches } 5685ebfdc409SJoe Perches } 5686ebfdc409SJoe Perches 5687f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5688dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5689f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5690f78d98f6SJoe Perches my $level = $1; 5691f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5692f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5693f78d98f6SJoe Perches $fix) { 5694f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5695f78d98f6SJoe Perches } 5696f78d98f6SJoe Perches } 5697f78d98f6SJoe Perches 569845c55e92SJoe Perches# check for logging continuations 569945c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 570045c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 570145c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 570245c55e92SJoe Perches } 570345c55e92SJoe Perches 5704abb08a53SJoe Perches# check for mask then right shift without a parentheses 57055b57980dSJoe Perches if ($perl_version_ok && 5706abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5707abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5708abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5709abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5710abb08a53SJoe Perches } 5711abb08a53SJoe Perches 5712b75ac618SJoe Perches# check for pointer comparisons to NULL 57135b57980dSJoe Perches if ($perl_version_ok) { 5714b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5715b75ac618SJoe Perches my $val = $1; 5716b75ac618SJoe Perches my $equal = "!"; 5717b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5718b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5719b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5720b75ac618SJoe Perches $fix) { 5721b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5722b75ac618SJoe Perches } 5723b75ac618SJoe Perches } 5724b75ac618SJoe Perches } 5725b75ac618SJoe Perches 57268716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 57278716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 57288716de38SJoe Perches my $attr = $1; 57298716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 57308716de38SJoe Perches my $ptr = $1; 57318716de38SJoe Perches my $var = $2; 57328716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 57338716de38SJoe Perches ERROR("MISPLACED_INIT", 57348716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 57358716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 57368716de38SJoe Perches WARN("MISPLACED_INIT", 57378716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 57388716de38SJoe Perches $fix) { 5739194f66fcSJoe 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; 57408716de38SJoe Perches } 57418716de38SJoe Perches } 57428716de38SJoe Perches } 57438716de38SJoe Perches 5744e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5745e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5746e970b884SJoe Perches my $attr = $1; 5747e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5748e970b884SJoe Perches my $attr_prefix = $1; 5749e970b884SJoe Perches my $attr_type = $2; 5750e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5751e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5752e970b884SJoe Perches $fix) { 5753194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5754e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5755e970b884SJoe Perches } 5756e970b884SJoe Perches } 5757e970b884SJoe Perches 5758e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5759e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5760e970b884SJoe Perches my $attr = $1; 5761e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5762e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5763e970b884SJoe Perches $fix) { 5764194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5765e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5766e970b884SJoe Perches $lead = rtrim($1); 5767e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5768e970b884SJoe Perches $lead = "${lead}const "; 5769194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5770e970b884SJoe Perches } 5771e970b884SJoe Perches } 5772e970b884SJoe Perches 5773c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5774c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5775c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5776c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5777c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5778c17893c7SJoe Perches $fix) { 5779c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5780c17893c7SJoe Perches } 5781c17893c7SJoe Perches } 5782c17893c7SJoe Perches 5783fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5784fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5785fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5786fbdb8138SJoe Perches my $constant_func = $1; 5787fbdb8138SJoe Perches my $func = $constant_func; 5788fbdb8138SJoe Perches $func =~ s/^__constant_//; 5789fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5790fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5791fbdb8138SJoe Perches $fix) { 5792194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5793fbdb8138SJoe Perches } 5794fbdb8138SJoe Perches } 5795fbdb8138SJoe Perches 57961a15a250SPatrick Pannuto# prefer usleep_range over udelay 579737581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 579843c1d77cSJoe Perches my $delay = $1; 57991a15a250SPatrick Pannuto # ignore udelay's < 10, however 580043c1d77cSJoe Perches if (! ($delay < 10) ) { 5801000d1cc1SJoe Perches CHK("USLEEP_RANGE", 5802458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 580343c1d77cSJoe Perches } 580443c1d77cSJoe Perches if ($delay > 2000) { 580543c1d77cSJoe Perches WARN("LONG_UDELAY", 580643c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 58071a15a250SPatrick Pannuto } 58081a15a250SPatrick Pannuto } 58091a15a250SPatrick Pannuto 581009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 581109ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 581209ef8725SPatrick Pannuto if ($1 < 20) { 5813000d1cc1SJoe Perches WARN("MSLEEP", 5814458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 581509ef8725SPatrick Pannuto } 581609ef8725SPatrick Pannuto } 581709ef8725SPatrick Pannuto 581836ec1939SJoe Perches# check for comparisons of jiffies 581936ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 582036ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 582136ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 582236ec1939SJoe Perches } 582336ec1939SJoe Perches 58249d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 58259d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 58269d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 58279d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 58289d7a34a5SJoe Perches } 58299d7a34a5SJoe Perches 583000df344fSAndy Whitcroft# warn about #ifdefs in C files 5831c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 583200df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 583300df344fSAndy Whitcroft# print "$herecurr"; 583400df344fSAndy Whitcroft# $clean = 0; 583500df344fSAndy Whitcroft# } 583600df344fSAndy Whitcroft 583722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5838c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 58393705ce5bSJoe Perches if (ERROR("SPACING", 58403705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 58413705ce5bSJoe Perches $fix) { 5842194f66fcSJoe Perches $fixed[$fixlinenr] =~ 58433705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 58443705ce5bSJoe Perches } 58453705ce5bSJoe Perches 584622f2a2efSAndy Whitcroft } 584722f2a2efSAndy Whitcroft 58484a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5849171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5850171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 58514a0df2efSAndy Whitcroft my $which = $1; 58524a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5853000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5854000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 58554a0df2efSAndy Whitcroft } 58564a0df2efSAndy Whitcroft } 58574a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5858402c2553SMichael S. Tsirkin 5859402c2553SMichael S. Tsirkin my $barriers = qr{ 5860402c2553SMichael S. Tsirkin mb| 5861402c2553SMichael S. Tsirkin rmb| 5862402c2553SMichael S. Tsirkin wmb| 5863402c2553SMichael S. Tsirkin read_barrier_depends 5864402c2553SMichael S. Tsirkin }x; 5865402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5866402c2553SMichael S. Tsirkin mb__before_atomic| 5867402c2553SMichael S. Tsirkin mb__after_atomic| 5868402c2553SMichael S. Tsirkin store_release| 5869402c2553SMichael S. Tsirkin load_acquire| 5870402c2553SMichael S. Tsirkin store_mb| 5871402c2553SMichael S. Tsirkin (?:$barriers) 5872402c2553SMichael S. Tsirkin }x; 5873402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5874402c2553SMichael S. Tsirkin (?:$barriers)| 587543e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 587643e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5877402c2553SMichael S. Tsirkin }x; 5878402c2553SMichael S. Tsirkin 5879402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 58804a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5881c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5882000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 58834a0df2efSAndy Whitcroft } 58844a0df2efSAndy Whitcroft } 58853ad81779SPaul E. McKenney 5886f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5887f4073b0fSMichael S. Tsirkin 5888f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5889f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5890f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5891f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5892f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5893f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5894f4073b0fSMichael S. Tsirkin } 5895f4073b0fSMichael S. Tsirkin 5896cb426e99SJoe Perches# check for waitqueue_active without a comment. 5897cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5898cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5899cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5900cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5901cb426e99SJoe Perches } 5902cb426e99SJoe Perches } 59033ad81779SPaul E. McKenney 590491db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends 590591db2592SPaul E. McKenney if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { 590691db2592SPaul E. McKenney WARN("READ_BARRIER_DEPENDS", 590791db2592SPaul E. McKenney "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); 590891db2592SPaul E. McKenney } 590991db2592SPaul E. McKenney 59104a0df2efSAndy Whitcroft# check of hardware specific defines 5911c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5912000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5913000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 59140a920b5bSAndy Whitcroft } 5915653d4876SAndy Whitcroft 5916596ed45bSJoe Perches# check that the storage class is not after a type 5917596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5918000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5919596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5920596ed45bSJoe Perches } 5921596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5922596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5923596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5924596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5925596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5926596ed45bSJoe Perches WARN("STORAGE_CLASS", 5927596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5928d4977c78STobias Klauser } 5929d4977c78STobias Klauser 5930de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5931de7d4f0eSAndy Whitcroft# storage class and type. 59329c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 59339c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5934000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5935000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5936de7d4f0eSAndy Whitcroft } 5937de7d4f0eSAndy Whitcroft 59388905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 59392b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59402b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5941d5e616fcSJoe Perches if (WARN("INLINE", 5942d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5943d5e616fcSJoe Perches $fix) { 5944194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5945d5e616fcSJoe Perches 5946d5e616fcSJoe Perches } 59478905a67cSAndy Whitcroft } 59488905a67cSAndy Whitcroft 59493d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 59502b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59512b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5952000d1cc1SJoe Perches WARN("PREFER_PACKED", 5953000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 59543d130fd0SJoe Perches } 59553d130fd0SJoe Perches 595639b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 59572b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59582b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5959000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5960000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 596139b7e287SJoe Perches } 596239b7e287SJoe Perches 5963462811d9SJoe Perches# Check for __attribute__ section, prefer __section 5964462811d9SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5965462811d9SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { 5966462811d9SJoe Perches my $old = substr($rawline, $-[1], $+[1] - $-[1]); 5967462811d9SJoe Perches my $new = substr($old, 1, -1); 5968462811d9SJoe Perches if (WARN("PREFER_SECTION", 5969462811d9SJoe Perches "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && 5970462811d9SJoe Perches $fix) { 5971462811d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; 5972462811d9SJoe Perches } 5973462811d9SJoe Perches } 5974462811d9SJoe Perches 59755f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 59762b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59772b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5978d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5979d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5980d5e616fcSJoe Perches $fix) { 5981194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5982d5e616fcSJoe Perches 5983d5e616fcSJoe Perches } 59845f14d3bdSJoe Perches } 59855f14d3bdSJoe Perches 59866061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 59872b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59882b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5989d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5990d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5991d5e616fcSJoe Perches $fix) { 5992194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5993d5e616fcSJoe Perches } 59946061d949SJoe Perches } 59956061d949SJoe Perches 5996619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 59975b57980dSJoe Perches if ($perl_version_ok && 5998619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5999619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 6000619a908aSJoe Perches $line =~ /\b__weak\b/)) { 6001619a908aSJoe Perches ERROR("WEAK_DECLARATION", 6002619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 6003619a908aSJoe Perches } 6004619a908aSJoe Perches 6005fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 6006e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 6007fd39f904STomas Winkler $realfile !~ m@\btools/@ && 6008e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 6009e6176fa4SJoe Perches my $type = $1; 6010e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 6011e6176fa4SJoe Perches $type = $1; 6012e6176fa4SJoe Perches my $kernel_type = 'u'; 6013e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 6014e6176fa4SJoe Perches $type =~ /(\d+)/; 6015e6176fa4SJoe Perches $kernel_type .= $1; 6016e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 6017e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 6018e6176fa4SJoe Perches $fix) { 6019e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 6020e6176fa4SJoe Perches } 6021e6176fa4SJoe Perches } 6022e6176fa4SJoe Perches } 6023e6176fa4SJoe Perches 6024938224b5SJoe Perches# check for cast of C90 native int or longer types constants 6025938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 6026938224b5SJoe Perches my $cast = $1; 6027938224b5SJoe Perches my $const = $2; 6028938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 6029938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 6030938224b5SJoe Perches $fix) { 6031938224b5SJoe Perches my $suffix = ""; 6032938224b5SJoe Perches my $newconst = $const; 6033938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 6034938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6035938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6036938224b5SJoe Perches $suffix .= 'LL'; 6037938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6038938224b5SJoe Perches $suffix .= 'L'; 6039938224b5SJoe Perches } 6040938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6041938224b5SJoe Perches } 6042938224b5SJoe Perches } 6043938224b5SJoe Perches 60448f53a9b8SJoe Perches# check for sizeof(&) 60458f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6046000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6047000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 60488f53a9b8SJoe Perches } 60498f53a9b8SJoe Perches 605066c80b60SJoe Perches# check for sizeof without parenthesis 605166c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6052d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6053d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6054d5e616fcSJoe Perches $fix) { 6055194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6056d5e616fcSJoe Perches } 605766c80b60SJoe Perches } 605866c80b60SJoe Perches 605988982feaSJoe Perches# check for struct spinlock declarations 606088982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 606188982feaSJoe Perches WARN("USE_SPINLOCK_T", 606288982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 606388982feaSJoe Perches } 606488982feaSJoe Perches 6065a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 606606668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6067a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6068caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6069caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6070d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6071d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6072d5e616fcSJoe Perches $fix) { 6073194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6074d5e616fcSJoe Perches } 6075a6962d72SJoe Perches } 6076a6962d72SJoe Perches } 6077a6962d72SJoe Perches 60780b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 60795b57980dSJoe Perches if ($perl_version_ok && 60800b523769SJoe Perches defined $stat && 60810b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 60820b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6083e3c6bc95STobin C. Harding my $stat_real; 6084e3c6bc95STobin C. Harding 60850b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 60860b523769SJoe Perches $lc = $lc + $linenr; 60870b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6088ffe07513SJoe Perches my $specifier; 6089ffe07513SJoe Perches my $extension; 60903bd32d6aSSakari Ailus my $qualifier; 6091ffe07513SJoe Perches my $bad_specifier = ""; 60920b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 60930b523769SJoe Perches $fmt =~ s/%%//g; 6094e3c6bc95STobin C. Harding 60953bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6096e3c6bc95STobin C. Harding $specifier = $1; 6097e3c6bc95STobin C. Harding $extension = $2; 60983bd32d6aSSakari Ailus $qualifier = $3; 6099361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 61003bd32d6aSSakari Ailus ($extension eq "f" && 61013bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6102e3c6bc95STobin C. Harding $bad_specifier = $specifier; 61030b523769SJoe Perches last; 61040b523769SJoe Perches } 6105e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6106e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6107e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 61080b523769SJoe Perches } 6109e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6110e3c6bc95STobin 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"); 6111e3c6bc95STobin C. Harding } 6112e3c6bc95STobin C. Harding } 6113e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 61142a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 61151df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 61161df7338aSSergey Senozhatsky my $use = ""; 6117e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 61181df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6119e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 61201df7338aSSergey Senozhatsky } 61212a9f9d85STobin C. Harding 61220b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6123e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6124e3c6bc95STobin C. Harding } 61250b523769SJoe Perches } 61260b523769SJoe Perches } 61270b523769SJoe Perches 6128554e165cSAndy Whitcroft# Check for misused memsets 61295b57980dSJoe Perches if ($perl_version_ok && 6130d1fe9c09SJoe Perches defined $stat && 61319e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6132554e165cSAndy Whitcroft 6133d7c76ba7SJoe Perches my $ms_addr = $2; 6134d1fe9c09SJoe Perches my $ms_val = $7; 6135d1fe9c09SJoe Perches my $ms_size = $12; 6136d7c76ba7SJoe Perches 6137554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6138554e165cSAndy Whitcroft ERROR("MEMSET", 6139d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6140554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6141554e165cSAndy Whitcroft WARN("MEMSET", 6142d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6143d7c76ba7SJoe Perches } 6144d7c76ba7SJoe Perches } 6145d7c76ba7SJoe Perches 614698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 61475b57980dSJoe Perches# if ($perl_version_ok && 6148f333195dSJoe Perches# defined $stat && 6149f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6150f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6151f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6152f333195dSJoe Perches# $fix) { 6153f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6154f333195dSJoe Perches# } 6155f333195dSJoe Perches# } 615698a9bba5SJoe Perches 6157b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 61585b57980dSJoe Perches# if ($perl_version_ok && 6159f333195dSJoe Perches# defined $stat && 6160f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6161f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6162f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6163f333195dSJoe Perches# } 6164b6117d17SMateusz Kulikowski 61658617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 61668617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 61675b57980dSJoe Perches# if ($perl_version_ok && 6168f333195dSJoe Perches# defined $stat && 6169f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6170f333195dSJoe Perches# 6171f333195dSJoe Perches# my $ms_val = $7; 6172f333195dSJoe Perches# 6173f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6174f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6175f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6176f333195dSJoe Perches# $fix) { 6177f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6178f333195dSJoe Perches# } 6179f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6180f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6181f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6182f333195dSJoe Perches# $fix) { 6183f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6184f333195dSJoe Perches# } 6185f333195dSJoe Perches# } 6186f333195dSJoe Perches# } 61878617cd09SMateusz Kulikowski 6188d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 61895b57980dSJoe Perches if ($perl_version_ok && 6190d1fe9c09SJoe Perches defined $stat && 6191d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6192d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6193d7c76ba7SJoe Perches my $call = $1; 6194d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6195d7c76ba7SJoe Perches my $arg1 = $3; 6196d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6197d1fe9c09SJoe Perches my $arg2 = $8; 6198d7c76ba7SJoe Perches my $cast; 6199d7c76ba7SJoe Perches 6200d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6201d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6202d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6203d7c76ba7SJoe Perches $cast = $cast1; 6204d7c76ba7SJoe Perches } else { 6205d7c76ba7SJoe Perches $cast = $cast2; 6206d7c76ba7SJoe Perches } 6207d7c76ba7SJoe Perches WARN("MINMAX", 6208d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6209554e165cSAndy Whitcroft } 6210554e165cSAndy Whitcroft } 6211554e165cSAndy Whitcroft 62124a273195SJoe Perches# check usleep_range arguments 62135b57980dSJoe Perches if ($perl_version_ok && 62144a273195SJoe Perches defined $stat && 62154a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 62164a273195SJoe Perches my $min = $1; 62174a273195SJoe Perches my $max = $7; 62184a273195SJoe Perches if ($min eq $max) { 62194a273195SJoe Perches WARN("USLEEP_RANGE", 6220458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 62214a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 62224a273195SJoe Perches $min > $max) { 62234a273195SJoe Perches WARN("USLEEP_RANGE", 6224458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 62254a273195SJoe Perches } 62264a273195SJoe Perches } 62274a273195SJoe Perches 6228823b794cSJoe Perches# check for naked sscanf 62295b57980dSJoe Perches if ($perl_version_ok && 6230823b794cSJoe Perches defined $stat && 62316c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6232823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6233823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6234823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6235823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6236823b794cSJoe Perches $lc = $lc + $linenr; 62372a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6238823b794cSJoe Perches WARN("NAKED_SSCANF", 6239823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6240823b794cSJoe Perches } 6241823b794cSJoe Perches 6242afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 62435b57980dSJoe Perches if ($perl_version_ok && 6244afc819abSJoe Perches defined $stat && 6245afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6246afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6247afc819abSJoe Perches $lc = $lc + $linenr; 62482a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6249afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6250afc819abSJoe Perches my $format = $6; 6251afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6252afc819abSJoe Perches if ($count == 1 && 6253afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6254afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6255afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6256afc819abSJoe Perches } 6257afc819abSJoe Perches } 6258afc819abSJoe Perches } 6259afc819abSJoe Perches 626070dc8a48SJoe Perches# check for new externs in .h files. 626170dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 626270dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6263d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 626470dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 626570dc8a48SJoe Perches $fix) { 6266194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 626770dc8a48SJoe Perches } 626870dc8a48SJoe Perches } 626970dc8a48SJoe Perches 6270de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6271171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6272c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6273171ae1a4SAndy Whitcroft { 6274c45dcabdSAndy Whitcroft my $function_name = $1; 6275c45dcabdSAndy Whitcroft my $paren_space = $2; 6276171ae1a4SAndy Whitcroft 6277171ae1a4SAndy Whitcroft my $s = $stat; 6278171ae1a4SAndy Whitcroft if (defined $cond) { 6279171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6280171ae1a4SAndy Whitcroft } 6281c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 6282c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 6283c45dcabdSAndy Whitcroft { 6284000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6285000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6286de7d4f0eSAndy Whitcroft } 6287de7d4f0eSAndy Whitcroft 6288171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6289000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6290000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6291171ae1a4SAndy Whitcroft } 62929c9ba34eSAndy Whitcroft 62939c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 62949c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 62959c9ba34eSAndy Whitcroft { 6296000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6297000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6298171ae1a4SAndy Whitcroft } 6299171ae1a4SAndy Whitcroft 6300a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 630116b7f3c8SJoe Perches# while avoiding uninitialized_var(x) 6302a0ad7596SJoe Perches if (defined $stat && 630316b7f3c8SJoe Perches $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 630416b7f3c8SJoe Perches (!defined($1) || 630516b7f3c8SJoe Perches (defined($1) && $1 ne "uninitialized_var")) && 630616b7f3c8SJoe Perches $2 ne "void") { 630716b7f3c8SJoe Perches my $args = trim($2); 6308ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6309ca0d8929SJoe Perches my $arg = trim($1); 631016b7f3c8SJoe Perches if ($arg =~ /^$Type$/ && 631116b7f3c8SJoe Perches $arg !~ /enum\s+$Ident$/) { 6312ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6313ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6314ca0d8929SJoe Perches } 6315ca0d8929SJoe Perches } 6316ca0d8929SJoe Perches } 6317ca0d8929SJoe Perches 6318a0ad7596SJoe Perches# check for function definitions 63195b57980dSJoe Perches if ($perl_version_ok && 6320a0ad7596SJoe Perches defined $stat && 6321a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6322a0ad7596SJoe Perches $context_function = $1; 6323a0ad7596SJoe Perches 6324a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6325a0ad7596SJoe Perches my $ok = 0; 6326a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6327a0ad7596SJoe Perches my $herectx = $here . "\n"; 6328a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6329a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6330a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6331a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6332a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6333a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6334a0ad7596SJoe Perches } 6335a0ad7596SJoe Perches if (!$ok) { 6336a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6337a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6338a0ad7596SJoe Perches } 6339a0ad7596SJoe Perches } 6340a0ad7596SJoe Perches 6341de7d4f0eSAndy Whitcroft# checks for new __setup's 6342de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6343de7d4f0eSAndy Whitcroft my $name = $1; 6344de7d4f0eSAndy Whitcroft 6345de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6346000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 63478c27ceffSMauro Carvalho Chehab "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); 6348de7d4f0eSAndy Whitcroft } 6349653d4876SAndy Whitcroft } 63509c0ca6f9SAndy Whitcroft 6351e29a70f1SJoe Perches# check for pointless casting of alloc functions 6352e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6353000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6354000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 63559c0ca6f9SAndy Whitcroft } 635613214adfSAndy Whitcroft 6357a640d25cSJoe Perches# alloc style 6358a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 63595b57980dSJoe Perches if ($perl_version_ok && 6360e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6361a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6362a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6363a640d25cSJoe Perches } 6364a640d25cSJoe Perches 636560a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 63665b57980dSJoe Perches if ($perl_version_ok && 63671b4a2ed4SJoe Perches defined $stat && 63681b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 636960a55369SJoe Perches my $oldfunc = $3; 637060a55369SJoe Perches my $a1 = $4; 637160a55369SJoe Perches my $a2 = $10; 637260a55369SJoe Perches my $newfunc = "kmalloc_array"; 637360a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 637460a55369SJoe Perches my $r1 = $a1; 637560a55369SJoe Perches my $r2 = $a2; 637660a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 637760a55369SJoe Perches $r1 = $a2; 637860a55369SJoe Perches $r2 = $a1; 637960a55369SJoe Perches } 6380e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6381e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 63821b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6383e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6384e3d95a2aSTobin C. Harding 6385e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 63861b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 63871b4a2ed4SJoe Perches $cnt == 1 && 6388e367455aSJoe Perches $fix) { 6389194f66fcSJoe 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; 639060a55369SJoe Perches } 639160a55369SJoe Perches } 639260a55369SJoe Perches } 639360a55369SJoe Perches 6394972fdea2SJoe Perches# check for krealloc arg reuse 63955b57980dSJoe Perches if ($perl_version_ok && 63964cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 63974cab63ceSJoe Perches $1 eq $3) { 6398972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6399972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6400972fdea2SJoe Perches } 6401972fdea2SJoe Perches 64025ce59ae0SJoe Perches# check for alloc argument mismatch 64035ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 64045ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 64055ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 64065ce59ae0SJoe Perches } 64075ce59ae0SJoe Perches 6408caf2a54fSJoe Perches# check for multiple semicolons 6409caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6410d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6411d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6412d5e616fcSJoe Perches $fix) { 6413194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6414d5e616fcSJoe Perches } 6415d1e2ad07SJoe Perches } 6416d1e2ad07SJoe Perches 6417cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6418cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6419cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 64200ab90191SJoe Perches my $ull = ""; 64210ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 64220ab90191SJoe Perches if (CHK("BIT_MACRO", 64230ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 64240ab90191SJoe Perches $fix) { 64250ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 64260ab90191SJoe Perches } 64270ab90191SJoe Perches } 64280ab90191SJoe Perches 64292d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 64302d632745SJoe 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*$/) { 64312d632745SJoe Perches my $config = $1; 64322d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 64332d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 64342d632745SJoe Perches $fix) { 64352d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 64362d632745SJoe Perches } 64372d632745SJoe Perches } 64382d632745SJoe Perches 6439e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6440c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6441c34c09a8SJoe Perches my $has_break = 0; 6442c34c09a8SJoe Perches my $has_statement = 0; 6443c34c09a8SJoe Perches my $count = 0; 6444c34c09a8SJoe Perches my $prevline = $linenr; 6445e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6446c34c09a8SJoe Perches $prevline--; 6447c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6448c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6449c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6450c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6451c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6452c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6453c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6454c34c09a8SJoe Perches $has_statement = 1; 6455c34c09a8SJoe Perches $count++; 6456258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6457c34c09a8SJoe Perches } 6458c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6459c34c09a8SJoe Perches WARN("MISSING_BREAK", 6460224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6461c34c09a8SJoe Perches } 6462c34c09a8SJoe Perches } 6463c34c09a8SJoe Perches 6464f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 6465f36d3eb8SJoe Perches my @fallthroughs = ( 6466f36d3eb8SJoe Perches 'fallthrough', 6467f36d3eb8SJoe Perches '@fallthrough@', 6468f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 6469f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 6470f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 6471f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6472f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6473f36d3eb8SJoe Perches ); 6474f36d3eb8SJoe Perches if ($raw_comment ne '') { 6475f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 6476f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 6477f36d3eb8SJoe Perches my $msg_level = \&WARN; 6478f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 6479f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 6480f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 6481f36d3eb8SJoe Perches last; 6482f36d3eb8SJoe Perches } 6483f36d3eb8SJoe Perches } 6484f36d3eb8SJoe Perches } 6485f36d3eb8SJoe Perches 6486d1e2ad07SJoe Perches# check for switch/default statements without a break; 64875b57980dSJoe Perches if ($perl_version_ok && 6488d1e2ad07SJoe Perches defined $stat && 6489d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6490d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6491e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6492e3d95a2aSTobin C. Harding 6493d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6494d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6495caf2a54fSJoe Perches } 6496caf2a54fSJoe Perches 649713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6498d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6499d5e616fcSJoe Perches if (WARN("USE_FUNC", 6500d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6501d5e616fcSJoe Perches $fix) { 6502194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6503d5e616fcSJoe Perches } 650413214adfSAndy Whitcroft } 6505773647a0SAndy Whitcroft 650662ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 650762ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 650862ec818fSJoe Perches ERROR("DATE_TIME", 650962ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 651062ec818fSJoe Perches } 651162ec818fSJoe Perches 65122c92488aSJoe Perches# check for use of yield() 65132c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 65142c92488aSJoe Perches WARN("YIELD", 65152c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 65162c92488aSJoe Perches } 65172c92488aSJoe Perches 6518179f8f40SJoe Perches# check for comparisons against true and false 6519179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6520179f8f40SJoe Perches my $lead = $1; 6521179f8f40SJoe Perches my $arg = $2; 6522179f8f40SJoe Perches my $test = $3; 6523179f8f40SJoe Perches my $otype = $4; 6524179f8f40SJoe Perches my $trail = $5; 6525179f8f40SJoe Perches my $op = "!"; 6526179f8f40SJoe Perches 6527179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6528179f8f40SJoe Perches 6529179f8f40SJoe Perches my $type = lc($otype); 6530179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6531179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6532179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6533179f8f40SJoe Perches $op = ""; 6534179f8f40SJoe Perches } 6535179f8f40SJoe Perches 6536179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6537179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6538179f8f40SJoe Perches 6539179f8f40SJoe Perches## maybe suggesting a correct construct would better 6540179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6541179f8f40SJoe Perches 6542179f8f40SJoe Perches } 6543179f8f40SJoe Perches } 6544179f8f40SJoe Perches 65454882720bSThomas Gleixner# check for semaphores initialized locked 65464882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6547000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6548000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6549773647a0SAndy Whitcroft } 65506712d858SJoe Perches 655167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 655267d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6553000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 655467d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6555773647a0SAndy Whitcroft } 65566712d858SJoe Perches 6557ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6558f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6559000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6560ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6561f3db6639SMichael Ellerman } 65626712d858SJoe Perches 65633d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 65643d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 65653d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 65663d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 65673d709ab5SPaul E. McKenney } 65683d709ab5SPaul E. McKenney 65699189c7e7SJoe Perches# check for deprecated apis 65709189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 65719189c7e7SJoe Perches my $deprecated_api = $1; 65729189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 65739189c7e7SJoe Perches WARN("DEPRECATED_API", 65749189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 65759189c7e7SJoe Perches } 65769189c7e7SJoe Perches 65770f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6578d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 65796903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6580d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6581000d1cc1SJoe Perches WARN("CONST_STRUCT", 6582d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 65832b6db5cbSAndy Whitcroft } 6584773647a0SAndy Whitcroft 6585773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6586773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6587773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6588c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6589c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6590171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6591171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6592171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6593773647a0SAndy Whitcroft { 6594000d1cc1SJoe Perches WARN("NR_CPUS", 6595000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6596773647a0SAndy Whitcroft } 65979c9ba34eSAndy Whitcroft 659852ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 659952ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 660052ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 660152ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 660252ea8506SJoe Perches } 660352ea8506SJoe Perches 6604acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 66055b57980dSJoe Perches if ($perl_version_ok && 6606acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6607acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6608acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6609acd9362cSJoe Perches } 6610acd9362cSJoe Perches 6611de3f186fSDenis Efremov# nested likely/unlikely calls 6612de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 6613de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 6614de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 6615de3f186fSDenis Efremov } 6616de3f186fSDenis Efremov 6617691d77b6SAndy Whitcroft# whine mightly about in_atomic 6618691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6619691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6620000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6621000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6622f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6623000d1cc1SJoe Perches WARN("IN_ATOMIC", 6624000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6625691d77b6SAndy Whitcroft } 6626691d77b6SAndy Whitcroft } 66271704f47bSPeter Zijlstra 66280f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 66290f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 66300f5225b0SPeter Zijlstra ERROR("LOCKING", 66310f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 66320f5225b0SPeter Zijlstra } 66330f5225b0SPeter Zijlstra 66341704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 66351704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 66361704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 66371704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 66381704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 66391704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6640000d1cc1SJoe Perches ERROR("LOCKDEP", 6641000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 66421704f47bSPeter Zijlstra } 66431704f47bSPeter Zijlstra } 664488f8831cSDave Jones 6645b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6646b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6647000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6648000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 664988f8831cSDave Jones } 66502435880fSJoe Perches 665100180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 665200180468SJoe Perches# and whether or not function naming is typical and if 665300180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 66545b57980dSJoe Perches if ($perl_version_ok && 665500180468SJoe Perches defined $stat && 665600180468SJoe 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*\)/) { 665700180468SJoe Perches my $var = $1; 665800180468SJoe Perches my $perms = $2; 665900180468SJoe Perches my $show = $3; 666000180468SJoe Perches my $store = $4; 666100180468SJoe Perches my $octal_perms = perms_to_octal($perms); 666200180468SJoe Perches if ($show =~ /^${var}_show$/ && 666300180468SJoe Perches $store =~ /^${var}_store$/ && 666400180468SJoe Perches $octal_perms eq "0644") { 666500180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 666600180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 666700180468SJoe Perches $fix) { 666800180468SJoe 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})/; 666900180468SJoe Perches } 667000180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 667100180468SJoe Perches $store =~ /^NULL$/ && 667200180468SJoe Perches $octal_perms eq "0444") { 667300180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 667400180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 667500180468SJoe Perches $fix) { 667600180468SJoe 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})/; 667700180468SJoe Perches } 667800180468SJoe Perches } elsif ($show =~ /^NULL$/ && 667900180468SJoe Perches $store =~ /^${var}_store$/ && 668000180468SJoe Perches $octal_perms eq "0200") { 668100180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 668200180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 668300180468SJoe Perches $fix) { 668400180468SJoe 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})/; 668500180468SJoe Perches } 668600180468SJoe Perches } elsif ($octal_perms eq "0644" || 668700180468SJoe Perches $octal_perms eq "0444" || 668800180468SJoe Perches $octal_perms eq "0200") { 668900180468SJoe Perches my $newshow = "$show"; 669000180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 669100180468SJoe Perches my $newstore = $store; 669200180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 669300180468SJoe Perches my $rename = ""; 669400180468SJoe Perches if ($show ne $newshow) { 669500180468SJoe Perches $rename .= " '$show' to '$newshow'"; 669600180468SJoe Perches } 669700180468SJoe Perches if ($store ne $newstore) { 669800180468SJoe Perches $rename .= " '$store' to '$newstore'"; 669900180468SJoe Perches } 670000180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 670100180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 670200180468SJoe Perches } else { 670300180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 670400180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 670500180468SJoe Perches } 670600180468SJoe Perches } 670700180468SJoe Perches 6708515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6709515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 671073121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 671173121534SJoe Perches# specific definition of not visible in sysfs. 671273121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 671373121534SJoe Perches# use the default permissions 67145b57980dSJoe Perches if ($perl_version_ok && 6715459cf0aeSJoe Perches defined $stat && 6716515a235eSJoe Perches $line =~ /$mode_perms_search/) { 67172435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 67182435880fSJoe Perches my $func = $entry->[0]; 67192435880fSJoe Perches my $arg_pos = $entry->[1]; 67202435880fSJoe Perches 6721459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6722459cf0aeSJoe Perches $lc = $lc + $linenr; 67232a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6724459cf0aeSJoe Perches 67252435880fSJoe Perches my $skip_args = ""; 67262435880fSJoe Perches if ($arg_pos > 1) { 67272435880fSJoe Perches $arg_pos--; 67282435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 67292435880fSJoe Perches } 6730f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6731459cf0aeSJoe Perches if ($stat =~ /$test/) { 67322435880fSJoe Perches my $val = $1; 67332435880fSJoe Perches $val = $6 if ($skip_args ne ""); 673473121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 673573121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 673673121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 67372435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6738459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6739f90774e1SJoe Perches } 6740f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6741c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6742459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 67432435880fSJoe Perches } 6744459cf0aeSJoe Perches } 6745459cf0aeSJoe Perches } 6746459cf0aeSJoe Perches } 6747459cf0aeSJoe Perches 6748459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6749bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 675000180468SJoe Perches my $oval = $1; 675100180468SJoe Perches my $octal = perms_to_octal($oval); 6752f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6753459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6754f90774e1SJoe Perches $fix) { 675500180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 67562435880fSJoe Perches } 675713214adfSAndy Whitcroft } 67585a6d20ceSBjorn Andersson 67595a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 67605a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 67615a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 67625a6d20ceSBjorn Andersson my $valid_licenses = qr{ 67635a6d20ceSBjorn Andersson GPL| 67645a6d20ceSBjorn Andersson GPL\ v2| 67655a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 67665a6d20ceSBjorn Andersson Dual\ BSD/GPL| 67675a6d20ceSBjorn Andersson Dual\ MIT/GPL| 67685a6d20ceSBjorn Andersson Dual\ MPL/GPL| 67695a6d20ceSBjorn Andersson Proprietary 67705a6d20ceSBjorn Andersson }x; 67715a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 67725a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 67735a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 67745a6d20ceSBjorn Andersson } 67755a6d20ceSBjorn Andersson } 67766a8d76cbSMatteo Croce 67776a8d76cbSMatteo Croce# check for sysctl duplicate constants 67786a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 67796a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 67806a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 67816a8d76cbSMatteo Croce } 6782515a235eSJoe Perches } 678313214adfSAndy Whitcroft 678413214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 678513214adfSAndy Whitcroft # so just keep quiet. 678613214adfSAndy Whitcroft if ($#rawlines == -1) { 678713214adfSAndy Whitcroft exit(0); 67880a920b5bSAndy Whitcroft } 67890a920b5bSAndy Whitcroft 67908905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 67918905a67cSAndy Whitcroft # things that appear to be patches. 67928905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 67938905a67cSAndy Whitcroft exit(0); 67948905a67cSAndy Whitcroft } 67958905a67cSAndy Whitcroft 67968905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 67978905a67cSAndy Whitcroft # just keep quiet. 67988905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 67998905a67cSAndy Whitcroft exit(0); 68008905a67cSAndy Whitcroft } 68018905a67cSAndy Whitcroft 6802a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6803000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6804000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 68050a920b5bSAndy Whitcroft } 6806cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6807cd261496SGeert Uytterhoeven if ($signoff == 0) { 6808000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6809000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 6810cd261496SGeert Uytterhoeven } elsif (!$authorsignoff) { 6811cd261496SGeert Uytterhoeven WARN("NO_AUTHOR_SIGN_OFF", 6812cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 6813cd261496SGeert Uytterhoeven } 68140a920b5bSAndy Whitcroft } 68150a920b5bSAndy Whitcroft 6816f0a594c1SAndy Whitcroft print report_dump(); 681713214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 681813214adfSAndy Whitcroft print "$filename " if ($summary_file); 68196c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 68206c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 68216c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 68226c72ffaaSAndy Whitcroft } 68238905a67cSAndy Whitcroft 6824d2c0a235SAndy Whitcroft if ($quiet == 0) { 6825ef212196SJoe Perches # If there were any defects found and not already fixing them 6826ef212196SJoe Perches if (!$clean and !$fix) { 6827ef212196SJoe Perches print << "EOM" 6828ef212196SJoe Perches 6829ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6830ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6831ef212196SJoe PerchesEOM 6832ef212196SJoe Perches } 6833d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6834d2c0a235SAndy Whitcroft # then suggest that. 6835d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6836b0781216SMike Frysinger $rpt_cleaners = 0; 6837d8469f16SJoe Perches print << "EOM" 6838d8469f16SJoe Perches 6839d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6840d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6841d8469f16SJoe PerchesEOM 6842d2c0a235SAndy Whitcroft } 6843d2c0a235SAndy Whitcroft } 6844d2c0a235SAndy Whitcroft 6845d752fcc8SJoe Perches if ($clean == 0 && $fix && 6846d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6847d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 68489624b8d6SJoe Perches my $newfile = $filename; 68499624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 68503705ce5bSJoe Perches my $linecount = 0; 68513705ce5bSJoe Perches my $f; 68523705ce5bSJoe Perches 6853d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6854d752fcc8SJoe Perches 68553705ce5bSJoe Perches open($f, '>', $newfile) 68563705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 68573705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 68583705ce5bSJoe Perches $linecount++; 68593705ce5bSJoe Perches if ($file) { 68603705ce5bSJoe Perches if ($linecount > 3) { 68613705ce5bSJoe Perches $fixed_line =~ s/^\+//; 68623705ce5bSJoe Perches print $f $fixed_line . "\n"; 68633705ce5bSJoe Perches } 68643705ce5bSJoe Perches } else { 68653705ce5bSJoe Perches print $f $fixed_line . "\n"; 68663705ce5bSJoe Perches } 68673705ce5bSJoe Perches } 68683705ce5bSJoe Perches close($f); 68693705ce5bSJoe Perches 68703705ce5bSJoe Perches if (!$quiet) { 68713705ce5bSJoe Perches print << "EOM"; 6872d8469f16SJoe Perches 68733705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 68743705ce5bSJoe Perches 68753705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 68763705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 68773705ce5bSJoe Perches 68783705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 68793705ce5bSJoe PerchesNo warranties, expressed or implied... 68803705ce5bSJoe PerchesEOM 68813705ce5bSJoe Perches } 68823705ce5bSJoe Perches } 68833705ce5bSJoe Perches 6884d8469f16SJoe Perches if ($quiet == 0) { 6885d8469f16SJoe Perches print "\n"; 6886d8469f16SJoe Perches if ($clean == 1) { 6887d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6888d8469f16SJoe Perches } else { 6889d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 68900a920b5bSAndy Whitcroft } 68910a920b5bSAndy Whitcroft } 68920a920b5bSAndy Whitcroft return $clean; 68930a920b5bSAndy Whitcroft} 6894