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"; 64dadf680dSJoe Perchesmy $allow_c99_comments = 1; 6577f5b10aSHannes Eder 6677f5b10aSHannes Edersub help { 6777f5b10aSHannes Eder my ($exitcode) = @_; 6877f5b10aSHannes Eder 6977f5b10aSHannes Eder print << "EOM"; 7077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 7177f5b10aSHannes EderVersion: $V 7277f5b10aSHannes Eder 7377f5b10aSHannes EderOptions: 7477f5b10aSHannes Eder -q, --quiet quiet 7577f5b10aSHannes Eder --no-tree run without a kernel tree 7677f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 7777f5b10aSHannes Eder --patch treat FILE as patchfile (default) 7877f5b10aSHannes Eder --emacs emacs compile window format 7977f5b10aSHannes Eder --terse one line per report 8034d8815fSJoe Perches --showfile emit diffed file position, not input file position 814a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 824a593c34SDu, Changbin single git commit with: 834a593c34SDu, Changbin <rev> 844a593c34SDu, Changbin <rev>^ 854a593c34SDu, Changbin <rev>~n 864a593c34SDu, Changbin multiple git commits with: 874a593c34SDu, Changbin <rev1>..<rev2> 884a593c34SDu, Changbin <rev1>...<rev2> 894a593c34SDu, Changbin <rev>-<count> 904a593c34SDu, Changbin git merges are ignored 9177f5b10aSHannes Eder -f, --file treat FILE as regular source file 9277f5b10aSHannes Eder --subjective, --strict enable more subjective tests 933beb42ecSJoe Perches --list-types list the possible message types 9491bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 95000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 963beb42ecSJoe Perches --show-types show the specific message type in the output 976cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 9856193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 9977f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10077f5b10aSHannes Eder --no-summary suppress the per-file summary 10177f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 10277f5b10aSHannes Eder --summary-file include the filename in summary 10377f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 10477f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 10577f5b10aSHannes Eder is all off) 10677f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 10777f5b10aSHannes Eder literally 1083705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1093705ce5bSJoe Perches If correctable single-line errors exist, create 1103705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1113705ce5bSJoe Perches with potential errors corrected to the preferred 1123705ce5bSJoe Perches checkpatch style 1139624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1149624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1159624b8d6SJoe Perches file. It's your fault if there's no backup or git 116d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 117d62a201fSDave Hansen runtime errors. 118ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 119f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 120ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 12175ad8c57SJerome Forissier --typedefsfile Read additional types from this file 122737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 123737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 12477f5b10aSHannes Eder -h, --help, --version display this help and exit 12577f5b10aSHannes Eder 12677f5b10aSHannes EderWhen FILE is - read standard input. 12777f5b10aSHannes EderEOM 12877f5b10aSHannes Eder 12977f5b10aSHannes Eder exit($exitcode); 13077f5b10aSHannes Eder} 13177f5b10aSHannes Eder 1323beb42ecSJoe Perchessub uniq { 1333beb42ecSJoe Perches my %seen; 1343beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1353beb42ecSJoe Perches} 1363beb42ecSJoe Perches 1373beb42ecSJoe Perchessub list_types { 1383beb42ecSJoe Perches my ($exitcode) = @_; 1393beb42ecSJoe Perches 1403beb42ecSJoe Perches my $count = 0; 1413beb42ecSJoe Perches 1423beb42ecSJoe Perches local $/ = undef; 1433beb42ecSJoe Perches 1443beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1453beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1463beb42ecSJoe Perches 1473beb42ecSJoe Perches my $text = <$script>; 1483beb42ecSJoe Perches close($script); 1493beb42ecSJoe Perches 1503beb42ecSJoe Perches my @types = (); 1510547fa58SJean Delvare # Also catch when type or level is passed through a variable 1520547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1533beb42ecSJoe Perches push (@types, $_); 1543beb42ecSJoe Perches } 1553beb42ecSJoe Perches @types = sort(uniq(@types)); 1563beb42ecSJoe Perches print("#\tMessage type\n\n"); 1573beb42ecSJoe Perches foreach my $type (@types) { 1583beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1593beb42ecSJoe Perches } 1603beb42ecSJoe Perches 1613beb42ecSJoe Perches exit($exitcode); 1623beb42ecSJoe Perches} 1633beb42ecSJoe Perches 164000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 165000d1cc1SJoe Perchesif (-f $conf) { 166000d1cc1SJoe Perches my @conf_args; 167000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 168000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 169000d1cc1SJoe Perches 170000d1cc1SJoe Perches while (<$conffile>) { 171000d1cc1SJoe Perches my $line = $_; 172000d1cc1SJoe Perches 173000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 174000d1cc1SJoe Perches $line =~ s/^\s*//g; 175000d1cc1SJoe Perches $line =~ s/\s+/ /g; 176000d1cc1SJoe Perches 177000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 178000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 179000d1cc1SJoe Perches 180000d1cc1SJoe Perches my @words = split(" ", $line); 181000d1cc1SJoe Perches foreach my $word (@words) { 182000d1cc1SJoe Perches last if ($word =~ m/^#/); 183000d1cc1SJoe Perches push (@conf_args, $word); 184000d1cc1SJoe Perches } 185000d1cc1SJoe Perches } 186000d1cc1SJoe Perches close($conffile); 187000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 188000d1cc1SJoe Perches} 189000d1cc1SJoe Perches 190737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 191737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 192737c0767SJohn Brooksforeach (@ARGV) { 193737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 194737c0767SJohn Brooks $_ = "--color=$color"; 195737c0767SJohn Brooks } 196737c0767SJohn Brooks} 197737c0767SJohn Brooks 1980a920b5bSAndy WhitcroftGetOptions( 1996c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2000a920b5bSAndy Whitcroft 'tree!' => \$tree, 2010a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2020a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2036c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2048905a67cSAndy Whitcroft 'terse!' => \$terse, 20534d8815fSJoe Perches 'showfile!' => \$showfile, 20677f5b10aSHannes Eder 'f|file!' => \$file, 2074a593c34SDu, Changbin 'g|git!' => \$git, 2086c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2096c72ffaaSAndy Whitcroft 'strict!' => \$check, 210000d1cc1SJoe Perches 'ignore=s' => \@ignore, 21191bfe484SJoe Perches 'types=s' => \@use, 212000d1cc1SJoe Perches 'show-types!' => \$show_types, 2133beb42ecSJoe Perches 'list-types!' => \$list_types, 2146cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 21556193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 2166c72ffaaSAndy Whitcroft 'root=s' => \$root, 2178905a67cSAndy Whitcroft 'summary!' => \$summary, 2188905a67cSAndy Whitcroft 'mailback!' => \$mailback, 21913214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2203705ce5bSJoe Perches 'fix!' => \$fix, 2219624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 222d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 223c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 224773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 225ebfd7d62SJoe Perches 'codespell!' => \$codespell, 226ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 22775ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 228737c0767SJohn Brooks 'color=s' => \$color, 229737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 230737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 23177f5b10aSHannes Eder 'h|help' => \$help, 23277f5b10aSHannes Eder 'version' => \$help 23377f5b10aSHannes Eder) or help(1); 23477f5b10aSHannes Eder 23577f5b10aSHannes Ederhelp(0) if ($help); 2360a920b5bSAndy Whitcroft 2373beb42ecSJoe Percheslist_types(0) if ($list_types); 2383beb42ecSJoe Perches 2399624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2402ac73b4fSJoe Perches$check_orig = $check; 2419624b8d6SJoe Perches 2420a920b5bSAndy Whitcroftmy $exit = 0; 2430a920b5bSAndy Whitcroft 2445b57980dSJoe Perchesmy $perl_version_ok = 1; 245d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2465b57980dSJoe Perches $perl_version_ok = 0; 247d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2485b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 249d62a201fSDave Hansen} 250d62a201fSDave Hansen 25145107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2520a920b5bSAndy Whitcroftif ($#ARGV < 0) { 25345107ff6SAllen Hubbe push(@ARGV, '-'); 2540a920b5bSAndy Whitcroft} 2550a920b5bSAndy Whitcroft 256737c0767SJohn Brooksif ($color =~ /^[01]$/) { 257737c0767SJohn Brooks $color = !$color; 258737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 259737c0767SJohn Brooks $color = 1; 260737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 261737c0767SJohn Brooks $color = 0; 262737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 263737c0767SJohn Brooks $color = (-t STDOUT); 264737c0767SJohn Brooks} else { 265737c0767SJohn Brooks die "Invalid color mode: $color\n"; 266737c0767SJohn Brooks} 267737c0767SJohn Brooks 26891bfe484SJoe Perchessub hash_save_array_words { 26991bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 27091bfe484SJoe Perches 27191bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 27291bfe484SJoe Perches foreach my $word (@array) { 273000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 274000d1cc1SJoe Perches $word =~ s/^\s*//g; 275000d1cc1SJoe Perches $word =~ s/\s+/ /g; 276000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 277000d1cc1SJoe Perches 278000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 279000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 280000d1cc1SJoe Perches 28191bfe484SJoe Perches $hashRef->{$word}++; 282000d1cc1SJoe Perches } 28391bfe484SJoe Perches} 28491bfe484SJoe Perches 28591bfe484SJoe Perchessub hash_show_words { 28691bfe484SJoe Perches my ($hashRef, $prefix) = @_; 28791bfe484SJoe Perches 2883c816e49SJoe Perches if (keys %$hashRef) { 289d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 29058cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 29191bfe484SJoe Perches print " $word"; 29291bfe484SJoe Perches } 293d8469f16SJoe Perches print "\n"; 29491bfe484SJoe Perches } 29591bfe484SJoe Perches} 29691bfe484SJoe Perches 29791bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 29891bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 299000d1cc1SJoe Perches 300c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 301c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3027429c690SAndy Whitcroftmy $dbg_type = 0; 303a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 304c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 30521caa13cSAndy Whitcroft ## no critic 30621caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 30721caa13cSAndy Whitcroft die "$@" if ($@); 308c2fdda0dSAndy Whitcroft} 309c2fdda0dSAndy Whitcroft 310d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 311d2c0a235SAndy Whitcroft 3128905a67cSAndy Whitcroftif ($terse) { 3138905a67cSAndy Whitcroft $emacs = 1; 3148905a67cSAndy Whitcroft $quiet++; 3158905a67cSAndy Whitcroft} 3168905a67cSAndy Whitcroft 3176c72ffaaSAndy Whitcroftif ($tree) { 3186c72ffaaSAndy Whitcroft if (defined $root) { 3196c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3206c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3216c72ffaaSAndy Whitcroft } 3226c72ffaaSAndy Whitcroft } else { 3236c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3246c72ffaaSAndy Whitcroft $root = '.'; 3256c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3266c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3276c72ffaaSAndy Whitcroft $root = $1; 3286c72ffaaSAndy Whitcroft } 3296c72ffaaSAndy Whitcroft } 3306c72ffaaSAndy Whitcroft 3316c72ffaaSAndy Whitcroft if (!defined $root) { 3320a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3330a920b5bSAndy Whitcroft exit(2); 3340a920b5bSAndy Whitcroft } 3356c72ffaaSAndy Whitcroft} 3366c72ffaaSAndy Whitcroft 3376c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3386c72ffaaSAndy Whitcroft 3392ceb532bSAndy Whitcroftour $Ident = qr{ 3402ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3412ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3422ceb532bSAndy Whitcroft }x; 3436c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3446c72ffaaSAndy Whitcroftour $Sparse = qr{ 3456c72ffaaSAndy Whitcroft __user| 3466c72ffaaSAndy Whitcroft __kernel| 3476c72ffaaSAndy Whitcroft __force| 3486c72ffaaSAndy Whitcroft __iomem| 3496c72ffaaSAndy Whitcroft __must_check| 350417495edSAndy Whitcroft __kprobes| 351165e72a6SSven Eckelmann __ref| 35233aa4597SGeert Uytterhoeven __refconst| 35333aa4597SGeert Uytterhoeven __refdata| 354ad315455SBoqun Feng __rcu| 355ad315455SBoqun Feng __private 3566c72ffaaSAndy Whitcroft }x; 357e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 358e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 359e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 360e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 361e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3628716de38SJoe Perches 36352131292SWolfram Sang# Notes to $Attribute: 36452131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3656c72ffaaSAndy Whitcroftour $Attribute = qr{ 3666c72ffaaSAndy Whitcroft const| 36703f1df7dSJoe Perches __percpu| 36803f1df7dSJoe Perches __nocast| 36903f1df7dSJoe Perches __safe| 37046d832f5SMichael S. Tsirkin __bitwise| 37103f1df7dSJoe Perches __packed__| 37203f1df7dSJoe Perches __packed2__| 37303f1df7dSJoe Perches __naked| 37403f1df7dSJoe Perches __maybe_unused| 37503f1df7dSJoe Perches __always_unused| 37603f1df7dSJoe Perches __noreturn| 37703f1df7dSJoe Perches __used| 37803f1df7dSJoe Perches __cold| 379e23ef1f3SJoe Perches __pure| 38003f1df7dSJoe Perches __noclone| 38103f1df7dSJoe Perches __deprecated| 3826c72ffaaSAndy Whitcroft __read_mostly| 3836c72ffaaSAndy Whitcroft __kprobes| 3848716de38SJoe Perches $InitAttribute| 38524e1d81aSAndy Whitcroft ____cacheline_aligned| 38624e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3875fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3885fe3af11SAndy Whitcroft __weak 3896c72ffaaSAndy Whitcroft }x; 390c45dcabdSAndy Whitcroftour $Modifier; 39191cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 3926c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 3936c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 3946c72ffaaSAndy Whitcroft 39595e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 39695e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 39795e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 39895e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 3992435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 400c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 401326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 402326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 403326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 40474349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4052435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 406326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 407447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 40823f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4096c72ffaaSAndy Whitcroftour $Operators = qr{ 4106c72ffaaSAndy Whitcroft <=|>=|==|!=| 4116c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 41223f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4136c72ffaaSAndy Whitcroft }x; 4146c72ffaaSAndy Whitcroft 41591cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 41691cb5195SJoe Perches 417ab7e23f3SJoe Perchesour $BasicType; 4188905a67cSAndy Whitcroftour $NonptrType; 4191813087dSJoe Perchesour $NonptrTypeMisordered; 4208716de38SJoe Perchesour $NonptrTypeWithAttr; 4218905a67cSAndy Whitcroftour $Type; 4221813087dSJoe Perchesour $TypeMisordered; 4238905a67cSAndy Whitcroftour $Declare; 4241813087dSJoe Perchesour $DeclareMisordered; 4258905a67cSAndy Whitcroft 42615662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 42715662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 428171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 429171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 430171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 431171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 432171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 433171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 434171ae1a4SAndy Whitcroft}x; 435171ae1a4SAndy Whitcroft 43615662b3eSJoe Perchesour $UTF8 = qr{ 43715662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 43815662b3eSJoe Perches | $NON_ASCII_UTF8 43915662b3eSJoe Perches}x; 44015662b3eSJoe Perches 441e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 442021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 443021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 444021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 445021158b4SJoe Perches)}; 446e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 447fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4488ed22cadSAndy Whitcroft atomic_t 4498ed22cadSAndy Whitcroft)}; 450e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 451e6176fa4SJoe Perches $typeC99Typedefs\b| 452e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 453e6176fa4SJoe Perches $typeKernelTypedefs\b 454e6176fa4SJoe Perches)}; 4558ed22cadSAndy Whitcroft 4566d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4576d32f7a3SJoe Perches 458691e669bSJoe Perchesour $logFunctions = qr{(?x: 459758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4607d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 46187bd499aSJoe Perches TP_printk| 4626e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 463b0531722SJoe Perches panic| 46406668727SJoe Perches MODULE_[A-Z_]+| 46506668727SJoe Perches seq_vprintf|seq_printf|seq_puts 466691e669bSJoe Perches)}; 467691e669bSJoe Perches 46820112475SJoe Perchesour $signature_tags = qr{(?xi: 46920112475SJoe Perches Signed-off-by:| 47020112475SJoe Perches Acked-by:| 47120112475SJoe Perches Tested-by:| 47220112475SJoe Perches Reviewed-by:| 47320112475SJoe Perches Reported-by:| 4748543ae12SMugunthan V N Suggested-by:| 47520112475SJoe Perches To:| 47620112475SJoe Perches Cc: 47720112475SJoe Perches)}; 47820112475SJoe Perches 4791813087dSJoe Perchesour @typeListMisordered = ( 4801813087dSJoe Perches qr{char\s+(?:un)?signed}, 4811813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 4821813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 4831813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 4841813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 4851813087dSJoe Perches qr{short\s+(?:un)?signed}, 4861813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 4871813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 4881813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 4891813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 4901813087dSJoe Perches qr{int\s+(?:un)?signed}, 4911813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 4921813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 4931813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 4941813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 4951813087dSJoe Perches qr{long\s+(?:un)?signed}, 4961813087dSJoe Perches); 4971813087dSJoe Perches 4988905a67cSAndy Whitcroftour @typeList = ( 4998905a67cSAndy Whitcroft qr{void}, 5000c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5010c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5020c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5030c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5040c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5050c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5060c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5070c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5080c773d9dSJoe Perches qr{(?:un)?signed}, 5098905a67cSAndy Whitcroft qr{float}, 5108905a67cSAndy Whitcroft qr{double}, 5118905a67cSAndy Whitcroft qr{bool}, 5128905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5138905a67cSAndy Whitcroft qr{union\s+$Ident}, 5148905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5158905a67cSAndy Whitcroft qr{${Ident}_t}, 5168905a67cSAndy Whitcroft qr{${Ident}_handler}, 5178905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5181813087dSJoe Perches @typeListMisordered, 5198905a67cSAndy Whitcroft); 520938224b5SJoe Perches 521938224b5SJoe Perchesour $C90_int_types = qr{(?x: 522938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 523938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 524938224b5SJoe Perches long\s+long\s+(?:un)?signed| 525938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 526938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 527938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 528938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 529938224b5SJoe Perches 530938224b5SJoe Perches long\s+int\s+(?:un)?signed| 531938224b5SJoe Perches long\s+(?:un)?signed\s+int| 532938224b5SJoe Perches long\s+(?:un)?signed| 533938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 534938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 535938224b5SJoe Perches int\s+long\s+(?:un)?signed| 536938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 537938224b5SJoe Perches 538938224b5SJoe Perches int\s+(?:un)?signed| 539938224b5SJoe Perches (?:(?:un)?signed\s+)?int 540938224b5SJoe Perches)}; 541938224b5SJoe Perches 542485ff23eSAlex Dowadour @typeListFile = (); 5438716de38SJoe Perchesour @typeListWithAttr = ( 5448716de38SJoe Perches @typeList, 5458716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5468716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5478716de38SJoe Perches); 5488716de38SJoe Perches 549c45dcabdSAndy Whitcroftour @modifierList = ( 550c45dcabdSAndy Whitcroft qr{fastcall}, 551c45dcabdSAndy Whitcroft); 552485ff23eSAlex Dowadour @modifierListFile = (); 5538905a67cSAndy Whitcroft 5542435880fSJoe Perchesour @mode_permission_funcs = ( 5552435880fSJoe Perches ["module_param", 3], 5562435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5572435880fSJoe Perches ["module_param_array_named", 5], 5582435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5592435880fSJoe Perches ["proc_create(?:_data|)", 2], 560459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 561459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 562459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 563459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 564459cf0aeSJoe Perches ["__ATTR", 2], 5652435880fSJoe Perches); 5662435880fSJoe Perches 567515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 568515a235eSJoe Perchesour $mode_perms_search = ""; 569515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 570515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 571515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 572515a235eSJoe Perches} 57300180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 574515a235eSJoe Perches 575b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 576b392c64fSJoe Perches S_IWUGO | 577b392c64fSJoe Perches S_IWOTH | 578b392c64fSJoe Perches S_IRWXUGO | 579b392c64fSJoe Perches S_IALLUGO | 580b392c64fSJoe Perches 0[0-7][0-7][2367] 581b392c64fSJoe Perches}x; 582b392c64fSJoe Perches 583f90774e1SJoe Perchesour %mode_permission_string_types = ( 584f90774e1SJoe Perches "S_IRWXU" => 0700, 585f90774e1SJoe Perches "S_IRUSR" => 0400, 586f90774e1SJoe Perches "S_IWUSR" => 0200, 587f90774e1SJoe Perches "S_IXUSR" => 0100, 588f90774e1SJoe Perches "S_IRWXG" => 0070, 589f90774e1SJoe Perches "S_IRGRP" => 0040, 590f90774e1SJoe Perches "S_IWGRP" => 0020, 591f90774e1SJoe Perches "S_IXGRP" => 0010, 592f90774e1SJoe Perches "S_IRWXO" => 0007, 593f90774e1SJoe Perches "S_IROTH" => 0004, 594f90774e1SJoe Perches "S_IWOTH" => 0002, 595f90774e1SJoe Perches "S_IXOTH" => 0001, 596f90774e1SJoe Perches "S_IRWXUGO" => 0777, 597f90774e1SJoe Perches "S_IRUGO" => 0444, 598f90774e1SJoe Perches "S_IWUGO" => 0222, 599f90774e1SJoe Perches "S_IXUGO" => 0111, 600f90774e1SJoe Perches); 601f90774e1SJoe Perches 602f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 603f90774e1SJoe Perchesour $mode_perms_string_search = ""; 604f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 605f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 606f90774e1SJoe Perches $mode_perms_string_search .= $entry; 607f90774e1SJoe Perches} 60800180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 60900180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 61000180468SJoe Perches ${single_mode_perms_string_search} 61100180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 61200180468SJoe Perches}x; 61300180468SJoe Perches 61400180468SJoe Perchessub perms_to_octal { 61500180468SJoe Perches my ($string) = @_; 61600180468SJoe Perches 61700180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 61800180468SJoe Perches 61900180468SJoe Perches my $val = ""; 62000180468SJoe Perches my $oval = ""; 62100180468SJoe Perches my $to = 0; 62200180468SJoe Perches my $curpos = 0; 62300180468SJoe Perches my $lastpos = 0; 62400180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 62500180468SJoe Perches $curpos = pos($string); 62600180468SJoe Perches my $match = $2; 62700180468SJoe Perches my $omatch = $1; 62800180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 62900180468SJoe Perches $lastpos = $curpos; 63000180468SJoe Perches $to |= $mode_permission_string_types{$match}; 63100180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 63200180468SJoe Perches $val .= $match; 63300180468SJoe Perches $oval .= $omatch; 63400180468SJoe Perches } 63500180468SJoe Perches $oval =~ s/^\s*\|\s*//; 63600180468SJoe Perches $oval =~ s/\s*\|\s*$//; 63700180468SJoe Perches return sprintf("%04o", $to); 63800180468SJoe Perches} 639f90774e1SJoe Perches 6407840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6417840a94cSWolfram Sang irq| 642cdcee686SSergey Ryazanov memory| 643cdcee686SSergey Ryazanov time| 644cdcee686SSergey Ryazanov reboot 6457840a94cSWolfram Sang)}; 6467840a94cSWolfram Sang# memory.h: ARM has a custom one 6477840a94cSWolfram Sang 64866b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 64966b47b4aSKees Cookmy $misspellings; 65066b47b4aSKees Cookmy %spelling_fix; 65136061e38SJoe Perches 65236061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 65366b47b4aSKees Cook while (<$spelling>) { 65466b47b4aSKees Cook my $line = $_; 65566b47b4aSKees Cook 65666b47b4aSKees Cook $line =~ s/\s*\n?$//g; 65766b47b4aSKees Cook $line =~ s/^\s*//g; 65866b47b4aSKees Cook 65966b47b4aSKees Cook next if ($line =~ m/^\s*#/); 66066b47b4aSKees Cook next if ($line =~ m/^\s*$/); 66166b47b4aSKees Cook 66266b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 66366b47b4aSKees Cook 66466b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 66566b47b4aSKees Cook } 66666b47b4aSKees Cook close($spelling); 66736061e38SJoe Perches} else { 66836061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 66936061e38SJoe Perches} 67066b47b4aSKees Cook 671ebfd7d62SJoe Perchesif ($codespell) { 672ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 673ebfd7d62SJoe Perches while (<$spelling>) { 674ebfd7d62SJoe Perches my $line = $_; 675ebfd7d62SJoe Perches 676ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 677ebfd7d62SJoe Perches $line =~ s/^\s*//g; 678ebfd7d62SJoe Perches 679ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 680ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 681ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 682ebfd7d62SJoe Perches 683ebfd7d62SJoe Perches $line =~ s/,.*$//; 684ebfd7d62SJoe Perches 685ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 686ebfd7d62SJoe Perches 687ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 688ebfd7d62SJoe Perches } 689ebfd7d62SJoe Perches close($spelling); 690ebfd7d62SJoe Perches } else { 691ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 692ebfd7d62SJoe Perches } 693ebfd7d62SJoe Perches} 694ebfd7d62SJoe Perches 695ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 696ebfd7d62SJoe Perches 69775ad8c57SJerome Forissiersub read_words { 69875ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 69975ad8c57SJerome Forissier 70075ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 70175ad8c57SJerome Forissier while (<$words>) { 702bf1fa1daSJoe Perches my $line = $_; 703bf1fa1daSJoe Perches 704bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 705bf1fa1daSJoe Perches $line =~ s/^\s*//g; 706bf1fa1daSJoe Perches 707bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 708bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 709bf1fa1daSJoe Perches if ($line =~ /\s/) { 71075ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 711bf1fa1daSJoe Perches next; 712bf1fa1daSJoe Perches } 713bf1fa1daSJoe Perches 71475ad8c57SJerome Forissier $$wordsRef .= '|' if ($$wordsRef ne ""); 71575ad8c57SJerome Forissier $$wordsRef .= $line; 716bf1fa1daSJoe Perches } 71775ad8c57SJerome Forissier close($file); 71875ad8c57SJerome Forissier return 1; 719bf1fa1daSJoe Perches } 720bf1fa1daSJoe Perches 72175ad8c57SJerome Forissier return 0; 72275ad8c57SJerome Forissier} 72375ad8c57SJerome Forissier 72475ad8c57SJerome Forissiermy $const_structs = ""; 72575ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile) 72675ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 72775ad8c57SJerome Forissier 72875ad8c57SJerome Forissiermy $typeOtherTypedefs = ""; 72975ad8c57SJerome Forissierif (length($typedefsfile)) { 73075ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 73175ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 73275ad8c57SJerome Forissier} 73375ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); 73475ad8c57SJerome Forissier 7358905a67cSAndy Whitcroftsub build_types { 736485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 737485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7381813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7398716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 740c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 741ab7e23f3SJoe Perches $BasicType = qr{ 742ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 743ab7e23f3SJoe Perches (?:${all}\b) 744ab7e23f3SJoe Perches }x; 7458905a67cSAndy Whitcroft $NonptrType = qr{ 746d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 747cf655043SAndy Whitcroft (?: 7486b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 7498ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 750c45dcabdSAndy Whitcroft (?:${all}\b) 751cf655043SAndy Whitcroft ) 752c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 7538905a67cSAndy Whitcroft }x; 7541813087dSJoe Perches $NonptrTypeMisordered = qr{ 7551813087dSJoe Perches (?:$Modifier\s+|const\s+)* 7561813087dSJoe Perches (?: 7571813087dSJoe Perches (?:${Misordered}\b) 7581813087dSJoe Perches ) 7591813087dSJoe Perches (?:\s+$Modifier|\s+const)* 7601813087dSJoe Perches }x; 7618716de38SJoe Perches $NonptrTypeWithAttr = qr{ 7628716de38SJoe Perches (?:$Modifier\s+|const\s+)* 7638716de38SJoe Perches (?: 7648716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 7658716de38SJoe Perches (?:$typeTypedefs\b)| 7668716de38SJoe Perches (?:${allWithAttr}\b) 7678716de38SJoe Perches ) 7688716de38SJoe Perches (?:\s+$Modifier|\s+const)* 7698716de38SJoe Perches }x; 7708905a67cSAndy Whitcroft $Type = qr{ 771c45dcabdSAndy Whitcroft $NonptrType 7721574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 773c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 7748905a67cSAndy Whitcroft }x; 7751813087dSJoe Perches $TypeMisordered = qr{ 7761813087dSJoe Perches $NonptrTypeMisordered 7771813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 7781813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 7791813087dSJoe Perches }x; 78091cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 7811813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 7828905a67cSAndy Whitcroft} 7838905a67cSAndy Whitcroftbuild_types(); 7846c72ffaaSAndy Whitcroft 7857d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 786d1fe9c09SJoe Perches 787d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 788d1fe9c09SJoe Perches# requires at least perl version v5.10.0 789d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 790d1fe9c09SJoe Perches 791d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 7922435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 793c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 7947d2367afSJoe Perches 795f8422308SJoe Perchesour $declaration_macros = qr{(?x: 7963e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 797fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 7983d102fc0SGilad Ben-Yossef (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(| 7993d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 800f8422308SJoe Perches)}; 801f8422308SJoe Perches 8027d2367afSJoe Perchessub deparenthesize { 8037d2367afSJoe Perches my ($string) = @_; 8047d2367afSJoe Perches return "" if (!defined($string)); 8055b9553abSJoe Perches 8065b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8075b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8085b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8095b9553abSJoe Perches } 8105b9553abSJoe Perches 8117d2367afSJoe Perches $string =~ s@\s+@ @g; 8125b9553abSJoe Perches 8137d2367afSJoe Perches return $string; 8147d2367afSJoe Perches} 8157d2367afSJoe Perches 8163445686aSJoe Perchessub seed_camelcase_file { 8173445686aSJoe Perches my ($file) = @_; 8183445686aSJoe Perches 8193445686aSJoe Perches return if (!(-f $file)); 8203445686aSJoe Perches 8213445686aSJoe Perches local $/; 8223445686aSJoe Perches 8233445686aSJoe Perches open(my $include_file, '<', "$file") 8243445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8253445686aSJoe Perches my $text = <$include_file>; 8263445686aSJoe Perches close($include_file); 8273445686aSJoe Perches 8283445686aSJoe Perches my @lines = split('\n', $text); 8293445686aSJoe Perches 8303445686aSJoe Perches foreach my $line (@lines) { 8313445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8323445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8333445686aSJoe Perches $camelcase{$1} = 1; 83411ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 83511ea516aSJoe Perches $camelcase{$1} = 1; 83611ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8373445686aSJoe Perches $camelcase{$1} = 1; 8383445686aSJoe Perches } 8393445686aSJoe Perches } 8403445686aSJoe Perches} 8413445686aSJoe Perches 84285b0ee18SJoe Perchessub is_maintained_obsolete { 84385b0ee18SJoe Perches my ($filename) = @_; 84485b0ee18SJoe Perches 845f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 84685b0ee18SJoe Perches 8470616efa4SJoe Perches my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 84885b0ee18SJoe Perches 84985b0ee18SJoe Perches return $status =~ /obsolete/i; 85085b0ee18SJoe Perches} 85185b0ee18SJoe Perches 8523b6e8ac9SJoe Perchessub is_SPDX_License_valid { 8533b6e8ac9SJoe Perches my ($license) = @_; 8543b6e8ac9SJoe Perches 8553b6e8ac9SJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py")); 8563b6e8ac9SJoe Perches 8573b6e8ac9SJoe Perches my $status = `echo "$license" | python $root/scripts/spdxcheck.py -`; 8583b6e8ac9SJoe Perches return 0 if ($status ne ""); 8593b6e8ac9SJoe Perches return 1; 8603b6e8ac9SJoe Perches} 8613b6e8ac9SJoe Perches 8623445686aSJoe Perchesmy $camelcase_seeded = 0; 8633445686aSJoe Perchessub seed_camelcase_includes { 8643445686aSJoe Perches return if ($camelcase_seeded); 8653445686aSJoe Perches 8663445686aSJoe Perches my $files; 867c707a81dSJoe Perches my $camelcase_cache = ""; 868c707a81dSJoe Perches my @include_files = (); 869c707a81dSJoe Perches 870c707a81dSJoe Perches $camelcase_seeded = 1; 871351b2a1fSJoe Perches 8723645e328SRichard Genoud if (-e ".git") { 873351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 874351b2a1fSJoe Perches chomp $git_last_include_commit; 875c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 876c707a81dSJoe Perches } else { 877c707a81dSJoe Perches my $last_mod_date = 0; 878c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 879c707a81dSJoe Perches @include_files = split('\n', $files); 880c707a81dSJoe Perches foreach my $file (@include_files) { 881c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 882c707a81dSJoe Perches localtime((stat $file)[9])); 883c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 884c707a81dSJoe Perches } 885c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 886c707a81dSJoe Perches } 887c707a81dSJoe Perches 888c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 889c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 890c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 891351b2a1fSJoe Perches while (<$camelcase_file>) { 892351b2a1fSJoe Perches chomp; 893351b2a1fSJoe Perches $camelcase{$_} = 1; 894351b2a1fSJoe Perches } 895351b2a1fSJoe Perches close($camelcase_file); 896351b2a1fSJoe Perches 897351b2a1fSJoe Perches return; 898351b2a1fSJoe Perches } 899c707a81dSJoe Perches 9003645e328SRichard Genoud if (-e ".git") { 901c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 902c707a81dSJoe Perches @include_files = split('\n', $files); 9033445686aSJoe Perches } 904c707a81dSJoe Perches 9053445686aSJoe Perches foreach my $file (@include_files) { 9063445686aSJoe Perches seed_camelcase_file($file); 9073445686aSJoe Perches } 908351b2a1fSJoe Perches 909c707a81dSJoe Perches if ($camelcase_cache ne "") { 910351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 911c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 912c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 913351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 914351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 915351b2a1fSJoe Perches } 916351b2a1fSJoe Perches close($camelcase_file); 917351b2a1fSJoe Perches } 9183445686aSJoe Perches} 9193445686aSJoe Perches 920d311cd44SJoe Perchessub git_commit_info { 921d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 922d311cd44SJoe Perches 923d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 924d311cd44SJoe Perches 925d311cd44SJoe Perches my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; 926d311cd44SJoe Perches $output =~ s/^\s*//gm; 927d311cd44SJoe Perches my @lines = split("\n", $output); 928d311cd44SJoe Perches 9290d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9300d7835fcSJoe Perches 931d311cd44SJoe Perches if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { 932d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 933d311cd44SJoe Perches# all matching commit ids, but it's very slow... 934d311cd44SJoe Perches# 935d311cd44SJoe Perches# echo "checking commits $1..." 936d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 937d311cd44SJoe Perches# while read line ; do 938d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 939d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 940d311cd44SJoe Perches# done 941d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 942948b133aSHeinrich Schuchardt $id = undef; 943d311cd44SJoe Perches } else { 944d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 945d311cd44SJoe Perches $desc = substr($lines[0], 41); 946d311cd44SJoe Perches } 947d311cd44SJoe Perches 948d311cd44SJoe Perches return ($id, $desc); 949d311cd44SJoe Perches} 950d311cd44SJoe Perches 9516c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 9520a920b5bSAndy Whitcroft 95300df344fSAndy Whitcroftmy @rawlines = (); 954c2fdda0dSAndy Whitcroftmy @lines = (); 9553705ce5bSJoe Perchesmy @fixed = (); 956d752fcc8SJoe Perchesmy @fixed_inserted = (); 957d752fcc8SJoe Perchesmy @fixed_deleted = (); 958194f66fcSJoe Perchesmy $fixlinenr = -1; 959194f66fcSJoe Perches 9604a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 9614a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 9624a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 9634a593c34SDu, Changbin 9644a593c34SDu, Changbinif ($git) { 9654a593c34SDu, Changbin my @commits = (); 9660dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 9674a593c34SDu, Changbin my $git_range; 96828898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 96928898fd1SJoe Perches $git_range = "-$2 $1"; 9704a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 9714a593c34SDu, Changbin $git_range = "$commit_expr"; 9724a593c34SDu, Changbin } else { 9730dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 9740dea9f1eSJoe Perches } 9750dea9f1eSJoe Perches my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 9760dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 97728898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 97828898fd1SJoe Perches next if (!defined($1) || !defined($2)); 9790dea9f1eSJoe Perches my $sha1 = $1; 9800dea9f1eSJoe Perches my $subject = $2; 9810dea9f1eSJoe Perches unshift(@commits, $sha1); 9820dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 9834a593c34SDu, Changbin } 9844a593c34SDu, Changbin } 9854a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 9864a593c34SDu, Changbin @ARGV = @commits; 9874a593c34SDu, Changbin} 9884a593c34SDu, Changbin 989c2fdda0dSAndy Whitcroftmy $vname; 9906c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 99121caa13cSAndy Whitcroft my $FILE; 9924a593c34SDu, Changbin if ($git) { 9934a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 9944a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 9954a593c34SDu, Changbin } elsif ($file) { 99621caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 9976c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 99821caa13cSAndy Whitcroft } elsif ($filename eq '-') { 99921caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 10006c72ffaaSAndy Whitcroft } else { 100121caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 10026c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 10036c72ffaaSAndy Whitcroft } 1004c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1005c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 10064a593c34SDu, Changbin } elsif ($git) { 10070dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1008c2fdda0dSAndy Whitcroft } else { 1009c2fdda0dSAndy Whitcroft $vname = $filename; 1010c2fdda0dSAndy Whitcroft } 101121caa13cSAndy Whitcroft while (<$FILE>) { 10120a920b5bSAndy Whitcroft chomp; 101300df344fSAndy Whitcroft push(@rawlines, $_); 10146c72ffaaSAndy Whitcroft } 101521caa13cSAndy Whitcroft close($FILE); 1016d8469f16SJoe Perches 1017d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1018d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1019d8469f16SJoe Perches print "$vname\n"; 1020d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1021d8469f16SJoe Perches } 1022d8469f16SJoe Perches 1023c2fdda0dSAndy Whitcroft if (!process($filename)) { 10240a920b5bSAndy Whitcroft $exit = 1; 10250a920b5bSAndy Whitcroft } 102600df344fSAndy Whitcroft @rawlines = (); 102713214adfSAndy Whitcroft @lines = (); 10283705ce5bSJoe Perches @fixed = (); 1029d752fcc8SJoe Perches @fixed_inserted = (); 1030d752fcc8SJoe Perches @fixed_deleted = (); 1031194f66fcSJoe Perches $fixlinenr = -1; 1032485ff23eSAlex Dowad @modifierListFile = (); 1033485ff23eSAlex Dowad @typeListFile = (); 1034485ff23eSAlex Dowad build_types(); 10350a920b5bSAndy Whitcroft} 10360a920b5bSAndy Whitcroft 1037d8469f16SJoe Perchesif (!$quiet) { 10383c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 10393c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 10403c816e49SJoe Perches 10415b57980dSJoe Perches if (!$perl_version_ok) { 1042d8469f16SJoe Perches print << "EOM" 1043d8469f16SJoe Perches 1044d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 10455b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1046d8469f16SJoe PerchesEOM 1047d8469f16SJoe Perches } 1048d8469f16SJoe Perches if ($exit) { 1049d8469f16SJoe Perches print << "EOM" 1050d8469f16SJoe Perches 1051d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1052d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1053d8469f16SJoe PerchesEOM 1054d8469f16SJoe Perches } 1055d8469f16SJoe Perches} 1056d8469f16SJoe Perches 10570a920b5bSAndy Whitcroftexit($exit); 10580a920b5bSAndy Whitcroft 10590a920b5bSAndy Whitcroftsub top_of_kernel_tree { 10606c72ffaaSAndy Whitcroft my ($root) = @_; 10616c72ffaaSAndy Whitcroft 10626c72ffaaSAndy Whitcroft my @tree_check = ( 10636c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 10646c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 10656c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 10666c72ffaaSAndy Whitcroft ); 10676c72ffaaSAndy Whitcroft 10686c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 10696c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 10700a920b5bSAndy Whitcroft return 0; 10710a920b5bSAndy Whitcroft } 10726c72ffaaSAndy Whitcroft } 10736c72ffaaSAndy Whitcroft return 1; 10746c72ffaaSAndy Whitcroft} 10750a920b5bSAndy Whitcroft 107620112475SJoe Perchessub parse_email { 107720112475SJoe Perches my ($formatted_email) = @_; 107820112475SJoe Perches 107920112475SJoe Perches my $name = ""; 108020112475SJoe Perches my $address = ""; 108120112475SJoe Perches my $comment = ""; 108220112475SJoe Perches 108320112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 108420112475SJoe Perches $name = $1; 108520112475SJoe Perches $address = $2; 108620112475SJoe Perches $comment = $3 if defined $3; 108720112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 108820112475SJoe Perches $address = $1; 108920112475SJoe Perches $comment = $2 if defined $2; 109020112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 109120112475SJoe Perches $address = $1; 109220112475SJoe Perches $comment = $2 if defined $2; 109385e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 109420112475SJoe Perches $name = $formatted_email; 10953705ce5bSJoe Perches $name = trim($name); 109620112475SJoe Perches $name =~ s/^\"|\"$//g; 109720112475SJoe Perches # If there's a name left after stripping spaces and 109820112475SJoe Perches # leading quotes, and the address doesn't have both 109920112475SJoe Perches # leading and trailing angle brackets, the address 110020112475SJoe Perches # is invalid. ie: 110120112475SJoe Perches # "joe smith [email protected]" bad 110220112475SJoe Perches # "joe smith <[email protected]" bad 110320112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 110420112475SJoe Perches $name = ""; 110520112475SJoe Perches $address = ""; 110620112475SJoe Perches $comment = ""; 110720112475SJoe Perches } 110820112475SJoe Perches } 110920112475SJoe Perches 11103705ce5bSJoe Perches $name = trim($name); 111120112475SJoe Perches $name =~ s/^\"|\"$//g; 11123705ce5bSJoe Perches $address = trim($address); 111320112475SJoe Perches $address =~ s/^\<|\>$//g; 111420112475SJoe Perches 111520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 111620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 111720112475SJoe Perches $name = "\"$name\""; 111820112475SJoe Perches } 111920112475SJoe Perches 112020112475SJoe Perches return ($name, $address, $comment); 112120112475SJoe Perches} 112220112475SJoe Perches 112320112475SJoe Perchessub format_email { 112420112475SJoe Perches my ($name, $address) = @_; 112520112475SJoe Perches 112620112475SJoe Perches my $formatted_email; 112720112475SJoe Perches 11283705ce5bSJoe Perches $name = trim($name); 112920112475SJoe Perches $name =~ s/^\"|\"$//g; 11303705ce5bSJoe Perches $address = trim($address); 113120112475SJoe Perches 113220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 113320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 113420112475SJoe Perches $name = "\"$name\""; 113520112475SJoe Perches } 113620112475SJoe Perches 113720112475SJoe Perches if ("$name" eq "") { 113820112475SJoe Perches $formatted_email = "$address"; 113920112475SJoe Perches } else { 114020112475SJoe Perches $formatted_email = "$name <$address>"; 114120112475SJoe Perches } 114220112475SJoe Perches 114320112475SJoe Perches return $formatted_email; 114420112475SJoe Perches} 114520112475SJoe Perches 1146d311cd44SJoe Perchessub which { 1147d311cd44SJoe Perches my ($bin) = @_; 1148d311cd44SJoe Perches 1149d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1150d311cd44SJoe Perches if (-e "$path/$bin") { 1151d311cd44SJoe Perches return "$path/$bin"; 1152d311cd44SJoe Perches } 1153d311cd44SJoe Perches } 1154d311cd44SJoe Perches 1155d311cd44SJoe Perches return ""; 1156d311cd44SJoe Perches} 1157d311cd44SJoe Perches 1158000d1cc1SJoe Perchessub which_conf { 1159000d1cc1SJoe Perches my ($conf) = @_; 1160000d1cc1SJoe Perches 1161000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1162000d1cc1SJoe Perches if (-e "$path/$conf") { 1163000d1cc1SJoe Perches return "$path/$conf"; 1164000d1cc1SJoe Perches } 1165000d1cc1SJoe Perches } 1166000d1cc1SJoe Perches 1167000d1cc1SJoe Perches return ""; 1168000d1cc1SJoe Perches} 1169000d1cc1SJoe Perches 11700a920b5bSAndy Whitcroftsub expand_tabs { 11710a920b5bSAndy Whitcroft my ($str) = @_; 11720a920b5bSAndy Whitcroft 11730a920b5bSAndy Whitcroft my $res = ''; 11740a920b5bSAndy Whitcroft my $n = 0; 11750a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 11760a920b5bSAndy Whitcroft if ($c eq "\t") { 11770a920b5bSAndy Whitcroft $res .= ' '; 11780a920b5bSAndy Whitcroft $n++; 11790a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 11800a920b5bSAndy Whitcroft $res .= ' '; 11810a920b5bSAndy Whitcroft } 11820a920b5bSAndy Whitcroft next; 11830a920b5bSAndy Whitcroft } 11840a920b5bSAndy Whitcroft $res .= $c; 11850a920b5bSAndy Whitcroft $n++; 11860a920b5bSAndy Whitcroft } 11870a920b5bSAndy Whitcroft 11880a920b5bSAndy Whitcroft return $res; 11890a920b5bSAndy Whitcroft} 11906c72ffaaSAndy Whitcroftsub copy_spacing { 1191773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 11926c72ffaaSAndy Whitcroft return $res; 11936c72ffaaSAndy Whitcroft} 11940a920b5bSAndy Whitcroft 11954a0df2efSAndy Whitcroftsub line_stats { 11964a0df2efSAndy Whitcroft my ($line) = @_; 11974a0df2efSAndy Whitcroft 11984a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 11994a0df2efSAndy Whitcroft $line =~ s/^.//; 12004a0df2efSAndy Whitcroft $line = expand_tabs($line); 12014a0df2efSAndy Whitcroft 12024a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 12034a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 12044a0df2efSAndy Whitcroft 12054a0df2efSAndy Whitcroft return (length($line), length($white)); 12064a0df2efSAndy Whitcroft} 12074a0df2efSAndy Whitcroft 1208773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1209773647a0SAndy Whitcroft 1210773647a0SAndy Whitcroftsub sanitise_line_reset { 1211773647a0SAndy Whitcroft my ($in_comment) = @_; 1212773647a0SAndy Whitcroft 1213773647a0SAndy Whitcroft if ($in_comment) { 1214773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1215773647a0SAndy Whitcroft } else { 1216773647a0SAndy Whitcroft $sanitise_quote = ''; 1217773647a0SAndy Whitcroft } 1218773647a0SAndy Whitcroft} 121900df344fSAndy Whitcroftsub sanitise_line { 122000df344fSAndy Whitcroft my ($line) = @_; 122100df344fSAndy Whitcroft 122200df344fSAndy Whitcroft my $res = ''; 122300df344fSAndy Whitcroft my $l = ''; 122400df344fSAndy Whitcroft 1225c2fdda0dSAndy Whitcroft my $qlen = 0; 1226773647a0SAndy Whitcroft my $off = 0; 1227773647a0SAndy Whitcroft my $c; 122800df344fSAndy Whitcroft 1229773647a0SAndy Whitcroft # Always copy over the diff marker. 1230773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1231773647a0SAndy Whitcroft 1232773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1233773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1234773647a0SAndy Whitcroft 12358d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1236773647a0SAndy Whitcroft # and end, all to $;. 1237773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1238773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1239773647a0SAndy Whitcroft 1240773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1241773647a0SAndy Whitcroft $off++; 124200df344fSAndy Whitcroft next; 1243773647a0SAndy Whitcroft } 124481bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1245773647a0SAndy Whitcroft $sanitise_quote = ''; 1246773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1247773647a0SAndy Whitcroft $off++; 1248773647a0SAndy Whitcroft next; 1249773647a0SAndy Whitcroft } 1250113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1251113f04a8SDaniel Walker $sanitise_quote = '//'; 1252113f04a8SDaniel Walker 1253113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1254113f04a8SDaniel Walker $off++; 1255113f04a8SDaniel Walker next; 1256113f04a8SDaniel Walker } 1257773647a0SAndy Whitcroft 1258773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1259773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1260773647a0SAndy Whitcroft $c eq "\\") { 1261773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1262773647a0SAndy Whitcroft $off++; 1263773647a0SAndy Whitcroft next; 1264773647a0SAndy Whitcroft } 1265773647a0SAndy Whitcroft # Regular quotes. 1266773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1267773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1268773647a0SAndy Whitcroft $sanitise_quote = $c; 1269773647a0SAndy Whitcroft 1270773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1271773647a0SAndy Whitcroft next; 1272773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1273773647a0SAndy Whitcroft $sanitise_quote = ''; 127400df344fSAndy Whitcroft } 127500df344fSAndy Whitcroft } 1276773647a0SAndy Whitcroft 1277fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1278773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1279773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1280113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1281113f04a8SDaniel Walker substr($res, $off, 1, $;); 1282773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1283773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 128400df344fSAndy Whitcroft } else { 1285773647a0SAndy Whitcroft substr($res, $off, 1, $c); 128600df344fSAndy Whitcroft } 1287c2fdda0dSAndy Whitcroft } 1288c2fdda0dSAndy Whitcroft 1289113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1290113f04a8SDaniel Walker $sanitise_quote = ''; 1291113f04a8SDaniel Walker } 1292113f04a8SDaniel Walker 1293c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1294c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1295c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1296c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1297c2fdda0dSAndy Whitcroft 1298c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1299c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1300c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1301c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1302c2fdda0dSAndy Whitcroft } 1303c2fdda0dSAndy Whitcroft 1304dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1305dadf680dSJoe Perches my $match = $1; 1306dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1307dadf680dSJoe Perches } 1308dadf680dSJoe Perches 130900df344fSAndy Whitcroft return $res; 131000df344fSAndy Whitcroft} 131100df344fSAndy Whitcroft 1312a6962d72SJoe Perchessub get_quoted_string { 1313a6962d72SJoe Perches my ($line, $rawline) = @_; 1314a6962d72SJoe Perches 1315478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 131633acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1317a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1318a6962d72SJoe Perches} 1319a6962d72SJoe Perches 13208905a67cSAndy Whitcroftsub ctx_statement_block { 13218905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 13228905a67cSAndy Whitcroft my $line = $linenr - 1; 13238905a67cSAndy Whitcroft my $blk = ''; 13248905a67cSAndy Whitcroft my $soff = $off; 13258905a67cSAndy Whitcroft my $coff = $off - 1; 1326773647a0SAndy Whitcroft my $coff_set = 0; 13278905a67cSAndy Whitcroft 132813214adfSAndy Whitcroft my $loff = 0; 132913214adfSAndy Whitcroft 13308905a67cSAndy Whitcroft my $type = ''; 13318905a67cSAndy Whitcroft my $level = 0; 1332a2750645SAndy Whitcroft my @stack = (); 1333cf655043SAndy Whitcroft my $p; 13348905a67cSAndy Whitcroft my $c; 13358905a67cSAndy Whitcroft my $len = 0; 133613214adfSAndy Whitcroft 133713214adfSAndy Whitcroft my $remainder; 13388905a67cSAndy Whitcroft while (1) { 1339a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1340a2750645SAndy Whitcroft 1341773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 13428905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 13438905a67cSAndy Whitcroft # context. 13448905a67cSAndy Whitcroft if ($off >= $len) { 13458905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1346dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1347c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 13488905a67cSAndy Whitcroft $remain--; 134913214adfSAndy Whitcroft $loff = $len; 1350c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 13518905a67cSAndy Whitcroft $len = length($blk); 13528905a67cSAndy Whitcroft $line++; 13538905a67cSAndy Whitcroft last; 13548905a67cSAndy Whitcroft } 13558905a67cSAndy Whitcroft # Bail if there is no further context. 13568905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 135713214adfSAndy Whitcroft if ($off >= $len) { 13588905a67cSAndy Whitcroft last; 13598905a67cSAndy Whitcroft } 1360f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1361f74bd194SAndy Whitcroft $level++; 1362f74bd194SAndy Whitcroft $type = '#'; 1363f74bd194SAndy Whitcroft } 13648905a67cSAndy Whitcroft } 1365cf655043SAndy Whitcroft $p = $c; 13668905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 136713214adfSAndy Whitcroft $remainder = substr($blk, $off); 13688905a67cSAndy Whitcroft 1369773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 13704635f4fbSAndy Whitcroft 13714635f4fbSAndy Whitcroft # Handle nested #if/#else. 13724635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 13734635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 13744635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 13754635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 13764635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 13774635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 13784635f4fbSAndy Whitcroft } 13794635f4fbSAndy Whitcroft 13808905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 13818905a67cSAndy Whitcroft # outermost level. 13828905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 13838905a67cSAndy Whitcroft last; 13848905a67cSAndy Whitcroft } 13858905a67cSAndy Whitcroft 138613214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1387773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1388773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1389773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1390773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1391773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1392773647a0SAndy Whitcroft $coff_set = 1; 1393773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1394773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 139513214adfSAndy Whitcroft } 139613214adfSAndy Whitcroft 13978905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 13988905a67cSAndy Whitcroft $level++; 13998905a67cSAndy Whitcroft $type = '('; 14008905a67cSAndy Whitcroft } 14018905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 14028905a67cSAndy Whitcroft $level--; 14038905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 14048905a67cSAndy Whitcroft 14058905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 14068905a67cSAndy Whitcroft $coff = $off; 1407773647a0SAndy Whitcroft $coff_set = 1; 1408773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 14098905a67cSAndy Whitcroft } 14108905a67cSAndy Whitcroft } 14118905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 14128905a67cSAndy Whitcroft $level++; 14138905a67cSAndy Whitcroft $type = '{'; 14148905a67cSAndy Whitcroft } 14158905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 14168905a67cSAndy Whitcroft $level--; 14178905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 14188905a67cSAndy Whitcroft 14198905a67cSAndy Whitcroft if ($level == 0) { 1420b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1421b998e001SPatrick Pannuto $off++; 1422b998e001SPatrick Pannuto } 14238905a67cSAndy Whitcroft last; 14248905a67cSAndy Whitcroft } 14258905a67cSAndy Whitcroft } 1426f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1427f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1428f74bd194SAndy Whitcroft $level--; 1429f74bd194SAndy Whitcroft $type = ''; 1430f74bd194SAndy Whitcroft $off++; 1431f74bd194SAndy Whitcroft last; 1432f74bd194SAndy Whitcroft } 14338905a67cSAndy Whitcroft $off++; 14348905a67cSAndy Whitcroft } 1435a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 143613214adfSAndy Whitcroft if ($off == $len) { 1437a3bb97a7SAndy Whitcroft $loff = $len + 1; 143813214adfSAndy Whitcroft $line++; 143913214adfSAndy Whitcroft $remain--; 144013214adfSAndy Whitcroft } 14418905a67cSAndy Whitcroft 14428905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 14438905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 14448905a67cSAndy Whitcroft 14458905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 14468905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 14478905a67cSAndy Whitcroft 1448773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 144913214adfSAndy Whitcroft 145013214adfSAndy Whitcroft return ($statement, $condition, 145113214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 145213214adfSAndy Whitcroft} 145313214adfSAndy Whitcroft 1454cf655043SAndy Whitcroftsub statement_lines { 1455cf655043SAndy Whitcroft my ($stmt) = @_; 1456cf655043SAndy Whitcroft 1457cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1458cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1459cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1460cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1461cf655043SAndy Whitcroft 1462cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1463cf655043SAndy Whitcroft 1464cf655043SAndy Whitcroft return $#stmt_lines + 2; 1465cf655043SAndy Whitcroft} 1466cf655043SAndy Whitcroft 1467cf655043SAndy Whitcroftsub statement_rawlines { 1468cf655043SAndy Whitcroft my ($stmt) = @_; 1469cf655043SAndy Whitcroft 1470cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1471cf655043SAndy Whitcroft 1472cf655043SAndy Whitcroft return $#stmt_lines + 2; 1473cf655043SAndy Whitcroft} 1474cf655043SAndy Whitcroft 1475cf655043SAndy Whitcroftsub statement_block_size { 1476cf655043SAndy Whitcroft my ($stmt) = @_; 1477cf655043SAndy Whitcroft 1478cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1479cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1480cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1481cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1482cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1483cf655043SAndy Whitcroft 1484cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1485cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1486cf655043SAndy Whitcroft 1487cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1488cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1489cf655043SAndy Whitcroft 1490cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1491cf655043SAndy Whitcroft return $stmt_lines; 1492cf655043SAndy Whitcroft } else { 1493cf655043SAndy Whitcroft return $stmt_statements; 1494cf655043SAndy Whitcroft } 1495cf655043SAndy Whitcroft} 1496cf655043SAndy Whitcroft 149713214adfSAndy Whitcroftsub ctx_statement_full { 149813214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 149913214adfSAndy Whitcroft my ($statement, $condition, $level); 150013214adfSAndy Whitcroft 150113214adfSAndy Whitcroft my (@chunks); 150213214adfSAndy Whitcroft 1503cf655043SAndy Whitcroft # Grab the first conditional/block pair. 150413214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 150513214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1506773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 150713214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1508cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1509cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1510cf655043SAndy Whitcroft } 1511cf655043SAndy Whitcroft 1512cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1513cf655043SAndy Whitcroft # could continue the statement. 1514cf655043SAndy Whitcroft for (;;) { 151513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 151613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1517cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1518773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1519cf655043SAndy Whitcroft #print "C: push\n"; 1520cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 152113214adfSAndy Whitcroft } 152213214adfSAndy Whitcroft 152313214adfSAndy Whitcroft return ($level, $linenr, @chunks); 15248905a67cSAndy Whitcroft} 15258905a67cSAndy Whitcroft 15264a0df2efSAndy Whitcroftsub ctx_block_get { 1527f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 15284a0df2efSAndy Whitcroft my $line; 15294a0df2efSAndy Whitcroft my $start = $linenr - 1; 15304a0df2efSAndy Whitcroft my $blk = ''; 15314a0df2efSAndy Whitcroft my @o; 15324a0df2efSAndy Whitcroft my @c; 15334a0df2efSAndy Whitcroft my @res = (); 15344a0df2efSAndy Whitcroft 1535f0a594c1SAndy Whitcroft my $level = 0; 15364635f4fbSAndy Whitcroft my @stack = ($level); 153700df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 153800df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 153900df344fSAndy Whitcroft $remain--; 154000df344fSAndy Whitcroft 154100df344fSAndy Whitcroft $blk .= $rawlines[$line]; 15424635f4fbSAndy Whitcroft 15434635f4fbSAndy Whitcroft # Handle nested #if/#else. 154401464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 15454635f4fbSAndy Whitcroft push(@stack, $level); 154601464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 15474635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 154801464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 15494635f4fbSAndy Whitcroft $level = pop(@stack); 15504635f4fbSAndy Whitcroft } 15514635f4fbSAndy Whitcroft 155201464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1553f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1554f0a594c1SAndy Whitcroft if ($off > 0) { 1555f0a594c1SAndy Whitcroft $off--; 1556f0a594c1SAndy Whitcroft next; 1557f0a594c1SAndy Whitcroft } 15584a0df2efSAndy Whitcroft 1559f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1560f0a594c1SAndy Whitcroft $level--; 1561f0a594c1SAndy Whitcroft last if ($level == 0); 1562f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1563f0a594c1SAndy Whitcroft $level++; 1564f0a594c1SAndy Whitcroft } 1565f0a594c1SAndy Whitcroft } 15664a0df2efSAndy Whitcroft 1567f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 156800df344fSAndy Whitcroft push(@res, $rawlines[$line]); 15694a0df2efSAndy Whitcroft } 15704a0df2efSAndy Whitcroft 1571f0a594c1SAndy Whitcroft last if ($level == 0); 15724a0df2efSAndy Whitcroft } 15734a0df2efSAndy Whitcroft 1574f0a594c1SAndy Whitcroft return ($level, @res); 15754a0df2efSAndy Whitcroft} 15764a0df2efSAndy Whitcroftsub ctx_block_outer { 15774a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 15784a0df2efSAndy Whitcroft 1579f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1580f0a594c1SAndy Whitcroft return @r; 15814a0df2efSAndy Whitcroft} 15824a0df2efSAndy Whitcroftsub ctx_block { 15834a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 15844a0df2efSAndy Whitcroft 1585f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1586f0a594c1SAndy Whitcroft return @r; 1587653d4876SAndy Whitcroft} 1588653d4876SAndy Whitcroftsub ctx_statement { 1589f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1590f0a594c1SAndy Whitcroft 1591f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1592f0a594c1SAndy Whitcroft return @r; 1593f0a594c1SAndy Whitcroft} 1594f0a594c1SAndy Whitcroftsub ctx_block_level { 1595653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1596653d4876SAndy Whitcroft 1597f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 15984a0df2efSAndy Whitcroft} 15999c0ca6f9SAndy Whitcroftsub ctx_statement_level { 16009c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 16019c0ca6f9SAndy Whitcroft 16029c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 16039c0ca6f9SAndy Whitcroft} 16044a0df2efSAndy Whitcroft 16054a0df2efSAndy Whitcroftsub ctx_locate_comment { 16064a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16074a0df2efSAndy Whitcroft 16084a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1609beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 16104a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 16114a0df2efSAndy Whitcroft 16124a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 16134a0df2efSAndy Whitcroft # comment. 16144a0df2efSAndy Whitcroft my $in_comment = 0; 16154a0df2efSAndy Whitcroft $current_comment = ''; 16164a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 161700df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 161800df344fSAndy Whitcroft #warn " $line\n"; 16194a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 16204a0df2efSAndy Whitcroft $in_comment = 1; 16214a0df2efSAndy Whitcroft } 16224a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 16234a0df2efSAndy Whitcroft $in_comment = 1; 16244a0df2efSAndy Whitcroft } 16254a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 16264a0df2efSAndy Whitcroft $current_comment = ''; 16274a0df2efSAndy Whitcroft } 16284a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 16294a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 16304a0df2efSAndy Whitcroft $in_comment = 0; 16314a0df2efSAndy Whitcroft } 16324a0df2efSAndy Whitcroft } 16334a0df2efSAndy Whitcroft 16344a0df2efSAndy Whitcroft chomp($current_comment); 16354a0df2efSAndy Whitcroft return($current_comment); 16364a0df2efSAndy Whitcroft} 16374a0df2efSAndy Whitcroftsub ctx_has_comment { 16384a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16394a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 16404a0df2efSAndy Whitcroft 164100df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 16424a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 16434a0df2efSAndy Whitcroft 16444a0df2efSAndy Whitcroft return ($cmt ne ''); 16454a0df2efSAndy Whitcroft} 16464a0df2efSAndy Whitcroft 16474d001e4dSAndy Whitcroftsub raw_line { 16484d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 16494d001e4dSAndy Whitcroft 16504d001e4dSAndy Whitcroft my $offset = $linenr - 1; 16514d001e4dSAndy Whitcroft $cnt++; 16524d001e4dSAndy Whitcroft 16534d001e4dSAndy Whitcroft my $line; 16544d001e4dSAndy Whitcroft while ($cnt) { 16554d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 16564d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 16574d001e4dSAndy Whitcroft $cnt--; 16584d001e4dSAndy Whitcroft } 16594d001e4dSAndy Whitcroft 16604d001e4dSAndy Whitcroft return $line; 16614d001e4dSAndy Whitcroft} 16624d001e4dSAndy Whitcroft 16632a9f9d85STobin C. Hardingsub get_stat_real { 16642a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 16652a9f9d85STobin C. Harding 16662a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 16672a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 16682a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 16692a9f9d85STobin C. Harding } 16702a9f9d85STobin C. Harding 16712a9f9d85STobin C. Harding return $stat_real; 16722a9f9d85STobin C. Harding} 16732a9f9d85STobin C. Harding 1674e3d95a2aSTobin C. Hardingsub get_stat_here { 1675e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1676e3d95a2aSTobin C. Harding 1677e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1678e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1679e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1680e3d95a2aSTobin C. Harding } 1681e3d95a2aSTobin C. Harding 1682e3d95a2aSTobin C. Harding return $herectx; 1683e3d95a2aSTobin C. Harding} 1684e3d95a2aSTobin C. Harding 16850a920b5bSAndy Whitcroftsub cat_vet { 16860a920b5bSAndy Whitcroft my ($vet) = @_; 16879c0ca6f9SAndy Whitcroft my ($res, $coded); 16880a920b5bSAndy Whitcroft 16899c0ca6f9SAndy Whitcroft $res = ''; 16906c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 16916c72ffaaSAndy Whitcroft $res .= $1; 16926c72ffaaSAndy Whitcroft if ($2 ne '') { 16939c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 16946c72ffaaSAndy Whitcroft $res .= $coded; 16956c72ffaaSAndy Whitcroft } 16969c0ca6f9SAndy Whitcroft } 16979c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 16980a920b5bSAndy Whitcroft 16999c0ca6f9SAndy Whitcroft return $res; 17000a920b5bSAndy Whitcroft} 17010a920b5bSAndy Whitcroft 1702c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1703cf655043SAndy Whitcroftmy $av_pending; 1704c2fdda0dSAndy Whitcroftmy @av_paren_type; 17051f65f947SAndy Whitcroftmy $av_pend_colon; 1706c2fdda0dSAndy Whitcroft 1707c2fdda0dSAndy Whitcroftsub annotate_reset { 1708c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1709cf655043SAndy Whitcroft $av_pending = '_'; 1710cf655043SAndy Whitcroft @av_paren_type = ('E'); 17111f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1712c2fdda0dSAndy Whitcroft} 1713c2fdda0dSAndy Whitcroft 17146c72ffaaSAndy Whitcroftsub annotate_values { 17156c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 17166c72ffaaSAndy Whitcroft 17176c72ffaaSAndy Whitcroft my $res; 17181f65f947SAndy Whitcroft my $var = '_' x length($stream); 17196c72ffaaSAndy Whitcroft my $cur = $stream; 17206c72ffaaSAndy Whitcroft 1721c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 17226c72ffaaSAndy Whitcroft 17236c72ffaaSAndy Whitcroft while (length($cur)) { 1724773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1725cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1726171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 17276c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1728c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1729c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1730cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1731c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 17326c72ffaaSAndy Whitcroft } 17336c72ffaaSAndy Whitcroft 1734c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 17359446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 17369446ef56SAndy Whitcroft push(@av_paren_type, $type); 1737addcdceaSAndy Whitcroft $type = 'c'; 17389446ef56SAndy Whitcroft 1739e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1740c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 17416c72ffaaSAndy Whitcroft $type = 'T'; 17426c72ffaaSAndy Whitcroft 1743389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1744389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1745389a2fe5SAndy Whitcroft $type = 'T'; 1746389a2fe5SAndy Whitcroft 1747c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1748171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1749c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1750171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1751171ae1a4SAndy Whitcroft if ($2 ne '') { 1752cf655043SAndy Whitcroft $av_pending = 'N'; 1753171ae1a4SAndy Whitcroft } 1754171ae1a4SAndy Whitcroft $type = 'E'; 1755171ae1a4SAndy Whitcroft 1756c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1757171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1758171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1759171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 17606c72ffaaSAndy Whitcroft 1761c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1762cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1763c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1764cf655043SAndy Whitcroft 1765cf655043SAndy Whitcroft push(@av_paren_type, $type); 1766cf655043SAndy Whitcroft push(@av_paren_type, $type); 1767171ae1a4SAndy Whitcroft $type = 'E'; 1768cf655043SAndy Whitcroft 1769c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1770cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1771cf655043SAndy Whitcroft $av_preprocessor = 1; 1772cf655043SAndy Whitcroft 1773cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1774cf655043SAndy Whitcroft 1775171ae1a4SAndy Whitcroft $type = 'E'; 1776cf655043SAndy Whitcroft 1777c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1778cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1779cf655043SAndy Whitcroft 1780cf655043SAndy Whitcroft $av_preprocessor = 1; 1781cf655043SAndy Whitcroft 1782cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1783cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1784cf655043SAndy Whitcroft pop(@av_paren_type); 1785cf655043SAndy Whitcroft push(@av_paren_type, $type); 1786171ae1a4SAndy Whitcroft $type = 'E'; 17876c72ffaaSAndy Whitcroft 17886c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1789c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 17906c72ffaaSAndy Whitcroft 1791171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1792171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1793171ae1a4SAndy Whitcroft $av_pending = $type; 1794171ae1a4SAndy Whitcroft $type = 'N'; 1795171ae1a4SAndy Whitcroft 17966c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1797c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 17986c72ffaaSAndy Whitcroft if (defined $2) { 1799cf655043SAndy Whitcroft $av_pending = 'V'; 18006c72ffaaSAndy Whitcroft } 18016c72ffaaSAndy Whitcroft $type = 'N'; 18026c72ffaaSAndy Whitcroft 180314b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1804c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 180514b111c1SAndy Whitcroft $av_pending = 'E'; 18066c72ffaaSAndy Whitcroft $type = 'N'; 18076c72ffaaSAndy Whitcroft 18081f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 18091f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 18101f65f947SAndy Whitcroft $av_pend_colon = 'C'; 18111f65f947SAndy Whitcroft $type = 'N'; 18121f65f947SAndy Whitcroft 181314b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1814c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 18156c72ffaaSAndy Whitcroft $type = 'N'; 18166c72ffaaSAndy Whitcroft 18176c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1818c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1819cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1820cf655043SAndy Whitcroft $av_pending = '_'; 18216c72ffaaSAndy Whitcroft $type = 'N'; 18226c72ffaaSAndy Whitcroft 18236c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1824cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1825cf655043SAndy Whitcroft if ($new_type ne '_') { 1826cf655043SAndy Whitcroft $type = $new_type; 1827c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1828c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 18296c72ffaaSAndy Whitcroft } else { 1830c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 18316c72ffaaSAndy Whitcroft } 18326c72ffaaSAndy Whitcroft 1833c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1834c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1835c8cb2ca3SAndy Whitcroft $type = 'V'; 1836cf655043SAndy Whitcroft $av_pending = 'V'; 18376c72ffaaSAndy Whitcroft 18388e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 18398e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 18401f65f947SAndy Whitcroft $av_pend_colon = 'B'; 18418e761b04SAndy Whitcroft } elsif ($type eq 'E') { 18428e761b04SAndy Whitcroft $av_pend_colon = 'L'; 18431f65f947SAndy Whitcroft } 18441f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 18451f65f947SAndy Whitcroft $type = 'V'; 18461f65f947SAndy Whitcroft 18476c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1848c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 18496c72ffaaSAndy Whitcroft $type = 'V'; 18506c72ffaaSAndy Whitcroft 18516c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1852c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 18536c72ffaaSAndy Whitcroft $type = 'N'; 18546c72ffaaSAndy Whitcroft 1855cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1856c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 185713214adfSAndy Whitcroft $type = 'E'; 18581f65f947SAndy Whitcroft $av_pend_colon = 'O'; 185913214adfSAndy Whitcroft 18608e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 18618e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 18628e761b04SAndy Whitcroft $type = 'C'; 18638e761b04SAndy Whitcroft 18641f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 18651f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 18661f65f947SAndy Whitcroft $type = 'N'; 18671f65f947SAndy Whitcroft 18681f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 18691f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 18701f65f947SAndy Whitcroft 18711f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 18721f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 18731f65f947SAndy Whitcroft $type = 'E'; 18741f65f947SAndy Whitcroft } else { 18751f65f947SAndy Whitcroft $type = 'N'; 18761f65f947SAndy Whitcroft } 18771f65f947SAndy Whitcroft $av_pend_colon = 'O'; 18781f65f947SAndy Whitcroft 18798e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 188013214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 18816c72ffaaSAndy Whitcroft $type = 'N'; 18826c72ffaaSAndy Whitcroft 18830d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 188474048ed8SAndy Whitcroft my $variant; 188574048ed8SAndy Whitcroft 188674048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 188774048ed8SAndy Whitcroft if ($type eq 'V') { 188874048ed8SAndy Whitcroft $variant = 'B'; 188974048ed8SAndy Whitcroft } else { 189074048ed8SAndy Whitcroft $variant = 'U'; 189174048ed8SAndy Whitcroft } 189274048ed8SAndy Whitcroft 189374048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 189474048ed8SAndy Whitcroft $type = 'N'; 189574048ed8SAndy Whitcroft 18966c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1897c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 18986c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 18996c72ffaaSAndy Whitcroft $type = 'N'; 19006c72ffaaSAndy Whitcroft } 19016c72ffaaSAndy Whitcroft 19026c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1903c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 19046c72ffaaSAndy Whitcroft } 19056c72ffaaSAndy Whitcroft if (defined $1) { 19066c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 19076c72ffaaSAndy Whitcroft $res .= $type x length($1); 19086c72ffaaSAndy Whitcroft } 19096c72ffaaSAndy Whitcroft } 19106c72ffaaSAndy Whitcroft 19111f65f947SAndy Whitcroft return ($res, $var); 19126c72ffaaSAndy Whitcroft} 19136c72ffaaSAndy Whitcroft 19148905a67cSAndy Whitcroftsub possible { 191513214adfSAndy Whitcroft my ($possible, $line) = @_; 19169a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 19170776e594SAndy Whitcroft ^(?: 19180776e594SAndy Whitcroft $Modifier| 19190776e594SAndy Whitcroft $Storage| 19200776e594SAndy Whitcroft $Type| 19219a974fdbSAndy Whitcroft DEFINE_\S+ 19229a974fdbSAndy Whitcroft )$| 19239a974fdbSAndy Whitcroft ^(?: 19240776e594SAndy Whitcroft goto| 19250776e594SAndy Whitcroft return| 19260776e594SAndy Whitcroft case| 19270776e594SAndy Whitcroft else| 19280776e594SAndy Whitcroft asm|__asm__| 192989a88353SAndy Whitcroft do| 193089a88353SAndy Whitcroft \#| 193189a88353SAndy Whitcroft \#\#| 19329a974fdbSAndy Whitcroft )(?:\s|$)| 19330776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 19349a974fdbSAndy Whitcroft )}x; 19359a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 19369a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1937c45dcabdSAndy Whitcroft # Check for modifiers. 1938c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1939c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1940c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1941c45dcabdSAndy Whitcroft 1942c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1943c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1944d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 19459a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1946d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1947485ff23eSAlex Dowad push(@modifierListFile, $modifier); 1948d2506586SAndy Whitcroft } 19499a974fdbSAndy Whitcroft } 1950c45dcabdSAndy Whitcroft 1951c45dcabdSAndy Whitcroft } else { 195213214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1953485ff23eSAlex Dowad push(@typeListFile, $possible); 1954c45dcabdSAndy Whitcroft } 19558905a67cSAndy Whitcroft build_types(); 19560776e594SAndy Whitcroft } else { 19570776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 19588905a67cSAndy Whitcroft } 19598905a67cSAndy Whitcroft} 19608905a67cSAndy Whitcroft 19616c72ffaaSAndy Whitcroftmy $prefix = ''; 19626c72ffaaSAndy Whitcroft 1963000d1cc1SJoe Perchessub show_type { 1964cbec18afSJoe Perches my ($type) = @_; 196591bfe484SJoe Perches 1966522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 1967522b837cSAlexey Dobriyan 1968cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1969cbec18afSJoe Perches 1970cbec18afSJoe Perches return !defined $ignore_type{$type}; 1971000d1cc1SJoe Perches} 1972000d1cc1SJoe Perches 1973f0a594c1SAndy Whitcroftsub report { 1974cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1975cbec18afSJoe Perches 1976cbec18afSJoe Perches if (!show_type($type) || 1977cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1978773647a0SAndy Whitcroft return 0; 1979773647a0SAndy Whitcroft } 198057230297SJoe Perches my $output = ''; 1981737c0767SJohn Brooks if ($color) { 198257230297SJoe Perches if ($level eq 'ERROR') { 198357230297SJoe Perches $output .= RED; 198457230297SJoe Perches } elsif ($level eq 'WARNING') { 198557230297SJoe Perches $output .= YELLOW; 1986000d1cc1SJoe Perches } else { 198757230297SJoe Perches $output .= GREEN; 1988000d1cc1SJoe Perches } 198957230297SJoe Perches } 199057230297SJoe Perches $output .= $prefix . $level . ':'; 199157230297SJoe Perches if ($show_types) { 1992737c0767SJohn Brooks $output .= BLUE if ($color); 199357230297SJoe Perches $output .= "$type:"; 199457230297SJoe Perches } 1995737c0767SJohn Brooks $output .= RESET if ($color); 199657230297SJoe Perches $output .= ' ' . $msg . "\n"; 199734d8815fSJoe Perches 199834d8815fSJoe Perches if ($showfile) { 199934d8815fSJoe Perches my @lines = split("\n", $output, -1); 200034d8815fSJoe Perches splice(@lines, 1, 1); 200134d8815fSJoe Perches $output = join("\n", @lines); 200234d8815fSJoe Perches } 200357230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 20048905a67cSAndy Whitcroft 200557230297SJoe Perches push(our @report, $output); 2006773647a0SAndy Whitcroft 2007773647a0SAndy Whitcroft return 1; 2008f0a594c1SAndy Whitcroft} 2009cbec18afSJoe Perches 2010f0a594c1SAndy Whitcroftsub report_dump { 201113214adfSAndy Whitcroft our @report; 2012f0a594c1SAndy Whitcroft} 2013000d1cc1SJoe Perches 2014d752fcc8SJoe Perchessub fixup_current_range { 2015d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2016d752fcc8SJoe Perches 2017d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2018d752fcc8SJoe Perches my $o = $1; 2019d752fcc8SJoe Perches my $l = $2; 2020d752fcc8SJoe Perches my $no = $o + $offset; 2021d752fcc8SJoe Perches my $nl = $l + $length; 2022d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2023d752fcc8SJoe Perches } 2024d752fcc8SJoe Perches} 2025d752fcc8SJoe Perches 2026d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2027d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2028d752fcc8SJoe Perches 2029d752fcc8SJoe Perches my $range_last_linenr = 0; 2030d752fcc8SJoe Perches my $delta_offset = 0; 2031d752fcc8SJoe Perches 2032d752fcc8SJoe Perches my $old_linenr = 0; 2033d752fcc8SJoe Perches my $new_linenr = 0; 2034d752fcc8SJoe Perches 2035d752fcc8SJoe Perches my $next_insert = 0; 2036d752fcc8SJoe Perches my $next_delete = 0; 2037d752fcc8SJoe Perches 2038d752fcc8SJoe Perches my @lines = (); 2039d752fcc8SJoe Perches 2040d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2041d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2042d752fcc8SJoe Perches 2043d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2044d752fcc8SJoe Perches my $save_line = 1; 2045d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2046323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2047d752fcc8SJoe Perches $delta_offset = 0; 2048d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2049d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2050d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2051d752fcc8SJoe Perches } 2052d752fcc8SJoe Perches 2053d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2054d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2055d752fcc8SJoe Perches $save_line = 0; 2056d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2057d752fcc8SJoe Perches } 2058d752fcc8SJoe Perches 2059d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2060d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2061d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2062d752fcc8SJoe Perches $new_linenr++; 2063d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2064d752fcc8SJoe Perches } 2065d752fcc8SJoe Perches 2066d752fcc8SJoe Perches if ($save_line) { 2067d752fcc8SJoe Perches push(@lines, $line); 2068d752fcc8SJoe Perches $new_linenr++; 2069d752fcc8SJoe Perches } 2070d752fcc8SJoe Perches 2071d752fcc8SJoe Perches $old_linenr++; 2072d752fcc8SJoe Perches } 2073d752fcc8SJoe Perches 2074d752fcc8SJoe Perches return @lines; 2075d752fcc8SJoe Perches} 2076d752fcc8SJoe Perches 2077f2d7e4d4SJoe Perchessub fix_insert_line { 2078f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2079f2d7e4d4SJoe Perches 2080f2d7e4d4SJoe Perches my $inserted = { 2081f2d7e4d4SJoe Perches LINENR => $linenr, 2082f2d7e4d4SJoe Perches LINE => $line, 2083f2d7e4d4SJoe Perches }; 2084f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2085f2d7e4d4SJoe Perches} 2086f2d7e4d4SJoe Perches 2087f2d7e4d4SJoe Perchessub fix_delete_line { 2088f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2089f2d7e4d4SJoe Perches 2090f2d7e4d4SJoe Perches my $deleted = { 2091f2d7e4d4SJoe Perches LINENR => $linenr, 2092f2d7e4d4SJoe Perches LINE => $line, 2093f2d7e4d4SJoe Perches }; 2094f2d7e4d4SJoe Perches 2095f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2096f2d7e4d4SJoe Perches} 2097f2d7e4d4SJoe Perches 2098de7d4f0eSAndy Whitcroftsub ERROR { 2099cbec18afSJoe Perches my ($type, $msg) = @_; 2100cbec18afSJoe Perches 2101cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2102de7d4f0eSAndy Whitcroft our $clean = 0; 21036c72ffaaSAndy Whitcroft our $cnt_error++; 21043705ce5bSJoe Perches return 1; 2105de7d4f0eSAndy Whitcroft } 21063705ce5bSJoe Perches return 0; 2107773647a0SAndy Whitcroft} 2108de7d4f0eSAndy Whitcroftsub WARN { 2109cbec18afSJoe Perches my ($type, $msg) = @_; 2110cbec18afSJoe Perches 2111cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2112de7d4f0eSAndy Whitcroft our $clean = 0; 21136c72ffaaSAndy Whitcroft our $cnt_warn++; 21143705ce5bSJoe Perches return 1; 2115de7d4f0eSAndy Whitcroft } 21163705ce5bSJoe Perches return 0; 2117773647a0SAndy Whitcroft} 2118de7d4f0eSAndy Whitcroftsub CHK { 2119cbec18afSJoe Perches my ($type, $msg) = @_; 2120cbec18afSJoe Perches 2121cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2122de7d4f0eSAndy Whitcroft our $clean = 0; 21236c72ffaaSAndy Whitcroft our $cnt_chk++; 21243705ce5bSJoe Perches return 1; 21256c72ffaaSAndy Whitcroft } 21263705ce5bSJoe Perches return 0; 2127de7d4f0eSAndy Whitcroft} 2128de7d4f0eSAndy Whitcroft 21296ecd9674SAndy Whitcroftsub check_absolute_file { 21306ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 21316ecd9674SAndy Whitcroft my $file = $absolute; 21326ecd9674SAndy Whitcroft 21336ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 21346ecd9674SAndy Whitcroft 21356ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 21366ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 21376ecd9674SAndy Whitcroft if (-f "$root/$file") { 21386ecd9674SAndy Whitcroft ##print "file<$file>\n"; 21396ecd9674SAndy Whitcroft last; 21406ecd9674SAndy Whitcroft } 21416ecd9674SAndy Whitcroft } 21426ecd9674SAndy Whitcroft if (! -f _) { 21436ecd9674SAndy Whitcroft return 0; 21446ecd9674SAndy Whitcroft } 21456ecd9674SAndy Whitcroft 21466ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 21476ecd9674SAndy Whitcroft my $prefix = $absolute; 21486ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 21496ecd9674SAndy Whitcroft 21506ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 21516ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2152000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2153000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 21546ecd9674SAndy Whitcroft } 21556ecd9674SAndy Whitcroft} 21566ecd9674SAndy Whitcroft 21573705ce5bSJoe Perchessub trim { 21583705ce5bSJoe Perches my ($string) = @_; 21593705ce5bSJoe Perches 2160b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2161b34c648bSJoe Perches 2162b34c648bSJoe Perches return $string; 2163b34c648bSJoe Perches} 2164b34c648bSJoe Perches 2165b34c648bSJoe Perchessub ltrim { 2166b34c648bSJoe Perches my ($string) = @_; 2167b34c648bSJoe Perches 2168b34c648bSJoe Perches $string =~ s/^\s+//; 2169b34c648bSJoe Perches 2170b34c648bSJoe Perches return $string; 2171b34c648bSJoe Perches} 2172b34c648bSJoe Perches 2173b34c648bSJoe Perchessub rtrim { 2174b34c648bSJoe Perches my ($string) = @_; 2175b34c648bSJoe Perches 2176b34c648bSJoe Perches $string =~ s/\s+$//; 21773705ce5bSJoe Perches 21783705ce5bSJoe Perches return $string; 21793705ce5bSJoe Perches} 21803705ce5bSJoe Perches 218152ea8506SJoe Perchessub string_find_replace { 218252ea8506SJoe Perches my ($string, $find, $replace) = @_; 218352ea8506SJoe Perches 218452ea8506SJoe Perches $string =~ s/$find/$replace/g; 218552ea8506SJoe Perches 218652ea8506SJoe Perches return $string; 218752ea8506SJoe Perches} 218852ea8506SJoe Perches 21893705ce5bSJoe Perchessub tabify { 21903705ce5bSJoe Perches my ($leading) = @_; 21913705ce5bSJoe Perches 21923705ce5bSJoe Perches my $source_indent = 8; 21933705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 21943705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 21953705ce5bSJoe Perches 21963705ce5bSJoe Perches #convert leading spaces to tabs 21973705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 21983705ce5bSJoe Perches #Remove spaces before a tab 21993705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 22003705ce5bSJoe Perches 22013705ce5bSJoe Perches return "$leading"; 22023705ce5bSJoe Perches} 22033705ce5bSJoe Perches 2204d1fe9c09SJoe Perchessub pos_last_openparen { 2205d1fe9c09SJoe Perches my ($line) = @_; 2206d1fe9c09SJoe Perches 2207d1fe9c09SJoe Perches my $pos = 0; 2208d1fe9c09SJoe Perches 2209d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2210d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2211d1fe9c09SJoe Perches 2212d1fe9c09SJoe Perches my $last_openparen = 0; 2213d1fe9c09SJoe Perches 2214d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2215d1fe9c09SJoe Perches return -1; 2216d1fe9c09SJoe Perches } 2217d1fe9c09SJoe Perches 2218d1fe9c09SJoe Perches my $len = length($line); 2219d1fe9c09SJoe Perches 2220d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2221d1fe9c09SJoe Perches my $string = substr($line, $pos); 2222d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2223d1fe9c09SJoe Perches $pos += length($1) - 1; 2224d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2225d1fe9c09SJoe Perches $last_openparen = $pos; 2226d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2227d1fe9c09SJoe Perches last; 2228d1fe9c09SJoe Perches } 2229d1fe9c09SJoe Perches } 2230d1fe9c09SJoe Perches 223191cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2232d1fe9c09SJoe Perches} 2233d1fe9c09SJoe Perches 22340a920b5bSAndy Whitcroftsub process { 22350a920b5bSAndy Whitcroft my $filename = shift; 22360a920b5bSAndy Whitcroft 22370a920b5bSAndy Whitcroft my $linenr=0; 22380a920b5bSAndy Whitcroft my $prevline=""; 2239c2fdda0dSAndy Whitcroft my $prevrawline=""; 22400a920b5bSAndy Whitcroft my $stashline=""; 2241c2fdda0dSAndy Whitcroft my $stashrawline=""; 22420a920b5bSAndy Whitcroft 22434a0df2efSAndy Whitcroft my $length; 22440a920b5bSAndy Whitcroft my $indent; 22450a920b5bSAndy Whitcroft my $previndent=0; 22460a920b5bSAndy Whitcroft my $stashindent=0; 22470a920b5bSAndy Whitcroft 2248de7d4f0eSAndy Whitcroft our $clean = 1; 22490a920b5bSAndy Whitcroft my $signoff = 0; 2250cd261496SGeert Uytterhoeven my $author = ''; 2251cd261496SGeert Uytterhoeven my $authorsignoff = 0; 22520a920b5bSAndy Whitcroft my $is_patch = 0; 225329ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 225415662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 2255ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2256*490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2257bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 22582a076f40SJoe Perches my $commit_log_long_line = 0; 2259e518e9a5SJoe Perches my $commit_log_has_diff = 0; 226013f1937eSJoe Perches my $reported_maintainer_file = 0; 2261fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2262fa64205dSPasi Savanainen 2263365dd4eaSJoe Perches my $last_blank_line = 0; 22645e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2265365dd4eaSJoe Perches 226613214adfSAndy Whitcroft our @report = (); 22676c72ffaaSAndy Whitcroft our $cnt_lines = 0; 22686c72ffaaSAndy Whitcroft our $cnt_error = 0; 22696c72ffaaSAndy Whitcroft our $cnt_warn = 0; 22706c72ffaaSAndy Whitcroft our $cnt_chk = 0; 22716c72ffaaSAndy Whitcroft 22720a920b5bSAndy Whitcroft # Trace the real file/line as we go. 22730a920b5bSAndy Whitcroft my $realfile = ''; 22740a920b5bSAndy Whitcroft my $realline = 0; 22750a920b5bSAndy Whitcroft my $realcnt = 0; 22760a920b5bSAndy Whitcroft my $here = ''; 227777cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 22780a920b5bSAndy Whitcroft my $in_comment = 0; 2279c2fdda0dSAndy Whitcroft my $comment_edge = 0; 22800a920b5bSAndy Whitcroft my $first_line = 0; 22811e855726SWolfram Sang my $p1_prefix = ''; 22820a920b5bSAndy Whitcroft 228313214adfSAndy Whitcroft my $prev_values = 'E'; 228413214adfSAndy Whitcroft 228513214adfSAndy Whitcroft # suppression flags 2286773647a0SAndy Whitcroft my %suppress_ifbraces; 2287170d3a22SAndy Whitcroft my %suppress_whiletrailers; 22882b474a1aSAndy Whitcroft my %suppress_export; 22893e469cdcSAndy Whitcroft my $suppress_statement = 0; 2290653d4876SAndy Whitcroft 22917e51f197SJoe Perches my %signatures = (); 2292323c1260SJoe Perches 2293c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2294de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2295c2fdda0dSAndy Whitcroft # 2296de7d4f0eSAndy Whitcroft my @setup_docs = (); 2297de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2298773647a0SAndy Whitcroft 2299d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2300d8b07710SJoe Perches 23019f3a8992SRob Herring my $checklicenseline = 1; 23029f3a8992SRob Herring 2303773647a0SAndy Whitcroft sanitise_line_reset(); 2304c2fdda0dSAndy Whitcroft my $line; 2305c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2306773647a0SAndy Whitcroft $linenr++; 2307773647a0SAndy Whitcroft $line = $rawline; 2308c2fdda0dSAndy Whitcroft 23093705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 23103705ce5bSJoe Perches 2311773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2312de7d4f0eSAndy Whitcroft $setup_docs = 0; 23138c27ceffSMauro Carvalho Chehab if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { 2314de7d4f0eSAndy Whitcroft $setup_docs = 1; 2315de7d4f0eSAndy Whitcroft } 2316773647a0SAndy Whitcroft #next; 2317de7d4f0eSAndy Whitcroft } 231874fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2319773647a0SAndy Whitcroft $realline=$1-1; 2320773647a0SAndy Whitcroft if (defined $2) { 2321773647a0SAndy Whitcroft $realcnt=$3+1; 2322773647a0SAndy Whitcroft } else { 2323773647a0SAndy Whitcroft $realcnt=1+1; 2324773647a0SAndy Whitcroft } 2325c45dcabdSAndy Whitcroft $in_comment = 0; 2326773647a0SAndy Whitcroft 2327773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2328773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2329773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2330773647a0SAndy Whitcroft # at context start. 2331773647a0SAndy Whitcroft my $edge; 233201fa9147SAndy Whitcroft my $cnt = $realcnt; 233301fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 233401fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 233501fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 233601fa9147SAndy Whitcroft $cnt--; 233701fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2338721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2339fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2340fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2341fae17daeSAndy Whitcroft ($edge) = $1; 2342fae17daeSAndy Whitcroft last; 2343fae17daeSAndy Whitcroft } 2344773647a0SAndy Whitcroft } 2345773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2346773647a0SAndy Whitcroft $in_comment = 1; 2347773647a0SAndy Whitcroft } 2348773647a0SAndy Whitcroft 2349773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2350773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2351773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2352773647a0SAndy Whitcroft if (!defined $edge && 235383242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2354773647a0SAndy Whitcroft { 2355773647a0SAndy Whitcroft $in_comment = 1; 2356773647a0SAndy Whitcroft } 2357773647a0SAndy Whitcroft 2358773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2359773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2360773647a0SAndy Whitcroft 2361171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2362773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2363171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2364773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2365773647a0SAndy Whitcroft } 2366773647a0SAndy Whitcroft push(@lines, $line); 2367773647a0SAndy Whitcroft 2368773647a0SAndy Whitcroft if ($realcnt > 1) { 2369773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2370773647a0SAndy Whitcroft } else { 2371773647a0SAndy Whitcroft $realcnt = 0; 2372773647a0SAndy Whitcroft } 2373773647a0SAndy Whitcroft 2374773647a0SAndy Whitcroft #print "==>$rawline\n"; 2375773647a0SAndy Whitcroft #print "-->$line\n"; 2376de7d4f0eSAndy Whitcroft 2377de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2378de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2379de7d4f0eSAndy Whitcroft } 2380de7d4f0eSAndy Whitcroft } 2381de7d4f0eSAndy Whitcroft 23826c72ffaaSAndy Whitcroft $prefix = ''; 23836c72ffaaSAndy Whitcroft 2384773647a0SAndy Whitcroft $realcnt = 0; 2385773647a0SAndy Whitcroft $linenr = 0; 2386194f66fcSJoe Perches $fixlinenr = -1; 23870a920b5bSAndy Whitcroft foreach my $line (@lines) { 23880a920b5bSAndy Whitcroft $linenr++; 2389194f66fcSJoe Perches $fixlinenr++; 23901b5539b1SJoe Perches my $sline = $line; #copy of $line 23911b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 23920a920b5bSAndy Whitcroft 2393c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 23946c72ffaaSAndy Whitcroft 239512c253abSJoe Perches# check if it's a mode change, rename or start of a patch 239612c253abSJoe Perches if (!$in_commit_log && 239712c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 239812c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 239912c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 240012c253abSJoe Perches $is_patch = 1; 240112c253abSJoe Perches } 240212c253abSJoe Perches 24030a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2404e518e9a5SJoe Perches if (!$in_commit_log && 240574fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 240674fd4f34SJoe Perches my $context = $4; 24070a920b5bSAndy Whitcroft $is_patch = 1; 24084a0df2efSAndy Whitcroft $first_line = $linenr + 1; 24090a920b5bSAndy Whitcroft $realline=$1-1; 24100a920b5bSAndy Whitcroft if (defined $2) { 24110a920b5bSAndy Whitcroft $realcnt=$3+1; 24120a920b5bSAndy Whitcroft } else { 24130a920b5bSAndy Whitcroft $realcnt=1+1; 24140a920b5bSAndy Whitcroft } 2415c2fdda0dSAndy Whitcroft annotate_reset(); 241613214adfSAndy Whitcroft $prev_values = 'E'; 241713214adfSAndy Whitcroft 2418773647a0SAndy Whitcroft %suppress_ifbraces = (); 2419170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 24202b474a1aSAndy Whitcroft %suppress_export = (); 24213e469cdcSAndy Whitcroft $suppress_statement = 0; 242274fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 242374fd4f34SJoe Perches $context_function = $1; 242474fd4f34SJoe Perches } else { 242574fd4f34SJoe Perches undef $context_function; 242674fd4f34SJoe Perches } 24270a920b5bSAndy Whitcroft next; 24280a920b5bSAndy Whitcroft 24294a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 24304a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 24314a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2432773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 24330a920b5bSAndy Whitcroft $realline++; 2434d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 24350a920b5bSAndy Whitcroft 24364a0df2efSAndy Whitcroft # Measure the line length and indent. 2437c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 24380a920b5bSAndy Whitcroft 24390a920b5bSAndy Whitcroft # Track the previous line. 24400a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 24410a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2442c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2443c2fdda0dSAndy Whitcroft 2444773647a0SAndy Whitcroft #warn "line<$line>\n"; 24456c72ffaaSAndy Whitcroft 2446d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2447d8aaf121SAndy Whitcroft $realcnt--; 24480a920b5bSAndy Whitcroft } 24490a920b5bSAndy Whitcroft 2450cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2451cc77cdcaSAndy Whitcroft 24526c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 24536c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2454773647a0SAndy Whitcroft 24552ac73b4fSJoe Perches my $found_file = 0; 2456773647a0SAndy Whitcroft # extract the filename as it passes 24573bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 24583bf9a009SRabin Vincent $realfile = $1; 24592b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2460270c49a0SJoe Perches $in_commit_log = 0; 24612ac73b4fSJoe Perches $found_file = 1; 24623bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2463773647a0SAndy Whitcroft $realfile = $1; 24642b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2465270c49a0SJoe Perches $in_commit_log = 0; 24661e855726SWolfram Sang 24671e855726SWolfram Sang $p1_prefix = $1; 2468e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2469e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2470000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2471000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 24721e855726SWolfram Sang } 2473773647a0SAndy Whitcroft 2474c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2475000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2476000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2477773647a0SAndy Whitcroft } 24782ac73b4fSJoe Perches $found_file = 1; 24792ac73b4fSJoe Perches } 24802ac73b4fSJoe Perches 248134d8815fSJoe Perches#make up the handle for any error we report on this line 248234d8815fSJoe Perches if ($showfile) { 248334d8815fSJoe Perches $prefix = "$realfile:$realline: " 248434d8815fSJoe Perches } elsif ($emacs) { 24857d3a9f67SJoe Perches if ($file) { 24867d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 24877d3a9f67SJoe Perches } else { 248834d8815fSJoe Perches $prefix = "$filename:$linenr: "; 248934d8815fSJoe Perches } 24907d3a9f67SJoe Perches } 249134d8815fSJoe Perches 24922ac73b4fSJoe Perches if ($found_file) { 249385b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 249485b0ee18SJoe Perches WARN("OBSOLETE", 249585b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 249685b0ee18SJoe Perches } 24977bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 24982ac73b4fSJoe Perches $check = 1; 24992ac73b4fSJoe Perches } else { 25002ac73b4fSJoe Perches $check = $check_orig; 25012ac73b4fSJoe Perches } 25029f3a8992SRob Herring $checklicenseline = 1; 2503773647a0SAndy Whitcroft next; 2504773647a0SAndy Whitcroft } 2505773647a0SAndy Whitcroft 2506389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 25070a920b5bSAndy Whitcroft 2508c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2509c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2510c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 25110a920b5bSAndy Whitcroft 25126c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 25136c72ffaaSAndy Whitcroft 2514*490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2515*490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2516*490b292cSJoe Perches if ($in_commit_log) { 2517*490b292cSJoe Perches if ($line !~ /^\s*$/) { 2518*490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2519*490b292cSJoe Perches } 2520*490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2521*490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2522*490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2523*490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2524*490b292cSJoe Perches } 2525*490b292cSJoe Perches 2526e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2527e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2528e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2529e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2530e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2531e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2532e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2533e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2534e518e9a5SJoe Perches $commit_log_has_diff = 1; 2535e518e9a5SJoe Perches } 2536e518e9a5SJoe Perches 25373bf9a009SRabin Vincent# Check for incorrect file permissions 25383bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 25393bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 254004db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 254104db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2542000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2543000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 25443bf9a009SRabin Vincent } 25453bf9a009SRabin Vincent } 25463bf9a009SRabin Vincent 2547cd261496SGeert Uytterhoeven# Check the patch for a From: 2548cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2549cd261496SGeert Uytterhoeven $author = $1; 2550cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2551cd261496SGeert Uytterhoeven $author =~ s/"//g; 2552cd261496SGeert Uytterhoeven } 2553cd261496SGeert Uytterhoeven 255420112475SJoe Perches# Check the patch for a signoff: 2555d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 25564a0df2efSAndy Whitcroft $signoff++; 255715662b3eSJoe Perches $in_commit_log = 0; 2558cd261496SGeert Uytterhoeven if ($author ne '') { 2559cd261496SGeert Uytterhoeven my $l = $line; 2560cd261496SGeert Uytterhoeven $l =~ s/"//g; 2561cd261496SGeert Uytterhoeven if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) { 2562cd261496SGeert Uytterhoeven $authorsignoff = 1; 2563cd261496SGeert Uytterhoeven } 2564cd261496SGeert Uytterhoeven } 25650a920b5bSAndy Whitcroft } 256620112475SJoe Perches 2567e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2568e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2569e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2570e0d975b1SJoe Perches $reported_maintainer_file = 1; 2571e0d975b1SJoe Perches } 2572e0d975b1SJoe Perches 257320112475SJoe Perches# Check signature styles 2574270c49a0SJoe Perches if (!$in_header_lines && 2575ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 257620112475SJoe Perches my $space_before = $1; 257720112475SJoe Perches my $sign_off = $2; 257820112475SJoe Perches my $space_after = $3; 257920112475SJoe Perches my $email = $4; 258020112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 258120112475SJoe Perches 2582ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2583ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2584ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2585ce0338dfSJoe Perches } 258620112475SJoe Perches if (defined $space_before && $space_before ne "") { 25873705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25883705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 25893705ce5bSJoe Perches $fix) { 2590194f66fcSJoe Perches $fixed[$fixlinenr] = 25913705ce5bSJoe Perches "$ucfirst_sign_off $email"; 25923705ce5bSJoe Perches } 259320112475SJoe Perches } 259420112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 25953705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25963705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 25973705ce5bSJoe Perches $fix) { 2598194f66fcSJoe Perches $fixed[$fixlinenr] = 25993705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26003705ce5bSJoe Perches } 26013705ce5bSJoe Perches 260220112475SJoe Perches } 260320112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 26043705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26053705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 26063705ce5bSJoe Perches $fix) { 2607194f66fcSJoe Perches $fixed[$fixlinenr] = 26083705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26093705ce5bSJoe Perches } 261020112475SJoe Perches } 261120112475SJoe Perches 261220112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 261320112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 261420112475SJoe Perches if ($suggested_email eq "") { 2615000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2616000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 261720112475SJoe Perches } else { 261820112475SJoe Perches my $dequoted = $suggested_email; 261920112475SJoe Perches $dequoted =~ s/^"//; 262020112475SJoe Perches $dequoted =~ s/" </ </; 262120112475SJoe Perches # Don't force email to have quotes 262220112475SJoe Perches # Allow just an angle bracketed address 262320112475SJoe Perches if ("$dequoted$comment" ne $email && 262420112475SJoe Perches "<$email_address>$comment" ne $email && 262520112475SJoe Perches "$suggested_email$comment" ne $email) { 2626000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2627000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 262820112475SJoe Perches } 26290a920b5bSAndy Whitcroft } 26307e51f197SJoe Perches 26317e51f197SJoe Perches# Check for duplicate signatures 26327e51f197SJoe Perches my $sig_nospace = $line; 26337e51f197SJoe Perches $sig_nospace =~ s/\s//g; 26347e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 26357e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 26367e51f197SJoe Perches WARN("BAD_SIGN_OFF", 26377e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 26387e51f197SJoe Perches } else { 26397e51f197SJoe Perches $signatures{$sig_nospace} = 1; 26407e51f197SJoe Perches } 26410a920b5bSAndy Whitcroft } 26420a920b5bSAndy Whitcroft 2643a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2644a2fe16b9SJoe Perches if ($in_header_lines && 2645a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2646a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2647a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2648a2fe16b9SJoe Perches } 2649a2fe16b9SJoe Perches 26507ebd05efSChristopher Covington# Check for unwanted Gerrit info 26517ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 26527ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 26537ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 26547ebd05efSChristopher Covington } 26557ebd05efSChristopher Covington 2656369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2657369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2658369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2659369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2660369c8dd3SJoe Perches # timestamp 2661369c8dd3SJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) { 2662369c8dd3SJoe Perches # stack dump address 2663369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2664369c8dd3SJoe Perches } 2665369c8dd3SJoe Perches 26662a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 26672a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2668bf4daf12SJoe Perches length($line) > 75 && 2669bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2670bf4daf12SJoe Perches # file delta changes 2671bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2672bf4daf12SJoe Perches # filename then : 2673bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2674bf4daf12SJoe Perches # A Fixes: or Link: line 2675bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 26762a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 26772a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 26782a076f40SJoe Perches $commit_log_long_line = 1; 26792a076f40SJoe Perches } 26802a076f40SJoe Perches 2681bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2682bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2683bf4daf12SJoe Perches $line =~ /^\s*$/) { 2684bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2685bf4daf12SJoe Perches } 2686bf4daf12SJoe Perches 26870d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2688369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2689aab38f51SJoe Perches $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i && 2690e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2691fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2692aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2693369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2694bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2695fe043ea1SJoe Perches my $init_char = "c"; 2696fe043ea1SJoe Perches my $orig_commit = ""; 26970d7835fcSJoe Perches my $short = 1; 26980d7835fcSJoe Perches my $long = 0; 26990d7835fcSJoe Perches my $case = 1; 27000d7835fcSJoe Perches my $space = 1; 27010d7835fcSJoe Perches my $hasdesc = 0; 270219c146a6SJoe Perches my $hasparens = 0; 27030d7835fcSJoe Perches my $id = '0123456789ab'; 27040d7835fcSJoe Perches my $orig_desc = "commit description"; 27050d7835fcSJoe Perches my $description = ""; 27060d7835fcSJoe Perches 2707fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2708fe043ea1SJoe Perches $init_char = $1; 2709fe043ea1SJoe Perches $orig_commit = lc($2); 2710fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2711fe043ea1SJoe Perches $orig_commit = lc($1); 2712fe043ea1SJoe Perches } 2713fe043ea1SJoe Perches 27140d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 27150d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 27160d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 27170d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 27180d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 27190d7835fcSJoe Perches $orig_desc = $1; 272019c146a6SJoe Perches $hasparens = 1; 27210d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 27220d7835fcSJoe Perches defined $rawlines[$linenr] && 27230d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 27240d7835fcSJoe Perches $orig_desc = $1; 272519c146a6SJoe Perches $hasparens = 1; 2726b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2727b671fde0SJoe Perches defined $rawlines[$linenr] && 2728b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2729b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2730b671fde0SJoe Perches $orig_desc = $1; 2731b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2732b671fde0SJoe Perches $orig_desc .= " " . $1; 273319c146a6SJoe Perches $hasparens = 1; 27340d7835fcSJoe Perches } 27350d7835fcSJoe Perches 27360d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 27370d7835fcSJoe Perches $id, $orig_desc); 27380d7835fcSJoe Perches 2739948b133aSHeinrich Schuchardt if (defined($id) && 2740948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2741d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 27420d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 27430d7835fcSJoe Perches } 2744d311cd44SJoe Perches } 2745d311cd44SJoe Perches 274613f1937eSJoe Perches# Check for added, moved or deleted files 274713f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 274813f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 274913f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 275013f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 275113f1937eSJoe Perches (defined($1) || defined($2))))) { 2752a82603a8SAndrew Jeffery $is_patch = 1; 275313f1937eSJoe Perches $reported_maintainer_file = 1; 275413f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 275513f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 275613f1937eSJoe Perches } 275713f1937eSJoe Perches 275800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 27598905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2760000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2761000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 27626c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2763de7d4f0eSAndy Whitcroft } 2764de7d4f0eSAndy Whitcroft 2765de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2766de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2767171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2768171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2769171ae1a4SAndy Whitcroft 2770171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2771171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2772171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2773171ae1a4SAndy Whitcroft 277434d99219SJoe Perches CHK("INVALID_UTF8", 2775000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 277600df344fSAndy Whitcroft } 27770a920b5bSAndy Whitcroft 277815662b3eSJoe Perches# Check if it's the start of a commit log 277915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 278015662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2781eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2782eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 278315662b3eSJoe Perches $in_header_lines = 0; 278415662b3eSJoe Perches $in_commit_log = 1; 2785ed43c4e5SAllen Hubbe $has_commit_log = 1; 278615662b3eSJoe Perches } 278715662b3eSJoe Perches 2788fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2789fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2790fa64205dSPasi Savanainen if ($in_header_lines && 2791fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2792fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2793fa64205dSPasi Savanainen $non_utf8_charset = 1; 2794fa64205dSPasi Savanainen } 2795fa64205dSPasi Savanainen 2796fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 279715662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2798fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 279915662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 280015662b3eSJoe Perches } 280115662b3eSJoe Perches 2802d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2803d6430f71SJoe Perches if ($tree && $in_commit_log) { 2804d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2805d6430f71SJoe Perches my $file = $1; 2806d6430f71SJoe Perches 2807d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2808d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2809d6430f71SJoe Perches # 2810d6430f71SJoe Perches } else { 2811d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2812d6430f71SJoe Perches } 2813d6430f71SJoe Perches } 2814d6430f71SJoe Perches } 2815d6430f71SJoe Perches 281666b47b4aSKees Cook# Check for various typo / spelling mistakes 281766d7a382SJoe Perches if (defined($misspellings) && 281866d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2819ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 282066b47b4aSKees Cook my $typo = $1; 282166b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 282266b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 282366b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 28240675a8fbSJean Delvare my $msg_level = \&WARN; 28250675a8fbSJean Delvare $msg_level = \&CHK if ($file); 28260675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 282766b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 282866b47b4aSKees Cook $fix) { 282966b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 283066b47b4aSKees Cook } 283166b47b4aSKees Cook } 283266b47b4aSKees Cook } 283366b47b4aSKees Cook 283430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 283530670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 283600df344fSAndy Whitcroft 28370a920b5bSAndy Whitcroft#trailing whitespace 28389c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2839c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2840d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2841d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2842d5e616fcSJoe Perches $fix) { 2843194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2844d5e616fcSJoe Perches } 2845c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2846c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28473705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 28483705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 28493705ce5bSJoe Perches $fix) { 2850194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 28513705ce5bSJoe Perches } 28523705ce5bSJoe Perches 2853d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 28540a920b5bSAndy Whitcroft } 28555368df20SAndy Whitcroft 28564783f894SJosh Triplett# Check for FSF mailing addresses. 2857109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 28581bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 28593e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 28603e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 28614783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28620675a8fbSJean Delvare my $msg_level = \&ERROR; 28630675a8fbSJean Delvare $msg_level = \&CHK if ($file); 28640675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 28654783f894SJosh 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) 28664783f894SJosh Triplett } 28674783f894SJosh Triplett 28683354957aSAndi Kleen# check for Kconfig help text having a real description 28699fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 28709fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 28713354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 2872678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 2873678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 2874678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 2875678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 28763354957aSAndi Kleen my $length = 0; 28779fe287d7SAndy Whitcroft my $cnt = $realcnt; 28789fe287d7SAndy Whitcroft my $ln = $linenr + 1; 28799fe287d7SAndy Whitcroft my $f; 2880a1385803SAndy Whitcroft my $is_start = 0; 28819fe287d7SAndy Whitcroft my $is_end = 0; 2882a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 28839fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 28849fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 28859fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 28869fe287d7SAndy Whitcroft 28879fe287d7SAndy Whitcroft next if ($f =~ /^-/); 28888d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2889a1385803SAndy Whitcroft 289086adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 2891a1385803SAndy Whitcroft $is_start = 1; 289284af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 289384af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 289484af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 289584af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 289684af7a61SUlf Magnusson } 2897a1385803SAndy Whitcroft $length = -1; 2898a1385803SAndy Whitcroft } 2899a1385803SAndy Whitcroft 29009fe287d7SAndy Whitcroft $f =~ s/^.//; 29013354957aSAndi Kleen $f =~ s/#.*//; 29023354957aSAndi Kleen $f =~ s/^\s+//; 29033354957aSAndi Kleen next if ($f =~ /^$/); 2904678ae162SUlf Magnusson 2905678ae162SUlf Magnusson # This only checks context lines in the patch 2906678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 2907678ae162SUlf Magnusson # positives, even though some of these are 2908678ae162SUlf Magnusson # common words in help texts 2909678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 2910678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 29119fe287d7SAndy Whitcroft $is_end = 1; 29129fe287d7SAndy Whitcroft last; 29139fe287d7SAndy Whitcroft } 29143354957aSAndi Kleen $length++; 29153354957aSAndi Kleen } 291656193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 2917000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 291856193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 291956193274SVadim Bendebury } 2920a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 29213354957aSAndi Kleen } 29223354957aSAndi Kleen 2923628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form 2924628f91a2SJoe Perches if ($realfile =~ /^MAINTAINERS$/ && 2925628f91a2SJoe Perches $rawline =~ /^\+[A-Z]:/ && 2926628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 2927628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 2928628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 2929628f91a2SJoe Perches $fix) { 2930628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 2931628f91a2SJoe Perches } 2932628f91a2SJoe Perches } 2933628f91a2SJoe Perches 2934327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 2935327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 2936327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 2937327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 2938327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 2939327953e9SChristoph Jaeger } 2940327953e9SChristoph Jaeger 2941c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2942c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2943c68e5878SArnaud Lacombe my $flag = $1; 2944c68e5878SArnaud Lacombe my $replacement = { 2945c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2946c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2947c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2948c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2949c68e5878SArnaud Lacombe }; 2950c68e5878SArnaud Lacombe 2951c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2952c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2953c68e5878SArnaud Lacombe } 2954c68e5878SArnaud Lacombe 2955bff5da43SRob Herring# check for DT compatible documentation 29567dd05b38SFlorian Vaussard if (defined $root && 29577dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 29587dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 29597dd05b38SFlorian Vaussard 2960bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2961bff5da43SRob Herring 2962cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2963cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2964cc93319bSFlorian Vaussard 2965bff5da43SRob Herring foreach my $compat (@compats) { 2966bff5da43SRob Herring my $compat2 = $compat; 2967185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2968185d566bSRob Herring my $compat3 = $compat; 2969185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2970185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2971bff5da43SRob Herring if ( $? >> 8 ) { 2972bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2973bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2974bff5da43SRob Herring } 2975bff5da43SRob Herring 29764fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 29774fbf32a6SFlorian Vaussard my $vendor = $1; 2978cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2979bff5da43SRob Herring if ( $? >> 8 ) { 2980bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2981cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2982bff5da43SRob Herring } 2983bff5da43SRob Herring } 2984bff5da43SRob Herring } 2985bff5da43SRob Herring 29869f3a8992SRob Herring# check for using SPDX license tag at beginning of files 29879f3a8992SRob Herring if ($realline == $checklicenseline) { 29889f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 29899f3a8992SRob Herring $checklicenseline = 2; 29909f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 29919f3a8992SRob Herring my $comment = ""; 29929f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 29939f3a8992SRob Herring $comment = '/*'; 29949f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 29959f3a8992SRob Herring $comment = '//'; 29969f3a8992SRob Herring } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) { 29979f3a8992SRob Herring $comment = '#'; 29989f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 29999f3a8992SRob Herring $comment = '..'; 30009f3a8992SRob Herring } 30019f3a8992SRob Herring 30029f3a8992SRob Herring if ($comment !~ /^$/ && 30039f3a8992SRob Herring $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) { 30049f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 30059f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 30063b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 30073b6e8ac9SJoe Perches my $spdx_license = $1; 30083b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 30093b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 30103b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 30113b6e8ac9SJoe Perches } 30129f3a8992SRob Herring } 30139f3a8992SRob Herring } 30149f3a8992SRob Herring } 30159f3a8992SRob Herring 30165368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3017d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 30185368df20SAndy Whitcroft 301947e0c88bSJoe Perches# line length limit (with some exclusions) 302047e0c88bSJoe Perches# 302147e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 302247e0c88bSJoe Perches# logging functions like pr_info that end in a string 302347e0c88bSJoe Perches# lines with a single string 302447e0c88bSJoe Perches# #defines that are a single string 30252e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 302647e0c88bSJoe Perches# 302747e0c88bSJoe Perches# There are 3 different line length message types: 3028ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 302947e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 303047e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 303147e0c88bSJoe Perches# 303247e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 303347e0c88bSJoe Perches# 303447e0c88bSJoe Perches 3035b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 303647e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 303747e0c88bSJoe Perches 303847e0c88bSJoe Perches # Check the allowed long line types first 303947e0c88bSJoe Perches 304047e0c88bSJoe Perches # logging functions that end in a string that starts 304147e0c88bSJoe Perches # before $max_line_length 304247e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 304347e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 304447e0c88bSJoe Perches $msg_type = ""; 304547e0c88bSJoe Perches 304647e0c88bSJoe Perches # lines with only strings (w/ possible termination) 304747e0c88bSJoe Perches # #defines with only strings 304847e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 304947e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 305047e0c88bSJoe Perches $msg_type = ""; 305147e0c88bSJoe Perches 3052cc147506SJoe Perches # More special cases 3053cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3054cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3055d560a5f8SJoe Perches $msg_type = ""; 3056d560a5f8SJoe Perches 30572e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 30582e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 30592e4bbbc5SAndreas Brauchli $msg_type = ""; 30602e4bbbc5SAndreas Brauchli 306147e0c88bSJoe Perches # Otherwise set the alternate message types 306247e0c88bSJoe Perches 306347e0c88bSJoe Perches # a comment starts before $max_line_length 306447e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 306547e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 306647e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 306747e0c88bSJoe Perches 306847e0c88bSJoe Perches # a quoted string starts before $max_line_length 306947e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 307047e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 307147e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 307247e0c88bSJoe Perches } 307347e0c88bSJoe Perches 307447e0c88bSJoe Perches if ($msg_type ne "" && 307547e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 307647e0c88bSJoe Perches WARN($msg_type, 30776cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 30780a920b5bSAndy Whitcroft } 307947e0c88bSJoe Perches } 30800a920b5bSAndy Whitcroft 30818905a67cSAndy Whitcroft# check for adding lines without a newline. 30828905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3083000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3084000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 30858905a67cSAndy Whitcroft } 30868905a67cSAndy Whitcroft 3087b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3088de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 30890a920b5bSAndy Whitcroft 30900a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 30910a920b5bSAndy Whitcroft# more than 8 must use tabs. 3092c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3093c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3094c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3095d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 30963705ce5bSJoe Perches if (ERROR("CODE_INDENT", 30973705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 30983705ce5bSJoe Perches $fix) { 3099194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 31003705ce5bSJoe Perches } 31010a920b5bSAndy Whitcroft } 31020a920b5bSAndy Whitcroft 310308e44365SAlberto Panizzo# check for space before tabs. 310408e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 310508e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 31063705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 31073705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 31083705ce5bSJoe Perches $fix) { 3109194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3110d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 3111194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3112c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 31133705ce5bSJoe Perches } 311408e44365SAlberto Panizzo } 311508e44365SAlberto Panizzo 31166a487211SJoe Perches# check for assignments on the start of a line 31176a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 31186a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 31196a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 31206a487211SJoe Perches } 31216a487211SJoe Perches 3122d1fe9c09SJoe Perches# check for && or || at the start of a line 3123d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3124d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3125d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3126d1fe9c09SJoe Perches } 3127d1fe9c09SJoe Perches 3128a91e8994SJoe Perches# check indentation starts on a tab stop 31295b57980dSJoe Perches if ($perl_version_ok && 3130bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3131a91e8994SJoe Perches my $indent = length($1); 3132a91e8994SJoe Perches if ($indent % 8) { 3133a91e8994SJoe Perches if (WARN("TABSTOP", 3134a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3135a91e8994SJoe Perches $fix) { 3136a91e8994SJoe Perches $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; 3137a91e8994SJoe Perches } 3138a91e8994SJoe Perches } 3139a91e8994SJoe Perches } 3140a91e8994SJoe Perches 3141d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 31425b57980dSJoe Perches if ($perl_version_ok && 3143fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3144d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3145d1fe9c09SJoe Perches my $oldindent = $1; 3146d1fe9c09SJoe Perches my $rest = $2; 3147d1fe9c09SJoe Perches 3148d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3149d1fe9c09SJoe Perches if ($pos >= 0) { 3150b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3151b34a26f3SJoe Perches my $newindent = $2; 3152d1fe9c09SJoe Perches 3153d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3154d1fe9c09SJoe Perches "\t" x ($pos / 8) . 3155d1fe9c09SJoe Perches " " x ($pos % 8); 3156d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3157d1fe9c09SJoe Perches 3158d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3159d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 31603705ce5bSJoe Perches 31613705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 31623705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 31633705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3164194f66fcSJoe Perches $fixed[$fixlinenr] =~ 31653705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 31663705ce5bSJoe Perches } 3167d1fe9c09SJoe Perches } 3168d1fe9c09SJoe Perches } 3169d1fe9c09SJoe Perches } 3170d1fe9c09SJoe Perches 31716ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 31726ab3a970SJoe Perches# avoid checking a few false positives: 31736ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 31746ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 31756ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 31766ab3a970SJoe Perches# multiline macros that define functions 31776ab3a970SJoe Perches# known attributes or the __attribute__ keyword 31786ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 31796ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 31803705ce5bSJoe Perches if (CHK("SPACING", 3181f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 31823705ce5bSJoe Perches $fix) { 3183194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3184f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 31853705ce5bSJoe Perches } 3186aad4f614SJoe Perches } 3187aad4f614SJoe Perches 318886406b1cSJoe Perches# Block comment styles 318986406b1cSJoe Perches# Networking with an initial /* 319005880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3191fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 319285ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 319385ad978cSJoe Perches $realline > 2) { 319405880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 319505880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 319605880600SJoe Perches } 319705880600SJoe Perches 319886406b1cSJoe Perches# Block comments use * on subsequent lines 319986406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 320086406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3201a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 320261135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3203a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 320486406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 320586406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3206a605e32eSJoe Perches } 3207a605e32eSJoe Perches 320886406b1cSJoe Perches# Block comments use */ on trailing lines 320986406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3210c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3211c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3212c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 321386406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 321486406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 321505880600SJoe Perches } 321605880600SJoe Perches 321708eb9b80SJoe Perches# Block comment * alignment 321808eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3219af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3220af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3221af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 322208eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3223af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3224af207524SJoe Perches my $oldindent; 322508eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3226af207524SJoe Perches if (defined($1)) { 3227af207524SJoe Perches $oldindent = expand_tabs($1); 3228af207524SJoe Perches } else { 3229af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3230af207524SJoe Perches $oldindent = expand_tabs($1); 3231af207524SJoe Perches } 323208eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 323308eb9b80SJoe Perches my $newindent = $1; 323408eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3235af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 323608eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 323708eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 323808eb9b80SJoe Perches } 323908eb9b80SJoe Perches } 324008eb9b80SJoe Perches 32417f619191SJoe Perches# check for missing blank lines after struct/union declarations 32427f619191SJoe Perches# with exceptions for various attributes and macros 32437f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 32447f619191SJoe Perches $line =~ /^\+/ && 32457f619191SJoe Perches !($line =~ /^\+\s*$/ || 32467f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 32477f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 32487f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 32497f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 32507f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 32517f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 32520bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 32537f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3254d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3255d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3256d752fcc8SJoe Perches $fix) { 3257f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3258d752fcc8SJoe Perches } 32597f619191SJoe Perches } 32607f619191SJoe Perches 3261365dd4eaSJoe Perches# check for multiple consecutive blank lines 3262365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3263365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3264365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3265d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3266d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3267d752fcc8SJoe Perches $fix) { 3268f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3269d752fcc8SJoe Perches } 3270d752fcc8SJoe Perches 3271365dd4eaSJoe Perches $last_blank_line = $linenr; 3272365dd4eaSJoe Perches } 3273365dd4eaSJoe Perches 32743b617e3bSJoe Perches# check for missing blank lines after declarations 32753f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 32763f7bac03SJoe Perches # actual declarations 32773f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 32785a4e1fd3SJoe Perches # function pointer declarations 32795a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 32803f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 32813f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 32823f7bac03SJoe Perches # known declaration macros 32833f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 32843f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 32853f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 32863f7bac03SJoe Perches # other possible extensions of declaration lines 32873f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 32883f7bac03SJoe Perches # not starting a section or a macro "\" extended line 32893f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 32903f7bac03SJoe Perches # looks like a declaration 32913f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 32925a4e1fd3SJoe Perches # function pointer declarations 32935a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 32943f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 32953f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 32963f7bac03SJoe Perches # known declaration macros 32973f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 32983f7bac03SJoe Perches # start of struct or union or enum 32993b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 33003f7bac03SJoe Perches # start or end of block or continuation of declaration 33013f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 33023f7bac03SJoe Perches # bitfield continuation 33033f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 33043f7bac03SJoe Perches # other possible extensions of declaration lines 33053f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 33063f7bac03SJoe Perches # indentation of previous and current line are the same 33073f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3308d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3309d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3310d752fcc8SJoe Perches $fix) { 3311f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3312d752fcc8SJoe Perches } 33133b617e3bSJoe Perches } 33143b617e3bSJoe Perches 33155f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 33166b4c5bebSAndy Whitcroft# Exceptions: 33176b4c5bebSAndy Whitcroft# 1) within comments 33186b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 33196b4c5bebSAndy Whitcroft# 3) hanging labels 33203705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 33215f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 33223705ce5bSJoe Perches if (WARN("LEADING_SPACE", 33233705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 33243705ce5bSJoe Perches $fix) { 3325194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 33263705ce5bSJoe Perches } 33275f7ddae6SRaffaele Recalcati } 33285f7ddae6SRaffaele Recalcati 3329b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3330b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3331b9ea10d6SAndy Whitcroft 33325751a24eSJoe Perches# check for unusual line ending [ or ( 33335751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 33345751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 33355751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 33365751a24eSJoe Perches } 33375751a24eSJoe Perches 33384dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 33394dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 33404dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 33414dbed76fSJoe Perches $context_function = $1; 33424dbed76fSJoe Perches } 33434dbed76fSJoe Perches 33444dbed76fSJoe Perches# check if this appears to be the end of function declaration 33454dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 33464dbed76fSJoe Perches undef $context_function; 33474dbed76fSJoe Perches } 33484dbed76fSJoe Perches 3349032a4c0fSJoe Perches# check indentation of any line with a bare else 3350840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3351032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3352032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3353032a4c0fSJoe Perches my $tabs = length($1) + 1; 3354840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3355840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3356840080a0SJoe Perches defined $lines[$linenr] && 3357840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3358032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3359032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3360032a4c0fSJoe Perches } 3361032a4c0fSJoe Perches } 3362032a4c0fSJoe Perches 3363c00df19aSJoe Perches# check indentation of a line with a break; 3364c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3365c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3366c00df19aSJoe Perches my $tabs = $1; 3367c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3368c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3369c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3370c00df19aSJoe Perches } 3371c00df19aSJoe Perches } 3372c00df19aSJoe Perches 3373c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3374cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3375000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3376000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3377c2fdda0dSAndy Whitcroft } 337822f2a2efSAndy Whitcroft 337956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 338056e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 338156e77d70SJoe Perches WARN("HOTPLUG_SECTION", 338256e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 338356e77d70SJoe Perches } 338456e77d70SJoe Perches 33859c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 33862b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 33872b474a1aSAndy Whitcroft $realline_next); 33883e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3389ca819864SJoe Perches if ($linenr > $suppress_statement && 33901b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3391170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3392f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3393171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3394171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3395171ae1a4SAndy Whitcroft 33963e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 33973e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 33983e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 33993e469cdcSAndy Whitcroft # until we hit end of it. 34003e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 34013e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 34023e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 34033e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 34043e469cdcSAndy Whitcroft } 3405f74bd194SAndy Whitcroft 34062b474a1aSAndy Whitcroft # Find the real next line. 34072b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 34082b474a1aSAndy Whitcroft if (defined $realline_next && 34092b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 34102b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 34112b474a1aSAndy Whitcroft $realline_next++; 34122b474a1aSAndy Whitcroft } 34132b474a1aSAndy Whitcroft 3414171ae1a4SAndy Whitcroft my $s = $stat; 3415171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3416cf655043SAndy Whitcroft 3417c2fdda0dSAndy Whitcroft # Ignore goto labels. 3418171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3419c2fdda0dSAndy Whitcroft 3420c2fdda0dSAndy Whitcroft # Ignore functions being called 3421171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3422c2fdda0dSAndy Whitcroft 3423463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3424463f2864SAndy Whitcroft 3425c45dcabdSAndy Whitcroft # declarations always start with types 3426d2506586SAndy 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) { 3427c45dcabdSAndy Whitcroft my $type = $1; 3428c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3429c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3430c45dcabdSAndy Whitcroft 34316c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3432a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3433c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3434c2fdda0dSAndy Whitcroft } 34358905a67cSAndy Whitcroft 34366c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 343765863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3438c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 34399c0ca6f9SAndy Whitcroft } 34408905a67cSAndy Whitcroft 34418905a67cSAndy Whitcroft # Check for any sort of function declaration. 34428905a67cSAndy Whitcroft # int foo(something bar, other baz); 34438905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3444171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 34458905a67cSAndy Whitcroft my ($name_len) = length($1); 34468905a67cSAndy Whitcroft 3447cf655043SAndy Whitcroft my $ctx = $s; 3448773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 34498905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3450cf655043SAndy Whitcroft 34518905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3452c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 34538905a67cSAndy Whitcroft 3454c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 34558905a67cSAndy Whitcroft } 34568905a67cSAndy Whitcroft } 34578905a67cSAndy Whitcroft } 34588905a67cSAndy Whitcroft 34599c0ca6f9SAndy Whitcroft } 34609c0ca6f9SAndy Whitcroft 346100df344fSAndy Whitcroft# 346200df344fSAndy Whitcroft# Checks which may be anchored in the context. 346300df344fSAndy Whitcroft# 346400df344fSAndy Whitcroft 346500df344fSAndy Whitcroft# Check for switch () and associated case and default 346600df344fSAndy Whitcroft# statements should be at the same indent. 346700df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 346800df344fSAndy Whitcroft my $err = ''; 346900df344fSAndy Whitcroft my $sep = ''; 347000df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 347100df344fSAndy Whitcroft shift(@ctx); 347200df344fSAndy Whitcroft for my $ctx (@ctx) { 347300df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 347400df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 347500df344fSAndy Whitcroft $indent != $cindent) { 347600df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 347700df344fSAndy Whitcroft $sep = ''; 347800df344fSAndy Whitcroft } else { 347900df344fSAndy Whitcroft $sep = "[...]\n"; 348000df344fSAndy Whitcroft } 348100df344fSAndy Whitcroft } 348200df344fSAndy Whitcroft if ($err ne '') { 3483000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3484000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3485de7d4f0eSAndy Whitcroft } 3486de7d4f0eSAndy Whitcroft } 3487de7d4f0eSAndy Whitcroft 3488de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3489de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 34900fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3491773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3492773647a0SAndy Whitcroft 34939c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 34948eef05ddSJoe Perches 34958eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 34968eef05ddSJoe Perches WARN("DEEP_INDENTATION", 34978eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 34988eef05ddSJoe Perches } 34998eef05ddSJoe Perches 3500de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3501de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3502de7d4f0eSAndy Whitcroft 3503548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3504548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3505de7d4f0eSAndy Whitcroft 3506548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3507548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3508548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3509548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3510548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3511773647a0SAndy Whitcroft $ctx_ln++; 3512773647a0SAndy Whitcroft } 3513548596d5SAndy Whitcroft 351453210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 351553210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3516773647a0SAndy Whitcroft 3517773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3518000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3519000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 352001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 352100df344fSAndy Whitcroft } 3522773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3523773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3524773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3525773647a0SAndy Whitcroft { 35269c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 35279c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3528000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3529000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 353001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 35319c0ca6f9SAndy Whitcroft } 35329c0ca6f9SAndy Whitcroft } 353300df344fSAndy Whitcroft } 353400df344fSAndy Whitcroft 35354d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3536f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 35373e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 35383e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 35393e469cdcSAndy Whitcroft if (!defined $stat); 35404d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 35414d001e4dSAndy Whitcroft 35424d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 35434d001e4dSAndy Whitcroft 35449f5af480SJoe Perches # remove inline comments 35459f5af480SJoe Perches $s =~ s/$;/ /g; 35469f5af480SJoe Perches $c =~ s/$;/ /g; 35474d001e4dSAndy Whitcroft 35484d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 35496f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 35506f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 35514d001e4dSAndy Whitcroft 35529f5af480SJoe Perches # Make sure we remove the line prefixes as we have 35539f5af480SJoe Perches # none on the first line, and are going to readd them 35549f5af480SJoe Perches # where necessary. 35559f5af480SJoe Perches $s =~ s/\n./\n/gs; 35569f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 35579f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 35589f5af480SJoe Perches } 35599f5af480SJoe Perches 35604d001e4dSAndy Whitcroft # We want to check the first line inside the block 35614d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 35624d001e4dSAndy Whitcroft # 1) any blank line termination 35634d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 35644d001e4dSAndy Whitcroft # 3) any do (...) { 35654d001e4dSAndy Whitcroft my $continuation = 0; 35664d001e4dSAndy Whitcroft my $check = 0; 35674d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 35684d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 35694d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 35704d001e4dSAndy Whitcroft $continuation = 1; 35714d001e4dSAndy Whitcroft } 35729bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 35734d001e4dSAndy Whitcroft $check = 1; 35744d001e4dSAndy Whitcroft $cond_lines++; 35754d001e4dSAndy Whitcroft } 35764d001e4dSAndy Whitcroft 35774d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 35784d001e4dSAndy Whitcroft # preprocessor statement. 35794d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 35804d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 35814d001e4dSAndy Whitcroft $check = 0; 35824d001e4dSAndy Whitcroft } 35834d001e4dSAndy Whitcroft 35849bd49efeSAndy Whitcroft my $cond_ptr = -1; 3585740504c6SAndy Whitcroft $continuation = 0; 35869bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 35879bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 35884d001e4dSAndy Whitcroft 3589f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3590f16fa28fSAndy Whitcroft # is not linear. 3591f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3592f16fa28fSAndy Whitcroft $check = 0; 3593f16fa28fSAndy Whitcroft } 3594f16fa28fSAndy Whitcroft 35959bd49efeSAndy Whitcroft # Ignore: 35969bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 35979bd49efeSAndy Whitcroft # 2) preprocessor lines, and 35989bd49efeSAndy Whitcroft # 3) labels. 3599740504c6SAndy Whitcroft if ($continuation || 3600740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 36019bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 36029bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3603740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 360430dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 36059bd49efeSAndy Whitcroft $cond_lines++; 36069bd49efeSAndy Whitcroft } 36074d001e4dSAndy Whitcroft } 360830dad6ebSAndy Whitcroft } 36094d001e4dSAndy Whitcroft 36104d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 36114d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 36124d001e4dSAndy Whitcroft 36134d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 36144d001e4dSAndy Whitcroft # this is not this patch's fault. 36154d001e4dSAndy Whitcroft if (!defined($stat_real) || 36164d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 36174d001e4dSAndy Whitcroft $check = 0; 36184d001e4dSAndy Whitcroft } 36194d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 36204d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 36214d001e4dSAndy Whitcroft } 36224d001e4dSAndy Whitcroft 36239bd49efeSAndy 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"; 36244d001e4dSAndy Whitcroft 36259f5af480SJoe Perches if ($check && $s ne '' && 36269f5af480SJoe Perches (($sindent % 8) != 0 || 36279f5af480SJoe Perches ($sindent < $indent) || 3628f6950a73SJoe Perches ($sindent == $indent && 3629f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 36309f5af480SJoe Perches ($sindent > $indent + 8))) { 3631000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3632000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 36334d001e4dSAndy Whitcroft } 36344d001e4dSAndy Whitcroft } 36354d001e4dSAndy Whitcroft 36366c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 36376c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 36381f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 36391f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 36406c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3641c2fdda0dSAndy Whitcroft if ($dbg_values) { 3642c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3643cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3644cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 36451f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3646c2fdda0dSAndy Whitcroft } 36476c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 36486c72ffaaSAndy Whitcroft 364900df344fSAndy Whitcroft#ignore lines not being added 36503705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 365100df344fSAndy Whitcroft 365211ca40a0SJoe Perches# check for dereferences that span multiple lines 365311ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 365411ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 365511ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 365611ca40a0SJoe Perches my $ref = $1; 365711ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 365811ca40a0SJoe Perches $ref .= $1; 365911ca40a0SJoe Perches $ref =~ s/\s//g; 366011ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 366111ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 366211ca40a0SJoe Perches } 366311ca40a0SJoe Perches 3664a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3665c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3666a1ce18e4SJoe Perches my $type = $1; 3667a1ce18e4SJoe Perches my $var = $2; 3668207a8e84SJoe Perches $var = "" if (!defined $var); 3669207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3670a1ce18e4SJoe Perches my $sign = $1; 3671a1ce18e4SJoe Perches my $pointer = $2; 3672a1ce18e4SJoe Perches 3673a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3674a1ce18e4SJoe Perches 3675a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3676a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3677a1ce18e4SJoe Perches $fix) { 3678a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3679207a8e84SJoe Perches my $comp_pointer = $pointer; 3680207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3681207a8e84SJoe Perches $decl .= $comp_pointer; 3682207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3683207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3684a1ce18e4SJoe Perches } 3685a1ce18e4SJoe Perches } 3686a1ce18e4SJoe Perches } 3687a1ce18e4SJoe Perches 3688653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 36897429c690SAndy Whitcroft if ($dbg_type) { 36907429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3691000d1cc1SJoe Perches ERROR("TEST_TYPE", 3692000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 36937429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3694000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3695000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 36967429c690SAndy Whitcroft } 3697653d4876SAndy Whitcroft next; 3698653d4876SAndy Whitcroft } 3699a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3700a1ef277eSAndy Whitcroft if ($dbg_attr) { 37019360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3702000d1cc1SJoe Perches ERROR("TEST_ATTR", 3703000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 37049360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3705000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3706000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3707a1ef277eSAndy Whitcroft } 3708a1ef277eSAndy Whitcroft next; 3709a1ef277eSAndy Whitcroft } 3710653d4876SAndy Whitcroft 3711f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 371299423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 371399423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3714d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3715d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3716f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3717f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3718f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3719d752fcc8SJoe Perches my $fixedline = $prevrawline; 3720d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3721f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3722d752fcc8SJoe Perches $fixedline = $line; 37238d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3724f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3725d752fcc8SJoe Perches } 3726f0a594c1SAndy Whitcroft } 3727f0a594c1SAndy Whitcroft 372800df344fSAndy Whitcroft# 372900df344fSAndy Whitcroft# Checks which are anchored on the added line. 373000df344fSAndy Whitcroft# 373100df344fSAndy Whitcroft 3732653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3733c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3734653d4876SAndy Whitcroft my $path = $1; 3735653d4876SAndy Whitcroft if ($path =~ m{//}) { 3736000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3737495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3738495e9d84SJoe Perches } 3739495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3740495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3741495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3742653d4876SAndy Whitcroft } 3743653d4876SAndy Whitcroft } 3744653d4876SAndy Whitcroft 374500df344fSAndy Whitcroft# no C99 // comments 374600df344fSAndy Whitcroft if ($line =~ m{//}) { 37473705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 37483705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 37493705ce5bSJoe Perches $fix) { 3750194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 37513705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 37523705ce5bSJoe Perches my $comment = trim($1); 3753194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 37543705ce5bSJoe Perches } 37553705ce5bSJoe Perches } 375600df344fSAndy Whitcroft } 375700df344fSAndy Whitcroft # Remove C99 comments. 37580a920b5bSAndy Whitcroft $line =~ s@//.*@@; 37596c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 37600a920b5bSAndy Whitcroft 37612b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 37622b474a1aSAndy Whitcroft# the whole statement. 37632b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 37642b474a1aSAndy Whitcroft if (defined $realline_next && 37652b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 37662b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 37672b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 37682b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 37693cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 37703cbf62dfSAndy Whitcroft # a prefix: 37713cbf62dfSAndy Whitcroft # XXX(foo); 37723cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3773653d4876SAndy Whitcroft my $name = $1; 377487a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 37753cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 37763cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 37773cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 37783cbf62dfSAndy Whitcroft 37793cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 37802b474a1aSAndy Whitcroft \n.}\s*$| 378148012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 378248012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 378348012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 37842b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 37852b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 378648012058SAndy Whitcroft )/x) { 37872b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 37882b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 37892b474a1aSAndy Whitcroft } else { 37902b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 37910a920b5bSAndy Whitcroft } 37920a920b5bSAndy Whitcroft } 37932b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 37942b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 37952b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 37962b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 37972b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 37982b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 37992b474a1aSAndy Whitcroft } 38002b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 38012b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3802000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3803000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 38042b474a1aSAndy Whitcroft } 38050a920b5bSAndy Whitcroft 38065150bda4SJoe Eloff# check for global initialisers. 38076d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3808d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 38096d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3810d5e616fcSJoe Perches $fix) { 38116d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3812d5e616fcSJoe Perches } 3813f0a594c1SAndy Whitcroft } 38140a920b5bSAndy Whitcroft# check for static initialisers. 38156d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3816d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 38176d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3818d5e616fcSJoe Perches $herecurr) && 3819d5e616fcSJoe Perches $fix) { 38206d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3821d5e616fcSJoe Perches } 38220a920b5bSAndy Whitcroft } 38230a920b5bSAndy Whitcroft 38241813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 38251813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 38261813087dSJoe Perches my $tmp = trim($1); 38271813087dSJoe Perches WARN("MISORDERED_TYPE", 38281813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 38291813087dSJoe Perches } 38301813087dSJoe Perches 3831cb710ecaSJoe Perches# check for static const char * arrays. 3832cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3833000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3834000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3835cb710ecaSJoe Perches $herecurr); 3836cb710ecaSJoe Perches } 3837cb710ecaSJoe Perches 3838cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3839cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3840000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3841000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3842cb710ecaSJoe Perches $herecurr); 3843cb710ecaSJoe Perches } 3844cb710ecaSJoe Perches 3845ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 3846ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3847ab7e23f3SJoe Perches my $found = $1; 3848ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3849ab7e23f3SJoe Perches WARN("CONST_CONST", 3850ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3851ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3852ab7e23f3SJoe Perches WARN("CONST_CONST", 3853ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 3854ab7e23f3SJoe Perches } 3855ab7e23f3SJoe Perches } 3856ab7e23f3SJoe Perches 38579b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 38589b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 38599b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 38609b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 38619b0fa60dSJoe Perches $herecurr); 38629b0fa60dSJoe Perches } 38639b0fa60dSJoe Perches 3864b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 3865b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 3866b598b670SJoe Perches my $array = $1; 3867b598b670SJoe 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*\))@) { 3868b598b670SJoe Perches my $array_div = $1; 3869b598b670SJoe Perches if (WARN("ARRAY_SIZE", 3870b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 3871b598b670SJoe Perches $fix) { 3872b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 3873b598b670SJoe Perches } 3874b598b670SJoe Perches } 3875b598b670SJoe Perches } 3876b598b670SJoe Perches 3877b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 3878b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3879b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 3880b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 3881b36190c5SJoe Perches $fix) { 3882194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 3883b36190c5SJoe Perches } 3884b36190c5SJoe Perches } 3885b36190c5SJoe Perches 3886653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 3887653d4876SAndy Whitcroft# make sense. 3888653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 38898054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3890c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 38918ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 389246d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 3893000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 3894000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 38950a920b5bSAndy Whitcroft } 38960a920b5bSAndy Whitcroft 38970a920b5bSAndy Whitcroft# * goes on variable not on type 389865863862SAndy Whitcroft # (char*[ const]) 3899bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 3900bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 39013705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 3902d8aaf121SAndy Whitcroft 390365863862SAndy Whitcroft # Should start with a space. 390465863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 390565863862SAndy Whitcroft # Should not end with a space. 390665863862SAndy Whitcroft $to =~ s/\s+$//; 390765863862SAndy Whitcroft # '*'s should not have spaces between. 3908f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 390965863862SAndy Whitcroft } 3910d8aaf121SAndy Whitcroft 39113705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 391265863862SAndy Whitcroft if ($from ne $to) { 39133705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 39143705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 39153705ce5bSJoe Perches $fix) { 39163705ce5bSJoe Perches my $sub_from = $ident; 39173705ce5bSJoe Perches my $sub_to = $ident; 39183705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3919194f66fcSJoe Perches $fixed[$fixlinenr] =~ 39203705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 39213705ce5bSJoe Perches } 392265863862SAndy Whitcroft } 3923bfcb2cc7SAndy Whitcroft } 3924bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 3925bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 39263705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 3927d8aaf121SAndy Whitcroft 392865863862SAndy Whitcroft # Should start with a space. 392965863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 393065863862SAndy Whitcroft # Should not end with a space. 393165863862SAndy Whitcroft $to =~ s/\s+$//; 393265863862SAndy Whitcroft # '*'s should not have spaces between. 3933f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 393465863862SAndy Whitcroft } 393565863862SAndy Whitcroft # Modifiers should have spaces. 393665863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 393765863862SAndy Whitcroft 39383705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 3939667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 39403705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 39413705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 39423705ce5bSJoe Perches $fix) { 39433705ce5bSJoe Perches 39443705ce5bSJoe Perches my $sub_from = $match; 39453705ce5bSJoe Perches my $sub_to = $match; 39463705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3947194f66fcSJoe Perches $fixed[$fixlinenr] =~ 39483705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 39493705ce5bSJoe Perches } 395065863862SAndy Whitcroft } 39510a920b5bSAndy Whitcroft } 39520a920b5bSAndy Whitcroft 39539d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 39549d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 39550675a8fbSJean Delvare my $msg_level = \&WARN; 39560675a8fbSJean Delvare $msg_level = \&CHK if ($file); 39570675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 39589d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 39599d3e3c70SJoe Perches } 39600a920b5bSAndy Whitcroft 39619d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 39628905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 3963000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 3964000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 39658905a67cSAndy Whitcroft } 39668905a67cSAndy Whitcroft 396717441227SJoe Perches# check for uses of printk_ratelimit 396817441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 3969000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 3970000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 397117441227SJoe Perches } 397217441227SJoe Perches 3973eeef5733SJoe Perches# printk should use KERN_* levels 3974eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 3975000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 3976eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 397700df344fSAndy Whitcroft } 39780a920b5bSAndy Whitcroft 3979243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 3980243f3803SJoe Perches my $orig = $1; 3981243f3803SJoe Perches my $level = lc($orig); 3982243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 39838f26b837SJoe Perches my $level2 = $level; 39848f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 3985243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 3986daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 3987243f3803SJoe Perches } 3988243f3803SJoe Perches 3989243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 3990d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 3991d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 3992d5e616fcSJoe Perches $fix) { 3993194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3994d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 3995d5e616fcSJoe Perches } 3996243f3803SJoe Perches } 3997243f3803SJoe Perches 3998dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 3999dc139313SJoe Perches my $orig = $1; 4000dc139313SJoe Perches my $level = lc($orig); 4001dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4002dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4003dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4004dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4005dc139313SJoe Perches } 4006dc139313SJoe Perches 400791c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 400891c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 400991c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 401091c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 401191c9afafSAndy Lutomirski WARN("ENOSYS", 401291c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 401391c9afafSAndy Lutomirski } 401491c9afafSAndy Lutomirski 4015653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4016653d4876SAndy Whitcroft# or if closed on same line 40175b57980dSJoe Perches if ($perl_version_ok && 40182d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 40192d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 40202d453e3bSJoe Perches $sline !~ /}/) { 40218d182478SJoe Perches if (ERROR("OPEN_BRACE", 40222d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 40238d182478SJoe Perches $fix) { 40248d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 40258d182478SJoe Perches my $fixed_line = $rawline; 40268d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 40278d182478SJoe Perches my $line1 = $1; 40288d182478SJoe Perches my $line2 = $2; 40298d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 40308d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 40318d182478SJoe Perches if ($line2 !~ /^\s*$/) { 40328d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 40338d182478SJoe Perches } 40348d182478SJoe Perches } 40350a920b5bSAndy Whitcroft } 4036653d4876SAndy Whitcroft 40378905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 40388905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 40398905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 40408d182478SJoe Perches if (ERROR("OPEN_BRACE", 40418d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 40428d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 40438d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 40448d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 40458d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 40468d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40478d182478SJoe Perches $fixedline = $rawline; 40488d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 40498d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 40508d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40518d182478SJoe Perches } 40528d182478SJoe Perches } 40538905a67cSAndy Whitcroft } 40548905a67cSAndy Whitcroft 40550c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 40563705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 40573705ce5bSJoe Perches if (WARN("SPACING", 40583705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 40593705ce5bSJoe Perches $fix) { 4060194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40613705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 40623705ce5bSJoe Perches } 40630c73b4ebSAndy Whitcroft } 40640c73b4ebSAndy Whitcroft 406531070b5dSJoe Perches# Function pointer declarations 406631070b5dSJoe Perches# check spacing between type, funcptr, and args 406731070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 406891f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 406931070b5dSJoe Perches my $declare = $1; 407031070b5dSJoe Perches my $pre_pointer_space = $2; 407131070b5dSJoe Perches my $post_pointer_space = $3; 407231070b5dSJoe Perches my $funcname = $4; 407331070b5dSJoe Perches my $post_funcname_space = $5; 407431070b5dSJoe Perches my $pre_args_space = $6; 407531070b5dSJoe Perches 407691f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 407791f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 407891f72e9cSJoe Perches# don't need a space so don't warn for those. 407991f72e9cSJoe Perches my $post_declare_space = ""; 408091f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 408191f72e9cSJoe Perches $post_declare_space = $1; 408291f72e9cSJoe Perches $declare = rtrim($declare); 408391f72e9cSJoe Perches } 408491f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 408531070b5dSJoe Perches WARN("SPACING", 408631070b5dSJoe Perches "missing space after return type\n" . $herecurr); 408791f72e9cSJoe Perches $post_declare_space = " "; 408831070b5dSJoe Perches } 408931070b5dSJoe Perches 409031070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 409191f72e9cSJoe Perches# This test is not currently implemented because these declarations are 409291f72e9cSJoe Perches# equivalent to 409391f72e9cSJoe Perches# int foo(int bar, ...) 409491f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 409591f72e9cSJoe Perches# 409691f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 409791f72e9cSJoe Perches# WARN("SPACING", 409891f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 409991f72e9cSJoe Perches# } 410031070b5dSJoe Perches 410131070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 410231070b5dSJoe Perches if (defined $pre_pointer_space && 410331070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 410431070b5dSJoe Perches WARN("SPACING", 410531070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 410631070b5dSJoe Perches } 410731070b5dSJoe Perches 410831070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 410931070b5dSJoe Perches if (defined $post_pointer_space && 411031070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 411131070b5dSJoe Perches WARN("SPACING", 411231070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 411331070b5dSJoe Perches } 411431070b5dSJoe Perches 411531070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 411631070b5dSJoe Perches if (defined $post_funcname_space && 411731070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 411831070b5dSJoe Perches WARN("SPACING", 411931070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 412031070b5dSJoe Perches } 412131070b5dSJoe Perches 412231070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 412331070b5dSJoe Perches if (defined $pre_args_space && 412431070b5dSJoe Perches $pre_args_space =~ /^\s/) { 412531070b5dSJoe Perches WARN("SPACING", 412631070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 412731070b5dSJoe Perches } 412831070b5dSJoe Perches 412931070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4130194f66fcSJoe Perches $fixed[$fixlinenr] =~ 413191f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 413231070b5dSJoe Perches } 413331070b5dSJoe Perches } 413431070b5dSJoe Perches 41358d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 41368d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4137fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4138fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 41398d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 41408d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 41418d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4142fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 414338dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 41443705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 41453705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 41463705ce5bSJoe Perches $fix) { 4147194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41483705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 41493705ce5bSJoe Perches } 41508d31cfceSAndy Whitcroft } 41518d31cfceSAndy Whitcroft } 41528d31cfceSAndy Whitcroft 4153f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 41546c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4155c2fdda0dSAndy Whitcroft my $name = $1; 4156773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4157773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4158c2fdda0dSAndy Whitcroft 4159c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4160773647a0SAndy Whitcroft if ($name =~ /^(?: 4161773647a0SAndy Whitcroft if|for|while|switch|return|case| 4162773647a0SAndy Whitcroft volatile|__volatile__| 4163773647a0SAndy Whitcroft __attribute__|format|__extension__| 4164773647a0SAndy Whitcroft asm|__asm__)$/x) 4165773647a0SAndy Whitcroft { 4166c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4167c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4168c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4169c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4170773647a0SAndy Whitcroft 4171773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4172c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4173c2fdda0dSAndy Whitcroft 4174c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4175c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4176773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4177c2fdda0dSAndy Whitcroft 4178c2fdda0dSAndy Whitcroft } else { 41793705ce5bSJoe Perches if (WARN("SPACING", 41803705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 41813705ce5bSJoe Perches $fix) { 4182194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41833705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 41843705ce5bSJoe Perches } 4185f0a594c1SAndy Whitcroft } 41866c72ffaaSAndy Whitcroft } 41879a4cad4eSEric Nelson 4188653d4876SAndy Whitcroft# Check operator spacing. 41890a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 41903705ce5bSJoe Perches my $fixed_line = ""; 41913705ce5bSJoe Perches my $line_fixed = 0; 41923705ce5bSJoe Perches 41939c0ca6f9SAndy Whitcroft my $ops = qr{ 41949c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 41959c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 41969c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 41971f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 419884731623SJoe Perches \?:|\?|: 41999c0ca6f9SAndy Whitcroft }x; 4200cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 42013705ce5bSJoe Perches 42023705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 42033705ce5bSJoe Perches## foreach my $el (@elements) { 42043705ce5bSJoe Perches## print("el: <$el>\n"); 42053705ce5bSJoe Perches## } 42063705ce5bSJoe Perches 42073705ce5bSJoe Perches my @fix_elements = (); 420800df344fSAndy Whitcroft my $off = 0; 42096c72ffaaSAndy Whitcroft 42103705ce5bSJoe Perches foreach my $el (@elements) { 42113705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 42123705ce5bSJoe Perches $off += length($el); 42133705ce5bSJoe Perches } 42143705ce5bSJoe Perches 42153705ce5bSJoe Perches $off = 0; 42163705ce5bSJoe Perches 42176c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4218b34c648bSJoe Perches my $last_after = -1; 42196c72ffaaSAndy Whitcroft 42200a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 42213705ce5bSJoe Perches 42223705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 42233705ce5bSJoe Perches 42243705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 42253705ce5bSJoe Perches 42264a0df2efSAndy Whitcroft $off += length($elements[$n]); 42274a0df2efSAndy Whitcroft 422825985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4229773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4230773647a0SAndy Whitcroft my $cc = ''; 4231773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4232773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4233773647a0SAndy Whitcroft } 4234773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4235773647a0SAndy Whitcroft 42364a0df2efSAndy Whitcroft my $a = ''; 42374a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 42384a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4239cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 42404a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 42414a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4242773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 42434a0df2efSAndy Whitcroft 42440a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 42454a0df2efSAndy Whitcroft 42464a0df2efSAndy Whitcroft my $c = ''; 42470a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 42484a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 42494a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4250cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 42514a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 42524a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 42538b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 42544a0df2efSAndy Whitcroft } else { 42554a0df2efSAndy Whitcroft $c = 'E'; 42560a920b5bSAndy Whitcroft } 42570a920b5bSAndy Whitcroft 42584a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 42594a0df2efSAndy Whitcroft 42604a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 42614a0df2efSAndy Whitcroft 42626c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4263de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 42640a920b5bSAndy Whitcroft 426574048ed8SAndy Whitcroft # Pull out the value of this operator. 42666c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 42670a920b5bSAndy Whitcroft 42681f65f947SAndy Whitcroft # Get the full operator variant. 42691f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 42701f65f947SAndy Whitcroft 427113214adfSAndy Whitcroft # Ignore operators passed as parameters. 427213214adfSAndy Whitcroft if ($op_type ne 'V' && 4273d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 427413214adfSAndy Whitcroft 4275cf655043SAndy Whitcroft# # Ignore comments 4276cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 427713214adfSAndy Whitcroft 4278d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 427913214adfSAndy Whitcroft } elsif ($op eq ';') { 4280cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4281cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 42823705ce5bSJoe Perches if (ERROR("SPACING", 42833705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4284b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 42853705ce5bSJoe Perches $line_fixed = 1; 42863705ce5bSJoe Perches } 4287d8aaf121SAndy Whitcroft } 4288d8aaf121SAndy Whitcroft 4289d8aaf121SAndy Whitcroft # // is a comment 4290d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 42910a920b5bSAndy Whitcroft 4292b00e4814SJoe Perches # : when part of a bitfield 4293b00e4814SJoe Perches } elsif ($opv eq ':B') { 4294b00e4814SJoe Perches # skip the bitfield test for now 4295b00e4814SJoe Perches 42961f65f947SAndy Whitcroft # No spaces for: 42971f65f947SAndy Whitcroft # -> 4298b00e4814SJoe Perches } elsif ($op eq '->') { 42994a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 43003705ce5bSJoe Perches if (ERROR("SPACING", 43013705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4302b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 43033705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 43043705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 43053705ce5bSJoe Perches } 4306b34c648bSJoe Perches $line_fixed = 1; 43073705ce5bSJoe Perches } 43080a920b5bSAndy Whitcroft } 43090a920b5bSAndy Whitcroft 43102381097bSJoe Perches # , must not have a space before and must have a space on the right. 43110a920b5bSAndy Whitcroft } elsif ($op eq ',') { 43122381097bSJoe Perches my $rtrim_before = 0; 43132381097bSJoe Perches my $space_after = 0; 43142381097bSJoe Perches if ($ctx =~ /Wx./) { 43152381097bSJoe Perches if (ERROR("SPACING", 43162381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 43172381097bSJoe Perches $line_fixed = 1; 43182381097bSJoe Perches $rtrim_before = 1; 43192381097bSJoe Perches } 43202381097bSJoe Perches } 4321cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 43223705ce5bSJoe Perches if (ERROR("SPACING", 43233705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 43243705ce5bSJoe Perches $line_fixed = 1; 4325b34c648bSJoe Perches $last_after = $n; 43262381097bSJoe Perches $space_after = 1; 43272381097bSJoe Perches } 43282381097bSJoe Perches } 43292381097bSJoe Perches if ($rtrim_before || $space_after) { 43302381097bSJoe Perches if ($rtrim_before) { 43312381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 43322381097bSJoe Perches } else { 43332381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 43342381097bSJoe Perches } 43352381097bSJoe Perches if ($space_after) { 43362381097bSJoe Perches $good .= " "; 43373705ce5bSJoe Perches } 43380a920b5bSAndy Whitcroft } 43390a920b5bSAndy Whitcroft 43409c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 434174048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 43429c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 43439c0ca6f9SAndy Whitcroft 43449c0ca6f9SAndy Whitcroft # unary operators should have a space before and 43459c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 43469c0ca6f9SAndy Whitcroft # unary operator, or a cast 43479c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 434874048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 43490d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4350cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 43513705ce5bSJoe Perches if (ERROR("SPACING", 43523705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4353b34c648bSJoe Perches if ($n != $last_after + 2) { 4354b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 43553705ce5bSJoe Perches $line_fixed = 1; 43563705ce5bSJoe Perches } 43570a920b5bSAndy Whitcroft } 4358b34c648bSJoe Perches } 4359a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4360171ae1a4SAndy Whitcroft # A unary '*' may be const 4361171ae1a4SAndy Whitcroft 4362171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 43633705ce5bSJoe Perches if (ERROR("SPACING", 43643705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4365b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 43663705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 43673705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 43683705ce5bSJoe Perches } 4369b34c648bSJoe Perches $line_fixed = 1; 43703705ce5bSJoe Perches } 43710a920b5bSAndy Whitcroft } 43720a920b5bSAndy Whitcroft 43730a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 43740a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4375773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 43763705ce5bSJoe Perches if (ERROR("SPACING", 43773705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4378b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 43793705ce5bSJoe Perches $line_fixed = 1; 43803705ce5bSJoe Perches } 43810a920b5bSAndy Whitcroft } 4382773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4383773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 43843705ce5bSJoe Perches if (ERROR("SPACING", 43853705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4386b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 43873705ce5bSJoe Perches $line_fixed = 1; 43883705ce5bSJoe Perches } 4389653d4876SAndy Whitcroft } 4390773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 43913705ce5bSJoe Perches if (ERROR("SPACING", 43923705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4393b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 43943705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 43953705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4396773647a0SAndy Whitcroft } 4397b34c648bSJoe Perches $line_fixed = 1; 43983705ce5bSJoe Perches } 43993705ce5bSJoe Perches } 44000a920b5bSAndy Whitcroft 44010a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 44029c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 44039c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 44049c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4405c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4406c2fdda0dSAndy Whitcroft $op eq '%') 44070a920b5bSAndy Whitcroft { 4408d2e025f3SJoe Perches if ($check) { 4409d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4410d2e025f3SJoe Perches if (CHK("SPACING", 4411d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4412d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4413d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4414d2e025f3SJoe Perches $line_fixed = 1; 4415d2e025f3SJoe Perches } 4416d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4417d2e025f3SJoe Perches if (CHK("SPACING", 4418d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4419d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4420d2e025f3SJoe Perches $line_fixed = 1; 4421d2e025f3SJoe Perches } 4422d2e025f3SJoe Perches } 4423d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 44243705ce5bSJoe Perches if (ERROR("SPACING", 44253705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4426b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4427b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4428b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4429b34c648bSJoe Perches } 44303705ce5bSJoe Perches $line_fixed = 1; 44313705ce5bSJoe Perches } 44320a920b5bSAndy Whitcroft } 44330a920b5bSAndy Whitcroft 44341f65f947SAndy Whitcroft # A colon needs no spaces before when it is 44351f65f947SAndy Whitcroft # terminating a case value or a label. 44361f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 44371f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 44383705ce5bSJoe Perches if (ERROR("SPACING", 44393705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4440b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44413705ce5bSJoe Perches $line_fixed = 1; 44423705ce5bSJoe Perches } 44431f65f947SAndy Whitcroft } 44441f65f947SAndy Whitcroft 44450a920b5bSAndy Whitcroft # All the others need spaces both sides. 4446cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 44471f65f947SAndy Whitcroft my $ok = 0; 44481f65f947SAndy Whitcroft 444922f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 44501f65f947SAndy Whitcroft if (($op eq '<' && 44511f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 44521f65f947SAndy Whitcroft ($op eq '>' && 44531f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 44541f65f947SAndy Whitcroft { 44551f65f947SAndy Whitcroft $ok = 1; 44561f65f947SAndy Whitcroft } 44571f65f947SAndy Whitcroft 4458e0df7e1fSJoe Perches # for asm volatile statements 4459e0df7e1fSJoe Perches # ignore a colon with another 4460e0df7e1fSJoe Perches # colon immediately before or after 4461e0df7e1fSJoe Perches if (($op eq ':') && 4462e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4463e0df7e1fSJoe Perches $ok = 1; 4464e0df7e1fSJoe Perches } 4465e0df7e1fSJoe Perches 446684731623SJoe Perches # messages are ERROR, but ?: are CHK 44671f65f947SAndy Whitcroft if ($ok == 0) { 44680675a8fbSJean Delvare my $msg_level = \&ERROR; 44690675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 447084731623SJoe Perches 44710675a8fbSJean Delvare if (&{$msg_level}("SPACING", 44723705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4473b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4474b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4475b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4476b34c648bSJoe Perches } 44773705ce5bSJoe Perches $line_fixed = 1; 44783705ce5bSJoe Perches } 44790a920b5bSAndy Whitcroft } 448022f2a2efSAndy Whitcroft } 44814a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 44823705ce5bSJoe Perches 44833705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 44843705ce5bSJoe Perches 44853705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 44860a920b5bSAndy Whitcroft } 44873705ce5bSJoe Perches 44883705ce5bSJoe Perches if (($#elements % 2) == 0) { 44893705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 44903705ce5bSJoe Perches } 44913705ce5bSJoe Perches 4492194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4493194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 44943705ce5bSJoe Perches } 44953705ce5bSJoe Perches 44963705ce5bSJoe Perches 44970a920b5bSAndy Whitcroft } 44980a920b5bSAndy Whitcroft 4499786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4500d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4501786b6326SJoe Perches if (WARN("SPACING", 4502786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4503786b6326SJoe Perches $fix) { 4504194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4505786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4506786b6326SJoe Perches } 4507786b6326SJoe Perches } 4508786b6326SJoe Perches 4509f0a594c1SAndy Whitcroft# check for multiple assignments 4510f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4511000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4512000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4513f0a594c1SAndy Whitcroft } 4514f0a594c1SAndy Whitcroft 451522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 451622f2a2efSAndy Whitcroft## # continuation. 451722f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 451822f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 451922f2a2efSAndy Whitcroft## 452022f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 452122f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 452222f2a2efSAndy Whitcroft## my $ln = $line; 452322f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 452422f2a2efSAndy Whitcroft## } 452522f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4526000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4527000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 452822f2a2efSAndy Whitcroft## } 452922f2a2efSAndy Whitcroft## } 4530f0a594c1SAndy Whitcroft 45310a920b5bSAndy Whitcroft#need space before brace following if, while, etc 45326b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 45334e5d56bdSEddie Kovsky $line =~ /do\{/) { 45343705ce5bSJoe Perches if (ERROR("SPACING", 45353705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 45363705ce5bSJoe Perches $fix) { 45378d81ae05SCyril Bur $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/; 45383705ce5bSJoe Perches } 4539de7d4f0eSAndy Whitcroft } 4540de7d4f0eSAndy Whitcroft 4541c4a62ef9SJoe Perches## # check for blank lines before declarations 4542c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4543c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4544c4a62ef9SJoe Perches## WARN("SPACING", 4545c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4546c4a62ef9SJoe Perches## } 4547c4a62ef9SJoe Perches## 4548c4a62ef9SJoe Perches 4549de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4550de7d4f0eSAndy Whitcroft# on the line 4551de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 4552d5e616fcSJoe Perches if (ERROR("SPACING", 4553d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4554d5e616fcSJoe Perches $fix) { 4555194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4556d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4557d5e616fcSJoe Perches } 45580a920b5bSAndy Whitcroft } 45590a920b5bSAndy Whitcroft 456022f2a2efSAndy Whitcroft# check spacing on square brackets 456122f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 45623705ce5bSJoe Perches if (ERROR("SPACING", 45633705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 45643705ce5bSJoe Perches $fix) { 4565194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45663705ce5bSJoe Perches s/\[\s+/\[/; 45673705ce5bSJoe Perches } 456822f2a2efSAndy Whitcroft } 456922f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 45703705ce5bSJoe Perches if (ERROR("SPACING", 45713705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 45723705ce5bSJoe Perches $fix) { 4573194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45743705ce5bSJoe Perches s/\s+\]/\]/; 45753705ce5bSJoe Perches } 457622f2a2efSAndy Whitcroft } 457722f2a2efSAndy Whitcroft 4578c45dcabdSAndy Whitcroft# check spacing on parentheses 45799c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 45809c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 45813705ce5bSJoe Perches if (ERROR("SPACING", 45823705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 45833705ce5bSJoe Perches $fix) { 4584194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45853705ce5bSJoe Perches s/\(\s+/\(/; 45863705ce5bSJoe Perches } 458722f2a2efSAndy Whitcroft } 458813214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4589c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4590c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 45913705ce5bSJoe Perches if (ERROR("SPACING", 45923705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 45933705ce5bSJoe Perches $fix) { 4594194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45953705ce5bSJoe Perches s/\s+\)/\)/; 45963705ce5bSJoe Perches } 459722f2a2efSAndy Whitcroft } 459822f2a2efSAndy Whitcroft 4599e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4600e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4601e2826fd0SJoe Perches 4602e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4603ea4acbb1SJoe Perches my $var = $1; 4604ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4605ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4606ea4acbb1SJoe Perches $fix) { 4607ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4608ea4acbb1SJoe Perches } 4609ea4acbb1SJoe Perches } 4610ea4acbb1SJoe Perches 4611ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4612ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4613ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4614ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4615ea4acbb1SJoe Perches my $var = $2; 4616ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4617ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4618ea4acbb1SJoe Perches $fix) { 4619ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4620ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4621ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4622ea4acbb1SJoe Perches } 4623e2826fd0SJoe Perches } 4624e2826fd0SJoe Perches 462563b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4626a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4627a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 46285b57980dSJoe Perches $perl_version_ok && defined($stat) && 462963b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 463063b7c73eSJoe Perches my $if_stat = $1; 463163b7c73eSJoe Perches my $test = substr($2, 1, -1); 463263b7c73eSJoe Perches my $herectx; 463363b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 463463b7c73eSJoe Perches my $match = $1; 463563b7c73eSJoe Perches # avoid parentheses around potential macro args 463663b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 463763b7c73eSJoe Perches if (!defined($herectx)) { 463863b7c73eSJoe Perches $herectx = $here . "\n"; 463963b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 464063b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 464163b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 464263b7c73eSJoe Perches $herectx .= $rl . "\n"; 464363b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 464463b7c73eSJoe Perches } 464563b7c73eSJoe Perches } 464663b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 464763b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 464863b7c73eSJoe Perches } 464963b7c73eSJoe Perches } 465063b7c73eSJoe Perches 46510a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 46524a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 46530a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 46543705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 46553705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 46563705ce5bSJoe Perches $fix) { 4657194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46583705ce5bSJoe Perches s/^(.)\s+/$1/; 46593705ce5bSJoe Perches } 46600a920b5bSAndy Whitcroft } 46610a920b5bSAndy Whitcroft 46625b9553abSJoe Perches# return is not a function 4663507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4664c45dcabdSAndy Whitcroft my $spacing = $1; 46655b57980dSJoe Perches if ($perl_version_ok && 46665b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 46675b9553abSJoe Perches my $value = $1; 46685b9553abSJoe Perches $value = deparenthesize($value); 46695b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4670000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4671000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 46725b9553abSJoe Perches } 4673c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4674000d1cc1SJoe Perches ERROR("SPACING", 4675000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4676c45dcabdSAndy Whitcroft } 4677c45dcabdSAndy Whitcroft } 4678507e5141SJoe Perches 4679b43ae21bSJoe Perches# unnecessary return in a void function 4680b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4681b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4682b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4683b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4684b43ae21bSJoe Perches $linenr >= 3 && 4685b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4686b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 46879819cf25SJoe Perches WARN("RETURN_VOID", 4688b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 46899819cf25SJoe Perches } 46909819cf25SJoe Perches 4691189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 46925b57980dSJoe Perches if ($perl_version_ok && 4693189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4694189248d8SJoe Perches my $openparens = $1; 4695189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4696189248d8SJoe Perches my $msg = ""; 4697189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4698189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4699189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4700189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4701189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4702189248d8SJoe Perches } 4703189248d8SJoe Perches } 4704189248d8SJoe Perches 4705c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4706c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4707c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4708c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 47095b57980dSJoe Perches if ($perl_version_ok && 4710c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4711c5595fa2SJoe Perches my $lead = $1; 4712c5595fa2SJoe Perches my $const = $2; 4713c5595fa2SJoe Perches my $comp = $3; 4714c5595fa2SJoe Perches my $to = $4; 4715c5595fa2SJoe Perches my $newcomp = $comp; 4716f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4717c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4718c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4719c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4720c5595fa2SJoe Perches $fix) { 4721c5595fa2SJoe Perches if ($comp eq "<") { 4722c5595fa2SJoe Perches $newcomp = ">"; 4723c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4724c5595fa2SJoe Perches $newcomp = ">="; 4725c5595fa2SJoe Perches } elsif ($comp eq ">") { 4726c5595fa2SJoe Perches $newcomp = "<"; 4727c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4728c5595fa2SJoe Perches $newcomp = "<="; 4729c5595fa2SJoe Perches } 4730c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4731c5595fa2SJoe Perches } 4732c5595fa2SJoe Perches } 4733c5595fa2SJoe Perches 4734f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4735f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 473653a3c448SAndy Whitcroft my $name = $1; 473753a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4738000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4739f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 474053a3c448SAndy Whitcroft } 474153a3c448SAndy Whitcroft } 4742c45dcabdSAndy Whitcroft 47430a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 47444a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 47453705ce5bSJoe Perches if (ERROR("SPACING", 47463705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 47473705ce5bSJoe Perches $fix) { 4748194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47493705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 47503705ce5bSJoe Perches } 47510a920b5bSAndy Whitcroft } 47520a920b5bSAndy Whitcroft 4753f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4754f5fe35ddSAndy Whitcroft# statements after the conditional. 4755170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 47563e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 47573e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 47583e469cdcSAndy Whitcroft if (!defined $stat); 4759170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4760170d3a22SAndy Whitcroft $remain_next, $off_next); 4761170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4762170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4763170d3a22SAndy Whitcroft 4764170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4765170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4766170d3a22SAndy Whitcroft # then count those as offsets. 4767170d3a22SAndy Whitcroft my ($whitespace) = 4768170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4769170d3a22SAndy Whitcroft my $offset = 4770170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4771170d3a22SAndy Whitcroft 4772170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4773170d3a22SAndy Whitcroft $offset} = 1; 4774170d3a22SAndy Whitcroft } 4775170d3a22SAndy Whitcroft } 4776170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4777c11230f4SJoe Perches defined($stat) && defined($cond) && 4778170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4779171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 47808905a67cSAndy Whitcroft 4781b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4782000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4783000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 47848905a67cSAndy Whitcroft } 47858905a67cSAndy Whitcroft 47868905a67cSAndy Whitcroft # Find out what is on the end of the line after the 47878905a67cSAndy Whitcroft # conditional. 4788773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 47898905a67cSAndy Whitcroft $s =~ s/\n.*//g; 479013214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 479153210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 479253210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4793773647a0SAndy Whitcroft { 4794bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4795bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4796bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 479742bdf74cSHidetoshi Seto my $stat_real = ''; 4798bb44ad39SAndy Whitcroft 479942bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 480042bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4801bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4802bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4803bb44ad39SAndy Whitcroft } 4804bb44ad39SAndy Whitcroft 4805000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4806000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 48078905a67cSAndy Whitcroft } 48088905a67cSAndy Whitcroft } 48098905a67cSAndy Whitcroft 481013214adfSAndy Whitcroft# Check for bitwise tests written as boolean 481113214adfSAndy Whitcroft if ($line =~ / 481213214adfSAndy Whitcroft (?: 481313214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 481413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 481513214adfSAndy Whitcroft (?:\&\&|\|\|) 481613214adfSAndy Whitcroft | 481713214adfSAndy Whitcroft (?:\&\&|\|\|) 481813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 481913214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 482013214adfSAndy Whitcroft )/x) 482113214adfSAndy Whitcroft { 4822000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4823000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 482413214adfSAndy Whitcroft } 482513214adfSAndy Whitcroft 48268905a67cSAndy Whitcroft# if and else should not have general statements after it 482713214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 482813214adfSAndy Whitcroft my $s = $1; 482913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 483013214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4831000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4832000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 48330a920b5bSAndy Whitcroft } 483413214adfSAndy Whitcroft } 483539667782SAndy Whitcroft# if should not continue a brace 483639667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4837000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4838048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 483939667782SAndy Whitcroft $herecurr); 484039667782SAndy Whitcroft } 4841a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4842a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4843a1080bf8SAndy Whitcroft $line !~ /\G(?: 48443fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4845a1080bf8SAndy Whitcroft \s*return\s+ 4846a1080bf8SAndy Whitcroft )/xg) 4847a1080bf8SAndy Whitcroft { 4848000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4849000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4850a1080bf8SAndy Whitcroft } 48510a920b5bSAndy Whitcroft 48520a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 48530a920b5bSAndy Whitcroft # indent level to be relevant to each other. 48548b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 48550a920b5bSAndy Whitcroft $previndent == $indent) { 48568b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 48578b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 48588b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 48598b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 48608b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 48618b8856f4SJoe Perches my $fixedline = $prevrawline; 48628b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 48638b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 48648b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48658b8856f4SJoe Perches } 48668b8856f4SJoe Perches $fixedline = $rawline; 48678b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 48688b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48698b8856f4SJoe Perches } 48700a920b5bSAndy Whitcroft } 48710a920b5bSAndy Whitcroft 48728b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 4873c2fdda0dSAndy Whitcroft $previndent == $indent) { 4874c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 4875c2fdda0dSAndy Whitcroft 4876c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 4877c2fdda0dSAndy Whitcroft # conditional. 4878773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 4879c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 4880c2fdda0dSAndy Whitcroft 4881c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 48828b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 48838b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 48848b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 48858b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 48868b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 48878b8856f4SJoe Perches my $fixedline = $prevrawline; 48888b8856f4SJoe Perches my $trailing = $rawline; 48898b8856f4SJoe Perches $trailing =~ s/^\+//; 48908b8856f4SJoe Perches $trailing = trim($trailing); 48918b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 48928b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48938b8856f4SJoe Perches } 4894c2fdda0dSAndy Whitcroft } 4895c2fdda0dSAndy Whitcroft } 4896c2fdda0dSAndy Whitcroft 489795e2c602SJoe Perches#Specific variable tests 4898323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 4899323c1260SJoe Perches my $var = $1; 490095e2c602SJoe Perches 490195e2c602SJoe Perches#gcc binary extension 490295e2c602SJoe Perches if ($var =~ /^$Binary$/) { 4903d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 4904d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 4905d5e616fcSJoe Perches $fix) { 4906d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 4907194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4908d5e616fcSJoe Perches s/\b$var\b/$hexval/; 4909d5e616fcSJoe Perches } 491095e2c602SJoe Perches } 491195e2c602SJoe Perches 491295e2c602SJoe Perches#CamelCase 4913807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 4914be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 491522735ce8SJoe Perches#Ignore Page<foo> variants 4916807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 491722735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4918f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && 4919f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 4920f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 49217e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 49227e781f67SJoe Perches my $word = $1; 49237e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4924d8b07710SJoe Perches if ($check) { 4925d8b07710SJoe Perches seed_camelcase_includes(); 4926d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 4927d8b07710SJoe Perches seed_camelcase_file($realfile); 4928d8b07710SJoe Perches $camelcase_file_seeded = 1; 4929d8b07710SJoe Perches } 4930d8b07710SJoe Perches } 49317e781f67SJoe Perches if (!defined $camelcase{$word}) { 49327e781f67SJoe Perches $camelcase{$word} = 1; 4933be79794bSJoe Perches CHK("CAMELCASE", 49347e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 49357e781f67SJoe Perches } 4936323c1260SJoe Perches } 4937323c1260SJoe Perches } 49383445686aSJoe Perches } 49390a920b5bSAndy Whitcroft 49400a920b5bSAndy Whitcroft#no spaces allowed after \ in define 4941d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 4942d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 4943d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 4944d5e616fcSJoe Perches $fix) { 4945194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 4946d5e616fcSJoe Perches } 49470a920b5bSAndy Whitcroft } 49480a920b5bSAndy Whitcroft 49490e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 49500e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 4951c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4952e09dec48SAndy Whitcroft my $file = "$1.h"; 4953e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 4954e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 4955e09dec48SAndy Whitcroft $realfile ne $checkfile && 49567840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 4957c45dcabdSAndy Whitcroft { 49580e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 49590e212e0aSFabian Frederick if ($asminclude > 0) { 4960e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 4961000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 4962000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4963e09dec48SAndy Whitcroft } else { 4964000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 4965000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4966e09dec48SAndy Whitcroft } 49670a920b5bSAndy Whitcroft } 49680a920b5bSAndy Whitcroft } 49690e212e0aSFabian Frederick } 49700a920b5bSAndy Whitcroft 4971653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 4972653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 4973cf655043SAndy Whitcroft# in a known good container 4974b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 4975b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 4976d8aaf121SAndy Whitcroft my $ln = $linenr; 4977d8aaf121SAndy Whitcroft my $cnt = $realcnt; 4978c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 4979c45dcabdSAndy Whitcroft my $ctx = ''; 498008a2843eSJoe Perches my $has_flow_statement = 0; 498108a2843eSJoe Perches my $has_arg_concat = 0; 4982c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 4983f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4984f74bd194SAndy Whitcroft $ctx = $dstat; 4985c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4986a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4987c45dcabdSAndy Whitcroft 498808a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 498962e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 499008a2843eSJoe Perches 4991f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 4992f59b64bfSJoe Perches my $define_args = $1; 4993f59b64bfSJoe Perches my $define_stmt = $dstat; 4994f59b64bfSJoe Perches my @def_args = (); 4995f59b64bfSJoe Perches 4996f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 4997f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 4998f59b64bfSJoe Perches $define_args =~ s/\s*//g; 49998c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5000f59b64bfSJoe Perches @def_args = split(",", $define_args); 5001f59b64bfSJoe Perches } 5002f59b64bfSJoe Perches 5003292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5004c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5005c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5006c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5007c45dcabdSAndy Whitcroft 5008c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 5009bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 5010bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 50116b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 5012bf30d6edSAndy Whitcroft { 5013c45dcabdSAndy Whitcroft } 5014c45dcabdSAndy Whitcroft 5015e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 501633acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 501733acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5018e45bab8eSAndy Whitcroft { 5019e45bab8eSAndy Whitcroft } 5020e45bab8eSAndy Whitcroft 502142e15293SJoe Perches # Make asm volatile uses seem like a generic function 502242e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 502342e15293SJoe Perches 5024c45dcabdSAndy Whitcroft my $exceptions = qr{ 5025c45dcabdSAndy Whitcroft $Declare| 5026c45dcabdSAndy Whitcroft module_param_named| 5027a0a0a7a9SKees Cook MODULE_PARM_DESC| 5028c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5029c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5030383099fdSAndy Whitcroft __typeof__\(| 503122fd2d3eSStefani Seibold union| 503222fd2d3eSStefani Seibold struct| 5033ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 50346b10df42SVladimir Zapolskiy ^\"|\"$| 50356b10df42SVladimir Zapolskiy ^\[ 5036c45dcabdSAndy Whitcroft }x; 50375eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5038f59b64bfSJoe Perches 5039f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5040f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5041e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5042f59b64bfSJoe Perches 5043f74bd194SAndy Whitcroft if ($dstat ne '' && 5044f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5045f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 50463cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5047356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5048f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5049f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5050e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 505172f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5052f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5053f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5054f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 50554e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5056f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5057c45dcabdSAndy Whitcroft { 5058e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5059e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5060e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5061e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5062f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5063f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5064f74bd194SAndy Whitcroft } else { 5065000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5066388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5067d8aaf121SAndy Whitcroft } 5068f59b64bfSJoe Perches 5069f59b64bfSJoe Perches } 50705207649bSJoe Perches 50715207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 50725207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 50735207649bSJoe Perches my $first = 1; 50745207649bSJoe Perches $define_stmt = ""; 50755207649bSJoe Perches foreach my $l (@stmt_array) { 50765207649bSJoe Perches $l =~ s/\\$//; 50775207649bSJoe Perches if ($first) { 50785207649bSJoe Perches $define_stmt = $l; 50795207649bSJoe Perches $first = 0; 50805207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 50815207649bSJoe Perches $define_stmt .= substr($l, 1); 50825207649bSJoe Perches } 50835207649bSJoe Perches } 50845207649bSJoe Perches $define_stmt =~ s/$;//g; 50855207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 50865207649bSJoe Perches $define_stmt = trim($define_stmt); 50875207649bSJoe Perches 5088f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5089f59b64bfSJoe Perches foreach my $arg (@def_args) { 5090f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 50919192d41aSJoe Perches next if ($arg =~ /^type$/i); 50927fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 50937fe528a2SJoe Perches $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 50947fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 50957fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5096d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5097f59b64bfSJoe Perches if ($use_cnt > 1) { 5098f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5099f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5100f59b64bfSJoe Perches } 51019192d41aSJoe Perches# check if any macro arguments may have other precedence issues 51027fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 51039192d41aSJoe Perches ((defined($1) && $1 ne ',') || 51049192d41aSJoe Perches (defined($2) && $2 ne ','))) { 51059192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 51069192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 51079192d41aSJoe Perches } 51080a920b5bSAndy Whitcroft } 51095023d347SJoe Perches 511008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 511108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 511208a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 511308a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5114e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 511508a2843eSJoe Perches 511608a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 511708a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 511808a2843eSJoe Perches } 511908a2843eSJoe Perches 5120481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 51215023d347SJoe Perches 51225023d347SJoe Perches } else { 51235023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5124481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5125481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 51265023d347SJoe Perches $line =~ /^\+.*\\$/) { 51275023d347SJoe Perches WARN("LINE_CONTINUATIONS", 51285023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 51295023d347SJoe Perches } 5130653d4876SAndy Whitcroft } 51310a920b5bSAndy Whitcroft 5132b13edf7fSJoe Perches# do {} while (0) macro tests: 5133b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5134b13edf7fSJoe Perches# macro should not end with a semicolon 51355b57980dSJoe Perches if ($perl_version_ok && 5136b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5137b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5138b13edf7fSJoe Perches my $ln = $linenr; 5139b13edf7fSJoe Perches my $cnt = $realcnt; 5140b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5141b13edf7fSJoe Perches my $ctx = ''; 5142b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5143b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5144b13edf7fSJoe Perches $ctx = $dstat; 5145b13edf7fSJoe Perches 5146b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 51471b36b201SJoe Perches $dstat =~ s/$;/ /g; 5148b13edf7fSJoe Perches 5149b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5150b13edf7fSJoe Perches my $stmts = $2; 5151b13edf7fSJoe Perches my $semis = $3; 5152b13edf7fSJoe Perches 5153b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5154b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5155e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5156b13edf7fSJoe Perches 5157ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5158ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5159b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5160b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5161b13edf7fSJoe Perches } 5162b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5163b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5164b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5165b13edf7fSJoe Perches } 5166f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5167f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5168f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5169e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5170f5ef95b1SJoe Perches 5171f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5172f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5173b13edf7fSJoe Perches } 5174b13edf7fSJoe Perches } 5175b13edf7fSJoe Perches 5176f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 517713214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 517813214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5179cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 518013214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5181cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5182cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5183aad4f614SJoe Perches my @allowed = (); 5184aad4f614SJoe Perches my $allow = 0; 518513214adfSAndy Whitcroft my $seen = 0; 5186773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5187cf655043SAndy Whitcroft my $ln = $linenr - 1; 518813214adfSAndy Whitcroft for my $chunk (@chunks) { 518913214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 519013214adfSAndy Whitcroft 5191773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5192773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5193773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5194773647a0SAndy Whitcroft 5195aad4f614SJoe Perches $allowed[$allow] = 0; 5196773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5197773647a0SAndy Whitcroft 5198773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5199773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5200773647a0SAndy Whitcroft 5201773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5202cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5203cf655043SAndy Whitcroft 5204773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 520513214adfSAndy Whitcroft 520613214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 520713214adfSAndy Whitcroft 5208aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5209cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5210cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5211aad4f614SJoe Perches $allowed[$allow] = 1; 521213214adfSAndy Whitcroft } 521313214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5214cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5215aad4f614SJoe Perches $allowed[$allow] = 1; 521613214adfSAndy Whitcroft } 5217cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5218cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5219aad4f614SJoe Perches $allowed[$allow] = 1; 522013214adfSAndy Whitcroft } 5221aad4f614SJoe Perches $allow++; 522213214adfSAndy Whitcroft } 5223aad4f614SJoe Perches if ($seen) { 5224aad4f614SJoe Perches my $sum_allowed = 0; 5225aad4f614SJoe Perches foreach (@allowed) { 5226aad4f614SJoe Perches $sum_allowed += $_; 5227aad4f614SJoe Perches } 5228aad4f614SJoe Perches if ($sum_allowed == 0) { 5229000d1cc1SJoe Perches WARN("BRACES", 5230000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5231aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5232aad4f614SJoe Perches $seen != $allow) { 5233aad4f614SJoe Perches CHK("BRACES", 5234aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5235aad4f614SJoe Perches } 523613214adfSAndy Whitcroft } 523713214adfSAndy Whitcroft } 523813214adfSAndy Whitcroft } 5239773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 524013214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5241cf655043SAndy Whitcroft my $allowed = 0; 5242f0a594c1SAndy Whitcroft 5243cf655043SAndy Whitcroft # Check the pre-context. 5244cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5245cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5246cf655043SAndy Whitcroft $allowed = 1; 5247f0a594c1SAndy Whitcroft } 5248773647a0SAndy Whitcroft 5249773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5250773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5251773647a0SAndy Whitcroft 5252cf655043SAndy Whitcroft # Check the condition. 5253cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5254773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5255cf655043SAndy Whitcroft if (defined $cond) { 5256773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5257cf655043SAndy Whitcroft } 5258cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5259cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5260cf655043SAndy Whitcroft $allowed = 1; 5261cf655043SAndy Whitcroft } 5262cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5263cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5264cf655043SAndy Whitcroft $allowed = 1; 5265cf655043SAndy Whitcroft } 5266cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5267cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5268cf655043SAndy Whitcroft $allowed = 1; 5269cf655043SAndy Whitcroft } 5270cf655043SAndy Whitcroft # Check the post-context. 5271cf655043SAndy Whitcroft if (defined $chunks[1]) { 5272cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5273cf655043SAndy Whitcroft if (defined $cond) { 5274773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5275cf655043SAndy Whitcroft } 5276cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5277cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5278cf655043SAndy Whitcroft $allowed = 1; 5279cf655043SAndy Whitcroft } 5280cf655043SAndy Whitcroft } 5281cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5282f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5283e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5284cf655043SAndy Whitcroft 5285000d1cc1SJoe Perches WARN("BRACES", 5286000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5287f0a594c1SAndy Whitcroft } 5288f0a594c1SAndy Whitcroft } 5289f0a594c1SAndy Whitcroft 5290e4c5babdSJoe Perches# check for single line unbalanced braces 529195330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 529295330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5293e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5294e4c5babdSJoe Perches } 5295e4c5babdSJoe Perches 52960979ae66SJoe Perches# check for unnecessary blank lines around braces 529777b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5298f8e58219SJoe Perches if (CHK("BRACES", 5299f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5300f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5301f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5302f8e58219SJoe Perches } 53030979ae66SJoe Perches } 530477b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5305f8e58219SJoe Perches if (CHK("BRACES", 5306f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5307f8e58219SJoe Perches $fix) { 5308f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5309f8e58219SJoe Perches } 53100979ae66SJoe Perches } 53110979ae66SJoe Perches 53124a0df2efSAndy Whitcroft# no volatiles please 53136c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 53146c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5315000d1cc1SJoe Perches WARN("VOLATILE", 53168c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 53174a0df2efSAndy Whitcroft } 53184a0df2efSAndy Whitcroft 53195e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 53205e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 53215e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 53225e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 532333acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 53245e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 53255e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 53265e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 53275e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 53285e4f6ba5SJoe Perches $fix && 53295e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 53305e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 53315e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 53325e4f6ba5SJoe Perches my $comma_close = ""; 53335e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 53345e4f6ba5SJoe Perches $comma_close = $1; 53355e4f6ba5SJoe Perches } 53365e4f6ba5SJoe Perches 53375e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 53385e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 53395e4f6ba5SJoe Perches my $fixedline = $prevrawline; 53405e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 53415e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 53425e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 53435e4f6ba5SJoe Perches $fixedline = $rawline; 53445e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 53455e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 53465e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 53475e4f6ba5SJoe Perches } 53485e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 53495e4f6ba5SJoe Perches } 53505e4f6ba5SJoe Perches } 53515e4f6ba5SJoe Perches 53525e4f6ba5SJoe Perches# check for missing a space in a string concatenation 53535e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 53545e4f6ba5SJoe Perches WARN('MISSING_SPACE', 53555e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 53565e4f6ba5SJoe Perches } 53575e4f6ba5SJoe Perches 535877cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5359e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5360e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5361e4b7d309SJoe Perches# function declarations 536277cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 536377cb8546SJoe Perches defined($context_function) && 5364e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5365e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 536677cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5367e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 536877cb8546SJoe Perches } 536977cb8546SJoe Perches 53705e4f6ba5SJoe Perches# check for spaces before a quoted newline 53715e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 53725e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 53735e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 53745e4f6ba5SJoe Perches $fix) { 53755e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 53765e4f6ba5SJoe Perches } 53775e4f6ba5SJoe Perches 53785e4f6ba5SJoe Perches } 53795e4f6ba5SJoe Perches 5380f17dba4fSJoe Perches# concatenated string without spaces between elements 538179682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 538279682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 538379682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 538479682c0cSJoe Perches $fix) { 538579682c0cSJoe Perches while ($line =~ /($String)/g) { 538679682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 538779682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 538879682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 538979682c0cSJoe Perches } 539079682c0cSJoe Perches } 5391f17dba4fSJoe Perches } 5392f17dba4fSJoe Perches 539390ad30e5SJoe Perches# uncoalesced string fragments 539433acb54aSJoe Perches if ($line =~ /$String\s*"/) { 539579682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 539679682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 539779682c0cSJoe Perches $fix) { 539879682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 539979682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 540079682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 540179682c0cSJoe Perches } 540279682c0cSJoe Perches } 540390ad30e5SJoe Perches } 540490ad30e5SJoe Perches 5405522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5406522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5407522b837cSAlexey Dobriyan my $show_Z = 1; 54085e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5409522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 54105e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5411522b837cSAlexey Dobriyan # check for %L 5412522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 54135e4f6ba5SJoe Perches WARN("PRINTF_L", 5414522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5415522b837cSAlexey Dobriyan $show_L = 0; 54165e4f6ba5SJoe Perches } 5417522b837cSAlexey Dobriyan # check for %Z 5418522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5419522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5420522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5421522b837cSAlexey Dobriyan $show_Z = 0; 5422522b837cSAlexey Dobriyan } 5423522b837cSAlexey Dobriyan # check for 0x<decimal> 5424522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5425522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 54266e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 54276e300757SJoe Perches } 54285e4f6ba5SJoe Perches } 54295e4f6ba5SJoe Perches 54305e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 54313f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 54325e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 54335e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 54345e4f6ba5SJoe Perches } 54355e4f6ba5SJoe Perches 543600df344fSAndy Whitcroft# warn about #if 0 5437c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 543860f89010SPrakruthi Deepak Heragu WARN("IF_0", 543960f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 544060f89010SPrakruthi Deepak Heragu } 544160f89010SPrakruthi Deepak Heragu 544260f89010SPrakruthi Deepak Heragu# warn about #if 1 544360f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 544460f89010SPrakruthi Deepak Heragu WARN("IF_1", 544560f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 54464a0df2efSAndy Whitcroft } 54474a0df2efSAndy Whitcroft 544803df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 544903df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5450100425deSJoe Perches my $tested = quotemeta($1); 5451100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5452100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5453100425deSJoe Perches my $func = $1; 5454100425deSJoe Perches if (WARN('NEEDLESS_IF', 5455100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5456100425deSJoe Perches $fix) { 5457100425deSJoe Perches my $do_fix = 1; 5458100425deSJoe Perches my $leading_tabs = ""; 5459100425deSJoe Perches my $new_leading_tabs = ""; 5460100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5461100425deSJoe Perches $leading_tabs = $1; 5462100425deSJoe Perches } else { 5463100425deSJoe Perches $do_fix = 0; 5464100425deSJoe Perches } 5465100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5466100425deSJoe Perches $new_leading_tabs = $1; 5467100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5468100425deSJoe Perches $do_fix = 0; 5469100425deSJoe Perches } 5470100425deSJoe Perches } else { 5471100425deSJoe Perches $do_fix = 0; 5472100425deSJoe Perches } 5473100425deSJoe Perches if ($do_fix) { 5474100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5475100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5476100425deSJoe Perches } 5477100425deSJoe Perches } 54784c432a8fSGreg Kroah-Hartman } 54794c432a8fSGreg Kroah-Hartman } 5480f0a594c1SAndy Whitcroft 5481ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5482ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5483ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5484ebfdc409SJoe Perches (defined $1 || defined $3) && 5485ebfdc409SJoe Perches $linenr > 3) { 5486ebfdc409SJoe Perches my $testval = $2; 5487ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5488ebfdc409SJoe Perches 5489ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5490ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5491ebfdc409SJoe Perches 5492fb0d0e08SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) { 5493ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5494ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5495ebfdc409SJoe Perches } 5496ebfdc409SJoe Perches } 5497ebfdc409SJoe Perches 5498f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5499dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5500f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5501f78d98f6SJoe Perches my $level = $1; 5502f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5503f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5504f78d98f6SJoe Perches $fix) { 5505f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5506f78d98f6SJoe Perches } 5507f78d98f6SJoe Perches } 5508f78d98f6SJoe Perches 550945c55e92SJoe Perches# check for logging continuations 551045c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 551145c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 551245c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 551345c55e92SJoe Perches } 551445c55e92SJoe Perches 5515abb08a53SJoe Perches# check for mask then right shift without a parentheses 55165b57980dSJoe Perches if ($perl_version_ok && 5517abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5518abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5519abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5520abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5521abb08a53SJoe Perches } 5522abb08a53SJoe Perches 5523b75ac618SJoe Perches# check for pointer comparisons to NULL 55245b57980dSJoe Perches if ($perl_version_ok) { 5525b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5526b75ac618SJoe Perches my $val = $1; 5527b75ac618SJoe Perches my $equal = "!"; 5528b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5529b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5530b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5531b75ac618SJoe Perches $fix) { 5532b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5533b75ac618SJoe Perches } 5534b75ac618SJoe Perches } 5535b75ac618SJoe Perches } 5536b75ac618SJoe Perches 55378716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 55388716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 55398716de38SJoe Perches my $attr = $1; 55408716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 55418716de38SJoe Perches my $ptr = $1; 55428716de38SJoe Perches my $var = $2; 55438716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 55448716de38SJoe Perches ERROR("MISPLACED_INIT", 55458716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 55468716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 55478716de38SJoe Perches WARN("MISPLACED_INIT", 55488716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 55498716de38SJoe Perches $fix) { 5550194f66fcSJoe 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; 55518716de38SJoe Perches } 55528716de38SJoe Perches } 55538716de38SJoe Perches } 55548716de38SJoe Perches 5555e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5556e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5557e970b884SJoe Perches my $attr = $1; 5558e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5559e970b884SJoe Perches my $attr_prefix = $1; 5560e970b884SJoe Perches my $attr_type = $2; 5561e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5562e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5563e970b884SJoe Perches $fix) { 5564194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5565e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5566e970b884SJoe Perches } 5567e970b884SJoe Perches } 5568e970b884SJoe Perches 5569e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5570e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5571e970b884SJoe Perches my $attr = $1; 5572e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5573e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5574e970b884SJoe Perches $fix) { 5575194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5576e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5577e970b884SJoe Perches $lead = rtrim($1); 5578e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5579e970b884SJoe Perches $lead = "${lead}const "; 5580194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5581e970b884SJoe Perches } 5582e970b884SJoe Perches } 5583e970b884SJoe Perches 5584c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5585c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5586c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5587c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5588c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5589c17893c7SJoe Perches $fix) { 5590c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5591c17893c7SJoe Perches } 5592c17893c7SJoe Perches } 5593c17893c7SJoe Perches 5594fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5595fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5596fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5597fbdb8138SJoe Perches my $constant_func = $1; 5598fbdb8138SJoe Perches my $func = $constant_func; 5599fbdb8138SJoe Perches $func =~ s/^__constant_//; 5600fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5601fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5602fbdb8138SJoe Perches $fix) { 5603194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5604fbdb8138SJoe Perches } 5605fbdb8138SJoe Perches } 5606fbdb8138SJoe Perches 56071a15a250SPatrick Pannuto# prefer usleep_range over udelay 560837581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 560943c1d77cSJoe Perches my $delay = $1; 56101a15a250SPatrick Pannuto # ignore udelay's < 10, however 561143c1d77cSJoe Perches if (! ($delay < 10) ) { 5612000d1cc1SJoe Perches CHK("USLEEP_RANGE", 561343c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 561443c1d77cSJoe Perches } 561543c1d77cSJoe Perches if ($delay > 2000) { 561643c1d77cSJoe Perches WARN("LONG_UDELAY", 561743c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 56181a15a250SPatrick Pannuto } 56191a15a250SPatrick Pannuto } 56201a15a250SPatrick Pannuto 562109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 562209ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 562309ef8725SPatrick Pannuto if ($1 < 20) { 5624000d1cc1SJoe Perches WARN("MSLEEP", 562543c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 562609ef8725SPatrick Pannuto } 562709ef8725SPatrick Pannuto } 562809ef8725SPatrick Pannuto 562936ec1939SJoe Perches# check for comparisons of jiffies 563036ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 563136ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 563236ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 563336ec1939SJoe Perches } 563436ec1939SJoe Perches 56359d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 56369d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 56379d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 56389d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 56399d7a34a5SJoe Perches } 56409d7a34a5SJoe Perches 564100df344fSAndy Whitcroft# warn about #ifdefs in C files 5642c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 564300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 564400df344fSAndy Whitcroft# print "$herecurr"; 564500df344fSAndy Whitcroft# $clean = 0; 564600df344fSAndy Whitcroft# } 564700df344fSAndy Whitcroft 564822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5649c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 56503705ce5bSJoe Perches if (ERROR("SPACING", 56513705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 56523705ce5bSJoe Perches $fix) { 5653194f66fcSJoe Perches $fixed[$fixlinenr] =~ 56543705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 56553705ce5bSJoe Perches } 56563705ce5bSJoe Perches 565722f2a2efSAndy Whitcroft } 565822f2a2efSAndy Whitcroft 56594a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5660171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5661171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 56624a0df2efSAndy Whitcroft my $which = $1; 56634a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5664000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5665000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 56664a0df2efSAndy Whitcroft } 56674a0df2efSAndy Whitcroft } 56684a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5669402c2553SMichael S. Tsirkin 5670402c2553SMichael S. Tsirkin my $barriers = qr{ 5671402c2553SMichael S. Tsirkin mb| 5672402c2553SMichael S. Tsirkin rmb| 5673402c2553SMichael S. Tsirkin wmb| 5674402c2553SMichael S. Tsirkin read_barrier_depends 5675402c2553SMichael S. Tsirkin }x; 5676402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5677402c2553SMichael S. Tsirkin mb__before_atomic| 5678402c2553SMichael S. Tsirkin mb__after_atomic| 5679402c2553SMichael S. Tsirkin store_release| 5680402c2553SMichael S. Tsirkin load_acquire| 5681402c2553SMichael S. Tsirkin store_mb| 5682402c2553SMichael S. Tsirkin (?:$barriers) 5683402c2553SMichael S. Tsirkin }x; 5684402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5685402c2553SMichael S. Tsirkin (?:$barriers)| 568643e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 568743e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5688402c2553SMichael S. Tsirkin }x; 5689402c2553SMichael S. Tsirkin 5690402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 56914a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5692c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5693000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 56944a0df2efSAndy Whitcroft } 56954a0df2efSAndy Whitcroft } 56963ad81779SPaul E. McKenney 5697f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5698f4073b0fSMichael S. Tsirkin 5699f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5700f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5701f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5702f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5703f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5704f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5705f4073b0fSMichael S. Tsirkin } 5706f4073b0fSMichael S. Tsirkin 5707cb426e99SJoe Perches# check for waitqueue_active without a comment. 5708cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5709cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5710cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5711cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5712cb426e99SJoe Perches } 5713cb426e99SJoe Perches } 57143ad81779SPaul E. McKenney 571591db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends 571691db2592SPaul E. McKenney if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { 571791db2592SPaul E. McKenney WARN("READ_BARRIER_DEPENDS", 571891db2592SPaul E. McKenney "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); 571991db2592SPaul E. McKenney } 572091db2592SPaul E. McKenney 57214a0df2efSAndy Whitcroft# check of hardware specific defines 5722c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5723000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5724000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 57250a920b5bSAndy Whitcroft } 5726653d4876SAndy Whitcroft 5727596ed45bSJoe Perches# check that the storage class is not after a type 5728596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5729000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5730596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5731596ed45bSJoe Perches } 5732596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5733596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5734596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5735596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5736596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5737596ed45bSJoe Perches WARN("STORAGE_CLASS", 5738596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5739d4977c78STobias Klauser } 5740d4977c78STobias Klauser 5741de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5742de7d4f0eSAndy Whitcroft# storage class and type. 57439c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 57449c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5745000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5746000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5747de7d4f0eSAndy Whitcroft } 5748de7d4f0eSAndy Whitcroft 57498905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 57502b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57512b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5752d5e616fcSJoe Perches if (WARN("INLINE", 5753d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5754d5e616fcSJoe Perches $fix) { 5755194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5756d5e616fcSJoe Perches 5757d5e616fcSJoe Perches } 57588905a67cSAndy Whitcroft } 57598905a67cSAndy Whitcroft 57603d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 57612b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57622b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5763000d1cc1SJoe Perches WARN("PREFER_PACKED", 5764000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 57653d130fd0SJoe Perches } 57663d130fd0SJoe Perches 576739b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 57682b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57692b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5770000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5771000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 577239b7e287SJoe Perches } 577339b7e287SJoe Perches 57745f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 57752b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57762b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5777d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5778d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5779d5e616fcSJoe Perches $fix) { 5780194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5781d5e616fcSJoe Perches 5782d5e616fcSJoe Perches } 57835f14d3bdSJoe Perches } 57845f14d3bdSJoe Perches 57856061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 57862b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57872b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5788d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5789d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5790d5e616fcSJoe Perches $fix) { 5791194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5792d5e616fcSJoe Perches } 57936061d949SJoe Perches } 57946061d949SJoe Perches 5795619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 57965b57980dSJoe Perches if ($perl_version_ok && 5797619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5798619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5799619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5800619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5801619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5802619a908aSJoe Perches } 5803619a908aSJoe Perches 5804fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 5805e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5806fd39f904STomas Winkler $realfile !~ m@\btools/@ && 5807e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5808e6176fa4SJoe Perches my $type = $1; 5809e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5810e6176fa4SJoe Perches $type = $1; 5811e6176fa4SJoe Perches my $kernel_type = 'u'; 5812e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5813e6176fa4SJoe Perches $type =~ /(\d+)/; 5814e6176fa4SJoe Perches $kernel_type .= $1; 5815e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5816e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5817e6176fa4SJoe Perches $fix) { 5818e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5819e6176fa4SJoe Perches } 5820e6176fa4SJoe Perches } 5821e6176fa4SJoe Perches } 5822e6176fa4SJoe Perches 5823938224b5SJoe Perches# check for cast of C90 native int or longer types constants 5824938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 5825938224b5SJoe Perches my $cast = $1; 5826938224b5SJoe Perches my $const = $2; 5827938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 5828938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 5829938224b5SJoe Perches $fix) { 5830938224b5SJoe Perches my $suffix = ""; 5831938224b5SJoe Perches my $newconst = $const; 5832938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 5833938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 5834938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 5835938224b5SJoe Perches $suffix .= 'LL'; 5836938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 5837938224b5SJoe Perches $suffix .= 'L'; 5838938224b5SJoe Perches } 5839938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 5840938224b5SJoe Perches } 5841938224b5SJoe Perches } 5842938224b5SJoe Perches 58438f53a9b8SJoe Perches# check for sizeof(&) 58448f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5845000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5846000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 58478f53a9b8SJoe Perches } 58488f53a9b8SJoe Perches 584966c80b60SJoe Perches# check for sizeof without parenthesis 585066c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 5851d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 5852d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 5853d5e616fcSJoe Perches $fix) { 5854194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 5855d5e616fcSJoe Perches } 585666c80b60SJoe Perches } 585766c80b60SJoe Perches 585888982feaSJoe Perches# check for struct spinlock declarations 585988982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 586088982feaSJoe Perches WARN("USE_SPINLOCK_T", 586188982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 586288982feaSJoe Perches } 586388982feaSJoe Perches 5864a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 586506668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 5866a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 5867caac1d5fSHeba Aamer $fmt =~ s/%%//g; 5868caac1d5fSHeba Aamer if ($fmt !~ /%/) { 5869d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 5870d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 5871d5e616fcSJoe Perches $fix) { 5872194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 5873d5e616fcSJoe Perches } 5874a6962d72SJoe Perches } 5875a6962d72SJoe Perches } 5876a6962d72SJoe Perches 58770b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 58785b57980dSJoe Perches if ($perl_version_ok && 58790b523769SJoe Perches defined $stat && 58800b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 58810b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 5882e3c6bc95STobin C. Harding my $stat_real; 5883e3c6bc95STobin C. Harding 58840b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 58850b523769SJoe Perches $lc = $lc + $linenr; 58860b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 5887ffe07513SJoe Perches my $specifier; 5888ffe07513SJoe Perches my $extension; 5889ffe07513SJoe Perches my $bad_specifier = ""; 58900b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 58910b523769SJoe Perches $fmt =~ s/%%//g; 5892e3c6bc95STobin C. Harding 5893e3c6bc95STobin C. Harding while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) { 5894e3c6bc95STobin C. Harding $specifier = $1; 5895e3c6bc95STobin C. Harding $extension = $2; 5896e3c6bc95STobin C. Harding if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) { 5897e3c6bc95STobin C. Harding $bad_specifier = $specifier; 58980b523769SJoe Perches last; 58990b523769SJoe Perches } 5900e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 5901e3c6bc95STobin C. Harding if (!defined($stat_real)) { 5902e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 59030b523769SJoe Perches } 5904e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 5905e3c6bc95STobin 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"); 5906e3c6bc95STobin C. Harding } 5907e3c6bc95STobin C. Harding } 5908e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 59092a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 59101df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 59111df7338aSSergey Senozhatsky my $use = ""; 5912e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 59131df7338aSSergey Senozhatsky $ext_type = "Deprecated"; 59141df7338aSSergey Senozhatsky $use = " - use %pS instead"; 5915e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 59161df7338aSSergey Senozhatsky } 59172a9f9d85STobin C. Harding 59180b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 5919e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 5920e3c6bc95STobin C. Harding } 59210b523769SJoe Perches } 59220b523769SJoe Perches } 59230b523769SJoe Perches 5924554e165cSAndy Whitcroft# Check for misused memsets 59255b57980dSJoe Perches if ($perl_version_ok && 5926d1fe9c09SJoe Perches defined $stat && 59279e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 5928554e165cSAndy Whitcroft 5929d7c76ba7SJoe Perches my $ms_addr = $2; 5930d1fe9c09SJoe Perches my $ms_val = $7; 5931d1fe9c09SJoe Perches my $ms_size = $12; 5932d7c76ba7SJoe Perches 5933554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 5934554e165cSAndy Whitcroft ERROR("MEMSET", 5935d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 5936554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 5937554e165cSAndy Whitcroft WARN("MEMSET", 5938d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 5939d7c76ba7SJoe Perches } 5940d7c76ba7SJoe Perches } 5941d7c76ba7SJoe Perches 594298a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 59435b57980dSJoe Perches# if ($perl_version_ok && 5944f333195dSJoe Perches# defined $stat && 5945f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5946f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 5947f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 5948f333195dSJoe Perches# $fix) { 5949f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 5950f333195dSJoe Perches# } 5951f333195dSJoe Perches# } 595298a9bba5SJoe Perches 5953b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 59545b57980dSJoe Perches# if ($perl_version_ok && 5955f333195dSJoe Perches# defined $stat && 5956f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5957f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 5958f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 5959f333195dSJoe Perches# } 5960b6117d17SMateusz Kulikowski 59618617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 59628617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 59635b57980dSJoe Perches# if ($perl_version_ok && 5964f333195dSJoe Perches# defined $stat && 5965f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5966f333195dSJoe Perches# 5967f333195dSJoe Perches# my $ms_val = $7; 5968f333195dSJoe Perches# 5969f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 5970f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 5971f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 5972f333195dSJoe Perches# $fix) { 5973f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 5974f333195dSJoe Perches# } 5975f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 5976f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 5977f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 5978f333195dSJoe Perches# $fix) { 5979f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 5980f333195dSJoe Perches# } 5981f333195dSJoe Perches# } 5982f333195dSJoe Perches# } 59838617cd09SMateusz Kulikowski 5984d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 59855b57980dSJoe Perches if ($perl_version_ok && 5986d1fe9c09SJoe Perches defined $stat && 5987d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 5988d1fe9c09SJoe Perches if (defined $2 || defined $7) { 5989d7c76ba7SJoe Perches my $call = $1; 5990d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 5991d7c76ba7SJoe Perches my $arg1 = $3; 5992d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 5993d1fe9c09SJoe Perches my $arg2 = $8; 5994d7c76ba7SJoe Perches my $cast; 5995d7c76ba7SJoe Perches 5996d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 5997d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 5998d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 5999d7c76ba7SJoe Perches $cast = $cast1; 6000d7c76ba7SJoe Perches } else { 6001d7c76ba7SJoe Perches $cast = $cast2; 6002d7c76ba7SJoe Perches } 6003d7c76ba7SJoe Perches WARN("MINMAX", 6004d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6005554e165cSAndy Whitcroft } 6006554e165cSAndy Whitcroft } 6007554e165cSAndy Whitcroft 60084a273195SJoe Perches# check usleep_range arguments 60095b57980dSJoe Perches if ($perl_version_ok && 60104a273195SJoe Perches defined $stat && 60114a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 60124a273195SJoe Perches my $min = $1; 60134a273195SJoe Perches my $max = $7; 60144a273195SJoe Perches if ($min eq $max) { 60154a273195SJoe Perches WARN("USLEEP_RANGE", 60164a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 60174a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 60184a273195SJoe Perches $min > $max) { 60194a273195SJoe Perches WARN("USLEEP_RANGE", 60204a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 60214a273195SJoe Perches } 60224a273195SJoe Perches } 60234a273195SJoe Perches 6024823b794cSJoe Perches# check for naked sscanf 60255b57980dSJoe Perches if ($perl_version_ok && 6026823b794cSJoe Perches defined $stat && 60276c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6028823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6029823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6030823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6031823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6032823b794cSJoe Perches $lc = $lc + $linenr; 60332a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6034823b794cSJoe Perches WARN("NAKED_SSCANF", 6035823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6036823b794cSJoe Perches } 6037823b794cSJoe Perches 6038afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 60395b57980dSJoe Perches if ($perl_version_ok && 6040afc819abSJoe Perches defined $stat && 6041afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6042afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6043afc819abSJoe Perches $lc = $lc + $linenr; 60442a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6045afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6046afc819abSJoe Perches my $format = $6; 6047afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6048afc819abSJoe Perches if ($count == 1 && 6049afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6050afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6051afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6052afc819abSJoe Perches } 6053afc819abSJoe Perches } 6054afc819abSJoe Perches } 6055afc819abSJoe Perches 605670dc8a48SJoe Perches# check for new externs in .h files. 605770dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 605870dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6059d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 606070dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 606170dc8a48SJoe Perches $fix) { 6062194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 606370dc8a48SJoe Perches } 606470dc8a48SJoe Perches } 606570dc8a48SJoe Perches 6066de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6067171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6068c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6069171ae1a4SAndy Whitcroft { 6070c45dcabdSAndy Whitcroft my $function_name = $1; 6071c45dcabdSAndy Whitcroft my $paren_space = $2; 6072171ae1a4SAndy Whitcroft 6073171ae1a4SAndy Whitcroft my $s = $stat; 6074171ae1a4SAndy Whitcroft if (defined $cond) { 6075171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6076171ae1a4SAndy Whitcroft } 6077c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 6078c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 6079c45dcabdSAndy Whitcroft { 6080000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6081000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6082de7d4f0eSAndy Whitcroft } 6083de7d4f0eSAndy Whitcroft 6084171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6085000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6086000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6087171ae1a4SAndy Whitcroft } 60889c9ba34eSAndy Whitcroft 60899c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 60909c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 60919c9ba34eSAndy Whitcroft { 6092000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6093000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6094171ae1a4SAndy Whitcroft } 6095171ae1a4SAndy Whitcroft 6096a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6097a0ad7596SJoe Perches if (defined $stat && 609825bdda2bSMiles Chen $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6099ca0d8929SJoe Perches $1 ne "void") { 6100ca0d8929SJoe Perches my $args = trim($1); 6101ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6102ca0d8929SJoe Perches my $arg = trim($1); 6103ca0d8929SJoe Perches if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6104ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6105ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6106ca0d8929SJoe Perches } 6107ca0d8929SJoe Perches } 6108ca0d8929SJoe Perches } 6109ca0d8929SJoe Perches 6110a0ad7596SJoe Perches# check for function definitions 61115b57980dSJoe Perches if ($perl_version_ok && 6112a0ad7596SJoe Perches defined $stat && 6113a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6114a0ad7596SJoe Perches $context_function = $1; 6115a0ad7596SJoe Perches 6116a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6117a0ad7596SJoe Perches my $ok = 0; 6118a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6119a0ad7596SJoe Perches my $herectx = $here . "\n"; 6120a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6121a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6122a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6123a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6124a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6125a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6126a0ad7596SJoe Perches } 6127a0ad7596SJoe Perches if (!$ok) { 6128a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6129a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6130a0ad7596SJoe Perches } 6131a0ad7596SJoe Perches } 6132a0ad7596SJoe Perches 6133de7d4f0eSAndy Whitcroft# checks for new __setup's 6134de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6135de7d4f0eSAndy Whitcroft my $name = $1; 6136de7d4f0eSAndy Whitcroft 6137de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6138000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 61398c27ceffSMauro Carvalho Chehab "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); 6140de7d4f0eSAndy Whitcroft } 6141653d4876SAndy Whitcroft } 61429c0ca6f9SAndy Whitcroft 61439c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 6144caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 6145000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6146000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 61479c0ca6f9SAndy Whitcroft } 614813214adfSAndy Whitcroft 6149a640d25cSJoe Perches# alloc style 6150a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 61515b57980dSJoe Perches if ($perl_version_ok && 6152a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6153a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6154a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6155a640d25cSJoe Perches } 6156a640d25cSJoe Perches 615760a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 61585b57980dSJoe Perches if ($perl_version_ok && 61591b4a2ed4SJoe Perches defined $stat && 61601b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 616160a55369SJoe Perches my $oldfunc = $3; 616260a55369SJoe Perches my $a1 = $4; 616360a55369SJoe Perches my $a2 = $10; 616460a55369SJoe Perches my $newfunc = "kmalloc_array"; 616560a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 616660a55369SJoe Perches my $r1 = $a1; 616760a55369SJoe Perches my $r2 = $a2; 616860a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 616960a55369SJoe Perches $r1 = $a2; 617060a55369SJoe Perches $r2 = $a1; 617160a55369SJoe Perches } 6172e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6173e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 61741b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6175e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6176e3d95a2aSTobin C. Harding 6177e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 61781b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 61791b4a2ed4SJoe Perches $cnt == 1 && 6180e367455aSJoe Perches $fix) { 6181194f66fcSJoe 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; 618260a55369SJoe Perches } 618360a55369SJoe Perches } 618460a55369SJoe Perches } 618560a55369SJoe Perches 6186972fdea2SJoe Perches# check for krealloc arg reuse 61875b57980dSJoe Perches if ($perl_version_ok && 61884cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 61894cab63ceSJoe Perches $1 eq $3) { 6190972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6191972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6192972fdea2SJoe Perches } 6193972fdea2SJoe Perches 61945ce59ae0SJoe Perches# check for alloc argument mismatch 61955ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 61965ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 61975ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 61985ce59ae0SJoe Perches } 61995ce59ae0SJoe Perches 6200caf2a54fSJoe Perches# check for multiple semicolons 6201caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6202d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6203d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6204d5e616fcSJoe Perches $fix) { 6205194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6206d5e616fcSJoe Perches } 6207d1e2ad07SJoe Perches } 6208d1e2ad07SJoe Perches 6209cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6210cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6211cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 62120ab90191SJoe Perches my $ull = ""; 62130ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 62140ab90191SJoe Perches if (CHK("BIT_MACRO", 62150ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 62160ab90191SJoe Perches $fix) { 62170ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 62180ab90191SJoe Perches } 62190ab90191SJoe Perches } 62200ab90191SJoe Perches 62212d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 62222d632745SJoe 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*$/) { 62232d632745SJoe Perches my $config = $1; 62242d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 62252d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 62262d632745SJoe Perches $fix) { 62272d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 62282d632745SJoe Perches } 62292d632745SJoe Perches } 62302d632745SJoe Perches 6231e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6232c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6233c34c09a8SJoe Perches my $has_break = 0; 6234c34c09a8SJoe Perches my $has_statement = 0; 6235c34c09a8SJoe Perches my $count = 0; 6236c34c09a8SJoe Perches my $prevline = $linenr; 6237e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6238c34c09a8SJoe Perches $prevline--; 6239c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6240c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6241c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6242c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6243c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6244c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6245c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6246c34c09a8SJoe Perches $has_statement = 1; 6247c34c09a8SJoe Perches $count++; 6248258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6249c34c09a8SJoe Perches } 6250c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6251c34c09a8SJoe Perches WARN("MISSING_BREAK", 6252224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6253c34c09a8SJoe Perches } 6254c34c09a8SJoe Perches } 6255c34c09a8SJoe Perches 6256d1e2ad07SJoe Perches# check for switch/default statements without a break; 62575b57980dSJoe Perches if ($perl_version_ok && 6258d1e2ad07SJoe Perches defined $stat && 6259d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6260d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6261e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6262e3d95a2aSTobin C. Harding 6263d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6264d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6265caf2a54fSJoe Perches } 6266caf2a54fSJoe Perches 626713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6268d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6269d5e616fcSJoe Perches if (WARN("USE_FUNC", 6270d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6271d5e616fcSJoe Perches $fix) { 6272194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6273d5e616fcSJoe Perches } 627413214adfSAndy Whitcroft } 6275773647a0SAndy Whitcroft 627662ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 627762ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 627862ec818fSJoe Perches ERROR("DATE_TIME", 627962ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 628062ec818fSJoe Perches } 628162ec818fSJoe Perches 62822c92488aSJoe Perches# check for use of yield() 62832c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 62842c92488aSJoe Perches WARN("YIELD", 62852c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 62862c92488aSJoe Perches } 62872c92488aSJoe Perches 6288179f8f40SJoe Perches# check for comparisons against true and false 6289179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6290179f8f40SJoe Perches my $lead = $1; 6291179f8f40SJoe Perches my $arg = $2; 6292179f8f40SJoe Perches my $test = $3; 6293179f8f40SJoe Perches my $otype = $4; 6294179f8f40SJoe Perches my $trail = $5; 6295179f8f40SJoe Perches my $op = "!"; 6296179f8f40SJoe Perches 6297179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6298179f8f40SJoe Perches 6299179f8f40SJoe Perches my $type = lc($otype); 6300179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6301179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6302179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6303179f8f40SJoe Perches $op = ""; 6304179f8f40SJoe Perches } 6305179f8f40SJoe Perches 6306179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6307179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6308179f8f40SJoe Perches 6309179f8f40SJoe Perches## maybe suggesting a correct construct would better 6310179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6311179f8f40SJoe Perches 6312179f8f40SJoe Perches } 6313179f8f40SJoe Perches } 6314179f8f40SJoe Perches 63155d430902SJoe Perches# check for bool bitfields 63165d430902SJoe Perches if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) { 63175d430902SJoe Perches WARN("BOOL_BITFIELD", 63185d430902SJoe Perches "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr); 63195d430902SJoe Perches } 63205d430902SJoe Perches 6321d729593eSJoe Perches# check for bool use in .h files 6322d729593eSJoe Perches if ($realfile =~ /\.h$/ && 6323d729593eSJoe Perches $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) { 6324d729593eSJoe Perches CHK("BOOL_MEMBER", 6325d729593eSJoe Perches "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr); 6326d729593eSJoe Perches } 6327d729593eSJoe Perches 63284882720bSThomas Gleixner# check for semaphores initialized locked 63294882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6330000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6331000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6332773647a0SAndy Whitcroft } 63336712d858SJoe Perches 633467d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 633567d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6336000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 633767d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6338773647a0SAndy Whitcroft } 63396712d858SJoe Perches 6340ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6341f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6342000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6343ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6344f3db6639SMichael Ellerman } 63456712d858SJoe Perches 63460f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6347d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 63486903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6349d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6350000d1cc1SJoe Perches WARN("CONST_STRUCT", 6351d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 63522b6db5cbSAndy Whitcroft } 6353773647a0SAndy Whitcroft 6354773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6355773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6356773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6357c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6358c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6359171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6360171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6361171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6362773647a0SAndy Whitcroft { 6363000d1cc1SJoe Perches WARN("NR_CPUS", 6364000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6365773647a0SAndy Whitcroft } 63669c9ba34eSAndy Whitcroft 636752ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 636852ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 636952ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 637052ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 637152ea8506SJoe Perches } 637252ea8506SJoe Perches 6373acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 63745b57980dSJoe Perches if ($perl_version_ok && 6375acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6376acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6377acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6378acd9362cSJoe Perches } 6379acd9362cSJoe Perches 6380691d77b6SAndy Whitcroft# whine mightly about in_atomic 6381691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6382691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6383000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6384000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6385f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6386000d1cc1SJoe Perches WARN("IN_ATOMIC", 6387000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6388691d77b6SAndy Whitcroft } 6389691d77b6SAndy Whitcroft } 63901704f47bSPeter Zijlstra 63910f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 63920f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 63930f5225b0SPeter Zijlstra ERROR("LOCKING", 63940f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 63950f5225b0SPeter Zijlstra } 63960f5225b0SPeter Zijlstra 63971704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 63981704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 63991704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 64001704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 64011704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 64021704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6403000d1cc1SJoe Perches ERROR("LOCKDEP", 6404000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 64051704f47bSPeter Zijlstra } 64061704f47bSPeter Zijlstra } 640788f8831cSDave Jones 6408b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6409b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6410000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6411000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 641288f8831cSDave Jones } 64132435880fSJoe Perches 641400180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 641500180468SJoe Perches# and whether or not function naming is typical and if 641600180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 64175b57980dSJoe Perches if ($perl_version_ok && 641800180468SJoe Perches defined $stat && 641900180468SJoe 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*\)/) { 642000180468SJoe Perches my $var = $1; 642100180468SJoe Perches my $perms = $2; 642200180468SJoe Perches my $show = $3; 642300180468SJoe Perches my $store = $4; 642400180468SJoe Perches my $octal_perms = perms_to_octal($perms); 642500180468SJoe Perches if ($show =~ /^${var}_show$/ && 642600180468SJoe Perches $store =~ /^${var}_store$/ && 642700180468SJoe Perches $octal_perms eq "0644") { 642800180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 642900180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 643000180468SJoe Perches $fix) { 643100180468SJoe 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})/; 643200180468SJoe Perches } 643300180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 643400180468SJoe Perches $store =~ /^NULL$/ && 643500180468SJoe Perches $octal_perms eq "0444") { 643600180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 643700180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 643800180468SJoe Perches $fix) { 643900180468SJoe 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})/; 644000180468SJoe Perches } 644100180468SJoe Perches } elsif ($show =~ /^NULL$/ && 644200180468SJoe Perches $store =~ /^${var}_store$/ && 644300180468SJoe Perches $octal_perms eq "0200") { 644400180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 644500180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 644600180468SJoe Perches $fix) { 644700180468SJoe 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})/; 644800180468SJoe Perches } 644900180468SJoe Perches } elsif ($octal_perms eq "0644" || 645000180468SJoe Perches $octal_perms eq "0444" || 645100180468SJoe Perches $octal_perms eq "0200") { 645200180468SJoe Perches my $newshow = "$show"; 645300180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 645400180468SJoe Perches my $newstore = $store; 645500180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 645600180468SJoe Perches my $rename = ""; 645700180468SJoe Perches if ($show ne $newshow) { 645800180468SJoe Perches $rename .= " '$show' to '$newshow'"; 645900180468SJoe Perches } 646000180468SJoe Perches if ($store ne $newstore) { 646100180468SJoe Perches $rename .= " '$store' to '$newstore'"; 646200180468SJoe Perches } 646300180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 646400180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 646500180468SJoe Perches } else { 646600180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 646700180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 646800180468SJoe Perches } 646900180468SJoe Perches } 647000180468SJoe Perches 6471515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6472515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 647373121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 647473121534SJoe Perches# specific definition of not visible in sysfs. 647573121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 647673121534SJoe Perches# use the default permissions 64775b57980dSJoe Perches if ($perl_version_ok && 6478459cf0aeSJoe Perches defined $stat && 6479515a235eSJoe Perches $line =~ /$mode_perms_search/) { 64802435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 64812435880fSJoe Perches my $func = $entry->[0]; 64822435880fSJoe Perches my $arg_pos = $entry->[1]; 64832435880fSJoe Perches 6484459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6485459cf0aeSJoe Perches $lc = $lc + $linenr; 64862a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6487459cf0aeSJoe Perches 64882435880fSJoe Perches my $skip_args = ""; 64892435880fSJoe Perches if ($arg_pos > 1) { 64902435880fSJoe Perches $arg_pos--; 64912435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 64922435880fSJoe Perches } 6493f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6494459cf0aeSJoe Perches if ($stat =~ /$test/) { 64952435880fSJoe Perches my $val = $1; 64962435880fSJoe Perches $val = $6 if ($skip_args ne ""); 649773121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 649873121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 649973121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 65002435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6501459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6502f90774e1SJoe Perches } 6503f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6504c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6505459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 65062435880fSJoe Perches } 6507459cf0aeSJoe Perches } 6508459cf0aeSJoe Perches } 6509459cf0aeSJoe Perches } 6510459cf0aeSJoe Perches 6511459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6512bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 651300180468SJoe Perches my $oval = $1; 651400180468SJoe Perches my $octal = perms_to_octal($oval); 6515f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6516459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6517f90774e1SJoe Perches $fix) { 651800180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 65192435880fSJoe Perches } 652013214adfSAndy Whitcroft } 65215a6d20ceSBjorn Andersson 65225a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 65235a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 65245a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 65255a6d20ceSBjorn Andersson my $valid_licenses = qr{ 65265a6d20ceSBjorn Andersson GPL| 65275a6d20ceSBjorn Andersson GPL\ v2| 65285a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 65295a6d20ceSBjorn Andersson Dual\ BSD/GPL| 65305a6d20ceSBjorn Andersson Dual\ MIT/GPL| 65315a6d20ceSBjorn Andersson Dual\ MPL/GPL| 65325a6d20ceSBjorn Andersson Proprietary 65335a6d20ceSBjorn Andersson }x; 65345a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 65355a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 65365a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 65375a6d20ceSBjorn Andersson } 65385a6d20ceSBjorn Andersson } 6539515a235eSJoe Perches } 654013214adfSAndy Whitcroft 654113214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 654213214adfSAndy Whitcroft # so just keep quiet. 654313214adfSAndy Whitcroft if ($#rawlines == -1) { 654413214adfSAndy Whitcroft exit(0); 65450a920b5bSAndy Whitcroft } 65460a920b5bSAndy Whitcroft 65478905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 65488905a67cSAndy Whitcroft # things that appear to be patches. 65498905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 65508905a67cSAndy Whitcroft exit(0); 65518905a67cSAndy Whitcroft } 65528905a67cSAndy Whitcroft 65538905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 65548905a67cSAndy Whitcroft # just keep quiet. 65558905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 65568905a67cSAndy Whitcroft exit(0); 65578905a67cSAndy Whitcroft } 65588905a67cSAndy Whitcroft 6559a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6560000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6561000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 65620a920b5bSAndy Whitcroft } 6563cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6564cd261496SGeert Uytterhoeven if ($signoff == 0) { 6565000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6566000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 6567cd261496SGeert Uytterhoeven } elsif (!$authorsignoff) { 6568cd261496SGeert Uytterhoeven WARN("NO_AUTHOR_SIGN_OFF", 6569cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 6570cd261496SGeert Uytterhoeven } 65710a920b5bSAndy Whitcroft } 65720a920b5bSAndy Whitcroft 6573f0a594c1SAndy Whitcroft print report_dump(); 657413214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 657513214adfSAndy Whitcroft print "$filename " if ($summary_file); 65766c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 65776c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 65786c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 65796c72ffaaSAndy Whitcroft } 65808905a67cSAndy Whitcroft 6581d2c0a235SAndy Whitcroft if ($quiet == 0) { 6582ef212196SJoe Perches # If there were any defects found and not already fixing them 6583ef212196SJoe Perches if (!$clean and !$fix) { 6584ef212196SJoe Perches print << "EOM" 6585ef212196SJoe Perches 6586ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6587ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6588ef212196SJoe PerchesEOM 6589ef212196SJoe Perches } 6590d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6591d2c0a235SAndy Whitcroft # then suggest that. 6592d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6593b0781216SMike Frysinger $rpt_cleaners = 0; 6594d8469f16SJoe Perches print << "EOM" 6595d8469f16SJoe Perches 6596d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6597d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6598d8469f16SJoe PerchesEOM 6599d2c0a235SAndy Whitcroft } 6600d2c0a235SAndy Whitcroft } 6601d2c0a235SAndy Whitcroft 6602d752fcc8SJoe Perches if ($clean == 0 && $fix && 6603d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6604d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 66059624b8d6SJoe Perches my $newfile = $filename; 66069624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 66073705ce5bSJoe Perches my $linecount = 0; 66083705ce5bSJoe Perches my $f; 66093705ce5bSJoe Perches 6610d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6611d752fcc8SJoe Perches 66123705ce5bSJoe Perches open($f, '>', $newfile) 66133705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 66143705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 66153705ce5bSJoe Perches $linecount++; 66163705ce5bSJoe Perches if ($file) { 66173705ce5bSJoe Perches if ($linecount > 3) { 66183705ce5bSJoe Perches $fixed_line =~ s/^\+//; 66193705ce5bSJoe Perches print $f $fixed_line . "\n"; 66203705ce5bSJoe Perches } 66213705ce5bSJoe Perches } else { 66223705ce5bSJoe Perches print $f $fixed_line . "\n"; 66233705ce5bSJoe Perches } 66243705ce5bSJoe Perches } 66253705ce5bSJoe Perches close($f); 66263705ce5bSJoe Perches 66273705ce5bSJoe Perches if (!$quiet) { 66283705ce5bSJoe Perches print << "EOM"; 6629d8469f16SJoe Perches 66303705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 66313705ce5bSJoe Perches 66323705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 66333705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 66343705ce5bSJoe Perches 66353705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 66363705ce5bSJoe PerchesNo warranties, expressed or implied... 66373705ce5bSJoe PerchesEOM 66383705ce5bSJoe Perches } 66393705ce5bSJoe Perches } 66403705ce5bSJoe Perches 6641d8469f16SJoe Perches if ($quiet == 0) { 6642d8469f16SJoe Perches print "\n"; 6643d8469f16SJoe Perches if ($clean == 1) { 6644d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6645d8469f16SJoe Perches } else { 6646d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 66470a920b5bSAndy Whitcroft } 66480a920b5bSAndy Whitcroft } 66490a920b5bSAndy Whitcroft return $clean; 66500a920b5bSAndy Whitcroft} 6651