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'; 6777f5b10aSHannes Eder 6877f5b10aSHannes Edersub help { 6977f5b10aSHannes Eder my ($exitcode) = @_; 7077f5b10aSHannes Eder 7177f5b10aSHannes Eder print << "EOM"; 7277f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 7377f5b10aSHannes EderVersion: $V 7477f5b10aSHannes Eder 7577f5b10aSHannes EderOptions: 7677f5b10aSHannes Eder -q, --quiet quiet 7777f5b10aSHannes Eder --no-tree run without a kernel tree 7877f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 7977f5b10aSHannes Eder --patch treat FILE as patchfile (default) 8077f5b10aSHannes Eder --emacs emacs compile window format 8177f5b10aSHannes Eder --terse one line per report 8234d8815fSJoe Perches --showfile emit diffed file position, not input file position 834a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 844a593c34SDu, Changbin single git commit with: 854a593c34SDu, Changbin <rev> 864a593c34SDu, Changbin <rev>^ 874a593c34SDu, Changbin <rev>~n 884a593c34SDu, Changbin multiple git commits with: 894a593c34SDu, Changbin <rev1>..<rev2> 904a593c34SDu, Changbin <rev1>...<rev2> 914a593c34SDu, Changbin <rev>-<count> 924a593c34SDu, Changbin git merges are ignored 9377f5b10aSHannes Eder -f, --file treat FILE as regular source file 9477f5b10aSHannes Eder --subjective, --strict enable more subjective tests 953beb42ecSJoe Perches --list-types list the possible message types 9691bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 97000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 983beb42ecSJoe Perches --show-types show the specific message type in the output 996cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 10056193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 10177f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10277f5b10aSHannes Eder --no-summary suppress the per-file summary 10377f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 10477f5b10aSHannes Eder --summary-file include the filename in summary 10577f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 10677f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 10777f5b10aSHannes Eder is all off) 10877f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 10977f5b10aSHannes Eder literally 1103705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1113705ce5bSJoe Perches If correctable single-line errors exist, create 1123705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1133705ce5bSJoe Perches with potential errors corrected to the preferred 1143705ce5bSJoe Perches checkpatch style 1159624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1169624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1179624b8d6SJoe Perches file. It's your fault if there's no backup or git 118d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 119d62a201fSDave Hansen runtime errors. 120ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 121f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 122ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 12375ad8c57SJerome Forissier --typedefsfile Read additional types from this file 124737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 125737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 12677f5b10aSHannes Eder -h, --help, --version display this help and exit 12777f5b10aSHannes Eder 12877f5b10aSHannes EderWhen FILE is - read standard input. 12977f5b10aSHannes EderEOM 13077f5b10aSHannes Eder 13177f5b10aSHannes Eder exit($exitcode); 13277f5b10aSHannes Eder} 13377f5b10aSHannes Eder 1343beb42ecSJoe Perchessub uniq { 1353beb42ecSJoe Perches my %seen; 1363beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1373beb42ecSJoe Perches} 1383beb42ecSJoe Perches 1393beb42ecSJoe Perchessub list_types { 1403beb42ecSJoe Perches my ($exitcode) = @_; 1413beb42ecSJoe Perches 1423beb42ecSJoe Perches my $count = 0; 1433beb42ecSJoe Perches 1443beb42ecSJoe Perches local $/ = undef; 1453beb42ecSJoe Perches 1463beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1473beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1483beb42ecSJoe Perches 1493beb42ecSJoe Perches my $text = <$script>; 1503beb42ecSJoe Perches close($script); 1513beb42ecSJoe Perches 1523beb42ecSJoe Perches my @types = (); 1530547fa58SJean Delvare # Also catch when type or level is passed through a variable 1540547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1553beb42ecSJoe Perches push (@types, $_); 1563beb42ecSJoe Perches } 1573beb42ecSJoe Perches @types = sort(uniq(@types)); 1583beb42ecSJoe Perches print("#\tMessage type\n\n"); 1593beb42ecSJoe Perches foreach my $type (@types) { 1603beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1613beb42ecSJoe Perches } 1623beb42ecSJoe Perches 1633beb42ecSJoe Perches exit($exitcode); 1643beb42ecSJoe Perches} 1653beb42ecSJoe Perches 166000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 167000d1cc1SJoe Perchesif (-f $conf) { 168000d1cc1SJoe Perches my @conf_args; 169000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 170000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 171000d1cc1SJoe Perches 172000d1cc1SJoe Perches while (<$conffile>) { 173000d1cc1SJoe Perches my $line = $_; 174000d1cc1SJoe Perches 175000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 176000d1cc1SJoe Perches $line =~ s/^\s*//g; 177000d1cc1SJoe Perches $line =~ s/\s+/ /g; 178000d1cc1SJoe Perches 179000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 180000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 181000d1cc1SJoe Perches 182000d1cc1SJoe Perches my @words = split(" ", $line); 183000d1cc1SJoe Perches foreach my $word (@words) { 184000d1cc1SJoe Perches last if ($word =~ m/^#/); 185000d1cc1SJoe Perches push (@conf_args, $word); 186000d1cc1SJoe Perches } 187000d1cc1SJoe Perches } 188000d1cc1SJoe Perches close($conffile); 189000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 190000d1cc1SJoe Perches} 191000d1cc1SJoe Perches 192737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 193737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 194737c0767SJohn Brooksforeach (@ARGV) { 195737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 196737c0767SJohn Brooks $_ = "--color=$color"; 197737c0767SJohn Brooks } 198737c0767SJohn Brooks} 199737c0767SJohn Brooks 2000a920b5bSAndy WhitcroftGetOptions( 2016c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2020a920b5bSAndy Whitcroft 'tree!' => \$tree, 2030a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2040a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2056c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2068905a67cSAndy Whitcroft 'terse!' => \$terse, 20734d8815fSJoe Perches 'showfile!' => \$showfile, 20877f5b10aSHannes Eder 'f|file!' => \$file, 2094a593c34SDu, Changbin 'g|git!' => \$git, 2106c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2116c72ffaaSAndy Whitcroft 'strict!' => \$check, 212000d1cc1SJoe Perches 'ignore=s' => \@ignore, 21391bfe484SJoe Perches 'types=s' => \@use, 214000d1cc1SJoe Perches 'show-types!' => \$show_types, 2153beb42ecSJoe Perches 'list-types!' => \$list_types, 2166cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 21756193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 2186c72ffaaSAndy Whitcroft 'root=s' => \$root, 2198905a67cSAndy Whitcroft 'summary!' => \$summary, 2208905a67cSAndy Whitcroft 'mailback!' => \$mailback, 22113214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2223705ce5bSJoe Perches 'fix!' => \$fix, 2239624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 224d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 225c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 226773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 227ebfd7d62SJoe Perches 'codespell!' => \$codespell, 228ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 22975ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 230737c0767SJohn Brooks 'color=s' => \$color, 231737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 232737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 23377f5b10aSHannes Eder 'h|help' => \$help, 23477f5b10aSHannes Eder 'version' => \$help 23577f5b10aSHannes Eder) or help(1); 23677f5b10aSHannes Eder 23777f5b10aSHannes Ederhelp(0) if ($help); 2380a920b5bSAndy Whitcroft 2393beb42ecSJoe Percheslist_types(0) if ($list_types); 2403beb42ecSJoe Perches 2419624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2422ac73b4fSJoe Perches$check_orig = $check; 2439624b8d6SJoe Perches 2440a920b5bSAndy Whitcroftmy $exit = 0; 2450a920b5bSAndy Whitcroft 2465b57980dSJoe Perchesmy $perl_version_ok = 1; 247d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2485b57980dSJoe Perches $perl_version_ok = 0; 249d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2505b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 251d62a201fSDave Hansen} 252d62a201fSDave Hansen 25345107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2540a920b5bSAndy Whitcroftif ($#ARGV < 0) { 25545107ff6SAllen Hubbe push(@ARGV, '-'); 2560a920b5bSAndy Whitcroft} 2570a920b5bSAndy Whitcroft 258737c0767SJohn Brooksif ($color =~ /^[01]$/) { 259737c0767SJohn Brooks $color = !$color; 260737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 261737c0767SJohn Brooks $color = 1; 262737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 263737c0767SJohn Brooks $color = 0; 264737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 265737c0767SJohn Brooks $color = (-t STDOUT); 266737c0767SJohn Brooks} else { 267737c0767SJohn Brooks die "Invalid color mode: $color\n"; 268737c0767SJohn Brooks} 269737c0767SJohn Brooks 27091bfe484SJoe Perchessub hash_save_array_words { 27191bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 27291bfe484SJoe Perches 27391bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 27491bfe484SJoe Perches foreach my $word (@array) { 275000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 276000d1cc1SJoe Perches $word =~ s/^\s*//g; 277000d1cc1SJoe Perches $word =~ s/\s+/ /g; 278000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 279000d1cc1SJoe Perches 280000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 281000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 282000d1cc1SJoe Perches 28391bfe484SJoe Perches $hashRef->{$word}++; 284000d1cc1SJoe Perches } 28591bfe484SJoe Perches} 28691bfe484SJoe Perches 28791bfe484SJoe Perchessub hash_show_words { 28891bfe484SJoe Perches my ($hashRef, $prefix) = @_; 28991bfe484SJoe Perches 2903c816e49SJoe Perches if (keys %$hashRef) { 291d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 29258cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 29391bfe484SJoe Perches print " $word"; 29491bfe484SJoe Perches } 295d8469f16SJoe Perches print "\n"; 29691bfe484SJoe Perches } 29791bfe484SJoe Perches} 29891bfe484SJoe Perches 29991bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 30091bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 301000d1cc1SJoe Perches 302c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 303c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3047429c690SAndy Whitcroftmy $dbg_type = 0; 305a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 306c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 30721caa13cSAndy Whitcroft ## no critic 30821caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 30921caa13cSAndy Whitcroft die "$@" if ($@); 310c2fdda0dSAndy Whitcroft} 311c2fdda0dSAndy Whitcroft 312d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 313d2c0a235SAndy Whitcroft 3148905a67cSAndy Whitcroftif ($terse) { 3158905a67cSAndy Whitcroft $emacs = 1; 3168905a67cSAndy Whitcroft $quiet++; 3178905a67cSAndy Whitcroft} 3188905a67cSAndy Whitcroft 3196c72ffaaSAndy Whitcroftif ($tree) { 3206c72ffaaSAndy Whitcroft if (defined $root) { 3216c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3226c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3236c72ffaaSAndy Whitcroft } 3246c72ffaaSAndy Whitcroft } else { 3256c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3266c72ffaaSAndy Whitcroft $root = '.'; 3276c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3286c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3296c72ffaaSAndy Whitcroft $root = $1; 3306c72ffaaSAndy Whitcroft } 3316c72ffaaSAndy Whitcroft } 3326c72ffaaSAndy Whitcroft 3336c72ffaaSAndy Whitcroft if (!defined $root) { 3340a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3350a920b5bSAndy Whitcroft exit(2); 3360a920b5bSAndy Whitcroft } 3376c72ffaaSAndy Whitcroft} 3386c72ffaaSAndy Whitcroft 3396c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3406c72ffaaSAndy Whitcroft 3412ceb532bSAndy Whitcroftour $Ident = qr{ 3422ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3432ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3442ceb532bSAndy Whitcroft }x; 3456c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3466c72ffaaSAndy Whitcroftour $Sparse = qr{ 3476c72ffaaSAndy Whitcroft __user| 3486c72ffaaSAndy Whitcroft __kernel| 3496c72ffaaSAndy Whitcroft __force| 3506c72ffaaSAndy Whitcroft __iomem| 3516c72ffaaSAndy Whitcroft __must_check| 352417495edSAndy Whitcroft __kprobes| 353165e72a6SSven Eckelmann __ref| 35433aa4597SGeert Uytterhoeven __refconst| 35533aa4597SGeert Uytterhoeven __refdata| 356ad315455SBoqun Feng __rcu| 357ad315455SBoqun Feng __private 3586c72ffaaSAndy Whitcroft }x; 359e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 360e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 361e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 362e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 363e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3648716de38SJoe Perches 36552131292SWolfram Sang# Notes to $Attribute: 36652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3676c72ffaaSAndy Whitcroftour $Attribute = qr{ 3686c72ffaaSAndy Whitcroft const| 36903f1df7dSJoe Perches __percpu| 37003f1df7dSJoe Perches __nocast| 37103f1df7dSJoe Perches __safe| 37246d832f5SMichael S. Tsirkin __bitwise| 37303f1df7dSJoe Perches __packed__| 37403f1df7dSJoe Perches __packed2__| 37503f1df7dSJoe Perches __naked| 37603f1df7dSJoe Perches __maybe_unused| 37703f1df7dSJoe Perches __always_unused| 37803f1df7dSJoe Perches __noreturn| 37903f1df7dSJoe Perches __used| 38003f1df7dSJoe Perches __cold| 381e23ef1f3SJoe Perches __pure| 38203f1df7dSJoe Perches __noclone| 38303f1df7dSJoe Perches __deprecated| 3846c72ffaaSAndy Whitcroft __read_mostly| 385c5967e98SJoe Perches __ro_after_init| 3866c72ffaaSAndy Whitcroft __kprobes| 3878716de38SJoe Perches $InitAttribute| 38824e1d81aSAndy Whitcroft ____cacheline_aligned| 38924e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3905fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3915fe3af11SAndy Whitcroft __weak 3926c72ffaaSAndy Whitcroft }x; 393c45dcabdSAndy Whitcroftour $Modifier; 39491cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 3956c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 3966c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 3976c72ffaaSAndy Whitcroft 39895e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 39995e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 40095e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 40195e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4022435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 403c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 404326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 405326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 406326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 40774349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4082435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 409326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 410447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 41123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4126c72ffaaSAndy Whitcroftour $Operators = qr{ 4136c72ffaaSAndy Whitcroft <=|>=|==|!=| 4146c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 41523f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4166c72ffaaSAndy Whitcroft }x; 4176c72ffaaSAndy Whitcroft 41891cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 41991cb5195SJoe Perches 420ab7e23f3SJoe Perchesour $BasicType; 4218905a67cSAndy Whitcroftour $NonptrType; 4221813087dSJoe Perchesour $NonptrTypeMisordered; 4238716de38SJoe Perchesour $NonptrTypeWithAttr; 4248905a67cSAndy Whitcroftour $Type; 4251813087dSJoe Perchesour $TypeMisordered; 4268905a67cSAndy Whitcroftour $Declare; 4271813087dSJoe Perchesour $DeclareMisordered; 4288905a67cSAndy Whitcroft 42915662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 43015662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 431171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 432171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 433171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 434171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 435171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 436171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 437171ae1a4SAndy Whitcroft}x; 438171ae1a4SAndy Whitcroft 43915662b3eSJoe Perchesour $UTF8 = qr{ 44015662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 44115662b3eSJoe Perches | $NON_ASCII_UTF8 44215662b3eSJoe Perches}x; 44315662b3eSJoe Perches 444e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 445021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 446021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 447021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 448021158b4SJoe Perches)}; 449e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 450fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4518ed22cadSAndy Whitcroft atomic_t 4528ed22cadSAndy Whitcroft)}; 453e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 454e6176fa4SJoe Perches $typeC99Typedefs\b| 455e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 456e6176fa4SJoe Perches $typeKernelTypedefs\b 457e6176fa4SJoe Perches)}; 4588ed22cadSAndy Whitcroft 4596d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4606d32f7a3SJoe Perches 461691e669bSJoe Perchesour $logFunctions = qr{(?x: 462758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4637d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 46487bd499aSJoe Perches TP_printk| 4656e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 466b0531722SJoe Perches panic| 46706668727SJoe Perches MODULE_[A-Z_]+| 46806668727SJoe Perches seq_vprintf|seq_printf|seq_puts 469691e669bSJoe Perches)}; 470691e669bSJoe Perches 471e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 472e29a70f1SJoe Perches (?:(?:devm_)? 473e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 474e29a70f1SJoe Perches kstrdup(?:_const)? | 475e29a70f1SJoe Perches kmemdup(?:_nul)?) | 476e29a70f1SJoe Perches (?:\w+)?alloc_skb(?:ip_align)? | 477e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 478e29a70f1SJoe Perches dma_alloc_coherent 479e29a70f1SJoe Perches)}; 480e29a70f1SJoe Perches 48120112475SJoe Perchesour $signature_tags = qr{(?xi: 48220112475SJoe Perches Signed-off-by:| 483d499480cSJorge Ramirez-Ortiz Co-developed-by:| 48420112475SJoe Perches Acked-by:| 48520112475SJoe Perches Tested-by:| 48620112475SJoe Perches Reviewed-by:| 48720112475SJoe Perches Reported-by:| 4888543ae12SMugunthan V N Suggested-by:| 48920112475SJoe Perches To:| 49020112475SJoe Perches Cc: 49120112475SJoe Perches)}; 49220112475SJoe Perches 4931813087dSJoe Perchesour @typeListMisordered = ( 4941813087dSJoe Perches qr{char\s+(?:un)?signed}, 4951813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 4961813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 4971813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 4981813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 4991813087dSJoe Perches qr{short\s+(?:un)?signed}, 5001813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5011813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5021813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5031813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5041813087dSJoe Perches qr{int\s+(?:un)?signed}, 5051813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5061813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5071813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5081813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5091813087dSJoe Perches qr{long\s+(?:un)?signed}, 5101813087dSJoe Perches); 5111813087dSJoe Perches 5128905a67cSAndy Whitcroftour @typeList = ( 5138905a67cSAndy Whitcroft qr{void}, 5140c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5150c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5160c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5170c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5180c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5190c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5200c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5210c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5220c773d9dSJoe Perches qr{(?:un)?signed}, 5238905a67cSAndy Whitcroft qr{float}, 5248905a67cSAndy Whitcroft qr{double}, 5258905a67cSAndy Whitcroft qr{bool}, 5268905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5278905a67cSAndy Whitcroft qr{union\s+$Ident}, 5288905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5298905a67cSAndy Whitcroft qr{${Ident}_t}, 5308905a67cSAndy Whitcroft qr{${Ident}_handler}, 5318905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5321813087dSJoe Perches @typeListMisordered, 5338905a67cSAndy Whitcroft); 534938224b5SJoe Perches 535938224b5SJoe Perchesour $C90_int_types = qr{(?x: 536938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 537938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 538938224b5SJoe Perches long\s+long\s+(?:un)?signed| 539938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 540938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 541938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 542938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 543938224b5SJoe Perches 544938224b5SJoe Perches long\s+int\s+(?:un)?signed| 545938224b5SJoe Perches long\s+(?:un)?signed\s+int| 546938224b5SJoe Perches long\s+(?:un)?signed| 547938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 548938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 549938224b5SJoe Perches int\s+long\s+(?:un)?signed| 550938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 551938224b5SJoe Perches 552938224b5SJoe Perches int\s+(?:un)?signed| 553938224b5SJoe Perches (?:(?:un)?signed\s+)?int 554938224b5SJoe Perches)}; 555938224b5SJoe Perches 556485ff23eSAlex Dowadour @typeListFile = (); 5578716de38SJoe Perchesour @typeListWithAttr = ( 5588716de38SJoe Perches @typeList, 5598716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5608716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5618716de38SJoe Perches); 5628716de38SJoe Perches 563c45dcabdSAndy Whitcroftour @modifierList = ( 564c45dcabdSAndy Whitcroft qr{fastcall}, 565c45dcabdSAndy Whitcroft); 566485ff23eSAlex Dowadour @modifierListFile = (); 5678905a67cSAndy Whitcroft 5682435880fSJoe Perchesour @mode_permission_funcs = ( 5692435880fSJoe Perches ["module_param", 3], 5702435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5712435880fSJoe Perches ["module_param_array_named", 5], 5722435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5732435880fSJoe Perches ["proc_create(?:_data|)", 2], 574459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 575459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 576459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 577459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 578459cf0aeSJoe Perches ["__ATTR", 2], 5792435880fSJoe Perches); 5802435880fSJoe Perches 581515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 582515a235eSJoe Perchesour $mode_perms_search = ""; 583515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 584515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 585515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 586515a235eSJoe Perches} 58700180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 588515a235eSJoe Perches 5899189c7e7SJoe Perchesour %deprecated_apis = ( 5909189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 5919189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 5929189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 5939189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 5949189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 5959189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 5969189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 5979189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 5989189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 5999189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6009189c7e7SJoe Perches); 6019189c7e7SJoe Perches 6029189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6039189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6049189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6059189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6069189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6079189c7e7SJoe Perches} 6089189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6099189c7e7SJoe Perches 610b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 611b392c64fSJoe Perches S_IWUGO | 612b392c64fSJoe Perches S_IWOTH | 613b392c64fSJoe Perches S_IRWXUGO | 614b392c64fSJoe Perches S_IALLUGO | 615b392c64fSJoe Perches 0[0-7][0-7][2367] 616b392c64fSJoe Perches}x; 617b392c64fSJoe Perches 618f90774e1SJoe Perchesour %mode_permission_string_types = ( 619f90774e1SJoe Perches "S_IRWXU" => 0700, 620f90774e1SJoe Perches "S_IRUSR" => 0400, 621f90774e1SJoe Perches "S_IWUSR" => 0200, 622f90774e1SJoe Perches "S_IXUSR" => 0100, 623f90774e1SJoe Perches "S_IRWXG" => 0070, 624f90774e1SJoe Perches "S_IRGRP" => 0040, 625f90774e1SJoe Perches "S_IWGRP" => 0020, 626f90774e1SJoe Perches "S_IXGRP" => 0010, 627f90774e1SJoe Perches "S_IRWXO" => 0007, 628f90774e1SJoe Perches "S_IROTH" => 0004, 629f90774e1SJoe Perches "S_IWOTH" => 0002, 630f90774e1SJoe Perches "S_IXOTH" => 0001, 631f90774e1SJoe Perches "S_IRWXUGO" => 0777, 632f90774e1SJoe Perches "S_IRUGO" => 0444, 633f90774e1SJoe Perches "S_IWUGO" => 0222, 634f90774e1SJoe Perches "S_IXUGO" => 0111, 635f90774e1SJoe Perches); 636f90774e1SJoe Perches 637f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 638f90774e1SJoe Perchesour $mode_perms_string_search = ""; 639f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 640f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 641f90774e1SJoe Perches $mode_perms_string_search .= $entry; 642f90774e1SJoe Perches} 64300180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 64400180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 64500180468SJoe Perches ${single_mode_perms_string_search} 64600180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 64700180468SJoe Perches}x; 64800180468SJoe Perches 64900180468SJoe Perchessub perms_to_octal { 65000180468SJoe Perches my ($string) = @_; 65100180468SJoe Perches 65200180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 65300180468SJoe Perches 65400180468SJoe Perches my $val = ""; 65500180468SJoe Perches my $oval = ""; 65600180468SJoe Perches my $to = 0; 65700180468SJoe Perches my $curpos = 0; 65800180468SJoe Perches my $lastpos = 0; 65900180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 66000180468SJoe Perches $curpos = pos($string); 66100180468SJoe Perches my $match = $2; 66200180468SJoe Perches my $omatch = $1; 66300180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 66400180468SJoe Perches $lastpos = $curpos; 66500180468SJoe Perches $to |= $mode_permission_string_types{$match}; 66600180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 66700180468SJoe Perches $val .= $match; 66800180468SJoe Perches $oval .= $omatch; 66900180468SJoe Perches } 67000180468SJoe Perches $oval =~ s/^\s*\|\s*//; 67100180468SJoe Perches $oval =~ s/\s*\|\s*$//; 67200180468SJoe Perches return sprintf("%04o", $to); 67300180468SJoe Perches} 674f90774e1SJoe Perches 6757840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6767840a94cSWolfram Sang irq| 677cdcee686SSergey Ryazanov memory| 678cdcee686SSergey Ryazanov time| 679cdcee686SSergey Ryazanov reboot 6807840a94cSWolfram Sang)}; 6817840a94cSWolfram Sang# memory.h: ARM has a custom one 6827840a94cSWolfram Sang 68366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 68466b47b4aSKees Cookmy $misspellings; 68566b47b4aSKees Cookmy %spelling_fix; 68636061e38SJoe Perches 68736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 68866b47b4aSKees Cook while (<$spelling>) { 68966b47b4aSKees Cook my $line = $_; 69066b47b4aSKees Cook 69166b47b4aSKees Cook $line =~ s/\s*\n?$//g; 69266b47b4aSKees Cook $line =~ s/^\s*//g; 69366b47b4aSKees Cook 69466b47b4aSKees Cook next if ($line =~ m/^\s*#/); 69566b47b4aSKees Cook next if ($line =~ m/^\s*$/); 69666b47b4aSKees Cook 69766b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 69866b47b4aSKees Cook 69966b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 70066b47b4aSKees Cook } 70166b47b4aSKees Cook close($spelling); 70236061e38SJoe Perches} else { 70336061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 70436061e38SJoe Perches} 70566b47b4aSKees Cook 706ebfd7d62SJoe Perchesif ($codespell) { 707ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 708ebfd7d62SJoe Perches while (<$spelling>) { 709ebfd7d62SJoe Perches my $line = $_; 710ebfd7d62SJoe Perches 711ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 712ebfd7d62SJoe Perches $line =~ s/^\s*//g; 713ebfd7d62SJoe Perches 714ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 715ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 716ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 717ebfd7d62SJoe Perches 718ebfd7d62SJoe Perches $line =~ s/,.*$//; 719ebfd7d62SJoe Perches 720ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 721ebfd7d62SJoe Perches 722ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 723ebfd7d62SJoe Perches } 724ebfd7d62SJoe Perches close($spelling); 725ebfd7d62SJoe Perches } else { 726ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 727ebfd7d62SJoe Perches } 728ebfd7d62SJoe Perches} 729ebfd7d62SJoe Perches 730ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 731ebfd7d62SJoe Perches 73275ad8c57SJerome Forissiersub read_words { 73375ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 73475ad8c57SJerome Forissier 73575ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 73675ad8c57SJerome Forissier while (<$words>) { 737bf1fa1daSJoe Perches my $line = $_; 738bf1fa1daSJoe Perches 739bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 740bf1fa1daSJoe Perches $line =~ s/^\s*//g; 741bf1fa1daSJoe Perches 742bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 743bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 744bf1fa1daSJoe Perches if ($line =~ /\s/) { 74575ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 746bf1fa1daSJoe Perches next; 747bf1fa1daSJoe Perches } 748bf1fa1daSJoe Perches 74975ad8c57SJerome Forissier $$wordsRef .= '|' if ($$wordsRef ne ""); 75075ad8c57SJerome Forissier $$wordsRef .= $line; 751bf1fa1daSJoe Perches } 75275ad8c57SJerome Forissier close($file); 75375ad8c57SJerome Forissier return 1; 754bf1fa1daSJoe Perches } 755bf1fa1daSJoe Perches 75675ad8c57SJerome Forissier return 0; 75775ad8c57SJerome Forissier} 75875ad8c57SJerome Forissier 75975ad8c57SJerome Forissiermy $const_structs = ""; 76075ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile) 76175ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 76275ad8c57SJerome Forissier 76375ad8c57SJerome Forissiermy $typeOtherTypedefs = ""; 76475ad8c57SJerome Forissierif (length($typedefsfile)) { 76575ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 76675ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 76775ad8c57SJerome Forissier} 76875ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); 76975ad8c57SJerome Forissier 7708905a67cSAndy Whitcroftsub build_types { 771485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 772485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7731813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7748716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 775c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 776ab7e23f3SJoe Perches $BasicType = qr{ 777ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 778ab7e23f3SJoe Perches (?:${all}\b) 779ab7e23f3SJoe Perches }x; 7808905a67cSAndy Whitcroft $NonptrType = qr{ 781d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 782cf655043SAndy Whitcroft (?: 7836b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 7848ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 785c45dcabdSAndy Whitcroft (?:${all}\b) 786cf655043SAndy Whitcroft ) 787c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 7888905a67cSAndy Whitcroft }x; 7891813087dSJoe Perches $NonptrTypeMisordered = qr{ 7901813087dSJoe Perches (?:$Modifier\s+|const\s+)* 7911813087dSJoe Perches (?: 7921813087dSJoe Perches (?:${Misordered}\b) 7931813087dSJoe Perches ) 7941813087dSJoe Perches (?:\s+$Modifier|\s+const)* 7951813087dSJoe Perches }x; 7968716de38SJoe Perches $NonptrTypeWithAttr = qr{ 7978716de38SJoe Perches (?:$Modifier\s+|const\s+)* 7988716de38SJoe Perches (?: 7998716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8008716de38SJoe Perches (?:$typeTypedefs\b)| 8018716de38SJoe Perches (?:${allWithAttr}\b) 8028716de38SJoe Perches ) 8038716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8048716de38SJoe Perches }x; 8058905a67cSAndy Whitcroft $Type = qr{ 806c45dcabdSAndy Whitcroft $NonptrType 8071574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 808c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8098905a67cSAndy Whitcroft }x; 8101813087dSJoe Perches $TypeMisordered = qr{ 8111813087dSJoe Perches $NonptrTypeMisordered 8121813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 8131813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8141813087dSJoe Perches }x; 81591cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8161813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8178905a67cSAndy Whitcroft} 8188905a67cSAndy Whitcroftbuild_types(); 8196c72ffaaSAndy Whitcroft 8207d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 821d1fe9c09SJoe Perches 822d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 823d1fe9c09SJoe Perches# requires at least perl version v5.10.0 824d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 825d1fe9c09SJoe Perches 826d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 8272435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 828c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 8297d2367afSJoe Perches 830f8422308SJoe Perchesour $declaration_macros = qr{(?x: 8313e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 832fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 8333d102fc0SGilad Ben-Yossef (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(| 8343d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 835f8422308SJoe Perches)}; 836f8422308SJoe Perches 8377d2367afSJoe Perchessub deparenthesize { 8387d2367afSJoe Perches my ($string) = @_; 8397d2367afSJoe Perches return "" if (!defined($string)); 8405b9553abSJoe Perches 8415b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8425b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8435b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8445b9553abSJoe Perches } 8455b9553abSJoe Perches 8467d2367afSJoe Perches $string =~ s@\s+@ @g; 8475b9553abSJoe Perches 8487d2367afSJoe Perches return $string; 8497d2367afSJoe Perches} 8507d2367afSJoe Perches 8513445686aSJoe Perchessub seed_camelcase_file { 8523445686aSJoe Perches my ($file) = @_; 8533445686aSJoe Perches 8543445686aSJoe Perches return if (!(-f $file)); 8553445686aSJoe Perches 8563445686aSJoe Perches local $/; 8573445686aSJoe Perches 8583445686aSJoe Perches open(my $include_file, '<', "$file") 8593445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8603445686aSJoe Perches my $text = <$include_file>; 8613445686aSJoe Perches close($include_file); 8623445686aSJoe Perches 8633445686aSJoe Perches my @lines = split('\n', $text); 8643445686aSJoe Perches 8653445686aSJoe Perches foreach my $line (@lines) { 8663445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8673445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8683445686aSJoe Perches $camelcase{$1} = 1; 86911ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 87011ea516aSJoe Perches $camelcase{$1} = 1; 87111ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8723445686aSJoe Perches $camelcase{$1} = 1; 8733445686aSJoe Perches } 8743445686aSJoe Perches } 8753445686aSJoe Perches} 8763445686aSJoe Perches 877cd28b119SJoe Perchesour %maintained_status = (); 878cd28b119SJoe Perches 87985b0ee18SJoe Perchessub is_maintained_obsolete { 88085b0ee18SJoe Perches my ($filename) = @_; 88185b0ee18SJoe Perches 882f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 88385b0ee18SJoe Perches 884cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 885cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 886cd28b119SJoe Perches } 88785b0ee18SJoe Perches 888cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 88985b0ee18SJoe Perches} 89085b0ee18SJoe Perches 8913b6e8ac9SJoe Perchessub is_SPDX_License_valid { 8923b6e8ac9SJoe Perches my ($license) = @_; 8933b6e8ac9SJoe Perches 89456294112SJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git")); 8953b6e8ac9SJoe Perches 89656294112SJoe Perches my $root_path = abs_path($root); 89756294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 8983b6e8ac9SJoe Perches return 0 if ($status ne ""); 8993b6e8ac9SJoe Perches return 1; 9003b6e8ac9SJoe Perches} 9013b6e8ac9SJoe Perches 9023445686aSJoe Perchesmy $camelcase_seeded = 0; 9033445686aSJoe Perchessub seed_camelcase_includes { 9043445686aSJoe Perches return if ($camelcase_seeded); 9053445686aSJoe Perches 9063445686aSJoe Perches my $files; 907c707a81dSJoe Perches my $camelcase_cache = ""; 908c707a81dSJoe Perches my @include_files = (); 909c707a81dSJoe Perches 910c707a81dSJoe Perches $camelcase_seeded = 1; 911351b2a1fSJoe Perches 9123645e328SRichard Genoud if (-e ".git") { 913dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 914351b2a1fSJoe Perches chomp $git_last_include_commit; 915c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 916c707a81dSJoe Perches } else { 917c707a81dSJoe Perches my $last_mod_date = 0; 918c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 919c707a81dSJoe Perches @include_files = split('\n', $files); 920c707a81dSJoe Perches foreach my $file (@include_files) { 921c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 922c707a81dSJoe Perches localtime((stat $file)[9])); 923c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 924c707a81dSJoe Perches } 925c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 926c707a81dSJoe Perches } 927c707a81dSJoe Perches 928c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 929c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 930c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 931351b2a1fSJoe Perches while (<$camelcase_file>) { 932351b2a1fSJoe Perches chomp; 933351b2a1fSJoe Perches $camelcase{$_} = 1; 934351b2a1fSJoe Perches } 935351b2a1fSJoe Perches close($camelcase_file); 936351b2a1fSJoe Perches 937351b2a1fSJoe Perches return; 938351b2a1fSJoe Perches } 939c707a81dSJoe Perches 9403645e328SRichard Genoud if (-e ".git") { 941dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 942c707a81dSJoe Perches @include_files = split('\n', $files); 9433445686aSJoe Perches } 944c707a81dSJoe Perches 9453445686aSJoe Perches foreach my $file (@include_files) { 9463445686aSJoe Perches seed_camelcase_file($file); 9473445686aSJoe Perches } 948351b2a1fSJoe Perches 949c707a81dSJoe Perches if ($camelcase_cache ne "") { 950351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 951c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 952c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 953351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 954351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 955351b2a1fSJoe Perches } 956351b2a1fSJoe Perches close($camelcase_file); 957351b2a1fSJoe Perches } 9583445686aSJoe Perches} 9593445686aSJoe Perches 960d311cd44SJoe Perchessub git_commit_info { 961d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 962d311cd44SJoe Perches 963d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 964d311cd44SJoe Perches 965dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 966d311cd44SJoe Perches $output =~ s/^\s*//gm; 967d311cd44SJoe Perches my @lines = split("\n", $output); 968d311cd44SJoe Perches 9690d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9700d7835fcSJoe Perches 9715a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 972d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 973d311cd44SJoe Perches# all matching commit ids, but it's very slow... 974d311cd44SJoe Perches# 975d311cd44SJoe Perches# echo "checking commits $1..." 976d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 977d311cd44SJoe Perches# while read line ; do 978d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 979d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 980d311cd44SJoe Perches# done 981d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 982948b133aSHeinrich Schuchardt $id = undef; 983d311cd44SJoe Perches } else { 984d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 985d311cd44SJoe Perches $desc = substr($lines[0], 41); 986d311cd44SJoe Perches } 987d311cd44SJoe Perches 988d311cd44SJoe Perches return ($id, $desc); 989d311cd44SJoe Perches} 990d311cd44SJoe Perches 9916c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 9920a920b5bSAndy Whitcroft 99300df344fSAndy Whitcroftmy @rawlines = (); 994c2fdda0dSAndy Whitcroftmy @lines = (); 9953705ce5bSJoe Perchesmy @fixed = (); 996d752fcc8SJoe Perchesmy @fixed_inserted = (); 997d752fcc8SJoe Perchesmy @fixed_deleted = (); 998194f66fcSJoe Perchesmy $fixlinenr = -1; 999194f66fcSJoe Perches 10004a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10014a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 10024a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 10034a593c34SDu, Changbin 10044a593c34SDu, Changbinif ($git) { 10054a593c34SDu, Changbin my @commits = (); 10060dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 10074a593c34SDu, Changbin my $git_range; 100828898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 100928898fd1SJoe Perches $git_range = "-$2 $1"; 10104a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 10114a593c34SDu, Changbin $git_range = "$commit_expr"; 10124a593c34SDu, Changbin } else { 10130dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 10140dea9f1eSJoe Perches } 1015dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 10160dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 101728898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 101828898fd1SJoe Perches next if (!defined($1) || !defined($2)); 10190dea9f1eSJoe Perches my $sha1 = $1; 10200dea9f1eSJoe Perches my $subject = $2; 10210dea9f1eSJoe Perches unshift(@commits, $sha1); 10220dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 10234a593c34SDu, Changbin } 10244a593c34SDu, Changbin } 10254a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 10264a593c34SDu, Changbin @ARGV = @commits; 10274a593c34SDu, Changbin} 10284a593c34SDu, Changbin 1029c2fdda0dSAndy Whitcroftmy $vname; 103098005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 10316c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 103221caa13cSAndy Whitcroft my $FILE; 10334a593c34SDu, Changbin if ($git) { 10344a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 10354a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 10364a593c34SDu, Changbin } elsif ($file) { 103721caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 10386c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 103921caa13cSAndy Whitcroft } elsif ($filename eq '-') { 104021caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 10416c72ffaaSAndy Whitcroft } else { 104221caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 10436c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 10446c72ffaaSAndy Whitcroft } 1045c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1046c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 10474a593c34SDu, Changbin } elsif ($git) { 10480dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1049c2fdda0dSAndy Whitcroft } else { 1050c2fdda0dSAndy Whitcroft $vname = $filename; 1051c2fdda0dSAndy Whitcroft } 105221caa13cSAndy Whitcroft while (<$FILE>) { 10530a920b5bSAndy Whitcroft chomp; 105400df344fSAndy Whitcroft push(@rawlines, $_); 10556c72ffaaSAndy Whitcroft } 105621caa13cSAndy Whitcroft close($FILE); 1057d8469f16SJoe Perches 1058d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1059d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1060d8469f16SJoe Perches print "$vname\n"; 1061d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1062d8469f16SJoe Perches } 1063d8469f16SJoe Perches 1064c2fdda0dSAndy Whitcroft if (!process($filename)) { 10650a920b5bSAndy Whitcroft $exit = 1; 10660a920b5bSAndy Whitcroft } 106700df344fSAndy Whitcroft @rawlines = (); 106813214adfSAndy Whitcroft @lines = (); 10693705ce5bSJoe Perches @fixed = (); 1070d752fcc8SJoe Perches @fixed_inserted = (); 1071d752fcc8SJoe Perches @fixed_deleted = (); 1072194f66fcSJoe Perches $fixlinenr = -1; 1073485ff23eSAlex Dowad @modifierListFile = (); 1074485ff23eSAlex Dowad @typeListFile = (); 1075485ff23eSAlex Dowad build_types(); 10760a920b5bSAndy Whitcroft} 10770a920b5bSAndy Whitcroft 1078d8469f16SJoe Perchesif (!$quiet) { 10793c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 10803c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 10813c816e49SJoe Perches 10825b57980dSJoe Perches if (!$perl_version_ok) { 1083d8469f16SJoe Perches print << "EOM" 1084d8469f16SJoe Perches 1085d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 10865b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1087d8469f16SJoe PerchesEOM 1088d8469f16SJoe Perches } 1089d8469f16SJoe Perches if ($exit) { 1090d8469f16SJoe Perches print << "EOM" 1091d8469f16SJoe Perches 1092d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1093d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1094d8469f16SJoe PerchesEOM 1095d8469f16SJoe Perches } 1096d8469f16SJoe Perches} 1097d8469f16SJoe Perches 10980a920b5bSAndy Whitcroftexit($exit); 10990a920b5bSAndy Whitcroft 11000a920b5bSAndy Whitcroftsub top_of_kernel_tree { 11016c72ffaaSAndy Whitcroft my ($root) = @_; 11026c72ffaaSAndy Whitcroft 11036c72ffaaSAndy Whitcroft my @tree_check = ( 11046c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 11056c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 11066c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 11076c72ffaaSAndy Whitcroft ); 11086c72ffaaSAndy Whitcroft 11096c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 11106c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 11110a920b5bSAndy Whitcroft return 0; 11120a920b5bSAndy Whitcroft } 11136c72ffaaSAndy Whitcroft } 11146c72ffaaSAndy Whitcroft return 1; 11156c72ffaaSAndy Whitcroft} 11160a920b5bSAndy Whitcroft 111720112475SJoe Perchessub parse_email { 111820112475SJoe Perches my ($formatted_email) = @_; 111920112475SJoe Perches 112020112475SJoe Perches my $name = ""; 112120112475SJoe Perches my $address = ""; 112220112475SJoe Perches my $comment = ""; 112320112475SJoe Perches 112420112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 112520112475SJoe Perches $name = $1; 112620112475SJoe Perches $address = $2; 112720112475SJoe Perches $comment = $3 if defined $3; 112820112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 112920112475SJoe Perches $address = $1; 113020112475SJoe Perches $comment = $2 if defined $2; 113120112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 113220112475SJoe Perches $address = $1; 113320112475SJoe Perches $comment = $2 if defined $2; 113485e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 113520112475SJoe Perches $name = $formatted_email; 11363705ce5bSJoe Perches $name = trim($name); 113720112475SJoe Perches $name =~ s/^\"|\"$//g; 113820112475SJoe Perches # If there's a name left after stripping spaces and 113920112475SJoe Perches # leading quotes, and the address doesn't have both 114020112475SJoe Perches # leading and trailing angle brackets, the address 114120112475SJoe Perches # is invalid. ie: 114220112475SJoe Perches # "joe smith [email protected]" bad 114320112475SJoe Perches # "joe smith <[email protected]" bad 114420112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 114520112475SJoe Perches $name = ""; 114620112475SJoe Perches $address = ""; 114720112475SJoe Perches $comment = ""; 114820112475SJoe Perches } 114920112475SJoe Perches } 115020112475SJoe Perches 11513705ce5bSJoe Perches $name = trim($name); 115220112475SJoe Perches $name =~ s/^\"|\"$//g; 11533705ce5bSJoe Perches $address = trim($address); 115420112475SJoe Perches $address =~ s/^\<|\>$//g; 115520112475SJoe Perches 115620112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 115720112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 115820112475SJoe Perches $name = "\"$name\""; 115920112475SJoe Perches } 116020112475SJoe Perches 116120112475SJoe Perches return ($name, $address, $comment); 116220112475SJoe Perches} 116320112475SJoe Perches 116420112475SJoe Perchessub format_email { 116520112475SJoe Perches my ($name, $address) = @_; 116620112475SJoe Perches 116720112475SJoe Perches my $formatted_email; 116820112475SJoe Perches 11693705ce5bSJoe Perches $name = trim($name); 117020112475SJoe Perches $name =~ s/^\"|\"$//g; 11713705ce5bSJoe Perches $address = trim($address); 117220112475SJoe Perches 117320112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 117420112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 117520112475SJoe Perches $name = "\"$name\""; 117620112475SJoe Perches } 117720112475SJoe Perches 117820112475SJoe Perches if ("$name" eq "") { 117920112475SJoe Perches $formatted_email = "$address"; 118020112475SJoe Perches } else { 118120112475SJoe Perches $formatted_email = "$name <$address>"; 118220112475SJoe Perches } 118320112475SJoe Perches 118420112475SJoe Perches return $formatted_email; 118520112475SJoe Perches} 118620112475SJoe Perches 1187d311cd44SJoe Perchessub which { 1188d311cd44SJoe Perches my ($bin) = @_; 1189d311cd44SJoe Perches 1190d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1191d311cd44SJoe Perches if (-e "$path/$bin") { 1192d311cd44SJoe Perches return "$path/$bin"; 1193d311cd44SJoe Perches } 1194d311cd44SJoe Perches } 1195d311cd44SJoe Perches 1196d311cd44SJoe Perches return ""; 1197d311cd44SJoe Perches} 1198d311cd44SJoe Perches 1199000d1cc1SJoe Perchessub which_conf { 1200000d1cc1SJoe Perches my ($conf) = @_; 1201000d1cc1SJoe Perches 1202000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1203000d1cc1SJoe Perches if (-e "$path/$conf") { 1204000d1cc1SJoe Perches return "$path/$conf"; 1205000d1cc1SJoe Perches } 1206000d1cc1SJoe Perches } 1207000d1cc1SJoe Perches 1208000d1cc1SJoe Perches return ""; 1209000d1cc1SJoe Perches} 1210000d1cc1SJoe Perches 12110a920b5bSAndy Whitcroftsub expand_tabs { 12120a920b5bSAndy Whitcroft my ($str) = @_; 12130a920b5bSAndy Whitcroft 12140a920b5bSAndy Whitcroft my $res = ''; 12150a920b5bSAndy Whitcroft my $n = 0; 12160a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 12170a920b5bSAndy Whitcroft if ($c eq "\t") { 12180a920b5bSAndy Whitcroft $res .= ' '; 12190a920b5bSAndy Whitcroft $n++; 12200a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 12210a920b5bSAndy Whitcroft $res .= ' '; 12220a920b5bSAndy Whitcroft } 12230a920b5bSAndy Whitcroft next; 12240a920b5bSAndy Whitcroft } 12250a920b5bSAndy Whitcroft $res .= $c; 12260a920b5bSAndy Whitcroft $n++; 12270a920b5bSAndy Whitcroft } 12280a920b5bSAndy Whitcroft 12290a920b5bSAndy Whitcroft return $res; 12300a920b5bSAndy Whitcroft} 12316c72ffaaSAndy Whitcroftsub copy_spacing { 1232773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 12336c72ffaaSAndy Whitcroft return $res; 12346c72ffaaSAndy Whitcroft} 12350a920b5bSAndy Whitcroft 12364a0df2efSAndy Whitcroftsub line_stats { 12374a0df2efSAndy Whitcroft my ($line) = @_; 12384a0df2efSAndy Whitcroft 12394a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 12404a0df2efSAndy Whitcroft $line =~ s/^.//; 12414a0df2efSAndy Whitcroft $line = expand_tabs($line); 12424a0df2efSAndy Whitcroft 12434a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 12444a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 12454a0df2efSAndy Whitcroft 12464a0df2efSAndy Whitcroft return (length($line), length($white)); 12474a0df2efSAndy Whitcroft} 12484a0df2efSAndy Whitcroft 1249773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1250773647a0SAndy Whitcroft 1251773647a0SAndy Whitcroftsub sanitise_line_reset { 1252773647a0SAndy Whitcroft my ($in_comment) = @_; 1253773647a0SAndy Whitcroft 1254773647a0SAndy Whitcroft if ($in_comment) { 1255773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1256773647a0SAndy Whitcroft } else { 1257773647a0SAndy Whitcroft $sanitise_quote = ''; 1258773647a0SAndy Whitcroft } 1259773647a0SAndy Whitcroft} 126000df344fSAndy Whitcroftsub sanitise_line { 126100df344fSAndy Whitcroft my ($line) = @_; 126200df344fSAndy Whitcroft 126300df344fSAndy Whitcroft my $res = ''; 126400df344fSAndy Whitcroft my $l = ''; 126500df344fSAndy Whitcroft 1266c2fdda0dSAndy Whitcroft my $qlen = 0; 1267773647a0SAndy Whitcroft my $off = 0; 1268773647a0SAndy Whitcroft my $c; 126900df344fSAndy Whitcroft 1270773647a0SAndy Whitcroft # Always copy over the diff marker. 1271773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1272773647a0SAndy Whitcroft 1273773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1274773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1275773647a0SAndy Whitcroft 12768d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1277773647a0SAndy Whitcroft # and end, all to $;. 1278773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1279773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1280773647a0SAndy Whitcroft 1281773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1282773647a0SAndy Whitcroft $off++; 128300df344fSAndy Whitcroft next; 1284773647a0SAndy Whitcroft } 128581bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1286773647a0SAndy Whitcroft $sanitise_quote = ''; 1287773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1288773647a0SAndy Whitcroft $off++; 1289773647a0SAndy Whitcroft next; 1290773647a0SAndy Whitcroft } 1291113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1292113f04a8SDaniel Walker $sanitise_quote = '//'; 1293113f04a8SDaniel Walker 1294113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1295113f04a8SDaniel Walker $off++; 1296113f04a8SDaniel Walker next; 1297113f04a8SDaniel Walker } 1298773647a0SAndy Whitcroft 1299773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1300773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1301773647a0SAndy Whitcroft $c eq "\\") { 1302773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1303773647a0SAndy Whitcroft $off++; 1304773647a0SAndy Whitcroft next; 1305773647a0SAndy Whitcroft } 1306773647a0SAndy Whitcroft # Regular quotes. 1307773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1308773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1309773647a0SAndy Whitcroft $sanitise_quote = $c; 1310773647a0SAndy Whitcroft 1311773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1312773647a0SAndy Whitcroft next; 1313773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1314773647a0SAndy Whitcroft $sanitise_quote = ''; 131500df344fSAndy Whitcroft } 131600df344fSAndy Whitcroft } 1317773647a0SAndy Whitcroft 1318fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1319773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1320773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1321113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1322113f04a8SDaniel Walker substr($res, $off, 1, $;); 1323773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1324773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 132500df344fSAndy Whitcroft } else { 1326773647a0SAndy Whitcroft substr($res, $off, 1, $c); 132700df344fSAndy Whitcroft } 1328c2fdda0dSAndy Whitcroft } 1329c2fdda0dSAndy Whitcroft 1330113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1331113f04a8SDaniel Walker $sanitise_quote = ''; 1332113f04a8SDaniel Walker } 1333113f04a8SDaniel Walker 1334c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1335c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1336c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1337c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1338c2fdda0dSAndy Whitcroft 1339c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1340c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1341c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1342c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1343c2fdda0dSAndy Whitcroft } 1344c2fdda0dSAndy Whitcroft 1345dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1346dadf680dSJoe Perches my $match = $1; 1347dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1348dadf680dSJoe Perches } 1349dadf680dSJoe Perches 135000df344fSAndy Whitcroft return $res; 135100df344fSAndy Whitcroft} 135200df344fSAndy Whitcroft 1353a6962d72SJoe Perchessub get_quoted_string { 1354a6962d72SJoe Perches my ($line, $rawline) = @_; 1355a6962d72SJoe Perches 1356478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 135733acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1358a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1359a6962d72SJoe Perches} 1360a6962d72SJoe Perches 13618905a67cSAndy Whitcroftsub ctx_statement_block { 13628905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 13638905a67cSAndy Whitcroft my $line = $linenr - 1; 13648905a67cSAndy Whitcroft my $blk = ''; 13658905a67cSAndy Whitcroft my $soff = $off; 13668905a67cSAndy Whitcroft my $coff = $off - 1; 1367773647a0SAndy Whitcroft my $coff_set = 0; 13688905a67cSAndy Whitcroft 136913214adfSAndy Whitcroft my $loff = 0; 137013214adfSAndy Whitcroft 13718905a67cSAndy Whitcroft my $type = ''; 13728905a67cSAndy Whitcroft my $level = 0; 1373a2750645SAndy Whitcroft my @stack = (); 1374cf655043SAndy Whitcroft my $p; 13758905a67cSAndy Whitcroft my $c; 13768905a67cSAndy Whitcroft my $len = 0; 137713214adfSAndy Whitcroft 137813214adfSAndy Whitcroft my $remainder; 13798905a67cSAndy Whitcroft while (1) { 1380a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1381a2750645SAndy Whitcroft 1382773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 13838905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 13848905a67cSAndy Whitcroft # context. 13858905a67cSAndy Whitcroft if ($off >= $len) { 13868905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1387dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1388c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 13898905a67cSAndy Whitcroft $remain--; 139013214adfSAndy Whitcroft $loff = $len; 1391c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 13928905a67cSAndy Whitcroft $len = length($blk); 13938905a67cSAndy Whitcroft $line++; 13948905a67cSAndy Whitcroft last; 13958905a67cSAndy Whitcroft } 13968905a67cSAndy Whitcroft # Bail if there is no further context. 13978905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 139813214adfSAndy Whitcroft if ($off >= $len) { 13998905a67cSAndy Whitcroft last; 14008905a67cSAndy Whitcroft } 1401f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1402f74bd194SAndy Whitcroft $level++; 1403f74bd194SAndy Whitcroft $type = '#'; 1404f74bd194SAndy Whitcroft } 14058905a67cSAndy Whitcroft } 1406cf655043SAndy Whitcroft $p = $c; 14078905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 140813214adfSAndy Whitcroft $remainder = substr($blk, $off); 14098905a67cSAndy Whitcroft 1410773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 14114635f4fbSAndy Whitcroft 14124635f4fbSAndy Whitcroft # Handle nested #if/#else. 14134635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 14144635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 14154635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 14164635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 14174635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 14184635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 14194635f4fbSAndy Whitcroft } 14204635f4fbSAndy Whitcroft 14218905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 14228905a67cSAndy Whitcroft # outermost level. 14238905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 14248905a67cSAndy Whitcroft last; 14258905a67cSAndy Whitcroft } 14268905a67cSAndy Whitcroft 142713214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1428773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1429773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1430773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1431773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1432773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1433773647a0SAndy Whitcroft $coff_set = 1; 1434773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1435773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 143613214adfSAndy Whitcroft } 143713214adfSAndy Whitcroft 14388905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 14398905a67cSAndy Whitcroft $level++; 14408905a67cSAndy Whitcroft $type = '('; 14418905a67cSAndy Whitcroft } 14428905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 14438905a67cSAndy Whitcroft $level--; 14448905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 14458905a67cSAndy Whitcroft 14468905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 14478905a67cSAndy Whitcroft $coff = $off; 1448773647a0SAndy Whitcroft $coff_set = 1; 1449773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 14508905a67cSAndy Whitcroft } 14518905a67cSAndy Whitcroft } 14528905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 14538905a67cSAndy Whitcroft $level++; 14548905a67cSAndy Whitcroft $type = '{'; 14558905a67cSAndy Whitcroft } 14568905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 14578905a67cSAndy Whitcroft $level--; 14588905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 14598905a67cSAndy Whitcroft 14608905a67cSAndy Whitcroft if ($level == 0) { 1461b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1462b998e001SPatrick Pannuto $off++; 1463b998e001SPatrick Pannuto } 14648905a67cSAndy Whitcroft last; 14658905a67cSAndy Whitcroft } 14668905a67cSAndy Whitcroft } 1467f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1468f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1469f74bd194SAndy Whitcroft $level--; 1470f74bd194SAndy Whitcroft $type = ''; 1471f74bd194SAndy Whitcroft $off++; 1472f74bd194SAndy Whitcroft last; 1473f74bd194SAndy Whitcroft } 14748905a67cSAndy Whitcroft $off++; 14758905a67cSAndy Whitcroft } 1476a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 147713214adfSAndy Whitcroft if ($off == $len) { 1478a3bb97a7SAndy Whitcroft $loff = $len + 1; 147913214adfSAndy Whitcroft $line++; 148013214adfSAndy Whitcroft $remain--; 148113214adfSAndy Whitcroft } 14828905a67cSAndy Whitcroft 14838905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 14848905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 14858905a67cSAndy Whitcroft 14868905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 14878905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 14888905a67cSAndy Whitcroft 1489773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 149013214adfSAndy Whitcroft 149113214adfSAndy Whitcroft return ($statement, $condition, 149213214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 149313214adfSAndy Whitcroft} 149413214adfSAndy Whitcroft 1495cf655043SAndy Whitcroftsub statement_lines { 1496cf655043SAndy Whitcroft my ($stmt) = @_; 1497cf655043SAndy Whitcroft 1498cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1499cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1500cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1501cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1502cf655043SAndy Whitcroft 1503cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1504cf655043SAndy Whitcroft 1505cf655043SAndy Whitcroft return $#stmt_lines + 2; 1506cf655043SAndy Whitcroft} 1507cf655043SAndy Whitcroft 1508cf655043SAndy Whitcroftsub statement_rawlines { 1509cf655043SAndy Whitcroft my ($stmt) = @_; 1510cf655043SAndy Whitcroft 1511cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1512cf655043SAndy Whitcroft 1513cf655043SAndy Whitcroft return $#stmt_lines + 2; 1514cf655043SAndy Whitcroft} 1515cf655043SAndy Whitcroft 1516cf655043SAndy Whitcroftsub statement_block_size { 1517cf655043SAndy Whitcroft my ($stmt) = @_; 1518cf655043SAndy Whitcroft 1519cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1520cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1521cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1522cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1523cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1524cf655043SAndy Whitcroft 1525cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1526cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1527cf655043SAndy Whitcroft 1528cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1529cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1530cf655043SAndy Whitcroft 1531cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1532cf655043SAndy Whitcroft return $stmt_lines; 1533cf655043SAndy Whitcroft } else { 1534cf655043SAndy Whitcroft return $stmt_statements; 1535cf655043SAndy Whitcroft } 1536cf655043SAndy Whitcroft} 1537cf655043SAndy Whitcroft 153813214adfSAndy Whitcroftsub ctx_statement_full { 153913214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 154013214adfSAndy Whitcroft my ($statement, $condition, $level); 154113214adfSAndy Whitcroft 154213214adfSAndy Whitcroft my (@chunks); 154313214adfSAndy Whitcroft 1544cf655043SAndy Whitcroft # Grab the first conditional/block pair. 154513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 154613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1547773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 154813214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1549cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1550cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1551cf655043SAndy Whitcroft } 1552cf655043SAndy Whitcroft 1553cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1554cf655043SAndy Whitcroft # could continue the statement. 1555cf655043SAndy Whitcroft for (;;) { 155613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 155713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1558cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1559773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1560cf655043SAndy Whitcroft #print "C: push\n"; 1561cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 156213214adfSAndy Whitcroft } 156313214adfSAndy Whitcroft 156413214adfSAndy Whitcroft return ($level, $linenr, @chunks); 15658905a67cSAndy Whitcroft} 15668905a67cSAndy Whitcroft 15674a0df2efSAndy Whitcroftsub ctx_block_get { 1568f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 15694a0df2efSAndy Whitcroft my $line; 15704a0df2efSAndy Whitcroft my $start = $linenr - 1; 15714a0df2efSAndy Whitcroft my $blk = ''; 15724a0df2efSAndy Whitcroft my @o; 15734a0df2efSAndy Whitcroft my @c; 15744a0df2efSAndy Whitcroft my @res = (); 15754a0df2efSAndy Whitcroft 1576f0a594c1SAndy Whitcroft my $level = 0; 15774635f4fbSAndy Whitcroft my @stack = ($level); 157800df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 157900df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 158000df344fSAndy Whitcroft $remain--; 158100df344fSAndy Whitcroft 158200df344fSAndy Whitcroft $blk .= $rawlines[$line]; 15834635f4fbSAndy Whitcroft 15844635f4fbSAndy Whitcroft # Handle nested #if/#else. 158501464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 15864635f4fbSAndy Whitcroft push(@stack, $level); 158701464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 15884635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 158901464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 15904635f4fbSAndy Whitcroft $level = pop(@stack); 15914635f4fbSAndy Whitcroft } 15924635f4fbSAndy Whitcroft 159301464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1594f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1595f0a594c1SAndy Whitcroft if ($off > 0) { 1596f0a594c1SAndy Whitcroft $off--; 1597f0a594c1SAndy Whitcroft next; 1598f0a594c1SAndy Whitcroft } 15994a0df2efSAndy Whitcroft 1600f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1601f0a594c1SAndy Whitcroft $level--; 1602f0a594c1SAndy Whitcroft last if ($level == 0); 1603f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1604f0a594c1SAndy Whitcroft $level++; 1605f0a594c1SAndy Whitcroft } 1606f0a594c1SAndy Whitcroft } 16074a0df2efSAndy Whitcroft 1608f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 160900df344fSAndy Whitcroft push(@res, $rawlines[$line]); 16104a0df2efSAndy Whitcroft } 16114a0df2efSAndy Whitcroft 1612f0a594c1SAndy Whitcroft last if ($level == 0); 16134a0df2efSAndy Whitcroft } 16144a0df2efSAndy Whitcroft 1615f0a594c1SAndy Whitcroft return ($level, @res); 16164a0df2efSAndy Whitcroft} 16174a0df2efSAndy Whitcroftsub ctx_block_outer { 16184a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16194a0df2efSAndy Whitcroft 1620f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1621f0a594c1SAndy Whitcroft return @r; 16224a0df2efSAndy Whitcroft} 16234a0df2efSAndy Whitcroftsub ctx_block { 16244a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16254a0df2efSAndy Whitcroft 1626f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1627f0a594c1SAndy Whitcroft return @r; 1628653d4876SAndy Whitcroft} 1629653d4876SAndy Whitcroftsub ctx_statement { 1630f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1631f0a594c1SAndy Whitcroft 1632f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1633f0a594c1SAndy Whitcroft return @r; 1634f0a594c1SAndy Whitcroft} 1635f0a594c1SAndy Whitcroftsub ctx_block_level { 1636653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1637653d4876SAndy Whitcroft 1638f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 16394a0df2efSAndy Whitcroft} 16409c0ca6f9SAndy Whitcroftsub ctx_statement_level { 16419c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 16429c0ca6f9SAndy Whitcroft 16439c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 16449c0ca6f9SAndy Whitcroft} 16454a0df2efSAndy Whitcroft 16464a0df2efSAndy Whitcroftsub ctx_locate_comment { 16474a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16484a0df2efSAndy Whitcroft 16494a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1650beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 16514a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 16524a0df2efSAndy Whitcroft 16534a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 16544a0df2efSAndy Whitcroft # comment. 16554a0df2efSAndy Whitcroft my $in_comment = 0; 16564a0df2efSAndy Whitcroft $current_comment = ''; 16574a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 165800df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 165900df344fSAndy Whitcroft #warn " $line\n"; 16604a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 16614a0df2efSAndy Whitcroft $in_comment = 1; 16624a0df2efSAndy Whitcroft } 16634a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 16644a0df2efSAndy Whitcroft $in_comment = 1; 16654a0df2efSAndy Whitcroft } 16664a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 16674a0df2efSAndy Whitcroft $current_comment = ''; 16684a0df2efSAndy Whitcroft } 16694a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 16704a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 16714a0df2efSAndy Whitcroft $in_comment = 0; 16724a0df2efSAndy Whitcroft } 16734a0df2efSAndy Whitcroft } 16744a0df2efSAndy Whitcroft 16754a0df2efSAndy Whitcroft chomp($current_comment); 16764a0df2efSAndy Whitcroft return($current_comment); 16774a0df2efSAndy Whitcroft} 16784a0df2efSAndy Whitcroftsub ctx_has_comment { 16794a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16804a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 16814a0df2efSAndy Whitcroft 168200df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 16834a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 16844a0df2efSAndy Whitcroft 16854a0df2efSAndy Whitcroft return ($cmt ne ''); 16864a0df2efSAndy Whitcroft} 16874a0df2efSAndy Whitcroft 16884d001e4dSAndy Whitcroftsub raw_line { 16894d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 16904d001e4dSAndy Whitcroft 16914d001e4dSAndy Whitcroft my $offset = $linenr - 1; 16924d001e4dSAndy Whitcroft $cnt++; 16934d001e4dSAndy Whitcroft 16944d001e4dSAndy Whitcroft my $line; 16954d001e4dSAndy Whitcroft while ($cnt) { 16964d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 16974d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 16984d001e4dSAndy Whitcroft $cnt--; 16994d001e4dSAndy Whitcroft } 17004d001e4dSAndy Whitcroft 17014d001e4dSAndy Whitcroft return $line; 17024d001e4dSAndy Whitcroft} 17034d001e4dSAndy Whitcroft 17042a9f9d85STobin C. Hardingsub get_stat_real { 17052a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 17062a9f9d85STobin C. Harding 17072a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 17082a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 17092a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 17102a9f9d85STobin C. Harding } 17112a9f9d85STobin C. Harding 17122a9f9d85STobin C. Harding return $stat_real; 17132a9f9d85STobin C. Harding} 17142a9f9d85STobin C. Harding 1715e3d95a2aSTobin C. Hardingsub get_stat_here { 1716e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1717e3d95a2aSTobin C. Harding 1718e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1719e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1720e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1721e3d95a2aSTobin C. Harding } 1722e3d95a2aSTobin C. Harding 1723e3d95a2aSTobin C. Harding return $herectx; 1724e3d95a2aSTobin C. Harding} 1725e3d95a2aSTobin C. Harding 17260a920b5bSAndy Whitcroftsub cat_vet { 17270a920b5bSAndy Whitcroft my ($vet) = @_; 17289c0ca6f9SAndy Whitcroft my ($res, $coded); 17290a920b5bSAndy Whitcroft 17309c0ca6f9SAndy Whitcroft $res = ''; 17316c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 17326c72ffaaSAndy Whitcroft $res .= $1; 17336c72ffaaSAndy Whitcroft if ($2 ne '') { 17349c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 17356c72ffaaSAndy Whitcroft $res .= $coded; 17366c72ffaaSAndy Whitcroft } 17379c0ca6f9SAndy Whitcroft } 17389c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 17390a920b5bSAndy Whitcroft 17409c0ca6f9SAndy Whitcroft return $res; 17410a920b5bSAndy Whitcroft} 17420a920b5bSAndy Whitcroft 1743c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1744cf655043SAndy Whitcroftmy $av_pending; 1745c2fdda0dSAndy Whitcroftmy @av_paren_type; 17461f65f947SAndy Whitcroftmy $av_pend_colon; 1747c2fdda0dSAndy Whitcroft 1748c2fdda0dSAndy Whitcroftsub annotate_reset { 1749c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1750cf655043SAndy Whitcroft $av_pending = '_'; 1751cf655043SAndy Whitcroft @av_paren_type = ('E'); 17521f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1753c2fdda0dSAndy Whitcroft} 1754c2fdda0dSAndy Whitcroft 17556c72ffaaSAndy Whitcroftsub annotate_values { 17566c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 17576c72ffaaSAndy Whitcroft 17586c72ffaaSAndy Whitcroft my $res; 17591f65f947SAndy Whitcroft my $var = '_' x length($stream); 17606c72ffaaSAndy Whitcroft my $cur = $stream; 17616c72ffaaSAndy Whitcroft 1762c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 17636c72ffaaSAndy Whitcroft 17646c72ffaaSAndy Whitcroft while (length($cur)) { 1765773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1766cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1767171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 17686c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1769c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1770c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1771cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1772c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 17736c72ffaaSAndy Whitcroft } 17746c72ffaaSAndy Whitcroft 1775c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 17769446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 17779446ef56SAndy Whitcroft push(@av_paren_type, $type); 1778addcdceaSAndy Whitcroft $type = 'c'; 17799446ef56SAndy Whitcroft 1780e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1781c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 17826c72ffaaSAndy Whitcroft $type = 'T'; 17836c72ffaaSAndy Whitcroft 1784389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1785389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1786389a2fe5SAndy Whitcroft $type = 'T'; 1787389a2fe5SAndy Whitcroft 1788c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1789171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1790c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1791171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1792171ae1a4SAndy Whitcroft if ($2 ne '') { 1793cf655043SAndy Whitcroft $av_pending = 'N'; 1794171ae1a4SAndy Whitcroft } 1795171ae1a4SAndy Whitcroft $type = 'E'; 1796171ae1a4SAndy Whitcroft 1797c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1798171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1799171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1800171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 18016c72ffaaSAndy Whitcroft 1802c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1803cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1804c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1805cf655043SAndy Whitcroft 1806cf655043SAndy Whitcroft push(@av_paren_type, $type); 1807cf655043SAndy Whitcroft push(@av_paren_type, $type); 1808171ae1a4SAndy Whitcroft $type = 'E'; 1809cf655043SAndy Whitcroft 1810c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1811cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1812cf655043SAndy Whitcroft $av_preprocessor = 1; 1813cf655043SAndy Whitcroft 1814cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1815cf655043SAndy Whitcroft 1816171ae1a4SAndy Whitcroft $type = 'E'; 1817cf655043SAndy Whitcroft 1818c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1819cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1820cf655043SAndy Whitcroft 1821cf655043SAndy Whitcroft $av_preprocessor = 1; 1822cf655043SAndy Whitcroft 1823cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1824cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1825cf655043SAndy Whitcroft pop(@av_paren_type); 1826cf655043SAndy Whitcroft push(@av_paren_type, $type); 1827171ae1a4SAndy Whitcroft $type = 'E'; 18286c72ffaaSAndy Whitcroft 18296c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1830c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 18316c72ffaaSAndy Whitcroft 1832171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1833171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1834171ae1a4SAndy Whitcroft $av_pending = $type; 1835171ae1a4SAndy Whitcroft $type = 'N'; 1836171ae1a4SAndy Whitcroft 18376c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1838c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 18396c72ffaaSAndy Whitcroft if (defined $2) { 1840cf655043SAndy Whitcroft $av_pending = 'V'; 18416c72ffaaSAndy Whitcroft } 18426c72ffaaSAndy Whitcroft $type = 'N'; 18436c72ffaaSAndy Whitcroft 184414b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1845c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 184614b111c1SAndy Whitcroft $av_pending = 'E'; 18476c72ffaaSAndy Whitcroft $type = 'N'; 18486c72ffaaSAndy Whitcroft 18491f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 18501f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 18511f65f947SAndy Whitcroft $av_pend_colon = 'C'; 18521f65f947SAndy Whitcroft $type = 'N'; 18531f65f947SAndy Whitcroft 185414b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1855c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 18566c72ffaaSAndy Whitcroft $type = 'N'; 18576c72ffaaSAndy Whitcroft 18586c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1859c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1860cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1861cf655043SAndy Whitcroft $av_pending = '_'; 18626c72ffaaSAndy Whitcroft $type = 'N'; 18636c72ffaaSAndy Whitcroft 18646c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1865cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1866cf655043SAndy Whitcroft if ($new_type ne '_') { 1867cf655043SAndy Whitcroft $type = $new_type; 1868c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1869c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 18706c72ffaaSAndy Whitcroft } else { 1871c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 18726c72ffaaSAndy Whitcroft } 18736c72ffaaSAndy Whitcroft 1874c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1875c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1876c8cb2ca3SAndy Whitcroft $type = 'V'; 1877cf655043SAndy Whitcroft $av_pending = 'V'; 18786c72ffaaSAndy Whitcroft 18798e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 18808e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 18811f65f947SAndy Whitcroft $av_pend_colon = 'B'; 18828e761b04SAndy Whitcroft } elsif ($type eq 'E') { 18838e761b04SAndy Whitcroft $av_pend_colon = 'L'; 18841f65f947SAndy Whitcroft } 18851f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 18861f65f947SAndy Whitcroft $type = 'V'; 18871f65f947SAndy Whitcroft 18886c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1889c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 18906c72ffaaSAndy Whitcroft $type = 'V'; 18916c72ffaaSAndy Whitcroft 18926c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1893c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 18946c72ffaaSAndy Whitcroft $type = 'N'; 18956c72ffaaSAndy Whitcroft 1896cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1897c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 189813214adfSAndy Whitcroft $type = 'E'; 18991f65f947SAndy Whitcroft $av_pend_colon = 'O'; 190013214adfSAndy Whitcroft 19018e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 19028e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 19038e761b04SAndy Whitcroft $type = 'C'; 19048e761b04SAndy Whitcroft 19051f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 19061f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 19071f65f947SAndy Whitcroft $type = 'N'; 19081f65f947SAndy Whitcroft 19091f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 19101f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 19111f65f947SAndy Whitcroft 19121f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 19131f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 19141f65f947SAndy Whitcroft $type = 'E'; 19151f65f947SAndy Whitcroft } else { 19161f65f947SAndy Whitcroft $type = 'N'; 19171f65f947SAndy Whitcroft } 19181f65f947SAndy Whitcroft $av_pend_colon = 'O'; 19191f65f947SAndy Whitcroft 19208e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 192113214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 19226c72ffaaSAndy Whitcroft $type = 'N'; 19236c72ffaaSAndy Whitcroft 19240d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 192574048ed8SAndy Whitcroft my $variant; 192674048ed8SAndy Whitcroft 192774048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 192874048ed8SAndy Whitcroft if ($type eq 'V') { 192974048ed8SAndy Whitcroft $variant = 'B'; 193074048ed8SAndy Whitcroft } else { 193174048ed8SAndy Whitcroft $variant = 'U'; 193274048ed8SAndy Whitcroft } 193374048ed8SAndy Whitcroft 193474048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 193574048ed8SAndy Whitcroft $type = 'N'; 193674048ed8SAndy Whitcroft 19376c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1938c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 19396c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 19406c72ffaaSAndy Whitcroft $type = 'N'; 19416c72ffaaSAndy Whitcroft } 19426c72ffaaSAndy Whitcroft 19436c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1944c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 19456c72ffaaSAndy Whitcroft } 19466c72ffaaSAndy Whitcroft if (defined $1) { 19476c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 19486c72ffaaSAndy Whitcroft $res .= $type x length($1); 19496c72ffaaSAndy Whitcroft } 19506c72ffaaSAndy Whitcroft } 19516c72ffaaSAndy Whitcroft 19521f65f947SAndy Whitcroft return ($res, $var); 19536c72ffaaSAndy Whitcroft} 19546c72ffaaSAndy Whitcroft 19558905a67cSAndy Whitcroftsub possible { 195613214adfSAndy Whitcroft my ($possible, $line) = @_; 19579a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 19580776e594SAndy Whitcroft ^(?: 19590776e594SAndy Whitcroft $Modifier| 19600776e594SAndy Whitcroft $Storage| 19610776e594SAndy Whitcroft $Type| 19629a974fdbSAndy Whitcroft DEFINE_\S+ 19639a974fdbSAndy Whitcroft )$| 19649a974fdbSAndy Whitcroft ^(?: 19650776e594SAndy Whitcroft goto| 19660776e594SAndy Whitcroft return| 19670776e594SAndy Whitcroft case| 19680776e594SAndy Whitcroft else| 19690776e594SAndy Whitcroft asm|__asm__| 197089a88353SAndy Whitcroft do| 197189a88353SAndy Whitcroft \#| 197289a88353SAndy Whitcroft \#\#| 19739a974fdbSAndy Whitcroft )(?:\s|$)| 19740776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 19759a974fdbSAndy Whitcroft )}x; 19769a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 19779a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1978c45dcabdSAndy Whitcroft # Check for modifiers. 1979c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1980c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1981c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1982c45dcabdSAndy Whitcroft 1983c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1984c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1985d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 19869a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1987d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1988485ff23eSAlex Dowad push(@modifierListFile, $modifier); 1989d2506586SAndy Whitcroft } 19909a974fdbSAndy Whitcroft } 1991c45dcabdSAndy Whitcroft 1992c45dcabdSAndy Whitcroft } else { 199313214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1994485ff23eSAlex Dowad push(@typeListFile, $possible); 1995c45dcabdSAndy Whitcroft } 19968905a67cSAndy Whitcroft build_types(); 19970776e594SAndy Whitcroft } else { 19980776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 19998905a67cSAndy Whitcroft } 20008905a67cSAndy Whitcroft} 20018905a67cSAndy Whitcroft 20026c72ffaaSAndy Whitcroftmy $prefix = ''; 20036c72ffaaSAndy Whitcroft 2004000d1cc1SJoe Perchessub show_type { 2005cbec18afSJoe Perches my ($type) = @_; 200691bfe484SJoe Perches 2007522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2008522b837cSAlexey Dobriyan 2009cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2010cbec18afSJoe Perches 2011cbec18afSJoe Perches return !defined $ignore_type{$type}; 2012000d1cc1SJoe Perches} 2013000d1cc1SJoe Perches 2014f0a594c1SAndy Whitcroftsub report { 2015cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2016cbec18afSJoe Perches 2017cbec18afSJoe Perches if (!show_type($type) || 2018cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2019773647a0SAndy Whitcroft return 0; 2020773647a0SAndy Whitcroft } 202157230297SJoe Perches my $output = ''; 2022737c0767SJohn Brooks if ($color) { 202357230297SJoe Perches if ($level eq 'ERROR') { 202457230297SJoe Perches $output .= RED; 202557230297SJoe Perches } elsif ($level eq 'WARNING') { 202657230297SJoe Perches $output .= YELLOW; 2027000d1cc1SJoe Perches } else { 202857230297SJoe Perches $output .= GREEN; 2029000d1cc1SJoe Perches } 203057230297SJoe Perches } 203157230297SJoe Perches $output .= $prefix . $level . ':'; 203257230297SJoe Perches if ($show_types) { 2033737c0767SJohn Brooks $output .= BLUE if ($color); 203457230297SJoe Perches $output .= "$type:"; 203557230297SJoe Perches } 2036737c0767SJohn Brooks $output .= RESET if ($color); 203757230297SJoe Perches $output .= ' ' . $msg . "\n"; 203834d8815fSJoe Perches 203934d8815fSJoe Perches if ($showfile) { 204034d8815fSJoe Perches my @lines = split("\n", $output, -1); 204134d8815fSJoe Perches splice(@lines, 1, 1); 204234d8815fSJoe Perches $output = join("\n", @lines); 204334d8815fSJoe Perches } 204457230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 20458905a67cSAndy Whitcroft 204657230297SJoe Perches push(our @report, $output); 2047773647a0SAndy Whitcroft 2048773647a0SAndy Whitcroft return 1; 2049f0a594c1SAndy Whitcroft} 2050cbec18afSJoe Perches 2051f0a594c1SAndy Whitcroftsub report_dump { 205213214adfSAndy Whitcroft our @report; 2053f0a594c1SAndy Whitcroft} 2054000d1cc1SJoe Perches 2055d752fcc8SJoe Perchessub fixup_current_range { 2056d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2057d752fcc8SJoe Perches 2058d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2059d752fcc8SJoe Perches my $o = $1; 2060d752fcc8SJoe Perches my $l = $2; 2061d752fcc8SJoe Perches my $no = $o + $offset; 2062d752fcc8SJoe Perches my $nl = $l + $length; 2063d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2064d752fcc8SJoe Perches } 2065d752fcc8SJoe Perches} 2066d752fcc8SJoe Perches 2067d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2068d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2069d752fcc8SJoe Perches 2070d752fcc8SJoe Perches my $range_last_linenr = 0; 2071d752fcc8SJoe Perches my $delta_offset = 0; 2072d752fcc8SJoe Perches 2073d752fcc8SJoe Perches my $old_linenr = 0; 2074d752fcc8SJoe Perches my $new_linenr = 0; 2075d752fcc8SJoe Perches 2076d752fcc8SJoe Perches my $next_insert = 0; 2077d752fcc8SJoe Perches my $next_delete = 0; 2078d752fcc8SJoe Perches 2079d752fcc8SJoe Perches my @lines = (); 2080d752fcc8SJoe Perches 2081d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2082d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2083d752fcc8SJoe Perches 2084d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2085d752fcc8SJoe Perches my $save_line = 1; 2086d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2087323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2088d752fcc8SJoe Perches $delta_offset = 0; 2089d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2090d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2091d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2092d752fcc8SJoe Perches } 2093d752fcc8SJoe Perches 2094d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2095d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2096d752fcc8SJoe Perches $save_line = 0; 2097d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2098d752fcc8SJoe Perches } 2099d752fcc8SJoe Perches 2100d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2101d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2102d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2103d752fcc8SJoe Perches $new_linenr++; 2104d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2105d752fcc8SJoe Perches } 2106d752fcc8SJoe Perches 2107d752fcc8SJoe Perches if ($save_line) { 2108d752fcc8SJoe Perches push(@lines, $line); 2109d752fcc8SJoe Perches $new_linenr++; 2110d752fcc8SJoe Perches } 2111d752fcc8SJoe Perches 2112d752fcc8SJoe Perches $old_linenr++; 2113d752fcc8SJoe Perches } 2114d752fcc8SJoe Perches 2115d752fcc8SJoe Perches return @lines; 2116d752fcc8SJoe Perches} 2117d752fcc8SJoe Perches 2118f2d7e4d4SJoe Perchessub fix_insert_line { 2119f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2120f2d7e4d4SJoe Perches 2121f2d7e4d4SJoe Perches my $inserted = { 2122f2d7e4d4SJoe Perches LINENR => $linenr, 2123f2d7e4d4SJoe Perches LINE => $line, 2124f2d7e4d4SJoe Perches }; 2125f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2126f2d7e4d4SJoe Perches} 2127f2d7e4d4SJoe Perches 2128f2d7e4d4SJoe Perchessub fix_delete_line { 2129f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2130f2d7e4d4SJoe Perches 2131f2d7e4d4SJoe Perches my $deleted = { 2132f2d7e4d4SJoe Perches LINENR => $linenr, 2133f2d7e4d4SJoe Perches LINE => $line, 2134f2d7e4d4SJoe Perches }; 2135f2d7e4d4SJoe Perches 2136f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2137f2d7e4d4SJoe Perches} 2138f2d7e4d4SJoe Perches 2139de7d4f0eSAndy Whitcroftsub ERROR { 2140cbec18afSJoe Perches my ($type, $msg) = @_; 2141cbec18afSJoe Perches 2142cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2143de7d4f0eSAndy Whitcroft our $clean = 0; 21446c72ffaaSAndy Whitcroft our $cnt_error++; 21453705ce5bSJoe Perches return 1; 2146de7d4f0eSAndy Whitcroft } 21473705ce5bSJoe Perches return 0; 2148773647a0SAndy Whitcroft} 2149de7d4f0eSAndy Whitcroftsub WARN { 2150cbec18afSJoe Perches my ($type, $msg) = @_; 2151cbec18afSJoe Perches 2152cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2153de7d4f0eSAndy Whitcroft our $clean = 0; 21546c72ffaaSAndy Whitcroft our $cnt_warn++; 21553705ce5bSJoe Perches return 1; 2156de7d4f0eSAndy Whitcroft } 21573705ce5bSJoe Perches return 0; 2158773647a0SAndy Whitcroft} 2159de7d4f0eSAndy Whitcroftsub CHK { 2160cbec18afSJoe Perches my ($type, $msg) = @_; 2161cbec18afSJoe Perches 2162cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2163de7d4f0eSAndy Whitcroft our $clean = 0; 21646c72ffaaSAndy Whitcroft our $cnt_chk++; 21653705ce5bSJoe Perches return 1; 21666c72ffaaSAndy Whitcroft } 21673705ce5bSJoe Perches return 0; 2168de7d4f0eSAndy Whitcroft} 2169de7d4f0eSAndy Whitcroft 21706ecd9674SAndy Whitcroftsub check_absolute_file { 21716ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 21726ecd9674SAndy Whitcroft my $file = $absolute; 21736ecd9674SAndy Whitcroft 21746ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 21756ecd9674SAndy Whitcroft 21766ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 21776ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 21786ecd9674SAndy Whitcroft if (-f "$root/$file") { 21796ecd9674SAndy Whitcroft ##print "file<$file>\n"; 21806ecd9674SAndy Whitcroft last; 21816ecd9674SAndy Whitcroft } 21826ecd9674SAndy Whitcroft } 21836ecd9674SAndy Whitcroft if (! -f _) { 21846ecd9674SAndy Whitcroft return 0; 21856ecd9674SAndy Whitcroft } 21866ecd9674SAndy Whitcroft 21876ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 21886ecd9674SAndy Whitcroft my $prefix = $absolute; 21896ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 21906ecd9674SAndy Whitcroft 21916ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 21926ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2193000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2194000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 21956ecd9674SAndy Whitcroft } 21966ecd9674SAndy Whitcroft} 21976ecd9674SAndy Whitcroft 21983705ce5bSJoe Perchessub trim { 21993705ce5bSJoe Perches my ($string) = @_; 22003705ce5bSJoe Perches 2201b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2202b34c648bSJoe Perches 2203b34c648bSJoe Perches return $string; 2204b34c648bSJoe Perches} 2205b34c648bSJoe Perches 2206b34c648bSJoe Perchessub ltrim { 2207b34c648bSJoe Perches my ($string) = @_; 2208b34c648bSJoe Perches 2209b34c648bSJoe Perches $string =~ s/^\s+//; 2210b34c648bSJoe Perches 2211b34c648bSJoe Perches return $string; 2212b34c648bSJoe Perches} 2213b34c648bSJoe Perches 2214b34c648bSJoe Perchessub rtrim { 2215b34c648bSJoe Perches my ($string) = @_; 2216b34c648bSJoe Perches 2217b34c648bSJoe Perches $string =~ s/\s+$//; 22183705ce5bSJoe Perches 22193705ce5bSJoe Perches return $string; 22203705ce5bSJoe Perches} 22213705ce5bSJoe Perches 222252ea8506SJoe Perchessub string_find_replace { 222352ea8506SJoe Perches my ($string, $find, $replace) = @_; 222452ea8506SJoe Perches 222552ea8506SJoe Perches $string =~ s/$find/$replace/g; 222652ea8506SJoe Perches 222752ea8506SJoe Perches return $string; 222852ea8506SJoe Perches} 222952ea8506SJoe Perches 22303705ce5bSJoe Perchessub tabify { 22313705ce5bSJoe Perches my ($leading) = @_; 22323705ce5bSJoe Perches 22333705ce5bSJoe Perches my $source_indent = 8; 22343705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 22353705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 22363705ce5bSJoe Perches 22373705ce5bSJoe Perches #convert leading spaces to tabs 22383705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 22393705ce5bSJoe Perches #Remove spaces before a tab 22403705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 22413705ce5bSJoe Perches 22423705ce5bSJoe Perches return "$leading"; 22433705ce5bSJoe Perches} 22443705ce5bSJoe Perches 2245d1fe9c09SJoe Perchessub pos_last_openparen { 2246d1fe9c09SJoe Perches my ($line) = @_; 2247d1fe9c09SJoe Perches 2248d1fe9c09SJoe Perches my $pos = 0; 2249d1fe9c09SJoe Perches 2250d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2251d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2252d1fe9c09SJoe Perches 2253d1fe9c09SJoe Perches my $last_openparen = 0; 2254d1fe9c09SJoe Perches 2255d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2256d1fe9c09SJoe Perches return -1; 2257d1fe9c09SJoe Perches } 2258d1fe9c09SJoe Perches 2259d1fe9c09SJoe Perches my $len = length($line); 2260d1fe9c09SJoe Perches 2261d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2262d1fe9c09SJoe Perches my $string = substr($line, $pos); 2263d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2264d1fe9c09SJoe Perches $pos += length($1) - 1; 2265d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2266d1fe9c09SJoe Perches $last_openparen = $pos; 2267d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2268d1fe9c09SJoe Perches last; 2269d1fe9c09SJoe Perches } 2270d1fe9c09SJoe Perches } 2271d1fe9c09SJoe Perches 227291cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2273d1fe9c09SJoe Perches} 2274d1fe9c09SJoe Perches 22750a920b5bSAndy Whitcroftsub process { 22760a920b5bSAndy Whitcroft my $filename = shift; 22770a920b5bSAndy Whitcroft 22780a920b5bSAndy Whitcroft my $linenr=0; 22790a920b5bSAndy Whitcroft my $prevline=""; 2280c2fdda0dSAndy Whitcroft my $prevrawline=""; 22810a920b5bSAndy Whitcroft my $stashline=""; 2282c2fdda0dSAndy Whitcroft my $stashrawline=""; 22830a920b5bSAndy Whitcroft 22844a0df2efSAndy Whitcroft my $length; 22850a920b5bSAndy Whitcroft my $indent; 22860a920b5bSAndy Whitcroft my $previndent=0; 22870a920b5bSAndy Whitcroft my $stashindent=0; 22880a920b5bSAndy Whitcroft 2289de7d4f0eSAndy Whitcroft our $clean = 1; 22900a920b5bSAndy Whitcroft my $signoff = 0; 2291cd261496SGeert Uytterhoeven my $author = ''; 2292cd261496SGeert Uytterhoeven my $authorsignoff = 0; 22930a920b5bSAndy Whitcroft my $is_patch = 0; 2294133712a2SRob Herring my $is_binding_patch = -1; 229529ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 229615662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 2297ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2298490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2299bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 23002a076f40SJoe Perches my $commit_log_long_line = 0; 2301e518e9a5SJoe Perches my $commit_log_has_diff = 0; 230213f1937eSJoe Perches my $reported_maintainer_file = 0; 2303fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2304fa64205dSPasi Savanainen 2305365dd4eaSJoe Perches my $last_blank_line = 0; 23065e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2307365dd4eaSJoe Perches 230813214adfSAndy Whitcroft our @report = (); 23096c72ffaaSAndy Whitcroft our $cnt_lines = 0; 23106c72ffaaSAndy Whitcroft our $cnt_error = 0; 23116c72ffaaSAndy Whitcroft our $cnt_warn = 0; 23126c72ffaaSAndy Whitcroft our $cnt_chk = 0; 23136c72ffaaSAndy Whitcroft 23140a920b5bSAndy Whitcroft # Trace the real file/line as we go. 23150a920b5bSAndy Whitcroft my $realfile = ''; 23160a920b5bSAndy Whitcroft my $realline = 0; 23170a920b5bSAndy Whitcroft my $realcnt = 0; 23180a920b5bSAndy Whitcroft my $here = ''; 231977cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 23200a920b5bSAndy Whitcroft my $in_comment = 0; 2321c2fdda0dSAndy Whitcroft my $comment_edge = 0; 23220a920b5bSAndy Whitcroft my $first_line = 0; 23231e855726SWolfram Sang my $p1_prefix = ''; 23240a920b5bSAndy Whitcroft 232513214adfSAndy Whitcroft my $prev_values = 'E'; 232613214adfSAndy Whitcroft 232713214adfSAndy Whitcroft # suppression flags 2328773647a0SAndy Whitcroft my %suppress_ifbraces; 2329170d3a22SAndy Whitcroft my %suppress_whiletrailers; 23302b474a1aSAndy Whitcroft my %suppress_export; 23313e469cdcSAndy Whitcroft my $suppress_statement = 0; 2332653d4876SAndy Whitcroft 23337e51f197SJoe Perches my %signatures = (); 2334323c1260SJoe Perches 2335c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2336de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2337c2fdda0dSAndy Whitcroft # 2338de7d4f0eSAndy Whitcroft my @setup_docs = (); 2339de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2340773647a0SAndy Whitcroft 2341d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2342d8b07710SJoe Perches 23439f3a8992SRob Herring my $checklicenseline = 1; 23449f3a8992SRob Herring 2345773647a0SAndy Whitcroft sanitise_line_reset(); 2346c2fdda0dSAndy Whitcroft my $line; 2347c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2348773647a0SAndy Whitcroft $linenr++; 2349773647a0SAndy Whitcroft $line = $rawline; 2350c2fdda0dSAndy Whitcroft 23513705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 23523705ce5bSJoe Perches 2353773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2354de7d4f0eSAndy Whitcroft $setup_docs = 0; 23558c27ceffSMauro Carvalho Chehab if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { 2356de7d4f0eSAndy Whitcroft $setup_docs = 1; 2357de7d4f0eSAndy Whitcroft } 2358773647a0SAndy Whitcroft #next; 2359de7d4f0eSAndy Whitcroft } 236074fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2361773647a0SAndy Whitcroft $realline=$1-1; 2362773647a0SAndy Whitcroft if (defined $2) { 2363773647a0SAndy Whitcroft $realcnt=$3+1; 2364773647a0SAndy Whitcroft } else { 2365773647a0SAndy Whitcroft $realcnt=1+1; 2366773647a0SAndy Whitcroft } 2367c45dcabdSAndy Whitcroft $in_comment = 0; 2368773647a0SAndy Whitcroft 2369773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2370773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2371773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2372773647a0SAndy Whitcroft # at context start. 2373773647a0SAndy Whitcroft my $edge; 237401fa9147SAndy Whitcroft my $cnt = $realcnt; 237501fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 237601fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 237701fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 237801fa9147SAndy Whitcroft $cnt--; 237901fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2380721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2381fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2382fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2383fae17daeSAndy Whitcroft ($edge) = $1; 2384fae17daeSAndy Whitcroft last; 2385fae17daeSAndy Whitcroft } 2386773647a0SAndy Whitcroft } 2387773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2388773647a0SAndy Whitcroft $in_comment = 1; 2389773647a0SAndy Whitcroft } 2390773647a0SAndy Whitcroft 2391773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2392773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2393773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2394773647a0SAndy Whitcroft if (!defined $edge && 239583242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2396773647a0SAndy Whitcroft { 2397773647a0SAndy Whitcroft $in_comment = 1; 2398773647a0SAndy Whitcroft } 2399773647a0SAndy Whitcroft 2400773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2401773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2402773647a0SAndy Whitcroft 2403171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2404773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2405171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2406773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2407773647a0SAndy Whitcroft } 2408773647a0SAndy Whitcroft push(@lines, $line); 2409773647a0SAndy Whitcroft 2410773647a0SAndy Whitcroft if ($realcnt > 1) { 2411773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2412773647a0SAndy Whitcroft } else { 2413773647a0SAndy Whitcroft $realcnt = 0; 2414773647a0SAndy Whitcroft } 2415773647a0SAndy Whitcroft 2416773647a0SAndy Whitcroft #print "==>$rawline\n"; 2417773647a0SAndy Whitcroft #print "-->$line\n"; 2418de7d4f0eSAndy Whitcroft 2419de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2420de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2421de7d4f0eSAndy Whitcroft } 2422de7d4f0eSAndy Whitcroft } 2423de7d4f0eSAndy Whitcroft 24246c72ffaaSAndy Whitcroft $prefix = ''; 24256c72ffaaSAndy Whitcroft 2426773647a0SAndy Whitcroft $realcnt = 0; 2427773647a0SAndy Whitcroft $linenr = 0; 2428194f66fcSJoe Perches $fixlinenr = -1; 24290a920b5bSAndy Whitcroft foreach my $line (@lines) { 24300a920b5bSAndy Whitcroft $linenr++; 2431194f66fcSJoe Perches $fixlinenr++; 24321b5539b1SJoe Perches my $sline = $line; #copy of $line 24331b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 24340a920b5bSAndy Whitcroft 2435c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 24366c72ffaaSAndy Whitcroft 243712c253abSJoe Perches# check if it's a mode change, rename or start of a patch 243812c253abSJoe Perches if (!$in_commit_log && 243912c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 244012c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 244112c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 244212c253abSJoe Perches $is_patch = 1; 244312c253abSJoe Perches } 244412c253abSJoe Perches 24450a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2446e518e9a5SJoe Perches if (!$in_commit_log && 244774fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 244874fd4f34SJoe Perches my $context = $4; 24490a920b5bSAndy Whitcroft $is_patch = 1; 24504a0df2efSAndy Whitcroft $first_line = $linenr + 1; 24510a920b5bSAndy Whitcroft $realline=$1-1; 24520a920b5bSAndy Whitcroft if (defined $2) { 24530a920b5bSAndy Whitcroft $realcnt=$3+1; 24540a920b5bSAndy Whitcroft } else { 24550a920b5bSAndy Whitcroft $realcnt=1+1; 24560a920b5bSAndy Whitcroft } 2457c2fdda0dSAndy Whitcroft annotate_reset(); 245813214adfSAndy Whitcroft $prev_values = 'E'; 245913214adfSAndy Whitcroft 2460773647a0SAndy Whitcroft %suppress_ifbraces = (); 2461170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 24622b474a1aSAndy Whitcroft %suppress_export = (); 24633e469cdcSAndy Whitcroft $suppress_statement = 0; 246474fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 246574fd4f34SJoe Perches $context_function = $1; 246674fd4f34SJoe Perches } else { 246774fd4f34SJoe Perches undef $context_function; 246874fd4f34SJoe Perches } 24690a920b5bSAndy Whitcroft next; 24700a920b5bSAndy Whitcroft 24714a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 24724a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 24734a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2474773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 24750a920b5bSAndy Whitcroft $realline++; 2476d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 24770a920b5bSAndy Whitcroft 24784a0df2efSAndy Whitcroft # Measure the line length and indent. 2479c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 24800a920b5bSAndy Whitcroft 24810a920b5bSAndy Whitcroft # Track the previous line. 24820a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 24830a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2484c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2485c2fdda0dSAndy Whitcroft 2486773647a0SAndy Whitcroft #warn "line<$line>\n"; 24876c72ffaaSAndy Whitcroft 2488d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2489d8aaf121SAndy Whitcroft $realcnt--; 24900a920b5bSAndy Whitcroft } 24910a920b5bSAndy Whitcroft 2492cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2493cc77cdcaSAndy Whitcroft 24946c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 24956c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2496773647a0SAndy Whitcroft 24972ac73b4fSJoe Perches my $found_file = 0; 2498773647a0SAndy Whitcroft # extract the filename as it passes 24993bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 25003bf9a009SRabin Vincent $realfile = $1; 25012b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2502270c49a0SJoe Perches $in_commit_log = 0; 25032ac73b4fSJoe Perches $found_file = 1; 25043bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2505773647a0SAndy Whitcroft $realfile = $1; 25062b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2507270c49a0SJoe Perches $in_commit_log = 0; 25081e855726SWolfram Sang 25091e855726SWolfram Sang $p1_prefix = $1; 2510e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2511e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2512000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2513000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 25141e855726SWolfram Sang } 2515773647a0SAndy Whitcroft 2516c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2517000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2518000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2519773647a0SAndy Whitcroft } 25202ac73b4fSJoe Perches $found_file = 1; 25212ac73b4fSJoe Perches } 25222ac73b4fSJoe Perches 252334d8815fSJoe Perches#make up the handle for any error we report on this line 252434d8815fSJoe Perches if ($showfile) { 252534d8815fSJoe Perches $prefix = "$realfile:$realline: " 252634d8815fSJoe Perches } elsif ($emacs) { 25277d3a9f67SJoe Perches if ($file) { 25287d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 25297d3a9f67SJoe Perches } else { 253034d8815fSJoe Perches $prefix = "$filename:$linenr: "; 253134d8815fSJoe Perches } 25327d3a9f67SJoe Perches } 253334d8815fSJoe Perches 25342ac73b4fSJoe Perches if ($found_file) { 253585b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 253685b0ee18SJoe Perches WARN("OBSOLETE", 253785b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 253885b0ee18SJoe Perches } 25397bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 25402ac73b4fSJoe Perches $check = 1; 25412ac73b4fSJoe Perches } else { 25422ac73b4fSJoe Perches $check = $check_orig; 25432ac73b4fSJoe Perches } 25449f3a8992SRob Herring $checklicenseline = 1; 2545133712a2SRob Herring 2546133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2547133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2548133712a2SRob Herring 2549133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2550133712a2SRob Herring 2551133712a2SRob Herring if (($last_binding_patch != -1) && 2552133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2553133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2554133712a2SRob Herring "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n"); 2555133712a2SRob Herring } 2556133712a2SRob Herring } 2557133712a2SRob Herring 2558773647a0SAndy Whitcroft next; 2559773647a0SAndy Whitcroft } 2560773647a0SAndy Whitcroft 2561389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 25620a920b5bSAndy Whitcroft 2563c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2564c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2565c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 25660a920b5bSAndy Whitcroft 25676c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 25686c72ffaaSAndy Whitcroft 2569490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2570490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2571490b292cSJoe Perches if ($in_commit_log) { 2572490b292cSJoe Perches if ($line !~ /^\s*$/) { 2573490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2574490b292cSJoe Perches } 2575490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2576490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2577490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2578490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2579490b292cSJoe Perches } 2580490b292cSJoe Perches 2581e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2582e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2583e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2584e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2585e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2586e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2587e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2588e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2589e518e9a5SJoe Perches $commit_log_has_diff = 1; 2590e518e9a5SJoe Perches } 2591e518e9a5SJoe Perches 25923bf9a009SRabin Vincent# Check for incorrect file permissions 25933bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 25943bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 259504db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 259604db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2597000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2598000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 25993bf9a009SRabin Vincent } 26003bf9a009SRabin Vincent } 26013bf9a009SRabin Vincent 2602cd261496SGeert Uytterhoeven# Check the patch for a From: 2603cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2604cd261496SGeert Uytterhoeven $author = $1; 2605cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2606cd261496SGeert Uytterhoeven $author =~ s/"//g; 2607cd261496SGeert Uytterhoeven } 2608cd261496SGeert Uytterhoeven 260920112475SJoe Perches# Check the patch for a signoff: 2610d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 26114a0df2efSAndy Whitcroft $signoff++; 261215662b3eSJoe Perches $in_commit_log = 0; 2613cd261496SGeert Uytterhoeven if ($author ne '') { 2614cd261496SGeert Uytterhoeven my $l = $line; 2615cd261496SGeert Uytterhoeven $l =~ s/"//g; 2616cd261496SGeert Uytterhoeven if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) { 2617cd261496SGeert Uytterhoeven $authorsignoff = 1; 2618cd261496SGeert Uytterhoeven } 2619cd261496SGeert Uytterhoeven } 26200a920b5bSAndy Whitcroft } 262120112475SJoe Perches 2622e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2623e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2624e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2625e0d975b1SJoe Perches $reported_maintainer_file = 1; 2626e0d975b1SJoe Perches } 2627e0d975b1SJoe Perches 262820112475SJoe Perches# Check signature styles 2629270c49a0SJoe Perches if (!$in_header_lines && 2630ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 263120112475SJoe Perches my $space_before = $1; 263220112475SJoe Perches my $sign_off = $2; 263320112475SJoe Perches my $space_after = $3; 263420112475SJoe Perches my $email = $4; 263520112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 263620112475SJoe Perches 2637ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2638ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2639ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2640ce0338dfSJoe Perches } 264120112475SJoe Perches if (defined $space_before && $space_before ne "") { 26423705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26433705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 26443705ce5bSJoe Perches $fix) { 2645194f66fcSJoe Perches $fixed[$fixlinenr] = 26463705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26473705ce5bSJoe Perches } 264820112475SJoe Perches } 264920112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 26503705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26513705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 26523705ce5bSJoe Perches $fix) { 2653194f66fcSJoe Perches $fixed[$fixlinenr] = 26543705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26553705ce5bSJoe Perches } 26563705ce5bSJoe Perches 265720112475SJoe Perches } 265820112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 26593705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26603705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 26613705ce5bSJoe Perches $fix) { 2662194f66fcSJoe Perches $fixed[$fixlinenr] = 26633705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26643705ce5bSJoe Perches } 266520112475SJoe Perches } 266620112475SJoe Perches 266720112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 266820112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 266920112475SJoe Perches if ($suggested_email eq "") { 2670000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2671000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 267220112475SJoe Perches } else { 267320112475SJoe Perches my $dequoted = $suggested_email; 267420112475SJoe Perches $dequoted =~ s/^"//; 267520112475SJoe Perches $dequoted =~ s/" </ </; 267620112475SJoe Perches # Don't force email to have quotes 267720112475SJoe Perches # Allow just an angle bracketed address 267820112475SJoe Perches if ("$dequoted$comment" ne $email && 267920112475SJoe Perches "<$email_address>$comment" ne $email && 268020112475SJoe Perches "$suggested_email$comment" ne $email) { 2681000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2682000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 268320112475SJoe Perches } 26840a920b5bSAndy Whitcroft } 26857e51f197SJoe Perches 26867e51f197SJoe Perches# Check for duplicate signatures 26877e51f197SJoe Perches my $sig_nospace = $line; 26887e51f197SJoe Perches $sig_nospace =~ s/\s//g; 26897e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 26907e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 26917e51f197SJoe Perches WARN("BAD_SIGN_OFF", 26927e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 26937e51f197SJoe Perches } else { 26947e51f197SJoe Perches $signatures{$sig_nospace} = 1; 26957e51f197SJoe Perches } 26966c5d24eeSSean Christopherson 26976c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 26986c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 26996c5d24eeSSean Christopherson if ($email eq $author) { 27006c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27016c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 27026c5d24eeSSean Christopherson } 27036c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 27046c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27056c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 27066c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 27076c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27086c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27096c5d24eeSSean Christopherson } elsif ($1 ne $email) { 27106c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27116c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27126c5d24eeSSean Christopherson } 27136c5d24eeSSean Christopherson } 27140a920b5bSAndy Whitcroft } 27150a920b5bSAndy Whitcroft 2716a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2717a2fe16b9SJoe Perches if ($in_header_lines && 2718a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2719a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2720a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2721a2fe16b9SJoe Perches } 2722a2fe16b9SJoe Perches 27237ebd05efSChristopher Covington# Check for unwanted Gerrit info 27247ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 27257ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 27267ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 27277ebd05efSChristopher Covington } 27287ebd05efSChristopher Covington 2729369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2730369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2731369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2732369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2733369c8dd3SJoe Perches # timestamp 2734634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 2735634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 2736634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 2737634cffccSJoe Perches # stack dump address styles 2738369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2739369c8dd3SJoe Perches } 2740369c8dd3SJoe Perches 27412a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 27422a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2743bf4daf12SJoe Perches length($line) > 75 && 2744bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2745bf4daf12SJoe Perches # file delta changes 2746bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2747bf4daf12SJoe Perches # filename then : 2748bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2749bf4daf12SJoe Perches # A Fixes: or Link: line 2750bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 27512a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 27522a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 27532a076f40SJoe Perches $commit_log_long_line = 1; 27542a076f40SJoe Perches } 27552a076f40SJoe Perches 2756bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2757bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2758bf4daf12SJoe Perches $line =~ /^\s*$/) { 2759bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2760bf4daf12SJoe Perches } 2761bf4daf12SJoe Perches 27620d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2763369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2764aab38f51SJoe Perches $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i && 2765e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2766fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2767aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2768369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2769bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2770fe043ea1SJoe Perches my $init_char = "c"; 2771fe043ea1SJoe Perches my $orig_commit = ""; 27720d7835fcSJoe Perches my $short = 1; 27730d7835fcSJoe Perches my $long = 0; 27740d7835fcSJoe Perches my $case = 1; 27750d7835fcSJoe Perches my $space = 1; 27760d7835fcSJoe Perches my $hasdesc = 0; 277719c146a6SJoe Perches my $hasparens = 0; 27780d7835fcSJoe Perches my $id = '0123456789ab'; 27790d7835fcSJoe Perches my $orig_desc = "commit description"; 27800d7835fcSJoe Perches my $description = ""; 27810d7835fcSJoe Perches 2782fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2783fe043ea1SJoe Perches $init_char = $1; 2784fe043ea1SJoe Perches $orig_commit = lc($2); 2785fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2786fe043ea1SJoe Perches $orig_commit = lc($1); 2787fe043ea1SJoe Perches } 2788fe043ea1SJoe Perches 27890d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 27900d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 27910d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 27920d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 27930d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 27940d7835fcSJoe Perches $orig_desc = $1; 279519c146a6SJoe Perches $hasparens = 1; 27960d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 27970d7835fcSJoe Perches defined $rawlines[$linenr] && 27980d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 27990d7835fcSJoe Perches $orig_desc = $1; 280019c146a6SJoe Perches $hasparens = 1; 2801b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2802b671fde0SJoe Perches defined $rawlines[$linenr] && 2803b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2804b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2805b671fde0SJoe Perches $orig_desc = $1; 2806b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2807b671fde0SJoe Perches $orig_desc .= " " . $1; 280819c146a6SJoe Perches $hasparens = 1; 28090d7835fcSJoe Perches } 28100d7835fcSJoe Perches 28110d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 28120d7835fcSJoe Perches $id, $orig_desc); 28130d7835fcSJoe Perches 2814948b133aSHeinrich Schuchardt if (defined($id) && 2815948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2816d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 28170d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 28180d7835fcSJoe Perches } 2819d311cd44SJoe Perches } 2820d311cd44SJoe Perches 282113f1937eSJoe Perches# Check for added, moved or deleted files 282213f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 282313f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 282413f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 282513f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 282613f1937eSJoe Perches (defined($1) || defined($2))))) { 2827a82603a8SAndrew Jeffery $is_patch = 1; 282813f1937eSJoe Perches $reported_maintainer_file = 1; 282913f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 283013f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 283113f1937eSJoe Perches } 283213f1937eSJoe Perches 2833e400edb1SRob Herring# Check for adding new DT bindings not in schema format 2834e400edb1SRob Herring if (!$in_commit_log && 2835e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 2836e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 2837e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 2838e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 2839e400edb1SRob Herring } 2840e400edb1SRob Herring 284100df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 28428905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2843000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2844000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 28456c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2846de7d4f0eSAndy Whitcroft } 2847de7d4f0eSAndy Whitcroft 2848de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2849de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2850171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2851171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2852171ae1a4SAndy Whitcroft 2853171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2854171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2855171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2856171ae1a4SAndy Whitcroft 285734d99219SJoe Perches CHK("INVALID_UTF8", 2858000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 285900df344fSAndy Whitcroft } 28600a920b5bSAndy Whitcroft 286115662b3eSJoe Perches# Check if it's the start of a commit log 286215662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 286315662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2864eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2865eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 286615662b3eSJoe Perches $in_header_lines = 0; 286715662b3eSJoe Perches $in_commit_log = 1; 2868ed43c4e5SAllen Hubbe $has_commit_log = 1; 286915662b3eSJoe Perches } 287015662b3eSJoe Perches 2871fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2872fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2873fa64205dSPasi Savanainen if ($in_header_lines && 2874fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2875fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2876fa64205dSPasi Savanainen $non_utf8_charset = 1; 2877fa64205dSPasi Savanainen } 2878fa64205dSPasi Savanainen 2879fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 288015662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2881fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 288215662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 288315662b3eSJoe Perches } 288415662b3eSJoe Perches 2885d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2886d6430f71SJoe Perches if ($tree && $in_commit_log) { 2887d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2888d6430f71SJoe Perches my $file = $1; 2889d6430f71SJoe Perches 2890d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2891d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2892d6430f71SJoe Perches # 2893d6430f71SJoe Perches } else { 2894d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2895d6430f71SJoe Perches } 2896d6430f71SJoe Perches } 2897d6430f71SJoe Perches } 2898d6430f71SJoe Perches 289966b47b4aSKees Cook# Check for various typo / spelling mistakes 290066d7a382SJoe Perches if (defined($misspellings) && 290166d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2902ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 290366b47b4aSKees Cook my $typo = $1; 290466b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 290566b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 290666b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 29070675a8fbSJean Delvare my $msg_level = \&WARN; 29080675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29090675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 291066b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 291166b47b4aSKees Cook $fix) { 291266b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 291366b47b4aSKees Cook } 291466b47b4aSKees Cook } 291566b47b4aSKees Cook } 291666b47b4aSKees Cook 2917a8dd86bfSMatteo Croce# check for invalid commit id 2918a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 2919a8dd86bfSMatteo Croce my $id; 2920a8dd86bfSMatteo Croce my $description; 2921a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 2922a8dd86bfSMatteo Croce if (!defined($id)) { 2923a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 2924a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 2925a8dd86bfSMatteo Croce } 2926a8dd86bfSMatteo Croce } 2927a8dd86bfSMatteo Croce 292830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 292930670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 293000df344fSAndy Whitcroft 29310a920b5bSAndy Whitcroft#trailing whitespace 29329c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2933c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2934d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2935d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2936d5e616fcSJoe Perches $fix) { 2937194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2938d5e616fcSJoe Perches } 2939c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2940c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29413705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 29423705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 29433705ce5bSJoe Perches $fix) { 2944194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 29453705ce5bSJoe Perches } 29463705ce5bSJoe Perches 2947d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 29480a920b5bSAndy Whitcroft } 29495368df20SAndy Whitcroft 29504783f894SJosh Triplett# Check for FSF mailing addresses. 2951109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 29521bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 29533e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 29543e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 29554783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29560675a8fbSJean Delvare my $msg_level = \&ERROR; 29570675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29580675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 29594783f894SJosh 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) 29604783f894SJosh Triplett } 29614783f894SJosh Triplett 29623354957aSAndi Kleen# check for Kconfig help text having a real description 29639fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 29649fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 29653354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 2966678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 2967678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 2968678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 2969678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 29703354957aSAndi Kleen my $length = 0; 29719fe287d7SAndy Whitcroft my $cnt = $realcnt; 29729fe287d7SAndy Whitcroft my $ln = $linenr + 1; 29739fe287d7SAndy Whitcroft my $f; 2974a1385803SAndy Whitcroft my $is_start = 0; 29759fe287d7SAndy Whitcroft my $is_end = 0; 2976a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 29779fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 29789fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 29799fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 29809fe287d7SAndy Whitcroft 29819fe287d7SAndy Whitcroft next if ($f =~ /^-/); 29828d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2983a1385803SAndy Whitcroft 298486adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 2985a1385803SAndy Whitcroft $is_start = 1; 298684af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 298784af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 298884af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 298984af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 299084af7a61SUlf Magnusson } 2991a1385803SAndy Whitcroft $length = -1; 2992a1385803SAndy Whitcroft } 2993a1385803SAndy Whitcroft 29949fe287d7SAndy Whitcroft $f =~ s/^.//; 29953354957aSAndi Kleen $f =~ s/#.*//; 29963354957aSAndi Kleen $f =~ s/^\s+//; 29973354957aSAndi Kleen next if ($f =~ /^$/); 2998678ae162SUlf Magnusson 2999678ae162SUlf Magnusson # This only checks context lines in the patch 3000678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3001678ae162SUlf Magnusson # positives, even though some of these are 3002678ae162SUlf Magnusson # common words in help texts 3003678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3004678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 30059fe287d7SAndy Whitcroft $is_end = 1; 30069fe287d7SAndy Whitcroft last; 30079fe287d7SAndy Whitcroft } 30083354957aSAndi Kleen $length++; 30093354957aSAndi Kleen } 301056193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3011000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 301256193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 301356193274SVadim Bendebury } 3014a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 30153354957aSAndi Kleen } 30163354957aSAndi Kleen 3017628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form 3018628f91a2SJoe Perches if ($realfile =~ /^MAINTAINERS$/ && 3019628f91a2SJoe Perches $rawline =~ /^\+[A-Z]:/ && 3020628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3021628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3022628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3023628f91a2SJoe Perches $fix) { 3024628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3025628f91a2SJoe Perches } 3026628f91a2SJoe Perches } 3027628f91a2SJoe Perches 3028327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3029327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3030327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3031327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3032327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3033327953e9SChristoph Jaeger } 3034327953e9SChristoph Jaeger 3035c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3036c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3037c68e5878SArnaud Lacombe my $flag = $1; 3038c68e5878SArnaud Lacombe my $replacement = { 3039c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3040c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3041c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3042c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3043c68e5878SArnaud Lacombe }; 3044c68e5878SArnaud Lacombe 3045c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3046c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3047c68e5878SArnaud Lacombe } 3048c68e5878SArnaud Lacombe 3049bff5da43SRob Herring# check for DT compatible documentation 30507dd05b38SFlorian Vaussard if (defined $root && 30517dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 30527dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 30537dd05b38SFlorian Vaussard 3054bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3055bff5da43SRob Herring 3056cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3057852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3058cc93319bSFlorian Vaussard 3059bff5da43SRob Herring foreach my $compat (@compats) { 3060bff5da43SRob Herring my $compat2 = $compat; 3061185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3062185d566bSRob Herring my $compat3 = $compat; 3063185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3064185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3065bff5da43SRob Herring if ( $? >> 8 ) { 3066bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3067bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3068bff5da43SRob Herring } 3069bff5da43SRob Herring 30704fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 30714fbf32a6SFlorian Vaussard my $vendor = $1; 3072852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3073bff5da43SRob Herring if ( $? >> 8 ) { 3074bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3075cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3076bff5da43SRob Herring } 3077bff5da43SRob Herring } 3078bff5da43SRob Herring } 3079bff5da43SRob Herring 30809f3a8992SRob Herring# check for using SPDX license tag at beginning of files 30819f3a8992SRob Herring if ($realline == $checklicenseline) { 30829f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 30839f3a8992SRob Herring $checklicenseline = 2; 30849f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 30859f3a8992SRob Herring my $comment = ""; 30869f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 30879f3a8992SRob Herring $comment = '/*'; 30889f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 30899f3a8992SRob Herring $comment = '//'; 30909f3a8992SRob Herring } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) { 30919f3a8992SRob Herring $comment = '#'; 30929f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 30939f3a8992SRob Herring $comment = '..'; 30949f3a8992SRob Herring } 30959f3a8992SRob Herring 3096fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3097fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3098fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3099ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3100fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3101fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3102fdf13693SJoe Perches } 3103fdf13693SJoe Perches 31049f3a8992SRob Herring if ($comment !~ /^$/ && 3105ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 31069f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 31079f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 31083b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 31093b6e8ac9SJoe Perches my $spdx_license = $1; 31103b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 31113b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 31123b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 31133b6e8ac9SJoe Perches } 31149f3a8992SRob Herring } 31159f3a8992SRob Herring } 31169f3a8992SRob Herring } 31179f3a8992SRob Herring 31185368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3119d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 31205368df20SAndy Whitcroft 3121a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3122a8da38a9SJoe Perches if ($realline != $checklicenseline && 3123a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3124a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3125a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3126a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3127a8da38a9SJoe Perches } 3128a8da38a9SJoe Perches 312947e0c88bSJoe Perches# line length limit (with some exclusions) 313047e0c88bSJoe Perches# 313147e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 313247e0c88bSJoe Perches# logging functions like pr_info that end in a string 313347e0c88bSJoe Perches# lines with a single string 313447e0c88bSJoe Perches# #defines that are a single string 31352e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 313647e0c88bSJoe Perches# 313747e0c88bSJoe Perches# There are 3 different line length message types: 3138ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 313947e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 314047e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 314147e0c88bSJoe Perches# 314247e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 314347e0c88bSJoe Perches# 314447e0c88bSJoe Perches 3145b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 314647e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 314747e0c88bSJoe Perches 314847e0c88bSJoe Perches # Check the allowed long line types first 314947e0c88bSJoe Perches 315047e0c88bSJoe Perches # logging functions that end in a string that starts 315147e0c88bSJoe Perches # before $max_line_length 315247e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 315347e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 315447e0c88bSJoe Perches $msg_type = ""; 315547e0c88bSJoe Perches 315647e0c88bSJoe Perches # lines with only strings (w/ possible termination) 315747e0c88bSJoe Perches # #defines with only strings 315847e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 315947e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 316047e0c88bSJoe Perches $msg_type = ""; 316147e0c88bSJoe Perches 3162cc147506SJoe Perches # More special cases 3163cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3164cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3165d560a5f8SJoe Perches $msg_type = ""; 3166d560a5f8SJoe Perches 31672e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 31682e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 31692e4bbbc5SAndreas Brauchli $msg_type = ""; 31702e4bbbc5SAndreas Brauchli 317147e0c88bSJoe Perches # Otherwise set the alternate message types 317247e0c88bSJoe Perches 317347e0c88bSJoe Perches # a comment starts before $max_line_length 317447e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 317547e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 317647e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 317747e0c88bSJoe Perches 317847e0c88bSJoe Perches # a quoted string starts before $max_line_length 317947e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 318047e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 318147e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 318247e0c88bSJoe Perches } 318347e0c88bSJoe Perches 318447e0c88bSJoe Perches if ($msg_type ne "" && 318547e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 318647e0c88bSJoe Perches WARN($msg_type, 31876cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 31880a920b5bSAndy Whitcroft } 318947e0c88bSJoe Perches } 31900a920b5bSAndy Whitcroft 31918905a67cSAndy Whitcroft# check for adding lines without a newline. 31928905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3193000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3194000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 31958905a67cSAndy Whitcroft } 31968905a67cSAndy Whitcroft 3197b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3198de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 31990a920b5bSAndy Whitcroft 32000a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 32010a920b5bSAndy Whitcroft# more than 8 must use tabs. 3202c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3203c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3204c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3205d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 32063705ce5bSJoe Perches if (ERROR("CODE_INDENT", 32073705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 32083705ce5bSJoe Perches $fix) { 3209194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 32103705ce5bSJoe Perches } 32110a920b5bSAndy Whitcroft } 32120a920b5bSAndy Whitcroft 321308e44365SAlberto Panizzo# check for space before tabs. 321408e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 321508e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32163705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 32173705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 32183705ce5bSJoe Perches $fix) { 3219194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3220d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 3221194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3222c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 32233705ce5bSJoe Perches } 322408e44365SAlberto Panizzo } 322508e44365SAlberto Panizzo 32266a487211SJoe Perches# check for assignments on the start of a line 32276a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 32286a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 32296a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 32306a487211SJoe Perches } 32316a487211SJoe Perches 3232d1fe9c09SJoe Perches# check for && or || at the start of a line 3233d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3234d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3235d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3236d1fe9c09SJoe Perches } 3237d1fe9c09SJoe Perches 3238a91e8994SJoe Perches# check indentation starts on a tab stop 32395b57980dSJoe Perches if ($perl_version_ok && 3240bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3241a91e8994SJoe Perches my $indent = length($1); 3242a91e8994SJoe Perches if ($indent % 8) { 3243a91e8994SJoe Perches if (WARN("TABSTOP", 3244a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3245a91e8994SJoe Perches $fix) { 3246a91e8994SJoe Perches $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; 3247a91e8994SJoe Perches } 3248a91e8994SJoe Perches } 3249a91e8994SJoe Perches } 3250a91e8994SJoe Perches 3251d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 32525b57980dSJoe Perches if ($perl_version_ok && 3253fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3254d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3255d1fe9c09SJoe Perches my $oldindent = $1; 3256d1fe9c09SJoe Perches my $rest = $2; 3257d1fe9c09SJoe Perches 3258d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3259d1fe9c09SJoe Perches if ($pos >= 0) { 3260b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3261b34a26f3SJoe Perches my $newindent = $2; 3262d1fe9c09SJoe Perches 3263d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3264d1fe9c09SJoe Perches "\t" x ($pos / 8) . 3265d1fe9c09SJoe Perches " " x ($pos % 8); 3266d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3267d1fe9c09SJoe Perches 3268d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3269d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 32703705ce5bSJoe Perches 32713705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 32723705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 32733705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3274194f66fcSJoe Perches $fixed[$fixlinenr] =~ 32753705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 32763705ce5bSJoe Perches } 3277d1fe9c09SJoe Perches } 3278d1fe9c09SJoe Perches } 3279d1fe9c09SJoe Perches } 3280d1fe9c09SJoe Perches 32816ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 32826ab3a970SJoe Perches# avoid checking a few false positives: 32836ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 32846ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 32856ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 32866ab3a970SJoe Perches# multiline macros that define functions 32876ab3a970SJoe Perches# known attributes or the __attribute__ keyword 32886ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 32896ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 32903705ce5bSJoe Perches if (CHK("SPACING", 3291f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 32923705ce5bSJoe Perches $fix) { 3293194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3294f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 32953705ce5bSJoe Perches } 3296aad4f614SJoe Perches } 3297aad4f614SJoe Perches 329886406b1cSJoe Perches# Block comment styles 329986406b1cSJoe Perches# Networking with an initial /* 330005880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3301fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 330285ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 330385ad978cSJoe Perches $realline > 2) { 330405880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 330505880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 330605880600SJoe Perches } 330705880600SJoe Perches 330886406b1cSJoe Perches# Block comments use * on subsequent lines 330986406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 331086406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3311a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 331261135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3313a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 331486406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 331586406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3316a605e32eSJoe Perches } 3317a605e32eSJoe Perches 331886406b1cSJoe Perches# Block comments use */ on trailing lines 331986406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3320c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3321c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3322c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 332386406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 332486406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 332505880600SJoe Perches } 332605880600SJoe Perches 332708eb9b80SJoe Perches# Block comment * alignment 332808eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3329af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3330af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3331af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 333208eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3333af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3334af207524SJoe Perches my $oldindent; 333508eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3336af207524SJoe Perches if (defined($1)) { 3337af207524SJoe Perches $oldindent = expand_tabs($1); 3338af207524SJoe Perches } else { 3339af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3340af207524SJoe Perches $oldindent = expand_tabs($1); 3341af207524SJoe Perches } 334208eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 334308eb9b80SJoe Perches my $newindent = $1; 334408eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3345af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 334608eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 334708eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 334808eb9b80SJoe Perches } 334908eb9b80SJoe Perches } 335008eb9b80SJoe Perches 33517f619191SJoe Perches# check for missing blank lines after struct/union declarations 33527f619191SJoe Perches# with exceptions for various attributes and macros 33537f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 33547f619191SJoe Perches $line =~ /^\+/ && 33557f619191SJoe Perches !($line =~ /^\+\s*$/ || 33567f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 33577f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 33587f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 33597f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 33607f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 33617f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 33620bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 33637f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3364d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3365d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3366d752fcc8SJoe Perches $fix) { 3367f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3368d752fcc8SJoe Perches } 33697f619191SJoe Perches } 33707f619191SJoe Perches 3371365dd4eaSJoe Perches# check for multiple consecutive blank lines 3372365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3373365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3374365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3375d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3376d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3377d752fcc8SJoe Perches $fix) { 3378f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3379d752fcc8SJoe Perches } 3380d752fcc8SJoe Perches 3381365dd4eaSJoe Perches $last_blank_line = $linenr; 3382365dd4eaSJoe Perches } 3383365dd4eaSJoe Perches 33843b617e3bSJoe Perches# check for missing blank lines after declarations 33853f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 33863f7bac03SJoe Perches # actual declarations 33873f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 33885a4e1fd3SJoe Perches # function pointer declarations 33895a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 33903f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 33913f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 33923f7bac03SJoe Perches # known declaration macros 33933f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 33943f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 33953f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 33963f7bac03SJoe Perches # other possible extensions of declaration lines 33973f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 33983f7bac03SJoe Perches # not starting a section or a macro "\" extended line 33993f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 34003f7bac03SJoe Perches # looks like a declaration 34013f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34025a4e1fd3SJoe Perches # function pointer declarations 34035a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34043f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34053f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34063f7bac03SJoe Perches # known declaration macros 34073f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 34083f7bac03SJoe Perches # start of struct or union or enum 3409328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 34103f7bac03SJoe Perches # start or end of block or continuation of declaration 34113f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 34123f7bac03SJoe Perches # bitfield continuation 34133f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 34143f7bac03SJoe Perches # other possible extensions of declaration lines 34153f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 34163f7bac03SJoe Perches # indentation of previous and current line are the same 34173f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3418d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3419d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3420d752fcc8SJoe Perches $fix) { 3421f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3422d752fcc8SJoe Perches } 34233b617e3bSJoe Perches } 34243b617e3bSJoe Perches 34255f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 34266b4c5bebSAndy Whitcroft# Exceptions: 34276b4c5bebSAndy Whitcroft# 1) within comments 34286b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 34296b4c5bebSAndy Whitcroft# 3) hanging labels 34303705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 34315f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 34323705ce5bSJoe Perches if (WARN("LEADING_SPACE", 34333705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 34343705ce5bSJoe Perches $fix) { 3435194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 34363705ce5bSJoe Perches } 34375f7ddae6SRaffaele Recalcati } 34385f7ddae6SRaffaele Recalcati 3439b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3440b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3441b9ea10d6SAndy Whitcroft 34425751a24eSJoe Perches# check for unusual line ending [ or ( 34435751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 34445751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 34455751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 34465751a24eSJoe Perches } 34475751a24eSJoe Perches 34484dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 34494dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 34504dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 34514dbed76fSJoe Perches $context_function = $1; 34524dbed76fSJoe Perches } 34534dbed76fSJoe Perches 34544dbed76fSJoe Perches# check if this appears to be the end of function declaration 34554dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 34564dbed76fSJoe Perches undef $context_function; 34574dbed76fSJoe Perches } 34584dbed76fSJoe Perches 3459032a4c0fSJoe Perches# check indentation of any line with a bare else 3460840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3461032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3462032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3463032a4c0fSJoe Perches my $tabs = length($1) + 1; 3464840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3465840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3466840080a0SJoe Perches defined $lines[$linenr] && 3467840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3468032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3469032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3470032a4c0fSJoe Perches } 3471032a4c0fSJoe Perches } 3472032a4c0fSJoe Perches 3473c00df19aSJoe Perches# check indentation of a line with a break; 3474c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3475c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3476c00df19aSJoe Perches my $tabs = $1; 3477c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3478c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3479c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3480c00df19aSJoe Perches } 3481c00df19aSJoe Perches } 3482c00df19aSJoe Perches 3483c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3484cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3485000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3486000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3487c2fdda0dSAndy Whitcroft } 348822f2a2efSAndy Whitcroft 348956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 349056e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 349156e77d70SJoe Perches WARN("HOTPLUG_SECTION", 349256e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 349356e77d70SJoe Perches } 349456e77d70SJoe Perches 34959c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 34962b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 34972b474a1aSAndy Whitcroft $realline_next); 34983e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3499ca819864SJoe Perches if ($linenr > $suppress_statement && 35001b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3501170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3502f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3503171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3504171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3505171ae1a4SAndy Whitcroft 35063e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 35073e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 35083e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 35093e469cdcSAndy Whitcroft # until we hit end of it. 35103e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 35113e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 35123e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 35133e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 35143e469cdcSAndy Whitcroft } 3515f74bd194SAndy Whitcroft 35162b474a1aSAndy Whitcroft # Find the real next line. 35172b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 35182b474a1aSAndy Whitcroft if (defined $realline_next && 35192b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 35202b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 35212b474a1aSAndy Whitcroft $realline_next++; 35222b474a1aSAndy Whitcroft } 35232b474a1aSAndy Whitcroft 3524171ae1a4SAndy Whitcroft my $s = $stat; 3525171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3526cf655043SAndy Whitcroft 3527c2fdda0dSAndy Whitcroft # Ignore goto labels. 3528171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3529c2fdda0dSAndy Whitcroft 3530c2fdda0dSAndy Whitcroft # Ignore functions being called 3531171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3532c2fdda0dSAndy Whitcroft 3533463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3534463f2864SAndy Whitcroft 3535c45dcabdSAndy Whitcroft # declarations always start with types 3536d2506586SAndy 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) { 3537c45dcabdSAndy Whitcroft my $type = $1; 3538c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3539c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3540c45dcabdSAndy Whitcroft 35416c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3542a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3543c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3544c2fdda0dSAndy Whitcroft } 35458905a67cSAndy Whitcroft 35466c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 354765863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3548c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 35499c0ca6f9SAndy Whitcroft } 35508905a67cSAndy Whitcroft 35518905a67cSAndy Whitcroft # Check for any sort of function declaration. 35528905a67cSAndy Whitcroft # int foo(something bar, other baz); 35538905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3554171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 35558905a67cSAndy Whitcroft my ($name_len) = length($1); 35568905a67cSAndy Whitcroft 3557cf655043SAndy Whitcroft my $ctx = $s; 3558773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 35598905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3560cf655043SAndy Whitcroft 35618905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3562c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 35638905a67cSAndy Whitcroft 3564c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 35658905a67cSAndy Whitcroft } 35668905a67cSAndy Whitcroft } 35678905a67cSAndy Whitcroft } 35688905a67cSAndy Whitcroft 35699c0ca6f9SAndy Whitcroft } 35709c0ca6f9SAndy Whitcroft 357100df344fSAndy Whitcroft# 357200df344fSAndy Whitcroft# Checks which may be anchored in the context. 357300df344fSAndy Whitcroft# 357400df344fSAndy Whitcroft 357500df344fSAndy Whitcroft# Check for switch () and associated case and default 357600df344fSAndy Whitcroft# statements should be at the same indent. 357700df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 357800df344fSAndy Whitcroft my $err = ''; 357900df344fSAndy Whitcroft my $sep = ''; 358000df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 358100df344fSAndy Whitcroft shift(@ctx); 358200df344fSAndy Whitcroft for my $ctx (@ctx) { 358300df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 358400df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 358500df344fSAndy Whitcroft $indent != $cindent) { 358600df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 358700df344fSAndy Whitcroft $sep = ''; 358800df344fSAndy Whitcroft } else { 358900df344fSAndy Whitcroft $sep = "[...]\n"; 359000df344fSAndy Whitcroft } 359100df344fSAndy Whitcroft } 359200df344fSAndy Whitcroft if ($err ne '') { 3593000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3594000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3595de7d4f0eSAndy Whitcroft } 3596de7d4f0eSAndy Whitcroft } 3597de7d4f0eSAndy Whitcroft 3598de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3599de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 36000fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3601773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3602773647a0SAndy Whitcroft 36039c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 36048eef05ddSJoe Perches 36058eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 36068eef05ddSJoe Perches WARN("DEEP_INDENTATION", 36078eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 36088eef05ddSJoe Perches } 36098eef05ddSJoe Perches 3610de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3611de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3612de7d4f0eSAndy Whitcroft 3613548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3614548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3615de7d4f0eSAndy Whitcroft 3616548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3617548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3618548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3619548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3620548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3621773647a0SAndy Whitcroft $ctx_ln++; 3622773647a0SAndy Whitcroft } 3623548596d5SAndy Whitcroft 362453210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 362553210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3626773647a0SAndy Whitcroft 3627773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3628000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3629000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 363001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 363100df344fSAndy Whitcroft } 3632773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3633773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3634773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3635773647a0SAndy Whitcroft { 36369c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 36379c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3638000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3639000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 364001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 36419c0ca6f9SAndy Whitcroft } 36429c0ca6f9SAndy Whitcroft } 364300df344fSAndy Whitcroft } 364400df344fSAndy Whitcroft 36454d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3646f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 36473e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 36483e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 36493e469cdcSAndy Whitcroft if (!defined $stat); 36504d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 36514d001e4dSAndy Whitcroft 36524d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 36534d001e4dSAndy Whitcroft 36549f5af480SJoe Perches # remove inline comments 36559f5af480SJoe Perches $s =~ s/$;/ /g; 36569f5af480SJoe Perches $c =~ s/$;/ /g; 36574d001e4dSAndy Whitcroft 36584d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 36596f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 36606f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 36614d001e4dSAndy Whitcroft 36629f5af480SJoe Perches # Make sure we remove the line prefixes as we have 36639f5af480SJoe Perches # none on the first line, and are going to readd them 36649f5af480SJoe Perches # where necessary. 36659f5af480SJoe Perches $s =~ s/\n./\n/gs; 36669f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 36679f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 36689f5af480SJoe Perches } 36699f5af480SJoe Perches 36704d001e4dSAndy Whitcroft # We want to check the first line inside the block 36714d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 36724d001e4dSAndy Whitcroft # 1) any blank line termination 36734d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 36744d001e4dSAndy Whitcroft # 3) any do (...) { 36754d001e4dSAndy Whitcroft my $continuation = 0; 36764d001e4dSAndy Whitcroft my $check = 0; 36774d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 36784d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 36794d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 36804d001e4dSAndy Whitcroft $continuation = 1; 36814d001e4dSAndy Whitcroft } 36829bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 36834d001e4dSAndy Whitcroft $check = 1; 36844d001e4dSAndy Whitcroft $cond_lines++; 36854d001e4dSAndy Whitcroft } 36864d001e4dSAndy Whitcroft 36874d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 36884d001e4dSAndy Whitcroft # preprocessor statement. 36894d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 36904d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 36914d001e4dSAndy Whitcroft $check = 0; 36924d001e4dSAndy Whitcroft } 36934d001e4dSAndy Whitcroft 36949bd49efeSAndy Whitcroft my $cond_ptr = -1; 3695740504c6SAndy Whitcroft $continuation = 0; 36969bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 36979bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 36984d001e4dSAndy Whitcroft 3699f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3700f16fa28fSAndy Whitcroft # is not linear. 3701f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3702f16fa28fSAndy Whitcroft $check = 0; 3703f16fa28fSAndy Whitcroft } 3704f16fa28fSAndy Whitcroft 37059bd49efeSAndy Whitcroft # Ignore: 37069bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 37079bd49efeSAndy Whitcroft # 2) preprocessor lines, and 37089bd49efeSAndy Whitcroft # 3) labels. 3709740504c6SAndy Whitcroft if ($continuation || 3710740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 37119bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 37129bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3713740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 371430dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 37159bd49efeSAndy Whitcroft $cond_lines++; 37169bd49efeSAndy Whitcroft } 37174d001e4dSAndy Whitcroft } 371830dad6ebSAndy Whitcroft } 37194d001e4dSAndy Whitcroft 37204d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 37214d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 37224d001e4dSAndy Whitcroft 37234d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 37244d001e4dSAndy Whitcroft # this is not this patch's fault. 37254d001e4dSAndy Whitcroft if (!defined($stat_real) || 37264d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 37274d001e4dSAndy Whitcroft $check = 0; 37284d001e4dSAndy Whitcroft } 37294d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 37304d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 37314d001e4dSAndy Whitcroft } 37324d001e4dSAndy Whitcroft 37339bd49efeSAndy 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"; 37344d001e4dSAndy Whitcroft 37359f5af480SJoe Perches if ($check && $s ne '' && 37369f5af480SJoe Perches (($sindent % 8) != 0 || 37379f5af480SJoe Perches ($sindent < $indent) || 3738f6950a73SJoe Perches ($sindent == $indent && 3739f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 37409f5af480SJoe Perches ($sindent > $indent + 8))) { 3741000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3742000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 37434d001e4dSAndy Whitcroft } 37444d001e4dSAndy Whitcroft } 37454d001e4dSAndy Whitcroft 37466c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 37476c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 37481f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 37491f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 37506c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3751c2fdda0dSAndy Whitcroft if ($dbg_values) { 3752c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3753cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3754cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 37551f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3756c2fdda0dSAndy Whitcroft } 37576c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 37586c72ffaaSAndy Whitcroft 375900df344fSAndy Whitcroft#ignore lines not being added 37603705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 376100df344fSAndy Whitcroft 376211ca40a0SJoe Perches# check for dereferences that span multiple lines 376311ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 376411ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 376511ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 376611ca40a0SJoe Perches my $ref = $1; 376711ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 376811ca40a0SJoe Perches $ref .= $1; 376911ca40a0SJoe Perches $ref =~ s/\s//g; 377011ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 377111ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 377211ca40a0SJoe Perches } 377311ca40a0SJoe Perches 3774a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3775c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3776a1ce18e4SJoe Perches my $type = $1; 3777a1ce18e4SJoe Perches my $var = $2; 3778207a8e84SJoe Perches $var = "" if (!defined $var); 3779207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3780a1ce18e4SJoe Perches my $sign = $1; 3781a1ce18e4SJoe Perches my $pointer = $2; 3782a1ce18e4SJoe Perches 3783a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3784a1ce18e4SJoe Perches 3785a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3786a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3787a1ce18e4SJoe Perches $fix) { 3788a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3789207a8e84SJoe Perches my $comp_pointer = $pointer; 3790207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3791207a8e84SJoe Perches $decl .= $comp_pointer; 3792207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3793207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3794a1ce18e4SJoe Perches } 3795a1ce18e4SJoe Perches } 3796a1ce18e4SJoe Perches } 3797a1ce18e4SJoe Perches 3798653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 37997429c690SAndy Whitcroft if ($dbg_type) { 38007429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3801000d1cc1SJoe Perches ERROR("TEST_TYPE", 3802000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 38037429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3804000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3805000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 38067429c690SAndy Whitcroft } 3807653d4876SAndy Whitcroft next; 3808653d4876SAndy Whitcroft } 3809a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3810a1ef277eSAndy Whitcroft if ($dbg_attr) { 38119360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3812000d1cc1SJoe Perches ERROR("TEST_ATTR", 3813000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 38149360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3815000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3816000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3817a1ef277eSAndy Whitcroft } 3818a1ef277eSAndy Whitcroft next; 3819a1ef277eSAndy Whitcroft } 3820653d4876SAndy Whitcroft 3821f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 382299423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 382399423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3824d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3825d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3826f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3827f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3828f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3829d752fcc8SJoe Perches my $fixedline = $prevrawline; 3830d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3831f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3832d752fcc8SJoe Perches $fixedline = $line; 38338d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3834f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3835d752fcc8SJoe Perches } 3836f0a594c1SAndy Whitcroft } 3837f0a594c1SAndy Whitcroft 383800df344fSAndy Whitcroft# 383900df344fSAndy Whitcroft# Checks which are anchored on the added line. 384000df344fSAndy Whitcroft# 384100df344fSAndy Whitcroft 3842653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3843c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3844653d4876SAndy Whitcroft my $path = $1; 3845653d4876SAndy Whitcroft if ($path =~ m{//}) { 3846000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3847495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3848495e9d84SJoe Perches } 3849495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3850495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3851495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3852653d4876SAndy Whitcroft } 3853653d4876SAndy Whitcroft } 3854653d4876SAndy Whitcroft 385500df344fSAndy Whitcroft# no C99 // comments 385600df344fSAndy Whitcroft if ($line =~ m{//}) { 38573705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 38583705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 38593705ce5bSJoe Perches $fix) { 3860194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 38613705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 38623705ce5bSJoe Perches my $comment = trim($1); 3863194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 38643705ce5bSJoe Perches } 38653705ce5bSJoe Perches } 386600df344fSAndy Whitcroft } 386700df344fSAndy Whitcroft # Remove C99 comments. 38680a920b5bSAndy Whitcroft $line =~ s@//.*@@; 38696c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 38700a920b5bSAndy Whitcroft 38712b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 38722b474a1aSAndy Whitcroft# the whole statement. 38732b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 38742b474a1aSAndy Whitcroft if (defined $realline_next && 38752b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 38762b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 38772b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 38782b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 38793cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 38803cbf62dfSAndy Whitcroft # a prefix: 38813cbf62dfSAndy Whitcroft # XXX(foo); 38823cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3883653d4876SAndy Whitcroft my $name = $1; 388487a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 38853cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 38863cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 38873cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 38883cbf62dfSAndy Whitcroft 38893cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 38902b474a1aSAndy Whitcroft \n.}\s*$| 389148012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 389248012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 389348012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 38942b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 38952b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 389648012058SAndy Whitcroft )/x) { 38972b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 38982b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 38992b474a1aSAndy Whitcroft } else { 39002b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 39010a920b5bSAndy Whitcroft } 39020a920b5bSAndy Whitcroft } 39032b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 39042b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 39052b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 39062b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 39072b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 39082b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 39092b474a1aSAndy Whitcroft } 39102b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 39112b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3912000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3913000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 39142b474a1aSAndy Whitcroft } 39150a920b5bSAndy Whitcroft 39165150bda4SJoe Eloff# check for global initialisers. 39176d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3918d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 39196d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3920d5e616fcSJoe Perches $fix) { 39216d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3922d5e616fcSJoe Perches } 3923f0a594c1SAndy Whitcroft } 39240a920b5bSAndy Whitcroft# check for static initialisers. 39256d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3926d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 39276d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3928d5e616fcSJoe Perches $herecurr) && 3929d5e616fcSJoe Perches $fix) { 39306d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3931d5e616fcSJoe Perches } 39320a920b5bSAndy Whitcroft } 39330a920b5bSAndy Whitcroft 39341813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 39351813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 39361813087dSJoe Perches my $tmp = trim($1); 39371813087dSJoe Perches WARN("MISORDERED_TYPE", 39381813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 39391813087dSJoe Perches } 39401813087dSJoe Perches 3941809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 3942809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 3943809e082eSJoe Perches my $type = trim($1); 3944809e082eSJoe Perches next if ($type !~ /\bint\b/); 3945809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 3946809e082eSJoe Perches my $new_type = $type; 3947809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 3948809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 3949809e082eSJoe Perches $new_type =~ s/^const\s+//; 3950809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 3951809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 3952809e082eSJoe Perches $new_type =~ s/\s+/ /g; 3953809e082eSJoe Perches $new_type = trim($new_type); 3954809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 3955809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 3956809e082eSJoe Perches $fix) { 3957809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 3958809e082eSJoe Perches } 3959809e082eSJoe Perches } 3960809e082eSJoe Perches 3961cb710ecaSJoe Perches# check for static const char * arrays. 3962cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3963000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3964000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3965cb710ecaSJoe Perches $herecurr); 3966cb710ecaSJoe Perches } 3967cb710ecaSJoe Perches 396877b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 396977b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 397077b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 397177b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 397277b8c0a8SJoe Perches $fix) { 397377b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 397477b8c0a8SJoe Perches } 397577b8c0a8SJoe Perches } 397677b8c0a8SJoe Perches 3977cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3978cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3979000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3980000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3981cb710ecaSJoe Perches $herecurr); 3982cb710ecaSJoe Perches } 3983cb710ecaSJoe Perches 3984ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 3985ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3986ab7e23f3SJoe Perches my $found = $1; 3987ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3988ab7e23f3SJoe Perches WARN("CONST_CONST", 3989ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3990ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3991ab7e23f3SJoe Perches WARN("CONST_CONST", 3992ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 3993ab7e23f3SJoe Perches } 3994ab7e23f3SJoe Perches } 3995ab7e23f3SJoe Perches 39969b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 39979b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 39989b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 39999b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 40009b0fa60dSJoe Perches $herecurr); 40019b0fa60dSJoe Perches } 40029b0fa60dSJoe Perches 4003b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4004b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4005b598b670SJoe Perches my $array = $1; 4006b598b670SJoe 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*\))@) { 4007b598b670SJoe Perches my $array_div = $1; 4008b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4009b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4010b598b670SJoe Perches $fix) { 4011b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4012b598b670SJoe Perches } 4013b598b670SJoe Perches } 4014b598b670SJoe Perches } 4015b598b670SJoe Perches 4016b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 4017b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 4018b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4019b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4020b36190c5SJoe Perches $fix) { 4021194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4022b36190c5SJoe Perches } 4023b36190c5SJoe Perches } 4024b36190c5SJoe Perches 4025653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4026653d4876SAndy Whitcroft# make sense. 4027653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 40288054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4029c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 40308ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 403146d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4032000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4033000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 40340a920b5bSAndy Whitcroft } 40350a920b5bSAndy Whitcroft 40360a920b5bSAndy Whitcroft# * goes on variable not on type 403765863862SAndy Whitcroft # (char*[ const]) 4038bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4039bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 40403705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4041d8aaf121SAndy Whitcroft 404265863862SAndy Whitcroft # Should start with a space. 404365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 404465863862SAndy Whitcroft # Should not end with a space. 404565863862SAndy Whitcroft $to =~ s/\s+$//; 404665863862SAndy Whitcroft # '*'s should not have spaces between. 4047f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 404865863862SAndy Whitcroft } 4049d8aaf121SAndy Whitcroft 40503705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 405165863862SAndy Whitcroft if ($from ne $to) { 40523705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 40533705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 40543705ce5bSJoe Perches $fix) { 40553705ce5bSJoe Perches my $sub_from = $ident; 40563705ce5bSJoe Perches my $sub_to = $ident; 40573705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4058194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40593705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 40603705ce5bSJoe Perches } 406165863862SAndy Whitcroft } 4062bfcb2cc7SAndy Whitcroft } 4063bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4064bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 40653705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4066d8aaf121SAndy Whitcroft 406765863862SAndy Whitcroft # Should start with a space. 406865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 406965863862SAndy Whitcroft # Should not end with a space. 407065863862SAndy Whitcroft $to =~ s/\s+$//; 407165863862SAndy Whitcroft # '*'s should not have spaces between. 4072f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 407365863862SAndy Whitcroft } 407465863862SAndy Whitcroft # Modifiers should have spaces. 407565863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 407665863862SAndy Whitcroft 40773705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4078667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 40793705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 40803705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 40813705ce5bSJoe Perches $fix) { 40823705ce5bSJoe Perches 40833705ce5bSJoe Perches my $sub_from = $match; 40843705ce5bSJoe Perches my $sub_to = $match; 40853705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4086194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40873705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 40883705ce5bSJoe Perches } 408965863862SAndy Whitcroft } 40900a920b5bSAndy Whitcroft } 40910a920b5bSAndy Whitcroft 40929d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 40939d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 40940675a8fbSJean Delvare my $msg_level = \&WARN; 40950675a8fbSJean Delvare $msg_level = \&CHK if ($file); 40960675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 40979d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 40989d3e3c70SJoe Perches } 40990a920b5bSAndy Whitcroft 41009d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 41018905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4102000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4103000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 41048905a67cSAndy Whitcroft } 41058905a67cSAndy Whitcroft 410617441227SJoe Perches# check for uses of printk_ratelimit 410717441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4108000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4109000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 411017441227SJoe Perches } 411117441227SJoe Perches 4112eeef5733SJoe Perches# printk should use KERN_* levels 4113eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4114000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4115eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 411600df344fSAndy Whitcroft } 41170a920b5bSAndy Whitcroft 4118243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4119243f3803SJoe Perches my $orig = $1; 4120243f3803SJoe Perches my $level = lc($orig); 4121243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 41228f26b837SJoe Perches my $level2 = $level; 41238f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4124243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4125daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4126243f3803SJoe Perches } 4127243f3803SJoe Perches 4128dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4129dc139313SJoe Perches my $orig = $1; 4130dc139313SJoe Perches my $level = lc($orig); 4131dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4132dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4133dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4134dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4135dc139313SJoe Perches } 4136dc139313SJoe Perches 413791c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 413891c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 413991c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 414091c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 414191c9afafSAndy Lutomirski WARN("ENOSYS", 414291c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 414391c9afafSAndy Lutomirski } 414491c9afafSAndy Lutomirski 4145653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4146653d4876SAndy Whitcroft# or if closed on same line 41475b57980dSJoe Perches if ($perl_version_ok && 41482d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 41492d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 41502d453e3bSJoe Perches $sline !~ /}/) { 41518d182478SJoe Perches if (ERROR("OPEN_BRACE", 41522d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 41538d182478SJoe Perches $fix) { 41548d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 41558d182478SJoe Perches my $fixed_line = $rawline; 41568d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 41578d182478SJoe Perches my $line1 = $1; 41588d182478SJoe Perches my $line2 = $2; 41598d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 41608d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 41618d182478SJoe Perches if ($line2 !~ /^\s*$/) { 41628d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 41638d182478SJoe Perches } 41648d182478SJoe Perches } 41650a920b5bSAndy Whitcroft } 4166653d4876SAndy Whitcroft 41678905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 41688905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 41698905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 41708d182478SJoe Perches if (ERROR("OPEN_BRACE", 41718d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 41728d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 41738d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 41748d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 41758d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 41768d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 41778d182478SJoe Perches $fixedline = $rawline; 41788d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 41798d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 41808d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 41818d182478SJoe Perches } 41828d182478SJoe Perches } 41838905a67cSAndy Whitcroft } 41848905a67cSAndy Whitcroft 41850c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 41863705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 41873705ce5bSJoe Perches if (WARN("SPACING", 41883705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 41893705ce5bSJoe Perches $fix) { 4190194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41913705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 41923705ce5bSJoe Perches } 41930c73b4ebSAndy Whitcroft } 41940c73b4ebSAndy Whitcroft 419531070b5dSJoe Perches# Function pointer declarations 419631070b5dSJoe Perches# check spacing between type, funcptr, and args 419731070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 419891f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 419931070b5dSJoe Perches my $declare = $1; 420031070b5dSJoe Perches my $pre_pointer_space = $2; 420131070b5dSJoe Perches my $post_pointer_space = $3; 420231070b5dSJoe Perches my $funcname = $4; 420331070b5dSJoe Perches my $post_funcname_space = $5; 420431070b5dSJoe Perches my $pre_args_space = $6; 420531070b5dSJoe Perches 420691f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 420791f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 420891f72e9cSJoe Perches# don't need a space so don't warn for those. 420991f72e9cSJoe Perches my $post_declare_space = ""; 421091f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 421191f72e9cSJoe Perches $post_declare_space = $1; 421291f72e9cSJoe Perches $declare = rtrim($declare); 421391f72e9cSJoe Perches } 421491f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 421531070b5dSJoe Perches WARN("SPACING", 421631070b5dSJoe Perches "missing space after return type\n" . $herecurr); 421791f72e9cSJoe Perches $post_declare_space = " "; 421831070b5dSJoe Perches } 421931070b5dSJoe Perches 422031070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 422191f72e9cSJoe Perches# This test is not currently implemented because these declarations are 422291f72e9cSJoe Perches# equivalent to 422391f72e9cSJoe Perches# int foo(int bar, ...) 422491f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 422591f72e9cSJoe Perches# 422691f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 422791f72e9cSJoe Perches# WARN("SPACING", 422891f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 422991f72e9cSJoe Perches# } 423031070b5dSJoe Perches 423131070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 423231070b5dSJoe Perches if (defined $pre_pointer_space && 423331070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 423431070b5dSJoe Perches WARN("SPACING", 423531070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 423631070b5dSJoe Perches } 423731070b5dSJoe Perches 423831070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 423931070b5dSJoe Perches if (defined $post_pointer_space && 424031070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 424131070b5dSJoe Perches WARN("SPACING", 424231070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 424331070b5dSJoe Perches } 424431070b5dSJoe Perches 424531070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 424631070b5dSJoe Perches if (defined $post_funcname_space && 424731070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 424831070b5dSJoe Perches WARN("SPACING", 424931070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 425031070b5dSJoe Perches } 425131070b5dSJoe Perches 425231070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 425331070b5dSJoe Perches if (defined $pre_args_space && 425431070b5dSJoe Perches $pre_args_space =~ /^\s/) { 425531070b5dSJoe Perches WARN("SPACING", 425631070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 425731070b5dSJoe Perches } 425831070b5dSJoe Perches 425931070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4260194f66fcSJoe Perches $fixed[$fixlinenr] =~ 426191f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 426231070b5dSJoe Perches } 426331070b5dSJoe Perches } 426431070b5dSJoe Perches 42658d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 42668d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4267fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4268fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 42698d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 42708d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 42718d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4272fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 427338dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 42743705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 42753705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 42763705ce5bSJoe Perches $fix) { 4277194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42783705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 42793705ce5bSJoe Perches } 42808d31cfceSAndy Whitcroft } 42818d31cfceSAndy Whitcroft } 42828d31cfceSAndy Whitcroft 4283f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 42846c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4285c2fdda0dSAndy Whitcroft my $name = $1; 4286773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4287773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4288c2fdda0dSAndy Whitcroft 4289c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4290773647a0SAndy Whitcroft if ($name =~ /^(?: 4291773647a0SAndy Whitcroft if|for|while|switch|return|case| 4292773647a0SAndy Whitcroft volatile|__volatile__| 4293773647a0SAndy Whitcroft __attribute__|format|__extension__| 4294773647a0SAndy Whitcroft asm|__asm__)$/x) 4295773647a0SAndy Whitcroft { 4296c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4297c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4298c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4299c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4300773647a0SAndy Whitcroft 4301773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4302c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4303c2fdda0dSAndy Whitcroft 4304c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4305c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4306773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4307c2fdda0dSAndy Whitcroft 4308c2fdda0dSAndy Whitcroft } else { 43093705ce5bSJoe Perches if (WARN("SPACING", 43103705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 43113705ce5bSJoe Perches $fix) { 4312194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43133705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 43143705ce5bSJoe Perches } 4315f0a594c1SAndy Whitcroft } 43166c72ffaaSAndy Whitcroft } 43179a4cad4eSEric Nelson 4318653d4876SAndy Whitcroft# Check operator spacing. 43190a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 43203705ce5bSJoe Perches my $fixed_line = ""; 43213705ce5bSJoe Perches my $line_fixed = 0; 43223705ce5bSJoe Perches 43239c0ca6f9SAndy Whitcroft my $ops = qr{ 43249c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 43259c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 43269c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 43271f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 432884731623SJoe Perches \?:|\?|: 43299c0ca6f9SAndy Whitcroft }x; 4330cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 43313705ce5bSJoe Perches 43323705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 43333705ce5bSJoe Perches## foreach my $el (@elements) { 43343705ce5bSJoe Perches## print("el: <$el>\n"); 43353705ce5bSJoe Perches## } 43363705ce5bSJoe Perches 43373705ce5bSJoe Perches my @fix_elements = (); 433800df344fSAndy Whitcroft my $off = 0; 43396c72ffaaSAndy Whitcroft 43403705ce5bSJoe Perches foreach my $el (@elements) { 43413705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 43423705ce5bSJoe Perches $off += length($el); 43433705ce5bSJoe Perches } 43443705ce5bSJoe Perches 43453705ce5bSJoe Perches $off = 0; 43463705ce5bSJoe Perches 43476c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4348b34c648bSJoe Perches my $last_after = -1; 43496c72ffaaSAndy Whitcroft 43500a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 43513705ce5bSJoe Perches 43523705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 43533705ce5bSJoe Perches 43543705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 43553705ce5bSJoe Perches 43564a0df2efSAndy Whitcroft $off += length($elements[$n]); 43574a0df2efSAndy Whitcroft 435825985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4359773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4360773647a0SAndy Whitcroft my $cc = ''; 4361773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4362773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4363773647a0SAndy Whitcroft } 4364773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4365773647a0SAndy Whitcroft 43664a0df2efSAndy Whitcroft my $a = ''; 43674a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 43684a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4369cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 43704a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 43714a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4372773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 43734a0df2efSAndy Whitcroft 43740a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 43754a0df2efSAndy Whitcroft 43764a0df2efSAndy Whitcroft my $c = ''; 43770a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 43784a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 43794a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4380cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 43814a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 43824a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 43838b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 43844a0df2efSAndy Whitcroft } else { 43854a0df2efSAndy Whitcroft $c = 'E'; 43860a920b5bSAndy Whitcroft } 43870a920b5bSAndy Whitcroft 43884a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 43894a0df2efSAndy Whitcroft 43904a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 43914a0df2efSAndy Whitcroft 43926c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4393de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 43940a920b5bSAndy Whitcroft 439574048ed8SAndy Whitcroft # Pull out the value of this operator. 43966c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 43970a920b5bSAndy Whitcroft 43981f65f947SAndy Whitcroft # Get the full operator variant. 43991f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 44001f65f947SAndy Whitcroft 440113214adfSAndy Whitcroft # Ignore operators passed as parameters. 440213214adfSAndy Whitcroft if ($op_type ne 'V' && 4403d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 440413214adfSAndy Whitcroft 4405cf655043SAndy Whitcroft# # Ignore comments 4406cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 440713214adfSAndy Whitcroft 4408d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 440913214adfSAndy Whitcroft } elsif ($op eq ';') { 4410cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4411cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 44123705ce5bSJoe Perches if (ERROR("SPACING", 44133705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4414b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 44153705ce5bSJoe Perches $line_fixed = 1; 44163705ce5bSJoe Perches } 4417d8aaf121SAndy Whitcroft } 4418d8aaf121SAndy Whitcroft 4419d8aaf121SAndy Whitcroft # // is a comment 4420d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 44210a920b5bSAndy Whitcroft 4422b00e4814SJoe Perches # : when part of a bitfield 4423b00e4814SJoe Perches } elsif ($opv eq ':B') { 4424b00e4814SJoe Perches # skip the bitfield test for now 4425b00e4814SJoe Perches 44261f65f947SAndy Whitcroft # No spaces for: 44271f65f947SAndy Whitcroft # -> 4428b00e4814SJoe Perches } elsif ($op eq '->') { 44294a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 44303705ce5bSJoe Perches if (ERROR("SPACING", 44313705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4432b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44333705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 44343705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 44353705ce5bSJoe Perches } 4436b34c648bSJoe Perches $line_fixed = 1; 44373705ce5bSJoe Perches } 44380a920b5bSAndy Whitcroft } 44390a920b5bSAndy Whitcroft 44402381097bSJoe Perches # , must not have a space before and must have a space on the right. 44410a920b5bSAndy Whitcroft } elsif ($op eq ',') { 44422381097bSJoe Perches my $rtrim_before = 0; 44432381097bSJoe Perches my $space_after = 0; 44442381097bSJoe Perches if ($ctx =~ /Wx./) { 44452381097bSJoe Perches if (ERROR("SPACING", 44462381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 44472381097bSJoe Perches $line_fixed = 1; 44482381097bSJoe Perches $rtrim_before = 1; 44492381097bSJoe Perches } 44502381097bSJoe Perches } 4451cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 44523705ce5bSJoe Perches if (ERROR("SPACING", 44533705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 44543705ce5bSJoe Perches $line_fixed = 1; 4455b34c648bSJoe Perches $last_after = $n; 44562381097bSJoe Perches $space_after = 1; 44572381097bSJoe Perches } 44582381097bSJoe Perches } 44592381097bSJoe Perches if ($rtrim_before || $space_after) { 44602381097bSJoe Perches if ($rtrim_before) { 44612381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44622381097bSJoe Perches } else { 44632381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 44642381097bSJoe Perches } 44652381097bSJoe Perches if ($space_after) { 44662381097bSJoe Perches $good .= " "; 44673705ce5bSJoe Perches } 44680a920b5bSAndy Whitcroft } 44690a920b5bSAndy Whitcroft 44709c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 447174048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 44729c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 44739c0ca6f9SAndy Whitcroft 44749c0ca6f9SAndy Whitcroft # unary operators should have a space before and 44759c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 44769c0ca6f9SAndy Whitcroft # unary operator, or a cast 44779c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 447874048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 44790d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4480cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 44813705ce5bSJoe Perches if (ERROR("SPACING", 44823705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4483b34c648bSJoe Perches if ($n != $last_after + 2) { 4484b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 44853705ce5bSJoe Perches $line_fixed = 1; 44863705ce5bSJoe Perches } 44870a920b5bSAndy Whitcroft } 4488b34c648bSJoe Perches } 4489a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4490171ae1a4SAndy Whitcroft # A unary '*' may be const 4491171ae1a4SAndy Whitcroft 4492171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 44933705ce5bSJoe Perches if (ERROR("SPACING", 44943705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4495b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 44963705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 44973705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 44983705ce5bSJoe Perches } 4499b34c648bSJoe Perches $line_fixed = 1; 45003705ce5bSJoe Perches } 45010a920b5bSAndy Whitcroft } 45020a920b5bSAndy Whitcroft 45030a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 45040a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4505773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 45063705ce5bSJoe Perches if (ERROR("SPACING", 45073705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4508b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 45093705ce5bSJoe Perches $line_fixed = 1; 45103705ce5bSJoe Perches } 45110a920b5bSAndy Whitcroft } 4512773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4513773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 45143705ce5bSJoe Perches if (ERROR("SPACING", 45153705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4516b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45173705ce5bSJoe Perches $line_fixed = 1; 45183705ce5bSJoe Perches } 4519653d4876SAndy Whitcroft } 4520773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 45213705ce5bSJoe Perches if (ERROR("SPACING", 45223705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4523b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 45243705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45253705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4526773647a0SAndy Whitcroft } 4527b34c648bSJoe Perches $line_fixed = 1; 45283705ce5bSJoe Perches } 45293705ce5bSJoe Perches } 45300a920b5bSAndy Whitcroft 45310a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 45329c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 45339c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 45349c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4535c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4536c2fdda0dSAndy Whitcroft $op eq '%') 45370a920b5bSAndy Whitcroft { 4538d2e025f3SJoe Perches if ($check) { 4539d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4540d2e025f3SJoe Perches if (CHK("SPACING", 4541d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4542d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4543d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4544d2e025f3SJoe Perches $line_fixed = 1; 4545d2e025f3SJoe Perches } 4546d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4547d2e025f3SJoe Perches if (CHK("SPACING", 4548d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4549d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4550d2e025f3SJoe Perches $line_fixed = 1; 4551d2e025f3SJoe Perches } 4552d2e025f3SJoe Perches } 4553d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 45543705ce5bSJoe Perches if (ERROR("SPACING", 45553705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4556b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4557b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4558b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4559b34c648bSJoe Perches } 45603705ce5bSJoe Perches $line_fixed = 1; 45613705ce5bSJoe Perches } 45620a920b5bSAndy Whitcroft } 45630a920b5bSAndy Whitcroft 45641f65f947SAndy Whitcroft # A colon needs no spaces before when it is 45651f65f947SAndy Whitcroft # terminating a case value or a label. 45661f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 45671f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 45683705ce5bSJoe Perches if (ERROR("SPACING", 45693705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4570b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45713705ce5bSJoe Perches $line_fixed = 1; 45723705ce5bSJoe Perches } 45731f65f947SAndy Whitcroft } 45741f65f947SAndy Whitcroft 45750a920b5bSAndy Whitcroft # All the others need spaces both sides. 4576cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 45771f65f947SAndy Whitcroft my $ok = 0; 45781f65f947SAndy Whitcroft 457922f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 45801f65f947SAndy Whitcroft if (($op eq '<' && 45811f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 45821f65f947SAndy Whitcroft ($op eq '>' && 45831f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 45841f65f947SAndy Whitcroft { 45851f65f947SAndy Whitcroft $ok = 1; 45861f65f947SAndy Whitcroft } 45871f65f947SAndy Whitcroft 4588e0df7e1fSJoe Perches # for asm volatile statements 4589e0df7e1fSJoe Perches # ignore a colon with another 4590e0df7e1fSJoe Perches # colon immediately before or after 4591e0df7e1fSJoe Perches if (($op eq ':') && 4592e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4593e0df7e1fSJoe Perches $ok = 1; 4594e0df7e1fSJoe Perches } 4595e0df7e1fSJoe Perches 459684731623SJoe Perches # messages are ERROR, but ?: are CHK 45971f65f947SAndy Whitcroft if ($ok == 0) { 45980675a8fbSJean Delvare my $msg_level = \&ERROR; 45990675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 460084731623SJoe Perches 46010675a8fbSJean Delvare if (&{$msg_level}("SPACING", 46023705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4603b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4604b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4605b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4606b34c648bSJoe Perches } 46073705ce5bSJoe Perches $line_fixed = 1; 46083705ce5bSJoe Perches } 46090a920b5bSAndy Whitcroft } 461022f2a2efSAndy Whitcroft } 46114a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 46123705ce5bSJoe Perches 46133705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 46143705ce5bSJoe Perches 46153705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 46160a920b5bSAndy Whitcroft } 46173705ce5bSJoe Perches 46183705ce5bSJoe Perches if (($#elements % 2) == 0) { 46193705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 46203705ce5bSJoe Perches } 46213705ce5bSJoe Perches 4622194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4623194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 46243705ce5bSJoe Perches } 46253705ce5bSJoe Perches 46263705ce5bSJoe Perches 46270a920b5bSAndy Whitcroft } 46280a920b5bSAndy Whitcroft 4629786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4630d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4631786b6326SJoe Perches if (WARN("SPACING", 4632786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4633786b6326SJoe Perches $fix) { 4634194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4635786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4636786b6326SJoe Perches } 4637786b6326SJoe Perches } 4638786b6326SJoe Perches 4639f0a594c1SAndy Whitcroft# check for multiple assignments 4640f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4641000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4642000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4643f0a594c1SAndy Whitcroft } 4644f0a594c1SAndy Whitcroft 464522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 464622f2a2efSAndy Whitcroft## # continuation. 464722f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 464822f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 464922f2a2efSAndy Whitcroft## 465022f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 465122f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 465222f2a2efSAndy Whitcroft## my $ln = $line; 465322f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 465422f2a2efSAndy Whitcroft## } 465522f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4656000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4657000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 465822f2a2efSAndy Whitcroft## } 465922f2a2efSAndy Whitcroft## } 4660f0a594c1SAndy Whitcroft 46610a920b5bSAndy Whitcroft#need space before brace following if, while, etc 46626b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 46636ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 46643705ce5bSJoe Perches if (ERROR("SPACING", 46653705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 46663705ce5bSJoe Perches $fix) { 46676ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 46683705ce5bSJoe Perches } 4669de7d4f0eSAndy Whitcroft } 4670de7d4f0eSAndy Whitcroft 4671c4a62ef9SJoe Perches## # check for blank lines before declarations 4672c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4673c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4674c4a62ef9SJoe Perches## WARN("SPACING", 4675c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4676c4a62ef9SJoe Perches## } 4677c4a62ef9SJoe Perches## 4678c4a62ef9SJoe Perches 4679de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4680de7d4f0eSAndy Whitcroft# on the line 468194fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 4682d5e616fcSJoe Perches if (ERROR("SPACING", 4683d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4684d5e616fcSJoe Perches $fix) { 4685194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4686d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4687d5e616fcSJoe Perches } 46880a920b5bSAndy Whitcroft } 46890a920b5bSAndy Whitcroft 469022f2a2efSAndy Whitcroft# check spacing on square brackets 469122f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 46923705ce5bSJoe Perches if (ERROR("SPACING", 46933705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 46943705ce5bSJoe Perches $fix) { 4695194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46963705ce5bSJoe Perches s/\[\s+/\[/; 46973705ce5bSJoe Perches } 469822f2a2efSAndy Whitcroft } 469922f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 47003705ce5bSJoe Perches if (ERROR("SPACING", 47013705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 47023705ce5bSJoe Perches $fix) { 4703194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47043705ce5bSJoe Perches s/\s+\]/\]/; 47053705ce5bSJoe Perches } 470622f2a2efSAndy Whitcroft } 470722f2a2efSAndy Whitcroft 4708c45dcabdSAndy Whitcroft# check spacing on parentheses 47099c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 47109c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 47113705ce5bSJoe Perches if (ERROR("SPACING", 47123705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 47133705ce5bSJoe Perches $fix) { 4714194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47153705ce5bSJoe Perches s/\(\s+/\(/; 47163705ce5bSJoe Perches } 471722f2a2efSAndy Whitcroft } 471813214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4719c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4720c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 47213705ce5bSJoe Perches if (ERROR("SPACING", 47223705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 47233705ce5bSJoe Perches $fix) { 4724194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47253705ce5bSJoe Perches s/\s+\)/\)/; 47263705ce5bSJoe Perches } 472722f2a2efSAndy Whitcroft } 472822f2a2efSAndy Whitcroft 4729e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4730e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4731e2826fd0SJoe Perches 4732e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4733ea4acbb1SJoe Perches my $var = $1; 4734ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4735ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4736ea4acbb1SJoe Perches $fix) { 4737ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4738ea4acbb1SJoe Perches } 4739ea4acbb1SJoe Perches } 4740ea4acbb1SJoe Perches 4741ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4742ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4743ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4744ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4745ea4acbb1SJoe Perches my $var = $2; 4746ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4747ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4748ea4acbb1SJoe Perches $fix) { 4749ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4750ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4751ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4752ea4acbb1SJoe Perches } 4753e2826fd0SJoe Perches } 4754e2826fd0SJoe Perches 475563b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4756a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4757a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 47585b57980dSJoe Perches $perl_version_ok && defined($stat) && 475963b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 476063b7c73eSJoe Perches my $if_stat = $1; 476163b7c73eSJoe Perches my $test = substr($2, 1, -1); 476263b7c73eSJoe Perches my $herectx; 476363b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 476463b7c73eSJoe Perches my $match = $1; 476563b7c73eSJoe Perches # avoid parentheses around potential macro args 476663b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 476763b7c73eSJoe Perches if (!defined($herectx)) { 476863b7c73eSJoe Perches $herectx = $here . "\n"; 476963b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 477063b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 477163b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 477263b7c73eSJoe Perches $herectx .= $rl . "\n"; 477363b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 477463b7c73eSJoe Perches } 477563b7c73eSJoe Perches } 477663b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 477763b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 477863b7c73eSJoe Perches } 477963b7c73eSJoe Perches } 478063b7c73eSJoe Perches 47810a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 47824a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 47830a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 47843705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 47853705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 47863705ce5bSJoe Perches $fix) { 4787194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47883705ce5bSJoe Perches s/^(.)\s+/$1/; 47893705ce5bSJoe Perches } 47900a920b5bSAndy Whitcroft } 47910a920b5bSAndy Whitcroft 47925b9553abSJoe Perches# return is not a function 4793507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4794c45dcabdSAndy Whitcroft my $spacing = $1; 47955b57980dSJoe Perches if ($perl_version_ok && 47965b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 47975b9553abSJoe Perches my $value = $1; 47985b9553abSJoe Perches $value = deparenthesize($value); 47995b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4800000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4801000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 48025b9553abSJoe Perches } 4803c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4804000d1cc1SJoe Perches ERROR("SPACING", 4805000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4806c45dcabdSAndy Whitcroft } 4807c45dcabdSAndy Whitcroft } 4808507e5141SJoe Perches 4809b43ae21bSJoe Perches# unnecessary return in a void function 4810b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4811b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4812b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4813b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4814b43ae21bSJoe Perches $linenr >= 3 && 4815b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4816b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 48179819cf25SJoe Perches WARN("RETURN_VOID", 4818b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 48199819cf25SJoe Perches } 48209819cf25SJoe Perches 4821189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 48225b57980dSJoe Perches if ($perl_version_ok && 4823189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4824189248d8SJoe Perches my $openparens = $1; 4825189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4826189248d8SJoe Perches my $msg = ""; 4827189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4828189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4829189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4830189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4831189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4832189248d8SJoe Perches } 4833189248d8SJoe Perches } 4834189248d8SJoe Perches 4835c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4836c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4837c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4838c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 48395b57980dSJoe Perches if ($perl_version_ok && 4840c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4841c5595fa2SJoe Perches my $lead = $1; 4842c5595fa2SJoe Perches my $const = $2; 4843c5595fa2SJoe Perches my $comp = $3; 4844c5595fa2SJoe Perches my $to = $4; 4845c5595fa2SJoe Perches my $newcomp = $comp; 4846f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4847c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4848c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4849c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4850c5595fa2SJoe Perches $fix) { 4851c5595fa2SJoe Perches if ($comp eq "<") { 4852c5595fa2SJoe Perches $newcomp = ">"; 4853c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4854c5595fa2SJoe Perches $newcomp = ">="; 4855c5595fa2SJoe Perches } elsif ($comp eq ">") { 4856c5595fa2SJoe Perches $newcomp = "<"; 4857c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4858c5595fa2SJoe Perches $newcomp = "<="; 4859c5595fa2SJoe Perches } 4860c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4861c5595fa2SJoe Perches } 4862c5595fa2SJoe Perches } 4863c5595fa2SJoe Perches 4864f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4865f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 486653a3c448SAndy Whitcroft my $name = $1; 486753a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4868000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4869f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 487053a3c448SAndy Whitcroft } 487153a3c448SAndy Whitcroft } 4872c45dcabdSAndy Whitcroft 48730a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 48744a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 48753705ce5bSJoe Perches if (ERROR("SPACING", 48763705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 48773705ce5bSJoe Perches $fix) { 4878194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48793705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 48803705ce5bSJoe Perches } 48810a920b5bSAndy Whitcroft } 48820a920b5bSAndy Whitcroft 4883f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4884f5fe35ddSAndy Whitcroft# statements after the conditional. 4885170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 48863e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 48873e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 48883e469cdcSAndy Whitcroft if (!defined $stat); 4889170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4890170d3a22SAndy Whitcroft $remain_next, $off_next); 4891170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4892170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4893170d3a22SAndy Whitcroft 4894170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4895170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4896170d3a22SAndy Whitcroft # then count those as offsets. 4897170d3a22SAndy Whitcroft my ($whitespace) = 4898170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4899170d3a22SAndy Whitcroft my $offset = 4900170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4901170d3a22SAndy Whitcroft 4902170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4903170d3a22SAndy Whitcroft $offset} = 1; 4904170d3a22SAndy Whitcroft } 4905170d3a22SAndy Whitcroft } 4906170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4907c11230f4SJoe Perches defined($stat) && defined($cond) && 4908170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4909171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 49108905a67cSAndy Whitcroft 4911b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4912000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4913000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 49148905a67cSAndy Whitcroft } 49158905a67cSAndy Whitcroft 49168905a67cSAndy Whitcroft # Find out what is on the end of the line after the 49178905a67cSAndy Whitcroft # conditional. 4918773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 49198905a67cSAndy Whitcroft $s =~ s/\n.*//g; 492013214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 492153210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 492253210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4923773647a0SAndy Whitcroft { 4924bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4925bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4926bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 492742bdf74cSHidetoshi Seto my $stat_real = ''; 4928bb44ad39SAndy Whitcroft 492942bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 493042bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4931bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4932bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4933bb44ad39SAndy Whitcroft } 4934bb44ad39SAndy Whitcroft 4935000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4936000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 49378905a67cSAndy Whitcroft } 49388905a67cSAndy Whitcroft } 49398905a67cSAndy Whitcroft 494013214adfSAndy Whitcroft# Check for bitwise tests written as boolean 494113214adfSAndy Whitcroft if ($line =~ / 494213214adfSAndy Whitcroft (?: 494313214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 494413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 494513214adfSAndy Whitcroft (?:\&\&|\|\|) 494613214adfSAndy Whitcroft | 494713214adfSAndy Whitcroft (?:\&\&|\|\|) 494813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 494913214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 495013214adfSAndy Whitcroft )/x) 495113214adfSAndy Whitcroft { 4952000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4953000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 495413214adfSAndy Whitcroft } 495513214adfSAndy Whitcroft 49568905a67cSAndy Whitcroft# if and else should not have general statements after it 495713214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 495813214adfSAndy Whitcroft my $s = $1; 495913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 496013214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4961000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4962000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 49630a920b5bSAndy Whitcroft } 496413214adfSAndy Whitcroft } 496539667782SAndy Whitcroft# if should not continue a brace 496639667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4967000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4968048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 496939667782SAndy Whitcroft $herecurr); 497039667782SAndy Whitcroft } 4971a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4972a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4973a1080bf8SAndy Whitcroft $line !~ /\G(?: 49743fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4975a1080bf8SAndy Whitcroft \s*return\s+ 4976a1080bf8SAndy Whitcroft )/xg) 4977a1080bf8SAndy Whitcroft { 4978000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4979000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4980a1080bf8SAndy Whitcroft } 49810a920b5bSAndy Whitcroft 49820a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 49830a920b5bSAndy Whitcroft # indent level to be relevant to each other. 49848b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 49850a920b5bSAndy Whitcroft $previndent == $indent) { 49868b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 49878b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 49888b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 49898b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 49908b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 49918b8856f4SJoe Perches my $fixedline = $prevrawline; 49928b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 49938b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 49948b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 49958b8856f4SJoe Perches } 49968b8856f4SJoe Perches $fixedline = $rawline; 49978b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 49988b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 49998b8856f4SJoe Perches } 50000a920b5bSAndy Whitcroft } 50010a920b5bSAndy Whitcroft 50028b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5003c2fdda0dSAndy Whitcroft $previndent == $indent) { 5004c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5005c2fdda0dSAndy Whitcroft 5006c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5007c2fdda0dSAndy Whitcroft # conditional. 5008773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5009c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5010c2fdda0dSAndy Whitcroft 5011c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 50128b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 50138b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 50148b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 50158b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 50168b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 50178b8856f4SJoe Perches my $fixedline = $prevrawline; 50188b8856f4SJoe Perches my $trailing = $rawline; 50198b8856f4SJoe Perches $trailing =~ s/^\+//; 50208b8856f4SJoe Perches $trailing = trim($trailing); 50218b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 50228b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50238b8856f4SJoe Perches } 5024c2fdda0dSAndy Whitcroft } 5025c2fdda0dSAndy Whitcroft } 5026c2fdda0dSAndy Whitcroft 502795e2c602SJoe Perches#Specific variable tests 5028323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5029323c1260SJoe Perches my $var = $1; 503095e2c602SJoe Perches 503195e2c602SJoe Perches#CamelCase 5032807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5033be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 503422735ce8SJoe Perches#Ignore Page<foo> variants 5035807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5036d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5037d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5038d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5039f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5040f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 50417e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 50427e781f67SJoe Perches my $word = $1; 50437e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5044d8b07710SJoe Perches if ($check) { 5045d8b07710SJoe Perches seed_camelcase_includes(); 5046d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5047d8b07710SJoe Perches seed_camelcase_file($realfile); 5048d8b07710SJoe Perches $camelcase_file_seeded = 1; 5049d8b07710SJoe Perches } 5050d8b07710SJoe Perches } 50517e781f67SJoe Perches if (!defined $camelcase{$word}) { 50527e781f67SJoe Perches $camelcase{$word} = 1; 5053be79794bSJoe Perches CHK("CAMELCASE", 50547e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 50557e781f67SJoe Perches } 5056323c1260SJoe Perches } 5057323c1260SJoe Perches } 50583445686aSJoe Perches } 50590a920b5bSAndy Whitcroft 50600a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5061d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5062d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5063d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5064d5e616fcSJoe Perches $fix) { 5065194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5066d5e616fcSJoe Perches } 50670a920b5bSAndy Whitcroft } 50680a920b5bSAndy Whitcroft 50690e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 50700e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5071c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5072e09dec48SAndy Whitcroft my $file = "$1.h"; 5073e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5074e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5075e09dec48SAndy Whitcroft $realfile ne $checkfile && 50767840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5077c45dcabdSAndy Whitcroft { 50780e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 50790e212e0aSFabian Frederick if ($asminclude > 0) { 5080e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5081000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5082000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5083e09dec48SAndy Whitcroft } else { 5084000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5085000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5086e09dec48SAndy Whitcroft } 50870a920b5bSAndy Whitcroft } 50880a920b5bSAndy Whitcroft } 50890e212e0aSFabian Frederick } 50900a920b5bSAndy Whitcroft 5091653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5092653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5093cf655043SAndy Whitcroft# in a known good container 5094b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5095b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5096d8aaf121SAndy Whitcroft my $ln = $linenr; 5097d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5098c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5099c45dcabdSAndy Whitcroft my $ctx = ''; 510008a2843eSJoe Perches my $has_flow_statement = 0; 510108a2843eSJoe Perches my $has_arg_concat = 0; 5102c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5103f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5104f74bd194SAndy Whitcroft $ctx = $dstat; 5105c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5106a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5107c45dcabdSAndy Whitcroft 510808a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 510962e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 511008a2843eSJoe Perches 5111f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5112f59b64bfSJoe Perches my $define_args = $1; 5113f59b64bfSJoe Perches my $define_stmt = $dstat; 5114f59b64bfSJoe Perches my @def_args = (); 5115f59b64bfSJoe Perches 5116f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5117f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5118f59b64bfSJoe Perches $define_args =~ s/\s*//g; 51198c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5120f59b64bfSJoe Perches @def_args = split(",", $define_args); 5121f59b64bfSJoe Perches } 5122f59b64bfSJoe Perches 5123292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5124c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5125c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5126c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5127c45dcabdSAndy Whitcroft 5128c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 5129bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 5130bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 51316b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 5132bf30d6edSAndy Whitcroft { 5133c45dcabdSAndy Whitcroft } 5134c45dcabdSAndy Whitcroft 5135e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 513633acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 513733acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5138e45bab8eSAndy Whitcroft { 5139e45bab8eSAndy Whitcroft } 5140e45bab8eSAndy Whitcroft 514142e15293SJoe Perches # Make asm volatile uses seem like a generic function 514242e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 514342e15293SJoe Perches 5144c45dcabdSAndy Whitcroft my $exceptions = qr{ 5145c45dcabdSAndy Whitcroft $Declare| 5146c45dcabdSAndy Whitcroft module_param_named| 5147a0a0a7a9SKees Cook MODULE_PARM_DESC| 5148c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5149c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5150383099fdSAndy Whitcroft __typeof__\(| 515122fd2d3eSStefani Seibold union| 515222fd2d3eSStefani Seibold struct| 5153ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 51546b10df42SVladimir Zapolskiy ^\"|\"$| 51556b10df42SVladimir Zapolskiy ^\[ 5156c45dcabdSAndy Whitcroft }x; 51575eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5158f59b64bfSJoe Perches 5159f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5160f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5161e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5162f59b64bfSJoe Perches 5163f74bd194SAndy Whitcroft if ($dstat ne '' && 5164f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5165f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 51663cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5167356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5168f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5169f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5170e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 517172f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5172f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5173f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5174f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 51754e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5176f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5177c45dcabdSAndy Whitcroft { 5178e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5179e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5180e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5181e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5182f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5183f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5184f74bd194SAndy Whitcroft } else { 5185000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5186388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5187d8aaf121SAndy Whitcroft } 5188f59b64bfSJoe Perches 5189f59b64bfSJoe Perches } 51905207649bSJoe Perches 51915207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 51925207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 51935207649bSJoe Perches my $first = 1; 51945207649bSJoe Perches $define_stmt = ""; 51955207649bSJoe Perches foreach my $l (@stmt_array) { 51965207649bSJoe Perches $l =~ s/\\$//; 51975207649bSJoe Perches if ($first) { 51985207649bSJoe Perches $define_stmt = $l; 51995207649bSJoe Perches $first = 0; 52005207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 52015207649bSJoe Perches $define_stmt .= substr($l, 1); 52025207649bSJoe Perches } 52035207649bSJoe Perches } 52045207649bSJoe Perches $define_stmt =~ s/$;//g; 52055207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 52065207649bSJoe Perches $define_stmt = trim($define_stmt); 52075207649bSJoe Perches 5208f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5209f59b64bfSJoe Perches foreach my $arg (@def_args) { 5210f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 52119192d41aSJoe Perches next if ($arg =~ /^type$/i); 52127fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 52136dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 52147fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 52157fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5216d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5217f59b64bfSJoe Perches if ($use_cnt > 1) { 5218f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5219f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5220f59b64bfSJoe Perches } 52219192d41aSJoe Perches# check if any macro arguments may have other precedence issues 52227fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 52239192d41aSJoe Perches ((defined($1) && $1 ne ',') || 52249192d41aSJoe Perches (defined($2) && $2 ne ','))) { 52259192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 52269192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 52279192d41aSJoe Perches } 52280a920b5bSAndy Whitcroft } 52295023d347SJoe Perches 523008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 523108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 523208a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 523308a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5234e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 523508a2843eSJoe Perches 523608a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 523708a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 523808a2843eSJoe Perches } 523908a2843eSJoe Perches 5240481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 52415023d347SJoe Perches 52425023d347SJoe Perches } else { 52435023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5244481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5245481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 52465023d347SJoe Perches $line =~ /^\+.*\\$/) { 52475023d347SJoe Perches WARN("LINE_CONTINUATIONS", 52485023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 52495023d347SJoe Perches } 5250653d4876SAndy Whitcroft } 52510a920b5bSAndy Whitcroft 5252b13edf7fSJoe Perches# do {} while (0) macro tests: 5253b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5254b13edf7fSJoe Perches# macro should not end with a semicolon 52555b57980dSJoe Perches if ($perl_version_ok && 5256b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5257b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5258b13edf7fSJoe Perches my $ln = $linenr; 5259b13edf7fSJoe Perches my $cnt = $realcnt; 5260b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5261b13edf7fSJoe Perches my $ctx = ''; 5262b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5263b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5264b13edf7fSJoe Perches $ctx = $dstat; 5265b13edf7fSJoe Perches 5266b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 52671b36b201SJoe Perches $dstat =~ s/$;/ /g; 5268b13edf7fSJoe Perches 5269b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5270b13edf7fSJoe Perches my $stmts = $2; 5271b13edf7fSJoe Perches my $semis = $3; 5272b13edf7fSJoe Perches 5273b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5274b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5275e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5276b13edf7fSJoe Perches 5277ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5278ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5279b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5280b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5281b13edf7fSJoe Perches } 5282b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5283b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5284b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5285b13edf7fSJoe Perches } 5286f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5287f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5288f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5289e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5290f5ef95b1SJoe Perches 5291f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5292f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5293b13edf7fSJoe Perches } 5294b13edf7fSJoe Perches } 5295b13edf7fSJoe Perches 5296f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 529713214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 529813214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5299cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 530013214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5301cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5302cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5303aad4f614SJoe Perches my @allowed = (); 5304aad4f614SJoe Perches my $allow = 0; 530513214adfSAndy Whitcroft my $seen = 0; 5306773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5307cf655043SAndy Whitcroft my $ln = $linenr - 1; 530813214adfSAndy Whitcroft for my $chunk (@chunks) { 530913214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 531013214adfSAndy Whitcroft 5311773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5312773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5313773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5314773647a0SAndy Whitcroft 5315aad4f614SJoe Perches $allowed[$allow] = 0; 5316773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5317773647a0SAndy Whitcroft 5318773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5319773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5320773647a0SAndy Whitcroft 5321773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5322cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5323cf655043SAndy Whitcroft 5324773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 532513214adfSAndy Whitcroft 532613214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 532713214adfSAndy Whitcroft 5328aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5329cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5330cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5331aad4f614SJoe Perches $allowed[$allow] = 1; 533213214adfSAndy Whitcroft } 533313214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5334cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5335aad4f614SJoe Perches $allowed[$allow] = 1; 533613214adfSAndy Whitcroft } 5337cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5338cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5339aad4f614SJoe Perches $allowed[$allow] = 1; 534013214adfSAndy Whitcroft } 5341aad4f614SJoe Perches $allow++; 534213214adfSAndy Whitcroft } 5343aad4f614SJoe Perches if ($seen) { 5344aad4f614SJoe Perches my $sum_allowed = 0; 5345aad4f614SJoe Perches foreach (@allowed) { 5346aad4f614SJoe Perches $sum_allowed += $_; 5347aad4f614SJoe Perches } 5348aad4f614SJoe Perches if ($sum_allowed == 0) { 5349000d1cc1SJoe Perches WARN("BRACES", 5350000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5351aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5352aad4f614SJoe Perches $seen != $allow) { 5353aad4f614SJoe Perches CHK("BRACES", 5354aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5355aad4f614SJoe Perches } 535613214adfSAndy Whitcroft } 535713214adfSAndy Whitcroft } 535813214adfSAndy Whitcroft } 5359773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 536013214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5361cf655043SAndy Whitcroft my $allowed = 0; 5362f0a594c1SAndy Whitcroft 5363cf655043SAndy Whitcroft # Check the pre-context. 5364cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5365cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5366cf655043SAndy Whitcroft $allowed = 1; 5367f0a594c1SAndy Whitcroft } 5368773647a0SAndy Whitcroft 5369773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5370773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5371773647a0SAndy Whitcroft 5372cf655043SAndy Whitcroft # Check the condition. 5373cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5374773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5375cf655043SAndy Whitcroft if (defined $cond) { 5376773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5377cf655043SAndy Whitcroft } 5378cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5379cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5380cf655043SAndy Whitcroft $allowed = 1; 5381cf655043SAndy Whitcroft } 5382cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5383cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5384cf655043SAndy Whitcroft $allowed = 1; 5385cf655043SAndy Whitcroft } 5386cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5387cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5388cf655043SAndy Whitcroft $allowed = 1; 5389cf655043SAndy Whitcroft } 5390cf655043SAndy Whitcroft # Check the post-context. 5391cf655043SAndy Whitcroft if (defined $chunks[1]) { 5392cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5393cf655043SAndy Whitcroft if (defined $cond) { 5394773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5395cf655043SAndy Whitcroft } 5396cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5397cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5398cf655043SAndy Whitcroft $allowed = 1; 5399cf655043SAndy Whitcroft } 5400cf655043SAndy Whitcroft } 5401cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5402f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5403e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5404cf655043SAndy Whitcroft 5405000d1cc1SJoe Perches WARN("BRACES", 5406000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5407f0a594c1SAndy Whitcroft } 5408f0a594c1SAndy Whitcroft } 5409f0a594c1SAndy Whitcroft 5410e4c5babdSJoe Perches# check for single line unbalanced braces 541195330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 541295330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5413e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5414e4c5babdSJoe Perches } 5415e4c5babdSJoe Perches 54160979ae66SJoe Perches# check for unnecessary blank lines around braces 541777b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5418f8e58219SJoe Perches if (CHK("BRACES", 5419f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5420f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5421f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5422f8e58219SJoe Perches } 54230979ae66SJoe Perches } 542477b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5425f8e58219SJoe Perches if (CHK("BRACES", 5426f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5427f8e58219SJoe Perches $fix) { 5428f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5429f8e58219SJoe Perches } 54300979ae66SJoe Perches } 54310979ae66SJoe Perches 54324a0df2efSAndy Whitcroft# no volatiles please 54336c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 54346c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5435000d1cc1SJoe Perches WARN("VOLATILE", 54368c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 54374a0df2efSAndy Whitcroft } 54384a0df2efSAndy Whitcroft 54395e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 54405e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 54415e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 54425e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 544333acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 54445e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 54455e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 54465e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 54475e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 54485e4f6ba5SJoe Perches $fix && 54495e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 54505e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 54515e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 54525e4f6ba5SJoe Perches my $comma_close = ""; 54535e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 54545e4f6ba5SJoe Perches $comma_close = $1; 54555e4f6ba5SJoe Perches } 54565e4f6ba5SJoe Perches 54575e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 54585e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 54595e4f6ba5SJoe Perches my $fixedline = $prevrawline; 54605e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 54615e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 54625e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 54635e4f6ba5SJoe Perches $fixedline = $rawline; 54645e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 54655e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 54665e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 54675e4f6ba5SJoe Perches } 54685e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 54695e4f6ba5SJoe Perches } 54705e4f6ba5SJoe Perches } 54715e4f6ba5SJoe Perches 54725e4f6ba5SJoe Perches# check for missing a space in a string concatenation 54735e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 54745e4f6ba5SJoe Perches WARN('MISSING_SPACE', 54755e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 54765e4f6ba5SJoe Perches } 54775e4f6ba5SJoe Perches 547877cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5479e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5480e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5481e4b7d309SJoe Perches# function declarations 548277cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 548377cb8546SJoe Perches defined($context_function) && 5484e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5485e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 548677cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5487e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 548877cb8546SJoe Perches } 548977cb8546SJoe Perches 54905e4f6ba5SJoe Perches# check for spaces before a quoted newline 54915e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 54925e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 54935e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 54945e4f6ba5SJoe Perches $fix) { 54955e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 54965e4f6ba5SJoe Perches } 54975e4f6ba5SJoe Perches 54985e4f6ba5SJoe Perches } 54995e4f6ba5SJoe Perches 5500f17dba4fSJoe Perches# concatenated string without spaces between elements 550179682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 550279682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 550379682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 550479682c0cSJoe Perches $fix) { 550579682c0cSJoe Perches while ($line =~ /($String)/g) { 550679682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 550779682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 550879682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 550979682c0cSJoe Perches } 551079682c0cSJoe Perches } 5511f17dba4fSJoe Perches } 5512f17dba4fSJoe Perches 551390ad30e5SJoe Perches# uncoalesced string fragments 551433acb54aSJoe Perches if ($line =~ /$String\s*"/) { 551579682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 551679682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 551779682c0cSJoe Perches $fix) { 551879682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 551979682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 552079682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 552179682c0cSJoe Perches } 552279682c0cSJoe Perches } 552390ad30e5SJoe Perches } 552490ad30e5SJoe Perches 5525522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5526522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5527522b837cSAlexey Dobriyan my $show_Z = 1; 55285e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5529522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 55305e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5531522b837cSAlexey Dobriyan # check for %L 5532522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 55335e4f6ba5SJoe Perches WARN("PRINTF_L", 5534522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5535522b837cSAlexey Dobriyan $show_L = 0; 55365e4f6ba5SJoe Perches } 5537522b837cSAlexey Dobriyan # check for %Z 5538522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5539522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5540522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5541522b837cSAlexey Dobriyan $show_Z = 0; 5542522b837cSAlexey Dobriyan } 5543522b837cSAlexey Dobriyan # check for 0x<decimal> 5544522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5545522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 55466e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 55476e300757SJoe Perches } 55485e4f6ba5SJoe Perches } 55495e4f6ba5SJoe Perches 55505e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 55513f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 55525e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 55535e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 55545e4f6ba5SJoe Perches } 55555e4f6ba5SJoe Perches 555600df344fSAndy Whitcroft# warn about #if 0 5557c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 555860f89010SPrakruthi Deepak Heragu WARN("IF_0", 555960f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 556060f89010SPrakruthi Deepak Heragu } 556160f89010SPrakruthi Deepak Heragu 556260f89010SPrakruthi Deepak Heragu# warn about #if 1 556360f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 556460f89010SPrakruthi Deepak Heragu WARN("IF_1", 556560f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 55664a0df2efSAndy Whitcroft } 55674a0df2efSAndy Whitcroft 556803df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 556903df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5570100425deSJoe Perches my $tested = quotemeta($1); 5571100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5572100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5573100425deSJoe Perches my $func = $1; 5574100425deSJoe Perches if (WARN('NEEDLESS_IF', 5575100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5576100425deSJoe Perches $fix) { 5577100425deSJoe Perches my $do_fix = 1; 5578100425deSJoe Perches my $leading_tabs = ""; 5579100425deSJoe Perches my $new_leading_tabs = ""; 5580100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5581100425deSJoe Perches $leading_tabs = $1; 5582100425deSJoe Perches } else { 5583100425deSJoe Perches $do_fix = 0; 5584100425deSJoe Perches } 5585100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5586100425deSJoe Perches $new_leading_tabs = $1; 5587100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5588100425deSJoe Perches $do_fix = 0; 5589100425deSJoe Perches } 5590100425deSJoe Perches } else { 5591100425deSJoe Perches $do_fix = 0; 5592100425deSJoe Perches } 5593100425deSJoe Perches if ($do_fix) { 5594100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5595100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5596100425deSJoe Perches } 5597100425deSJoe Perches } 55984c432a8fSGreg Kroah-Hartman } 55994c432a8fSGreg Kroah-Hartman } 5600f0a594c1SAndy Whitcroft 5601ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5602ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5603ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5604ebfdc409SJoe Perches (defined $1 || defined $3) && 5605ebfdc409SJoe Perches $linenr > 3) { 5606ebfdc409SJoe Perches my $testval = $2; 5607ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5608ebfdc409SJoe Perches 5609ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5610ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5611ebfdc409SJoe Perches 5612e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 5613e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 5614ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5615ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5616ebfdc409SJoe Perches } 5617ebfdc409SJoe Perches } 5618ebfdc409SJoe Perches 5619f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5620dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5621f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5622f78d98f6SJoe Perches my $level = $1; 5623f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5624f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5625f78d98f6SJoe Perches $fix) { 5626f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5627f78d98f6SJoe Perches } 5628f78d98f6SJoe Perches } 5629f78d98f6SJoe Perches 563045c55e92SJoe Perches# check for logging continuations 563145c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 563245c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 563345c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 563445c55e92SJoe Perches } 563545c55e92SJoe Perches 5636abb08a53SJoe Perches# check for mask then right shift without a parentheses 56375b57980dSJoe Perches if ($perl_version_ok && 5638abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5639abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5640abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5641abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5642abb08a53SJoe Perches } 5643abb08a53SJoe Perches 5644b75ac618SJoe Perches# check for pointer comparisons to NULL 56455b57980dSJoe Perches if ($perl_version_ok) { 5646b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5647b75ac618SJoe Perches my $val = $1; 5648b75ac618SJoe Perches my $equal = "!"; 5649b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5650b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5651b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5652b75ac618SJoe Perches $fix) { 5653b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5654b75ac618SJoe Perches } 5655b75ac618SJoe Perches } 5656b75ac618SJoe Perches } 5657b75ac618SJoe Perches 56588716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 56598716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 56608716de38SJoe Perches my $attr = $1; 56618716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 56628716de38SJoe Perches my $ptr = $1; 56638716de38SJoe Perches my $var = $2; 56648716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 56658716de38SJoe Perches ERROR("MISPLACED_INIT", 56668716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 56678716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 56688716de38SJoe Perches WARN("MISPLACED_INIT", 56698716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 56708716de38SJoe Perches $fix) { 5671194f66fcSJoe 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; 56728716de38SJoe Perches } 56738716de38SJoe Perches } 56748716de38SJoe Perches } 56758716de38SJoe Perches 5676e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5677e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5678e970b884SJoe Perches my $attr = $1; 5679e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5680e970b884SJoe Perches my $attr_prefix = $1; 5681e970b884SJoe Perches my $attr_type = $2; 5682e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5683e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5684e970b884SJoe Perches $fix) { 5685194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5686e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5687e970b884SJoe Perches } 5688e970b884SJoe Perches } 5689e970b884SJoe Perches 5690e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5691e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5692e970b884SJoe Perches my $attr = $1; 5693e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5694e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5695e970b884SJoe Perches $fix) { 5696194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5697e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5698e970b884SJoe Perches $lead = rtrim($1); 5699e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5700e970b884SJoe Perches $lead = "${lead}const "; 5701194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5702e970b884SJoe Perches } 5703e970b884SJoe Perches } 5704e970b884SJoe Perches 5705c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5706c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5707c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5708c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5709c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5710c17893c7SJoe Perches $fix) { 5711c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5712c17893c7SJoe Perches } 5713c17893c7SJoe Perches } 5714c17893c7SJoe Perches 5715fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5716fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5717fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5718fbdb8138SJoe Perches my $constant_func = $1; 5719fbdb8138SJoe Perches my $func = $constant_func; 5720fbdb8138SJoe Perches $func =~ s/^__constant_//; 5721fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5722fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5723fbdb8138SJoe Perches $fix) { 5724194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5725fbdb8138SJoe Perches } 5726fbdb8138SJoe Perches } 5727fbdb8138SJoe Perches 57281a15a250SPatrick Pannuto# prefer usleep_range over udelay 572937581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 573043c1d77cSJoe Perches my $delay = $1; 57311a15a250SPatrick Pannuto # ignore udelay's < 10, however 573243c1d77cSJoe Perches if (! ($delay < 10) ) { 5733000d1cc1SJoe Perches CHK("USLEEP_RANGE", 5734458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 573543c1d77cSJoe Perches } 573643c1d77cSJoe Perches if ($delay > 2000) { 573743c1d77cSJoe Perches WARN("LONG_UDELAY", 573843c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 57391a15a250SPatrick Pannuto } 57401a15a250SPatrick Pannuto } 57411a15a250SPatrick Pannuto 574209ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 574309ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 574409ef8725SPatrick Pannuto if ($1 < 20) { 5745000d1cc1SJoe Perches WARN("MSLEEP", 5746458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 574709ef8725SPatrick Pannuto } 574809ef8725SPatrick Pannuto } 574909ef8725SPatrick Pannuto 575036ec1939SJoe Perches# check for comparisons of jiffies 575136ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 575236ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 575336ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 575436ec1939SJoe Perches } 575536ec1939SJoe Perches 57569d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 57579d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 57589d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 57599d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 57609d7a34a5SJoe Perches } 57619d7a34a5SJoe Perches 576200df344fSAndy Whitcroft# warn about #ifdefs in C files 5763c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 576400df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 576500df344fSAndy Whitcroft# print "$herecurr"; 576600df344fSAndy Whitcroft# $clean = 0; 576700df344fSAndy Whitcroft# } 576800df344fSAndy Whitcroft 576922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5770c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 57713705ce5bSJoe Perches if (ERROR("SPACING", 57723705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 57733705ce5bSJoe Perches $fix) { 5774194f66fcSJoe Perches $fixed[$fixlinenr] =~ 57753705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 57763705ce5bSJoe Perches } 57773705ce5bSJoe Perches 577822f2a2efSAndy Whitcroft } 577922f2a2efSAndy Whitcroft 57804a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5781171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5782171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 57834a0df2efSAndy Whitcroft my $which = $1; 57844a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5785000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5786000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 57874a0df2efSAndy Whitcroft } 57884a0df2efSAndy Whitcroft } 57894a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5790402c2553SMichael S. Tsirkin 5791402c2553SMichael S. Tsirkin my $barriers = qr{ 5792402c2553SMichael S. Tsirkin mb| 5793402c2553SMichael S. Tsirkin rmb| 5794402c2553SMichael S. Tsirkin wmb| 5795402c2553SMichael S. Tsirkin read_barrier_depends 5796402c2553SMichael S. Tsirkin }x; 5797402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5798402c2553SMichael S. Tsirkin mb__before_atomic| 5799402c2553SMichael S. Tsirkin mb__after_atomic| 5800402c2553SMichael S. Tsirkin store_release| 5801402c2553SMichael S. Tsirkin load_acquire| 5802402c2553SMichael S. Tsirkin store_mb| 5803402c2553SMichael S. Tsirkin (?:$barriers) 5804402c2553SMichael S. Tsirkin }x; 5805402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5806402c2553SMichael S. Tsirkin (?:$barriers)| 580743e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 580843e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5809402c2553SMichael S. Tsirkin }x; 5810402c2553SMichael S. Tsirkin 5811402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 58124a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5813c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5814000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 58154a0df2efSAndy Whitcroft } 58164a0df2efSAndy Whitcroft } 58173ad81779SPaul E. McKenney 5818f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5819f4073b0fSMichael S. Tsirkin 5820f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5821f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5822f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5823f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5824f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5825f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5826f4073b0fSMichael S. Tsirkin } 5827f4073b0fSMichael S. Tsirkin 5828cb426e99SJoe Perches# check for waitqueue_active without a comment. 5829cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5830cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5831cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5832cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5833cb426e99SJoe Perches } 5834cb426e99SJoe Perches } 58353ad81779SPaul E. McKenney 5836*5099a722SMarco Elver# check for data_race without a comment. 5837*5099a722SMarco Elver if ($line =~ /\bdata_race\s*\(/) { 5838*5099a722SMarco Elver if (!ctx_has_comment($first_line, $linenr)) { 5839*5099a722SMarco Elver WARN("DATA_RACE", 5840*5099a722SMarco Elver "data_race without comment\n" . $herecurr); 5841*5099a722SMarco Elver } 5842*5099a722SMarco Elver } 5843*5099a722SMarco Elver 584491db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends 584591db2592SPaul E. McKenney if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { 584691db2592SPaul E. McKenney WARN("READ_BARRIER_DEPENDS", 584791db2592SPaul E. McKenney "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); 584891db2592SPaul E. McKenney } 584991db2592SPaul E. McKenney 58504a0df2efSAndy Whitcroft# check of hardware specific defines 5851c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5852000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5853000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 58540a920b5bSAndy Whitcroft } 5855653d4876SAndy Whitcroft 5856596ed45bSJoe Perches# check that the storage class is not after a type 5857596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5858000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5859596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5860596ed45bSJoe Perches } 5861596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5862596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5863596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5864596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5865596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5866596ed45bSJoe Perches WARN("STORAGE_CLASS", 5867596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5868d4977c78STobias Klauser } 5869d4977c78STobias Klauser 5870de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5871de7d4f0eSAndy Whitcroft# storage class and type. 58729c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 58739c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5874000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5875000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5876de7d4f0eSAndy Whitcroft } 5877de7d4f0eSAndy Whitcroft 58788905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 58792b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 58802b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5881d5e616fcSJoe Perches if (WARN("INLINE", 5882d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5883d5e616fcSJoe Perches $fix) { 5884194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5885d5e616fcSJoe Perches 5886d5e616fcSJoe Perches } 58878905a67cSAndy Whitcroft } 58888905a67cSAndy Whitcroft 58893d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 58902b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 58912b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5892000d1cc1SJoe Perches WARN("PREFER_PACKED", 5893000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 58943d130fd0SJoe Perches } 58953d130fd0SJoe Perches 589639b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 58972b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 58982b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5899000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5900000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 590139b7e287SJoe Perches } 590239b7e287SJoe Perches 5903462811d9SJoe Perches# Check for __attribute__ section, prefer __section 5904462811d9SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5905462811d9SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { 5906462811d9SJoe Perches my $old = substr($rawline, $-[1], $+[1] - $-[1]); 5907462811d9SJoe Perches my $new = substr($old, 1, -1); 5908462811d9SJoe Perches if (WARN("PREFER_SECTION", 5909462811d9SJoe Perches "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && 5910462811d9SJoe Perches $fix) { 5911462811d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; 5912462811d9SJoe Perches } 5913462811d9SJoe Perches } 5914462811d9SJoe Perches 59155f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 59162b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59172b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5918d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5919d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5920d5e616fcSJoe Perches $fix) { 5921194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5922d5e616fcSJoe Perches 5923d5e616fcSJoe Perches } 59245f14d3bdSJoe Perches } 59255f14d3bdSJoe Perches 59266061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 59272b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59282b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5929d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5930d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5931d5e616fcSJoe Perches $fix) { 5932194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5933d5e616fcSJoe Perches } 59346061d949SJoe Perches } 59356061d949SJoe Perches 5936619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 59375b57980dSJoe Perches if ($perl_version_ok && 5938619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5939619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5940619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5941619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5942619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5943619a908aSJoe Perches } 5944619a908aSJoe Perches 5945fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 5946e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5947fd39f904STomas Winkler $realfile !~ m@\btools/@ && 5948e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5949e6176fa4SJoe Perches my $type = $1; 5950e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5951e6176fa4SJoe Perches $type = $1; 5952e6176fa4SJoe Perches my $kernel_type = 'u'; 5953e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5954e6176fa4SJoe Perches $type =~ /(\d+)/; 5955e6176fa4SJoe Perches $kernel_type .= $1; 5956e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5957e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5958e6176fa4SJoe Perches $fix) { 5959e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5960e6176fa4SJoe Perches } 5961e6176fa4SJoe Perches } 5962e6176fa4SJoe Perches } 5963e6176fa4SJoe Perches 5964938224b5SJoe Perches# check for cast of C90 native int or longer types constants 5965938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 5966938224b5SJoe Perches my $cast = $1; 5967938224b5SJoe Perches my $const = $2; 5968938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 5969938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 5970938224b5SJoe Perches $fix) { 5971938224b5SJoe Perches my $suffix = ""; 5972938224b5SJoe Perches my $newconst = $const; 5973938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 5974938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 5975938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 5976938224b5SJoe Perches $suffix .= 'LL'; 5977938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 5978938224b5SJoe Perches $suffix .= 'L'; 5979938224b5SJoe Perches } 5980938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 5981938224b5SJoe Perches } 5982938224b5SJoe Perches } 5983938224b5SJoe Perches 59848f53a9b8SJoe Perches# check for sizeof(&) 59858f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5986000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5987000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 59888f53a9b8SJoe Perches } 59898f53a9b8SJoe Perches 599066c80b60SJoe Perches# check for sizeof without parenthesis 599166c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 5992d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 5993d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 5994d5e616fcSJoe Perches $fix) { 5995194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 5996d5e616fcSJoe Perches } 599766c80b60SJoe Perches } 599866c80b60SJoe Perches 599988982feaSJoe Perches# check for struct spinlock declarations 600088982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 600188982feaSJoe Perches WARN("USE_SPINLOCK_T", 600288982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 600388982feaSJoe Perches } 600488982feaSJoe Perches 6005a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 600606668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6007a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6008caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6009caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6010d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6011d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6012d5e616fcSJoe Perches $fix) { 6013194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6014d5e616fcSJoe Perches } 6015a6962d72SJoe Perches } 6016a6962d72SJoe Perches } 6017a6962d72SJoe Perches 60180b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 60195b57980dSJoe Perches if ($perl_version_ok && 60200b523769SJoe Perches defined $stat && 60210b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 60220b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6023e3c6bc95STobin C. Harding my $stat_real; 6024e3c6bc95STobin C. Harding 60250b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 60260b523769SJoe Perches $lc = $lc + $linenr; 60270b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6028ffe07513SJoe Perches my $specifier; 6029ffe07513SJoe Perches my $extension; 60303bd32d6aSSakari Ailus my $qualifier; 6031ffe07513SJoe Perches my $bad_specifier = ""; 60320b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 60330b523769SJoe Perches $fmt =~ s/%%//g; 6034e3c6bc95STobin C. Harding 60353bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6036e3c6bc95STobin C. Harding $specifier = $1; 6037e3c6bc95STobin C. Harding $extension = $2; 60383bd32d6aSSakari Ailus $qualifier = $3; 6039361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 60403bd32d6aSSakari Ailus ($extension eq "f" && 60413bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6042e3c6bc95STobin C. Harding $bad_specifier = $specifier; 60430b523769SJoe Perches last; 60440b523769SJoe Perches } 6045e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6046e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6047e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 60480b523769SJoe Perches } 6049e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6050e3c6bc95STobin 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"); 6051e3c6bc95STobin C. Harding } 6052e3c6bc95STobin C. Harding } 6053e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 60542a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 60551df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 60561df7338aSSergey Senozhatsky my $use = ""; 6057e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 60581df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6059e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 60601df7338aSSergey Senozhatsky } 60612a9f9d85STobin C. Harding 60620b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6063e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6064e3c6bc95STobin C. Harding } 60650b523769SJoe Perches } 60660b523769SJoe Perches } 60670b523769SJoe Perches 6068554e165cSAndy Whitcroft# Check for misused memsets 60695b57980dSJoe Perches if ($perl_version_ok && 6070d1fe9c09SJoe Perches defined $stat && 60719e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6072554e165cSAndy Whitcroft 6073d7c76ba7SJoe Perches my $ms_addr = $2; 6074d1fe9c09SJoe Perches my $ms_val = $7; 6075d1fe9c09SJoe Perches my $ms_size = $12; 6076d7c76ba7SJoe Perches 6077554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6078554e165cSAndy Whitcroft ERROR("MEMSET", 6079d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6080554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6081554e165cSAndy Whitcroft WARN("MEMSET", 6082d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6083d7c76ba7SJoe Perches } 6084d7c76ba7SJoe Perches } 6085d7c76ba7SJoe Perches 608698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 60875b57980dSJoe Perches# if ($perl_version_ok && 6088f333195dSJoe Perches# defined $stat && 6089f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6090f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6091f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6092f333195dSJoe Perches# $fix) { 6093f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6094f333195dSJoe Perches# } 6095f333195dSJoe Perches# } 609698a9bba5SJoe Perches 6097b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 60985b57980dSJoe Perches# if ($perl_version_ok && 6099f333195dSJoe Perches# defined $stat && 6100f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6101f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6102f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6103f333195dSJoe Perches# } 6104b6117d17SMateusz Kulikowski 61058617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 61068617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 61075b57980dSJoe Perches# if ($perl_version_ok && 6108f333195dSJoe Perches# defined $stat && 6109f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6110f333195dSJoe Perches# 6111f333195dSJoe Perches# my $ms_val = $7; 6112f333195dSJoe Perches# 6113f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6114f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6115f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6116f333195dSJoe Perches# $fix) { 6117f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6118f333195dSJoe Perches# } 6119f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6120f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6121f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6122f333195dSJoe Perches# $fix) { 6123f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6124f333195dSJoe Perches# } 6125f333195dSJoe Perches# } 6126f333195dSJoe Perches# } 61278617cd09SMateusz Kulikowski 6128d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 61295b57980dSJoe Perches if ($perl_version_ok && 6130d1fe9c09SJoe Perches defined $stat && 6131d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6132d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6133d7c76ba7SJoe Perches my $call = $1; 6134d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6135d7c76ba7SJoe Perches my $arg1 = $3; 6136d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6137d1fe9c09SJoe Perches my $arg2 = $8; 6138d7c76ba7SJoe Perches my $cast; 6139d7c76ba7SJoe Perches 6140d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6141d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6142d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6143d7c76ba7SJoe Perches $cast = $cast1; 6144d7c76ba7SJoe Perches } else { 6145d7c76ba7SJoe Perches $cast = $cast2; 6146d7c76ba7SJoe Perches } 6147d7c76ba7SJoe Perches WARN("MINMAX", 6148d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6149554e165cSAndy Whitcroft } 6150554e165cSAndy Whitcroft } 6151554e165cSAndy Whitcroft 61524a273195SJoe Perches# check usleep_range arguments 61535b57980dSJoe Perches if ($perl_version_ok && 61544a273195SJoe Perches defined $stat && 61554a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 61564a273195SJoe Perches my $min = $1; 61574a273195SJoe Perches my $max = $7; 61584a273195SJoe Perches if ($min eq $max) { 61594a273195SJoe Perches WARN("USLEEP_RANGE", 6160458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 61614a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 61624a273195SJoe Perches $min > $max) { 61634a273195SJoe Perches WARN("USLEEP_RANGE", 6164458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 61654a273195SJoe Perches } 61664a273195SJoe Perches } 61674a273195SJoe Perches 6168823b794cSJoe Perches# check for naked sscanf 61695b57980dSJoe Perches if ($perl_version_ok && 6170823b794cSJoe Perches defined $stat && 61716c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6172823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6173823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6174823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6175823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6176823b794cSJoe Perches $lc = $lc + $linenr; 61772a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6178823b794cSJoe Perches WARN("NAKED_SSCANF", 6179823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6180823b794cSJoe Perches } 6181823b794cSJoe Perches 6182afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 61835b57980dSJoe Perches if ($perl_version_ok && 6184afc819abSJoe Perches defined $stat && 6185afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6186afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6187afc819abSJoe Perches $lc = $lc + $linenr; 61882a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6189afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6190afc819abSJoe Perches my $format = $6; 6191afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6192afc819abSJoe Perches if ($count == 1 && 6193afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6194afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6195afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6196afc819abSJoe Perches } 6197afc819abSJoe Perches } 6198afc819abSJoe Perches } 6199afc819abSJoe Perches 620070dc8a48SJoe Perches# check for new externs in .h files. 620170dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 620270dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6203d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 620470dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 620570dc8a48SJoe Perches $fix) { 6206194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 620770dc8a48SJoe Perches } 620870dc8a48SJoe Perches } 620970dc8a48SJoe Perches 6210de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6211171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6212c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6213171ae1a4SAndy Whitcroft { 6214c45dcabdSAndy Whitcroft my $function_name = $1; 6215c45dcabdSAndy Whitcroft my $paren_space = $2; 6216171ae1a4SAndy Whitcroft 6217171ae1a4SAndy Whitcroft my $s = $stat; 6218171ae1a4SAndy Whitcroft if (defined $cond) { 6219171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6220171ae1a4SAndy Whitcroft } 6221c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 6222c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 6223c45dcabdSAndy Whitcroft { 6224000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6225000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6226de7d4f0eSAndy Whitcroft } 6227de7d4f0eSAndy Whitcroft 6228171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6229000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6230000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6231171ae1a4SAndy Whitcroft } 62329c9ba34eSAndy Whitcroft 62339c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 62349c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 62359c9ba34eSAndy Whitcroft { 6236000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6237000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6238171ae1a4SAndy Whitcroft } 6239171ae1a4SAndy Whitcroft 6240a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6241a0ad7596SJoe Perches if (defined $stat && 624225bdda2bSMiles Chen $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6243ca0d8929SJoe Perches $1 ne "void") { 6244ca0d8929SJoe Perches my $args = trim($1); 6245ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6246ca0d8929SJoe Perches my $arg = trim($1); 6247ca0d8929SJoe Perches if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6248ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6249ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6250ca0d8929SJoe Perches } 6251ca0d8929SJoe Perches } 6252ca0d8929SJoe Perches } 6253ca0d8929SJoe Perches 6254a0ad7596SJoe Perches# check for function definitions 62555b57980dSJoe Perches if ($perl_version_ok && 6256a0ad7596SJoe Perches defined $stat && 6257a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6258a0ad7596SJoe Perches $context_function = $1; 6259a0ad7596SJoe Perches 6260a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6261a0ad7596SJoe Perches my $ok = 0; 6262a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6263a0ad7596SJoe Perches my $herectx = $here . "\n"; 6264a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6265a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6266a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6267a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6268a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6269a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6270a0ad7596SJoe Perches } 6271a0ad7596SJoe Perches if (!$ok) { 6272a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6273a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6274a0ad7596SJoe Perches } 6275a0ad7596SJoe Perches } 6276a0ad7596SJoe Perches 6277de7d4f0eSAndy Whitcroft# checks for new __setup's 6278de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6279de7d4f0eSAndy Whitcroft my $name = $1; 6280de7d4f0eSAndy Whitcroft 6281de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6282000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 62838c27ceffSMauro Carvalho Chehab "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); 6284de7d4f0eSAndy Whitcroft } 6285653d4876SAndy Whitcroft } 62869c0ca6f9SAndy Whitcroft 6287e29a70f1SJoe Perches# check for pointless casting of alloc functions 6288e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6289000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6290000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 62919c0ca6f9SAndy Whitcroft } 629213214adfSAndy Whitcroft 6293a640d25cSJoe Perches# alloc style 6294a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 62955b57980dSJoe Perches if ($perl_version_ok && 6296e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6297a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6298a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6299a640d25cSJoe Perches } 6300a640d25cSJoe Perches 630160a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 63025b57980dSJoe Perches if ($perl_version_ok && 63031b4a2ed4SJoe Perches defined $stat && 63041b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 630560a55369SJoe Perches my $oldfunc = $3; 630660a55369SJoe Perches my $a1 = $4; 630760a55369SJoe Perches my $a2 = $10; 630860a55369SJoe Perches my $newfunc = "kmalloc_array"; 630960a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 631060a55369SJoe Perches my $r1 = $a1; 631160a55369SJoe Perches my $r2 = $a2; 631260a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 631360a55369SJoe Perches $r1 = $a2; 631460a55369SJoe Perches $r2 = $a1; 631560a55369SJoe Perches } 6316e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6317e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 63181b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6319e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6320e3d95a2aSTobin C. Harding 6321e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 63221b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 63231b4a2ed4SJoe Perches $cnt == 1 && 6324e367455aSJoe Perches $fix) { 6325194f66fcSJoe 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; 632660a55369SJoe Perches } 632760a55369SJoe Perches } 632860a55369SJoe Perches } 632960a55369SJoe Perches 6330972fdea2SJoe Perches# check for krealloc arg reuse 63315b57980dSJoe Perches if ($perl_version_ok && 63324cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 63334cab63ceSJoe Perches $1 eq $3) { 6334972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6335972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6336972fdea2SJoe Perches } 6337972fdea2SJoe Perches 63385ce59ae0SJoe Perches# check for alloc argument mismatch 63395ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 63405ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 63415ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 63425ce59ae0SJoe Perches } 63435ce59ae0SJoe Perches 6344caf2a54fSJoe Perches# check for multiple semicolons 6345caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6346d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6347d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6348d5e616fcSJoe Perches $fix) { 6349194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6350d5e616fcSJoe Perches } 6351d1e2ad07SJoe Perches } 6352d1e2ad07SJoe Perches 6353cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6354cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6355cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 63560ab90191SJoe Perches my $ull = ""; 63570ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 63580ab90191SJoe Perches if (CHK("BIT_MACRO", 63590ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 63600ab90191SJoe Perches $fix) { 63610ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 63620ab90191SJoe Perches } 63630ab90191SJoe Perches } 63640ab90191SJoe Perches 63652d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 63662d632745SJoe 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*$/) { 63672d632745SJoe Perches my $config = $1; 63682d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 63692d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 63702d632745SJoe Perches $fix) { 63712d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 63722d632745SJoe Perches } 63732d632745SJoe Perches } 63742d632745SJoe Perches 6375e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6376c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6377c34c09a8SJoe Perches my $has_break = 0; 6378c34c09a8SJoe Perches my $has_statement = 0; 6379c34c09a8SJoe Perches my $count = 0; 6380c34c09a8SJoe Perches my $prevline = $linenr; 6381e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6382c34c09a8SJoe Perches $prevline--; 6383c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6384c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6385c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6386c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6387c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6388c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6389c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6390c34c09a8SJoe Perches $has_statement = 1; 6391c34c09a8SJoe Perches $count++; 6392258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6393c34c09a8SJoe Perches } 6394c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6395c34c09a8SJoe Perches WARN("MISSING_BREAK", 6396224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6397c34c09a8SJoe Perches } 6398c34c09a8SJoe Perches } 6399c34c09a8SJoe Perches 6400d1e2ad07SJoe Perches# check for switch/default statements without a break; 64015b57980dSJoe Perches if ($perl_version_ok && 6402d1e2ad07SJoe Perches defined $stat && 6403d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6404d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6405e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6406e3d95a2aSTobin C. Harding 6407d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6408d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6409caf2a54fSJoe Perches } 6410caf2a54fSJoe Perches 641113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6412d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6413d5e616fcSJoe Perches if (WARN("USE_FUNC", 6414d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6415d5e616fcSJoe Perches $fix) { 6416194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6417d5e616fcSJoe Perches } 641813214adfSAndy Whitcroft } 6419773647a0SAndy Whitcroft 642062ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 642162ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 642262ec818fSJoe Perches ERROR("DATE_TIME", 642362ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 642462ec818fSJoe Perches } 642562ec818fSJoe Perches 64262c92488aSJoe Perches# check for use of yield() 64272c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 64282c92488aSJoe Perches WARN("YIELD", 64292c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 64302c92488aSJoe Perches } 64312c92488aSJoe Perches 6432179f8f40SJoe Perches# check for comparisons against true and false 6433179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6434179f8f40SJoe Perches my $lead = $1; 6435179f8f40SJoe Perches my $arg = $2; 6436179f8f40SJoe Perches my $test = $3; 6437179f8f40SJoe Perches my $otype = $4; 6438179f8f40SJoe Perches my $trail = $5; 6439179f8f40SJoe Perches my $op = "!"; 6440179f8f40SJoe Perches 6441179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6442179f8f40SJoe Perches 6443179f8f40SJoe Perches my $type = lc($otype); 6444179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6445179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6446179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6447179f8f40SJoe Perches $op = ""; 6448179f8f40SJoe Perches } 6449179f8f40SJoe Perches 6450179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6451179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6452179f8f40SJoe Perches 6453179f8f40SJoe Perches## maybe suggesting a correct construct would better 6454179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6455179f8f40SJoe Perches 6456179f8f40SJoe Perches } 6457179f8f40SJoe Perches } 6458179f8f40SJoe Perches 64594882720bSThomas Gleixner# check for semaphores initialized locked 64604882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6461000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6462000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6463773647a0SAndy Whitcroft } 64646712d858SJoe Perches 646567d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 646667d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6467000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 646867d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6469773647a0SAndy Whitcroft } 64706712d858SJoe Perches 6471ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6472f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6473000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6474ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6475f3db6639SMichael Ellerman } 64766712d858SJoe Perches 64773d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 64783d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 64793d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 64803d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 64813d709ab5SPaul E. McKenney } 64823d709ab5SPaul E. McKenney 64839189c7e7SJoe Perches# check for deprecated apis 64849189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 64859189c7e7SJoe Perches my $deprecated_api = $1; 64869189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 64879189c7e7SJoe Perches WARN("DEPRECATED_API", 64889189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 64899189c7e7SJoe Perches } 64909189c7e7SJoe Perches 64910f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6492d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 64936903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6494d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6495000d1cc1SJoe Perches WARN("CONST_STRUCT", 6496d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 64972b6db5cbSAndy Whitcroft } 6498773647a0SAndy Whitcroft 6499773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6500773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6501773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6502c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6503c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6504171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6505171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6506171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6507773647a0SAndy Whitcroft { 6508000d1cc1SJoe Perches WARN("NR_CPUS", 6509000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6510773647a0SAndy Whitcroft } 65119c9ba34eSAndy Whitcroft 651252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 651352ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 651452ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 651552ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 651652ea8506SJoe Perches } 651752ea8506SJoe Perches 6518acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 65195b57980dSJoe Perches if ($perl_version_ok && 6520acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6521acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6522acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6523acd9362cSJoe Perches } 6524acd9362cSJoe Perches 6525de3f186fSDenis Efremov# nested likely/unlikely calls 6526de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 6527de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 6528de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 6529de3f186fSDenis Efremov } 6530de3f186fSDenis Efremov 6531691d77b6SAndy Whitcroft# whine mightly about in_atomic 6532691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6533691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6534000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6535000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6536f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6537000d1cc1SJoe Perches WARN("IN_ATOMIC", 6538000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6539691d77b6SAndy Whitcroft } 6540691d77b6SAndy Whitcroft } 65411704f47bSPeter Zijlstra 65420f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 65430f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 65440f5225b0SPeter Zijlstra ERROR("LOCKING", 65450f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 65460f5225b0SPeter Zijlstra } 65470f5225b0SPeter Zijlstra 65481704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 65491704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 65501704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 65511704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 65521704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 65531704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6554000d1cc1SJoe Perches ERROR("LOCKDEP", 6555000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 65561704f47bSPeter Zijlstra } 65571704f47bSPeter Zijlstra } 655888f8831cSDave Jones 6559b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6560b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6561000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6562000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 656388f8831cSDave Jones } 65642435880fSJoe Perches 656500180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 656600180468SJoe Perches# and whether or not function naming is typical and if 656700180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 65685b57980dSJoe Perches if ($perl_version_ok && 656900180468SJoe Perches defined $stat && 657000180468SJoe 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*\)/) { 657100180468SJoe Perches my $var = $1; 657200180468SJoe Perches my $perms = $2; 657300180468SJoe Perches my $show = $3; 657400180468SJoe Perches my $store = $4; 657500180468SJoe Perches my $octal_perms = perms_to_octal($perms); 657600180468SJoe Perches if ($show =~ /^${var}_show$/ && 657700180468SJoe Perches $store =~ /^${var}_store$/ && 657800180468SJoe Perches $octal_perms eq "0644") { 657900180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 658000180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 658100180468SJoe Perches $fix) { 658200180468SJoe 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})/; 658300180468SJoe Perches } 658400180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 658500180468SJoe Perches $store =~ /^NULL$/ && 658600180468SJoe Perches $octal_perms eq "0444") { 658700180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 658800180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 658900180468SJoe Perches $fix) { 659000180468SJoe 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})/; 659100180468SJoe Perches } 659200180468SJoe Perches } elsif ($show =~ /^NULL$/ && 659300180468SJoe Perches $store =~ /^${var}_store$/ && 659400180468SJoe Perches $octal_perms eq "0200") { 659500180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 659600180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 659700180468SJoe Perches $fix) { 659800180468SJoe 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})/; 659900180468SJoe Perches } 660000180468SJoe Perches } elsif ($octal_perms eq "0644" || 660100180468SJoe Perches $octal_perms eq "0444" || 660200180468SJoe Perches $octal_perms eq "0200") { 660300180468SJoe Perches my $newshow = "$show"; 660400180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 660500180468SJoe Perches my $newstore = $store; 660600180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 660700180468SJoe Perches my $rename = ""; 660800180468SJoe Perches if ($show ne $newshow) { 660900180468SJoe Perches $rename .= " '$show' to '$newshow'"; 661000180468SJoe Perches } 661100180468SJoe Perches if ($store ne $newstore) { 661200180468SJoe Perches $rename .= " '$store' to '$newstore'"; 661300180468SJoe Perches } 661400180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 661500180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 661600180468SJoe Perches } else { 661700180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 661800180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 661900180468SJoe Perches } 662000180468SJoe Perches } 662100180468SJoe Perches 6622515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6623515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 662473121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 662573121534SJoe Perches# specific definition of not visible in sysfs. 662673121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 662773121534SJoe Perches# use the default permissions 66285b57980dSJoe Perches if ($perl_version_ok && 6629459cf0aeSJoe Perches defined $stat && 6630515a235eSJoe Perches $line =~ /$mode_perms_search/) { 66312435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 66322435880fSJoe Perches my $func = $entry->[0]; 66332435880fSJoe Perches my $arg_pos = $entry->[1]; 66342435880fSJoe Perches 6635459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6636459cf0aeSJoe Perches $lc = $lc + $linenr; 66372a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6638459cf0aeSJoe Perches 66392435880fSJoe Perches my $skip_args = ""; 66402435880fSJoe Perches if ($arg_pos > 1) { 66412435880fSJoe Perches $arg_pos--; 66422435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 66432435880fSJoe Perches } 6644f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6645459cf0aeSJoe Perches if ($stat =~ /$test/) { 66462435880fSJoe Perches my $val = $1; 66472435880fSJoe Perches $val = $6 if ($skip_args ne ""); 664873121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 664973121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 665073121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 66512435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6652459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6653f90774e1SJoe Perches } 6654f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6655c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6656459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 66572435880fSJoe Perches } 6658459cf0aeSJoe Perches } 6659459cf0aeSJoe Perches } 6660459cf0aeSJoe Perches } 6661459cf0aeSJoe Perches 6662459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6663bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 666400180468SJoe Perches my $oval = $1; 666500180468SJoe Perches my $octal = perms_to_octal($oval); 6666f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6667459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6668f90774e1SJoe Perches $fix) { 666900180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 66702435880fSJoe Perches } 667113214adfSAndy Whitcroft } 66725a6d20ceSBjorn Andersson 66735a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 66745a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 66755a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 66765a6d20ceSBjorn Andersson my $valid_licenses = qr{ 66775a6d20ceSBjorn Andersson GPL| 66785a6d20ceSBjorn Andersson GPL\ v2| 66795a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 66805a6d20ceSBjorn Andersson Dual\ BSD/GPL| 66815a6d20ceSBjorn Andersson Dual\ MIT/GPL| 66825a6d20ceSBjorn Andersson Dual\ MPL/GPL| 66835a6d20ceSBjorn Andersson Proprietary 66845a6d20ceSBjorn Andersson }x; 66855a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 66865a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 66875a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 66885a6d20ceSBjorn Andersson } 66895a6d20ceSBjorn Andersson } 66906a8d76cbSMatteo Croce 66916a8d76cbSMatteo Croce# check for sysctl duplicate constants 66926a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 66936a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 66946a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 66956a8d76cbSMatteo Croce } 6696515a235eSJoe Perches } 669713214adfSAndy Whitcroft 669813214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 669913214adfSAndy Whitcroft # so just keep quiet. 670013214adfSAndy Whitcroft if ($#rawlines == -1) { 670113214adfSAndy Whitcroft exit(0); 67020a920b5bSAndy Whitcroft } 67030a920b5bSAndy Whitcroft 67048905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 67058905a67cSAndy Whitcroft # things that appear to be patches. 67068905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 67078905a67cSAndy Whitcroft exit(0); 67088905a67cSAndy Whitcroft } 67098905a67cSAndy Whitcroft 67108905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 67118905a67cSAndy Whitcroft # just keep quiet. 67128905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 67138905a67cSAndy Whitcroft exit(0); 67148905a67cSAndy Whitcroft } 67158905a67cSAndy Whitcroft 6716a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6717000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6718000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 67190a920b5bSAndy Whitcroft } 6720cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6721cd261496SGeert Uytterhoeven if ($signoff == 0) { 6722000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6723000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 6724cd261496SGeert Uytterhoeven } elsif (!$authorsignoff) { 6725cd261496SGeert Uytterhoeven WARN("NO_AUTHOR_SIGN_OFF", 6726cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 6727cd261496SGeert Uytterhoeven } 67280a920b5bSAndy Whitcroft } 67290a920b5bSAndy Whitcroft 6730f0a594c1SAndy Whitcroft print report_dump(); 673113214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 673213214adfSAndy Whitcroft print "$filename " if ($summary_file); 67336c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 67346c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 67356c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 67366c72ffaaSAndy Whitcroft } 67378905a67cSAndy Whitcroft 6738d2c0a235SAndy Whitcroft if ($quiet == 0) { 6739ef212196SJoe Perches # If there were any defects found and not already fixing them 6740ef212196SJoe Perches if (!$clean and !$fix) { 6741ef212196SJoe Perches print << "EOM" 6742ef212196SJoe Perches 6743ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6744ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6745ef212196SJoe PerchesEOM 6746ef212196SJoe Perches } 6747d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6748d2c0a235SAndy Whitcroft # then suggest that. 6749d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6750b0781216SMike Frysinger $rpt_cleaners = 0; 6751d8469f16SJoe Perches print << "EOM" 6752d8469f16SJoe Perches 6753d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6754d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6755d8469f16SJoe PerchesEOM 6756d2c0a235SAndy Whitcroft } 6757d2c0a235SAndy Whitcroft } 6758d2c0a235SAndy Whitcroft 6759d752fcc8SJoe Perches if ($clean == 0 && $fix && 6760d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6761d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 67629624b8d6SJoe Perches my $newfile = $filename; 67639624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 67643705ce5bSJoe Perches my $linecount = 0; 67653705ce5bSJoe Perches my $f; 67663705ce5bSJoe Perches 6767d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6768d752fcc8SJoe Perches 67693705ce5bSJoe Perches open($f, '>', $newfile) 67703705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 67713705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 67723705ce5bSJoe Perches $linecount++; 67733705ce5bSJoe Perches if ($file) { 67743705ce5bSJoe Perches if ($linecount > 3) { 67753705ce5bSJoe Perches $fixed_line =~ s/^\+//; 67763705ce5bSJoe Perches print $f $fixed_line . "\n"; 67773705ce5bSJoe Perches } 67783705ce5bSJoe Perches } else { 67793705ce5bSJoe Perches print $f $fixed_line . "\n"; 67803705ce5bSJoe Perches } 67813705ce5bSJoe Perches } 67823705ce5bSJoe Perches close($f); 67833705ce5bSJoe Perches 67843705ce5bSJoe Perches if (!$quiet) { 67853705ce5bSJoe Perches print << "EOM"; 6786d8469f16SJoe Perches 67873705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 67883705ce5bSJoe Perches 67893705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 67903705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 67913705ce5bSJoe Perches 67923705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 67933705ce5bSJoe PerchesNo warranties, expressed or implied... 67943705ce5bSJoe PerchesEOM 67953705ce5bSJoe Perches } 67963705ce5bSJoe Perches } 67973705ce5bSJoe Perches 6798d8469f16SJoe Perches if ($quiet == 0) { 6799d8469f16SJoe Perches print "\n"; 6800d8469f16SJoe Perches if ($clean == 1) { 6801d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6802d8469f16SJoe Perches } else { 6803d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 68040a920b5bSAndy Whitcroft } 68050a920b5bSAndy Whitcroft } 68060a920b5bSAndy Whitcroft return $clean; 68070a920b5bSAndy Whitcroft} 6808