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); 16*cd261496SGeert 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 8523445686aSJoe Perchesmy $camelcase_seeded = 0; 8533445686aSJoe Perchessub seed_camelcase_includes { 8543445686aSJoe Perches return if ($camelcase_seeded); 8553445686aSJoe Perches 8563445686aSJoe Perches my $files; 857c707a81dSJoe Perches my $camelcase_cache = ""; 858c707a81dSJoe Perches my @include_files = (); 859c707a81dSJoe Perches 860c707a81dSJoe Perches $camelcase_seeded = 1; 861351b2a1fSJoe Perches 8623645e328SRichard Genoud if (-e ".git") { 863351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 864351b2a1fSJoe Perches chomp $git_last_include_commit; 865c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 866c707a81dSJoe Perches } else { 867c707a81dSJoe Perches my $last_mod_date = 0; 868c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 869c707a81dSJoe Perches @include_files = split('\n', $files); 870c707a81dSJoe Perches foreach my $file (@include_files) { 871c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 872c707a81dSJoe Perches localtime((stat $file)[9])); 873c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 874c707a81dSJoe Perches } 875c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 876c707a81dSJoe Perches } 877c707a81dSJoe Perches 878c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 879c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 880c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 881351b2a1fSJoe Perches while (<$camelcase_file>) { 882351b2a1fSJoe Perches chomp; 883351b2a1fSJoe Perches $camelcase{$_} = 1; 884351b2a1fSJoe Perches } 885351b2a1fSJoe Perches close($camelcase_file); 886351b2a1fSJoe Perches 887351b2a1fSJoe Perches return; 888351b2a1fSJoe Perches } 889c707a81dSJoe Perches 8903645e328SRichard Genoud if (-e ".git") { 891c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 892c707a81dSJoe Perches @include_files = split('\n', $files); 8933445686aSJoe Perches } 894c707a81dSJoe Perches 8953445686aSJoe Perches foreach my $file (@include_files) { 8963445686aSJoe Perches seed_camelcase_file($file); 8973445686aSJoe Perches } 898351b2a1fSJoe Perches 899c707a81dSJoe Perches if ($camelcase_cache ne "") { 900351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 901c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 902c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 903351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 904351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 905351b2a1fSJoe Perches } 906351b2a1fSJoe Perches close($camelcase_file); 907351b2a1fSJoe Perches } 9083445686aSJoe Perches} 9093445686aSJoe Perches 910d311cd44SJoe Perchessub git_commit_info { 911d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 912d311cd44SJoe Perches 913d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 914d311cd44SJoe Perches 915d311cd44SJoe Perches my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; 916d311cd44SJoe Perches $output =~ s/^\s*//gm; 917d311cd44SJoe Perches my @lines = split("\n", $output); 918d311cd44SJoe Perches 9190d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9200d7835fcSJoe Perches 921d311cd44SJoe Perches if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { 922d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 923d311cd44SJoe Perches# all matching commit ids, but it's very slow... 924d311cd44SJoe Perches# 925d311cd44SJoe Perches# echo "checking commits $1..." 926d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 927d311cd44SJoe Perches# while read line ; do 928d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 929d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 930d311cd44SJoe Perches# done 931d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 932948b133aSHeinrich Schuchardt $id = undef; 933d311cd44SJoe Perches } else { 934d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 935d311cd44SJoe Perches $desc = substr($lines[0], 41); 936d311cd44SJoe Perches } 937d311cd44SJoe Perches 938d311cd44SJoe Perches return ($id, $desc); 939d311cd44SJoe Perches} 940d311cd44SJoe Perches 9416c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 9420a920b5bSAndy Whitcroft 94300df344fSAndy Whitcroftmy @rawlines = (); 944c2fdda0dSAndy Whitcroftmy @lines = (); 9453705ce5bSJoe Perchesmy @fixed = (); 946d752fcc8SJoe Perchesmy @fixed_inserted = (); 947d752fcc8SJoe Perchesmy @fixed_deleted = (); 948194f66fcSJoe Perchesmy $fixlinenr = -1; 949194f66fcSJoe Perches 9504a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 9514a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 9524a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 9534a593c34SDu, Changbin 9544a593c34SDu, Changbinif ($git) { 9554a593c34SDu, Changbin my @commits = (); 9560dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 9574a593c34SDu, Changbin my $git_range; 95828898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 95928898fd1SJoe Perches $git_range = "-$2 $1"; 9604a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 9614a593c34SDu, Changbin $git_range = "$commit_expr"; 9624a593c34SDu, Changbin } else { 9630dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 9640dea9f1eSJoe Perches } 9650dea9f1eSJoe Perches my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 9660dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 96728898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 96828898fd1SJoe Perches next if (!defined($1) || !defined($2)); 9690dea9f1eSJoe Perches my $sha1 = $1; 9700dea9f1eSJoe Perches my $subject = $2; 9710dea9f1eSJoe Perches unshift(@commits, $sha1); 9720dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 9734a593c34SDu, Changbin } 9744a593c34SDu, Changbin } 9754a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 9764a593c34SDu, Changbin @ARGV = @commits; 9774a593c34SDu, Changbin} 9784a593c34SDu, Changbin 979c2fdda0dSAndy Whitcroftmy $vname; 9806c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 98121caa13cSAndy Whitcroft my $FILE; 9824a593c34SDu, Changbin if ($git) { 9834a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 9844a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 9854a593c34SDu, Changbin } elsif ($file) { 98621caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 9876c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 98821caa13cSAndy Whitcroft } elsif ($filename eq '-') { 98921caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 9906c72ffaaSAndy Whitcroft } else { 99121caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 9926c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 9936c72ffaaSAndy Whitcroft } 994c2fdda0dSAndy Whitcroft if ($filename eq '-') { 995c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 9964a593c34SDu, Changbin } elsif ($git) { 9970dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 998c2fdda0dSAndy Whitcroft } else { 999c2fdda0dSAndy Whitcroft $vname = $filename; 1000c2fdda0dSAndy Whitcroft } 100121caa13cSAndy Whitcroft while (<$FILE>) { 10020a920b5bSAndy Whitcroft chomp; 100300df344fSAndy Whitcroft push(@rawlines, $_); 10046c72ffaaSAndy Whitcroft } 100521caa13cSAndy Whitcroft close($FILE); 1006d8469f16SJoe Perches 1007d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1008d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1009d8469f16SJoe Perches print "$vname\n"; 1010d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1011d8469f16SJoe Perches } 1012d8469f16SJoe Perches 1013c2fdda0dSAndy Whitcroft if (!process($filename)) { 10140a920b5bSAndy Whitcroft $exit = 1; 10150a920b5bSAndy Whitcroft } 101600df344fSAndy Whitcroft @rawlines = (); 101713214adfSAndy Whitcroft @lines = (); 10183705ce5bSJoe Perches @fixed = (); 1019d752fcc8SJoe Perches @fixed_inserted = (); 1020d752fcc8SJoe Perches @fixed_deleted = (); 1021194f66fcSJoe Perches $fixlinenr = -1; 1022485ff23eSAlex Dowad @modifierListFile = (); 1023485ff23eSAlex Dowad @typeListFile = (); 1024485ff23eSAlex Dowad build_types(); 10250a920b5bSAndy Whitcroft} 10260a920b5bSAndy Whitcroft 1027d8469f16SJoe Perchesif (!$quiet) { 10283c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 10293c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 10303c816e49SJoe Perches 10315b57980dSJoe Perches if (!$perl_version_ok) { 1032d8469f16SJoe Perches print << "EOM" 1033d8469f16SJoe Perches 1034d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 10355b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1036d8469f16SJoe PerchesEOM 1037d8469f16SJoe Perches } 1038d8469f16SJoe Perches if ($exit) { 1039d8469f16SJoe Perches print << "EOM" 1040d8469f16SJoe Perches 1041d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1042d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1043d8469f16SJoe PerchesEOM 1044d8469f16SJoe Perches } 1045d8469f16SJoe Perches} 1046d8469f16SJoe Perches 10470a920b5bSAndy Whitcroftexit($exit); 10480a920b5bSAndy Whitcroft 10490a920b5bSAndy Whitcroftsub top_of_kernel_tree { 10506c72ffaaSAndy Whitcroft my ($root) = @_; 10516c72ffaaSAndy Whitcroft 10526c72ffaaSAndy Whitcroft my @tree_check = ( 10536c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 10546c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 10556c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 10566c72ffaaSAndy Whitcroft ); 10576c72ffaaSAndy Whitcroft 10586c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 10596c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 10600a920b5bSAndy Whitcroft return 0; 10610a920b5bSAndy Whitcroft } 10626c72ffaaSAndy Whitcroft } 10636c72ffaaSAndy Whitcroft return 1; 10646c72ffaaSAndy Whitcroft} 10650a920b5bSAndy Whitcroft 106620112475SJoe Perchessub parse_email { 106720112475SJoe Perches my ($formatted_email) = @_; 106820112475SJoe Perches 106920112475SJoe Perches my $name = ""; 107020112475SJoe Perches my $address = ""; 107120112475SJoe Perches my $comment = ""; 107220112475SJoe Perches 107320112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 107420112475SJoe Perches $name = $1; 107520112475SJoe Perches $address = $2; 107620112475SJoe Perches $comment = $3 if defined $3; 107720112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 107820112475SJoe Perches $address = $1; 107920112475SJoe Perches $comment = $2 if defined $2; 108020112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 108120112475SJoe Perches $address = $1; 108220112475SJoe Perches $comment = $2 if defined $2; 108385e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 108420112475SJoe Perches $name = $formatted_email; 10853705ce5bSJoe Perches $name = trim($name); 108620112475SJoe Perches $name =~ s/^\"|\"$//g; 108720112475SJoe Perches # If there's a name left after stripping spaces and 108820112475SJoe Perches # leading quotes, and the address doesn't have both 108920112475SJoe Perches # leading and trailing angle brackets, the address 109020112475SJoe Perches # is invalid. ie: 109120112475SJoe Perches # "joe smith [email protected]" bad 109220112475SJoe Perches # "joe smith <[email protected]" bad 109320112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 109420112475SJoe Perches $name = ""; 109520112475SJoe Perches $address = ""; 109620112475SJoe Perches $comment = ""; 109720112475SJoe Perches } 109820112475SJoe Perches } 109920112475SJoe Perches 11003705ce5bSJoe Perches $name = trim($name); 110120112475SJoe Perches $name =~ s/^\"|\"$//g; 11023705ce5bSJoe Perches $address = trim($address); 110320112475SJoe Perches $address =~ s/^\<|\>$//g; 110420112475SJoe Perches 110520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 110620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 110720112475SJoe Perches $name = "\"$name\""; 110820112475SJoe Perches } 110920112475SJoe Perches 111020112475SJoe Perches return ($name, $address, $comment); 111120112475SJoe Perches} 111220112475SJoe Perches 111320112475SJoe Perchessub format_email { 111420112475SJoe Perches my ($name, $address) = @_; 111520112475SJoe Perches 111620112475SJoe Perches my $formatted_email; 111720112475SJoe Perches 11183705ce5bSJoe Perches $name = trim($name); 111920112475SJoe Perches $name =~ s/^\"|\"$//g; 11203705ce5bSJoe Perches $address = trim($address); 112120112475SJoe Perches 112220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 112320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 112420112475SJoe Perches $name = "\"$name\""; 112520112475SJoe Perches } 112620112475SJoe Perches 112720112475SJoe Perches if ("$name" eq "") { 112820112475SJoe Perches $formatted_email = "$address"; 112920112475SJoe Perches } else { 113020112475SJoe Perches $formatted_email = "$name <$address>"; 113120112475SJoe Perches } 113220112475SJoe Perches 113320112475SJoe Perches return $formatted_email; 113420112475SJoe Perches} 113520112475SJoe Perches 1136d311cd44SJoe Perchessub which { 1137d311cd44SJoe Perches my ($bin) = @_; 1138d311cd44SJoe Perches 1139d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1140d311cd44SJoe Perches if (-e "$path/$bin") { 1141d311cd44SJoe Perches return "$path/$bin"; 1142d311cd44SJoe Perches } 1143d311cd44SJoe Perches } 1144d311cd44SJoe Perches 1145d311cd44SJoe Perches return ""; 1146d311cd44SJoe Perches} 1147d311cd44SJoe Perches 1148000d1cc1SJoe Perchessub which_conf { 1149000d1cc1SJoe Perches my ($conf) = @_; 1150000d1cc1SJoe Perches 1151000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1152000d1cc1SJoe Perches if (-e "$path/$conf") { 1153000d1cc1SJoe Perches return "$path/$conf"; 1154000d1cc1SJoe Perches } 1155000d1cc1SJoe Perches } 1156000d1cc1SJoe Perches 1157000d1cc1SJoe Perches return ""; 1158000d1cc1SJoe Perches} 1159000d1cc1SJoe Perches 11600a920b5bSAndy Whitcroftsub expand_tabs { 11610a920b5bSAndy Whitcroft my ($str) = @_; 11620a920b5bSAndy Whitcroft 11630a920b5bSAndy Whitcroft my $res = ''; 11640a920b5bSAndy Whitcroft my $n = 0; 11650a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 11660a920b5bSAndy Whitcroft if ($c eq "\t") { 11670a920b5bSAndy Whitcroft $res .= ' '; 11680a920b5bSAndy Whitcroft $n++; 11690a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 11700a920b5bSAndy Whitcroft $res .= ' '; 11710a920b5bSAndy Whitcroft } 11720a920b5bSAndy Whitcroft next; 11730a920b5bSAndy Whitcroft } 11740a920b5bSAndy Whitcroft $res .= $c; 11750a920b5bSAndy Whitcroft $n++; 11760a920b5bSAndy Whitcroft } 11770a920b5bSAndy Whitcroft 11780a920b5bSAndy Whitcroft return $res; 11790a920b5bSAndy Whitcroft} 11806c72ffaaSAndy Whitcroftsub copy_spacing { 1181773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 11826c72ffaaSAndy Whitcroft return $res; 11836c72ffaaSAndy Whitcroft} 11840a920b5bSAndy Whitcroft 11854a0df2efSAndy Whitcroftsub line_stats { 11864a0df2efSAndy Whitcroft my ($line) = @_; 11874a0df2efSAndy Whitcroft 11884a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 11894a0df2efSAndy Whitcroft $line =~ s/^.//; 11904a0df2efSAndy Whitcroft $line = expand_tabs($line); 11914a0df2efSAndy Whitcroft 11924a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 11934a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 11944a0df2efSAndy Whitcroft 11954a0df2efSAndy Whitcroft return (length($line), length($white)); 11964a0df2efSAndy Whitcroft} 11974a0df2efSAndy Whitcroft 1198773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1199773647a0SAndy Whitcroft 1200773647a0SAndy Whitcroftsub sanitise_line_reset { 1201773647a0SAndy Whitcroft my ($in_comment) = @_; 1202773647a0SAndy Whitcroft 1203773647a0SAndy Whitcroft if ($in_comment) { 1204773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1205773647a0SAndy Whitcroft } else { 1206773647a0SAndy Whitcroft $sanitise_quote = ''; 1207773647a0SAndy Whitcroft } 1208773647a0SAndy Whitcroft} 120900df344fSAndy Whitcroftsub sanitise_line { 121000df344fSAndy Whitcroft my ($line) = @_; 121100df344fSAndy Whitcroft 121200df344fSAndy Whitcroft my $res = ''; 121300df344fSAndy Whitcroft my $l = ''; 121400df344fSAndy Whitcroft 1215c2fdda0dSAndy Whitcroft my $qlen = 0; 1216773647a0SAndy Whitcroft my $off = 0; 1217773647a0SAndy Whitcroft my $c; 121800df344fSAndy Whitcroft 1219773647a0SAndy Whitcroft # Always copy over the diff marker. 1220773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1221773647a0SAndy Whitcroft 1222773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1223773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1224773647a0SAndy Whitcroft 12258d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1226773647a0SAndy Whitcroft # and end, all to $;. 1227773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1228773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1229773647a0SAndy Whitcroft 1230773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1231773647a0SAndy Whitcroft $off++; 123200df344fSAndy Whitcroft next; 1233773647a0SAndy Whitcroft } 123481bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1235773647a0SAndy Whitcroft $sanitise_quote = ''; 1236773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1237773647a0SAndy Whitcroft $off++; 1238773647a0SAndy Whitcroft next; 1239773647a0SAndy Whitcroft } 1240113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1241113f04a8SDaniel Walker $sanitise_quote = '//'; 1242113f04a8SDaniel Walker 1243113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1244113f04a8SDaniel Walker $off++; 1245113f04a8SDaniel Walker next; 1246113f04a8SDaniel Walker } 1247773647a0SAndy Whitcroft 1248773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1249773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1250773647a0SAndy Whitcroft $c eq "\\") { 1251773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1252773647a0SAndy Whitcroft $off++; 1253773647a0SAndy Whitcroft next; 1254773647a0SAndy Whitcroft } 1255773647a0SAndy Whitcroft # Regular quotes. 1256773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1257773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1258773647a0SAndy Whitcroft $sanitise_quote = $c; 1259773647a0SAndy Whitcroft 1260773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1261773647a0SAndy Whitcroft next; 1262773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1263773647a0SAndy Whitcroft $sanitise_quote = ''; 126400df344fSAndy Whitcroft } 126500df344fSAndy Whitcroft } 1266773647a0SAndy Whitcroft 1267fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1268773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1269773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1270113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1271113f04a8SDaniel Walker substr($res, $off, 1, $;); 1272773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1273773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 127400df344fSAndy Whitcroft } else { 1275773647a0SAndy Whitcroft substr($res, $off, 1, $c); 127600df344fSAndy Whitcroft } 1277c2fdda0dSAndy Whitcroft } 1278c2fdda0dSAndy Whitcroft 1279113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1280113f04a8SDaniel Walker $sanitise_quote = ''; 1281113f04a8SDaniel Walker } 1282113f04a8SDaniel Walker 1283c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1284c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1285c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1286c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1287c2fdda0dSAndy Whitcroft 1288c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1289c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1290c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1291c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1292c2fdda0dSAndy Whitcroft } 1293c2fdda0dSAndy Whitcroft 1294dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1295dadf680dSJoe Perches my $match = $1; 1296dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1297dadf680dSJoe Perches } 1298dadf680dSJoe Perches 129900df344fSAndy Whitcroft return $res; 130000df344fSAndy Whitcroft} 130100df344fSAndy Whitcroft 1302a6962d72SJoe Perchessub get_quoted_string { 1303a6962d72SJoe Perches my ($line, $rawline) = @_; 1304a6962d72SJoe Perches 1305478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 130633acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1307a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1308a6962d72SJoe Perches} 1309a6962d72SJoe Perches 13108905a67cSAndy Whitcroftsub ctx_statement_block { 13118905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 13128905a67cSAndy Whitcroft my $line = $linenr - 1; 13138905a67cSAndy Whitcroft my $blk = ''; 13148905a67cSAndy Whitcroft my $soff = $off; 13158905a67cSAndy Whitcroft my $coff = $off - 1; 1316773647a0SAndy Whitcroft my $coff_set = 0; 13178905a67cSAndy Whitcroft 131813214adfSAndy Whitcroft my $loff = 0; 131913214adfSAndy Whitcroft 13208905a67cSAndy Whitcroft my $type = ''; 13218905a67cSAndy Whitcroft my $level = 0; 1322a2750645SAndy Whitcroft my @stack = (); 1323cf655043SAndy Whitcroft my $p; 13248905a67cSAndy Whitcroft my $c; 13258905a67cSAndy Whitcroft my $len = 0; 132613214adfSAndy Whitcroft 132713214adfSAndy Whitcroft my $remainder; 13288905a67cSAndy Whitcroft while (1) { 1329a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1330a2750645SAndy Whitcroft 1331773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 13328905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 13338905a67cSAndy Whitcroft # context. 13348905a67cSAndy Whitcroft if ($off >= $len) { 13358905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1336dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1337c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 13388905a67cSAndy Whitcroft $remain--; 133913214adfSAndy Whitcroft $loff = $len; 1340c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 13418905a67cSAndy Whitcroft $len = length($blk); 13428905a67cSAndy Whitcroft $line++; 13438905a67cSAndy Whitcroft last; 13448905a67cSAndy Whitcroft } 13458905a67cSAndy Whitcroft # Bail if there is no further context. 13468905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 134713214adfSAndy Whitcroft if ($off >= $len) { 13488905a67cSAndy Whitcroft last; 13498905a67cSAndy Whitcroft } 1350f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1351f74bd194SAndy Whitcroft $level++; 1352f74bd194SAndy Whitcroft $type = '#'; 1353f74bd194SAndy Whitcroft } 13548905a67cSAndy Whitcroft } 1355cf655043SAndy Whitcroft $p = $c; 13568905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 135713214adfSAndy Whitcroft $remainder = substr($blk, $off); 13588905a67cSAndy Whitcroft 1359773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 13604635f4fbSAndy Whitcroft 13614635f4fbSAndy Whitcroft # Handle nested #if/#else. 13624635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 13634635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 13644635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 13654635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 13664635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 13674635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 13684635f4fbSAndy Whitcroft } 13694635f4fbSAndy Whitcroft 13708905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 13718905a67cSAndy Whitcroft # outermost level. 13728905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 13738905a67cSAndy Whitcroft last; 13748905a67cSAndy Whitcroft } 13758905a67cSAndy Whitcroft 137613214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1377773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1378773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1379773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1380773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1381773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1382773647a0SAndy Whitcroft $coff_set = 1; 1383773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1384773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 138513214adfSAndy Whitcroft } 138613214adfSAndy Whitcroft 13878905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 13888905a67cSAndy Whitcroft $level++; 13898905a67cSAndy Whitcroft $type = '('; 13908905a67cSAndy Whitcroft } 13918905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 13928905a67cSAndy Whitcroft $level--; 13938905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 13948905a67cSAndy Whitcroft 13958905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 13968905a67cSAndy Whitcroft $coff = $off; 1397773647a0SAndy Whitcroft $coff_set = 1; 1398773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 13998905a67cSAndy Whitcroft } 14008905a67cSAndy Whitcroft } 14018905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 14028905a67cSAndy Whitcroft $level++; 14038905a67cSAndy Whitcroft $type = '{'; 14048905a67cSAndy Whitcroft } 14058905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 14068905a67cSAndy Whitcroft $level--; 14078905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 14088905a67cSAndy Whitcroft 14098905a67cSAndy Whitcroft if ($level == 0) { 1410b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1411b998e001SPatrick Pannuto $off++; 1412b998e001SPatrick Pannuto } 14138905a67cSAndy Whitcroft last; 14148905a67cSAndy Whitcroft } 14158905a67cSAndy Whitcroft } 1416f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1417f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1418f74bd194SAndy Whitcroft $level--; 1419f74bd194SAndy Whitcroft $type = ''; 1420f74bd194SAndy Whitcroft $off++; 1421f74bd194SAndy Whitcroft last; 1422f74bd194SAndy Whitcroft } 14238905a67cSAndy Whitcroft $off++; 14248905a67cSAndy Whitcroft } 1425a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 142613214adfSAndy Whitcroft if ($off == $len) { 1427a3bb97a7SAndy Whitcroft $loff = $len + 1; 142813214adfSAndy Whitcroft $line++; 142913214adfSAndy Whitcroft $remain--; 143013214adfSAndy Whitcroft } 14318905a67cSAndy Whitcroft 14328905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 14338905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 14348905a67cSAndy Whitcroft 14358905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 14368905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 14378905a67cSAndy Whitcroft 1438773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 143913214adfSAndy Whitcroft 144013214adfSAndy Whitcroft return ($statement, $condition, 144113214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 144213214adfSAndy Whitcroft} 144313214adfSAndy Whitcroft 1444cf655043SAndy Whitcroftsub statement_lines { 1445cf655043SAndy Whitcroft my ($stmt) = @_; 1446cf655043SAndy Whitcroft 1447cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1448cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1449cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1450cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1451cf655043SAndy Whitcroft 1452cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1453cf655043SAndy Whitcroft 1454cf655043SAndy Whitcroft return $#stmt_lines + 2; 1455cf655043SAndy Whitcroft} 1456cf655043SAndy Whitcroft 1457cf655043SAndy Whitcroftsub statement_rawlines { 1458cf655043SAndy Whitcroft my ($stmt) = @_; 1459cf655043SAndy Whitcroft 1460cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1461cf655043SAndy Whitcroft 1462cf655043SAndy Whitcroft return $#stmt_lines + 2; 1463cf655043SAndy Whitcroft} 1464cf655043SAndy Whitcroft 1465cf655043SAndy Whitcroftsub statement_block_size { 1466cf655043SAndy Whitcroft my ($stmt) = @_; 1467cf655043SAndy Whitcroft 1468cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1469cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1470cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1471cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1472cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1473cf655043SAndy Whitcroft 1474cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1475cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1476cf655043SAndy Whitcroft 1477cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1478cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1479cf655043SAndy Whitcroft 1480cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1481cf655043SAndy Whitcroft return $stmt_lines; 1482cf655043SAndy Whitcroft } else { 1483cf655043SAndy Whitcroft return $stmt_statements; 1484cf655043SAndy Whitcroft } 1485cf655043SAndy Whitcroft} 1486cf655043SAndy Whitcroft 148713214adfSAndy Whitcroftsub ctx_statement_full { 148813214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 148913214adfSAndy Whitcroft my ($statement, $condition, $level); 149013214adfSAndy Whitcroft 149113214adfSAndy Whitcroft my (@chunks); 149213214adfSAndy Whitcroft 1493cf655043SAndy Whitcroft # Grab the first conditional/block pair. 149413214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 149513214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1496773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 149713214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1498cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1499cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1500cf655043SAndy Whitcroft } 1501cf655043SAndy Whitcroft 1502cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1503cf655043SAndy Whitcroft # could continue the statement. 1504cf655043SAndy Whitcroft for (;;) { 150513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 150613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1507cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1508773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1509cf655043SAndy Whitcroft #print "C: push\n"; 1510cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 151113214adfSAndy Whitcroft } 151213214adfSAndy Whitcroft 151313214adfSAndy Whitcroft return ($level, $linenr, @chunks); 15148905a67cSAndy Whitcroft} 15158905a67cSAndy Whitcroft 15164a0df2efSAndy Whitcroftsub ctx_block_get { 1517f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 15184a0df2efSAndy Whitcroft my $line; 15194a0df2efSAndy Whitcroft my $start = $linenr - 1; 15204a0df2efSAndy Whitcroft my $blk = ''; 15214a0df2efSAndy Whitcroft my @o; 15224a0df2efSAndy Whitcroft my @c; 15234a0df2efSAndy Whitcroft my @res = (); 15244a0df2efSAndy Whitcroft 1525f0a594c1SAndy Whitcroft my $level = 0; 15264635f4fbSAndy Whitcroft my @stack = ($level); 152700df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 152800df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 152900df344fSAndy Whitcroft $remain--; 153000df344fSAndy Whitcroft 153100df344fSAndy Whitcroft $blk .= $rawlines[$line]; 15324635f4fbSAndy Whitcroft 15334635f4fbSAndy Whitcroft # Handle nested #if/#else. 153401464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 15354635f4fbSAndy Whitcroft push(@stack, $level); 153601464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 15374635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 153801464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 15394635f4fbSAndy Whitcroft $level = pop(@stack); 15404635f4fbSAndy Whitcroft } 15414635f4fbSAndy Whitcroft 154201464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1543f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1544f0a594c1SAndy Whitcroft if ($off > 0) { 1545f0a594c1SAndy Whitcroft $off--; 1546f0a594c1SAndy Whitcroft next; 1547f0a594c1SAndy Whitcroft } 15484a0df2efSAndy Whitcroft 1549f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1550f0a594c1SAndy Whitcroft $level--; 1551f0a594c1SAndy Whitcroft last if ($level == 0); 1552f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1553f0a594c1SAndy Whitcroft $level++; 1554f0a594c1SAndy Whitcroft } 1555f0a594c1SAndy Whitcroft } 15564a0df2efSAndy Whitcroft 1557f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 155800df344fSAndy Whitcroft push(@res, $rawlines[$line]); 15594a0df2efSAndy Whitcroft } 15604a0df2efSAndy Whitcroft 1561f0a594c1SAndy Whitcroft last if ($level == 0); 15624a0df2efSAndy Whitcroft } 15634a0df2efSAndy Whitcroft 1564f0a594c1SAndy Whitcroft return ($level, @res); 15654a0df2efSAndy Whitcroft} 15664a0df2efSAndy Whitcroftsub ctx_block_outer { 15674a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 15684a0df2efSAndy Whitcroft 1569f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1570f0a594c1SAndy Whitcroft return @r; 15714a0df2efSAndy Whitcroft} 15724a0df2efSAndy Whitcroftsub ctx_block { 15734a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 15744a0df2efSAndy Whitcroft 1575f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1576f0a594c1SAndy Whitcroft return @r; 1577653d4876SAndy Whitcroft} 1578653d4876SAndy Whitcroftsub ctx_statement { 1579f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1580f0a594c1SAndy Whitcroft 1581f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1582f0a594c1SAndy Whitcroft return @r; 1583f0a594c1SAndy Whitcroft} 1584f0a594c1SAndy Whitcroftsub ctx_block_level { 1585653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1586653d4876SAndy Whitcroft 1587f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 15884a0df2efSAndy Whitcroft} 15899c0ca6f9SAndy Whitcroftsub ctx_statement_level { 15909c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 15919c0ca6f9SAndy Whitcroft 15929c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 15939c0ca6f9SAndy Whitcroft} 15944a0df2efSAndy Whitcroft 15954a0df2efSAndy Whitcroftsub ctx_locate_comment { 15964a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 15974a0df2efSAndy Whitcroft 15984a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1599beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 16004a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 16014a0df2efSAndy Whitcroft 16024a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 16034a0df2efSAndy Whitcroft # comment. 16044a0df2efSAndy Whitcroft my $in_comment = 0; 16054a0df2efSAndy Whitcroft $current_comment = ''; 16064a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 160700df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 160800df344fSAndy Whitcroft #warn " $line\n"; 16094a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 16104a0df2efSAndy Whitcroft $in_comment = 1; 16114a0df2efSAndy Whitcroft } 16124a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 16134a0df2efSAndy Whitcroft $in_comment = 1; 16144a0df2efSAndy Whitcroft } 16154a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 16164a0df2efSAndy Whitcroft $current_comment = ''; 16174a0df2efSAndy Whitcroft } 16184a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 16194a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 16204a0df2efSAndy Whitcroft $in_comment = 0; 16214a0df2efSAndy Whitcroft } 16224a0df2efSAndy Whitcroft } 16234a0df2efSAndy Whitcroft 16244a0df2efSAndy Whitcroft chomp($current_comment); 16254a0df2efSAndy Whitcroft return($current_comment); 16264a0df2efSAndy Whitcroft} 16274a0df2efSAndy Whitcroftsub ctx_has_comment { 16284a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16294a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 16304a0df2efSAndy Whitcroft 163100df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 16324a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 16334a0df2efSAndy Whitcroft 16344a0df2efSAndy Whitcroft return ($cmt ne ''); 16354a0df2efSAndy Whitcroft} 16364a0df2efSAndy Whitcroft 16374d001e4dSAndy Whitcroftsub raw_line { 16384d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 16394d001e4dSAndy Whitcroft 16404d001e4dSAndy Whitcroft my $offset = $linenr - 1; 16414d001e4dSAndy Whitcroft $cnt++; 16424d001e4dSAndy Whitcroft 16434d001e4dSAndy Whitcroft my $line; 16444d001e4dSAndy Whitcroft while ($cnt) { 16454d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 16464d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 16474d001e4dSAndy Whitcroft $cnt--; 16484d001e4dSAndy Whitcroft } 16494d001e4dSAndy Whitcroft 16504d001e4dSAndy Whitcroft return $line; 16514d001e4dSAndy Whitcroft} 16524d001e4dSAndy Whitcroft 16532a9f9d85STobin C. Hardingsub get_stat_real { 16542a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 16552a9f9d85STobin C. Harding 16562a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 16572a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 16582a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 16592a9f9d85STobin C. Harding } 16602a9f9d85STobin C. Harding 16612a9f9d85STobin C. Harding return $stat_real; 16622a9f9d85STobin C. Harding} 16632a9f9d85STobin C. Harding 1664e3d95a2aSTobin C. Hardingsub get_stat_here { 1665e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1666e3d95a2aSTobin C. Harding 1667e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1668e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1669e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1670e3d95a2aSTobin C. Harding } 1671e3d95a2aSTobin C. Harding 1672e3d95a2aSTobin C. Harding return $herectx; 1673e3d95a2aSTobin C. Harding} 1674e3d95a2aSTobin C. Harding 16750a920b5bSAndy Whitcroftsub cat_vet { 16760a920b5bSAndy Whitcroft my ($vet) = @_; 16779c0ca6f9SAndy Whitcroft my ($res, $coded); 16780a920b5bSAndy Whitcroft 16799c0ca6f9SAndy Whitcroft $res = ''; 16806c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 16816c72ffaaSAndy Whitcroft $res .= $1; 16826c72ffaaSAndy Whitcroft if ($2 ne '') { 16839c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 16846c72ffaaSAndy Whitcroft $res .= $coded; 16856c72ffaaSAndy Whitcroft } 16869c0ca6f9SAndy Whitcroft } 16879c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 16880a920b5bSAndy Whitcroft 16899c0ca6f9SAndy Whitcroft return $res; 16900a920b5bSAndy Whitcroft} 16910a920b5bSAndy Whitcroft 1692c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1693cf655043SAndy Whitcroftmy $av_pending; 1694c2fdda0dSAndy Whitcroftmy @av_paren_type; 16951f65f947SAndy Whitcroftmy $av_pend_colon; 1696c2fdda0dSAndy Whitcroft 1697c2fdda0dSAndy Whitcroftsub annotate_reset { 1698c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1699cf655043SAndy Whitcroft $av_pending = '_'; 1700cf655043SAndy Whitcroft @av_paren_type = ('E'); 17011f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1702c2fdda0dSAndy Whitcroft} 1703c2fdda0dSAndy Whitcroft 17046c72ffaaSAndy Whitcroftsub annotate_values { 17056c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 17066c72ffaaSAndy Whitcroft 17076c72ffaaSAndy Whitcroft my $res; 17081f65f947SAndy Whitcroft my $var = '_' x length($stream); 17096c72ffaaSAndy Whitcroft my $cur = $stream; 17106c72ffaaSAndy Whitcroft 1711c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 17126c72ffaaSAndy Whitcroft 17136c72ffaaSAndy Whitcroft while (length($cur)) { 1714773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1715cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1716171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 17176c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1718c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1719c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1720cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1721c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 17226c72ffaaSAndy Whitcroft } 17236c72ffaaSAndy Whitcroft 1724c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 17259446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 17269446ef56SAndy Whitcroft push(@av_paren_type, $type); 1727addcdceaSAndy Whitcroft $type = 'c'; 17289446ef56SAndy Whitcroft 1729e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1730c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 17316c72ffaaSAndy Whitcroft $type = 'T'; 17326c72ffaaSAndy Whitcroft 1733389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1734389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1735389a2fe5SAndy Whitcroft $type = 'T'; 1736389a2fe5SAndy Whitcroft 1737c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1738171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1739c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1740171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1741171ae1a4SAndy Whitcroft if ($2 ne '') { 1742cf655043SAndy Whitcroft $av_pending = 'N'; 1743171ae1a4SAndy Whitcroft } 1744171ae1a4SAndy Whitcroft $type = 'E'; 1745171ae1a4SAndy Whitcroft 1746c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1747171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1748171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1749171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 17506c72ffaaSAndy Whitcroft 1751c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1752cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1753c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1754cf655043SAndy Whitcroft 1755cf655043SAndy Whitcroft push(@av_paren_type, $type); 1756cf655043SAndy Whitcroft push(@av_paren_type, $type); 1757171ae1a4SAndy Whitcroft $type = 'E'; 1758cf655043SAndy Whitcroft 1759c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1760cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1761cf655043SAndy Whitcroft $av_preprocessor = 1; 1762cf655043SAndy Whitcroft 1763cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1764cf655043SAndy Whitcroft 1765171ae1a4SAndy Whitcroft $type = 'E'; 1766cf655043SAndy Whitcroft 1767c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1768cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1769cf655043SAndy Whitcroft 1770cf655043SAndy Whitcroft $av_preprocessor = 1; 1771cf655043SAndy Whitcroft 1772cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1773cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1774cf655043SAndy Whitcroft pop(@av_paren_type); 1775cf655043SAndy Whitcroft push(@av_paren_type, $type); 1776171ae1a4SAndy Whitcroft $type = 'E'; 17776c72ffaaSAndy Whitcroft 17786c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1779c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 17806c72ffaaSAndy Whitcroft 1781171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1782171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1783171ae1a4SAndy Whitcroft $av_pending = $type; 1784171ae1a4SAndy Whitcroft $type = 'N'; 1785171ae1a4SAndy Whitcroft 17866c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1787c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 17886c72ffaaSAndy Whitcroft if (defined $2) { 1789cf655043SAndy Whitcroft $av_pending = 'V'; 17906c72ffaaSAndy Whitcroft } 17916c72ffaaSAndy Whitcroft $type = 'N'; 17926c72ffaaSAndy Whitcroft 179314b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1794c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 179514b111c1SAndy Whitcroft $av_pending = 'E'; 17966c72ffaaSAndy Whitcroft $type = 'N'; 17976c72ffaaSAndy Whitcroft 17981f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 17991f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 18001f65f947SAndy Whitcroft $av_pend_colon = 'C'; 18011f65f947SAndy Whitcroft $type = 'N'; 18021f65f947SAndy Whitcroft 180314b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1804c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 18056c72ffaaSAndy Whitcroft $type = 'N'; 18066c72ffaaSAndy Whitcroft 18076c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1808c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1809cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1810cf655043SAndy Whitcroft $av_pending = '_'; 18116c72ffaaSAndy Whitcroft $type = 'N'; 18126c72ffaaSAndy Whitcroft 18136c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1814cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1815cf655043SAndy Whitcroft if ($new_type ne '_') { 1816cf655043SAndy Whitcroft $type = $new_type; 1817c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1818c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 18196c72ffaaSAndy Whitcroft } else { 1820c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 18216c72ffaaSAndy Whitcroft } 18226c72ffaaSAndy Whitcroft 1823c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1824c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1825c8cb2ca3SAndy Whitcroft $type = 'V'; 1826cf655043SAndy Whitcroft $av_pending = 'V'; 18276c72ffaaSAndy Whitcroft 18288e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 18298e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 18301f65f947SAndy Whitcroft $av_pend_colon = 'B'; 18318e761b04SAndy Whitcroft } elsif ($type eq 'E') { 18328e761b04SAndy Whitcroft $av_pend_colon = 'L'; 18331f65f947SAndy Whitcroft } 18341f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 18351f65f947SAndy Whitcroft $type = 'V'; 18361f65f947SAndy Whitcroft 18376c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1838c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 18396c72ffaaSAndy Whitcroft $type = 'V'; 18406c72ffaaSAndy Whitcroft 18416c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1842c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 18436c72ffaaSAndy Whitcroft $type = 'N'; 18446c72ffaaSAndy Whitcroft 1845cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1846c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 184713214adfSAndy Whitcroft $type = 'E'; 18481f65f947SAndy Whitcroft $av_pend_colon = 'O'; 184913214adfSAndy Whitcroft 18508e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 18518e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 18528e761b04SAndy Whitcroft $type = 'C'; 18538e761b04SAndy Whitcroft 18541f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 18551f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 18561f65f947SAndy Whitcroft $type = 'N'; 18571f65f947SAndy Whitcroft 18581f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 18591f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 18601f65f947SAndy Whitcroft 18611f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 18621f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 18631f65f947SAndy Whitcroft $type = 'E'; 18641f65f947SAndy Whitcroft } else { 18651f65f947SAndy Whitcroft $type = 'N'; 18661f65f947SAndy Whitcroft } 18671f65f947SAndy Whitcroft $av_pend_colon = 'O'; 18681f65f947SAndy Whitcroft 18698e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 187013214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 18716c72ffaaSAndy Whitcroft $type = 'N'; 18726c72ffaaSAndy Whitcroft 18730d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 187474048ed8SAndy Whitcroft my $variant; 187574048ed8SAndy Whitcroft 187674048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 187774048ed8SAndy Whitcroft if ($type eq 'V') { 187874048ed8SAndy Whitcroft $variant = 'B'; 187974048ed8SAndy Whitcroft } else { 188074048ed8SAndy Whitcroft $variant = 'U'; 188174048ed8SAndy Whitcroft } 188274048ed8SAndy Whitcroft 188374048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 188474048ed8SAndy Whitcroft $type = 'N'; 188574048ed8SAndy Whitcroft 18866c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1887c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 18886c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 18896c72ffaaSAndy Whitcroft $type = 'N'; 18906c72ffaaSAndy Whitcroft } 18916c72ffaaSAndy Whitcroft 18926c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1893c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 18946c72ffaaSAndy Whitcroft } 18956c72ffaaSAndy Whitcroft if (defined $1) { 18966c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 18976c72ffaaSAndy Whitcroft $res .= $type x length($1); 18986c72ffaaSAndy Whitcroft } 18996c72ffaaSAndy Whitcroft } 19006c72ffaaSAndy Whitcroft 19011f65f947SAndy Whitcroft return ($res, $var); 19026c72ffaaSAndy Whitcroft} 19036c72ffaaSAndy Whitcroft 19048905a67cSAndy Whitcroftsub possible { 190513214adfSAndy Whitcroft my ($possible, $line) = @_; 19069a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 19070776e594SAndy Whitcroft ^(?: 19080776e594SAndy Whitcroft $Modifier| 19090776e594SAndy Whitcroft $Storage| 19100776e594SAndy Whitcroft $Type| 19119a974fdbSAndy Whitcroft DEFINE_\S+ 19129a974fdbSAndy Whitcroft )$| 19139a974fdbSAndy Whitcroft ^(?: 19140776e594SAndy Whitcroft goto| 19150776e594SAndy Whitcroft return| 19160776e594SAndy Whitcroft case| 19170776e594SAndy Whitcroft else| 19180776e594SAndy Whitcroft asm|__asm__| 191989a88353SAndy Whitcroft do| 192089a88353SAndy Whitcroft \#| 192189a88353SAndy Whitcroft \#\#| 19229a974fdbSAndy Whitcroft )(?:\s|$)| 19230776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 19249a974fdbSAndy Whitcroft )}x; 19259a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 19269a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1927c45dcabdSAndy Whitcroft # Check for modifiers. 1928c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1929c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1930c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1931c45dcabdSAndy Whitcroft 1932c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1933c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1934d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 19359a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1936d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1937485ff23eSAlex Dowad push(@modifierListFile, $modifier); 1938d2506586SAndy Whitcroft } 19399a974fdbSAndy Whitcroft } 1940c45dcabdSAndy Whitcroft 1941c45dcabdSAndy Whitcroft } else { 194213214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1943485ff23eSAlex Dowad push(@typeListFile, $possible); 1944c45dcabdSAndy Whitcroft } 19458905a67cSAndy Whitcroft build_types(); 19460776e594SAndy Whitcroft } else { 19470776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 19488905a67cSAndy Whitcroft } 19498905a67cSAndy Whitcroft} 19508905a67cSAndy Whitcroft 19516c72ffaaSAndy Whitcroftmy $prefix = ''; 19526c72ffaaSAndy Whitcroft 1953000d1cc1SJoe Perchessub show_type { 1954cbec18afSJoe Perches my ($type) = @_; 195591bfe484SJoe Perches 1956522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 1957522b837cSAlexey Dobriyan 1958cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1959cbec18afSJoe Perches 1960cbec18afSJoe Perches return !defined $ignore_type{$type}; 1961000d1cc1SJoe Perches} 1962000d1cc1SJoe Perches 1963f0a594c1SAndy Whitcroftsub report { 1964cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1965cbec18afSJoe Perches 1966cbec18afSJoe Perches if (!show_type($type) || 1967cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1968773647a0SAndy Whitcroft return 0; 1969773647a0SAndy Whitcroft } 197057230297SJoe Perches my $output = ''; 1971737c0767SJohn Brooks if ($color) { 197257230297SJoe Perches if ($level eq 'ERROR') { 197357230297SJoe Perches $output .= RED; 197457230297SJoe Perches } elsif ($level eq 'WARNING') { 197557230297SJoe Perches $output .= YELLOW; 1976000d1cc1SJoe Perches } else { 197757230297SJoe Perches $output .= GREEN; 1978000d1cc1SJoe Perches } 197957230297SJoe Perches } 198057230297SJoe Perches $output .= $prefix . $level . ':'; 198157230297SJoe Perches if ($show_types) { 1982737c0767SJohn Brooks $output .= BLUE if ($color); 198357230297SJoe Perches $output .= "$type:"; 198457230297SJoe Perches } 1985737c0767SJohn Brooks $output .= RESET if ($color); 198657230297SJoe Perches $output .= ' ' . $msg . "\n"; 198734d8815fSJoe Perches 198834d8815fSJoe Perches if ($showfile) { 198934d8815fSJoe Perches my @lines = split("\n", $output, -1); 199034d8815fSJoe Perches splice(@lines, 1, 1); 199134d8815fSJoe Perches $output = join("\n", @lines); 199234d8815fSJoe Perches } 199357230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 19948905a67cSAndy Whitcroft 199557230297SJoe Perches push(our @report, $output); 1996773647a0SAndy Whitcroft 1997773647a0SAndy Whitcroft return 1; 1998f0a594c1SAndy Whitcroft} 1999cbec18afSJoe Perches 2000f0a594c1SAndy Whitcroftsub report_dump { 200113214adfSAndy Whitcroft our @report; 2002f0a594c1SAndy Whitcroft} 2003000d1cc1SJoe Perches 2004d752fcc8SJoe Perchessub fixup_current_range { 2005d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2006d752fcc8SJoe Perches 2007d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2008d752fcc8SJoe Perches my $o = $1; 2009d752fcc8SJoe Perches my $l = $2; 2010d752fcc8SJoe Perches my $no = $o + $offset; 2011d752fcc8SJoe Perches my $nl = $l + $length; 2012d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2013d752fcc8SJoe Perches } 2014d752fcc8SJoe Perches} 2015d752fcc8SJoe Perches 2016d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2017d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2018d752fcc8SJoe Perches 2019d752fcc8SJoe Perches my $range_last_linenr = 0; 2020d752fcc8SJoe Perches my $delta_offset = 0; 2021d752fcc8SJoe Perches 2022d752fcc8SJoe Perches my $old_linenr = 0; 2023d752fcc8SJoe Perches my $new_linenr = 0; 2024d752fcc8SJoe Perches 2025d752fcc8SJoe Perches my $next_insert = 0; 2026d752fcc8SJoe Perches my $next_delete = 0; 2027d752fcc8SJoe Perches 2028d752fcc8SJoe Perches my @lines = (); 2029d752fcc8SJoe Perches 2030d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2031d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2032d752fcc8SJoe Perches 2033d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2034d752fcc8SJoe Perches my $save_line = 1; 2035d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2036323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2037d752fcc8SJoe Perches $delta_offset = 0; 2038d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2039d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2040d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2041d752fcc8SJoe Perches } 2042d752fcc8SJoe Perches 2043d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2044d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2045d752fcc8SJoe Perches $save_line = 0; 2046d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2047d752fcc8SJoe Perches } 2048d752fcc8SJoe Perches 2049d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2050d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2051d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2052d752fcc8SJoe Perches $new_linenr++; 2053d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2054d752fcc8SJoe Perches } 2055d752fcc8SJoe Perches 2056d752fcc8SJoe Perches if ($save_line) { 2057d752fcc8SJoe Perches push(@lines, $line); 2058d752fcc8SJoe Perches $new_linenr++; 2059d752fcc8SJoe Perches } 2060d752fcc8SJoe Perches 2061d752fcc8SJoe Perches $old_linenr++; 2062d752fcc8SJoe Perches } 2063d752fcc8SJoe Perches 2064d752fcc8SJoe Perches return @lines; 2065d752fcc8SJoe Perches} 2066d752fcc8SJoe Perches 2067f2d7e4d4SJoe Perchessub fix_insert_line { 2068f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2069f2d7e4d4SJoe Perches 2070f2d7e4d4SJoe Perches my $inserted = { 2071f2d7e4d4SJoe Perches LINENR => $linenr, 2072f2d7e4d4SJoe Perches LINE => $line, 2073f2d7e4d4SJoe Perches }; 2074f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2075f2d7e4d4SJoe Perches} 2076f2d7e4d4SJoe Perches 2077f2d7e4d4SJoe Perchessub fix_delete_line { 2078f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2079f2d7e4d4SJoe Perches 2080f2d7e4d4SJoe Perches my $deleted = { 2081f2d7e4d4SJoe Perches LINENR => $linenr, 2082f2d7e4d4SJoe Perches LINE => $line, 2083f2d7e4d4SJoe Perches }; 2084f2d7e4d4SJoe Perches 2085f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2086f2d7e4d4SJoe Perches} 2087f2d7e4d4SJoe Perches 2088de7d4f0eSAndy Whitcroftsub ERROR { 2089cbec18afSJoe Perches my ($type, $msg) = @_; 2090cbec18afSJoe Perches 2091cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2092de7d4f0eSAndy Whitcroft our $clean = 0; 20936c72ffaaSAndy Whitcroft our $cnt_error++; 20943705ce5bSJoe Perches return 1; 2095de7d4f0eSAndy Whitcroft } 20963705ce5bSJoe Perches return 0; 2097773647a0SAndy Whitcroft} 2098de7d4f0eSAndy Whitcroftsub WARN { 2099cbec18afSJoe Perches my ($type, $msg) = @_; 2100cbec18afSJoe Perches 2101cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2102de7d4f0eSAndy Whitcroft our $clean = 0; 21036c72ffaaSAndy Whitcroft our $cnt_warn++; 21043705ce5bSJoe Perches return 1; 2105de7d4f0eSAndy Whitcroft } 21063705ce5bSJoe Perches return 0; 2107773647a0SAndy Whitcroft} 2108de7d4f0eSAndy Whitcroftsub CHK { 2109cbec18afSJoe Perches my ($type, $msg) = @_; 2110cbec18afSJoe Perches 2111cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2112de7d4f0eSAndy Whitcroft our $clean = 0; 21136c72ffaaSAndy Whitcroft our $cnt_chk++; 21143705ce5bSJoe Perches return 1; 21156c72ffaaSAndy Whitcroft } 21163705ce5bSJoe Perches return 0; 2117de7d4f0eSAndy Whitcroft} 2118de7d4f0eSAndy Whitcroft 21196ecd9674SAndy Whitcroftsub check_absolute_file { 21206ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 21216ecd9674SAndy Whitcroft my $file = $absolute; 21226ecd9674SAndy Whitcroft 21236ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 21246ecd9674SAndy Whitcroft 21256ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 21266ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 21276ecd9674SAndy Whitcroft if (-f "$root/$file") { 21286ecd9674SAndy Whitcroft ##print "file<$file>\n"; 21296ecd9674SAndy Whitcroft last; 21306ecd9674SAndy Whitcroft } 21316ecd9674SAndy Whitcroft } 21326ecd9674SAndy Whitcroft if (! -f _) { 21336ecd9674SAndy Whitcroft return 0; 21346ecd9674SAndy Whitcroft } 21356ecd9674SAndy Whitcroft 21366ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 21376ecd9674SAndy Whitcroft my $prefix = $absolute; 21386ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 21396ecd9674SAndy Whitcroft 21406ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 21416ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2142000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2143000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 21446ecd9674SAndy Whitcroft } 21456ecd9674SAndy Whitcroft} 21466ecd9674SAndy Whitcroft 21473705ce5bSJoe Perchessub trim { 21483705ce5bSJoe Perches my ($string) = @_; 21493705ce5bSJoe Perches 2150b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2151b34c648bSJoe Perches 2152b34c648bSJoe Perches return $string; 2153b34c648bSJoe Perches} 2154b34c648bSJoe Perches 2155b34c648bSJoe Perchessub ltrim { 2156b34c648bSJoe Perches my ($string) = @_; 2157b34c648bSJoe Perches 2158b34c648bSJoe Perches $string =~ s/^\s+//; 2159b34c648bSJoe Perches 2160b34c648bSJoe Perches return $string; 2161b34c648bSJoe Perches} 2162b34c648bSJoe Perches 2163b34c648bSJoe Perchessub rtrim { 2164b34c648bSJoe Perches my ($string) = @_; 2165b34c648bSJoe Perches 2166b34c648bSJoe Perches $string =~ s/\s+$//; 21673705ce5bSJoe Perches 21683705ce5bSJoe Perches return $string; 21693705ce5bSJoe Perches} 21703705ce5bSJoe Perches 217152ea8506SJoe Perchessub string_find_replace { 217252ea8506SJoe Perches my ($string, $find, $replace) = @_; 217352ea8506SJoe Perches 217452ea8506SJoe Perches $string =~ s/$find/$replace/g; 217552ea8506SJoe Perches 217652ea8506SJoe Perches return $string; 217752ea8506SJoe Perches} 217852ea8506SJoe Perches 21793705ce5bSJoe Perchessub tabify { 21803705ce5bSJoe Perches my ($leading) = @_; 21813705ce5bSJoe Perches 21823705ce5bSJoe Perches my $source_indent = 8; 21833705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 21843705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 21853705ce5bSJoe Perches 21863705ce5bSJoe Perches #convert leading spaces to tabs 21873705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 21883705ce5bSJoe Perches #Remove spaces before a tab 21893705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 21903705ce5bSJoe Perches 21913705ce5bSJoe Perches return "$leading"; 21923705ce5bSJoe Perches} 21933705ce5bSJoe Perches 2194d1fe9c09SJoe Perchessub pos_last_openparen { 2195d1fe9c09SJoe Perches my ($line) = @_; 2196d1fe9c09SJoe Perches 2197d1fe9c09SJoe Perches my $pos = 0; 2198d1fe9c09SJoe Perches 2199d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2200d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2201d1fe9c09SJoe Perches 2202d1fe9c09SJoe Perches my $last_openparen = 0; 2203d1fe9c09SJoe Perches 2204d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2205d1fe9c09SJoe Perches return -1; 2206d1fe9c09SJoe Perches } 2207d1fe9c09SJoe Perches 2208d1fe9c09SJoe Perches my $len = length($line); 2209d1fe9c09SJoe Perches 2210d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2211d1fe9c09SJoe Perches my $string = substr($line, $pos); 2212d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2213d1fe9c09SJoe Perches $pos += length($1) - 1; 2214d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2215d1fe9c09SJoe Perches $last_openparen = $pos; 2216d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2217d1fe9c09SJoe Perches last; 2218d1fe9c09SJoe Perches } 2219d1fe9c09SJoe Perches } 2220d1fe9c09SJoe Perches 222191cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2222d1fe9c09SJoe Perches} 2223d1fe9c09SJoe Perches 22240a920b5bSAndy Whitcroftsub process { 22250a920b5bSAndy Whitcroft my $filename = shift; 22260a920b5bSAndy Whitcroft 22270a920b5bSAndy Whitcroft my $linenr=0; 22280a920b5bSAndy Whitcroft my $prevline=""; 2229c2fdda0dSAndy Whitcroft my $prevrawline=""; 22300a920b5bSAndy Whitcroft my $stashline=""; 2231c2fdda0dSAndy Whitcroft my $stashrawline=""; 22320a920b5bSAndy Whitcroft 22334a0df2efSAndy Whitcroft my $length; 22340a920b5bSAndy Whitcroft my $indent; 22350a920b5bSAndy Whitcroft my $previndent=0; 22360a920b5bSAndy Whitcroft my $stashindent=0; 22370a920b5bSAndy Whitcroft 2238de7d4f0eSAndy Whitcroft our $clean = 1; 22390a920b5bSAndy Whitcroft my $signoff = 0; 2240*cd261496SGeert Uytterhoeven my $author = ''; 2241*cd261496SGeert Uytterhoeven my $authorsignoff = 0; 22420a920b5bSAndy Whitcroft my $is_patch = 0; 224329ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 224415662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 2245ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2246bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 22472a076f40SJoe Perches my $commit_log_long_line = 0; 2248e518e9a5SJoe Perches my $commit_log_has_diff = 0; 224913f1937eSJoe Perches my $reported_maintainer_file = 0; 2250fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2251fa64205dSPasi Savanainen 2252365dd4eaSJoe Perches my $last_blank_line = 0; 22535e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2254365dd4eaSJoe Perches 225513214adfSAndy Whitcroft our @report = (); 22566c72ffaaSAndy Whitcroft our $cnt_lines = 0; 22576c72ffaaSAndy Whitcroft our $cnt_error = 0; 22586c72ffaaSAndy Whitcroft our $cnt_warn = 0; 22596c72ffaaSAndy Whitcroft our $cnt_chk = 0; 22606c72ffaaSAndy Whitcroft 22610a920b5bSAndy Whitcroft # Trace the real file/line as we go. 22620a920b5bSAndy Whitcroft my $realfile = ''; 22630a920b5bSAndy Whitcroft my $realline = 0; 22640a920b5bSAndy Whitcroft my $realcnt = 0; 22650a920b5bSAndy Whitcroft my $here = ''; 226677cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 22670a920b5bSAndy Whitcroft my $in_comment = 0; 2268c2fdda0dSAndy Whitcroft my $comment_edge = 0; 22690a920b5bSAndy Whitcroft my $first_line = 0; 22701e855726SWolfram Sang my $p1_prefix = ''; 22710a920b5bSAndy Whitcroft 227213214adfSAndy Whitcroft my $prev_values = 'E'; 227313214adfSAndy Whitcroft 227413214adfSAndy Whitcroft # suppression flags 2275773647a0SAndy Whitcroft my %suppress_ifbraces; 2276170d3a22SAndy Whitcroft my %suppress_whiletrailers; 22772b474a1aSAndy Whitcroft my %suppress_export; 22783e469cdcSAndy Whitcroft my $suppress_statement = 0; 2279653d4876SAndy Whitcroft 22807e51f197SJoe Perches my %signatures = (); 2281323c1260SJoe Perches 2282c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2283de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2284c2fdda0dSAndy Whitcroft # 2285de7d4f0eSAndy Whitcroft my @setup_docs = (); 2286de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2287773647a0SAndy Whitcroft 2288d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2289d8b07710SJoe Perches 22909f3a8992SRob Herring my $checklicenseline = 1; 22919f3a8992SRob Herring 2292773647a0SAndy Whitcroft sanitise_line_reset(); 2293c2fdda0dSAndy Whitcroft my $line; 2294c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2295773647a0SAndy Whitcroft $linenr++; 2296773647a0SAndy Whitcroft $line = $rawline; 2297c2fdda0dSAndy Whitcroft 22983705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 22993705ce5bSJoe Perches 2300773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2301de7d4f0eSAndy Whitcroft $setup_docs = 0; 23028c27ceffSMauro Carvalho Chehab if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { 2303de7d4f0eSAndy Whitcroft $setup_docs = 1; 2304de7d4f0eSAndy Whitcroft } 2305773647a0SAndy Whitcroft #next; 2306de7d4f0eSAndy Whitcroft } 230774fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2308773647a0SAndy Whitcroft $realline=$1-1; 2309773647a0SAndy Whitcroft if (defined $2) { 2310773647a0SAndy Whitcroft $realcnt=$3+1; 2311773647a0SAndy Whitcroft } else { 2312773647a0SAndy Whitcroft $realcnt=1+1; 2313773647a0SAndy Whitcroft } 2314c45dcabdSAndy Whitcroft $in_comment = 0; 2315773647a0SAndy Whitcroft 2316773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2317773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2318773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2319773647a0SAndy Whitcroft # at context start. 2320773647a0SAndy Whitcroft my $edge; 232101fa9147SAndy Whitcroft my $cnt = $realcnt; 232201fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 232301fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 232401fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 232501fa9147SAndy Whitcroft $cnt--; 232601fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2327721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2328fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2329fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2330fae17daeSAndy Whitcroft ($edge) = $1; 2331fae17daeSAndy Whitcroft last; 2332fae17daeSAndy Whitcroft } 2333773647a0SAndy Whitcroft } 2334773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2335773647a0SAndy Whitcroft $in_comment = 1; 2336773647a0SAndy Whitcroft } 2337773647a0SAndy Whitcroft 2338773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2339773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2340773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2341773647a0SAndy Whitcroft if (!defined $edge && 234283242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2343773647a0SAndy Whitcroft { 2344773647a0SAndy Whitcroft $in_comment = 1; 2345773647a0SAndy Whitcroft } 2346773647a0SAndy Whitcroft 2347773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2348773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2349773647a0SAndy Whitcroft 2350171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2351773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2352171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2353773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2354773647a0SAndy Whitcroft } 2355773647a0SAndy Whitcroft push(@lines, $line); 2356773647a0SAndy Whitcroft 2357773647a0SAndy Whitcroft if ($realcnt > 1) { 2358773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2359773647a0SAndy Whitcroft } else { 2360773647a0SAndy Whitcroft $realcnt = 0; 2361773647a0SAndy Whitcroft } 2362773647a0SAndy Whitcroft 2363773647a0SAndy Whitcroft #print "==>$rawline\n"; 2364773647a0SAndy Whitcroft #print "-->$line\n"; 2365de7d4f0eSAndy Whitcroft 2366de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2367de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2368de7d4f0eSAndy Whitcroft } 2369de7d4f0eSAndy Whitcroft } 2370de7d4f0eSAndy Whitcroft 23716c72ffaaSAndy Whitcroft $prefix = ''; 23726c72ffaaSAndy Whitcroft 2373773647a0SAndy Whitcroft $realcnt = 0; 2374773647a0SAndy Whitcroft $linenr = 0; 2375194f66fcSJoe Perches $fixlinenr = -1; 23760a920b5bSAndy Whitcroft foreach my $line (@lines) { 23770a920b5bSAndy Whitcroft $linenr++; 2378194f66fcSJoe Perches $fixlinenr++; 23791b5539b1SJoe Perches my $sline = $line; #copy of $line 23801b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 23810a920b5bSAndy Whitcroft 2382c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 23836c72ffaaSAndy Whitcroft 238412c253abSJoe Perches# check if it's a mode change, rename or start of a patch 238512c253abSJoe Perches if (!$in_commit_log && 238612c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 238712c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 238812c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 238912c253abSJoe Perches $is_patch = 1; 239012c253abSJoe Perches } 239112c253abSJoe Perches 23920a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2393e518e9a5SJoe Perches if (!$in_commit_log && 239474fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 239574fd4f34SJoe Perches my $context = $4; 23960a920b5bSAndy Whitcroft $is_patch = 1; 23974a0df2efSAndy Whitcroft $first_line = $linenr + 1; 23980a920b5bSAndy Whitcroft $realline=$1-1; 23990a920b5bSAndy Whitcroft if (defined $2) { 24000a920b5bSAndy Whitcroft $realcnt=$3+1; 24010a920b5bSAndy Whitcroft } else { 24020a920b5bSAndy Whitcroft $realcnt=1+1; 24030a920b5bSAndy Whitcroft } 2404c2fdda0dSAndy Whitcroft annotate_reset(); 240513214adfSAndy Whitcroft $prev_values = 'E'; 240613214adfSAndy Whitcroft 2407773647a0SAndy Whitcroft %suppress_ifbraces = (); 2408170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 24092b474a1aSAndy Whitcroft %suppress_export = (); 24103e469cdcSAndy Whitcroft $suppress_statement = 0; 241174fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 241274fd4f34SJoe Perches $context_function = $1; 241374fd4f34SJoe Perches } else { 241474fd4f34SJoe Perches undef $context_function; 241574fd4f34SJoe Perches } 24160a920b5bSAndy Whitcroft next; 24170a920b5bSAndy Whitcroft 24184a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 24194a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 24204a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2421773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 24220a920b5bSAndy Whitcroft $realline++; 2423d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 24240a920b5bSAndy Whitcroft 24254a0df2efSAndy Whitcroft # Measure the line length and indent. 2426c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 24270a920b5bSAndy Whitcroft 24280a920b5bSAndy Whitcroft # Track the previous line. 24290a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 24300a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2431c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2432c2fdda0dSAndy Whitcroft 2433773647a0SAndy Whitcroft #warn "line<$line>\n"; 24346c72ffaaSAndy Whitcroft 2435d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2436d8aaf121SAndy Whitcroft $realcnt--; 24370a920b5bSAndy Whitcroft } 24380a920b5bSAndy Whitcroft 2439cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2440cc77cdcaSAndy Whitcroft 24416c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 24426c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2443773647a0SAndy Whitcroft 24442ac73b4fSJoe Perches my $found_file = 0; 2445773647a0SAndy Whitcroft # extract the filename as it passes 24463bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 24473bf9a009SRabin Vincent $realfile = $1; 24482b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2449270c49a0SJoe Perches $in_commit_log = 0; 24502ac73b4fSJoe Perches $found_file = 1; 24513bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2452773647a0SAndy Whitcroft $realfile = $1; 24532b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2454270c49a0SJoe Perches $in_commit_log = 0; 24551e855726SWolfram Sang 24561e855726SWolfram Sang $p1_prefix = $1; 2457e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2458e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2459000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2460000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 24611e855726SWolfram Sang } 2462773647a0SAndy Whitcroft 2463c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2464000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2465000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2466773647a0SAndy Whitcroft } 24672ac73b4fSJoe Perches $found_file = 1; 24682ac73b4fSJoe Perches } 24692ac73b4fSJoe Perches 247034d8815fSJoe Perches#make up the handle for any error we report on this line 247134d8815fSJoe Perches if ($showfile) { 247234d8815fSJoe Perches $prefix = "$realfile:$realline: " 247334d8815fSJoe Perches } elsif ($emacs) { 24747d3a9f67SJoe Perches if ($file) { 24757d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 24767d3a9f67SJoe Perches } else { 247734d8815fSJoe Perches $prefix = "$filename:$linenr: "; 247834d8815fSJoe Perches } 24797d3a9f67SJoe Perches } 248034d8815fSJoe Perches 24812ac73b4fSJoe Perches if ($found_file) { 248285b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 248385b0ee18SJoe Perches WARN("OBSOLETE", 248485b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 248585b0ee18SJoe Perches } 24867bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 24872ac73b4fSJoe Perches $check = 1; 24882ac73b4fSJoe Perches } else { 24892ac73b4fSJoe Perches $check = $check_orig; 24902ac73b4fSJoe Perches } 24919f3a8992SRob Herring $checklicenseline = 1; 2492773647a0SAndy Whitcroft next; 2493773647a0SAndy Whitcroft } 2494773647a0SAndy Whitcroft 2495389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 24960a920b5bSAndy Whitcroft 2497c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2498c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2499c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 25000a920b5bSAndy Whitcroft 25016c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 25026c72ffaaSAndy Whitcroft 2503e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2504e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2505e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2506e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2507e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2508e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2509e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2510e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2511e518e9a5SJoe Perches $commit_log_has_diff = 1; 2512e518e9a5SJoe Perches } 2513e518e9a5SJoe Perches 25143bf9a009SRabin Vincent# Check for incorrect file permissions 25153bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 25163bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 251704db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 251804db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2519000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2520000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 25213bf9a009SRabin Vincent } 25223bf9a009SRabin Vincent } 25233bf9a009SRabin Vincent 2524*cd261496SGeert Uytterhoeven# Check the patch for a From: 2525*cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2526*cd261496SGeert Uytterhoeven $author = $1; 2527*cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2528*cd261496SGeert Uytterhoeven $author =~ s/"//g; 2529*cd261496SGeert Uytterhoeven } 2530*cd261496SGeert Uytterhoeven 253120112475SJoe Perches# Check the patch for a signoff: 2532d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 25334a0df2efSAndy Whitcroft $signoff++; 253415662b3eSJoe Perches $in_commit_log = 0; 2535*cd261496SGeert Uytterhoeven if ($author ne '') { 2536*cd261496SGeert Uytterhoeven my $l = $line; 2537*cd261496SGeert Uytterhoeven $l =~ s/"//g; 2538*cd261496SGeert Uytterhoeven if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) { 2539*cd261496SGeert Uytterhoeven $authorsignoff = 1; 2540*cd261496SGeert Uytterhoeven } 2541*cd261496SGeert Uytterhoeven } 25420a920b5bSAndy Whitcroft } 254320112475SJoe Perches 2544e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2545e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2546e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2547e0d975b1SJoe Perches $reported_maintainer_file = 1; 2548e0d975b1SJoe Perches } 2549e0d975b1SJoe Perches 255020112475SJoe Perches# Check signature styles 2551270c49a0SJoe Perches if (!$in_header_lines && 2552ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 255320112475SJoe Perches my $space_before = $1; 255420112475SJoe Perches my $sign_off = $2; 255520112475SJoe Perches my $space_after = $3; 255620112475SJoe Perches my $email = $4; 255720112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 255820112475SJoe Perches 2559ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2560ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2561ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2562ce0338dfSJoe Perches } 256320112475SJoe Perches if (defined $space_before && $space_before ne "") { 25643705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25653705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 25663705ce5bSJoe Perches $fix) { 2567194f66fcSJoe Perches $fixed[$fixlinenr] = 25683705ce5bSJoe Perches "$ucfirst_sign_off $email"; 25693705ce5bSJoe Perches } 257020112475SJoe Perches } 257120112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 25723705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25733705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 25743705ce5bSJoe Perches $fix) { 2575194f66fcSJoe Perches $fixed[$fixlinenr] = 25763705ce5bSJoe Perches "$ucfirst_sign_off $email"; 25773705ce5bSJoe Perches } 25783705ce5bSJoe Perches 257920112475SJoe Perches } 258020112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 25813705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25823705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 25833705ce5bSJoe Perches $fix) { 2584194f66fcSJoe Perches $fixed[$fixlinenr] = 25853705ce5bSJoe Perches "$ucfirst_sign_off $email"; 25863705ce5bSJoe Perches } 258720112475SJoe Perches } 258820112475SJoe Perches 258920112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 259020112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 259120112475SJoe Perches if ($suggested_email eq "") { 2592000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2593000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 259420112475SJoe Perches } else { 259520112475SJoe Perches my $dequoted = $suggested_email; 259620112475SJoe Perches $dequoted =~ s/^"//; 259720112475SJoe Perches $dequoted =~ s/" </ </; 259820112475SJoe Perches # Don't force email to have quotes 259920112475SJoe Perches # Allow just an angle bracketed address 260020112475SJoe Perches if ("$dequoted$comment" ne $email && 260120112475SJoe Perches "<$email_address>$comment" ne $email && 260220112475SJoe Perches "$suggested_email$comment" ne $email) { 2603000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2604000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 260520112475SJoe Perches } 26060a920b5bSAndy Whitcroft } 26077e51f197SJoe Perches 26087e51f197SJoe Perches# Check for duplicate signatures 26097e51f197SJoe Perches my $sig_nospace = $line; 26107e51f197SJoe Perches $sig_nospace =~ s/\s//g; 26117e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 26127e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 26137e51f197SJoe Perches WARN("BAD_SIGN_OFF", 26147e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 26157e51f197SJoe Perches } else { 26167e51f197SJoe Perches $signatures{$sig_nospace} = 1; 26177e51f197SJoe Perches } 26180a920b5bSAndy Whitcroft } 26190a920b5bSAndy Whitcroft 2620a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2621a2fe16b9SJoe Perches if ($in_header_lines && 2622a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2623a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2624a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2625a2fe16b9SJoe Perches } 2626a2fe16b9SJoe Perches 26277ebd05efSChristopher Covington# Check for unwanted Gerrit info 26287ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 26297ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 26307ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 26317ebd05efSChristopher Covington } 26327ebd05efSChristopher Covington 2633369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2634369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2635369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2636369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2637369c8dd3SJoe Perches # timestamp 2638369c8dd3SJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) { 2639369c8dd3SJoe Perches # stack dump address 2640369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2641369c8dd3SJoe Perches } 2642369c8dd3SJoe Perches 26432a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 26442a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2645bf4daf12SJoe Perches length($line) > 75 && 2646bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2647bf4daf12SJoe Perches # file delta changes 2648bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2649bf4daf12SJoe Perches # filename then : 2650bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2651bf4daf12SJoe Perches # A Fixes: or Link: line 2652bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 26532a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 26542a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 26552a076f40SJoe Perches $commit_log_long_line = 1; 26562a076f40SJoe Perches } 26572a076f40SJoe Perches 2658bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2659bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2660bf4daf12SJoe Perches $line =~ /^\s*$/) { 2661bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2662bf4daf12SJoe Perches } 2663bf4daf12SJoe Perches 26640d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2665369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2666aab38f51SJoe Perches $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i && 2667e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2668fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2669aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2670369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2671bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2672fe043ea1SJoe Perches my $init_char = "c"; 2673fe043ea1SJoe Perches my $orig_commit = ""; 26740d7835fcSJoe Perches my $short = 1; 26750d7835fcSJoe Perches my $long = 0; 26760d7835fcSJoe Perches my $case = 1; 26770d7835fcSJoe Perches my $space = 1; 26780d7835fcSJoe Perches my $hasdesc = 0; 267919c146a6SJoe Perches my $hasparens = 0; 26800d7835fcSJoe Perches my $id = '0123456789ab'; 26810d7835fcSJoe Perches my $orig_desc = "commit description"; 26820d7835fcSJoe Perches my $description = ""; 26830d7835fcSJoe Perches 2684fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2685fe043ea1SJoe Perches $init_char = $1; 2686fe043ea1SJoe Perches $orig_commit = lc($2); 2687fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2688fe043ea1SJoe Perches $orig_commit = lc($1); 2689fe043ea1SJoe Perches } 2690fe043ea1SJoe Perches 26910d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 26920d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 26930d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 26940d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 26950d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 26960d7835fcSJoe Perches $orig_desc = $1; 269719c146a6SJoe Perches $hasparens = 1; 26980d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 26990d7835fcSJoe Perches defined $rawlines[$linenr] && 27000d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 27010d7835fcSJoe Perches $orig_desc = $1; 270219c146a6SJoe Perches $hasparens = 1; 2703b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2704b671fde0SJoe Perches defined $rawlines[$linenr] && 2705b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2706b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2707b671fde0SJoe Perches $orig_desc = $1; 2708b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2709b671fde0SJoe Perches $orig_desc .= " " . $1; 271019c146a6SJoe Perches $hasparens = 1; 27110d7835fcSJoe Perches } 27120d7835fcSJoe Perches 27130d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 27140d7835fcSJoe Perches $id, $orig_desc); 27150d7835fcSJoe Perches 2716948b133aSHeinrich Schuchardt if (defined($id) && 2717948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2718d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 27190d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 27200d7835fcSJoe Perches } 2721d311cd44SJoe Perches } 2722d311cd44SJoe Perches 272313f1937eSJoe Perches# Check for added, moved or deleted files 272413f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 272513f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 272613f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 272713f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 272813f1937eSJoe Perches (defined($1) || defined($2))))) { 2729a82603a8SAndrew Jeffery $is_patch = 1; 273013f1937eSJoe Perches $reported_maintainer_file = 1; 273113f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 273213f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 273313f1937eSJoe Perches } 273413f1937eSJoe Perches 273500df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 27368905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2737000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2738000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 27396c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2740de7d4f0eSAndy Whitcroft } 2741de7d4f0eSAndy Whitcroft 2742de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2743de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2744171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2745171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2746171ae1a4SAndy Whitcroft 2747171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2748171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2749171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2750171ae1a4SAndy Whitcroft 275134d99219SJoe Perches CHK("INVALID_UTF8", 2752000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 275300df344fSAndy Whitcroft } 27540a920b5bSAndy Whitcroft 275515662b3eSJoe Perches# Check if it's the start of a commit log 275615662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 275715662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2758eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2759eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 276015662b3eSJoe Perches $in_header_lines = 0; 276115662b3eSJoe Perches $in_commit_log = 1; 2762ed43c4e5SAllen Hubbe $has_commit_log = 1; 276315662b3eSJoe Perches } 276415662b3eSJoe Perches 2765fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2766fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2767fa64205dSPasi Savanainen if ($in_header_lines && 2768fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2769fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2770fa64205dSPasi Savanainen $non_utf8_charset = 1; 2771fa64205dSPasi Savanainen } 2772fa64205dSPasi Savanainen 2773fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 277415662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2775fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 277615662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 277715662b3eSJoe Perches } 277815662b3eSJoe Perches 2779d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2780d6430f71SJoe Perches if ($tree && $in_commit_log) { 2781d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2782d6430f71SJoe Perches my $file = $1; 2783d6430f71SJoe Perches 2784d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2785d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2786d6430f71SJoe Perches # 2787d6430f71SJoe Perches } else { 2788d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2789d6430f71SJoe Perches } 2790d6430f71SJoe Perches } 2791d6430f71SJoe Perches } 2792d6430f71SJoe Perches 279366b47b4aSKees Cook# Check for various typo / spelling mistakes 279466d7a382SJoe Perches if (defined($misspellings) && 279566d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2796ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 279766b47b4aSKees Cook my $typo = $1; 279866b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 279966b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 280066b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 28010675a8fbSJean Delvare my $msg_level = \&WARN; 28020675a8fbSJean Delvare $msg_level = \&CHK if ($file); 28030675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 280466b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 280566b47b4aSKees Cook $fix) { 280666b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 280766b47b4aSKees Cook } 280866b47b4aSKees Cook } 280966b47b4aSKees Cook } 281066b47b4aSKees Cook 281130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 281230670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 281300df344fSAndy Whitcroft 28140a920b5bSAndy Whitcroft#trailing whitespace 28159c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2816c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2817d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2818d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2819d5e616fcSJoe Perches $fix) { 2820194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2821d5e616fcSJoe Perches } 2822c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2823c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28243705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 28253705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 28263705ce5bSJoe Perches $fix) { 2827194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 28283705ce5bSJoe Perches } 28293705ce5bSJoe Perches 2830d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 28310a920b5bSAndy Whitcroft } 28325368df20SAndy Whitcroft 28334783f894SJosh Triplett# Check for FSF mailing addresses. 2834109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 28351bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 28363e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 28373e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 28384783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28390675a8fbSJean Delvare my $msg_level = \&ERROR; 28400675a8fbSJean Delvare $msg_level = \&CHK if ($file); 28410675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 28424783f894SJosh 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) 28434783f894SJosh Triplett } 28444783f894SJosh Triplett 28453354957aSAndi Kleen# check for Kconfig help text having a real description 28469fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 28479fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 28483354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 2849678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 2850678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 2851678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 2852678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 28533354957aSAndi Kleen my $length = 0; 28549fe287d7SAndy Whitcroft my $cnt = $realcnt; 28559fe287d7SAndy Whitcroft my $ln = $linenr + 1; 28569fe287d7SAndy Whitcroft my $f; 2857a1385803SAndy Whitcroft my $is_start = 0; 28589fe287d7SAndy Whitcroft my $is_end = 0; 2859a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 28609fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 28619fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 28629fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 28639fe287d7SAndy Whitcroft 28649fe287d7SAndy Whitcroft next if ($f =~ /^-/); 28658d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2866a1385803SAndy Whitcroft 286786adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 2868a1385803SAndy Whitcroft $is_start = 1; 286984af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 287084af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 287184af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 287284af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 287384af7a61SUlf Magnusson } 2874a1385803SAndy Whitcroft $length = -1; 2875a1385803SAndy Whitcroft } 2876a1385803SAndy Whitcroft 28779fe287d7SAndy Whitcroft $f =~ s/^.//; 28783354957aSAndi Kleen $f =~ s/#.*//; 28793354957aSAndi Kleen $f =~ s/^\s+//; 28803354957aSAndi Kleen next if ($f =~ /^$/); 2881678ae162SUlf Magnusson 2882678ae162SUlf Magnusson # This only checks context lines in the patch 2883678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 2884678ae162SUlf Magnusson # positives, even though some of these are 2885678ae162SUlf Magnusson # common words in help texts 2886678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 2887678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 28889fe287d7SAndy Whitcroft $is_end = 1; 28899fe287d7SAndy Whitcroft last; 28909fe287d7SAndy Whitcroft } 28913354957aSAndi Kleen $length++; 28923354957aSAndi Kleen } 289356193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 2894000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 289556193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 289656193274SVadim Bendebury } 2897a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 28983354957aSAndi Kleen } 28993354957aSAndi Kleen 2900628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form 2901628f91a2SJoe Perches if ($realfile =~ /^MAINTAINERS$/ && 2902628f91a2SJoe Perches $rawline =~ /^\+[A-Z]:/ && 2903628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 2904628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 2905628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 2906628f91a2SJoe Perches $fix) { 2907628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 2908628f91a2SJoe Perches } 2909628f91a2SJoe Perches } 2910628f91a2SJoe Perches 2911327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 2912327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 2913327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 2914327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 2915327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 2916327953e9SChristoph Jaeger } 2917327953e9SChristoph Jaeger 2918c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2919c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2920c68e5878SArnaud Lacombe my $flag = $1; 2921c68e5878SArnaud Lacombe my $replacement = { 2922c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2923c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2924c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2925c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2926c68e5878SArnaud Lacombe }; 2927c68e5878SArnaud Lacombe 2928c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2929c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2930c68e5878SArnaud Lacombe } 2931c68e5878SArnaud Lacombe 2932bff5da43SRob Herring# check for DT compatible documentation 29337dd05b38SFlorian Vaussard if (defined $root && 29347dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 29357dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 29367dd05b38SFlorian Vaussard 2937bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2938bff5da43SRob Herring 2939cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2940cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2941cc93319bSFlorian Vaussard 2942bff5da43SRob Herring foreach my $compat (@compats) { 2943bff5da43SRob Herring my $compat2 = $compat; 2944185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2945185d566bSRob Herring my $compat3 = $compat; 2946185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2947185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2948bff5da43SRob Herring if ( $? >> 8 ) { 2949bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2950bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2951bff5da43SRob Herring } 2952bff5da43SRob Herring 29534fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 29544fbf32a6SFlorian Vaussard my $vendor = $1; 2955cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2956bff5da43SRob Herring if ( $? >> 8 ) { 2957bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2958cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2959bff5da43SRob Herring } 2960bff5da43SRob Herring } 2961bff5da43SRob Herring } 2962bff5da43SRob Herring 29639f3a8992SRob Herring# check for using SPDX license tag at beginning of files 29649f3a8992SRob Herring if ($realline == $checklicenseline) { 29659f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 29669f3a8992SRob Herring $checklicenseline = 2; 29679f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 29689f3a8992SRob Herring my $comment = ""; 29699f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 29709f3a8992SRob Herring $comment = '/*'; 29719f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 29729f3a8992SRob Herring $comment = '//'; 29739f3a8992SRob Herring } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) { 29749f3a8992SRob Herring $comment = '#'; 29759f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 29769f3a8992SRob Herring $comment = '..'; 29779f3a8992SRob Herring } 29789f3a8992SRob Herring 29799f3a8992SRob Herring if ($comment !~ /^$/ && 29809f3a8992SRob Herring $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) { 29819f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 29829f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 29839f3a8992SRob Herring } 29849f3a8992SRob Herring } 29859f3a8992SRob Herring } 29869f3a8992SRob Herring 29875368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 2988d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 29895368df20SAndy Whitcroft 299047e0c88bSJoe Perches# line length limit (with some exclusions) 299147e0c88bSJoe Perches# 299247e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 299347e0c88bSJoe Perches# logging functions like pr_info that end in a string 299447e0c88bSJoe Perches# lines with a single string 299547e0c88bSJoe Perches# #defines that are a single string 29962e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 299747e0c88bSJoe Perches# 299847e0c88bSJoe Perches# There are 3 different line length message types: 2999ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 300047e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 300147e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 300247e0c88bSJoe Perches# 300347e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 300447e0c88bSJoe Perches# 300547e0c88bSJoe Perches 3006b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 300747e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 300847e0c88bSJoe Perches 300947e0c88bSJoe Perches # Check the allowed long line types first 301047e0c88bSJoe Perches 301147e0c88bSJoe Perches # logging functions that end in a string that starts 301247e0c88bSJoe Perches # before $max_line_length 301347e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 301447e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 301547e0c88bSJoe Perches $msg_type = ""; 301647e0c88bSJoe Perches 301747e0c88bSJoe Perches # lines with only strings (w/ possible termination) 301847e0c88bSJoe Perches # #defines with only strings 301947e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 302047e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 302147e0c88bSJoe Perches $msg_type = ""; 302247e0c88bSJoe Perches 3023cc147506SJoe Perches # More special cases 3024cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3025cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3026d560a5f8SJoe Perches $msg_type = ""; 3027d560a5f8SJoe Perches 30282e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 30292e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 30302e4bbbc5SAndreas Brauchli $msg_type = ""; 30312e4bbbc5SAndreas Brauchli 303247e0c88bSJoe Perches # Otherwise set the alternate message types 303347e0c88bSJoe Perches 303447e0c88bSJoe Perches # a comment starts before $max_line_length 303547e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 303647e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 303747e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 303847e0c88bSJoe Perches 303947e0c88bSJoe Perches # a quoted string starts before $max_line_length 304047e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 304147e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 304247e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 304347e0c88bSJoe Perches } 304447e0c88bSJoe Perches 304547e0c88bSJoe Perches if ($msg_type ne "" && 304647e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 304747e0c88bSJoe Perches WARN($msg_type, 30486cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 30490a920b5bSAndy Whitcroft } 305047e0c88bSJoe Perches } 30510a920b5bSAndy Whitcroft 30528905a67cSAndy Whitcroft# check for adding lines without a newline. 30538905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3054000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3055000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 30568905a67cSAndy Whitcroft } 30578905a67cSAndy Whitcroft 3058b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3059de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 30600a920b5bSAndy Whitcroft 30610a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 30620a920b5bSAndy Whitcroft# more than 8 must use tabs. 3063c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3064c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3065c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3066d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 30673705ce5bSJoe Perches if (ERROR("CODE_INDENT", 30683705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 30693705ce5bSJoe Perches $fix) { 3070194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 30713705ce5bSJoe Perches } 30720a920b5bSAndy Whitcroft } 30730a920b5bSAndy Whitcroft 307408e44365SAlberto Panizzo# check for space before tabs. 307508e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 307608e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 30773705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 30783705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 30793705ce5bSJoe Perches $fix) { 3080194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3081d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 3082194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3083c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 30843705ce5bSJoe Perches } 308508e44365SAlberto Panizzo } 308608e44365SAlberto Panizzo 30876a487211SJoe Perches# check for assignments on the start of a line 30886a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 30896a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 30906a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 30916a487211SJoe Perches } 30926a487211SJoe Perches 3093d1fe9c09SJoe Perches# check for && or || at the start of a line 3094d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3095d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3096d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3097d1fe9c09SJoe Perches } 3098d1fe9c09SJoe Perches 3099a91e8994SJoe Perches# check indentation starts on a tab stop 31005b57980dSJoe Perches if ($perl_version_ok && 3101bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3102a91e8994SJoe Perches my $indent = length($1); 3103a91e8994SJoe Perches if ($indent % 8) { 3104a91e8994SJoe Perches if (WARN("TABSTOP", 3105a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3106a91e8994SJoe Perches $fix) { 3107a91e8994SJoe Perches $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; 3108a91e8994SJoe Perches } 3109a91e8994SJoe Perches } 3110a91e8994SJoe Perches } 3111a91e8994SJoe Perches 3112d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 31135b57980dSJoe Perches if ($perl_version_ok && 3114fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3115d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3116d1fe9c09SJoe Perches my $oldindent = $1; 3117d1fe9c09SJoe Perches my $rest = $2; 3118d1fe9c09SJoe Perches 3119d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3120d1fe9c09SJoe Perches if ($pos >= 0) { 3121b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3122b34a26f3SJoe Perches my $newindent = $2; 3123d1fe9c09SJoe Perches 3124d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3125d1fe9c09SJoe Perches "\t" x ($pos / 8) . 3126d1fe9c09SJoe Perches " " x ($pos % 8); 3127d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3128d1fe9c09SJoe Perches 3129d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3130d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 31313705ce5bSJoe Perches 31323705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 31333705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 31343705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3135194f66fcSJoe Perches $fixed[$fixlinenr] =~ 31363705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 31373705ce5bSJoe Perches } 3138d1fe9c09SJoe Perches } 3139d1fe9c09SJoe Perches } 3140d1fe9c09SJoe Perches } 3141d1fe9c09SJoe Perches 31426ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 31436ab3a970SJoe Perches# avoid checking a few false positives: 31446ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 31456ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 31466ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 31476ab3a970SJoe Perches# multiline macros that define functions 31486ab3a970SJoe Perches# known attributes or the __attribute__ keyword 31496ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 31506ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 31513705ce5bSJoe Perches if (CHK("SPACING", 3152f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 31533705ce5bSJoe Perches $fix) { 3154194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3155f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 31563705ce5bSJoe Perches } 3157aad4f614SJoe Perches } 3158aad4f614SJoe Perches 315986406b1cSJoe Perches# Block comment styles 316086406b1cSJoe Perches# Networking with an initial /* 316105880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3162fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 316385ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 316485ad978cSJoe Perches $realline > 2) { 316505880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 316605880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 316705880600SJoe Perches } 316805880600SJoe Perches 316986406b1cSJoe Perches# Block comments use * on subsequent lines 317086406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 317186406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3172a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 317361135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3174a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 317586406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 317686406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3177a605e32eSJoe Perches } 3178a605e32eSJoe Perches 317986406b1cSJoe Perches# Block comments use */ on trailing lines 318086406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3181c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3182c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3183c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 318486406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 318586406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 318605880600SJoe Perches } 318705880600SJoe Perches 318808eb9b80SJoe Perches# Block comment * alignment 318908eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3190af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3191af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3192af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 319308eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3194af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3195af207524SJoe Perches my $oldindent; 319608eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3197af207524SJoe Perches if (defined($1)) { 3198af207524SJoe Perches $oldindent = expand_tabs($1); 3199af207524SJoe Perches } else { 3200af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3201af207524SJoe Perches $oldindent = expand_tabs($1); 3202af207524SJoe Perches } 320308eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 320408eb9b80SJoe Perches my $newindent = $1; 320508eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3206af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 320708eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 320808eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 320908eb9b80SJoe Perches } 321008eb9b80SJoe Perches } 321108eb9b80SJoe Perches 32127f619191SJoe Perches# check for missing blank lines after struct/union declarations 32137f619191SJoe Perches# with exceptions for various attributes and macros 32147f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 32157f619191SJoe Perches $line =~ /^\+/ && 32167f619191SJoe Perches !($line =~ /^\+\s*$/ || 32177f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 32187f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 32197f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 32207f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 32217f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 32227f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 32230bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 32247f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3225d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3226d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3227d752fcc8SJoe Perches $fix) { 3228f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3229d752fcc8SJoe Perches } 32307f619191SJoe Perches } 32317f619191SJoe Perches 3232365dd4eaSJoe Perches# check for multiple consecutive blank lines 3233365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3234365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3235365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3236d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3237d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3238d752fcc8SJoe Perches $fix) { 3239f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3240d752fcc8SJoe Perches } 3241d752fcc8SJoe Perches 3242365dd4eaSJoe Perches $last_blank_line = $linenr; 3243365dd4eaSJoe Perches } 3244365dd4eaSJoe Perches 32453b617e3bSJoe Perches# check for missing blank lines after declarations 32463f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 32473f7bac03SJoe Perches # actual declarations 32483f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 32495a4e1fd3SJoe Perches # function pointer declarations 32505a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 32513f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 32523f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 32533f7bac03SJoe Perches # known declaration macros 32543f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 32553f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 32563f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 32573f7bac03SJoe Perches # other possible extensions of declaration lines 32583f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 32593f7bac03SJoe Perches # not starting a section or a macro "\" extended line 32603f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 32613f7bac03SJoe Perches # looks like a declaration 32623f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 32635a4e1fd3SJoe Perches # function pointer declarations 32645a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 32653f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 32663f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 32673f7bac03SJoe Perches # known declaration macros 32683f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 32693f7bac03SJoe Perches # start of struct or union or enum 32703b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 32713f7bac03SJoe Perches # start or end of block or continuation of declaration 32723f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 32733f7bac03SJoe Perches # bitfield continuation 32743f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 32753f7bac03SJoe Perches # other possible extensions of declaration lines 32763f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 32773f7bac03SJoe Perches # indentation of previous and current line are the same 32783f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3279d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3280d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3281d752fcc8SJoe Perches $fix) { 3282f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3283d752fcc8SJoe Perches } 32843b617e3bSJoe Perches } 32853b617e3bSJoe Perches 32865f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 32876b4c5bebSAndy Whitcroft# Exceptions: 32886b4c5bebSAndy Whitcroft# 1) within comments 32896b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 32906b4c5bebSAndy Whitcroft# 3) hanging labels 32913705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 32925f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32933705ce5bSJoe Perches if (WARN("LEADING_SPACE", 32943705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 32953705ce5bSJoe Perches $fix) { 3296194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 32973705ce5bSJoe Perches } 32985f7ddae6SRaffaele Recalcati } 32995f7ddae6SRaffaele Recalcati 3300b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3301b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3302b9ea10d6SAndy Whitcroft 33035751a24eSJoe Perches# check for unusual line ending [ or ( 33045751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 33055751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 33065751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 33075751a24eSJoe Perches } 33085751a24eSJoe Perches 33094dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 33104dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 33114dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 33124dbed76fSJoe Perches $context_function = $1; 33134dbed76fSJoe Perches } 33144dbed76fSJoe Perches 33154dbed76fSJoe Perches# check if this appears to be the end of function declaration 33164dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 33174dbed76fSJoe Perches undef $context_function; 33184dbed76fSJoe Perches } 33194dbed76fSJoe Perches 3320032a4c0fSJoe Perches# check indentation of any line with a bare else 3321840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3322032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3323032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3324032a4c0fSJoe Perches my $tabs = length($1) + 1; 3325840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3326840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3327840080a0SJoe Perches defined $lines[$linenr] && 3328840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3329032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3330032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3331032a4c0fSJoe Perches } 3332032a4c0fSJoe Perches } 3333032a4c0fSJoe Perches 3334c00df19aSJoe Perches# check indentation of a line with a break; 3335c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3336c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3337c00df19aSJoe Perches my $tabs = $1; 3338c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3339c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3340c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3341c00df19aSJoe Perches } 3342c00df19aSJoe Perches } 3343c00df19aSJoe Perches 3344c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3345cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3346000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3347000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3348c2fdda0dSAndy Whitcroft } 334922f2a2efSAndy Whitcroft 335056e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 335156e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 335256e77d70SJoe Perches WARN("HOTPLUG_SECTION", 335356e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 335456e77d70SJoe Perches } 335556e77d70SJoe Perches 33569c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 33572b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 33582b474a1aSAndy Whitcroft $realline_next); 33593e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3360ca819864SJoe Perches if ($linenr > $suppress_statement && 33611b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3362170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3363f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3364171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3365171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3366171ae1a4SAndy Whitcroft 33673e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 33683e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 33693e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 33703e469cdcSAndy Whitcroft # until we hit end of it. 33713e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 33723e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 33733e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 33743e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 33753e469cdcSAndy Whitcroft } 3376f74bd194SAndy Whitcroft 33772b474a1aSAndy Whitcroft # Find the real next line. 33782b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 33792b474a1aSAndy Whitcroft if (defined $realline_next && 33802b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 33812b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 33822b474a1aSAndy Whitcroft $realline_next++; 33832b474a1aSAndy Whitcroft } 33842b474a1aSAndy Whitcroft 3385171ae1a4SAndy Whitcroft my $s = $stat; 3386171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3387cf655043SAndy Whitcroft 3388c2fdda0dSAndy Whitcroft # Ignore goto labels. 3389171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3390c2fdda0dSAndy Whitcroft 3391c2fdda0dSAndy Whitcroft # Ignore functions being called 3392171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3393c2fdda0dSAndy Whitcroft 3394463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3395463f2864SAndy Whitcroft 3396c45dcabdSAndy Whitcroft # declarations always start with types 3397d2506586SAndy 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) { 3398c45dcabdSAndy Whitcroft my $type = $1; 3399c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3400c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3401c45dcabdSAndy Whitcroft 34026c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3403a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3404c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3405c2fdda0dSAndy Whitcroft } 34068905a67cSAndy Whitcroft 34076c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 340865863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3409c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 34109c0ca6f9SAndy Whitcroft } 34118905a67cSAndy Whitcroft 34128905a67cSAndy Whitcroft # Check for any sort of function declaration. 34138905a67cSAndy Whitcroft # int foo(something bar, other baz); 34148905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3415171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 34168905a67cSAndy Whitcroft my ($name_len) = length($1); 34178905a67cSAndy Whitcroft 3418cf655043SAndy Whitcroft my $ctx = $s; 3419773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 34208905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3421cf655043SAndy Whitcroft 34228905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3423c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 34248905a67cSAndy Whitcroft 3425c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 34268905a67cSAndy Whitcroft } 34278905a67cSAndy Whitcroft } 34288905a67cSAndy Whitcroft } 34298905a67cSAndy Whitcroft 34309c0ca6f9SAndy Whitcroft } 34319c0ca6f9SAndy Whitcroft 343200df344fSAndy Whitcroft# 343300df344fSAndy Whitcroft# Checks which may be anchored in the context. 343400df344fSAndy Whitcroft# 343500df344fSAndy Whitcroft 343600df344fSAndy Whitcroft# Check for switch () and associated case and default 343700df344fSAndy Whitcroft# statements should be at the same indent. 343800df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 343900df344fSAndy Whitcroft my $err = ''; 344000df344fSAndy Whitcroft my $sep = ''; 344100df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 344200df344fSAndy Whitcroft shift(@ctx); 344300df344fSAndy Whitcroft for my $ctx (@ctx) { 344400df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 344500df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 344600df344fSAndy Whitcroft $indent != $cindent) { 344700df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 344800df344fSAndy Whitcroft $sep = ''; 344900df344fSAndy Whitcroft } else { 345000df344fSAndy Whitcroft $sep = "[...]\n"; 345100df344fSAndy Whitcroft } 345200df344fSAndy Whitcroft } 345300df344fSAndy Whitcroft if ($err ne '') { 3454000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3455000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3456de7d4f0eSAndy Whitcroft } 3457de7d4f0eSAndy Whitcroft } 3458de7d4f0eSAndy Whitcroft 3459de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3460de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 34610fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3462773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3463773647a0SAndy Whitcroft 34649c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 34658eef05ddSJoe Perches 34668eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 34678eef05ddSJoe Perches WARN("DEEP_INDENTATION", 34688eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 34698eef05ddSJoe Perches } 34708eef05ddSJoe Perches 3471de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3472de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3473de7d4f0eSAndy Whitcroft 3474548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3475548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3476de7d4f0eSAndy Whitcroft 3477548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3478548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3479548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3480548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3481548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3482773647a0SAndy Whitcroft $ctx_ln++; 3483773647a0SAndy Whitcroft } 3484548596d5SAndy Whitcroft 348553210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 348653210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3487773647a0SAndy Whitcroft 3488773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3489000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3490000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 349101464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 349200df344fSAndy Whitcroft } 3493773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3494773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3495773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3496773647a0SAndy Whitcroft { 34979c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 34989c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3499000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3500000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 350101464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 35029c0ca6f9SAndy Whitcroft } 35039c0ca6f9SAndy Whitcroft } 350400df344fSAndy Whitcroft } 350500df344fSAndy Whitcroft 35064d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3507f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 35083e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 35093e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 35103e469cdcSAndy Whitcroft if (!defined $stat); 35114d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 35124d001e4dSAndy Whitcroft 35134d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 35144d001e4dSAndy Whitcroft 35159f5af480SJoe Perches # remove inline comments 35169f5af480SJoe Perches $s =~ s/$;/ /g; 35179f5af480SJoe Perches $c =~ s/$;/ /g; 35184d001e4dSAndy Whitcroft 35194d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 35206f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 35216f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 35224d001e4dSAndy Whitcroft 35239f5af480SJoe Perches # Make sure we remove the line prefixes as we have 35249f5af480SJoe Perches # none on the first line, and are going to readd them 35259f5af480SJoe Perches # where necessary. 35269f5af480SJoe Perches $s =~ s/\n./\n/gs; 35279f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 35289f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 35299f5af480SJoe Perches } 35309f5af480SJoe Perches 35314d001e4dSAndy Whitcroft # We want to check the first line inside the block 35324d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 35334d001e4dSAndy Whitcroft # 1) any blank line termination 35344d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 35354d001e4dSAndy Whitcroft # 3) any do (...) { 35364d001e4dSAndy Whitcroft my $continuation = 0; 35374d001e4dSAndy Whitcroft my $check = 0; 35384d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 35394d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 35404d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 35414d001e4dSAndy Whitcroft $continuation = 1; 35424d001e4dSAndy Whitcroft } 35439bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 35444d001e4dSAndy Whitcroft $check = 1; 35454d001e4dSAndy Whitcroft $cond_lines++; 35464d001e4dSAndy Whitcroft } 35474d001e4dSAndy Whitcroft 35484d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 35494d001e4dSAndy Whitcroft # preprocessor statement. 35504d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 35514d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 35524d001e4dSAndy Whitcroft $check = 0; 35534d001e4dSAndy Whitcroft } 35544d001e4dSAndy Whitcroft 35559bd49efeSAndy Whitcroft my $cond_ptr = -1; 3556740504c6SAndy Whitcroft $continuation = 0; 35579bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 35589bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 35594d001e4dSAndy Whitcroft 3560f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3561f16fa28fSAndy Whitcroft # is not linear. 3562f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3563f16fa28fSAndy Whitcroft $check = 0; 3564f16fa28fSAndy Whitcroft } 3565f16fa28fSAndy Whitcroft 35669bd49efeSAndy Whitcroft # Ignore: 35679bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 35689bd49efeSAndy Whitcroft # 2) preprocessor lines, and 35699bd49efeSAndy Whitcroft # 3) labels. 3570740504c6SAndy Whitcroft if ($continuation || 3571740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 35729bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 35739bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3574740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 357530dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 35769bd49efeSAndy Whitcroft $cond_lines++; 35779bd49efeSAndy Whitcroft } 35784d001e4dSAndy Whitcroft } 357930dad6ebSAndy Whitcroft } 35804d001e4dSAndy Whitcroft 35814d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 35824d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 35834d001e4dSAndy Whitcroft 35844d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 35854d001e4dSAndy Whitcroft # this is not this patch's fault. 35864d001e4dSAndy Whitcroft if (!defined($stat_real) || 35874d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 35884d001e4dSAndy Whitcroft $check = 0; 35894d001e4dSAndy Whitcroft } 35904d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 35914d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 35924d001e4dSAndy Whitcroft } 35934d001e4dSAndy Whitcroft 35949bd49efeSAndy 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"; 35954d001e4dSAndy Whitcroft 35969f5af480SJoe Perches if ($check && $s ne '' && 35979f5af480SJoe Perches (($sindent % 8) != 0 || 35989f5af480SJoe Perches ($sindent < $indent) || 3599f6950a73SJoe Perches ($sindent == $indent && 3600f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 36019f5af480SJoe Perches ($sindent > $indent + 8))) { 3602000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3603000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 36044d001e4dSAndy Whitcroft } 36054d001e4dSAndy Whitcroft } 36064d001e4dSAndy Whitcroft 36076c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 36086c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 36091f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 36101f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 36116c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3612c2fdda0dSAndy Whitcroft if ($dbg_values) { 3613c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3614cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3615cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 36161f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3617c2fdda0dSAndy Whitcroft } 36186c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 36196c72ffaaSAndy Whitcroft 362000df344fSAndy Whitcroft#ignore lines not being added 36213705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 362200df344fSAndy Whitcroft 362311ca40a0SJoe Perches# check for dereferences that span multiple lines 362411ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 362511ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 362611ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 362711ca40a0SJoe Perches my $ref = $1; 362811ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 362911ca40a0SJoe Perches $ref .= $1; 363011ca40a0SJoe Perches $ref =~ s/\s//g; 363111ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 363211ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 363311ca40a0SJoe Perches } 363411ca40a0SJoe Perches 3635a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3636c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3637a1ce18e4SJoe Perches my $type = $1; 3638a1ce18e4SJoe Perches my $var = $2; 3639207a8e84SJoe Perches $var = "" if (!defined $var); 3640207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3641a1ce18e4SJoe Perches my $sign = $1; 3642a1ce18e4SJoe Perches my $pointer = $2; 3643a1ce18e4SJoe Perches 3644a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3645a1ce18e4SJoe Perches 3646a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3647a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3648a1ce18e4SJoe Perches $fix) { 3649a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3650207a8e84SJoe Perches my $comp_pointer = $pointer; 3651207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3652207a8e84SJoe Perches $decl .= $comp_pointer; 3653207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3654207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3655a1ce18e4SJoe Perches } 3656a1ce18e4SJoe Perches } 3657a1ce18e4SJoe Perches } 3658a1ce18e4SJoe Perches 3659653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 36607429c690SAndy Whitcroft if ($dbg_type) { 36617429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3662000d1cc1SJoe Perches ERROR("TEST_TYPE", 3663000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 36647429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3665000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3666000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 36677429c690SAndy Whitcroft } 3668653d4876SAndy Whitcroft next; 3669653d4876SAndy Whitcroft } 3670a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3671a1ef277eSAndy Whitcroft if ($dbg_attr) { 36729360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3673000d1cc1SJoe Perches ERROR("TEST_ATTR", 3674000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 36759360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3676000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3677000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3678a1ef277eSAndy Whitcroft } 3679a1ef277eSAndy Whitcroft next; 3680a1ef277eSAndy Whitcroft } 3681653d4876SAndy Whitcroft 3682f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 368399423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 368499423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3685d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3686d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3687f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3688f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3689f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3690d752fcc8SJoe Perches my $fixedline = $prevrawline; 3691d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3692f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3693d752fcc8SJoe Perches $fixedline = $line; 36948d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3695f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3696d752fcc8SJoe Perches } 3697f0a594c1SAndy Whitcroft } 3698f0a594c1SAndy Whitcroft 369900df344fSAndy Whitcroft# 370000df344fSAndy Whitcroft# Checks which are anchored on the added line. 370100df344fSAndy Whitcroft# 370200df344fSAndy Whitcroft 3703653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3704c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3705653d4876SAndy Whitcroft my $path = $1; 3706653d4876SAndy Whitcroft if ($path =~ m{//}) { 3707000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3708495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3709495e9d84SJoe Perches } 3710495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3711495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3712495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3713653d4876SAndy Whitcroft } 3714653d4876SAndy Whitcroft } 3715653d4876SAndy Whitcroft 371600df344fSAndy Whitcroft# no C99 // comments 371700df344fSAndy Whitcroft if ($line =~ m{//}) { 37183705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 37193705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 37203705ce5bSJoe Perches $fix) { 3721194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 37223705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 37233705ce5bSJoe Perches my $comment = trim($1); 3724194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 37253705ce5bSJoe Perches } 37263705ce5bSJoe Perches } 372700df344fSAndy Whitcroft } 372800df344fSAndy Whitcroft # Remove C99 comments. 37290a920b5bSAndy Whitcroft $line =~ s@//.*@@; 37306c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 37310a920b5bSAndy Whitcroft 37322b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 37332b474a1aSAndy Whitcroft# the whole statement. 37342b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 37352b474a1aSAndy Whitcroft if (defined $realline_next && 37362b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 37372b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 37382b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 37392b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 37403cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 37413cbf62dfSAndy Whitcroft # a prefix: 37423cbf62dfSAndy Whitcroft # XXX(foo); 37433cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3744653d4876SAndy Whitcroft my $name = $1; 374587a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 37463cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 37473cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 37483cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 37493cbf62dfSAndy Whitcroft 37503cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 37512b474a1aSAndy Whitcroft \n.}\s*$| 375248012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 375348012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 375448012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 37552b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 37562b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 375748012058SAndy Whitcroft )/x) { 37582b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 37592b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 37602b474a1aSAndy Whitcroft } else { 37612b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 37620a920b5bSAndy Whitcroft } 37630a920b5bSAndy Whitcroft } 37642b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 37652b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 37662b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 37672b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 37682b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 37692b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 37702b474a1aSAndy Whitcroft } 37712b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 37722b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3773000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3774000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 37752b474a1aSAndy Whitcroft } 37760a920b5bSAndy Whitcroft 37775150bda4SJoe Eloff# check for global initialisers. 37786d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3779d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 37806d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3781d5e616fcSJoe Perches $fix) { 37826d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3783d5e616fcSJoe Perches } 3784f0a594c1SAndy Whitcroft } 37850a920b5bSAndy Whitcroft# check for static initialisers. 37866d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3787d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 37886d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3789d5e616fcSJoe Perches $herecurr) && 3790d5e616fcSJoe Perches $fix) { 37916d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3792d5e616fcSJoe Perches } 37930a920b5bSAndy Whitcroft } 37940a920b5bSAndy Whitcroft 37951813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 37961813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 37971813087dSJoe Perches my $tmp = trim($1); 37981813087dSJoe Perches WARN("MISORDERED_TYPE", 37991813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 38001813087dSJoe Perches } 38011813087dSJoe Perches 3802cb710ecaSJoe Perches# check for static const char * arrays. 3803cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3804000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3805000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3806cb710ecaSJoe Perches $herecurr); 3807cb710ecaSJoe Perches } 3808cb710ecaSJoe Perches 3809cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3810cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3811000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3812000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3813cb710ecaSJoe Perches $herecurr); 3814cb710ecaSJoe Perches } 3815cb710ecaSJoe Perches 3816ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 3817ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3818ab7e23f3SJoe Perches my $found = $1; 3819ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3820ab7e23f3SJoe Perches WARN("CONST_CONST", 3821ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3822ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3823ab7e23f3SJoe Perches WARN("CONST_CONST", 3824ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 3825ab7e23f3SJoe Perches } 3826ab7e23f3SJoe Perches } 3827ab7e23f3SJoe Perches 38289b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 38299b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 38309b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 38319b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 38329b0fa60dSJoe Perches $herecurr); 38339b0fa60dSJoe Perches } 38349b0fa60dSJoe Perches 3835b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 3836b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 3837b598b670SJoe Perches my $array = $1; 3838b598b670SJoe 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*\))@) { 3839b598b670SJoe Perches my $array_div = $1; 3840b598b670SJoe Perches if (WARN("ARRAY_SIZE", 3841b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 3842b598b670SJoe Perches $fix) { 3843b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 3844b598b670SJoe Perches } 3845b598b670SJoe Perches } 3846b598b670SJoe Perches } 3847b598b670SJoe Perches 3848b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 3849b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3850b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 3851b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 3852b36190c5SJoe Perches $fix) { 3853194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 3854b36190c5SJoe Perches } 3855b36190c5SJoe Perches } 3856b36190c5SJoe Perches 3857653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 3858653d4876SAndy Whitcroft# make sense. 3859653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 38608054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3861c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 38628ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 386346d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 3864000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 3865000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 38660a920b5bSAndy Whitcroft } 38670a920b5bSAndy Whitcroft 38680a920b5bSAndy Whitcroft# * goes on variable not on type 386965863862SAndy Whitcroft # (char*[ const]) 3870bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 3871bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 38723705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 3873d8aaf121SAndy Whitcroft 387465863862SAndy Whitcroft # Should start with a space. 387565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 387665863862SAndy Whitcroft # Should not end with a space. 387765863862SAndy Whitcroft $to =~ s/\s+$//; 387865863862SAndy Whitcroft # '*'s should not have spaces between. 3879f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 388065863862SAndy Whitcroft } 3881d8aaf121SAndy Whitcroft 38823705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 388365863862SAndy Whitcroft if ($from ne $to) { 38843705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 38853705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 38863705ce5bSJoe Perches $fix) { 38873705ce5bSJoe Perches my $sub_from = $ident; 38883705ce5bSJoe Perches my $sub_to = $ident; 38893705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3890194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38913705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 38923705ce5bSJoe Perches } 389365863862SAndy Whitcroft } 3894bfcb2cc7SAndy Whitcroft } 3895bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 3896bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 38973705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 3898d8aaf121SAndy Whitcroft 389965863862SAndy Whitcroft # Should start with a space. 390065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 390165863862SAndy Whitcroft # Should not end with a space. 390265863862SAndy Whitcroft $to =~ s/\s+$//; 390365863862SAndy Whitcroft # '*'s should not have spaces between. 3904f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 390565863862SAndy Whitcroft } 390665863862SAndy Whitcroft # Modifiers should have spaces. 390765863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 390865863862SAndy Whitcroft 39093705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 3910667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 39113705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 39123705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 39133705ce5bSJoe Perches $fix) { 39143705ce5bSJoe Perches 39153705ce5bSJoe Perches my $sub_from = $match; 39163705ce5bSJoe Perches my $sub_to = $match; 39173705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3918194f66fcSJoe Perches $fixed[$fixlinenr] =~ 39193705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 39203705ce5bSJoe Perches } 392165863862SAndy Whitcroft } 39220a920b5bSAndy Whitcroft } 39230a920b5bSAndy Whitcroft 39249d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 39259d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 39260675a8fbSJean Delvare my $msg_level = \&WARN; 39270675a8fbSJean Delvare $msg_level = \&CHK if ($file); 39280675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 39299d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 39309d3e3c70SJoe Perches } 39310a920b5bSAndy Whitcroft 39329d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 39338905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 3934000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 3935000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 39368905a67cSAndy Whitcroft } 39378905a67cSAndy Whitcroft 393817441227SJoe Perches# check for uses of printk_ratelimit 393917441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 3940000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 3941000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 394217441227SJoe Perches } 394317441227SJoe Perches 3944eeef5733SJoe Perches# printk should use KERN_* levels 3945eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 3946000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 3947eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 394800df344fSAndy Whitcroft } 39490a920b5bSAndy Whitcroft 3950243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 3951243f3803SJoe Perches my $orig = $1; 3952243f3803SJoe Perches my $level = lc($orig); 3953243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 39548f26b837SJoe Perches my $level2 = $level; 39558f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 3956243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 3957daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 3958243f3803SJoe Perches } 3959243f3803SJoe Perches 3960243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 3961d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 3962d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 3963d5e616fcSJoe Perches $fix) { 3964194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3965d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 3966d5e616fcSJoe Perches } 3967243f3803SJoe Perches } 3968243f3803SJoe Perches 3969dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 3970dc139313SJoe Perches my $orig = $1; 3971dc139313SJoe Perches my $level = lc($orig); 3972dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 3973dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 3974dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 3975dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 3976dc139313SJoe Perches } 3977dc139313SJoe Perches 397891c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 397991c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 398091c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 398191c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 398291c9afafSAndy Lutomirski WARN("ENOSYS", 398391c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 398491c9afafSAndy Lutomirski } 398591c9afafSAndy Lutomirski 3986653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 3987653d4876SAndy Whitcroft# or if closed on same line 39885b57980dSJoe Perches if ($perl_version_ok && 39892d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 39902d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 39912d453e3bSJoe Perches $sline !~ /}/) { 39928d182478SJoe Perches if (ERROR("OPEN_BRACE", 39932d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 39948d182478SJoe Perches $fix) { 39958d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 39968d182478SJoe Perches my $fixed_line = $rawline; 39978d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 39988d182478SJoe Perches my $line1 = $1; 39998d182478SJoe Perches my $line2 = $2; 40008d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 40018d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 40028d182478SJoe Perches if ($line2 !~ /^\s*$/) { 40038d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 40048d182478SJoe Perches } 40058d182478SJoe Perches } 40060a920b5bSAndy Whitcroft } 4007653d4876SAndy Whitcroft 40088905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 40098905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 40108905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 40118d182478SJoe Perches if (ERROR("OPEN_BRACE", 40128d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 40138d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 40148d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 40158d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 40168d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 40178d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40188d182478SJoe Perches $fixedline = $rawline; 40198d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 40208d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 40218d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40228d182478SJoe Perches } 40238d182478SJoe Perches } 40248905a67cSAndy Whitcroft } 40258905a67cSAndy Whitcroft 40260c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 40273705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 40283705ce5bSJoe Perches if (WARN("SPACING", 40293705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 40303705ce5bSJoe Perches $fix) { 4031194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40323705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 40333705ce5bSJoe Perches } 40340c73b4ebSAndy Whitcroft } 40350c73b4ebSAndy Whitcroft 403631070b5dSJoe Perches# Function pointer declarations 403731070b5dSJoe Perches# check spacing between type, funcptr, and args 403831070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 403991f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 404031070b5dSJoe Perches my $declare = $1; 404131070b5dSJoe Perches my $pre_pointer_space = $2; 404231070b5dSJoe Perches my $post_pointer_space = $3; 404331070b5dSJoe Perches my $funcname = $4; 404431070b5dSJoe Perches my $post_funcname_space = $5; 404531070b5dSJoe Perches my $pre_args_space = $6; 404631070b5dSJoe Perches 404791f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 404891f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 404991f72e9cSJoe Perches# don't need a space so don't warn for those. 405091f72e9cSJoe Perches my $post_declare_space = ""; 405191f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 405291f72e9cSJoe Perches $post_declare_space = $1; 405391f72e9cSJoe Perches $declare = rtrim($declare); 405491f72e9cSJoe Perches } 405591f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 405631070b5dSJoe Perches WARN("SPACING", 405731070b5dSJoe Perches "missing space after return type\n" . $herecurr); 405891f72e9cSJoe Perches $post_declare_space = " "; 405931070b5dSJoe Perches } 406031070b5dSJoe Perches 406131070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 406291f72e9cSJoe Perches# This test is not currently implemented because these declarations are 406391f72e9cSJoe Perches# equivalent to 406491f72e9cSJoe Perches# int foo(int bar, ...) 406591f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 406691f72e9cSJoe Perches# 406791f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 406891f72e9cSJoe Perches# WARN("SPACING", 406991f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 407091f72e9cSJoe Perches# } 407131070b5dSJoe Perches 407231070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 407331070b5dSJoe Perches if (defined $pre_pointer_space && 407431070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 407531070b5dSJoe Perches WARN("SPACING", 407631070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 407731070b5dSJoe Perches } 407831070b5dSJoe Perches 407931070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 408031070b5dSJoe Perches if (defined $post_pointer_space && 408131070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 408231070b5dSJoe Perches WARN("SPACING", 408331070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 408431070b5dSJoe Perches } 408531070b5dSJoe Perches 408631070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 408731070b5dSJoe Perches if (defined $post_funcname_space && 408831070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 408931070b5dSJoe Perches WARN("SPACING", 409031070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 409131070b5dSJoe Perches } 409231070b5dSJoe Perches 409331070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 409431070b5dSJoe Perches if (defined $pre_args_space && 409531070b5dSJoe Perches $pre_args_space =~ /^\s/) { 409631070b5dSJoe Perches WARN("SPACING", 409731070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 409831070b5dSJoe Perches } 409931070b5dSJoe Perches 410031070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4101194f66fcSJoe Perches $fixed[$fixlinenr] =~ 410291f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 410331070b5dSJoe Perches } 410431070b5dSJoe Perches } 410531070b5dSJoe Perches 41068d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 41078d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4108fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4109fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 41108d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 41118d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 41128d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4113fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 411438dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 41153705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 41163705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 41173705ce5bSJoe Perches $fix) { 4118194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41193705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 41203705ce5bSJoe Perches } 41218d31cfceSAndy Whitcroft } 41228d31cfceSAndy Whitcroft } 41238d31cfceSAndy Whitcroft 4124f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 41256c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4126c2fdda0dSAndy Whitcroft my $name = $1; 4127773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4128773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4129c2fdda0dSAndy Whitcroft 4130c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4131773647a0SAndy Whitcroft if ($name =~ /^(?: 4132773647a0SAndy Whitcroft if|for|while|switch|return|case| 4133773647a0SAndy Whitcroft volatile|__volatile__| 4134773647a0SAndy Whitcroft __attribute__|format|__extension__| 4135773647a0SAndy Whitcroft asm|__asm__)$/x) 4136773647a0SAndy Whitcroft { 4137c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4138c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4139c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4140c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4141773647a0SAndy Whitcroft 4142773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4143c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4144c2fdda0dSAndy Whitcroft 4145c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4146c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4147773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4148c2fdda0dSAndy Whitcroft 4149c2fdda0dSAndy Whitcroft } else { 41503705ce5bSJoe Perches if (WARN("SPACING", 41513705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 41523705ce5bSJoe Perches $fix) { 4153194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41543705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 41553705ce5bSJoe Perches } 4156f0a594c1SAndy Whitcroft } 41576c72ffaaSAndy Whitcroft } 41589a4cad4eSEric Nelson 4159653d4876SAndy Whitcroft# Check operator spacing. 41600a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 41613705ce5bSJoe Perches my $fixed_line = ""; 41623705ce5bSJoe Perches my $line_fixed = 0; 41633705ce5bSJoe Perches 41649c0ca6f9SAndy Whitcroft my $ops = qr{ 41659c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 41669c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 41679c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 41681f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 416984731623SJoe Perches \?:|\?|: 41709c0ca6f9SAndy Whitcroft }x; 4171cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 41723705ce5bSJoe Perches 41733705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 41743705ce5bSJoe Perches## foreach my $el (@elements) { 41753705ce5bSJoe Perches## print("el: <$el>\n"); 41763705ce5bSJoe Perches## } 41773705ce5bSJoe Perches 41783705ce5bSJoe Perches my @fix_elements = (); 417900df344fSAndy Whitcroft my $off = 0; 41806c72ffaaSAndy Whitcroft 41813705ce5bSJoe Perches foreach my $el (@elements) { 41823705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 41833705ce5bSJoe Perches $off += length($el); 41843705ce5bSJoe Perches } 41853705ce5bSJoe Perches 41863705ce5bSJoe Perches $off = 0; 41873705ce5bSJoe Perches 41886c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4189b34c648bSJoe Perches my $last_after = -1; 41906c72ffaaSAndy Whitcroft 41910a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 41923705ce5bSJoe Perches 41933705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 41943705ce5bSJoe Perches 41953705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 41963705ce5bSJoe Perches 41974a0df2efSAndy Whitcroft $off += length($elements[$n]); 41984a0df2efSAndy Whitcroft 419925985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4200773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4201773647a0SAndy Whitcroft my $cc = ''; 4202773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4203773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4204773647a0SAndy Whitcroft } 4205773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4206773647a0SAndy Whitcroft 42074a0df2efSAndy Whitcroft my $a = ''; 42084a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 42094a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4210cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 42114a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 42124a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4213773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 42144a0df2efSAndy Whitcroft 42150a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 42164a0df2efSAndy Whitcroft 42174a0df2efSAndy Whitcroft my $c = ''; 42180a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 42194a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 42204a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4221cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 42224a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 42234a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 42248b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 42254a0df2efSAndy Whitcroft } else { 42264a0df2efSAndy Whitcroft $c = 'E'; 42270a920b5bSAndy Whitcroft } 42280a920b5bSAndy Whitcroft 42294a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 42304a0df2efSAndy Whitcroft 42314a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 42324a0df2efSAndy Whitcroft 42336c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4234de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 42350a920b5bSAndy Whitcroft 423674048ed8SAndy Whitcroft # Pull out the value of this operator. 42376c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 42380a920b5bSAndy Whitcroft 42391f65f947SAndy Whitcroft # Get the full operator variant. 42401f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 42411f65f947SAndy Whitcroft 424213214adfSAndy Whitcroft # Ignore operators passed as parameters. 424313214adfSAndy Whitcroft if ($op_type ne 'V' && 4244d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 424513214adfSAndy Whitcroft 4246cf655043SAndy Whitcroft# # Ignore comments 4247cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 424813214adfSAndy Whitcroft 4249d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 425013214adfSAndy Whitcroft } elsif ($op eq ';') { 4251cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4252cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 42533705ce5bSJoe Perches if (ERROR("SPACING", 42543705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4255b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 42563705ce5bSJoe Perches $line_fixed = 1; 42573705ce5bSJoe Perches } 4258d8aaf121SAndy Whitcroft } 4259d8aaf121SAndy Whitcroft 4260d8aaf121SAndy Whitcroft # // is a comment 4261d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 42620a920b5bSAndy Whitcroft 4263b00e4814SJoe Perches # : when part of a bitfield 4264b00e4814SJoe Perches } elsif ($opv eq ':B') { 4265b00e4814SJoe Perches # skip the bitfield test for now 4266b00e4814SJoe Perches 42671f65f947SAndy Whitcroft # No spaces for: 42681f65f947SAndy Whitcroft # -> 4269b00e4814SJoe Perches } elsif ($op eq '->') { 42704a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 42713705ce5bSJoe Perches if (ERROR("SPACING", 42723705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4273b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 42743705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 42753705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 42763705ce5bSJoe Perches } 4277b34c648bSJoe Perches $line_fixed = 1; 42783705ce5bSJoe Perches } 42790a920b5bSAndy Whitcroft } 42800a920b5bSAndy Whitcroft 42812381097bSJoe Perches # , must not have a space before and must have a space on the right. 42820a920b5bSAndy Whitcroft } elsif ($op eq ',') { 42832381097bSJoe Perches my $rtrim_before = 0; 42842381097bSJoe Perches my $space_after = 0; 42852381097bSJoe Perches if ($ctx =~ /Wx./) { 42862381097bSJoe Perches if (ERROR("SPACING", 42872381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 42882381097bSJoe Perches $line_fixed = 1; 42892381097bSJoe Perches $rtrim_before = 1; 42902381097bSJoe Perches } 42912381097bSJoe Perches } 4292cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 42933705ce5bSJoe Perches if (ERROR("SPACING", 42943705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 42953705ce5bSJoe Perches $line_fixed = 1; 4296b34c648bSJoe Perches $last_after = $n; 42972381097bSJoe Perches $space_after = 1; 42982381097bSJoe Perches } 42992381097bSJoe Perches } 43002381097bSJoe Perches if ($rtrim_before || $space_after) { 43012381097bSJoe Perches if ($rtrim_before) { 43022381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 43032381097bSJoe Perches } else { 43042381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 43052381097bSJoe Perches } 43062381097bSJoe Perches if ($space_after) { 43072381097bSJoe Perches $good .= " "; 43083705ce5bSJoe Perches } 43090a920b5bSAndy Whitcroft } 43100a920b5bSAndy Whitcroft 43119c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 431274048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 43139c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 43149c0ca6f9SAndy Whitcroft 43159c0ca6f9SAndy Whitcroft # unary operators should have a space before and 43169c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 43179c0ca6f9SAndy Whitcroft # unary operator, or a cast 43189c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 431974048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 43200d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4321cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 43223705ce5bSJoe Perches if (ERROR("SPACING", 43233705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4324b34c648bSJoe Perches if ($n != $last_after + 2) { 4325b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 43263705ce5bSJoe Perches $line_fixed = 1; 43273705ce5bSJoe Perches } 43280a920b5bSAndy Whitcroft } 4329b34c648bSJoe Perches } 4330a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4331171ae1a4SAndy Whitcroft # A unary '*' may be const 4332171ae1a4SAndy Whitcroft 4333171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 43343705ce5bSJoe Perches if (ERROR("SPACING", 43353705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4336b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 43373705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 43383705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 43393705ce5bSJoe Perches } 4340b34c648bSJoe Perches $line_fixed = 1; 43413705ce5bSJoe Perches } 43420a920b5bSAndy Whitcroft } 43430a920b5bSAndy Whitcroft 43440a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 43450a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4346773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 43473705ce5bSJoe Perches if (ERROR("SPACING", 43483705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4349b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 43503705ce5bSJoe Perches $line_fixed = 1; 43513705ce5bSJoe Perches } 43520a920b5bSAndy Whitcroft } 4353773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4354773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 43553705ce5bSJoe Perches if (ERROR("SPACING", 43563705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4357b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 43583705ce5bSJoe Perches $line_fixed = 1; 43593705ce5bSJoe Perches } 4360653d4876SAndy Whitcroft } 4361773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 43623705ce5bSJoe Perches if (ERROR("SPACING", 43633705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4364b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 43653705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 43663705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4367773647a0SAndy Whitcroft } 4368b34c648bSJoe Perches $line_fixed = 1; 43693705ce5bSJoe Perches } 43703705ce5bSJoe Perches } 43710a920b5bSAndy Whitcroft 43720a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 43739c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 43749c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 43759c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4376c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4377c2fdda0dSAndy Whitcroft $op eq '%') 43780a920b5bSAndy Whitcroft { 4379d2e025f3SJoe Perches if ($check) { 4380d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4381d2e025f3SJoe Perches if (CHK("SPACING", 4382d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4383d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4384d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4385d2e025f3SJoe Perches $line_fixed = 1; 4386d2e025f3SJoe Perches } 4387d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4388d2e025f3SJoe Perches if (CHK("SPACING", 4389d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4390d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4391d2e025f3SJoe Perches $line_fixed = 1; 4392d2e025f3SJoe Perches } 4393d2e025f3SJoe Perches } 4394d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 43953705ce5bSJoe Perches if (ERROR("SPACING", 43963705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4397b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4398b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4399b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4400b34c648bSJoe Perches } 44013705ce5bSJoe Perches $line_fixed = 1; 44023705ce5bSJoe Perches } 44030a920b5bSAndy Whitcroft } 44040a920b5bSAndy Whitcroft 44051f65f947SAndy Whitcroft # A colon needs no spaces before when it is 44061f65f947SAndy Whitcroft # terminating a case value or a label. 44071f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 44081f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 44093705ce5bSJoe Perches if (ERROR("SPACING", 44103705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4411b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44123705ce5bSJoe Perches $line_fixed = 1; 44133705ce5bSJoe Perches } 44141f65f947SAndy Whitcroft } 44151f65f947SAndy Whitcroft 44160a920b5bSAndy Whitcroft # All the others need spaces both sides. 4417cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 44181f65f947SAndy Whitcroft my $ok = 0; 44191f65f947SAndy Whitcroft 442022f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 44211f65f947SAndy Whitcroft if (($op eq '<' && 44221f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 44231f65f947SAndy Whitcroft ($op eq '>' && 44241f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 44251f65f947SAndy Whitcroft { 44261f65f947SAndy Whitcroft $ok = 1; 44271f65f947SAndy Whitcroft } 44281f65f947SAndy Whitcroft 4429e0df7e1fSJoe Perches # for asm volatile statements 4430e0df7e1fSJoe Perches # ignore a colon with another 4431e0df7e1fSJoe Perches # colon immediately before or after 4432e0df7e1fSJoe Perches if (($op eq ':') && 4433e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4434e0df7e1fSJoe Perches $ok = 1; 4435e0df7e1fSJoe Perches } 4436e0df7e1fSJoe Perches 443784731623SJoe Perches # messages are ERROR, but ?: are CHK 44381f65f947SAndy Whitcroft if ($ok == 0) { 44390675a8fbSJean Delvare my $msg_level = \&ERROR; 44400675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 444184731623SJoe Perches 44420675a8fbSJean Delvare if (&{$msg_level}("SPACING", 44433705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4444b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4445b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4446b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4447b34c648bSJoe Perches } 44483705ce5bSJoe Perches $line_fixed = 1; 44493705ce5bSJoe Perches } 44500a920b5bSAndy Whitcroft } 445122f2a2efSAndy Whitcroft } 44524a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 44533705ce5bSJoe Perches 44543705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 44553705ce5bSJoe Perches 44563705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 44570a920b5bSAndy Whitcroft } 44583705ce5bSJoe Perches 44593705ce5bSJoe Perches if (($#elements % 2) == 0) { 44603705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 44613705ce5bSJoe Perches } 44623705ce5bSJoe Perches 4463194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4464194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 44653705ce5bSJoe Perches } 44663705ce5bSJoe Perches 44673705ce5bSJoe Perches 44680a920b5bSAndy Whitcroft } 44690a920b5bSAndy Whitcroft 4470786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4471d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4472786b6326SJoe Perches if (WARN("SPACING", 4473786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4474786b6326SJoe Perches $fix) { 4475194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4476786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4477786b6326SJoe Perches } 4478786b6326SJoe Perches } 4479786b6326SJoe Perches 4480f0a594c1SAndy Whitcroft# check for multiple assignments 4481f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4482000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4483000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4484f0a594c1SAndy Whitcroft } 4485f0a594c1SAndy Whitcroft 448622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 448722f2a2efSAndy Whitcroft## # continuation. 448822f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 448922f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 449022f2a2efSAndy Whitcroft## 449122f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 449222f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 449322f2a2efSAndy Whitcroft## my $ln = $line; 449422f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 449522f2a2efSAndy Whitcroft## } 449622f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4497000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4498000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 449922f2a2efSAndy Whitcroft## } 450022f2a2efSAndy Whitcroft## } 4501f0a594c1SAndy Whitcroft 45020a920b5bSAndy Whitcroft#need space before brace following if, while, etc 45036b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 45044e5d56bdSEddie Kovsky $line =~ /do\{/) { 45053705ce5bSJoe Perches if (ERROR("SPACING", 45063705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 45073705ce5bSJoe Perches $fix) { 45088d81ae05SCyril Bur $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/; 45093705ce5bSJoe Perches } 4510de7d4f0eSAndy Whitcroft } 4511de7d4f0eSAndy Whitcroft 4512c4a62ef9SJoe Perches## # check for blank lines before declarations 4513c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4514c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4515c4a62ef9SJoe Perches## WARN("SPACING", 4516c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4517c4a62ef9SJoe Perches## } 4518c4a62ef9SJoe Perches## 4519c4a62ef9SJoe Perches 4520de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4521de7d4f0eSAndy Whitcroft# on the line 4522de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 4523d5e616fcSJoe Perches if (ERROR("SPACING", 4524d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4525d5e616fcSJoe Perches $fix) { 4526194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4527d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4528d5e616fcSJoe Perches } 45290a920b5bSAndy Whitcroft } 45300a920b5bSAndy Whitcroft 453122f2a2efSAndy Whitcroft# check spacing on square brackets 453222f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 45333705ce5bSJoe Perches if (ERROR("SPACING", 45343705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 45353705ce5bSJoe Perches $fix) { 4536194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45373705ce5bSJoe Perches s/\[\s+/\[/; 45383705ce5bSJoe Perches } 453922f2a2efSAndy Whitcroft } 454022f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 45413705ce5bSJoe Perches if (ERROR("SPACING", 45423705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 45433705ce5bSJoe Perches $fix) { 4544194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45453705ce5bSJoe Perches s/\s+\]/\]/; 45463705ce5bSJoe Perches } 454722f2a2efSAndy Whitcroft } 454822f2a2efSAndy Whitcroft 4549c45dcabdSAndy Whitcroft# check spacing on parentheses 45509c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 45519c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 45523705ce5bSJoe Perches if (ERROR("SPACING", 45533705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 45543705ce5bSJoe Perches $fix) { 4555194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45563705ce5bSJoe Perches s/\(\s+/\(/; 45573705ce5bSJoe Perches } 455822f2a2efSAndy Whitcroft } 455913214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4560c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4561c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 45623705ce5bSJoe Perches if (ERROR("SPACING", 45633705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 45643705ce5bSJoe Perches $fix) { 4565194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45663705ce5bSJoe Perches s/\s+\)/\)/; 45673705ce5bSJoe Perches } 456822f2a2efSAndy Whitcroft } 456922f2a2efSAndy Whitcroft 4570e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4571e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4572e2826fd0SJoe Perches 4573e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4574ea4acbb1SJoe Perches my $var = $1; 4575ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4576ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4577ea4acbb1SJoe Perches $fix) { 4578ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4579ea4acbb1SJoe Perches } 4580ea4acbb1SJoe Perches } 4581ea4acbb1SJoe Perches 4582ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4583ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4584ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4585ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4586ea4acbb1SJoe Perches my $var = $2; 4587ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4588ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4589ea4acbb1SJoe Perches $fix) { 4590ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4591ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4592ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4593ea4acbb1SJoe Perches } 4594e2826fd0SJoe Perches } 4595e2826fd0SJoe Perches 459663b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4597a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4598a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 45995b57980dSJoe Perches $perl_version_ok && defined($stat) && 460063b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 460163b7c73eSJoe Perches my $if_stat = $1; 460263b7c73eSJoe Perches my $test = substr($2, 1, -1); 460363b7c73eSJoe Perches my $herectx; 460463b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 460563b7c73eSJoe Perches my $match = $1; 460663b7c73eSJoe Perches # avoid parentheses around potential macro args 460763b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 460863b7c73eSJoe Perches if (!defined($herectx)) { 460963b7c73eSJoe Perches $herectx = $here . "\n"; 461063b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 461163b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 461263b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 461363b7c73eSJoe Perches $herectx .= $rl . "\n"; 461463b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 461563b7c73eSJoe Perches } 461663b7c73eSJoe Perches } 461763b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 461863b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 461963b7c73eSJoe Perches } 462063b7c73eSJoe Perches } 462163b7c73eSJoe Perches 46220a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 46234a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 46240a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 46253705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 46263705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 46273705ce5bSJoe Perches $fix) { 4628194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46293705ce5bSJoe Perches s/^(.)\s+/$1/; 46303705ce5bSJoe Perches } 46310a920b5bSAndy Whitcroft } 46320a920b5bSAndy Whitcroft 46335b9553abSJoe Perches# return is not a function 4634507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4635c45dcabdSAndy Whitcroft my $spacing = $1; 46365b57980dSJoe Perches if ($perl_version_ok && 46375b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 46385b9553abSJoe Perches my $value = $1; 46395b9553abSJoe Perches $value = deparenthesize($value); 46405b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4641000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4642000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 46435b9553abSJoe Perches } 4644c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4645000d1cc1SJoe Perches ERROR("SPACING", 4646000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4647c45dcabdSAndy Whitcroft } 4648c45dcabdSAndy Whitcroft } 4649507e5141SJoe Perches 4650b43ae21bSJoe Perches# unnecessary return in a void function 4651b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4652b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4653b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4654b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4655b43ae21bSJoe Perches $linenr >= 3 && 4656b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4657b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 46589819cf25SJoe Perches WARN("RETURN_VOID", 4659b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 46609819cf25SJoe Perches } 46619819cf25SJoe Perches 4662189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 46635b57980dSJoe Perches if ($perl_version_ok && 4664189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4665189248d8SJoe Perches my $openparens = $1; 4666189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4667189248d8SJoe Perches my $msg = ""; 4668189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4669189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4670189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4671189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4672189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4673189248d8SJoe Perches } 4674189248d8SJoe Perches } 4675189248d8SJoe Perches 4676c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4677c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4678c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4679c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 46805b57980dSJoe Perches if ($perl_version_ok && 4681c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4682c5595fa2SJoe Perches my $lead = $1; 4683c5595fa2SJoe Perches my $const = $2; 4684c5595fa2SJoe Perches my $comp = $3; 4685c5595fa2SJoe Perches my $to = $4; 4686c5595fa2SJoe Perches my $newcomp = $comp; 4687f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4688c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4689c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4690c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4691c5595fa2SJoe Perches $fix) { 4692c5595fa2SJoe Perches if ($comp eq "<") { 4693c5595fa2SJoe Perches $newcomp = ">"; 4694c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4695c5595fa2SJoe Perches $newcomp = ">="; 4696c5595fa2SJoe Perches } elsif ($comp eq ">") { 4697c5595fa2SJoe Perches $newcomp = "<"; 4698c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4699c5595fa2SJoe Perches $newcomp = "<="; 4700c5595fa2SJoe Perches } 4701c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4702c5595fa2SJoe Perches } 4703c5595fa2SJoe Perches } 4704c5595fa2SJoe Perches 4705f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4706f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 470753a3c448SAndy Whitcroft my $name = $1; 470853a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4709000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4710f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 471153a3c448SAndy Whitcroft } 471253a3c448SAndy Whitcroft } 4713c45dcabdSAndy Whitcroft 47140a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 47154a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 47163705ce5bSJoe Perches if (ERROR("SPACING", 47173705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 47183705ce5bSJoe Perches $fix) { 4719194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47203705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 47213705ce5bSJoe Perches } 47220a920b5bSAndy Whitcroft } 47230a920b5bSAndy Whitcroft 4724f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4725f5fe35ddSAndy Whitcroft# statements after the conditional. 4726170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 47273e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 47283e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 47293e469cdcSAndy Whitcroft if (!defined $stat); 4730170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4731170d3a22SAndy Whitcroft $remain_next, $off_next); 4732170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4733170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4734170d3a22SAndy Whitcroft 4735170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4736170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4737170d3a22SAndy Whitcroft # then count those as offsets. 4738170d3a22SAndy Whitcroft my ($whitespace) = 4739170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4740170d3a22SAndy Whitcroft my $offset = 4741170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4742170d3a22SAndy Whitcroft 4743170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4744170d3a22SAndy Whitcroft $offset} = 1; 4745170d3a22SAndy Whitcroft } 4746170d3a22SAndy Whitcroft } 4747170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4748c11230f4SJoe Perches defined($stat) && defined($cond) && 4749170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4750171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 47518905a67cSAndy Whitcroft 4752b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4753000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4754000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 47558905a67cSAndy Whitcroft } 47568905a67cSAndy Whitcroft 47578905a67cSAndy Whitcroft # Find out what is on the end of the line after the 47588905a67cSAndy Whitcroft # conditional. 4759773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 47608905a67cSAndy Whitcroft $s =~ s/\n.*//g; 476113214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 476253210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 476353210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4764773647a0SAndy Whitcroft { 4765bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4766bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4767bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 476842bdf74cSHidetoshi Seto my $stat_real = ''; 4769bb44ad39SAndy Whitcroft 477042bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 477142bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4772bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4773bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4774bb44ad39SAndy Whitcroft } 4775bb44ad39SAndy Whitcroft 4776000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4777000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 47788905a67cSAndy Whitcroft } 47798905a67cSAndy Whitcroft } 47808905a67cSAndy Whitcroft 478113214adfSAndy Whitcroft# Check for bitwise tests written as boolean 478213214adfSAndy Whitcroft if ($line =~ / 478313214adfSAndy Whitcroft (?: 478413214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 478513214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 478613214adfSAndy Whitcroft (?:\&\&|\|\|) 478713214adfSAndy Whitcroft | 478813214adfSAndy Whitcroft (?:\&\&|\|\|) 478913214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 479013214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 479113214adfSAndy Whitcroft )/x) 479213214adfSAndy Whitcroft { 4793000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4794000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 479513214adfSAndy Whitcroft } 479613214adfSAndy Whitcroft 47978905a67cSAndy Whitcroft# if and else should not have general statements after it 479813214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 479913214adfSAndy Whitcroft my $s = $1; 480013214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 480113214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4802000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4803000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 48040a920b5bSAndy Whitcroft } 480513214adfSAndy Whitcroft } 480639667782SAndy Whitcroft# if should not continue a brace 480739667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4808000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4809048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 481039667782SAndy Whitcroft $herecurr); 481139667782SAndy Whitcroft } 4812a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4813a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4814a1080bf8SAndy Whitcroft $line !~ /\G(?: 48153fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4816a1080bf8SAndy Whitcroft \s*return\s+ 4817a1080bf8SAndy Whitcroft )/xg) 4818a1080bf8SAndy Whitcroft { 4819000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4820000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4821a1080bf8SAndy Whitcroft } 48220a920b5bSAndy Whitcroft 48230a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 48240a920b5bSAndy Whitcroft # indent level to be relevant to each other. 48258b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 48260a920b5bSAndy Whitcroft $previndent == $indent) { 48278b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 48288b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 48298b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 48308b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 48318b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 48328b8856f4SJoe Perches my $fixedline = $prevrawline; 48338b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 48348b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 48358b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48368b8856f4SJoe Perches } 48378b8856f4SJoe Perches $fixedline = $rawline; 48388b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 48398b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48408b8856f4SJoe Perches } 48410a920b5bSAndy Whitcroft } 48420a920b5bSAndy Whitcroft 48438b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 4844c2fdda0dSAndy Whitcroft $previndent == $indent) { 4845c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 4846c2fdda0dSAndy Whitcroft 4847c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 4848c2fdda0dSAndy Whitcroft # conditional. 4849773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 4850c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 4851c2fdda0dSAndy Whitcroft 4852c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 48538b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 48548b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 48558b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 48568b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 48578b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 48588b8856f4SJoe Perches my $fixedline = $prevrawline; 48598b8856f4SJoe Perches my $trailing = $rawline; 48608b8856f4SJoe Perches $trailing =~ s/^\+//; 48618b8856f4SJoe Perches $trailing = trim($trailing); 48628b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 48638b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48648b8856f4SJoe Perches } 4865c2fdda0dSAndy Whitcroft } 4866c2fdda0dSAndy Whitcroft } 4867c2fdda0dSAndy Whitcroft 486895e2c602SJoe Perches#Specific variable tests 4869323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 4870323c1260SJoe Perches my $var = $1; 487195e2c602SJoe Perches 487295e2c602SJoe Perches#gcc binary extension 487395e2c602SJoe Perches if ($var =~ /^$Binary$/) { 4874d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 4875d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 4876d5e616fcSJoe Perches $fix) { 4877d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 4878194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4879d5e616fcSJoe Perches s/\b$var\b/$hexval/; 4880d5e616fcSJoe Perches } 488195e2c602SJoe Perches } 488295e2c602SJoe Perches 488395e2c602SJoe Perches#CamelCase 4884807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 4885be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 488622735ce8SJoe Perches#Ignore Page<foo> variants 4887807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 488822735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4889f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && 4890f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 4891f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 48927e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 48937e781f67SJoe Perches my $word = $1; 48947e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4895d8b07710SJoe Perches if ($check) { 4896d8b07710SJoe Perches seed_camelcase_includes(); 4897d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 4898d8b07710SJoe Perches seed_camelcase_file($realfile); 4899d8b07710SJoe Perches $camelcase_file_seeded = 1; 4900d8b07710SJoe Perches } 4901d8b07710SJoe Perches } 49027e781f67SJoe Perches if (!defined $camelcase{$word}) { 49037e781f67SJoe Perches $camelcase{$word} = 1; 4904be79794bSJoe Perches CHK("CAMELCASE", 49057e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 49067e781f67SJoe Perches } 4907323c1260SJoe Perches } 4908323c1260SJoe Perches } 49093445686aSJoe Perches } 49100a920b5bSAndy Whitcroft 49110a920b5bSAndy Whitcroft#no spaces allowed after \ in define 4912d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 4913d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 4914d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 4915d5e616fcSJoe Perches $fix) { 4916194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 4917d5e616fcSJoe Perches } 49180a920b5bSAndy Whitcroft } 49190a920b5bSAndy Whitcroft 49200e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 49210e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 4922c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4923e09dec48SAndy Whitcroft my $file = "$1.h"; 4924e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 4925e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 4926e09dec48SAndy Whitcroft $realfile ne $checkfile && 49277840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 4928c45dcabdSAndy Whitcroft { 49290e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 49300e212e0aSFabian Frederick if ($asminclude > 0) { 4931e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 4932000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 4933000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4934e09dec48SAndy Whitcroft } else { 4935000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 4936000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4937e09dec48SAndy Whitcroft } 49380a920b5bSAndy Whitcroft } 49390a920b5bSAndy Whitcroft } 49400e212e0aSFabian Frederick } 49410a920b5bSAndy Whitcroft 4942653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 4943653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 4944cf655043SAndy Whitcroft# in a known good container 4945b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 4946b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 4947d8aaf121SAndy Whitcroft my $ln = $linenr; 4948d8aaf121SAndy Whitcroft my $cnt = $realcnt; 4949c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 4950c45dcabdSAndy Whitcroft my $ctx = ''; 495108a2843eSJoe Perches my $has_flow_statement = 0; 495208a2843eSJoe Perches my $has_arg_concat = 0; 4953c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 4954f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4955f74bd194SAndy Whitcroft $ctx = $dstat; 4956c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4957a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4958c45dcabdSAndy Whitcroft 495908a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 496062e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 496108a2843eSJoe Perches 4962f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 4963f59b64bfSJoe Perches my $define_args = $1; 4964f59b64bfSJoe Perches my $define_stmt = $dstat; 4965f59b64bfSJoe Perches my @def_args = (); 4966f59b64bfSJoe Perches 4967f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 4968f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 4969f59b64bfSJoe Perches $define_args =~ s/\s*//g; 4970f59b64bfSJoe Perches @def_args = split(",", $define_args); 4971f59b64bfSJoe Perches } 4972f59b64bfSJoe Perches 4973292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 4974c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 4975c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 4976c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 4977c45dcabdSAndy Whitcroft 4978c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 4979bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 4980bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 49816b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 4982bf30d6edSAndy Whitcroft { 4983c45dcabdSAndy Whitcroft } 4984c45dcabdSAndy Whitcroft 4985e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 498633acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 498733acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 4988e45bab8eSAndy Whitcroft { 4989e45bab8eSAndy Whitcroft } 4990e45bab8eSAndy Whitcroft 499142e15293SJoe Perches # Make asm volatile uses seem like a generic function 499242e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 499342e15293SJoe Perches 4994c45dcabdSAndy Whitcroft my $exceptions = qr{ 4995c45dcabdSAndy Whitcroft $Declare| 4996c45dcabdSAndy Whitcroft module_param_named| 4997a0a0a7a9SKees Cook MODULE_PARM_DESC| 4998c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 4999c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5000383099fdSAndy Whitcroft __typeof__\(| 500122fd2d3eSStefani Seibold union| 500222fd2d3eSStefani Seibold struct| 5003ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 50046b10df42SVladimir Zapolskiy ^\"|\"$| 50056b10df42SVladimir Zapolskiy ^\[ 5006c45dcabdSAndy Whitcroft }x; 50075eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5008f59b64bfSJoe Perches 5009f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5010f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5011e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5012f59b64bfSJoe Perches 5013f74bd194SAndy Whitcroft if ($dstat ne '' && 5014f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5015f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 50163cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5017356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5018f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5019f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5020e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 502172f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5022f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5023f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5024f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 50254e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5026f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5027c45dcabdSAndy Whitcroft { 5028e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5029e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5030e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5031e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5032f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5033f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5034f74bd194SAndy Whitcroft } else { 5035000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5036388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5037d8aaf121SAndy Whitcroft } 5038f59b64bfSJoe Perches 5039f59b64bfSJoe Perches } 50405207649bSJoe Perches 50415207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 50425207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 50435207649bSJoe Perches my $first = 1; 50445207649bSJoe Perches $define_stmt = ""; 50455207649bSJoe Perches foreach my $l (@stmt_array) { 50465207649bSJoe Perches $l =~ s/\\$//; 50475207649bSJoe Perches if ($first) { 50485207649bSJoe Perches $define_stmt = $l; 50495207649bSJoe Perches $first = 0; 50505207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 50515207649bSJoe Perches $define_stmt .= substr($l, 1); 50525207649bSJoe Perches } 50535207649bSJoe Perches } 50545207649bSJoe Perches $define_stmt =~ s/$;//g; 50555207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 50565207649bSJoe Perches $define_stmt = trim($define_stmt); 50575207649bSJoe Perches 5058f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5059f59b64bfSJoe Perches foreach my $arg (@def_args) { 5060f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 50619192d41aSJoe Perches next if ($arg =~ /^type$/i); 50627fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 50637fe528a2SJoe Perches $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 50647fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 50657fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5066d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5067f59b64bfSJoe Perches if ($use_cnt > 1) { 5068f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5069f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5070f59b64bfSJoe Perches } 50719192d41aSJoe Perches# check if any macro arguments may have other precedence issues 50727fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 50739192d41aSJoe Perches ((defined($1) && $1 ne ',') || 50749192d41aSJoe Perches (defined($2) && $2 ne ','))) { 50759192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 50769192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 50779192d41aSJoe Perches } 50780a920b5bSAndy Whitcroft } 50795023d347SJoe Perches 508008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 508108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 508208a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 508308a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5084e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 508508a2843eSJoe Perches 508608a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 508708a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 508808a2843eSJoe Perches } 508908a2843eSJoe Perches 5090481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 50915023d347SJoe Perches 50925023d347SJoe Perches } else { 50935023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5094481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5095481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 50965023d347SJoe Perches $line =~ /^\+.*\\$/) { 50975023d347SJoe Perches WARN("LINE_CONTINUATIONS", 50985023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 50995023d347SJoe Perches } 5100653d4876SAndy Whitcroft } 51010a920b5bSAndy Whitcroft 5102b13edf7fSJoe Perches# do {} while (0) macro tests: 5103b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5104b13edf7fSJoe Perches# macro should not end with a semicolon 51055b57980dSJoe Perches if ($perl_version_ok && 5106b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5107b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5108b13edf7fSJoe Perches my $ln = $linenr; 5109b13edf7fSJoe Perches my $cnt = $realcnt; 5110b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5111b13edf7fSJoe Perches my $ctx = ''; 5112b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5113b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5114b13edf7fSJoe Perches $ctx = $dstat; 5115b13edf7fSJoe Perches 5116b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 51171b36b201SJoe Perches $dstat =~ s/$;/ /g; 5118b13edf7fSJoe Perches 5119b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5120b13edf7fSJoe Perches my $stmts = $2; 5121b13edf7fSJoe Perches my $semis = $3; 5122b13edf7fSJoe Perches 5123b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5124b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5125e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5126b13edf7fSJoe Perches 5127ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5128ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5129b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5130b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5131b13edf7fSJoe Perches } 5132b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5133b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5134b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5135b13edf7fSJoe Perches } 5136f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5137f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5138f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5139e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5140f5ef95b1SJoe Perches 5141f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5142f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5143b13edf7fSJoe Perches } 5144b13edf7fSJoe Perches } 5145b13edf7fSJoe Perches 5146f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 514713214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 514813214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5149cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 515013214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5151cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5152cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5153aad4f614SJoe Perches my @allowed = (); 5154aad4f614SJoe Perches my $allow = 0; 515513214adfSAndy Whitcroft my $seen = 0; 5156773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5157cf655043SAndy Whitcroft my $ln = $linenr - 1; 515813214adfSAndy Whitcroft for my $chunk (@chunks) { 515913214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 516013214adfSAndy Whitcroft 5161773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5162773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5163773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5164773647a0SAndy Whitcroft 5165aad4f614SJoe Perches $allowed[$allow] = 0; 5166773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5167773647a0SAndy Whitcroft 5168773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5169773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5170773647a0SAndy Whitcroft 5171773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5172cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5173cf655043SAndy Whitcroft 5174773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 517513214adfSAndy Whitcroft 517613214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 517713214adfSAndy Whitcroft 5178aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5179cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5180cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5181aad4f614SJoe Perches $allowed[$allow] = 1; 518213214adfSAndy Whitcroft } 518313214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5184cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5185aad4f614SJoe Perches $allowed[$allow] = 1; 518613214adfSAndy Whitcroft } 5187cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5188cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5189aad4f614SJoe Perches $allowed[$allow] = 1; 519013214adfSAndy Whitcroft } 5191aad4f614SJoe Perches $allow++; 519213214adfSAndy Whitcroft } 5193aad4f614SJoe Perches if ($seen) { 5194aad4f614SJoe Perches my $sum_allowed = 0; 5195aad4f614SJoe Perches foreach (@allowed) { 5196aad4f614SJoe Perches $sum_allowed += $_; 5197aad4f614SJoe Perches } 5198aad4f614SJoe Perches if ($sum_allowed == 0) { 5199000d1cc1SJoe Perches WARN("BRACES", 5200000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5201aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5202aad4f614SJoe Perches $seen != $allow) { 5203aad4f614SJoe Perches CHK("BRACES", 5204aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5205aad4f614SJoe Perches } 520613214adfSAndy Whitcroft } 520713214adfSAndy Whitcroft } 520813214adfSAndy Whitcroft } 5209773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 521013214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5211cf655043SAndy Whitcroft my $allowed = 0; 5212f0a594c1SAndy Whitcroft 5213cf655043SAndy Whitcroft # Check the pre-context. 5214cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5215cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5216cf655043SAndy Whitcroft $allowed = 1; 5217f0a594c1SAndy Whitcroft } 5218773647a0SAndy Whitcroft 5219773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5220773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5221773647a0SAndy Whitcroft 5222cf655043SAndy Whitcroft # Check the condition. 5223cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5224773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5225cf655043SAndy Whitcroft if (defined $cond) { 5226773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5227cf655043SAndy Whitcroft } 5228cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5229cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5230cf655043SAndy Whitcroft $allowed = 1; 5231cf655043SAndy Whitcroft } 5232cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5233cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5234cf655043SAndy Whitcroft $allowed = 1; 5235cf655043SAndy Whitcroft } 5236cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5237cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5238cf655043SAndy Whitcroft $allowed = 1; 5239cf655043SAndy Whitcroft } 5240cf655043SAndy Whitcroft # Check the post-context. 5241cf655043SAndy Whitcroft if (defined $chunks[1]) { 5242cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5243cf655043SAndy Whitcroft if (defined $cond) { 5244773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5245cf655043SAndy Whitcroft } 5246cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5247cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5248cf655043SAndy Whitcroft $allowed = 1; 5249cf655043SAndy Whitcroft } 5250cf655043SAndy Whitcroft } 5251cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5252f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5253e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5254cf655043SAndy Whitcroft 5255000d1cc1SJoe Perches WARN("BRACES", 5256000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5257f0a594c1SAndy Whitcroft } 5258f0a594c1SAndy Whitcroft } 5259f0a594c1SAndy Whitcroft 5260e4c5babdSJoe Perches# check for single line unbalanced braces 526195330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 526295330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5263e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5264e4c5babdSJoe Perches } 5265e4c5babdSJoe Perches 52660979ae66SJoe Perches# check for unnecessary blank lines around braces 526777b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5268f8e58219SJoe Perches if (CHK("BRACES", 5269f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5270f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5271f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5272f8e58219SJoe Perches } 52730979ae66SJoe Perches } 527477b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5275f8e58219SJoe Perches if (CHK("BRACES", 5276f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5277f8e58219SJoe Perches $fix) { 5278f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5279f8e58219SJoe Perches } 52800979ae66SJoe Perches } 52810979ae66SJoe Perches 52824a0df2efSAndy Whitcroft# no volatiles please 52836c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 52846c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5285000d1cc1SJoe Perches WARN("VOLATILE", 52868c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 52874a0df2efSAndy Whitcroft } 52884a0df2efSAndy Whitcroft 52895e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 52905e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 52915e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 52925e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 529333acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 52945e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 52955e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 52965e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 52975e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 52985e4f6ba5SJoe Perches $fix && 52995e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 53005e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 53015e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 53025e4f6ba5SJoe Perches my $comma_close = ""; 53035e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 53045e4f6ba5SJoe Perches $comma_close = $1; 53055e4f6ba5SJoe Perches } 53065e4f6ba5SJoe Perches 53075e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 53085e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 53095e4f6ba5SJoe Perches my $fixedline = $prevrawline; 53105e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 53115e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 53125e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 53135e4f6ba5SJoe Perches $fixedline = $rawline; 53145e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 53155e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 53165e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 53175e4f6ba5SJoe Perches } 53185e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 53195e4f6ba5SJoe Perches } 53205e4f6ba5SJoe Perches } 53215e4f6ba5SJoe Perches 53225e4f6ba5SJoe Perches# check for missing a space in a string concatenation 53235e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 53245e4f6ba5SJoe Perches WARN('MISSING_SPACE', 53255e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 53265e4f6ba5SJoe Perches } 53275e4f6ba5SJoe Perches 532877cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5329e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5330e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5331e4b7d309SJoe Perches# function declarations 533277cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 533377cb8546SJoe Perches defined($context_function) && 5334e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5335e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 533677cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5337e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 533877cb8546SJoe Perches } 533977cb8546SJoe Perches 53405e4f6ba5SJoe Perches# check for spaces before a quoted newline 53415e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 53425e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 53435e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 53445e4f6ba5SJoe Perches $fix) { 53455e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 53465e4f6ba5SJoe Perches } 53475e4f6ba5SJoe Perches 53485e4f6ba5SJoe Perches } 53495e4f6ba5SJoe Perches 5350f17dba4fSJoe Perches# concatenated string without spaces between elements 535179682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 535279682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 535379682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 535479682c0cSJoe Perches $fix) { 535579682c0cSJoe Perches while ($line =~ /($String)/g) { 535679682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 535779682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 535879682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 535979682c0cSJoe Perches } 536079682c0cSJoe Perches } 5361f17dba4fSJoe Perches } 5362f17dba4fSJoe Perches 536390ad30e5SJoe Perches# uncoalesced string fragments 536433acb54aSJoe Perches if ($line =~ /$String\s*"/) { 536579682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 536679682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 536779682c0cSJoe Perches $fix) { 536879682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 536979682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 537079682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 537179682c0cSJoe Perches } 537279682c0cSJoe Perches } 537390ad30e5SJoe Perches } 537490ad30e5SJoe Perches 5375522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5376522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5377522b837cSAlexey Dobriyan my $show_Z = 1; 53785e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5379522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 53805e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5381522b837cSAlexey Dobriyan # check for %L 5382522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 53835e4f6ba5SJoe Perches WARN("PRINTF_L", 5384522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5385522b837cSAlexey Dobriyan $show_L = 0; 53865e4f6ba5SJoe Perches } 5387522b837cSAlexey Dobriyan # check for %Z 5388522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5389522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5390522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5391522b837cSAlexey Dobriyan $show_Z = 0; 5392522b837cSAlexey Dobriyan } 5393522b837cSAlexey Dobriyan # check for 0x<decimal> 5394522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5395522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 53966e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 53976e300757SJoe Perches } 53985e4f6ba5SJoe Perches } 53995e4f6ba5SJoe Perches 54005e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 54013f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 54025e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 54035e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 54045e4f6ba5SJoe Perches } 54055e4f6ba5SJoe Perches 540600df344fSAndy Whitcroft# warn about #if 0 5407c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 5408000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 5409000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 5410de7d4f0eSAndy Whitcroft $herecurr); 54114a0df2efSAndy Whitcroft } 54124a0df2efSAndy Whitcroft 541303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 541403df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5415100425deSJoe Perches my $tested = quotemeta($1); 5416100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5417100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5418100425deSJoe Perches my $func = $1; 5419100425deSJoe Perches if (WARN('NEEDLESS_IF', 5420100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5421100425deSJoe Perches $fix) { 5422100425deSJoe Perches my $do_fix = 1; 5423100425deSJoe Perches my $leading_tabs = ""; 5424100425deSJoe Perches my $new_leading_tabs = ""; 5425100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5426100425deSJoe Perches $leading_tabs = $1; 5427100425deSJoe Perches } else { 5428100425deSJoe Perches $do_fix = 0; 5429100425deSJoe Perches } 5430100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5431100425deSJoe Perches $new_leading_tabs = $1; 5432100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5433100425deSJoe Perches $do_fix = 0; 5434100425deSJoe Perches } 5435100425deSJoe Perches } else { 5436100425deSJoe Perches $do_fix = 0; 5437100425deSJoe Perches } 5438100425deSJoe Perches if ($do_fix) { 5439100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5440100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5441100425deSJoe Perches } 5442100425deSJoe Perches } 54434c432a8fSGreg Kroah-Hartman } 54444c432a8fSGreg Kroah-Hartman } 5445f0a594c1SAndy Whitcroft 5446ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5447ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5448ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5449ebfdc409SJoe Perches (defined $1 || defined $3) && 5450ebfdc409SJoe Perches $linenr > 3) { 5451ebfdc409SJoe Perches my $testval = $2; 5452ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5453ebfdc409SJoe Perches 5454ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5455ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5456ebfdc409SJoe Perches 5457fb0d0e08SJoe 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)/) { 5458ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5459ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5460ebfdc409SJoe Perches } 5461ebfdc409SJoe Perches } 5462ebfdc409SJoe Perches 5463f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5464dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5465f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5466f78d98f6SJoe Perches my $level = $1; 5467f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5468f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5469f78d98f6SJoe Perches $fix) { 5470f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5471f78d98f6SJoe Perches } 5472f78d98f6SJoe Perches } 5473f78d98f6SJoe Perches 547445c55e92SJoe Perches# check for logging continuations 547545c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 547645c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 547745c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 547845c55e92SJoe Perches } 547945c55e92SJoe Perches 5480abb08a53SJoe Perches# check for mask then right shift without a parentheses 54815b57980dSJoe Perches if ($perl_version_ok && 5482abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5483abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5484abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5485abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5486abb08a53SJoe Perches } 5487abb08a53SJoe Perches 5488b75ac618SJoe Perches# check for pointer comparisons to NULL 54895b57980dSJoe Perches if ($perl_version_ok) { 5490b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5491b75ac618SJoe Perches my $val = $1; 5492b75ac618SJoe Perches my $equal = "!"; 5493b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5494b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5495b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5496b75ac618SJoe Perches $fix) { 5497b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5498b75ac618SJoe Perches } 5499b75ac618SJoe Perches } 5500b75ac618SJoe Perches } 5501b75ac618SJoe Perches 55028716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 55038716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 55048716de38SJoe Perches my $attr = $1; 55058716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 55068716de38SJoe Perches my $ptr = $1; 55078716de38SJoe Perches my $var = $2; 55088716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 55098716de38SJoe Perches ERROR("MISPLACED_INIT", 55108716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 55118716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 55128716de38SJoe Perches WARN("MISPLACED_INIT", 55138716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 55148716de38SJoe Perches $fix) { 5515194f66fcSJoe 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; 55168716de38SJoe Perches } 55178716de38SJoe Perches } 55188716de38SJoe Perches } 55198716de38SJoe Perches 5520e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5521e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5522e970b884SJoe Perches my $attr = $1; 5523e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5524e970b884SJoe Perches my $attr_prefix = $1; 5525e970b884SJoe Perches my $attr_type = $2; 5526e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5527e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5528e970b884SJoe Perches $fix) { 5529194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5530e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5531e970b884SJoe Perches } 5532e970b884SJoe Perches } 5533e970b884SJoe Perches 5534e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5535e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5536e970b884SJoe Perches my $attr = $1; 5537e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5538e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5539e970b884SJoe Perches $fix) { 5540194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5541e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5542e970b884SJoe Perches $lead = rtrim($1); 5543e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5544e970b884SJoe Perches $lead = "${lead}const "; 5545194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5546e970b884SJoe Perches } 5547e970b884SJoe Perches } 5548e970b884SJoe Perches 5549c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5550c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5551c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5552c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5553c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5554c17893c7SJoe Perches $fix) { 5555c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5556c17893c7SJoe Perches } 5557c17893c7SJoe Perches } 5558c17893c7SJoe Perches 5559fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5560fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5561fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5562fbdb8138SJoe Perches my $constant_func = $1; 5563fbdb8138SJoe Perches my $func = $constant_func; 5564fbdb8138SJoe Perches $func =~ s/^__constant_//; 5565fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5566fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5567fbdb8138SJoe Perches $fix) { 5568194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5569fbdb8138SJoe Perches } 5570fbdb8138SJoe Perches } 5571fbdb8138SJoe Perches 55721a15a250SPatrick Pannuto# prefer usleep_range over udelay 557337581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 557443c1d77cSJoe Perches my $delay = $1; 55751a15a250SPatrick Pannuto # ignore udelay's < 10, however 557643c1d77cSJoe Perches if (! ($delay < 10) ) { 5577000d1cc1SJoe Perches CHK("USLEEP_RANGE", 557843c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 557943c1d77cSJoe Perches } 558043c1d77cSJoe Perches if ($delay > 2000) { 558143c1d77cSJoe Perches WARN("LONG_UDELAY", 558243c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 55831a15a250SPatrick Pannuto } 55841a15a250SPatrick Pannuto } 55851a15a250SPatrick Pannuto 558609ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 558709ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 558809ef8725SPatrick Pannuto if ($1 < 20) { 5589000d1cc1SJoe Perches WARN("MSLEEP", 559043c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 559109ef8725SPatrick Pannuto } 559209ef8725SPatrick Pannuto } 559309ef8725SPatrick Pannuto 559436ec1939SJoe Perches# check for comparisons of jiffies 559536ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 559636ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 559736ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 559836ec1939SJoe Perches } 559936ec1939SJoe Perches 56009d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 56019d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 56029d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 56039d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 56049d7a34a5SJoe Perches } 56059d7a34a5SJoe Perches 560600df344fSAndy Whitcroft# warn about #ifdefs in C files 5607c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 560800df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 560900df344fSAndy Whitcroft# print "$herecurr"; 561000df344fSAndy Whitcroft# $clean = 0; 561100df344fSAndy Whitcroft# } 561200df344fSAndy Whitcroft 561322f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5614c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 56153705ce5bSJoe Perches if (ERROR("SPACING", 56163705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 56173705ce5bSJoe Perches $fix) { 5618194f66fcSJoe Perches $fixed[$fixlinenr] =~ 56193705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 56203705ce5bSJoe Perches } 56213705ce5bSJoe Perches 562222f2a2efSAndy Whitcroft } 562322f2a2efSAndy Whitcroft 56244a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5625171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5626171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 56274a0df2efSAndy Whitcroft my $which = $1; 56284a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5629000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5630000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 56314a0df2efSAndy Whitcroft } 56324a0df2efSAndy Whitcroft } 56334a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5634402c2553SMichael S. Tsirkin 5635402c2553SMichael S. Tsirkin my $barriers = qr{ 5636402c2553SMichael S. Tsirkin mb| 5637402c2553SMichael S. Tsirkin rmb| 5638402c2553SMichael S. Tsirkin wmb| 5639402c2553SMichael S. Tsirkin read_barrier_depends 5640402c2553SMichael S. Tsirkin }x; 5641402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5642402c2553SMichael S. Tsirkin mb__before_atomic| 5643402c2553SMichael S. Tsirkin mb__after_atomic| 5644402c2553SMichael S. Tsirkin store_release| 5645402c2553SMichael S. Tsirkin load_acquire| 5646402c2553SMichael S. Tsirkin store_mb| 5647402c2553SMichael S. Tsirkin (?:$barriers) 5648402c2553SMichael S. Tsirkin }x; 5649402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5650402c2553SMichael S. Tsirkin (?:$barriers)| 565143e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 565243e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5653402c2553SMichael S. Tsirkin }x; 5654402c2553SMichael S. Tsirkin 5655402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 56564a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5657c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5658000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 56594a0df2efSAndy Whitcroft } 56604a0df2efSAndy Whitcroft } 56613ad81779SPaul E. McKenney 5662f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5663f4073b0fSMichael S. Tsirkin 5664f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5665f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5666f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5667f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5668f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5669f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5670f4073b0fSMichael S. Tsirkin } 5671f4073b0fSMichael S. Tsirkin 5672cb426e99SJoe Perches# check for waitqueue_active without a comment. 5673cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5674cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5675cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5676cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5677cb426e99SJoe Perches } 5678cb426e99SJoe Perches } 56793ad81779SPaul E. McKenney 568091db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends 568191db2592SPaul E. McKenney if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { 568291db2592SPaul E. McKenney WARN("READ_BARRIER_DEPENDS", 568391db2592SPaul E. McKenney "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); 568491db2592SPaul E. McKenney } 568591db2592SPaul E. McKenney 56864a0df2efSAndy Whitcroft# check of hardware specific defines 5687c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5688000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5689000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 56900a920b5bSAndy Whitcroft } 5691653d4876SAndy Whitcroft 5692596ed45bSJoe Perches# check that the storage class is not after a type 5693596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5694000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5695596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5696596ed45bSJoe Perches } 5697596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5698596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5699596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5700596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5701596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5702596ed45bSJoe Perches WARN("STORAGE_CLASS", 5703596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5704d4977c78STobias Klauser } 5705d4977c78STobias Klauser 5706de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5707de7d4f0eSAndy Whitcroft# storage class and type. 57089c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 57099c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5710000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5711000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5712de7d4f0eSAndy Whitcroft } 5713de7d4f0eSAndy Whitcroft 57148905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 57152b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57162b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5717d5e616fcSJoe Perches if (WARN("INLINE", 5718d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5719d5e616fcSJoe Perches $fix) { 5720194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5721d5e616fcSJoe Perches 5722d5e616fcSJoe Perches } 57238905a67cSAndy Whitcroft } 57248905a67cSAndy Whitcroft 57253d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 57262b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57272b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5728000d1cc1SJoe Perches WARN("PREFER_PACKED", 5729000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 57303d130fd0SJoe Perches } 57313d130fd0SJoe Perches 573239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 57332b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57342b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5735000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5736000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 573739b7e287SJoe Perches } 573839b7e287SJoe Perches 57395f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 57402b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57412b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5742d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5743d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5744d5e616fcSJoe Perches $fix) { 5745194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5746d5e616fcSJoe Perches 5747d5e616fcSJoe Perches } 57485f14d3bdSJoe Perches } 57495f14d3bdSJoe Perches 57506061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 57512b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57522b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5753d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5754d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5755d5e616fcSJoe Perches $fix) { 5756194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5757d5e616fcSJoe Perches } 57586061d949SJoe Perches } 57596061d949SJoe Perches 5760619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 57615b57980dSJoe Perches if ($perl_version_ok && 5762619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5763619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5764619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5765619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5766619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5767619a908aSJoe Perches } 5768619a908aSJoe Perches 5769fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 5770e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5771fd39f904STomas Winkler $realfile !~ m@\btools/@ && 5772e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5773e6176fa4SJoe Perches my $type = $1; 5774e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5775e6176fa4SJoe Perches $type = $1; 5776e6176fa4SJoe Perches my $kernel_type = 'u'; 5777e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5778e6176fa4SJoe Perches $type =~ /(\d+)/; 5779e6176fa4SJoe Perches $kernel_type .= $1; 5780e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5781e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5782e6176fa4SJoe Perches $fix) { 5783e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5784e6176fa4SJoe Perches } 5785e6176fa4SJoe Perches } 5786e6176fa4SJoe Perches } 5787e6176fa4SJoe Perches 5788938224b5SJoe Perches# check for cast of C90 native int or longer types constants 5789938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 5790938224b5SJoe Perches my $cast = $1; 5791938224b5SJoe Perches my $const = $2; 5792938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 5793938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 5794938224b5SJoe Perches $fix) { 5795938224b5SJoe Perches my $suffix = ""; 5796938224b5SJoe Perches my $newconst = $const; 5797938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 5798938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 5799938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 5800938224b5SJoe Perches $suffix .= 'LL'; 5801938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 5802938224b5SJoe Perches $suffix .= 'L'; 5803938224b5SJoe Perches } 5804938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 5805938224b5SJoe Perches } 5806938224b5SJoe Perches } 5807938224b5SJoe Perches 58088f53a9b8SJoe Perches# check for sizeof(&) 58098f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5810000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5811000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 58128f53a9b8SJoe Perches } 58138f53a9b8SJoe Perches 581466c80b60SJoe Perches# check for sizeof without parenthesis 581566c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 5816d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 5817d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 5818d5e616fcSJoe Perches $fix) { 5819194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 5820d5e616fcSJoe Perches } 582166c80b60SJoe Perches } 582266c80b60SJoe Perches 582388982feaSJoe Perches# check for struct spinlock declarations 582488982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 582588982feaSJoe Perches WARN("USE_SPINLOCK_T", 582688982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 582788982feaSJoe Perches } 582888982feaSJoe Perches 5829a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 583006668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 5831a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 5832caac1d5fSHeba Aamer $fmt =~ s/%%//g; 5833caac1d5fSHeba Aamer if ($fmt !~ /%/) { 5834d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 5835d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 5836d5e616fcSJoe Perches $fix) { 5837194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 5838d5e616fcSJoe Perches } 5839a6962d72SJoe Perches } 5840a6962d72SJoe Perches } 5841a6962d72SJoe Perches 58420b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 58435b57980dSJoe Perches if ($perl_version_ok && 58440b523769SJoe Perches defined $stat && 58450b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 58460b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 5847e3c6bc95STobin C. Harding my $stat_real; 5848e3c6bc95STobin C. Harding 58490b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 58500b523769SJoe Perches $lc = $lc + $linenr; 58510b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 5852ffe07513SJoe Perches my $specifier; 5853ffe07513SJoe Perches my $extension; 5854ffe07513SJoe Perches my $bad_specifier = ""; 58550b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 58560b523769SJoe Perches $fmt =~ s/%%//g; 5857e3c6bc95STobin C. Harding 5858e3c6bc95STobin C. Harding while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) { 5859e3c6bc95STobin C. Harding $specifier = $1; 5860e3c6bc95STobin C. Harding $extension = $2; 5861e3c6bc95STobin C. Harding if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) { 5862e3c6bc95STobin C. Harding $bad_specifier = $specifier; 58630b523769SJoe Perches last; 58640b523769SJoe Perches } 5865e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 5866e3c6bc95STobin C. Harding if (!defined($stat_real)) { 5867e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 58680b523769SJoe Perches } 5869e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 5870e3c6bc95STobin 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"); 5871e3c6bc95STobin C. Harding } 5872e3c6bc95STobin C. Harding } 5873e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 58742a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 58751df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 58761df7338aSSergey Senozhatsky my $use = ""; 5877e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 58781df7338aSSergey Senozhatsky $ext_type = "Deprecated"; 58791df7338aSSergey Senozhatsky $use = " - use %pS instead"; 5880e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 58811df7338aSSergey Senozhatsky } 58822a9f9d85STobin C. Harding 58830b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 5884e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 5885e3c6bc95STobin C. Harding } 58860b523769SJoe Perches } 58870b523769SJoe Perches } 58880b523769SJoe Perches 5889554e165cSAndy Whitcroft# Check for misused memsets 58905b57980dSJoe Perches if ($perl_version_ok && 5891d1fe9c09SJoe Perches defined $stat && 58929e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 5893554e165cSAndy Whitcroft 5894d7c76ba7SJoe Perches my $ms_addr = $2; 5895d1fe9c09SJoe Perches my $ms_val = $7; 5896d1fe9c09SJoe Perches my $ms_size = $12; 5897d7c76ba7SJoe Perches 5898554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 5899554e165cSAndy Whitcroft ERROR("MEMSET", 5900d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 5901554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 5902554e165cSAndy Whitcroft WARN("MEMSET", 5903d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 5904d7c76ba7SJoe Perches } 5905d7c76ba7SJoe Perches } 5906d7c76ba7SJoe Perches 590798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 59085b57980dSJoe Perches# if ($perl_version_ok && 5909f333195dSJoe Perches# defined $stat && 5910f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5911f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 5912f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 5913f333195dSJoe Perches# $fix) { 5914f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 5915f333195dSJoe Perches# } 5916f333195dSJoe Perches# } 591798a9bba5SJoe Perches 5918b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 59195b57980dSJoe Perches# if ($perl_version_ok && 5920f333195dSJoe Perches# defined $stat && 5921f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5922f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 5923f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 5924f333195dSJoe Perches# } 5925b6117d17SMateusz Kulikowski 59268617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 59278617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 59285b57980dSJoe Perches# if ($perl_version_ok && 5929f333195dSJoe Perches# defined $stat && 5930f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5931f333195dSJoe Perches# 5932f333195dSJoe Perches# my $ms_val = $7; 5933f333195dSJoe Perches# 5934f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 5935f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 5936f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 5937f333195dSJoe Perches# $fix) { 5938f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 5939f333195dSJoe Perches# } 5940f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 5941f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 5942f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 5943f333195dSJoe Perches# $fix) { 5944f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 5945f333195dSJoe Perches# } 5946f333195dSJoe Perches# } 5947f333195dSJoe Perches# } 59488617cd09SMateusz Kulikowski 5949d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 59505b57980dSJoe Perches if ($perl_version_ok && 5951d1fe9c09SJoe Perches defined $stat && 5952d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 5953d1fe9c09SJoe Perches if (defined $2 || defined $7) { 5954d7c76ba7SJoe Perches my $call = $1; 5955d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 5956d7c76ba7SJoe Perches my $arg1 = $3; 5957d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 5958d1fe9c09SJoe Perches my $arg2 = $8; 5959d7c76ba7SJoe Perches my $cast; 5960d7c76ba7SJoe Perches 5961d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 5962d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 5963d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 5964d7c76ba7SJoe Perches $cast = $cast1; 5965d7c76ba7SJoe Perches } else { 5966d7c76ba7SJoe Perches $cast = $cast2; 5967d7c76ba7SJoe Perches } 5968d7c76ba7SJoe Perches WARN("MINMAX", 5969d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 5970554e165cSAndy Whitcroft } 5971554e165cSAndy Whitcroft } 5972554e165cSAndy Whitcroft 59734a273195SJoe Perches# check usleep_range arguments 59745b57980dSJoe Perches if ($perl_version_ok && 59754a273195SJoe Perches defined $stat && 59764a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 59774a273195SJoe Perches my $min = $1; 59784a273195SJoe Perches my $max = $7; 59794a273195SJoe Perches if ($min eq $max) { 59804a273195SJoe Perches WARN("USLEEP_RANGE", 59814a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 59824a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 59834a273195SJoe Perches $min > $max) { 59844a273195SJoe Perches WARN("USLEEP_RANGE", 59854a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 59864a273195SJoe Perches } 59874a273195SJoe Perches } 59884a273195SJoe Perches 5989823b794cSJoe Perches# check for naked sscanf 59905b57980dSJoe Perches if ($perl_version_ok && 5991823b794cSJoe Perches defined $stat && 59926c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 5993823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 5994823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 5995823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 5996823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 5997823b794cSJoe Perches $lc = $lc + $linenr; 59982a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 5999823b794cSJoe Perches WARN("NAKED_SSCANF", 6000823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6001823b794cSJoe Perches } 6002823b794cSJoe Perches 6003afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 60045b57980dSJoe Perches if ($perl_version_ok && 6005afc819abSJoe Perches defined $stat && 6006afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6007afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6008afc819abSJoe Perches $lc = $lc + $linenr; 60092a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6010afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6011afc819abSJoe Perches my $format = $6; 6012afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6013afc819abSJoe Perches if ($count == 1 && 6014afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6015afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6016afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6017afc819abSJoe Perches } 6018afc819abSJoe Perches } 6019afc819abSJoe Perches } 6020afc819abSJoe Perches 602170dc8a48SJoe Perches# check for new externs in .h files. 602270dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 602370dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6024d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 602570dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 602670dc8a48SJoe Perches $fix) { 6027194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 602870dc8a48SJoe Perches } 602970dc8a48SJoe Perches } 603070dc8a48SJoe Perches 6031de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6032171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6033c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6034171ae1a4SAndy Whitcroft { 6035c45dcabdSAndy Whitcroft my $function_name = $1; 6036c45dcabdSAndy Whitcroft my $paren_space = $2; 6037171ae1a4SAndy Whitcroft 6038171ae1a4SAndy Whitcroft my $s = $stat; 6039171ae1a4SAndy Whitcroft if (defined $cond) { 6040171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6041171ae1a4SAndy Whitcroft } 6042c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 6043c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 6044c45dcabdSAndy Whitcroft { 6045000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6046000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6047de7d4f0eSAndy Whitcroft } 6048de7d4f0eSAndy Whitcroft 6049171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6050000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6051000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6052171ae1a4SAndy Whitcroft } 60539c9ba34eSAndy Whitcroft 60549c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 60559c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 60569c9ba34eSAndy Whitcroft { 6057000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6058000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6059171ae1a4SAndy Whitcroft } 6060171ae1a4SAndy Whitcroft 6061a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6062a0ad7596SJoe Perches if (defined $stat && 606325bdda2bSMiles Chen $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6064ca0d8929SJoe Perches $1 ne "void") { 6065ca0d8929SJoe Perches my $args = trim($1); 6066ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6067ca0d8929SJoe Perches my $arg = trim($1); 6068ca0d8929SJoe Perches if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6069ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6070ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6071ca0d8929SJoe Perches } 6072ca0d8929SJoe Perches } 6073ca0d8929SJoe Perches } 6074ca0d8929SJoe Perches 6075a0ad7596SJoe Perches# check for function definitions 60765b57980dSJoe Perches if ($perl_version_ok && 6077a0ad7596SJoe Perches defined $stat && 6078a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6079a0ad7596SJoe Perches $context_function = $1; 6080a0ad7596SJoe Perches 6081a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6082a0ad7596SJoe Perches my $ok = 0; 6083a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6084a0ad7596SJoe Perches my $herectx = $here . "\n"; 6085a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6086a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6087a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6088a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6089a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6090a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6091a0ad7596SJoe Perches } 6092a0ad7596SJoe Perches if (!$ok) { 6093a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6094a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6095a0ad7596SJoe Perches } 6096a0ad7596SJoe Perches } 6097a0ad7596SJoe Perches 6098de7d4f0eSAndy Whitcroft# checks for new __setup's 6099de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6100de7d4f0eSAndy Whitcroft my $name = $1; 6101de7d4f0eSAndy Whitcroft 6102de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6103000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 61048c27ceffSMauro Carvalho Chehab "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); 6105de7d4f0eSAndy Whitcroft } 6106653d4876SAndy Whitcroft } 61079c0ca6f9SAndy Whitcroft 61089c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 6109caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 6110000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6111000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 61129c0ca6f9SAndy Whitcroft } 611313214adfSAndy Whitcroft 6114a640d25cSJoe Perches# alloc style 6115a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 61165b57980dSJoe Perches if ($perl_version_ok && 6117a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6118a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6119a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6120a640d25cSJoe Perches } 6121a640d25cSJoe Perches 612260a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 61235b57980dSJoe Perches if ($perl_version_ok && 61241b4a2ed4SJoe Perches defined $stat && 61251b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 612660a55369SJoe Perches my $oldfunc = $3; 612760a55369SJoe Perches my $a1 = $4; 612860a55369SJoe Perches my $a2 = $10; 612960a55369SJoe Perches my $newfunc = "kmalloc_array"; 613060a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 613160a55369SJoe Perches my $r1 = $a1; 613260a55369SJoe Perches my $r2 = $a2; 613360a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 613460a55369SJoe Perches $r1 = $a2; 613560a55369SJoe Perches $r2 = $a1; 613660a55369SJoe Perches } 6137e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6138e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 61391b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6140e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6141e3d95a2aSTobin C. Harding 6142e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 61431b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 61441b4a2ed4SJoe Perches $cnt == 1 && 6145e367455aSJoe Perches $fix) { 6146194f66fcSJoe 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; 614760a55369SJoe Perches } 614860a55369SJoe Perches } 614960a55369SJoe Perches } 615060a55369SJoe Perches 6151972fdea2SJoe Perches# check for krealloc arg reuse 61525b57980dSJoe Perches if ($perl_version_ok && 6153972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 6154972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6155972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6156972fdea2SJoe Perches } 6157972fdea2SJoe Perches 61585ce59ae0SJoe Perches# check for alloc argument mismatch 61595ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 61605ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 61615ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 61625ce59ae0SJoe Perches } 61635ce59ae0SJoe Perches 6164caf2a54fSJoe Perches# check for multiple semicolons 6165caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6166d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6167d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6168d5e616fcSJoe Perches $fix) { 6169194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6170d5e616fcSJoe Perches } 6171d1e2ad07SJoe Perches } 6172d1e2ad07SJoe Perches 6173cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6174cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6175cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 61760ab90191SJoe Perches my $ull = ""; 61770ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 61780ab90191SJoe Perches if (CHK("BIT_MACRO", 61790ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 61800ab90191SJoe Perches $fix) { 61810ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 61820ab90191SJoe Perches } 61830ab90191SJoe Perches } 61840ab90191SJoe Perches 61852d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 61862d632745SJoe 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*$/) { 61872d632745SJoe Perches my $config = $1; 61882d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 61892d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 61902d632745SJoe Perches $fix) { 61912d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 61922d632745SJoe Perches } 61932d632745SJoe Perches } 61942d632745SJoe Perches 6195e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6196c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6197c34c09a8SJoe Perches my $has_break = 0; 6198c34c09a8SJoe Perches my $has_statement = 0; 6199c34c09a8SJoe Perches my $count = 0; 6200c34c09a8SJoe Perches my $prevline = $linenr; 6201e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6202c34c09a8SJoe Perches $prevline--; 6203c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6204c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6205c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6206c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6207c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6208c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6209c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6210c34c09a8SJoe Perches $has_statement = 1; 6211c34c09a8SJoe Perches $count++; 6212258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6213c34c09a8SJoe Perches } 6214c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6215c34c09a8SJoe Perches WARN("MISSING_BREAK", 6216224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6217c34c09a8SJoe Perches } 6218c34c09a8SJoe Perches } 6219c34c09a8SJoe Perches 6220d1e2ad07SJoe Perches# check for switch/default statements without a break; 62215b57980dSJoe Perches if ($perl_version_ok && 6222d1e2ad07SJoe Perches defined $stat && 6223d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6224d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6225e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6226e3d95a2aSTobin C. Harding 6227d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6228d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6229caf2a54fSJoe Perches } 6230caf2a54fSJoe Perches 623113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6232d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6233d5e616fcSJoe Perches if (WARN("USE_FUNC", 6234d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6235d5e616fcSJoe Perches $fix) { 6236194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6237d5e616fcSJoe Perches } 623813214adfSAndy Whitcroft } 6239773647a0SAndy Whitcroft 624062ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 624162ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 624262ec818fSJoe Perches ERROR("DATE_TIME", 624362ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 624462ec818fSJoe Perches } 624562ec818fSJoe Perches 62462c92488aSJoe Perches# check for use of yield() 62472c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 62482c92488aSJoe Perches WARN("YIELD", 62492c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 62502c92488aSJoe Perches } 62512c92488aSJoe Perches 6252179f8f40SJoe Perches# check for comparisons against true and false 6253179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6254179f8f40SJoe Perches my $lead = $1; 6255179f8f40SJoe Perches my $arg = $2; 6256179f8f40SJoe Perches my $test = $3; 6257179f8f40SJoe Perches my $otype = $4; 6258179f8f40SJoe Perches my $trail = $5; 6259179f8f40SJoe Perches my $op = "!"; 6260179f8f40SJoe Perches 6261179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6262179f8f40SJoe Perches 6263179f8f40SJoe Perches my $type = lc($otype); 6264179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6265179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6266179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6267179f8f40SJoe Perches $op = ""; 6268179f8f40SJoe Perches } 6269179f8f40SJoe Perches 6270179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6271179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6272179f8f40SJoe Perches 6273179f8f40SJoe Perches## maybe suggesting a correct construct would better 6274179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6275179f8f40SJoe Perches 6276179f8f40SJoe Perches } 6277179f8f40SJoe Perches } 6278179f8f40SJoe Perches 62795d430902SJoe Perches# check for bool bitfields 62805d430902SJoe Perches if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) { 62815d430902SJoe Perches WARN("BOOL_BITFIELD", 62825d430902SJoe Perches "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr); 62835d430902SJoe Perches } 62845d430902SJoe Perches 6285d729593eSJoe Perches# check for bool use in .h files 6286d729593eSJoe Perches if ($realfile =~ /\.h$/ && 6287d729593eSJoe Perches $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) { 6288d729593eSJoe Perches CHK("BOOL_MEMBER", 6289d729593eSJoe Perches "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr); 6290d729593eSJoe Perches } 6291d729593eSJoe Perches 62924882720bSThomas Gleixner# check for semaphores initialized locked 62934882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6294000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6295000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6296773647a0SAndy Whitcroft } 62976712d858SJoe Perches 629867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 629967d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6300000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 630167d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6302773647a0SAndy Whitcroft } 63036712d858SJoe Perches 6304ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6305f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6306000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6307ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6308f3db6639SMichael Ellerman } 63096712d858SJoe Perches 63100f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6311d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 63126903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6313d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6314000d1cc1SJoe Perches WARN("CONST_STRUCT", 6315d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 63162b6db5cbSAndy Whitcroft } 6317773647a0SAndy Whitcroft 6318773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6319773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6320773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6321c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6322c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6323171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6324171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6325171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6326773647a0SAndy Whitcroft { 6327000d1cc1SJoe Perches WARN("NR_CPUS", 6328000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6329773647a0SAndy Whitcroft } 63309c9ba34eSAndy Whitcroft 633152ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 633252ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 633352ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 633452ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 633552ea8506SJoe Perches } 633652ea8506SJoe Perches 6337acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 63385b57980dSJoe Perches if ($perl_version_ok && 6339acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6340acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6341acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6342acd9362cSJoe Perches } 6343acd9362cSJoe Perches 6344691d77b6SAndy Whitcroft# whine mightly about in_atomic 6345691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6346691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6347000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6348000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6349f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6350000d1cc1SJoe Perches WARN("IN_ATOMIC", 6351000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6352691d77b6SAndy Whitcroft } 6353691d77b6SAndy Whitcroft } 63541704f47bSPeter Zijlstra 63550f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 63560f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 63570f5225b0SPeter Zijlstra ERROR("LOCKING", 63580f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 63590f5225b0SPeter Zijlstra } 63600f5225b0SPeter Zijlstra 63611704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 63621704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 63631704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 63641704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 63651704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 63661704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6367000d1cc1SJoe Perches ERROR("LOCKDEP", 6368000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 63691704f47bSPeter Zijlstra } 63701704f47bSPeter Zijlstra } 637188f8831cSDave Jones 6372b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6373b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6374000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6375000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 637688f8831cSDave Jones } 63772435880fSJoe Perches 637800180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 637900180468SJoe Perches# and whether or not function naming is typical and if 638000180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 63815b57980dSJoe Perches if ($perl_version_ok && 638200180468SJoe Perches defined $stat && 638300180468SJoe 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*\)/) { 638400180468SJoe Perches my $var = $1; 638500180468SJoe Perches my $perms = $2; 638600180468SJoe Perches my $show = $3; 638700180468SJoe Perches my $store = $4; 638800180468SJoe Perches my $octal_perms = perms_to_octal($perms); 638900180468SJoe Perches if ($show =~ /^${var}_show$/ && 639000180468SJoe Perches $store =~ /^${var}_store$/ && 639100180468SJoe Perches $octal_perms eq "0644") { 639200180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 639300180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 639400180468SJoe Perches $fix) { 639500180468SJoe 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})/; 639600180468SJoe Perches } 639700180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 639800180468SJoe Perches $store =~ /^NULL$/ && 639900180468SJoe Perches $octal_perms eq "0444") { 640000180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 640100180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 640200180468SJoe Perches $fix) { 640300180468SJoe 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})/; 640400180468SJoe Perches } 640500180468SJoe Perches } elsif ($show =~ /^NULL$/ && 640600180468SJoe Perches $store =~ /^${var}_store$/ && 640700180468SJoe Perches $octal_perms eq "0200") { 640800180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 640900180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 641000180468SJoe Perches $fix) { 641100180468SJoe 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})/; 641200180468SJoe Perches } 641300180468SJoe Perches } elsif ($octal_perms eq "0644" || 641400180468SJoe Perches $octal_perms eq "0444" || 641500180468SJoe Perches $octal_perms eq "0200") { 641600180468SJoe Perches my $newshow = "$show"; 641700180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 641800180468SJoe Perches my $newstore = $store; 641900180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 642000180468SJoe Perches my $rename = ""; 642100180468SJoe Perches if ($show ne $newshow) { 642200180468SJoe Perches $rename .= " '$show' to '$newshow'"; 642300180468SJoe Perches } 642400180468SJoe Perches if ($store ne $newstore) { 642500180468SJoe Perches $rename .= " '$store' to '$newstore'"; 642600180468SJoe Perches } 642700180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 642800180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 642900180468SJoe Perches } else { 643000180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 643100180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 643200180468SJoe Perches } 643300180468SJoe Perches } 643400180468SJoe Perches 6435515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6436515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 643773121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 643873121534SJoe Perches# specific definition of not visible in sysfs. 643973121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 644073121534SJoe Perches# use the default permissions 64415b57980dSJoe Perches if ($perl_version_ok && 6442459cf0aeSJoe Perches defined $stat && 6443515a235eSJoe Perches $line =~ /$mode_perms_search/) { 64442435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 64452435880fSJoe Perches my $func = $entry->[0]; 64462435880fSJoe Perches my $arg_pos = $entry->[1]; 64472435880fSJoe Perches 6448459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6449459cf0aeSJoe Perches $lc = $lc + $linenr; 64502a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6451459cf0aeSJoe Perches 64522435880fSJoe Perches my $skip_args = ""; 64532435880fSJoe Perches if ($arg_pos > 1) { 64542435880fSJoe Perches $arg_pos--; 64552435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 64562435880fSJoe Perches } 6457f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6458459cf0aeSJoe Perches if ($stat =~ /$test/) { 64592435880fSJoe Perches my $val = $1; 64602435880fSJoe Perches $val = $6 if ($skip_args ne ""); 646173121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 646273121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 646373121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 64642435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6465459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6466f90774e1SJoe Perches } 6467f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6468c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6469459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 64702435880fSJoe Perches } 6471459cf0aeSJoe Perches } 6472459cf0aeSJoe Perches } 6473459cf0aeSJoe Perches } 6474459cf0aeSJoe Perches 6475459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6476bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 647700180468SJoe Perches my $oval = $1; 647800180468SJoe Perches my $octal = perms_to_octal($oval); 6479f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6480459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6481f90774e1SJoe Perches $fix) { 648200180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 64832435880fSJoe Perches } 648413214adfSAndy Whitcroft } 64855a6d20ceSBjorn Andersson 64865a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 64875a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 64885a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 64895a6d20ceSBjorn Andersson my $valid_licenses = qr{ 64905a6d20ceSBjorn Andersson GPL| 64915a6d20ceSBjorn Andersson GPL\ v2| 64925a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 64935a6d20ceSBjorn Andersson Dual\ BSD/GPL| 64945a6d20ceSBjorn Andersson Dual\ MIT/GPL| 64955a6d20ceSBjorn Andersson Dual\ MPL/GPL| 64965a6d20ceSBjorn Andersson Proprietary 64975a6d20ceSBjorn Andersson }x; 64985a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 64995a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 65005a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 65015a6d20ceSBjorn Andersson } 65025a6d20ceSBjorn Andersson } 6503515a235eSJoe Perches } 650413214adfSAndy Whitcroft 650513214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 650613214adfSAndy Whitcroft # so just keep quiet. 650713214adfSAndy Whitcroft if ($#rawlines == -1) { 650813214adfSAndy Whitcroft exit(0); 65090a920b5bSAndy Whitcroft } 65100a920b5bSAndy Whitcroft 65118905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 65128905a67cSAndy Whitcroft # things that appear to be patches. 65138905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 65148905a67cSAndy Whitcroft exit(0); 65158905a67cSAndy Whitcroft } 65168905a67cSAndy Whitcroft 65178905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 65188905a67cSAndy Whitcroft # just keep quiet. 65198905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 65208905a67cSAndy Whitcroft exit(0); 65218905a67cSAndy Whitcroft } 65228905a67cSAndy Whitcroft 6523a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6524000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6525000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 65260a920b5bSAndy Whitcroft } 6527*cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6528*cd261496SGeert Uytterhoeven if ($signoff == 0) { 6529000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6530000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 6531*cd261496SGeert Uytterhoeven } elsif (!$authorsignoff) { 6532*cd261496SGeert Uytterhoeven WARN("NO_AUTHOR_SIGN_OFF", 6533*cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 6534*cd261496SGeert Uytterhoeven } 65350a920b5bSAndy Whitcroft } 65360a920b5bSAndy Whitcroft 6537f0a594c1SAndy Whitcroft print report_dump(); 653813214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 653913214adfSAndy Whitcroft print "$filename " if ($summary_file); 65406c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 65416c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 65426c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 65436c72ffaaSAndy Whitcroft } 65448905a67cSAndy Whitcroft 6545d2c0a235SAndy Whitcroft if ($quiet == 0) { 6546ef212196SJoe Perches # If there were any defects found and not already fixing them 6547ef212196SJoe Perches if (!$clean and !$fix) { 6548ef212196SJoe Perches print << "EOM" 6549ef212196SJoe Perches 6550ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6551ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6552ef212196SJoe PerchesEOM 6553ef212196SJoe Perches } 6554d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6555d2c0a235SAndy Whitcroft # then suggest that. 6556d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6557b0781216SMike Frysinger $rpt_cleaners = 0; 6558d8469f16SJoe Perches print << "EOM" 6559d8469f16SJoe Perches 6560d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6561d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6562d8469f16SJoe PerchesEOM 6563d2c0a235SAndy Whitcroft } 6564d2c0a235SAndy Whitcroft } 6565d2c0a235SAndy Whitcroft 6566d752fcc8SJoe Perches if ($clean == 0 && $fix && 6567d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6568d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 65699624b8d6SJoe Perches my $newfile = $filename; 65709624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 65713705ce5bSJoe Perches my $linecount = 0; 65723705ce5bSJoe Perches my $f; 65733705ce5bSJoe Perches 6574d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6575d752fcc8SJoe Perches 65763705ce5bSJoe Perches open($f, '>', $newfile) 65773705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 65783705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 65793705ce5bSJoe Perches $linecount++; 65803705ce5bSJoe Perches if ($file) { 65813705ce5bSJoe Perches if ($linecount > 3) { 65823705ce5bSJoe Perches $fixed_line =~ s/^\+//; 65833705ce5bSJoe Perches print $f $fixed_line . "\n"; 65843705ce5bSJoe Perches } 65853705ce5bSJoe Perches } else { 65863705ce5bSJoe Perches print $f $fixed_line . "\n"; 65873705ce5bSJoe Perches } 65883705ce5bSJoe Perches } 65893705ce5bSJoe Perches close($f); 65903705ce5bSJoe Perches 65913705ce5bSJoe Perches if (!$quiet) { 65923705ce5bSJoe Perches print << "EOM"; 6593d8469f16SJoe Perches 65943705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 65953705ce5bSJoe Perches 65963705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 65973705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 65983705ce5bSJoe Perches 65993705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 66003705ce5bSJoe PerchesNo warranties, expressed or implied... 66013705ce5bSJoe PerchesEOM 66023705ce5bSJoe Perches } 66033705ce5bSJoe Perches } 66043705ce5bSJoe Perches 6605d8469f16SJoe Perches if ($quiet == 0) { 6606d8469f16SJoe Perches print "\n"; 6607d8469f16SJoe Perches if ($clean == 1) { 6608d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6609d8469f16SJoe Perches } else { 6610d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 66110a920b5bSAndy Whitcroft } 66120a920b5bSAndy Whitcroft } 66130a920b5bSAndy Whitcroft return $clean; 66140a920b5bSAndy Whitcroft} 6615