1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl 2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0 3882ea1d6SJoe Perches# 4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit) 500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit) 62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) 7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]> 8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]> 90a920b5bSAndy Whitcroft 100a920b5bSAndy Whitcroftuse strict; 11cb77f0d6SKamil Rytarowskiuse warnings; 12c707a81dSJoe Perchesuse POSIX; 1336061e38SJoe Perchesuse File::Basename; 1436061e38SJoe Perchesuse Cwd 'abs_path'; 1557230297SJoe Perchesuse Term::ANSIColor qw(:constants); 16cd261496SGeert Uytterhoevenuse Encode qw(decode encode); 170a920b5bSAndy Whitcroft 180a920b5bSAndy Whitcroftmy $P = $0; 1936061e38SJoe Perchesmy $D = dirname(abs_path($P)); 200a920b5bSAndy Whitcroft 21000d1cc1SJoe Perchesmy $V = '0.32'; 220a920b5bSAndy Whitcroft 230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 240a920b5bSAndy Whitcroft 250a920b5bSAndy Whitcroftmy $quiet = 0; 260a920b5bSAndy Whitcroftmy $tree = 1; 270a920b5bSAndy Whitcroftmy $chk_signoff = 1; 280a920b5bSAndy Whitcroftmy $chk_patch = 1; 29773647a0SAndy Whitcroftmy $tst_only; 306c72ffaaSAndy Whitcroftmy $emacs = 0; 318905a67cSAndy Whitcroftmy $terse = 0; 3234d8815fSJoe Perchesmy $showfile = 0; 336c72ffaaSAndy Whitcroftmy $file = 0; 344a593c34SDu, Changbinmy $git = 0; 350dea9f1eSJoe Perchesmy %git_commits = (); 366c72ffaaSAndy Whitcroftmy $check = 0; 372ac73b4fSJoe Perchesmy $check_orig = 0; 388905a67cSAndy Whitcroftmy $summary = 1; 398905a67cSAndy Whitcroftmy $mailback = 0; 4013214adfSAndy Whitcroftmy $summary_file = 0; 41000d1cc1SJoe Perchesmy $show_types = 0; 423beb42ecSJoe Perchesmy $list_types = 0; 433705ce5bSJoe Perchesmy $fix = 0; 449624b8d6SJoe Perchesmy $fix_inplace = 0; 456c72ffaaSAndy Whitcroftmy $root; 46c2fdda0dSAndy Whitcroftmy %debug; 473445686aSJoe Perchesmy %camelcase = (); 4891bfe484SJoe Perchesmy %use_type = (); 4991bfe484SJoe Perchesmy @use = (); 5091bfe484SJoe Perchesmy %ignore_type = (); 51000d1cc1SJoe Perchesmy @ignore = (); 5277f5b10aSHannes Edermy $help = 0; 53000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 546cd7f386SJoe Perchesmy $max_line_length = 80; 55d62a201fSDave Hansenmy $ignore_perl_version = 0; 56d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5756193274SVadim Bendeburymy $min_conf_desc_length = 4; 5866b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 59ebfd7d62SJoe Perchesmy $codespell = 0; 60f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 61bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch"; 6275ad8c57SJerome Forissiermy $typedefsfile = ""; 63737c0767SJohn Brooksmy $color = "auto"; 6498005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE 65dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE 66dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git'; 6777f5b10aSHannes Eder 6877f5b10aSHannes Edersub help { 6977f5b10aSHannes Eder my ($exitcode) = @_; 7077f5b10aSHannes Eder 7177f5b10aSHannes Eder print << "EOM"; 7277f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 7377f5b10aSHannes EderVersion: $V 7477f5b10aSHannes Eder 7577f5b10aSHannes EderOptions: 7677f5b10aSHannes Eder -q, --quiet quiet 7777f5b10aSHannes Eder --no-tree run without a kernel tree 7877f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 7977f5b10aSHannes Eder --patch treat FILE as patchfile (default) 8077f5b10aSHannes Eder --emacs emacs compile window format 8177f5b10aSHannes Eder --terse one line per report 8234d8815fSJoe Perches --showfile emit diffed file position, not input file position 834a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 844a593c34SDu, Changbin single git commit with: 854a593c34SDu, Changbin <rev> 864a593c34SDu, Changbin <rev>^ 874a593c34SDu, Changbin <rev>~n 884a593c34SDu, Changbin multiple git commits with: 894a593c34SDu, Changbin <rev1>..<rev2> 904a593c34SDu, Changbin <rev1>...<rev2> 914a593c34SDu, Changbin <rev>-<count> 924a593c34SDu, Changbin git merges are ignored 9377f5b10aSHannes Eder -f, --file treat FILE as regular source file 9477f5b10aSHannes Eder --subjective, --strict enable more subjective tests 953beb42ecSJoe Perches --list-types list the possible message types 9691bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 97000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 983beb42ecSJoe Perches --show-types show the specific message type in the output 996cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 10056193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 10177f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 10277f5b10aSHannes Eder --no-summary suppress the per-file summary 10377f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 10477f5b10aSHannes Eder --summary-file include the filename in summary 10577f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 10677f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 10777f5b10aSHannes Eder is all off) 10877f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 10977f5b10aSHannes Eder literally 1103705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1113705ce5bSJoe Perches If correctable single-line errors exist, create 1123705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1133705ce5bSJoe Perches with potential errors corrected to the preferred 1143705ce5bSJoe Perches checkpatch style 1159624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1169624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1179624b8d6SJoe Perches file. It's your fault if there's no backup or git 118d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 119d62a201fSDave Hansen runtime errors. 120ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 121f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 122ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 12375ad8c57SJerome Forissier --typedefsfile Read additional types from this file 124737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 125737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 12677f5b10aSHannes Eder -h, --help, --version display this help and exit 12777f5b10aSHannes Eder 12877f5b10aSHannes EderWhen FILE is - read standard input. 12977f5b10aSHannes EderEOM 13077f5b10aSHannes Eder 13177f5b10aSHannes Eder exit($exitcode); 13277f5b10aSHannes Eder} 13377f5b10aSHannes Eder 1343beb42ecSJoe Perchessub uniq { 1353beb42ecSJoe Perches my %seen; 1363beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1373beb42ecSJoe Perches} 1383beb42ecSJoe Perches 1393beb42ecSJoe Perchessub list_types { 1403beb42ecSJoe Perches my ($exitcode) = @_; 1413beb42ecSJoe Perches 1423beb42ecSJoe Perches my $count = 0; 1433beb42ecSJoe Perches 1443beb42ecSJoe Perches local $/ = undef; 1453beb42ecSJoe Perches 1463beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1473beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1483beb42ecSJoe Perches 1493beb42ecSJoe Perches my $text = <$script>; 1503beb42ecSJoe Perches close($script); 1513beb42ecSJoe Perches 1523beb42ecSJoe Perches my @types = (); 1530547fa58SJean Delvare # Also catch when type or level is passed through a variable 1540547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1553beb42ecSJoe Perches push (@types, $_); 1563beb42ecSJoe Perches } 1573beb42ecSJoe Perches @types = sort(uniq(@types)); 1583beb42ecSJoe Perches print("#\tMessage type\n\n"); 1593beb42ecSJoe Perches foreach my $type (@types) { 1603beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1613beb42ecSJoe Perches } 1623beb42ecSJoe Perches 1633beb42ecSJoe Perches exit($exitcode); 1643beb42ecSJoe Perches} 1653beb42ecSJoe Perches 166000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 167000d1cc1SJoe Perchesif (-f $conf) { 168000d1cc1SJoe Perches my @conf_args; 169000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 170000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 171000d1cc1SJoe Perches 172000d1cc1SJoe Perches while (<$conffile>) { 173000d1cc1SJoe Perches my $line = $_; 174000d1cc1SJoe Perches 175000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 176000d1cc1SJoe Perches $line =~ s/^\s*//g; 177000d1cc1SJoe Perches $line =~ s/\s+/ /g; 178000d1cc1SJoe Perches 179000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 180000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 181000d1cc1SJoe Perches 182000d1cc1SJoe Perches my @words = split(" ", $line); 183000d1cc1SJoe Perches foreach my $word (@words) { 184000d1cc1SJoe Perches last if ($word =~ m/^#/); 185000d1cc1SJoe Perches push (@conf_args, $word); 186000d1cc1SJoe Perches } 187000d1cc1SJoe Perches } 188000d1cc1SJoe Perches close($conffile); 189000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 190000d1cc1SJoe Perches} 191000d1cc1SJoe Perches 192737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 193737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 194737c0767SJohn Brooksforeach (@ARGV) { 195737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 196737c0767SJohn Brooks $_ = "--color=$color"; 197737c0767SJohn Brooks } 198737c0767SJohn Brooks} 199737c0767SJohn Brooks 2000a920b5bSAndy WhitcroftGetOptions( 2016c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 2020a920b5bSAndy Whitcroft 'tree!' => \$tree, 2030a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 2040a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2056c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2068905a67cSAndy Whitcroft 'terse!' => \$terse, 20734d8815fSJoe Perches 'showfile!' => \$showfile, 20877f5b10aSHannes Eder 'f|file!' => \$file, 2094a593c34SDu, Changbin 'g|git!' => \$git, 2106c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2116c72ffaaSAndy Whitcroft 'strict!' => \$check, 212000d1cc1SJoe Perches 'ignore=s' => \@ignore, 21391bfe484SJoe Perches 'types=s' => \@use, 214000d1cc1SJoe Perches 'show-types!' => \$show_types, 2153beb42ecSJoe Perches 'list-types!' => \$list_types, 2166cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 21756193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 2186c72ffaaSAndy Whitcroft 'root=s' => \$root, 2198905a67cSAndy Whitcroft 'summary!' => \$summary, 2208905a67cSAndy Whitcroft 'mailback!' => \$mailback, 22113214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2223705ce5bSJoe Perches 'fix!' => \$fix, 2239624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 224d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 225c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 226773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 227ebfd7d62SJoe Perches 'codespell!' => \$codespell, 228ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 22975ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 230737c0767SJohn Brooks 'color=s' => \$color, 231737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 232737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 23377f5b10aSHannes Eder 'h|help' => \$help, 23477f5b10aSHannes Eder 'version' => \$help 23577f5b10aSHannes Eder) or help(1); 23677f5b10aSHannes Eder 23777f5b10aSHannes Ederhelp(0) if ($help); 2380a920b5bSAndy Whitcroft 2393beb42ecSJoe Percheslist_types(0) if ($list_types); 2403beb42ecSJoe Perches 2419624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2422ac73b4fSJoe Perches$check_orig = $check; 2439624b8d6SJoe Perches 2440a920b5bSAndy Whitcroftmy $exit = 0; 2450a920b5bSAndy Whitcroft 2465b57980dSJoe Perchesmy $perl_version_ok = 1; 247d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 2485b57980dSJoe Perches $perl_version_ok = 0; 249d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 2505b57980dSJoe Perches exit(1) if (!$ignore_perl_version); 251d62a201fSDave Hansen} 252d62a201fSDave Hansen 25345107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2540a920b5bSAndy Whitcroftif ($#ARGV < 0) { 25545107ff6SAllen Hubbe push(@ARGV, '-'); 2560a920b5bSAndy Whitcroft} 2570a920b5bSAndy Whitcroft 258737c0767SJohn Brooksif ($color =~ /^[01]$/) { 259737c0767SJohn Brooks $color = !$color; 260737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 261737c0767SJohn Brooks $color = 1; 262737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 263737c0767SJohn Brooks $color = 0; 264737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 265737c0767SJohn Brooks $color = (-t STDOUT); 266737c0767SJohn Brooks} else { 267737c0767SJohn Brooks die "Invalid color mode: $color\n"; 268737c0767SJohn Brooks} 269737c0767SJohn Brooks 27091bfe484SJoe Perchessub hash_save_array_words { 27191bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 27291bfe484SJoe Perches 27391bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 27491bfe484SJoe Perches foreach my $word (@array) { 275000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 276000d1cc1SJoe Perches $word =~ s/^\s*//g; 277000d1cc1SJoe Perches $word =~ s/\s+/ /g; 278000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 279000d1cc1SJoe Perches 280000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 281000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 282000d1cc1SJoe Perches 28391bfe484SJoe Perches $hashRef->{$word}++; 284000d1cc1SJoe Perches } 28591bfe484SJoe Perches} 28691bfe484SJoe Perches 28791bfe484SJoe Perchessub hash_show_words { 28891bfe484SJoe Perches my ($hashRef, $prefix) = @_; 28991bfe484SJoe Perches 2903c816e49SJoe Perches if (keys %$hashRef) { 291d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 29258cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 29391bfe484SJoe Perches print " $word"; 29491bfe484SJoe Perches } 295d8469f16SJoe Perches print "\n"; 29691bfe484SJoe Perches } 29791bfe484SJoe Perches} 29891bfe484SJoe Perches 29991bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 30091bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 301000d1cc1SJoe Perches 302c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 303c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 3047429c690SAndy Whitcroftmy $dbg_type = 0; 305a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 306c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 30721caa13cSAndy Whitcroft ## no critic 30821caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 30921caa13cSAndy Whitcroft die "$@" if ($@); 310c2fdda0dSAndy Whitcroft} 311c2fdda0dSAndy Whitcroft 312d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 313d2c0a235SAndy Whitcroft 3148905a67cSAndy Whitcroftif ($terse) { 3158905a67cSAndy Whitcroft $emacs = 1; 3168905a67cSAndy Whitcroft $quiet++; 3178905a67cSAndy Whitcroft} 3188905a67cSAndy Whitcroft 3196c72ffaaSAndy Whitcroftif ($tree) { 3206c72ffaaSAndy Whitcroft if (defined $root) { 3216c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3226c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3236c72ffaaSAndy Whitcroft } 3246c72ffaaSAndy Whitcroft } else { 3256c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3266c72ffaaSAndy Whitcroft $root = '.'; 3276c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3286c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3296c72ffaaSAndy Whitcroft $root = $1; 3306c72ffaaSAndy Whitcroft } 3316c72ffaaSAndy Whitcroft } 3326c72ffaaSAndy Whitcroft 3336c72ffaaSAndy Whitcroft if (!defined $root) { 3340a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3350a920b5bSAndy Whitcroft exit(2); 3360a920b5bSAndy Whitcroft } 3376c72ffaaSAndy Whitcroft} 3386c72ffaaSAndy Whitcroft 3396c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3406c72ffaaSAndy Whitcroft 3412ceb532bSAndy Whitcroftour $Ident = qr{ 3422ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3432ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3442ceb532bSAndy Whitcroft }x; 3456c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3466c72ffaaSAndy Whitcroftour $Sparse = qr{ 3476c72ffaaSAndy Whitcroft __user| 3486c72ffaaSAndy Whitcroft __kernel| 3496c72ffaaSAndy Whitcroft __force| 3506c72ffaaSAndy Whitcroft __iomem| 3516c72ffaaSAndy Whitcroft __must_check| 352417495edSAndy Whitcroft __kprobes| 353165e72a6SSven Eckelmann __ref| 35433aa4597SGeert Uytterhoeven __refconst| 35533aa4597SGeert Uytterhoeven __refdata| 356ad315455SBoqun Feng __rcu| 357ad315455SBoqun Feng __private 3586c72ffaaSAndy Whitcroft }x; 359e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 360e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 361e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 362e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 363e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3648716de38SJoe Perches 36552131292SWolfram Sang# Notes to $Attribute: 36652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3676c72ffaaSAndy Whitcroftour $Attribute = qr{ 3686c72ffaaSAndy Whitcroft const| 36903f1df7dSJoe Perches __percpu| 37003f1df7dSJoe Perches __nocast| 37103f1df7dSJoe Perches __safe| 37246d832f5SMichael S. Tsirkin __bitwise| 37303f1df7dSJoe Perches __packed__| 37403f1df7dSJoe Perches __packed2__| 37503f1df7dSJoe Perches __naked| 37603f1df7dSJoe Perches __maybe_unused| 37703f1df7dSJoe Perches __always_unused| 37803f1df7dSJoe Perches __noreturn| 37903f1df7dSJoe Perches __used| 38003f1df7dSJoe Perches __cold| 381e23ef1f3SJoe Perches __pure| 38203f1df7dSJoe Perches __noclone| 38303f1df7dSJoe Perches __deprecated| 3846c72ffaaSAndy Whitcroft __read_mostly| 385c5967e98SJoe Perches __ro_after_init| 3866c72ffaaSAndy Whitcroft __kprobes| 3878716de38SJoe Perches $InitAttribute| 38824e1d81aSAndy Whitcroft ____cacheline_aligned| 38924e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3905fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3915fe3af11SAndy Whitcroft __weak 3926c72ffaaSAndy Whitcroft }x; 393c45dcabdSAndy Whitcroftour $Modifier; 39491cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 3956c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 3966c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 3976c72ffaaSAndy Whitcroft 39895e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 39995e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 40095e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 40195e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 4022435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 403c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 404326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 405326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 406326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 40774349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4082435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 409326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 410447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 41123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4126c72ffaaSAndy Whitcroftour $Operators = qr{ 4136c72ffaaSAndy Whitcroft <=|>=|==|!=| 4146c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 41523f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4166c72ffaaSAndy Whitcroft }x; 4176c72ffaaSAndy Whitcroft 41891cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 41991cb5195SJoe Perches 420ab7e23f3SJoe Perchesour $BasicType; 4218905a67cSAndy Whitcroftour $NonptrType; 4221813087dSJoe Perchesour $NonptrTypeMisordered; 4238716de38SJoe Perchesour $NonptrTypeWithAttr; 4248905a67cSAndy Whitcroftour $Type; 4251813087dSJoe Perchesour $TypeMisordered; 4268905a67cSAndy Whitcroftour $Declare; 4271813087dSJoe Perchesour $DeclareMisordered; 4288905a67cSAndy Whitcroft 42915662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 43015662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 431171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 432171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 433171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 434171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 435171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 436171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 437171ae1a4SAndy Whitcroft}x; 438171ae1a4SAndy Whitcroft 43915662b3eSJoe Perchesour $UTF8 = qr{ 44015662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 44115662b3eSJoe Perches | $NON_ASCII_UTF8 44215662b3eSJoe Perches}x; 44315662b3eSJoe Perches 444e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 445021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 446021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 447021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 448021158b4SJoe Perches)}; 449e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 450fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4518ed22cadSAndy Whitcroft atomic_t 4528ed22cadSAndy Whitcroft)}; 453e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 454e6176fa4SJoe Perches $typeC99Typedefs\b| 455e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 456e6176fa4SJoe Perches $typeKernelTypedefs\b 457e6176fa4SJoe Perches)}; 4588ed22cadSAndy Whitcroft 4596d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4606d32f7a3SJoe Perches 461691e669bSJoe Perchesour $logFunctions = qr{(?x: 462758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4637d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 46487bd499aSJoe Perches TP_printk| 4656e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 466b0531722SJoe Perches panic| 46706668727SJoe Perches MODULE_[A-Z_]+| 46806668727SJoe Perches seq_vprintf|seq_printf|seq_puts 469691e669bSJoe Perches)}; 470691e669bSJoe Perches 471e29a70f1SJoe Perchesour $allocFunctions = qr{(?x: 472e29a70f1SJoe Perches (?:(?:devm_)? 473e29a70f1SJoe Perches (?:kv|k|v)[czm]alloc(?:_node|_array)? | 474e29a70f1SJoe Perches kstrdup(?:_const)? | 475e29a70f1SJoe Perches kmemdup(?:_nul)?) | 476e29a70f1SJoe Perches (?:\w+)?alloc_skb(?:ip_align)? | 477e29a70f1SJoe Perches # dev_alloc_skb/netdev_alloc_skb, et al 478e29a70f1SJoe Perches dma_alloc_coherent 479e29a70f1SJoe Perches)}; 480e29a70f1SJoe Perches 48120112475SJoe Perchesour $signature_tags = qr{(?xi: 48220112475SJoe Perches Signed-off-by:| 483d499480cSJorge Ramirez-Ortiz Co-developed-by:| 48420112475SJoe Perches Acked-by:| 48520112475SJoe Perches Tested-by:| 48620112475SJoe Perches Reviewed-by:| 48720112475SJoe Perches Reported-by:| 4888543ae12SMugunthan V N Suggested-by:| 48920112475SJoe Perches To:| 49020112475SJoe Perches Cc: 49120112475SJoe Perches)}; 49220112475SJoe Perches 4931813087dSJoe Perchesour @typeListMisordered = ( 4941813087dSJoe Perches qr{char\s+(?:un)?signed}, 4951813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 4961813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 4971813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 4981813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 4991813087dSJoe Perches qr{short\s+(?:un)?signed}, 5001813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 5011813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 5021813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 5031813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 5041813087dSJoe Perches qr{int\s+(?:un)?signed}, 5051813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 5061813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 5071813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 5081813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 5091813087dSJoe Perches qr{long\s+(?:un)?signed}, 5101813087dSJoe Perches); 5111813087dSJoe Perches 5128905a67cSAndy Whitcroftour @typeList = ( 5138905a67cSAndy Whitcroft qr{void}, 5140c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 5150c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 5160c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 5170c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5180c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5190c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5200c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5210c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5220c773d9dSJoe Perches qr{(?:un)?signed}, 5238905a67cSAndy Whitcroft qr{float}, 5248905a67cSAndy Whitcroft qr{double}, 5258905a67cSAndy Whitcroft qr{bool}, 5268905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5278905a67cSAndy Whitcroft qr{union\s+$Ident}, 5288905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5298905a67cSAndy Whitcroft qr{${Ident}_t}, 5308905a67cSAndy Whitcroft qr{${Ident}_handler}, 5318905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5321813087dSJoe Perches @typeListMisordered, 5338905a67cSAndy Whitcroft); 534938224b5SJoe Perches 535938224b5SJoe Perchesour $C90_int_types = qr{(?x: 536938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 537938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 538938224b5SJoe Perches long\s+long\s+(?:un)?signed| 539938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 540938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 541938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 542938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 543938224b5SJoe Perches 544938224b5SJoe Perches long\s+int\s+(?:un)?signed| 545938224b5SJoe Perches long\s+(?:un)?signed\s+int| 546938224b5SJoe Perches long\s+(?:un)?signed| 547938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 548938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 549938224b5SJoe Perches int\s+long\s+(?:un)?signed| 550938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 551938224b5SJoe Perches 552938224b5SJoe Perches int\s+(?:un)?signed| 553938224b5SJoe Perches (?:(?:un)?signed\s+)?int 554938224b5SJoe Perches)}; 555938224b5SJoe Perches 556485ff23eSAlex Dowadour @typeListFile = (); 5578716de38SJoe Perchesour @typeListWithAttr = ( 5588716de38SJoe Perches @typeList, 5598716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5608716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5618716de38SJoe Perches); 5628716de38SJoe Perches 563c45dcabdSAndy Whitcroftour @modifierList = ( 564c45dcabdSAndy Whitcroft qr{fastcall}, 565c45dcabdSAndy Whitcroft); 566485ff23eSAlex Dowadour @modifierListFile = (); 5678905a67cSAndy Whitcroft 5682435880fSJoe Perchesour @mode_permission_funcs = ( 5692435880fSJoe Perches ["module_param", 3], 5702435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5712435880fSJoe Perches ["module_param_array_named", 5], 5722435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5732435880fSJoe Perches ["proc_create(?:_data|)", 2], 574459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 575459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 576459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 577459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 578459cf0aeSJoe Perches ["__ATTR", 2], 5792435880fSJoe Perches); 5802435880fSJoe Perches 581515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 582515a235eSJoe Perchesour $mode_perms_search = ""; 583515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 584515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 585515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 586515a235eSJoe Perches} 58700180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 588515a235eSJoe Perches 5899189c7e7SJoe Perchesour %deprecated_apis = ( 5909189c7e7SJoe Perches "synchronize_rcu_bh" => "synchronize_rcu", 5919189c7e7SJoe Perches "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", 5929189c7e7SJoe Perches "call_rcu_bh" => "call_rcu", 5939189c7e7SJoe Perches "rcu_barrier_bh" => "rcu_barrier", 5949189c7e7SJoe Perches "synchronize_sched" => "synchronize_rcu", 5959189c7e7SJoe Perches "synchronize_sched_expedited" => "synchronize_rcu_expedited", 5969189c7e7SJoe Perches "call_rcu_sched" => "call_rcu", 5979189c7e7SJoe Perches "rcu_barrier_sched" => "rcu_barrier", 5989189c7e7SJoe Perches "get_state_synchronize_sched" => "get_state_synchronize_rcu", 5999189c7e7SJoe Perches "cond_synchronize_sched" => "cond_synchronize_rcu", 6009189c7e7SJoe Perches); 6019189c7e7SJoe Perches 6029189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below 6039189c7e7SJoe Perchesour $deprecated_apis_search = ""; 6049189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) { 6059189c7e7SJoe Perches $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); 6069189c7e7SJoe Perches $deprecated_apis_search .= $entry; 6079189c7e7SJoe Perches} 6089189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})"; 6099189c7e7SJoe Perches 610b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 611b392c64fSJoe Perches S_IWUGO | 612b392c64fSJoe Perches S_IWOTH | 613b392c64fSJoe Perches S_IRWXUGO | 614b392c64fSJoe Perches S_IALLUGO | 615b392c64fSJoe Perches 0[0-7][0-7][2367] 616b392c64fSJoe Perches}x; 617b392c64fSJoe Perches 618f90774e1SJoe Perchesour %mode_permission_string_types = ( 619f90774e1SJoe Perches "S_IRWXU" => 0700, 620f90774e1SJoe Perches "S_IRUSR" => 0400, 621f90774e1SJoe Perches "S_IWUSR" => 0200, 622f90774e1SJoe Perches "S_IXUSR" => 0100, 623f90774e1SJoe Perches "S_IRWXG" => 0070, 624f90774e1SJoe Perches "S_IRGRP" => 0040, 625f90774e1SJoe Perches "S_IWGRP" => 0020, 626f90774e1SJoe Perches "S_IXGRP" => 0010, 627f90774e1SJoe Perches "S_IRWXO" => 0007, 628f90774e1SJoe Perches "S_IROTH" => 0004, 629f90774e1SJoe Perches "S_IWOTH" => 0002, 630f90774e1SJoe Perches "S_IXOTH" => 0001, 631f90774e1SJoe Perches "S_IRWXUGO" => 0777, 632f90774e1SJoe Perches "S_IRUGO" => 0444, 633f90774e1SJoe Perches "S_IWUGO" => 0222, 634f90774e1SJoe Perches "S_IXUGO" => 0111, 635f90774e1SJoe Perches); 636f90774e1SJoe Perches 637f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 638f90774e1SJoe Perchesour $mode_perms_string_search = ""; 639f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 640f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 641f90774e1SJoe Perches $mode_perms_string_search .= $entry; 642f90774e1SJoe Perches} 64300180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 64400180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 64500180468SJoe Perches ${single_mode_perms_string_search} 64600180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 64700180468SJoe Perches}x; 64800180468SJoe Perches 64900180468SJoe Perchessub perms_to_octal { 65000180468SJoe Perches my ($string) = @_; 65100180468SJoe Perches 65200180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 65300180468SJoe Perches 65400180468SJoe Perches my $val = ""; 65500180468SJoe Perches my $oval = ""; 65600180468SJoe Perches my $to = 0; 65700180468SJoe Perches my $curpos = 0; 65800180468SJoe Perches my $lastpos = 0; 65900180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 66000180468SJoe Perches $curpos = pos($string); 66100180468SJoe Perches my $match = $2; 66200180468SJoe Perches my $omatch = $1; 66300180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 66400180468SJoe Perches $lastpos = $curpos; 66500180468SJoe Perches $to |= $mode_permission_string_types{$match}; 66600180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 66700180468SJoe Perches $val .= $match; 66800180468SJoe Perches $oval .= $omatch; 66900180468SJoe Perches } 67000180468SJoe Perches $oval =~ s/^\s*\|\s*//; 67100180468SJoe Perches $oval =~ s/\s*\|\s*$//; 67200180468SJoe Perches return sprintf("%04o", $to); 67300180468SJoe Perches} 674f90774e1SJoe Perches 6757840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6767840a94cSWolfram Sang irq| 677cdcee686SSergey Ryazanov memory| 678cdcee686SSergey Ryazanov time| 679cdcee686SSergey Ryazanov reboot 6807840a94cSWolfram Sang)}; 6817840a94cSWolfram Sang# memory.h: ARM has a custom one 6827840a94cSWolfram Sang 68366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 68466b47b4aSKees Cookmy $misspellings; 68566b47b4aSKees Cookmy %spelling_fix; 68636061e38SJoe Perches 68736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 68866b47b4aSKees Cook while (<$spelling>) { 68966b47b4aSKees Cook my $line = $_; 69066b47b4aSKees Cook 69166b47b4aSKees Cook $line =~ s/\s*\n?$//g; 69266b47b4aSKees Cook $line =~ s/^\s*//g; 69366b47b4aSKees Cook 69466b47b4aSKees Cook next if ($line =~ m/^\s*#/); 69566b47b4aSKees Cook next if ($line =~ m/^\s*$/); 69666b47b4aSKees Cook 69766b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 69866b47b4aSKees Cook 69966b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 70066b47b4aSKees Cook } 70166b47b4aSKees Cook close($spelling); 70236061e38SJoe Perches} else { 70336061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 70436061e38SJoe Perches} 70566b47b4aSKees Cook 706ebfd7d62SJoe Perchesif ($codespell) { 707ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 708ebfd7d62SJoe Perches while (<$spelling>) { 709ebfd7d62SJoe Perches my $line = $_; 710ebfd7d62SJoe Perches 711ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 712ebfd7d62SJoe Perches $line =~ s/^\s*//g; 713ebfd7d62SJoe Perches 714ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 715ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 716ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 717ebfd7d62SJoe Perches 718ebfd7d62SJoe Perches $line =~ s/,.*$//; 719ebfd7d62SJoe Perches 720ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 721ebfd7d62SJoe Perches 722ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 723ebfd7d62SJoe Perches } 724ebfd7d62SJoe Perches close($spelling); 725ebfd7d62SJoe Perches } else { 726ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 727ebfd7d62SJoe Perches } 728ebfd7d62SJoe Perches} 729ebfd7d62SJoe Perches 730ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 731ebfd7d62SJoe Perches 73275ad8c57SJerome Forissiersub read_words { 73375ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 73475ad8c57SJerome Forissier 73575ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 73675ad8c57SJerome Forissier while (<$words>) { 737bf1fa1daSJoe Perches my $line = $_; 738bf1fa1daSJoe Perches 739bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 740bf1fa1daSJoe Perches $line =~ s/^\s*//g; 741bf1fa1daSJoe Perches 742bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 743bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 744bf1fa1daSJoe Perches if ($line =~ /\s/) { 74575ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 746bf1fa1daSJoe Perches next; 747bf1fa1daSJoe Perches } 748bf1fa1daSJoe Perches 74975ad8c57SJerome Forissier $$wordsRef .= '|' if ($$wordsRef ne ""); 75075ad8c57SJerome Forissier $$wordsRef .= $line; 751bf1fa1daSJoe Perches } 75275ad8c57SJerome Forissier close($file); 75375ad8c57SJerome Forissier return 1; 754bf1fa1daSJoe Perches } 755bf1fa1daSJoe Perches 75675ad8c57SJerome Forissier return 0; 75775ad8c57SJerome Forissier} 75875ad8c57SJerome Forissier 75975ad8c57SJerome Forissiermy $const_structs = ""; 76075ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile) 76175ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 76275ad8c57SJerome Forissier 76375ad8c57SJerome Forissiermy $typeOtherTypedefs = ""; 76475ad8c57SJerome Forissierif (length($typedefsfile)) { 76575ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 76675ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 76775ad8c57SJerome Forissier} 76875ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); 76975ad8c57SJerome Forissier 7708905a67cSAndy Whitcroftsub build_types { 771485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 772485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7731813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7748716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 775c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 776ab7e23f3SJoe Perches $BasicType = qr{ 777ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 778ab7e23f3SJoe Perches (?:${all}\b) 779ab7e23f3SJoe Perches }x; 7808905a67cSAndy Whitcroft $NonptrType = qr{ 781d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 782cf655043SAndy Whitcroft (?: 7836b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 7848ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 785c45dcabdSAndy Whitcroft (?:${all}\b) 786cf655043SAndy Whitcroft ) 787c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 7888905a67cSAndy Whitcroft }x; 7891813087dSJoe Perches $NonptrTypeMisordered = qr{ 7901813087dSJoe Perches (?:$Modifier\s+|const\s+)* 7911813087dSJoe Perches (?: 7921813087dSJoe Perches (?:${Misordered}\b) 7931813087dSJoe Perches ) 7941813087dSJoe Perches (?:\s+$Modifier|\s+const)* 7951813087dSJoe Perches }x; 7968716de38SJoe Perches $NonptrTypeWithAttr = qr{ 7978716de38SJoe Perches (?:$Modifier\s+|const\s+)* 7988716de38SJoe Perches (?: 7998716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 8008716de38SJoe Perches (?:$typeTypedefs\b)| 8018716de38SJoe Perches (?:${allWithAttr}\b) 8028716de38SJoe Perches ) 8038716de38SJoe Perches (?:\s+$Modifier|\s+const)* 8048716de38SJoe Perches }x; 8058905a67cSAndy Whitcroft $Type = qr{ 806c45dcabdSAndy Whitcroft $NonptrType 8071574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 808c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 8098905a67cSAndy Whitcroft }x; 8101813087dSJoe Perches $TypeMisordered = qr{ 8111813087dSJoe Perches $NonptrTypeMisordered 8121813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 8131813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 8141813087dSJoe Perches }x; 81591cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 8161813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 8178905a67cSAndy Whitcroft} 8188905a67cSAndy Whitcroftbuild_types(); 8196c72ffaaSAndy Whitcroft 8207d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 821d1fe9c09SJoe Perches 822d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 823d1fe9c09SJoe Perches# requires at least perl version v5.10.0 824d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 825d1fe9c09SJoe Perches 826d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 8272435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 828c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 8297d2367afSJoe Perches 830f8422308SJoe Perchesour $declaration_macros = qr{(?x: 8313e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 832fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 8333d102fc0SGilad Ben-Yossef (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(| 8343d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 835f8422308SJoe Perches)}; 836f8422308SJoe Perches 8377d2367afSJoe Perchessub deparenthesize { 8387d2367afSJoe Perches my ($string) = @_; 8397d2367afSJoe Perches return "" if (!defined($string)); 8405b9553abSJoe Perches 8415b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8425b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8435b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8445b9553abSJoe Perches } 8455b9553abSJoe Perches 8467d2367afSJoe Perches $string =~ s@\s+@ @g; 8475b9553abSJoe Perches 8487d2367afSJoe Perches return $string; 8497d2367afSJoe Perches} 8507d2367afSJoe Perches 8513445686aSJoe Perchessub seed_camelcase_file { 8523445686aSJoe Perches my ($file) = @_; 8533445686aSJoe Perches 8543445686aSJoe Perches return if (!(-f $file)); 8553445686aSJoe Perches 8563445686aSJoe Perches local $/; 8573445686aSJoe Perches 8583445686aSJoe Perches open(my $include_file, '<', "$file") 8593445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8603445686aSJoe Perches my $text = <$include_file>; 8613445686aSJoe Perches close($include_file); 8623445686aSJoe Perches 8633445686aSJoe Perches my @lines = split('\n', $text); 8643445686aSJoe Perches 8653445686aSJoe Perches foreach my $line (@lines) { 8663445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8673445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8683445686aSJoe Perches $camelcase{$1} = 1; 86911ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 87011ea516aSJoe Perches $camelcase{$1} = 1; 87111ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8723445686aSJoe Perches $camelcase{$1} = 1; 8733445686aSJoe Perches } 8743445686aSJoe Perches } 8753445686aSJoe Perches} 8763445686aSJoe Perches 877cd28b119SJoe Perchesour %maintained_status = (); 878cd28b119SJoe Perches 87985b0ee18SJoe Perchessub is_maintained_obsolete { 88085b0ee18SJoe Perches my ($filename) = @_; 88185b0ee18SJoe Perches 882f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 88385b0ee18SJoe Perches 884cd28b119SJoe Perches if (!exists($maintained_status{$filename})) { 885cd28b119SJoe Perches $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 886cd28b119SJoe Perches } 88785b0ee18SJoe Perches 888cd28b119SJoe Perches return $maintained_status{$filename} =~ /obsolete/i; 88985b0ee18SJoe Perches} 89085b0ee18SJoe Perches 8913b6e8ac9SJoe Perchessub is_SPDX_License_valid { 8923b6e8ac9SJoe Perches my ($license) = @_; 8933b6e8ac9SJoe Perches 89456294112SJoe Perches return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git")); 8953b6e8ac9SJoe Perches 89656294112SJoe Perches my $root_path = abs_path($root); 89756294112SJoe Perches my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; 8983b6e8ac9SJoe Perches return 0 if ($status ne ""); 8993b6e8ac9SJoe Perches return 1; 9003b6e8ac9SJoe Perches} 9013b6e8ac9SJoe Perches 9023445686aSJoe Perchesmy $camelcase_seeded = 0; 9033445686aSJoe Perchessub seed_camelcase_includes { 9043445686aSJoe Perches return if ($camelcase_seeded); 9053445686aSJoe Perches 9063445686aSJoe Perches my $files; 907c707a81dSJoe Perches my $camelcase_cache = ""; 908c707a81dSJoe Perches my @include_files = (); 909c707a81dSJoe Perches 910c707a81dSJoe Perches $camelcase_seeded = 1; 911351b2a1fSJoe Perches 9123645e328SRichard Genoud if (-e ".git") { 913dbbf869dSJoe Perches my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; 914351b2a1fSJoe Perches chomp $git_last_include_commit; 915c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 916c707a81dSJoe Perches } else { 917c707a81dSJoe Perches my $last_mod_date = 0; 918c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 919c707a81dSJoe Perches @include_files = split('\n', $files); 920c707a81dSJoe Perches foreach my $file (@include_files) { 921c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 922c707a81dSJoe Perches localtime((stat $file)[9])); 923c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 924c707a81dSJoe Perches } 925c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 926c707a81dSJoe Perches } 927c707a81dSJoe Perches 928c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 929c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 930c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 931351b2a1fSJoe Perches while (<$camelcase_file>) { 932351b2a1fSJoe Perches chomp; 933351b2a1fSJoe Perches $camelcase{$_} = 1; 934351b2a1fSJoe Perches } 935351b2a1fSJoe Perches close($camelcase_file); 936351b2a1fSJoe Perches 937351b2a1fSJoe Perches return; 938351b2a1fSJoe Perches } 939c707a81dSJoe Perches 9403645e328SRichard Genoud if (-e ".git") { 941dbbf869dSJoe Perches $files = `${git_command} ls-files "include/*.h"`; 942c707a81dSJoe Perches @include_files = split('\n', $files); 9433445686aSJoe Perches } 944c707a81dSJoe Perches 9453445686aSJoe Perches foreach my $file (@include_files) { 9463445686aSJoe Perches seed_camelcase_file($file); 9473445686aSJoe Perches } 948351b2a1fSJoe Perches 949c707a81dSJoe Perches if ($camelcase_cache ne "") { 950351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 951c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 952c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 953351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 954351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 955351b2a1fSJoe Perches } 956351b2a1fSJoe Perches close($camelcase_file); 957351b2a1fSJoe Perches } 9583445686aSJoe Perches} 9593445686aSJoe Perches 960d311cd44SJoe Perchessub git_commit_info { 961d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 962d311cd44SJoe Perches 963d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 964d311cd44SJoe Perches 965dbbf869dSJoe Perches my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; 966d311cd44SJoe Perches $output =~ s/^\s*//gm; 967d311cd44SJoe Perches my @lines = split("\n", $output); 968d311cd44SJoe Perches 9690d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9700d7835fcSJoe Perches 9715a7f4455SSean Christopherson if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { 972d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 973d311cd44SJoe Perches# all matching commit ids, but it's very slow... 974d311cd44SJoe Perches# 975d311cd44SJoe Perches# echo "checking commits $1..." 976d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 977d311cd44SJoe Perches# while read line ; do 978d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 979d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 980d311cd44SJoe Perches# done 981d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 982948b133aSHeinrich Schuchardt $id = undef; 983d311cd44SJoe Perches } else { 984d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 985d311cd44SJoe Perches $desc = substr($lines[0], 41); 986d311cd44SJoe Perches } 987d311cd44SJoe Perches 988d311cd44SJoe Perches return ($id, $desc); 989d311cd44SJoe Perches} 990d311cd44SJoe Perches 9916c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 9920a920b5bSAndy Whitcroft 99300df344fSAndy Whitcroftmy @rawlines = (); 994c2fdda0dSAndy Whitcroftmy @lines = (); 9953705ce5bSJoe Perchesmy @fixed = (); 996d752fcc8SJoe Perchesmy @fixed_inserted = (); 997d752fcc8SJoe Perchesmy @fixed_deleted = (); 998194f66fcSJoe Perchesmy $fixlinenr = -1; 999194f66fcSJoe Perches 10004a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 10014a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 10024a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 10034a593c34SDu, Changbin 10044a593c34SDu, Changbinif ($git) { 10054a593c34SDu, Changbin my @commits = (); 10060dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 10074a593c34SDu, Changbin my $git_range; 100828898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 100928898fd1SJoe Perches $git_range = "-$2 $1"; 10104a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 10114a593c34SDu, Changbin $git_range = "$commit_expr"; 10124a593c34SDu, Changbin } else { 10130dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 10140dea9f1eSJoe Perches } 1015dbbf869dSJoe Perches my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 10160dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 101728898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 101828898fd1SJoe Perches next if (!defined($1) || !defined($2)); 10190dea9f1eSJoe Perches my $sha1 = $1; 10200dea9f1eSJoe Perches my $subject = $2; 10210dea9f1eSJoe Perches unshift(@commits, $sha1); 10220dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 10234a593c34SDu, Changbin } 10244a593c34SDu, Changbin } 10254a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 10264a593c34SDu, Changbin @ARGV = @commits; 10274a593c34SDu, Changbin} 10284a593c34SDu, Changbin 1029c2fdda0dSAndy Whitcroftmy $vname; 103098005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; 10316c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 103221caa13cSAndy Whitcroft my $FILE; 10334a593c34SDu, Changbin if ($git) { 10344a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 10354a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 10364a593c34SDu, Changbin } elsif ($file) { 103721caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 10386c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 103921caa13cSAndy Whitcroft } elsif ($filename eq '-') { 104021caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 10416c72ffaaSAndy Whitcroft } else { 104221caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 10436c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 10446c72ffaaSAndy Whitcroft } 1045c2fdda0dSAndy Whitcroft if ($filename eq '-') { 1046c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 10474a593c34SDu, Changbin } elsif ($git) { 10480dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 1049c2fdda0dSAndy Whitcroft } else { 1050c2fdda0dSAndy Whitcroft $vname = $filename; 1051c2fdda0dSAndy Whitcroft } 105221caa13cSAndy Whitcroft while (<$FILE>) { 10530a920b5bSAndy Whitcroft chomp; 105400df344fSAndy Whitcroft push(@rawlines, $_); 10556c72ffaaSAndy Whitcroft } 105621caa13cSAndy Whitcroft close($FILE); 1057d8469f16SJoe Perches 1058d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1059d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1060d8469f16SJoe Perches print "$vname\n"; 1061d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1062d8469f16SJoe Perches } 1063d8469f16SJoe Perches 1064c2fdda0dSAndy Whitcroft if (!process($filename)) { 10650a920b5bSAndy Whitcroft $exit = 1; 10660a920b5bSAndy Whitcroft } 106700df344fSAndy Whitcroft @rawlines = (); 106813214adfSAndy Whitcroft @lines = (); 10693705ce5bSJoe Perches @fixed = (); 1070d752fcc8SJoe Perches @fixed_inserted = (); 1071d752fcc8SJoe Perches @fixed_deleted = (); 1072194f66fcSJoe Perches $fixlinenr = -1; 1073485ff23eSAlex Dowad @modifierListFile = (); 1074485ff23eSAlex Dowad @typeListFile = (); 1075485ff23eSAlex Dowad build_types(); 10760a920b5bSAndy Whitcroft} 10770a920b5bSAndy Whitcroft 1078d8469f16SJoe Perchesif (!$quiet) { 10793c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 10803c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 10813c816e49SJoe Perches 10825b57980dSJoe Perches if (!$perl_version_ok) { 1083d8469f16SJoe Perches print << "EOM" 1084d8469f16SJoe Perches 1085d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 10865b57980dSJoe Perches An upgrade to at least perl $minimum_perl_version is suggested. 1087d8469f16SJoe PerchesEOM 1088d8469f16SJoe Perches } 1089d8469f16SJoe Perches if ($exit) { 1090d8469f16SJoe Perches print << "EOM" 1091d8469f16SJoe Perches 1092d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1093d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1094d8469f16SJoe PerchesEOM 1095d8469f16SJoe Perches } 1096d8469f16SJoe Perches} 1097d8469f16SJoe Perches 10980a920b5bSAndy Whitcroftexit($exit); 10990a920b5bSAndy Whitcroft 11000a920b5bSAndy Whitcroftsub top_of_kernel_tree { 11016c72ffaaSAndy Whitcroft my ($root) = @_; 11026c72ffaaSAndy Whitcroft 11036c72ffaaSAndy Whitcroft my @tree_check = ( 11046c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 11056c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 11066c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 11076c72ffaaSAndy Whitcroft ); 11086c72ffaaSAndy Whitcroft 11096c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 11106c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 11110a920b5bSAndy Whitcroft return 0; 11120a920b5bSAndy Whitcroft } 11136c72ffaaSAndy Whitcroft } 11146c72ffaaSAndy Whitcroft return 1; 11156c72ffaaSAndy Whitcroft} 11160a920b5bSAndy Whitcroft 111720112475SJoe Perchessub parse_email { 111820112475SJoe Perches my ($formatted_email) = @_; 111920112475SJoe Perches 112020112475SJoe Perches my $name = ""; 1121dfa05c28SJoe Perches my $name_comment = ""; 112220112475SJoe Perches my $address = ""; 112320112475SJoe Perches my $comment = ""; 112420112475SJoe Perches 112520112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 112620112475SJoe Perches $name = $1; 112720112475SJoe Perches $address = $2; 112820112475SJoe Perches $comment = $3 if defined $3; 112920112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 113020112475SJoe Perches $address = $1; 113120112475SJoe Perches $comment = $2 if defined $2; 113220112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 113320112475SJoe Perches $address = $1; 113420112475SJoe Perches $comment = $2 if defined $2; 113585e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 113620112475SJoe Perches $name = $formatted_email; 11373705ce5bSJoe Perches $name = trim($name); 113820112475SJoe Perches $name =~ s/^\"|\"$//g; 113920112475SJoe Perches # If there's a name left after stripping spaces and 114020112475SJoe Perches # leading quotes, and the address doesn't have both 114120112475SJoe Perches # leading and trailing angle brackets, the address 114220112475SJoe Perches # is invalid. ie: 114320112475SJoe Perches # "joe smith [email protected]" bad 114420112475SJoe Perches # "joe smith <[email protected]" bad 114520112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 114620112475SJoe Perches $name = ""; 114720112475SJoe Perches $address = ""; 114820112475SJoe Perches $comment = ""; 114920112475SJoe Perches } 115020112475SJoe Perches } 115120112475SJoe Perches 11523705ce5bSJoe Perches $name = trim($name); 115320112475SJoe Perches $name =~ s/^\"|\"$//g; 1154dfa05c28SJoe Perches $name =~ s/(\s*\([^\)]+\))\s*//; 1155dfa05c28SJoe Perches if (defined($1)) { 1156dfa05c28SJoe Perches $name_comment = trim($1); 1157dfa05c28SJoe Perches } 11583705ce5bSJoe Perches $address = trim($address); 115920112475SJoe Perches $address =~ s/^\<|\>$//g; 116020112475SJoe Perches 116120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 116220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 116320112475SJoe Perches $name = "\"$name\""; 116420112475SJoe Perches } 116520112475SJoe Perches 1166dfa05c28SJoe Perches return ($name, $name_comment, $address, $comment); 116720112475SJoe Perches} 116820112475SJoe Perches 116920112475SJoe Perchessub format_email { 117020112475SJoe Perches my ($name, $address) = @_; 117120112475SJoe Perches 117220112475SJoe Perches my $formatted_email; 117320112475SJoe Perches 11743705ce5bSJoe Perches $name = trim($name); 117520112475SJoe Perches $name =~ s/^\"|\"$//g; 11763705ce5bSJoe Perches $address = trim($address); 117720112475SJoe Perches 117820112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 117920112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 118020112475SJoe Perches $name = "\"$name\""; 118120112475SJoe Perches } 118220112475SJoe Perches 118320112475SJoe Perches if ("$name" eq "") { 118420112475SJoe Perches $formatted_email = "$address"; 118520112475SJoe Perches } else { 118620112475SJoe Perches $formatted_email = "$name <$address>"; 118720112475SJoe Perches } 118820112475SJoe Perches 118920112475SJoe Perches return $formatted_email; 119020112475SJoe Perches} 119120112475SJoe Perches 1192dfa05c28SJoe Perchessub reformat_email { 1193dfa05c28SJoe Perches my ($email) = @_; 1194dfa05c28SJoe Perches 1195dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 1196dfa05c28SJoe Perches return format_email($email_name, $email_address); 1197dfa05c28SJoe Perches} 1198dfa05c28SJoe Perches 1199dfa05c28SJoe Perchessub same_email_addresses { 1200dfa05c28SJoe Perches my ($email1, $email2) = @_; 1201dfa05c28SJoe Perches 1202dfa05c28SJoe Perches my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); 1203dfa05c28SJoe Perches my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); 1204dfa05c28SJoe Perches 1205dfa05c28SJoe Perches return $email1_name eq $email2_name && 1206dfa05c28SJoe Perches $email1_address eq $email2_address; 1207dfa05c28SJoe Perches} 1208dfa05c28SJoe Perches 1209d311cd44SJoe Perchessub which { 1210d311cd44SJoe Perches my ($bin) = @_; 1211d311cd44SJoe Perches 1212d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1213d311cd44SJoe Perches if (-e "$path/$bin") { 1214d311cd44SJoe Perches return "$path/$bin"; 1215d311cd44SJoe Perches } 1216d311cd44SJoe Perches } 1217d311cd44SJoe Perches 1218d311cd44SJoe Perches return ""; 1219d311cd44SJoe Perches} 1220d311cd44SJoe Perches 1221000d1cc1SJoe Perchessub which_conf { 1222000d1cc1SJoe Perches my ($conf) = @_; 1223000d1cc1SJoe Perches 1224000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1225000d1cc1SJoe Perches if (-e "$path/$conf") { 1226000d1cc1SJoe Perches return "$path/$conf"; 1227000d1cc1SJoe Perches } 1228000d1cc1SJoe Perches } 1229000d1cc1SJoe Perches 1230000d1cc1SJoe Perches return ""; 1231000d1cc1SJoe Perches} 1232000d1cc1SJoe Perches 12330a920b5bSAndy Whitcroftsub expand_tabs { 12340a920b5bSAndy Whitcroft my ($str) = @_; 12350a920b5bSAndy Whitcroft 12360a920b5bSAndy Whitcroft my $res = ''; 12370a920b5bSAndy Whitcroft my $n = 0; 12380a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 12390a920b5bSAndy Whitcroft if ($c eq "\t") { 12400a920b5bSAndy Whitcroft $res .= ' '; 12410a920b5bSAndy Whitcroft $n++; 12420a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 12430a920b5bSAndy Whitcroft $res .= ' '; 12440a920b5bSAndy Whitcroft } 12450a920b5bSAndy Whitcroft next; 12460a920b5bSAndy Whitcroft } 12470a920b5bSAndy Whitcroft $res .= $c; 12480a920b5bSAndy Whitcroft $n++; 12490a920b5bSAndy Whitcroft } 12500a920b5bSAndy Whitcroft 12510a920b5bSAndy Whitcroft return $res; 12520a920b5bSAndy Whitcroft} 12536c72ffaaSAndy Whitcroftsub copy_spacing { 1254773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 12556c72ffaaSAndy Whitcroft return $res; 12566c72ffaaSAndy Whitcroft} 12570a920b5bSAndy Whitcroft 12584a0df2efSAndy Whitcroftsub line_stats { 12594a0df2efSAndy Whitcroft my ($line) = @_; 12604a0df2efSAndy Whitcroft 12614a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 12624a0df2efSAndy Whitcroft $line =~ s/^.//; 12634a0df2efSAndy Whitcroft $line = expand_tabs($line); 12644a0df2efSAndy Whitcroft 12654a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 12664a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 12674a0df2efSAndy Whitcroft 12684a0df2efSAndy Whitcroft return (length($line), length($white)); 12694a0df2efSAndy Whitcroft} 12704a0df2efSAndy Whitcroft 1271773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1272773647a0SAndy Whitcroft 1273773647a0SAndy Whitcroftsub sanitise_line_reset { 1274773647a0SAndy Whitcroft my ($in_comment) = @_; 1275773647a0SAndy Whitcroft 1276773647a0SAndy Whitcroft if ($in_comment) { 1277773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1278773647a0SAndy Whitcroft } else { 1279773647a0SAndy Whitcroft $sanitise_quote = ''; 1280773647a0SAndy Whitcroft } 1281773647a0SAndy Whitcroft} 128200df344fSAndy Whitcroftsub sanitise_line { 128300df344fSAndy Whitcroft my ($line) = @_; 128400df344fSAndy Whitcroft 128500df344fSAndy Whitcroft my $res = ''; 128600df344fSAndy Whitcroft my $l = ''; 128700df344fSAndy Whitcroft 1288c2fdda0dSAndy Whitcroft my $qlen = 0; 1289773647a0SAndy Whitcroft my $off = 0; 1290773647a0SAndy Whitcroft my $c; 129100df344fSAndy Whitcroft 1292773647a0SAndy Whitcroft # Always copy over the diff marker. 1293773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1294773647a0SAndy Whitcroft 1295773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1296773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1297773647a0SAndy Whitcroft 12988d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1299773647a0SAndy Whitcroft # and end, all to $;. 1300773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1301773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1302773647a0SAndy Whitcroft 1303773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1304773647a0SAndy Whitcroft $off++; 130500df344fSAndy Whitcroft next; 1306773647a0SAndy Whitcroft } 130781bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1308773647a0SAndy Whitcroft $sanitise_quote = ''; 1309773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1310773647a0SAndy Whitcroft $off++; 1311773647a0SAndy Whitcroft next; 1312773647a0SAndy Whitcroft } 1313113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1314113f04a8SDaniel Walker $sanitise_quote = '//'; 1315113f04a8SDaniel Walker 1316113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1317113f04a8SDaniel Walker $off++; 1318113f04a8SDaniel Walker next; 1319113f04a8SDaniel Walker } 1320773647a0SAndy Whitcroft 1321773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1322773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1323773647a0SAndy Whitcroft $c eq "\\") { 1324773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1325773647a0SAndy Whitcroft $off++; 1326773647a0SAndy Whitcroft next; 1327773647a0SAndy Whitcroft } 1328773647a0SAndy Whitcroft # Regular quotes. 1329773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1330773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1331773647a0SAndy Whitcroft $sanitise_quote = $c; 1332773647a0SAndy Whitcroft 1333773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1334773647a0SAndy Whitcroft next; 1335773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1336773647a0SAndy Whitcroft $sanitise_quote = ''; 133700df344fSAndy Whitcroft } 133800df344fSAndy Whitcroft } 1339773647a0SAndy Whitcroft 1340fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1341773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1342773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1343113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1344113f04a8SDaniel Walker substr($res, $off, 1, $;); 1345773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1346773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 134700df344fSAndy Whitcroft } else { 1348773647a0SAndy Whitcroft substr($res, $off, 1, $c); 134900df344fSAndy Whitcroft } 1350c2fdda0dSAndy Whitcroft } 1351c2fdda0dSAndy Whitcroft 1352113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1353113f04a8SDaniel Walker $sanitise_quote = ''; 1354113f04a8SDaniel Walker } 1355113f04a8SDaniel Walker 1356c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1357c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1358c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1359c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1360c2fdda0dSAndy Whitcroft 1361c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1362c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1363c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1364c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1365c2fdda0dSAndy Whitcroft } 1366c2fdda0dSAndy Whitcroft 1367dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1368dadf680dSJoe Perches my $match = $1; 1369dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1370dadf680dSJoe Perches } 1371dadf680dSJoe Perches 137200df344fSAndy Whitcroft return $res; 137300df344fSAndy Whitcroft} 137400df344fSAndy Whitcroft 1375a6962d72SJoe Perchessub get_quoted_string { 1376a6962d72SJoe Perches my ($line, $rawline) = @_; 1377a6962d72SJoe Perches 1378478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 137933acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1380a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1381a6962d72SJoe Perches} 1382a6962d72SJoe Perches 13838905a67cSAndy Whitcroftsub ctx_statement_block { 13848905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 13858905a67cSAndy Whitcroft my $line = $linenr - 1; 13868905a67cSAndy Whitcroft my $blk = ''; 13878905a67cSAndy Whitcroft my $soff = $off; 13888905a67cSAndy Whitcroft my $coff = $off - 1; 1389773647a0SAndy Whitcroft my $coff_set = 0; 13908905a67cSAndy Whitcroft 139113214adfSAndy Whitcroft my $loff = 0; 139213214adfSAndy Whitcroft 13938905a67cSAndy Whitcroft my $type = ''; 13948905a67cSAndy Whitcroft my $level = 0; 1395a2750645SAndy Whitcroft my @stack = (); 1396cf655043SAndy Whitcroft my $p; 13978905a67cSAndy Whitcroft my $c; 13988905a67cSAndy Whitcroft my $len = 0; 139913214adfSAndy Whitcroft 140013214adfSAndy Whitcroft my $remainder; 14018905a67cSAndy Whitcroft while (1) { 1402a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1403a2750645SAndy Whitcroft 1404773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 14058905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 14068905a67cSAndy Whitcroft # context. 14078905a67cSAndy Whitcroft if ($off >= $len) { 14088905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1409dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1410c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 14118905a67cSAndy Whitcroft $remain--; 141213214adfSAndy Whitcroft $loff = $len; 1413c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 14148905a67cSAndy Whitcroft $len = length($blk); 14158905a67cSAndy Whitcroft $line++; 14168905a67cSAndy Whitcroft last; 14178905a67cSAndy Whitcroft } 14188905a67cSAndy Whitcroft # Bail if there is no further context. 14198905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 142013214adfSAndy Whitcroft if ($off >= $len) { 14218905a67cSAndy Whitcroft last; 14228905a67cSAndy Whitcroft } 1423f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1424f74bd194SAndy Whitcroft $level++; 1425f74bd194SAndy Whitcroft $type = '#'; 1426f74bd194SAndy Whitcroft } 14278905a67cSAndy Whitcroft } 1428cf655043SAndy Whitcroft $p = $c; 14298905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 143013214adfSAndy Whitcroft $remainder = substr($blk, $off); 14318905a67cSAndy Whitcroft 1432773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 14334635f4fbSAndy Whitcroft 14344635f4fbSAndy Whitcroft # Handle nested #if/#else. 14354635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 14364635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 14374635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 14384635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 14394635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 14404635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 14414635f4fbSAndy Whitcroft } 14424635f4fbSAndy Whitcroft 14438905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 14448905a67cSAndy Whitcroft # outermost level. 14458905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 14468905a67cSAndy Whitcroft last; 14478905a67cSAndy Whitcroft } 14488905a67cSAndy Whitcroft 144913214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1450773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1451773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1452773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1453773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1454773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1455773647a0SAndy Whitcroft $coff_set = 1; 1456773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1457773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 145813214adfSAndy Whitcroft } 145913214adfSAndy Whitcroft 14608905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 14618905a67cSAndy Whitcroft $level++; 14628905a67cSAndy Whitcroft $type = '('; 14638905a67cSAndy Whitcroft } 14648905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 14658905a67cSAndy Whitcroft $level--; 14668905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 14678905a67cSAndy Whitcroft 14688905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 14698905a67cSAndy Whitcroft $coff = $off; 1470773647a0SAndy Whitcroft $coff_set = 1; 1471773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 14728905a67cSAndy Whitcroft } 14738905a67cSAndy Whitcroft } 14748905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 14758905a67cSAndy Whitcroft $level++; 14768905a67cSAndy Whitcroft $type = '{'; 14778905a67cSAndy Whitcroft } 14788905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 14798905a67cSAndy Whitcroft $level--; 14808905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 14818905a67cSAndy Whitcroft 14828905a67cSAndy Whitcroft if ($level == 0) { 1483b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1484b998e001SPatrick Pannuto $off++; 1485b998e001SPatrick Pannuto } 14868905a67cSAndy Whitcroft last; 14878905a67cSAndy Whitcroft } 14888905a67cSAndy Whitcroft } 1489f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1490f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1491f74bd194SAndy Whitcroft $level--; 1492f74bd194SAndy Whitcroft $type = ''; 1493f74bd194SAndy Whitcroft $off++; 1494f74bd194SAndy Whitcroft last; 1495f74bd194SAndy Whitcroft } 14968905a67cSAndy Whitcroft $off++; 14978905a67cSAndy Whitcroft } 1498a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 149913214adfSAndy Whitcroft if ($off == $len) { 1500a3bb97a7SAndy Whitcroft $loff = $len + 1; 150113214adfSAndy Whitcroft $line++; 150213214adfSAndy Whitcroft $remain--; 150313214adfSAndy Whitcroft } 15048905a67cSAndy Whitcroft 15058905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 15068905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 15078905a67cSAndy Whitcroft 15088905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 15098905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 15108905a67cSAndy Whitcroft 1511773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 151213214adfSAndy Whitcroft 151313214adfSAndy Whitcroft return ($statement, $condition, 151413214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 151513214adfSAndy Whitcroft} 151613214adfSAndy Whitcroft 1517cf655043SAndy Whitcroftsub statement_lines { 1518cf655043SAndy Whitcroft my ($stmt) = @_; 1519cf655043SAndy Whitcroft 1520cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1521cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1522cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1523cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1524cf655043SAndy Whitcroft 1525cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1526cf655043SAndy Whitcroft 1527cf655043SAndy Whitcroft return $#stmt_lines + 2; 1528cf655043SAndy Whitcroft} 1529cf655043SAndy Whitcroft 1530cf655043SAndy Whitcroftsub statement_rawlines { 1531cf655043SAndy Whitcroft my ($stmt) = @_; 1532cf655043SAndy Whitcroft 1533cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1534cf655043SAndy Whitcroft 1535cf655043SAndy Whitcroft return $#stmt_lines + 2; 1536cf655043SAndy Whitcroft} 1537cf655043SAndy Whitcroft 1538cf655043SAndy Whitcroftsub statement_block_size { 1539cf655043SAndy Whitcroft my ($stmt) = @_; 1540cf655043SAndy Whitcroft 1541cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1542cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1543cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1544cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1545cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1546cf655043SAndy Whitcroft 1547cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1548cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1549cf655043SAndy Whitcroft 1550cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1551cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1552cf655043SAndy Whitcroft 1553cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1554cf655043SAndy Whitcroft return $stmt_lines; 1555cf655043SAndy Whitcroft } else { 1556cf655043SAndy Whitcroft return $stmt_statements; 1557cf655043SAndy Whitcroft } 1558cf655043SAndy Whitcroft} 1559cf655043SAndy Whitcroft 156013214adfSAndy Whitcroftsub ctx_statement_full { 156113214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 156213214adfSAndy Whitcroft my ($statement, $condition, $level); 156313214adfSAndy Whitcroft 156413214adfSAndy Whitcroft my (@chunks); 156513214adfSAndy Whitcroft 1566cf655043SAndy Whitcroft # Grab the first conditional/block pair. 156713214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 156813214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1569773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 157013214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1571cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1572cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1573cf655043SAndy Whitcroft } 1574cf655043SAndy Whitcroft 1575cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1576cf655043SAndy Whitcroft # could continue the statement. 1577cf655043SAndy Whitcroft for (;;) { 157813214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 157913214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1580cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1581773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1582cf655043SAndy Whitcroft #print "C: push\n"; 1583cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 158413214adfSAndy Whitcroft } 158513214adfSAndy Whitcroft 158613214adfSAndy Whitcroft return ($level, $linenr, @chunks); 15878905a67cSAndy Whitcroft} 15888905a67cSAndy Whitcroft 15894a0df2efSAndy Whitcroftsub ctx_block_get { 1590f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 15914a0df2efSAndy Whitcroft my $line; 15924a0df2efSAndy Whitcroft my $start = $linenr - 1; 15934a0df2efSAndy Whitcroft my $blk = ''; 15944a0df2efSAndy Whitcroft my @o; 15954a0df2efSAndy Whitcroft my @c; 15964a0df2efSAndy Whitcroft my @res = (); 15974a0df2efSAndy Whitcroft 1598f0a594c1SAndy Whitcroft my $level = 0; 15994635f4fbSAndy Whitcroft my @stack = ($level); 160000df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 160100df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 160200df344fSAndy Whitcroft $remain--; 160300df344fSAndy Whitcroft 160400df344fSAndy Whitcroft $blk .= $rawlines[$line]; 16054635f4fbSAndy Whitcroft 16064635f4fbSAndy Whitcroft # Handle nested #if/#else. 160701464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 16084635f4fbSAndy Whitcroft push(@stack, $level); 160901464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 16104635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 161101464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 16124635f4fbSAndy Whitcroft $level = pop(@stack); 16134635f4fbSAndy Whitcroft } 16144635f4fbSAndy Whitcroft 161501464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1616f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1617f0a594c1SAndy Whitcroft if ($off > 0) { 1618f0a594c1SAndy Whitcroft $off--; 1619f0a594c1SAndy Whitcroft next; 1620f0a594c1SAndy Whitcroft } 16214a0df2efSAndy Whitcroft 1622f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1623f0a594c1SAndy Whitcroft $level--; 1624f0a594c1SAndy Whitcroft last if ($level == 0); 1625f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1626f0a594c1SAndy Whitcroft $level++; 1627f0a594c1SAndy Whitcroft } 1628f0a594c1SAndy Whitcroft } 16294a0df2efSAndy Whitcroft 1630f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 163100df344fSAndy Whitcroft push(@res, $rawlines[$line]); 16324a0df2efSAndy Whitcroft } 16334a0df2efSAndy Whitcroft 1634f0a594c1SAndy Whitcroft last if ($level == 0); 16354a0df2efSAndy Whitcroft } 16364a0df2efSAndy Whitcroft 1637f0a594c1SAndy Whitcroft return ($level, @res); 16384a0df2efSAndy Whitcroft} 16394a0df2efSAndy Whitcroftsub ctx_block_outer { 16404a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16414a0df2efSAndy Whitcroft 1642f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1643f0a594c1SAndy Whitcroft return @r; 16444a0df2efSAndy Whitcroft} 16454a0df2efSAndy Whitcroftsub ctx_block { 16464a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 16474a0df2efSAndy Whitcroft 1648f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1649f0a594c1SAndy Whitcroft return @r; 1650653d4876SAndy Whitcroft} 1651653d4876SAndy Whitcroftsub ctx_statement { 1652f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1653f0a594c1SAndy Whitcroft 1654f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1655f0a594c1SAndy Whitcroft return @r; 1656f0a594c1SAndy Whitcroft} 1657f0a594c1SAndy Whitcroftsub ctx_block_level { 1658653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1659653d4876SAndy Whitcroft 1660f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 16614a0df2efSAndy Whitcroft} 16629c0ca6f9SAndy Whitcroftsub ctx_statement_level { 16639c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 16649c0ca6f9SAndy Whitcroft 16659c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 16669c0ca6f9SAndy Whitcroft} 16674a0df2efSAndy Whitcroft 16684a0df2efSAndy Whitcroftsub ctx_locate_comment { 16694a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16704a0df2efSAndy Whitcroft 16714a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1672beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 16734a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 16744a0df2efSAndy Whitcroft 16754a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 16764a0df2efSAndy Whitcroft # comment. 16774a0df2efSAndy Whitcroft my $in_comment = 0; 16784a0df2efSAndy Whitcroft $current_comment = ''; 16794a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 168000df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 168100df344fSAndy Whitcroft #warn " $line\n"; 16824a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 16834a0df2efSAndy Whitcroft $in_comment = 1; 16844a0df2efSAndy Whitcroft } 16854a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 16864a0df2efSAndy Whitcroft $in_comment = 1; 16874a0df2efSAndy Whitcroft } 16884a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 16894a0df2efSAndy Whitcroft $current_comment = ''; 16904a0df2efSAndy Whitcroft } 16914a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 16924a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 16934a0df2efSAndy Whitcroft $in_comment = 0; 16944a0df2efSAndy Whitcroft } 16954a0df2efSAndy Whitcroft } 16964a0df2efSAndy Whitcroft 16974a0df2efSAndy Whitcroft chomp($current_comment); 16984a0df2efSAndy Whitcroft return($current_comment); 16994a0df2efSAndy Whitcroft} 17004a0df2efSAndy Whitcroftsub ctx_has_comment { 17014a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 17024a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 17034a0df2efSAndy Whitcroft 170400df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 17054a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 17064a0df2efSAndy Whitcroft 17074a0df2efSAndy Whitcroft return ($cmt ne ''); 17084a0df2efSAndy Whitcroft} 17094a0df2efSAndy Whitcroft 17104d001e4dSAndy Whitcroftsub raw_line { 17114d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 17124d001e4dSAndy Whitcroft 17134d001e4dSAndy Whitcroft my $offset = $linenr - 1; 17144d001e4dSAndy Whitcroft $cnt++; 17154d001e4dSAndy Whitcroft 17164d001e4dSAndy Whitcroft my $line; 17174d001e4dSAndy Whitcroft while ($cnt) { 17184d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 17194d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 17204d001e4dSAndy Whitcroft $cnt--; 17214d001e4dSAndy Whitcroft } 17224d001e4dSAndy Whitcroft 17234d001e4dSAndy Whitcroft return $line; 17244d001e4dSAndy Whitcroft} 17254d001e4dSAndy Whitcroft 17262a9f9d85STobin C. Hardingsub get_stat_real { 17272a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 17282a9f9d85STobin C. Harding 17292a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 17302a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 17312a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 17322a9f9d85STobin C. Harding } 17332a9f9d85STobin C. Harding 17342a9f9d85STobin C. Harding return $stat_real; 17352a9f9d85STobin C. Harding} 17362a9f9d85STobin C. Harding 1737e3d95a2aSTobin C. Hardingsub get_stat_here { 1738e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1739e3d95a2aSTobin C. Harding 1740e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1741e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1742e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1743e3d95a2aSTobin C. Harding } 1744e3d95a2aSTobin C. Harding 1745e3d95a2aSTobin C. Harding return $herectx; 1746e3d95a2aSTobin C. Harding} 1747e3d95a2aSTobin C. Harding 17480a920b5bSAndy Whitcroftsub cat_vet { 17490a920b5bSAndy Whitcroft my ($vet) = @_; 17509c0ca6f9SAndy Whitcroft my ($res, $coded); 17510a920b5bSAndy Whitcroft 17529c0ca6f9SAndy Whitcroft $res = ''; 17536c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 17546c72ffaaSAndy Whitcroft $res .= $1; 17556c72ffaaSAndy Whitcroft if ($2 ne '') { 17569c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 17576c72ffaaSAndy Whitcroft $res .= $coded; 17586c72ffaaSAndy Whitcroft } 17599c0ca6f9SAndy Whitcroft } 17609c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 17610a920b5bSAndy Whitcroft 17629c0ca6f9SAndy Whitcroft return $res; 17630a920b5bSAndy Whitcroft} 17640a920b5bSAndy Whitcroft 1765c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1766cf655043SAndy Whitcroftmy $av_pending; 1767c2fdda0dSAndy Whitcroftmy @av_paren_type; 17681f65f947SAndy Whitcroftmy $av_pend_colon; 1769c2fdda0dSAndy Whitcroft 1770c2fdda0dSAndy Whitcroftsub annotate_reset { 1771c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1772cf655043SAndy Whitcroft $av_pending = '_'; 1773cf655043SAndy Whitcroft @av_paren_type = ('E'); 17741f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1775c2fdda0dSAndy Whitcroft} 1776c2fdda0dSAndy Whitcroft 17776c72ffaaSAndy Whitcroftsub annotate_values { 17786c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 17796c72ffaaSAndy Whitcroft 17806c72ffaaSAndy Whitcroft my $res; 17811f65f947SAndy Whitcroft my $var = '_' x length($stream); 17826c72ffaaSAndy Whitcroft my $cur = $stream; 17836c72ffaaSAndy Whitcroft 1784c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 17856c72ffaaSAndy Whitcroft 17866c72ffaaSAndy Whitcroft while (length($cur)) { 1787773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1788cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1789171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 17906c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1791c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1792c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1793cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1794c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 17956c72ffaaSAndy Whitcroft } 17966c72ffaaSAndy Whitcroft 1797c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 17989446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 17999446ef56SAndy Whitcroft push(@av_paren_type, $type); 1800addcdceaSAndy Whitcroft $type = 'c'; 18019446ef56SAndy Whitcroft 1802e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1803c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 18046c72ffaaSAndy Whitcroft $type = 'T'; 18056c72ffaaSAndy Whitcroft 1806389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1807389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1808389a2fe5SAndy Whitcroft $type = 'T'; 1809389a2fe5SAndy Whitcroft 1810c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1811171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1812c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1813171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1814171ae1a4SAndy Whitcroft if ($2 ne '') { 1815cf655043SAndy Whitcroft $av_pending = 'N'; 1816171ae1a4SAndy Whitcroft } 1817171ae1a4SAndy Whitcroft $type = 'E'; 1818171ae1a4SAndy Whitcroft 1819c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1820171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1821171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1822171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 18236c72ffaaSAndy Whitcroft 1824c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1825cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1826c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1827cf655043SAndy Whitcroft 1828cf655043SAndy Whitcroft push(@av_paren_type, $type); 1829cf655043SAndy Whitcroft push(@av_paren_type, $type); 1830171ae1a4SAndy Whitcroft $type = 'E'; 1831cf655043SAndy Whitcroft 1832c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1833cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1834cf655043SAndy Whitcroft $av_preprocessor = 1; 1835cf655043SAndy Whitcroft 1836cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1837cf655043SAndy Whitcroft 1838171ae1a4SAndy Whitcroft $type = 'E'; 1839cf655043SAndy Whitcroft 1840c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1841cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1842cf655043SAndy Whitcroft 1843cf655043SAndy Whitcroft $av_preprocessor = 1; 1844cf655043SAndy Whitcroft 1845cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1846cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1847cf655043SAndy Whitcroft pop(@av_paren_type); 1848cf655043SAndy Whitcroft push(@av_paren_type, $type); 1849171ae1a4SAndy Whitcroft $type = 'E'; 18506c72ffaaSAndy Whitcroft 18516c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1852c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 18536c72ffaaSAndy Whitcroft 1854171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1855171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1856171ae1a4SAndy Whitcroft $av_pending = $type; 1857171ae1a4SAndy Whitcroft $type = 'N'; 1858171ae1a4SAndy Whitcroft 18596c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1860c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 18616c72ffaaSAndy Whitcroft if (defined $2) { 1862cf655043SAndy Whitcroft $av_pending = 'V'; 18636c72ffaaSAndy Whitcroft } 18646c72ffaaSAndy Whitcroft $type = 'N'; 18656c72ffaaSAndy Whitcroft 186614b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1867c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 186814b111c1SAndy Whitcroft $av_pending = 'E'; 18696c72ffaaSAndy Whitcroft $type = 'N'; 18706c72ffaaSAndy Whitcroft 18711f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 18721f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 18731f65f947SAndy Whitcroft $av_pend_colon = 'C'; 18741f65f947SAndy Whitcroft $type = 'N'; 18751f65f947SAndy Whitcroft 187614b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1877c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 18786c72ffaaSAndy Whitcroft $type = 'N'; 18796c72ffaaSAndy Whitcroft 18806c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1881c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1882cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1883cf655043SAndy Whitcroft $av_pending = '_'; 18846c72ffaaSAndy Whitcroft $type = 'N'; 18856c72ffaaSAndy Whitcroft 18866c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1887cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1888cf655043SAndy Whitcroft if ($new_type ne '_') { 1889cf655043SAndy Whitcroft $type = $new_type; 1890c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1891c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 18926c72ffaaSAndy Whitcroft } else { 1893c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 18946c72ffaaSAndy Whitcroft } 18956c72ffaaSAndy Whitcroft 1896c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1897c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1898c8cb2ca3SAndy Whitcroft $type = 'V'; 1899cf655043SAndy Whitcroft $av_pending = 'V'; 19006c72ffaaSAndy Whitcroft 19018e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 19028e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 19031f65f947SAndy Whitcroft $av_pend_colon = 'B'; 19048e761b04SAndy Whitcroft } elsif ($type eq 'E') { 19058e761b04SAndy Whitcroft $av_pend_colon = 'L'; 19061f65f947SAndy Whitcroft } 19071f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 19081f65f947SAndy Whitcroft $type = 'V'; 19091f65f947SAndy Whitcroft 19106c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1911c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 19126c72ffaaSAndy Whitcroft $type = 'V'; 19136c72ffaaSAndy Whitcroft 19146c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1915c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 19166c72ffaaSAndy Whitcroft $type = 'N'; 19176c72ffaaSAndy Whitcroft 1918cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1919c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 192013214adfSAndy Whitcroft $type = 'E'; 19211f65f947SAndy Whitcroft $av_pend_colon = 'O'; 192213214adfSAndy Whitcroft 19238e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 19248e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 19258e761b04SAndy Whitcroft $type = 'C'; 19268e761b04SAndy Whitcroft 19271f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 19281f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 19291f65f947SAndy Whitcroft $type = 'N'; 19301f65f947SAndy Whitcroft 19311f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 19321f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 19331f65f947SAndy Whitcroft 19341f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 19351f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 19361f65f947SAndy Whitcroft $type = 'E'; 19371f65f947SAndy Whitcroft } else { 19381f65f947SAndy Whitcroft $type = 'N'; 19391f65f947SAndy Whitcroft } 19401f65f947SAndy Whitcroft $av_pend_colon = 'O'; 19411f65f947SAndy Whitcroft 19428e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 194313214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 19446c72ffaaSAndy Whitcroft $type = 'N'; 19456c72ffaaSAndy Whitcroft 19460d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 194774048ed8SAndy Whitcroft my $variant; 194874048ed8SAndy Whitcroft 194974048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 195074048ed8SAndy Whitcroft if ($type eq 'V') { 195174048ed8SAndy Whitcroft $variant = 'B'; 195274048ed8SAndy Whitcroft } else { 195374048ed8SAndy Whitcroft $variant = 'U'; 195474048ed8SAndy Whitcroft } 195574048ed8SAndy Whitcroft 195674048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 195774048ed8SAndy Whitcroft $type = 'N'; 195874048ed8SAndy Whitcroft 19596c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1960c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 19616c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 19626c72ffaaSAndy Whitcroft $type = 'N'; 19636c72ffaaSAndy Whitcroft } 19646c72ffaaSAndy Whitcroft 19656c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1966c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 19676c72ffaaSAndy Whitcroft } 19686c72ffaaSAndy Whitcroft if (defined $1) { 19696c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 19706c72ffaaSAndy Whitcroft $res .= $type x length($1); 19716c72ffaaSAndy Whitcroft } 19726c72ffaaSAndy Whitcroft } 19736c72ffaaSAndy Whitcroft 19741f65f947SAndy Whitcroft return ($res, $var); 19756c72ffaaSAndy Whitcroft} 19766c72ffaaSAndy Whitcroft 19778905a67cSAndy Whitcroftsub possible { 197813214adfSAndy Whitcroft my ($possible, $line) = @_; 19799a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 19800776e594SAndy Whitcroft ^(?: 19810776e594SAndy Whitcroft $Modifier| 19820776e594SAndy Whitcroft $Storage| 19830776e594SAndy Whitcroft $Type| 19849a974fdbSAndy Whitcroft DEFINE_\S+ 19859a974fdbSAndy Whitcroft )$| 19869a974fdbSAndy Whitcroft ^(?: 19870776e594SAndy Whitcroft goto| 19880776e594SAndy Whitcroft return| 19890776e594SAndy Whitcroft case| 19900776e594SAndy Whitcroft else| 19910776e594SAndy Whitcroft asm|__asm__| 199289a88353SAndy Whitcroft do| 199389a88353SAndy Whitcroft \#| 199489a88353SAndy Whitcroft \#\#| 19959a974fdbSAndy Whitcroft )(?:\s|$)| 19960776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 19979a974fdbSAndy Whitcroft )}x; 19989a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 19999a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 2000c45dcabdSAndy Whitcroft # Check for modifiers. 2001c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 2002c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 2003c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 2004c45dcabdSAndy Whitcroft 2005c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 2006c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 2007d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 20089a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 2009d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 2010485ff23eSAlex Dowad push(@modifierListFile, $modifier); 2011d2506586SAndy Whitcroft } 20129a974fdbSAndy Whitcroft } 2013c45dcabdSAndy Whitcroft 2014c45dcabdSAndy Whitcroft } else { 201513214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 2016485ff23eSAlex Dowad push(@typeListFile, $possible); 2017c45dcabdSAndy Whitcroft } 20188905a67cSAndy Whitcroft build_types(); 20190776e594SAndy Whitcroft } else { 20200776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 20218905a67cSAndy Whitcroft } 20228905a67cSAndy Whitcroft} 20238905a67cSAndy Whitcroft 20246c72ffaaSAndy Whitcroftmy $prefix = ''; 20256c72ffaaSAndy Whitcroft 2026000d1cc1SJoe Perchessub show_type { 2027cbec18afSJoe Perches my ($type) = @_; 202891bfe484SJoe Perches 2029522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 2030522b837cSAlexey Dobriyan 2031cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 2032cbec18afSJoe Perches 2033cbec18afSJoe Perches return !defined $ignore_type{$type}; 2034000d1cc1SJoe Perches} 2035000d1cc1SJoe Perches 2036f0a594c1SAndy Whitcroftsub report { 2037cbec18afSJoe Perches my ($level, $type, $msg) = @_; 2038cbec18afSJoe Perches 2039cbec18afSJoe Perches if (!show_type($type) || 2040cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 2041773647a0SAndy Whitcroft return 0; 2042773647a0SAndy Whitcroft } 204357230297SJoe Perches my $output = ''; 2044737c0767SJohn Brooks if ($color) { 204557230297SJoe Perches if ($level eq 'ERROR') { 204657230297SJoe Perches $output .= RED; 204757230297SJoe Perches } elsif ($level eq 'WARNING') { 204857230297SJoe Perches $output .= YELLOW; 2049000d1cc1SJoe Perches } else { 205057230297SJoe Perches $output .= GREEN; 2051000d1cc1SJoe Perches } 205257230297SJoe Perches } 205357230297SJoe Perches $output .= $prefix . $level . ':'; 205457230297SJoe Perches if ($show_types) { 2055737c0767SJohn Brooks $output .= BLUE if ($color); 205657230297SJoe Perches $output .= "$type:"; 205757230297SJoe Perches } 2058737c0767SJohn Brooks $output .= RESET if ($color); 205957230297SJoe Perches $output .= ' ' . $msg . "\n"; 206034d8815fSJoe Perches 206134d8815fSJoe Perches if ($showfile) { 206234d8815fSJoe Perches my @lines = split("\n", $output, -1); 206334d8815fSJoe Perches splice(@lines, 1, 1); 206434d8815fSJoe Perches $output = join("\n", @lines); 206534d8815fSJoe Perches } 206657230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 20678905a67cSAndy Whitcroft 206857230297SJoe Perches push(our @report, $output); 2069773647a0SAndy Whitcroft 2070773647a0SAndy Whitcroft return 1; 2071f0a594c1SAndy Whitcroft} 2072cbec18afSJoe Perches 2073f0a594c1SAndy Whitcroftsub report_dump { 207413214adfSAndy Whitcroft our @report; 2075f0a594c1SAndy Whitcroft} 2076000d1cc1SJoe Perches 2077d752fcc8SJoe Perchessub fixup_current_range { 2078d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2079d752fcc8SJoe Perches 2080d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2081d752fcc8SJoe Perches my $o = $1; 2082d752fcc8SJoe Perches my $l = $2; 2083d752fcc8SJoe Perches my $no = $o + $offset; 2084d752fcc8SJoe Perches my $nl = $l + $length; 2085d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2086d752fcc8SJoe Perches } 2087d752fcc8SJoe Perches} 2088d752fcc8SJoe Perches 2089d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2090d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2091d752fcc8SJoe Perches 2092d752fcc8SJoe Perches my $range_last_linenr = 0; 2093d752fcc8SJoe Perches my $delta_offset = 0; 2094d752fcc8SJoe Perches 2095d752fcc8SJoe Perches my $old_linenr = 0; 2096d752fcc8SJoe Perches my $new_linenr = 0; 2097d752fcc8SJoe Perches 2098d752fcc8SJoe Perches my $next_insert = 0; 2099d752fcc8SJoe Perches my $next_delete = 0; 2100d752fcc8SJoe Perches 2101d752fcc8SJoe Perches my @lines = (); 2102d752fcc8SJoe Perches 2103d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2104d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2105d752fcc8SJoe Perches 2106d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2107d752fcc8SJoe Perches my $save_line = 1; 2108d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2109323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2110d752fcc8SJoe Perches $delta_offset = 0; 2111d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2112d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2113d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2114d752fcc8SJoe Perches } 2115d752fcc8SJoe Perches 2116d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2117d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2118d752fcc8SJoe Perches $save_line = 0; 2119d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2120d752fcc8SJoe Perches } 2121d752fcc8SJoe Perches 2122d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2123d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2124d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2125d752fcc8SJoe Perches $new_linenr++; 2126d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2127d752fcc8SJoe Perches } 2128d752fcc8SJoe Perches 2129d752fcc8SJoe Perches if ($save_line) { 2130d752fcc8SJoe Perches push(@lines, $line); 2131d752fcc8SJoe Perches $new_linenr++; 2132d752fcc8SJoe Perches } 2133d752fcc8SJoe Perches 2134d752fcc8SJoe Perches $old_linenr++; 2135d752fcc8SJoe Perches } 2136d752fcc8SJoe Perches 2137d752fcc8SJoe Perches return @lines; 2138d752fcc8SJoe Perches} 2139d752fcc8SJoe Perches 2140f2d7e4d4SJoe Perchessub fix_insert_line { 2141f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2142f2d7e4d4SJoe Perches 2143f2d7e4d4SJoe Perches my $inserted = { 2144f2d7e4d4SJoe Perches LINENR => $linenr, 2145f2d7e4d4SJoe Perches LINE => $line, 2146f2d7e4d4SJoe Perches }; 2147f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2148f2d7e4d4SJoe Perches} 2149f2d7e4d4SJoe Perches 2150f2d7e4d4SJoe Perchessub fix_delete_line { 2151f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2152f2d7e4d4SJoe Perches 2153f2d7e4d4SJoe Perches my $deleted = { 2154f2d7e4d4SJoe Perches LINENR => $linenr, 2155f2d7e4d4SJoe Perches LINE => $line, 2156f2d7e4d4SJoe Perches }; 2157f2d7e4d4SJoe Perches 2158f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2159f2d7e4d4SJoe Perches} 2160f2d7e4d4SJoe Perches 2161de7d4f0eSAndy Whitcroftsub ERROR { 2162cbec18afSJoe Perches my ($type, $msg) = @_; 2163cbec18afSJoe Perches 2164cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2165de7d4f0eSAndy Whitcroft our $clean = 0; 21666c72ffaaSAndy Whitcroft our $cnt_error++; 21673705ce5bSJoe Perches return 1; 2168de7d4f0eSAndy Whitcroft } 21693705ce5bSJoe Perches return 0; 2170773647a0SAndy Whitcroft} 2171de7d4f0eSAndy Whitcroftsub WARN { 2172cbec18afSJoe Perches my ($type, $msg) = @_; 2173cbec18afSJoe Perches 2174cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2175de7d4f0eSAndy Whitcroft our $clean = 0; 21766c72ffaaSAndy Whitcroft our $cnt_warn++; 21773705ce5bSJoe Perches return 1; 2178de7d4f0eSAndy Whitcroft } 21793705ce5bSJoe Perches return 0; 2180773647a0SAndy Whitcroft} 2181de7d4f0eSAndy Whitcroftsub CHK { 2182cbec18afSJoe Perches my ($type, $msg) = @_; 2183cbec18afSJoe Perches 2184cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2185de7d4f0eSAndy Whitcroft our $clean = 0; 21866c72ffaaSAndy Whitcroft our $cnt_chk++; 21873705ce5bSJoe Perches return 1; 21886c72ffaaSAndy Whitcroft } 21893705ce5bSJoe Perches return 0; 2190de7d4f0eSAndy Whitcroft} 2191de7d4f0eSAndy Whitcroft 21926ecd9674SAndy Whitcroftsub check_absolute_file { 21936ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 21946ecd9674SAndy Whitcroft my $file = $absolute; 21956ecd9674SAndy Whitcroft 21966ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 21976ecd9674SAndy Whitcroft 21986ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 21996ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 22006ecd9674SAndy Whitcroft if (-f "$root/$file") { 22016ecd9674SAndy Whitcroft ##print "file<$file>\n"; 22026ecd9674SAndy Whitcroft last; 22036ecd9674SAndy Whitcroft } 22046ecd9674SAndy Whitcroft } 22056ecd9674SAndy Whitcroft if (! -f _) { 22066ecd9674SAndy Whitcroft return 0; 22076ecd9674SAndy Whitcroft } 22086ecd9674SAndy Whitcroft 22096ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 22106ecd9674SAndy Whitcroft my $prefix = $absolute; 22116ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 22126ecd9674SAndy Whitcroft 22136ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 22146ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2215000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2216000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 22176ecd9674SAndy Whitcroft } 22186ecd9674SAndy Whitcroft} 22196ecd9674SAndy Whitcroft 22203705ce5bSJoe Perchessub trim { 22213705ce5bSJoe Perches my ($string) = @_; 22223705ce5bSJoe Perches 2223b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2224b34c648bSJoe Perches 2225b34c648bSJoe Perches return $string; 2226b34c648bSJoe Perches} 2227b34c648bSJoe Perches 2228b34c648bSJoe Perchessub ltrim { 2229b34c648bSJoe Perches my ($string) = @_; 2230b34c648bSJoe Perches 2231b34c648bSJoe Perches $string =~ s/^\s+//; 2232b34c648bSJoe Perches 2233b34c648bSJoe Perches return $string; 2234b34c648bSJoe Perches} 2235b34c648bSJoe Perches 2236b34c648bSJoe Perchessub rtrim { 2237b34c648bSJoe Perches my ($string) = @_; 2238b34c648bSJoe Perches 2239b34c648bSJoe Perches $string =~ s/\s+$//; 22403705ce5bSJoe Perches 22413705ce5bSJoe Perches return $string; 22423705ce5bSJoe Perches} 22433705ce5bSJoe Perches 224452ea8506SJoe Perchessub string_find_replace { 224552ea8506SJoe Perches my ($string, $find, $replace) = @_; 224652ea8506SJoe Perches 224752ea8506SJoe Perches $string =~ s/$find/$replace/g; 224852ea8506SJoe Perches 224952ea8506SJoe Perches return $string; 225052ea8506SJoe Perches} 225152ea8506SJoe Perches 22523705ce5bSJoe Perchessub tabify { 22533705ce5bSJoe Perches my ($leading) = @_; 22543705ce5bSJoe Perches 22553705ce5bSJoe Perches my $source_indent = 8; 22563705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 22573705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 22583705ce5bSJoe Perches 22593705ce5bSJoe Perches #convert leading spaces to tabs 22603705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 22613705ce5bSJoe Perches #Remove spaces before a tab 22623705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 22633705ce5bSJoe Perches 22643705ce5bSJoe Perches return "$leading"; 22653705ce5bSJoe Perches} 22663705ce5bSJoe Perches 2267d1fe9c09SJoe Perchessub pos_last_openparen { 2268d1fe9c09SJoe Perches my ($line) = @_; 2269d1fe9c09SJoe Perches 2270d1fe9c09SJoe Perches my $pos = 0; 2271d1fe9c09SJoe Perches 2272d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2273d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2274d1fe9c09SJoe Perches 2275d1fe9c09SJoe Perches my $last_openparen = 0; 2276d1fe9c09SJoe Perches 2277d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2278d1fe9c09SJoe Perches return -1; 2279d1fe9c09SJoe Perches } 2280d1fe9c09SJoe Perches 2281d1fe9c09SJoe Perches my $len = length($line); 2282d1fe9c09SJoe Perches 2283d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2284d1fe9c09SJoe Perches my $string = substr($line, $pos); 2285d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2286d1fe9c09SJoe Perches $pos += length($1) - 1; 2287d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2288d1fe9c09SJoe Perches $last_openparen = $pos; 2289d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2290d1fe9c09SJoe Perches last; 2291d1fe9c09SJoe Perches } 2292d1fe9c09SJoe Perches } 2293d1fe9c09SJoe Perches 229491cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2295d1fe9c09SJoe Perches} 2296d1fe9c09SJoe Perches 22970a920b5bSAndy Whitcroftsub process { 22980a920b5bSAndy Whitcroft my $filename = shift; 22990a920b5bSAndy Whitcroft 23000a920b5bSAndy Whitcroft my $linenr=0; 23010a920b5bSAndy Whitcroft my $prevline=""; 2302c2fdda0dSAndy Whitcroft my $prevrawline=""; 23030a920b5bSAndy Whitcroft my $stashline=""; 2304c2fdda0dSAndy Whitcroft my $stashrawline=""; 23050a920b5bSAndy Whitcroft 23064a0df2efSAndy Whitcroft my $length; 23070a920b5bSAndy Whitcroft my $indent; 23080a920b5bSAndy Whitcroft my $previndent=0; 23090a920b5bSAndy Whitcroft my $stashindent=0; 23100a920b5bSAndy Whitcroft 2311de7d4f0eSAndy Whitcroft our $clean = 1; 23120a920b5bSAndy Whitcroft my $signoff = 0; 2313cd261496SGeert Uytterhoeven my $author = ''; 2314cd261496SGeert Uytterhoeven my $authorsignoff = 0; 23150a920b5bSAndy Whitcroft my $is_patch = 0; 2316133712a2SRob Herring my $is_binding_patch = -1; 231729ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 231815662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 2319ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2320490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2321bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 23222a076f40SJoe Perches my $commit_log_long_line = 0; 2323e518e9a5SJoe Perches my $commit_log_has_diff = 0; 232413f1937eSJoe Perches my $reported_maintainer_file = 0; 2325fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2326fa64205dSPasi Savanainen 2327365dd4eaSJoe Perches my $last_blank_line = 0; 23285e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2329365dd4eaSJoe Perches 233013214adfSAndy Whitcroft our @report = (); 23316c72ffaaSAndy Whitcroft our $cnt_lines = 0; 23326c72ffaaSAndy Whitcroft our $cnt_error = 0; 23336c72ffaaSAndy Whitcroft our $cnt_warn = 0; 23346c72ffaaSAndy Whitcroft our $cnt_chk = 0; 23356c72ffaaSAndy Whitcroft 23360a920b5bSAndy Whitcroft # Trace the real file/line as we go. 23370a920b5bSAndy Whitcroft my $realfile = ''; 23380a920b5bSAndy Whitcroft my $realline = 0; 23390a920b5bSAndy Whitcroft my $realcnt = 0; 23400a920b5bSAndy Whitcroft my $here = ''; 234177cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 23420a920b5bSAndy Whitcroft my $in_comment = 0; 2343c2fdda0dSAndy Whitcroft my $comment_edge = 0; 23440a920b5bSAndy Whitcroft my $first_line = 0; 23451e855726SWolfram Sang my $p1_prefix = ''; 23460a920b5bSAndy Whitcroft 234713214adfSAndy Whitcroft my $prev_values = 'E'; 234813214adfSAndy Whitcroft 234913214adfSAndy Whitcroft # suppression flags 2350773647a0SAndy Whitcroft my %suppress_ifbraces; 2351170d3a22SAndy Whitcroft my %suppress_whiletrailers; 23522b474a1aSAndy Whitcroft my %suppress_export; 23533e469cdcSAndy Whitcroft my $suppress_statement = 0; 2354653d4876SAndy Whitcroft 23557e51f197SJoe Perches my %signatures = (); 2356323c1260SJoe Perches 2357c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2358de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2359c2fdda0dSAndy Whitcroft # 2360de7d4f0eSAndy Whitcroft my @setup_docs = (); 2361de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2362773647a0SAndy Whitcroft 2363d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2364d8b07710SJoe Perches 23659f3a8992SRob Herring my $checklicenseline = 1; 23669f3a8992SRob Herring 2367773647a0SAndy Whitcroft sanitise_line_reset(); 2368c2fdda0dSAndy Whitcroft my $line; 2369c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2370773647a0SAndy Whitcroft $linenr++; 2371773647a0SAndy Whitcroft $line = $rawline; 2372c2fdda0dSAndy Whitcroft 23733705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 23743705ce5bSJoe Perches 2375773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2376de7d4f0eSAndy Whitcroft $setup_docs = 0; 23778c27ceffSMauro Carvalho Chehab if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { 2378de7d4f0eSAndy Whitcroft $setup_docs = 1; 2379de7d4f0eSAndy Whitcroft } 2380773647a0SAndy Whitcroft #next; 2381de7d4f0eSAndy Whitcroft } 238274fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2383773647a0SAndy Whitcroft $realline=$1-1; 2384773647a0SAndy Whitcroft if (defined $2) { 2385773647a0SAndy Whitcroft $realcnt=$3+1; 2386773647a0SAndy Whitcroft } else { 2387773647a0SAndy Whitcroft $realcnt=1+1; 2388773647a0SAndy Whitcroft } 2389c45dcabdSAndy Whitcroft $in_comment = 0; 2390773647a0SAndy Whitcroft 2391773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2392773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2393773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2394773647a0SAndy Whitcroft # at context start. 2395773647a0SAndy Whitcroft my $edge; 239601fa9147SAndy Whitcroft my $cnt = $realcnt; 239701fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 239801fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 239901fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 240001fa9147SAndy Whitcroft $cnt--; 240101fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2402721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2403fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2404fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2405fae17daeSAndy Whitcroft ($edge) = $1; 2406fae17daeSAndy Whitcroft last; 2407fae17daeSAndy Whitcroft } 2408773647a0SAndy Whitcroft } 2409773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2410773647a0SAndy Whitcroft $in_comment = 1; 2411773647a0SAndy Whitcroft } 2412773647a0SAndy Whitcroft 2413773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2414773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2415773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2416773647a0SAndy Whitcroft if (!defined $edge && 241783242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2418773647a0SAndy Whitcroft { 2419773647a0SAndy Whitcroft $in_comment = 1; 2420773647a0SAndy Whitcroft } 2421773647a0SAndy Whitcroft 2422773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2423773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2424773647a0SAndy Whitcroft 2425171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2426773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2427171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2428773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2429773647a0SAndy Whitcroft } 2430773647a0SAndy Whitcroft push(@lines, $line); 2431773647a0SAndy Whitcroft 2432773647a0SAndy Whitcroft if ($realcnt > 1) { 2433773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2434773647a0SAndy Whitcroft } else { 2435773647a0SAndy Whitcroft $realcnt = 0; 2436773647a0SAndy Whitcroft } 2437773647a0SAndy Whitcroft 2438773647a0SAndy Whitcroft #print "==>$rawline\n"; 2439773647a0SAndy Whitcroft #print "-->$line\n"; 2440de7d4f0eSAndy Whitcroft 2441de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2442de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2443de7d4f0eSAndy Whitcroft } 2444de7d4f0eSAndy Whitcroft } 2445de7d4f0eSAndy Whitcroft 24466c72ffaaSAndy Whitcroft $prefix = ''; 24476c72ffaaSAndy Whitcroft 2448773647a0SAndy Whitcroft $realcnt = 0; 2449773647a0SAndy Whitcroft $linenr = 0; 2450194f66fcSJoe Perches $fixlinenr = -1; 24510a920b5bSAndy Whitcroft foreach my $line (@lines) { 24520a920b5bSAndy Whitcroft $linenr++; 2453194f66fcSJoe Perches $fixlinenr++; 24541b5539b1SJoe Perches my $sline = $line; #copy of $line 24551b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 24560a920b5bSAndy Whitcroft 2457c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 24586c72ffaaSAndy Whitcroft 245912c253abSJoe Perches# check if it's a mode change, rename or start of a patch 246012c253abSJoe Perches if (!$in_commit_log && 246112c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 246212c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 246312c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 246412c253abSJoe Perches $is_patch = 1; 246512c253abSJoe Perches } 246612c253abSJoe Perches 24670a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2468e518e9a5SJoe Perches if (!$in_commit_log && 246974fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 247074fd4f34SJoe Perches my $context = $4; 24710a920b5bSAndy Whitcroft $is_patch = 1; 24724a0df2efSAndy Whitcroft $first_line = $linenr + 1; 24730a920b5bSAndy Whitcroft $realline=$1-1; 24740a920b5bSAndy Whitcroft if (defined $2) { 24750a920b5bSAndy Whitcroft $realcnt=$3+1; 24760a920b5bSAndy Whitcroft } else { 24770a920b5bSAndy Whitcroft $realcnt=1+1; 24780a920b5bSAndy Whitcroft } 2479c2fdda0dSAndy Whitcroft annotate_reset(); 248013214adfSAndy Whitcroft $prev_values = 'E'; 248113214adfSAndy Whitcroft 2482773647a0SAndy Whitcroft %suppress_ifbraces = (); 2483170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 24842b474a1aSAndy Whitcroft %suppress_export = (); 24853e469cdcSAndy Whitcroft $suppress_statement = 0; 248674fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 248774fd4f34SJoe Perches $context_function = $1; 248874fd4f34SJoe Perches } else { 248974fd4f34SJoe Perches undef $context_function; 249074fd4f34SJoe Perches } 24910a920b5bSAndy Whitcroft next; 24920a920b5bSAndy Whitcroft 24934a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 24944a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 24954a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2496773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 24970a920b5bSAndy Whitcroft $realline++; 2498d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 24990a920b5bSAndy Whitcroft 25004a0df2efSAndy Whitcroft # Measure the line length and indent. 2501c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 25020a920b5bSAndy Whitcroft 25030a920b5bSAndy Whitcroft # Track the previous line. 25040a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 25050a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2506c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2507c2fdda0dSAndy Whitcroft 2508773647a0SAndy Whitcroft #warn "line<$line>\n"; 25096c72ffaaSAndy Whitcroft 2510d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2511d8aaf121SAndy Whitcroft $realcnt--; 25120a920b5bSAndy Whitcroft } 25130a920b5bSAndy Whitcroft 2514cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2515cc77cdcaSAndy Whitcroft 25166c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 25176c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2518773647a0SAndy Whitcroft 25192ac73b4fSJoe Perches my $found_file = 0; 2520773647a0SAndy Whitcroft # extract the filename as it passes 25213bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 25223bf9a009SRabin Vincent $realfile = $1; 25232b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2524270c49a0SJoe Perches $in_commit_log = 0; 25252ac73b4fSJoe Perches $found_file = 1; 25263bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2527773647a0SAndy Whitcroft $realfile = $1; 25282b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2529270c49a0SJoe Perches $in_commit_log = 0; 25301e855726SWolfram Sang 25311e855726SWolfram Sang $p1_prefix = $1; 2532e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2533e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2534000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2535000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 25361e855726SWolfram Sang } 2537773647a0SAndy Whitcroft 2538c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2539000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2540000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2541773647a0SAndy Whitcroft } 25422ac73b4fSJoe Perches $found_file = 1; 25432ac73b4fSJoe Perches } 25442ac73b4fSJoe Perches 254534d8815fSJoe Perches#make up the handle for any error we report on this line 254634d8815fSJoe Perches if ($showfile) { 254734d8815fSJoe Perches $prefix = "$realfile:$realline: " 254834d8815fSJoe Perches } elsif ($emacs) { 25497d3a9f67SJoe Perches if ($file) { 25507d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 25517d3a9f67SJoe Perches } else { 255234d8815fSJoe Perches $prefix = "$filename:$linenr: "; 255334d8815fSJoe Perches } 25547d3a9f67SJoe Perches } 255534d8815fSJoe Perches 25562ac73b4fSJoe Perches if ($found_file) { 255785b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 255885b0ee18SJoe Perches WARN("OBSOLETE", 255985b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 256085b0ee18SJoe Perches } 25617bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 25622ac73b4fSJoe Perches $check = 1; 25632ac73b4fSJoe Perches } else { 25642ac73b4fSJoe Perches $check = $check_orig; 25652ac73b4fSJoe Perches } 25669f3a8992SRob Herring $checklicenseline = 1; 2567133712a2SRob Herring 2568133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2569133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2570133712a2SRob Herring 2571133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2572133712a2SRob Herring 2573133712a2SRob Herring if (($last_binding_patch != -1) && 2574133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2575133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2576133712a2SRob Herring "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n"); 2577133712a2SRob Herring } 2578133712a2SRob Herring } 2579133712a2SRob Herring 2580773647a0SAndy Whitcroft next; 2581773647a0SAndy Whitcroft } 2582773647a0SAndy Whitcroft 2583389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 25840a920b5bSAndy Whitcroft 2585c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2586c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2587c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 25880a920b5bSAndy Whitcroft 25896c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 25906c72ffaaSAndy Whitcroft 2591490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2592490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2593490b292cSJoe Perches if ($in_commit_log) { 2594490b292cSJoe Perches if ($line !~ /^\s*$/) { 2595490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2596490b292cSJoe Perches } 2597490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2598490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2599490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2600490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2601490b292cSJoe Perches } 2602490b292cSJoe Perches 2603e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2604e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2605e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2606e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2607e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2608e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2609e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2610e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2611e518e9a5SJoe Perches $commit_log_has_diff = 1; 2612e518e9a5SJoe Perches } 2613e518e9a5SJoe Perches 26143bf9a009SRabin Vincent# Check for incorrect file permissions 26153bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 26163bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 261704db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 261804db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2619000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2620000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 26213bf9a009SRabin Vincent } 26223bf9a009SRabin Vincent } 26233bf9a009SRabin Vincent 2624cd261496SGeert Uytterhoeven# Check the patch for a From: 2625cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2626cd261496SGeert Uytterhoeven $author = $1; 2627cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2628cd261496SGeert Uytterhoeven $author =~ s/"//g; 2629dfa05c28SJoe Perches $author = reformat_email($author); 2630cd261496SGeert Uytterhoeven } 2631cd261496SGeert Uytterhoeven 263220112475SJoe Perches# Check the patch for a signoff: 2633dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 26344a0df2efSAndy Whitcroft $signoff++; 263515662b3eSJoe Perches $in_commit_log = 0; 2636cd261496SGeert Uytterhoeven if ($author ne '') { 2637dfa05c28SJoe Perches if (same_email_addresses($1, $author)) { 2638cd261496SGeert Uytterhoeven $authorsignoff = 1; 2639cd261496SGeert Uytterhoeven } 2640cd261496SGeert Uytterhoeven } 26410a920b5bSAndy Whitcroft } 264220112475SJoe Perches 2643e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2644e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2645e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2646e0d975b1SJoe Perches $reported_maintainer_file = 1; 2647e0d975b1SJoe Perches } 2648e0d975b1SJoe Perches 264920112475SJoe Perches# Check signature styles 2650270c49a0SJoe Perches if (!$in_header_lines && 2651ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 265220112475SJoe Perches my $space_before = $1; 265320112475SJoe Perches my $sign_off = $2; 265420112475SJoe Perches my $space_after = $3; 265520112475SJoe Perches my $email = $4; 265620112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 265720112475SJoe Perches 2658ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2659ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2660ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2661ce0338dfSJoe Perches } 266220112475SJoe Perches if (defined $space_before && $space_before ne "") { 26633705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26643705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 26653705ce5bSJoe Perches $fix) { 2666194f66fcSJoe Perches $fixed[$fixlinenr] = 26673705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26683705ce5bSJoe Perches } 266920112475SJoe Perches } 267020112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 26713705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26723705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 26733705ce5bSJoe Perches $fix) { 2674194f66fcSJoe Perches $fixed[$fixlinenr] = 26753705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26763705ce5bSJoe Perches } 26773705ce5bSJoe Perches 267820112475SJoe Perches } 267920112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 26803705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26813705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 26823705ce5bSJoe Perches $fix) { 2683194f66fcSJoe Perches $fixed[$fixlinenr] = 26843705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26853705ce5bSJoe Perches } 268620112475SJoe Perches } 268720112475SJoe Perches 2688dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 268920112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 269020112475SJoe Perches if ($suggested_email eq "") { 2691000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2692000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 269320112475SJoe Perches } else { 269420112475SJoe Perches my $dequoted = $suggested_email; 269520112475SJoe Perches $dequoted =~ s/^"//; 269620112475SJoe Perches $dequoted =~ s/" </ </; 269720112475SJoe Perches # Don't force email to have quotes 269820112475SJoe Perches # Allow just an angle bracketed address 2699dfa05c28SJoe Perches if (!same_email_addresses($email, $suggested_email)) { 2700000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2701000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 270220112475SJoe Perches } 27030a920b5bSAndy Whitcroft } 27047e51f197SJoe Perches 27057e51f197SJoe Perches# Check for duplicate signatures 27067e51f197SJoe Perches my $sig_nospace = $line; 27077e51f197SJoe Perches $sig_nospace =~ s/\s//g; 27087e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 27097e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 27107e51f197SJoe Perches WARN("BAD_SIGN_OFF", 27117e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 27127e51f197SJoe Perches } else { 27137e51f197SJoe Perches $signatures{$sig_nospace} = 1; 27147e51f197SJoe Perches } 27156c5d24eeSSean Christopherson 27166c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 27176c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 27186c5d24eeSSean Christopherson if ($email eq $author) { 27196c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27206c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 27216c5d24eeSSean Christopherson } 27226c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 27236c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27246c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 27256c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 27266c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27276c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27286c5d24eeSSean Christopherson } elsif ($1 ne $email) { 27296c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27306c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27316c5d24eeSSean Christopherson } 27326c5d24eeSSean Christopherson } 27330a920b5bSAndy Whitcroft } 27340a920b5bSAndy Whitcroft 2735a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2736a2fe16b9SJoe Perches if ($in_header_lines && 2737a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2738a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2739a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2740a2fe16b9SJoe Perches } 2741a2fe16b9SJoe Perches 27427ebd05efSChristopher Covington# Check for unwanted Gerrit info 27437ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 27447ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 27457ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 27467ebd05efSChristopher Covington } 27477ebd05efSChristopher Covington 2748369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2749369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2750369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2751369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2752369c8dd3SJoe Perches # timestamp 2753634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 2754634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 2755634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 2756634cffccSJoe Perches # stack dump address styles 2757369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2758369c8dd3SJoe Perches } 2759369c8dd3SJoe Perches 27602a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 27612a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2762bf4daf12SJoe Perches length($line) > 75 && 2763bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2764bf4daf12SJoe Perches # file delta changes 2765bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2766bf4daf12SJoe Perches # filename then : 2767bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2768bf4daf12SJoe Perches # A Fixes: or Link: line 2769bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 27702a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 27712a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 27722a076f40SJoe Perches $commit_log_long_line = 1; 27732a076f40SJoe Perches } 27742a076f40SJoe Perches 2775bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2776bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2777bf4daf12SJoe Perches $line =~ /^\s*$/) { 2778bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2779bf4daf12SJoe Perches } 2780bf4daf12SJoe Perches 27810d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2782369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2783aab38f51SJoe Perches $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i && 2784e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2785fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2786aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2787369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2788bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2789fe043ea1SJoe Perches my $init_char = "c"; 2790fe043ea1SJoe Perches my $orig_commit = ""; 27910d7835fcSJoe Perches my $short = 1; 27920d7835fcSJoe Perches my $long = 0; 27930d7835fcSJoe Perches my $case = 1; 27940d7835fcSJoe Perches my $space = 1; 27950d7835fcSJoe Perches my $hasdesc = 0; 279619c146a6SJoe Perches my $hasparens = 0; 27970d7835fcSJoe Perches my $id = '0123456789ab'; 27980d7835fcSJoe Perches my $orig_desc = "commit description"; 27990d7835fcSJoe Perches my $description = ""; 28000d7835fcSJoe Perches 2801fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2802fe043ea1SJoe Perches $init_char = $1; 2803fe043ea1SJoe Perches $orig_commit = lc($2); 2804fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2805fe043ea1SJoe Perches $orig_commit = lc($1); 2806fe043ea1SJoe Perches } 2807fe043ea1SJoe Perches 28080d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 28090d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 28100d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 28110d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 28120d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 28130d7835fcSJoe Perches $orig_desc = $1; 281419c146a6SJoe Perches $hasparens = 1; 28150d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 28160d7835fcSJoe Perches defined $rawlines[$linenr] && 28170d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 28180d7835fcSJoe Perches $orig_desc = $1; 281919c146a6SJoe Perches $hasparens = 1; 2820b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2821b671fde0SJoe Perches defined $rawlines[$linenr] && 2822b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2823b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2824b671fde0SJoe Perches $orig_desc = $1; 2825b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2826b671fde0SJoe Perches $orig_desc .= " " . $1; 282719c146a6SJoe Perches $hasparens = 1; 28280d7835fcSJoe Perches } 28290d7835fcSJoe Perches 28300d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 28310d7835fcSJoe Perches $id, $orig_desc); 28320d7835fcSJoe Perches 2833948b133aSHeinrich Schuchardt if (defined($id) && 2834948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2835d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 28360d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 28370d7835fcSJoe Perches } 2838d311cd44SJoe Perches } 2839d311cd44SJoe Perches 284013f1937eSJoe Perches# Check for added, moved or deleted files 284113f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 284213f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 284313f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 284413f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 284513f1937eSJoe Perches (defined($1) || defined($2))))) { 2846a82603a8SAndrew Jeffery $is_patch = 1; 284713f1937eSJoe Perches $reported_maintainer_file = 1; 284813f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 284913f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 285013f1937eSJoe Perches } 285113f1937eSJoe Perches 2852e400edb1SRob Herring# Check for adding new DT bindings not in schema format 2853e400edb1SRob Herring if (!$in_commit_log && 2854e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 2855e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 2856e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 2857e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 2858e400edb1SRob Herring } 2859e400edb1SRob Herring 286000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 28618905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2862000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2863000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 28646c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2865de7d4f0eSAndy Whitcroft } 2866de7d4f0eSAndy Whitcroft 2867de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2868de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2869171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2870171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2871171ae1a4SAndy Whitcroft 2872171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2873171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2874171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2875171ae1a4SAndy Whitcroft 287634d99219SJoe Perches CHK("INVALID_UTF8", 2877000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 287800df344fSAndy Whitcroft } 28790a920b5bSAndy Whitcroft 288015662b3eSJoe Perches# Check if it's the start of a commit log 288115662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 288215662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2883eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2884eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 288515662b3eSJoe Perches $in_header_lines = 0; 288615662b3eSJoe Perches $in_commit_log = 1; 2887ed43c4e5SAllen Hubbe $has_commit_log = 1; 288815662b3eSJoe Perches } 288915662b3eSJoe Perches 2890fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2891fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2892fa64205dSPasi Savanainen if ($in_header_lines && 2893fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2894fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2895fa64205dSPasi Savanainen $non_utf8_charset = 1; 2896fa64205dSPasi Savanainen } 2897fa64205dSPasi Savanainen 2898fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 289915662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2900fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 290115662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 290215662b3eSJoe Perches } 290315662b3eSJoe Perches 2904d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2905d6430f71SJoe Perches if ($tree && $in_commit_log) { 2906d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2907d6430f71SJoe Perches my $file = $1; 2908d6430f71SJoe Perches 2909d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2910d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2911d6430f71SJoe Perches # 2912d6430f71SJoe Perches } else { 2913d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2914d6430f71SJoe Perches } 2915d6430f71SJoe Perches } 2916d6430f71SJoe Perches } 2917d6430f71SJoe Perches 291866b47b4aSKees Cook# Check for various typo / spelling mistakes 291966d7a382SJoe Perches if (defined($misspellings) && 292066d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2921ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 292266b47b4aSKees Cook my $typo = $1; 292366b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 292466b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 292566b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 29260675a8fbSJean Delvare my $msg_level = \&WARN; 29270675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29280675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 292966b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 293066b47b4aSKees Cook $fix) { 293166b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 293266b47b4aSKees Cook } 293366b47b4aSKees Cook } 293466b47b4aSKees Cook } 293566b47b4aSKees Cook 2936a8dd86bfSMatteo Croce# check for invalid commit id 2937a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 2938a8dd86bfSMatteo Croce my $id; 2939a8dd86bfSMatteo Croce my $description; 2940a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 2941a8dd86bfSMatteo Croce if (!defined($id)) { 2942a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 2943a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 2944a8dd86bfSMatteo Croce } 2945a8dd86bfSMatteo Croce } 2946a8dd86bfSMatteo Croce 294730670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 294830670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 294900df344fSAndy Whitcroft 29500a920b5bSAndy Whitcroft#trailing whitespace 29519c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2952c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2953d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2954d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2955d5e616fcSJoe Perches $fix) { 2956194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2957d5e616fcSJoe Perches } 2958c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2959c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29603705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 29613705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 29623705ce5bSJoe Perches $fix) { 2963194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 29643705ce5bSJoe Perches } 29653705ce5bSJoe Perches 2966d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 29670a920b5bSAndy Whitcroft } 29685368df20SAndy Whitcroft 29694783f894SJosh Triplett# Check for FSF mailing addresses. 2970109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 29711bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 29723e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 29733e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 29744783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29750675a8fbSJean Delvare my $msg_level = \&ERROR; 29760675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29770675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 29784783f894SJosh 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) 29794783f894SJosh Triplett } 29804783f894SJosh Triplett 29813354957aSAndi Kleen# check for Kconfig help text having a real description 29829fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 29839fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 29843354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 2985678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 2986678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 2987678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 2988678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 29893354957aSAndi Kleen my $length = 0; 29909fe287d7SAndy Whitcroft my $cnt = $realcnt; 29919fe287d7SAndy Whitcroft my $ln = $linenr + 1; 29929fe287d7SAndy Whitcroft my $f; 2993a1385803SAndy Whitcroft my $is_start = 0; 29949fe287d7SAndy Whitcroft my $is_end = 0; 2995a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 29969fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 29979fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 29989fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 29999fe287d7SAndy Whitcroft 30009fe287d7SAndy Whitcroft next if ($f =~ /^-/); 30018d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3002a1385803SAndy Whitcroft 300386adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3004a1385803SAndy Whitcroft $is_start = 1; 300584af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 300684af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 300784af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 300884af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 300984af7a61SUlf Magnusson } 3010a1385803SAndy Whitcroft $length = -1; 3011a1385803SAndy Whitcroft } 3012a1385803SAndy Whitcroft 30139fe287d7SAndy Whitcroft $f =~ s/^.//; 30143354957aSAndi Kleen $f =~ s/#.*//; 30153354957aSAndi Kleen $f =~ s/^\s+//; 30163354957aSAndi Kleen next if ($f =~ /^$/); 3017678ae162SUlf Magnusson 3018678ae162SUlf Magnusson # This only checks context lines in the patch 3019678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3020678ae162SUlf Magnusson # positives, even though some of these are 3021678ae162SUlf Magnusson # common words in help texts 3022678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3023678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 30249fe287d7SAndy Whitcroft $is_end = 1; 30259fe287d7SAndy Whitcroft last; 30269fe287d7SAndy Whitcroft } 30273354957aSAndi Kleen $length++; 30283354957aSAndi Kleen } 302956193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3030000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 303156193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 303256193274SVadim Bendebury } 3033a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 30343354957aSAndi Kleen } 30353354957aSAndi Kleen 3036628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form 3037628f91a2SJoe Perches if ($realfile =~ /^MAINTAINERS$/ && 3038628f91a2SJoe Perches $rawline =~ /^\+[A-Z]:/ && 3039628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3040628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3041628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3042628f91a2SJoe Perches $fix) { 3043628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3044628f91a2SJoe Perches } 3045628f91a2SJoe Perches } 3046628f91a2SJoe Perches 3047327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3048327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3049327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3050327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3051327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3052327953e9SChristoph Jaeger } 3053327953e9SChristoph Jaeger 3054c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3055c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3056c68e5878SArnaud Lacombe my $flag = $1; 3057c68e5878SArnaud Lacombe my $replacement = { 3058c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3059c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3060c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3061c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3062c68e5878SArnaud Lacombe }; 3063c68e5878SArnaud Lacombe 3064c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3065c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3066c68e5878SArnaud Lacombe } 3067c68e5878SArnaud Lacombe 3068bff5da43SRob Herring# check for DT compatible documentation 30697dd05b38SFlorian Vaussard if (defined $root && 30707dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 30717dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 30727dd05b38SFlorian Vaussard 3073bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3074bff5da43SRob Herring 3075cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3076852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3077cc93319bSFlorian Vaussard 3078bff5da43SRob Herring foreach my $compat (@compats) { 3079bff5da43SRob Herring my $compat2 = $compat; 3080185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3081185d566bSRob Herring my $compat3 = $compat; 3082185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3083185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3084bff5da43SRob Herring if ( $? >> 8 ) { 3085bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3086bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3087bff5da43SRob Herring } 3088bff5da43SRob Herring 30894fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 30904fbf32a6SFlorian Vaussard my $vendor = $1; 3091852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3092bff5da43SRob Herring if ( $? >> 8 ) { 3093bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3094cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3095bff5da43SRob Herring } 3096bff5da43SRob Herring } 3097bff5da43SRob Herring } 3098bff5da43SRob Herring 30999f3a8992SRob Herring# check for using SPDX license tag at beginning of files 31009f3a8992SRob Herring if ($realline == $checklicenseline) { 31019f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 31029f3a8992SRob Herring $checklicenseline = 2; 31039f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 31049f3a8992SRob Herring my $comment = ""; 31059f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 31069f3a8992SRob Herring $comment = '/*'; 31079f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 31089f3a8992SRob Herring $comment = '//'; 3109*c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 31109f3a8992SRob Herring $comment = '#'; 31119f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 31129f3a8992SRob Herring $comment = '..'; 31139f3a8992SRob Herring } 31149f3a8992SRob Herring 3115fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3116fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3117fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3118ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3119fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3120fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3121fdf13693SJoe Perches } 3122fdf13693SJoe Perches 31239f3a8992SRob Herring if ($comment !~ /^$/ && 3124ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 31259f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 31269f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 31273b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 31283b6e8ac9SJoe Perches my $spdx_license = $1; 31293b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 31303b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 31313b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 31323b6e8ac9SJoe Perches } 31339f3a8992SRob Herring } 31349f3a8992SRob Herring } 31359f3a8992SRob Herring } 31369f3a8992SRob Herring 31375368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3138d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 31395368df20SAndy Whitcroft 3140a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3141a8da38a9SJoe Perches if ($realline != $checklicenseline && 3142a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3143a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3144a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3145a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3146a8da38a9SJoe Perches } 3147a8da38a9SJoe Perches 314847e0c88bSJoe Perches# line length limit (with some exclusions) 314947e0c88bSJoe Perches# 315047e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 315147e0c88bSJoe Perches# logging functions like pr_info that end in a string 315247e0c88bSJoe Perches# lines with a single string 315347e0c88bSJoe Perches# #defines that are a single string 31542e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 315547e0c88bSJoe Perches# 315647e0c88bSJoe Perches# There are 3 different line length message types: 3157ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 315847e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 315947e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 316047e0c88bSJoe Perches# 316147e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 316247e0c88bSJoe Perches# 316347e0c88bSJoe Perches 3164b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 316547e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 316647e0c88bSJoe Perches 316747e0c88bSJoe Perches # Check the allowed long line types first 316847e0c88bSJoe Perches 316947e0c88bSJoe Perches # logging functions that end in a string that starts 317047e0c88bSJoe Perches # before $max_line_length 317147e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 317247e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 317347e0c88bSJoe Perches $msg_type = ""; 317447e0c88bSJoe Perches 317547e0c88bSJoe Perches # lines with only strings (w/ possible termination) 317647e0c88bSJoe Perches # #defines with only strings 317747e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 317847e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 317947e0c88bSJoe Perches $msg_type = ""; 318047e0c88bSJoe Perches 3181cc147506SJoe Perches # More special cases 3182cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3183cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3184d560a5f8SJoe Perches $msg_type = ""; 3185d560a5f8SJoe Perches 31862e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 31872e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 31882e4bbbc5SAndreas Brauchli $msg_type = ""; 31892e4bbbc5SAndreas Brauchli 319047e0c88bSJoe Perches # Otherwise set the alternate message types 319147e0c88bSJoe Perches 319247e0c88bSJoe Perches # a comment starts before $max_line_length 319347e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 319447e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 319547e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 319647e0c88bSJoe Perches 319747e0c88bSJoe Perches # a quoted string starts before $max_line_length 319847e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 319947e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 320047e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 320147e0c88bSJoe Perches } 320247e0c88bSJoe Perches 320347e0c88bSJoe Perches if ($msg_type ne "" && 320447e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 320547e0c88bSJoe Perches WARN($msg_type, 32066cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 32070a920b5bSAndy Whitcroft } 320847e0c88bSJoe Perches } 32090a920b5bSAndy Whitcroft 32108905a67cSAndy Whitcroft# check for adding lines without a newline. 32118905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3212000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3213000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 32148905a67cSAndy Whitcroft } 32158905a67cSAndy Whitcroft 3216b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3217de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 32180a920b5bSAndy Whitcroft 32190a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 32200a920b5bSAndy Whitcroft# more than 8 must use tabs. 3221c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3222c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3223c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3224d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 32253705ce5bSJoe Perches if (ERROR("CODE_INDENT", 32263705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 32273705ce5bSJoe Perches $fix) { 3228194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 32293705ce5bSJoe Perches } 32300a920b5bSAndy Whitcroft } 32310a920b5bSAndy Whitcroft 323208e44365SAlberto Panizzo# check for space before tabs. 323308e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 323408e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32353705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 32363705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 32373705ce5bSJoe Perches $fix) { 3238194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3239d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 3240194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3241c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 32423705ce5bSJoe Perches } 324308e44365SAlberto Panizzo } 324408e44365SAlberto Panizzo 32456a487211SJoe Perches# check for assignments on the start of a line 32466a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 32476a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 32486a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 32496a487211SJoe Perches } 32506a487211SJoe Perches 3251d1fe9c09SJoe Perches# check for && or || at the start of a line 3252d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3253d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3254d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3255d1fe9c09SJoe Perches } 3256d1fe9c09SJoe Perches 3257a91e8994SJoe Perches# check indentation starts on a tab stop 32585b57980dSJoe Perches if ($perl_version_ok && 3259bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3260a91e8994SJoe Perches my $indent = length($1); 3261a91e8994SJoe Perches if ($indent % 8) { 3262a91e8994SJoe Perches if (WARN("TABSTOP", 3263a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3264a91e8994SJoe Perches $fix) { 3265a91e8994SJoe Perches $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; 3266a91e8994SJoe Perches } 3267a91e8994SJoe Perches } 3268a91e8994SJoe Perches } 3269a91e8994SJoe Perches 3270d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 32715b57980dSJoe Perches if ($perl_version_ok && 3272fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3273d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3274d1fe9c09SJoe Perches my $oldindent = $1; 3275d1fe9c09SJoe Perches my $rest = $2; 3276d1fe9c09SJoe Perches 3277d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3278d1fe9c09SJoe Perches if ($pos >= 0) { 3279b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3280b34a26f3SJoe Perches my $newindent = $2; 3281d1fe9c09SJoe Perches 3282d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3283d1fe9c09SJoe Perches "\t" x ($pos / 8) . 3284d1fe9c09SJoe Perches " " x ($pos % 8); 3285d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3286d1fe9c09SJoe Perches 3287d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3288d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 32893705ce5bSJoe Perches 32903705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 32913705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 32923705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3293194f66fcSJoe Perches $fixed[$fixlinenr] =~ 32943705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 32953705ce5bSJoe Perches } 3296d1fe9c09SJoe Perches } 3297d1fe9c09SJoe Perches } 3298d1fe9c09SJoe Perches } 3299d1fe9c09SJoe Perches 33006ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 33016ab3a970SJoe Perches# avoid checking a few false positives: 33026ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 33036ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 33046ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 33056ab3a970SJoe Perches# multiline macros that define functions 33066ab3a970SJoe Perches# known attributes or the __attribute__ keyword 33076ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 33086ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 33093705ce5bSJoe Perches if (CHK("SPACING", 3310f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 33113705ce5bSJoe Perches $fix) { 3312194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3313f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 33143705ce5bSJoe Perches } 3315aad4f614SJoe Perches } 3316aad4f614SJoe Perches 331786406b1cSJoe Perches# Block comment styles 331886406b1cSJoe Perches# Networking with an initial /* 331905880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3320fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 332185ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 332285ad978cSJoe Perches $realline > 2) { 332305880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 332405880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 332505880600SJoe Perches } 332605880600SJoe Perches 332786406b1cSJoe Perches# Block comments use * on subsequent lines 332886406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 332986406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3330a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 333161135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3332a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 333386406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 333486406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3335a605e32eSJoe Perches } 3336a605e32eSJoe Perches 333786406b1cSJoe Perches# Block comments use */ on trailing lines 333886406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3339c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3340c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3341c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 334286406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 334386406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 334405880600SJoe Perches } 334505880600SJoe Perches 334608eb9b80SJoe Perches# Block comment * alignment 334708eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3348af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3349af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3350af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 335108eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3352af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3353af207524SJoe Perches my $oldindent; 335408eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3355af207524SJoe Perches if (defined($1)) { 3356af207524SJoe Perches $oldindent = expand_tabs($1); 3357af207524SJoe Perches } else { 3358af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3359af207524SJoe Perches $oldindent = expand_tabs($1); 3360af207524SJoe Perches } 336108eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 336208eb9b80SJoe Perches my $newindent = $1; 336308eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3364af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 336508eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 336608eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 336708eb9b80SJoe Perches } 336808eb9b80SJoe Perches } 336908eb9b80SJoe Perches 33707f619191SJoe Perches# check for missing blank lines after struct/union declarations 33717f619191SJoe Perches# with exceptions for various attributes and macros 33727f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 33737f619191SJoe Perches $line =~ /^\+/ && 33747f619191SJoe Perches !($line =~ /^\+\s*$/ || 33757f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 33767f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 33777f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 33787f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 33797f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 33807f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 33810bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 33827f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3383d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3384d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3385d752fcc8SJoe Perches $fix) { 3386f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3387d752fcc8SJoe Perches } 33887f619191SJoe Perches } 33897f619191SJoe Perches 3390365dd4eaSJoe Perches# check for multiple consecutive blank lines 3391365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3392365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3393365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3394d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3395d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3396d752fcc8SJoe Perches $fix) { 3397f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3398d752fcc8SJoe Perches } 3399d752fcc8SJoe Perches 3400365dd4eaSJoe Perches $last_blank_line = $linenr; 3401365dd4eaSJoe Perches } 3402365dd4eaSJoe Perches 34033b617e3bSJoe Perches# check for missing blank lines after declarations 34043f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 34053f7bac03SJoe Perches # actual declarations 34063f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34075a4e1fd3SJoe Perches # function pointer declarations 34085a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34093f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34103f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34113f7bac03SJoe Perches # known declaration macros 34123f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 34133f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 34143f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 34153f7bac03SJoe Perches # other possible extensions of declaration lines 34163f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 34173f7bac03SJoe Perches # not starting a section or a macro "\" extended line 34183f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 34193f7bac03SJoe Perches # looks like a declaration 34203f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34215a4e1fd3SJoe Perches # function pointer declarations 34225a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34233f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34243f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34253f7bac03SJoe Perches # known declaration macros 34263f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 34273f7bac03SJoe Perches # start of struct or union or enum 3428328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 34293f7bac03SJoe Perches # start or end of block or continuation of declaration 34303f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 34313f7bac03SJoe Perches # bitfield continuation 34323f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 34333f7bac03SJoe Perches # other possible extensions of declaration lines 34343f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 34353f7bac03SJoe Perches # indentation of previous and current line are the same 34363f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3437d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3438d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3439d752fcc8SJoe Perches $fix) { 3440f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3441d752fcc8SJoe Perches } 34423b617e3bSJoe Perches } 34433b617e3bSJoe Perches 34445f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 34456b4c5bebSAndy Whitcroft# Exceptions: 34466b4c5bebSAndy Whitcroft# 1) within comments 34476b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 34486b4c5bebSAndy Whitcroft# 3) hanging labels 34493705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 34505f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 34513705ce5bSJoe Perches if (WARN("LEADING_SPACE", 34523705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 34533705ce5bSJoe Perches $fix) { 3454194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 34553705ce5bSJoe Perches } 34565f7ddae6SRaffaele Recalcati } 34575f7ddae6SRaffaele Recalcati 3458b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3459b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3460b9ea10d6SAndy Whitcroft 34615751a24eSJoe Perches# check for unusual line ending [ or ( 34625751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 34635751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 34645751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 34655751a24eSJoe Perches } 34665751a24eSJoe Perches 34674dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 34684dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 34694dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 34704dbed76fSJoe Perches $context_function = $1; 34714dbed76fSJoe Perches } 34724dbed76fSJoe Perches 34734dbed76fSJoe Perches# check if this appears to be the end of function declaration 34744dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 34754dbed76fSJoe Perches undef $context_function; 34764dbed76fSJoe Perches } 34774dbed76fSJoe Perches 3478032a4c0fSJoe Perches# check indentation of any line with a bare else 3479840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3480032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3481032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3482032a4c0fSJoe Perches my $tabs = length($1) + 1; 3483840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3484840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3485840080a0SJoe Perches defined $lines[$linenr] && 3486840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3487032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3488032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3489032a4c0fSJoe Perches } 3490032a4c0fSJoe Perches } 3491032a4c0fSJoe Perches 3492c00df19aSJoe Perches# check indentation of a line with a break; 3493c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3494c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3495c00df19aSJoe Perches my $tabs = $1; 3496c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3497c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3498c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3499c00df19aSJoe Perches } 3500c00df19aSJoe Perches } 3501c00df19aSJoe Perches 3502c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3503cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3504000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3505000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3506c2fdda0dSAndy Whitcroft } 350722f2a2efSAndy Whitcroft 350856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 350956e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 351056e77d70SJoe Perches WARN("HOTPLUG_SECTION", 351156e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 351256e77d70SJoe Perches } 351356e77d70SJoe Perches 35149c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 35152b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 35162b474a1aSAndy Whitcroft $realline_next); 35173e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3518ca819864SJoe Perches if ($linenr > $suppress_statement && 35191b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3520170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3521f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3522171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3523171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3524171ae1a4SAndy Whitcroft 35253e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 35263e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 35273e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 35283e469cdcSAndy Whitcroft # until we hit end of it. 35293e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 35303e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 35313e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 35323e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 35333e469cdcSAndy Whitcroft } 3534f74bd194SAndy Whitcroft 35352b474a1aSAndy Whitcroft # Find the real next line. 35362b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 35372b474a1aSAndy Whitcroft if (defined $realline_next && 35382b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 35392b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 35402b474a1aSAndy Whitcroft $realline_next++; 35412b474a1aSAndy Whitcroft } 35422b474a1aSAndy Whitcroft 3543171ae1a4SAndy Whitcroft my $s = $stat; 3544171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3545cf655043SAndy Whitcroft 3546c2fdda0dSAndy Whitcroft # Ignore goto labels. 3547171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3548c2fdda0dSAndy Whitcroft 3549c2fdda0dSAndy Whitcroft # Ignore functions being called 3550171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3551c2fdda0dSAndy Whitcroft 3552463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3553463f2864SAndy Whitcroft 3554c45dcabdSAndy Whitcroft # declarations always start with types 3555d2506586SAndy 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) { 3556c45dcabdSAndy Whitcroft my $type = $1; 3557c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3558c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3559c45dcabdSAndy Whitcroft 35606c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3561a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3562c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3563c2fdda0dSAndy Whitcroft } 35648905a67cSAndy Whitcroft 35656c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 356665863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3567c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 35689c0ca6f9SAndy Whitcroft } 35698905a67cSAndy Whitcroft 35708905a67cSAndy Whitcroft # Check for any sort of function declaration. 35718905a67cSAndy Whitcroft # int foo(something bar, other baz); 35728905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3573171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 35748905a67cSAndy Whitcroft my ($name_len) = length($1); 35758905a67cSAndy Whitcroft 3576cf655043SAndy Whitcroft my $ctx = $s; 3577773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 35788905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3579cf655043SAndy Whitcroft 35808905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3581c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 35828905a67cSAndy Whitcroft 3583c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 35848905a67cSAndy Whitcroft } 35858905a67cSAndy Whitcroft } 35868905a67cSAndy Whitcroft } 35878905a67cSAndy Whitcroft 35889c0ca6f9SAndy Whitcroft } 35899c0ca6f9SAndy Whitcroft 359000df344fSAndy Whitcroft# 359100df344fSAndy Whitcroft# Checks which may be anchored in the context. 359200df344fSAndy Whitcroft# 359300df344fSAndy Whitcroft 359400df344fSAndy Whitcroft# Check for switch () and associated case and default 359500df344fSAndy Whitcroft# statements should be at the same indent. 359600df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 359700df344fSAndy Whitcroft my $err = ''; 359800df344fSAndy Whitcroft my $sep = ''; 359900df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 360000df344fSAndy Whitcroft shift(@ctx); 360100df344fSAndy Whitcroft for my $ctx (@ctx) { 360200df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 360300df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 360400df344fSAndy Whitcroft $indent != $cindent) { 360500df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 360600df344fSAndy Whitcroft $sep = ''; 360700df344fSAndy Whitcroft } else { 360800df344fSAndy Whitcroft $sep = "[...]\n"; 360900df344fSAndy Whitcroft } 361000df344fSAndy Whitcroft } 361100df344fSAndy Whitcroft if ($err ne '') { 3612000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3613000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3614de7d4f0eSAndy Whitcroft } 3615de7d4f0eSAndy Whitcroft } 3616de7d4f0eSAndy Whitcroft 3617de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3618de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 36190fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3620773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3621773647a0SAndy Whitcroft 36229c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 36238eef05ddSJoe Perches 36248eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 36258eef05ddSJoe Perches WARN("DEEP_INDENTATION", 36268eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 36278eef05ddSJoe Perches } 36288eef05ddSJoe Perches 3629de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3630de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3631de7d4f0eSAndy Whitcroft 3632548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3633548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3634de7d4f0eSAndy Whitcroft 3635548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3636548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3637548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3638548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3639548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3640773647a0SAndy Whitcroft $ctx_ln++; 3641773647a0SAndy Whitcroft } 3642548596d5SAndy Whitcroft 364353210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 364453210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3645773647a0SAndy Whitcroft 3646773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3647000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3648000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 364901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 365000df344fSAndy Whitcroft } 3651773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3652773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3653773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3654773647a0SAndy Whitcroft { 36559c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 36569c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3657000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3658000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 365901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 36609c0ca6f9SAndy Whitcroft } 36619c0ca6f9SAndy Whitcroft } 366200df344fSAndy Whitcroft } 366300df344fSAndy Whitcroft 36644d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3665f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 36663e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 36673e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 36683e469cdcSAndy Whitcroft if (!defined $stat); 36694d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 36704d001e4dSAndy Whitcroft 36714d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 36724d001e4dSAndy Whitcroft 36739f5af480SJoe Perches # remove inline comments 36749f5af480SJoe Perches $s =~ s/$;/ /g; 36759f5af480SJoe Perches $c =~ s/$;/ /g; 36764d001e4dSAndy Whitcroft 36774d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 36786f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 36796f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 36804d001e4dSAndy Whitcroft 36819f5af480SJoe Perches # Make sure we remove the line prefixes as we have 36829f5af480SJoe Perches # none on the first line, and are going to readd them 36839f5af480SJoe Perches # where necessary. 36849f5af480SJoe Perches $s =~ s/\n./\n/gs; 36859f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 36869f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 36879f5af480SJoe Perches } 36889f5af480SJoe Perches 36894d001e4dSAndy Whitcroft # We want to check the first line inside the block 36904d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 36914d001e4dSAndy Whitcroft # 1) any blank line termination 36924d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 36934d001e4dSAndy Whitcroft # 3) any do (...) { 36944d001e4dSAndy Whitcroft my $continuation = 0; 36954d001e4dSAndy Whitcroft my $check = 0; 36964d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 36974d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 36984d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 36994d001e4dSAndy Whitcroft $continuation = 1; 37004d001e4dSAndy Whitcroft } 37019bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 37024d001e4dSAndy Whitcroft $check = 1; 37034d001e4dSAndy Whitcroft $cond_lines++; 37044d001e4dSAndy Whitcroft } 37054d001e4dSAndy Whitcroft 37064d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 37074d001e4dSAndy Whitcroft # preprocessor statement. 37084d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 37094d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 37104d001e4dSAndy Whitcroft $check = 0; 37114d001e4dSAndy Whitcroft } 37124d001e4dSAndy Whitcroft 37139bd49efeSAndy Whitcroft my $cond_ptr = -1; 3714740504c6SAndy Whitcroft $continuation = 0; 37159bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 37169bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 37174d001e4dSAndy Whitcroft 3718f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3719f16fa28fSAndy Whitcroft # is not linear. 3720f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3721f16fa28fSAndy Whitcroft $check = 0; 3722f16fa28fSAndy Whitcroft } 3723f16fa28fSAndy Whitcroft 37249bd49efeSAndy Whitcroft # Ignore: 37259bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 37269bd49efeSAndy Whitcroft # 2) preprocessor lines, and 37279bd49efeSAndy Whitcroft # 3) labels. 3728740504c6SAndy Whitcroft if ($continuation || 3729740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 37309bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 37319bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3732740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 373330dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 37349bd49efeSAndy Whitcroft $cond_lines++; 37359bd49efeSAndy Whitcroft } 37364d001e4dSAndy Whitcroft } 373730dad6ebSAndy Whitcroft } 37384d001e4dSAndy Whitcroft 37394d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 37404d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 37414d001e4dSAndy Whitcroft 37424d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 37434d001e4dSAndy Whitcroft # this is not this patch's fault. 37444d001e4dSAndy Whitcroft if (!defined($stat_real) || 37454d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 37464d001e4dSAndy Whitcroft $check = 0; 37474d001e4dSAndy Whitcroft } 37484d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 37494d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 37504d001e4dSAndy Whitcroft } 37514d001e4dSAndy Whitcroft 37529bd49efeSAndy 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"; 37534d001e4dSAndy Whitcroft 37549f5af480SJoe Perches if ($check && $s ne '' && 37559f5af480SJoe Perches (($sindent % 8) != 0 || 37569f5af480SJoe Perches ($sindent < $indent) || 3757f6950a73SJoe Perches ($sindent == $indent && 3758f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 37599f5af480SJoe Perches ($sindent > $indent + 8))) { 3760000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3761000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 37624d001e4dSAndy Whitcroft } 37634d001e4dSAndy Whitcroft } 37644d001e4dSAndy Whitcroft 37656c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 37666c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 37671f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 37681f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 37696c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3770c2fdda0dSAndy Whitcroft if ($dbg_values) { 3771c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3772cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3773cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 37741f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3775c2fdda0dSAndy Whitcroft } 37766c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 37776c72ffaaSAndy Whitcroft 377800df344fSAndy Whitcroft#ignore lines not being added 37793705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 378000df344fSAndy Whitcroft 378111ca40a0SJoe Perches# check for dereferences that span multiple lines 378211ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 378311ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 378411ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 378511ca40a0SJoe Perches my $ref = $1; 378611ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 378711ca40a0SJoe Perches $ref .= $1; 378811ca40a0SJoe Perches $ref =~ s/\s//g; 378911ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 379011ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 379111ca40a0SJoe Perches } 379211ca40a0SJoe Perches 3793a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3794c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3795a1ce18e4SJoe Perches my $type = $1; 3796a1ce18e4SJoe Perches my $var = $2; 3797207a8e84SJoe Perches $var = "" if (!defined $var); 3798207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3799a1ce18e4SJoe Perches my $sign = $1; 3800a1ce18e4SJoe Perches my $pointer = $2; 3801a1ce18e4SJoe Perches 3802a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3803a1ce18e4SJoe Perches 3804a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3805a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3806a1ce18e4SJoe Perches $fix) { 3807a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3808207a8e84SJoe Perches my $comp_pointer = $pointer; 3809207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3810207a8e84SJoe Perches $decl .= $comp_pointer; 3811207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3812207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3813a1ce18e4SJoe Perches } 3814a1ce18e4SJoe Perches } 3815a1ce18e4SJoe Perches } 3816a1ce18e4SJoe Perches 3817653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 38187429c690SAndy Whitcroft if ($dbg_type) { 38197429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3820000d1cc1SJoe Perches ERROR("TEST_TYPE", 3821000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 38227429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3823000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3824000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 38257429c690SAndy Whitcroft } 3826653d4876SAndy Whitcroft next; 3827653d4876SAndy Whitcroft } 3828a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3829a1ef277eSAndy Whitcroft if ($dbg_attr) { 38309360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3831000d1cc1SJoe Perches ERROR("TEST_ATTR", 3832000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 38339360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3834000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3835000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3836a1ef277eSAndy Whitcroft } 3837a1ef277eSAndy Whitcroft next; 3838a1ef277eSAndy Whitcroft } 3839653d4876SAndy Whitcroft 3840f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 384199423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 384299423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3843d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3844d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3845f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3846f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3847f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3848d752fcc8SJoe Perches my $fixedline = $prevrawline; 3849d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3850f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3851d752fcc8SJoe Perches $fixedline = $line; 38528d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3853f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3854d752fcc8SJoe Perches } 3855f0a594c1SAndy Whitcroft } 3856f0a594c1SAndy Whitcroft 385700df344fSAndy Whitcroft# 385800df344fSAndy Whitcroft# Checks which are anchored on the added line. 385900df344fSAndy Whitcroft# 386000df344fSAndy Whitcroft 3861653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3862c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3863653d4876SAndy Whitcroft my $path = $1; 3864653d4876SAndy Whitcroft if ($path =~ m{//}) { 3865000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3866495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3867495e9d84SJoe Perches } 3868495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3869495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3870495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3871653d4876SAndy Whitcroft } 3872653d4876SAndy Whitcroft } 3873653d4876SAndy Whitcroft 387400df344fSAndy Whitcroft# no C99 // comments 387500df344fSAndy Whitcroft if ($line =~ m{//}) { 38763705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 38773705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 38783705ce5bSJoe Perches $fix) { 3879194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 38803705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 38813705ce5bSJoe Perches my $comment = trim($1); 3882194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 38833705ce5bSJoe Perches } 38843705ce5bSJoe Perches } 388500df344fSAndy Whitcroft } 388600df344fSAndy Whitcroft # Remove C99 comments. 38870a920b5bSAndy Whitcroft $line =~ s@//.*@@; 38886c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 38890a920b5bSAndy Whitcroft 38902b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 38912b474a1aSAndy Whitcroft# the whole statement. 38922b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 38932b474a1aSAndy Whitcroft if (defined $realline_next && 38942b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 38952b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 38962b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 38972b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 38983cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 38993cbf62dfSAndy Whitcroft # a prefix: 39003cbf62dfSAndy Whitcroft # XXX(foo); 39013cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3902653d4876SAndy Whitcroft my $name = $1; 390387a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 39043cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 39053cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 39063cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 39073cbf62dfSAndy Whitcroft 39083cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 39092b474a1aSAndy Whitcroft \n.}\s*$| 391048012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 391148012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 391248012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 39132b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 39142b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 391548012058SAndy Whitcroft )/x) { 39162b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 39172b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 39182b474a1aSAndy Whitcroft } else { 39192b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 39200a920b5bSAndy Whitcroft } 39210a920b5bSAndy Whitcroft } 39222b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 39232b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 39242b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 39252b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 39262b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 39272b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 39282b474a1aSAndy Whitcroft } 39292b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 39302b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3931000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3932000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 39332b474a1aSAndy Whitcroft } 39340a920b5bSAndy Whitcroft 39355150bda4SJoe Eloff# check for global initialisers. 39366d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3937d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 39386d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3939d5e616fcSJoe Perches $fix) { 39406d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3941d5e616fcSJoe Perches } 3942f0a594c1SAndy Whitcroft } 39430a920b5bSAndy Whitcroft# check for static initialisers. 39446d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3945d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 39466d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3947d5e616fcSJoe Perches $herecurr) && 3948d5e616fcSJoe Perches $fix) { 39496d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3950d5e616fcSJoe Perches } 39510a920b5bSAndy Whitcroft } 39520a920b5bSAndy Whitcroft 39531813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 39541813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 39551813087dSJoe Perches my $tmp = trim($1); 39561813087dSJoe Perches WARN("MISORDERED_TYPE", 39571813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 39581813087dSJoe Perches } 39591813087dSJoe Perches 3960809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 3961809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 3962809e082eSJoe Perches my $type = trim($1); 3963809e082eSJoe Perches next if ($type !~ /\bint\b/); 3964809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 3965809e082eSJoe Perches my $new_type = $type; 3966809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 3967809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 3968809e082eSJoe Perches $new_type =~ s/^const\s+//; 3969809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 3970809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 3971809e082eSJoe Perches $new_type =~ s/\s+/ /g; 3972809e082eSJoe Perches $new_type = trim($new_type); 3973809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 3974809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 3975809e082eSJoe Perches $fix) { 3976809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 3977809e082eSJoe Perches } 3978809e082eSJoe Perches } 3979809e082eSJoe Perches 3980cb710ecaSJoe Perches# check for static const char * arrays. 3981cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3982000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3983000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3984cb710ecaSJoe Perches $herecurr); 3985cb710ecaSJoe Perches } 3986cb710ecaSJoe Perches 398777b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 398877b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 398977b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 399077b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 399177b8c0a8SJoe Perches $fix) { 399277b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 399377b8c0a8SJoe Perches } 399477b8c0a8SJoe Perches } 399577b8c0a8SJoe Perches 3996cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3997cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3998000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3999000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4000cb710ecaSJoe Perches $herecurr); 4001cb710ecaSJoe Perches } 4002cb710ecaSJoe Perches 4003ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4004ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4005ab7e23f3SJoe Perches my $found = $1; 4006ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4007ab7e23f3SJoe Perches WARN("CONST_CONST", 4008ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4009ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4010ab7e23f3SJoe Perches WARN("CONST_CONST", 4011ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4012ab7e23f3SJoe Perches } 4013ab7e23f3SJoe Perches } 4014ab7e23f3SJoe Perches 40159b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 40169b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 40179b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 40189b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 40199b0fa60dSJoe Perches $herecurr); 40209b0fa60dSJoe Perches } 40219b0fa60dSJoe Perches 4022b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4023b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4024b598b670SJoe Perches my $array = $1; 4025b598b670SJoe 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*\))@) { 4026b598b670SJoe Perches my $array_div = $1; 4027b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4028b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4029b598b670SJoe Perches $fix) { 4030b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4031b598b670SJoe Perches } 4032b598b670SJoe Perches } 4033b598b670SJoe Perches } 4034b598b670SJoe Perches 4035b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 4036b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 4037b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4038b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4039b36190c5SJoe Perches $fix) { 4040194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4041b36190c5SJoe Perches } 4042b36190c5SJoe Perches } 4043b36190c5SJoe Perches 4044653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4045653d4876SAndy Whitcroft# make sense. 4046653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 40478054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4048c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 40498ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 405046d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4051000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4052000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 40530a920b5bSAndy Whitcroft } 40540a920b5bSAndy Whitcroft 40550a920b5bSAndy Whitcroft# * goes on variable not on type 405665863862SAndy Whitcroft # (char*[ const]) 4057bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4058bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 40593705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4060d8aaf121SAndy Whitcroft 406165863862SAndy Whitcroft # Should start with a space. 406265863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 406365863862SAndy Whitcroft # Should not end with a space. 406465863862SAndy Whitcroft $to =~ s/\s+$//; 406565863862SAndy Whitcroft # '*'s should not have spaces between. 4066f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 406765863862SAndy Whitcroft } 4068d8aaf121SAndy Whitcroft 40693705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 407065863862SAndy Whitcroft if ($from ne $to) { 40713705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 40723705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 40733705ce5bSJoe Perches $fix) { 40743705ce5bSJoe Perches my $sub_from = $ident; 40753705ce5bSJoe Perches my $sub_to = $ident; 40763705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4077194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40783705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 40793705ce5bSJoe Perches } 408065863862SAndy Whitcroft } 4081bfcb2cc7SAndy Whitcroft } 4082bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4083bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 40843705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4085d8aaf121SAndy Whitcroft 408665863862SAndy Whitcroft # Should start with a space. 408765863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 408865863862SAndy Whitcroft # Should not end with a space. 408965863862SAndy Whitcroft $to =~ s/\s+$//; 409065863862SAndy Whitcroft # '*'s should not have spaces between. 4091f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 409265863862SAndy Whitcroft } 409365863862SAndy Whitcroft # Modifiers should have spaces. 409465863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 409565863862SAndy Whitcroft 40963705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4097667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 40983705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 40993705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 41003705ce5bSJoe Perches $fix) { 41013705ce5bSJoe Perches 41023705ce5bSJoe Perches my $sub_from = $match; 41033705ce5bSJoe Perches my $sub_to = $match; 41043705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4105194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41063705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 41073705ce5bSJoe Perches } 410865863862SAndy Whitcroft } 41090a920b5bSAndy Whitcroft } 41100a920b5bSAndy Whitcroft 41119d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 41129d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 41130675a8fbSJean Delvare my $msg_level = \&WARN; 41140675a8fbSJean Delvare $msg_level = \&CHK if ($file); 41150675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 41169d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 41179d3e3c70SJoe Perches } 41180a920b5bSAndy Whitcroft 41199d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 41208905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4121000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4122000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 41238905a67cSAndy Whitcroft } 41248905a67cSAndy Whitcroft 412517441227SJoe Perches# check for uses of printk_ratelimit 412617441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4127000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4128000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 412917441227SJoe Perches } 413017441227SJoe Perches 4131eeef5733SJoe Perches# printk should use KERN_* levels 4132eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4133000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4134eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 413500df344fSAndy Whitcroft } 41360a920b5bSAndy Whitcroft 4137243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4138243f3803SJoe Perches my $orig = $1; 4139243f3803SJoe Perches my $level = lc($orig); 4140243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 41418f26b837SJoe Perches my $level2 = $level; 41428f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4143243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4144daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4145243f3803SJoe Perches } 4146243f3803SJoe Perches 4147dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4148dc139313SJoe Perches my $orig = $1; 4149dc139313SJoe Perches my $level = lc($orig); 4150dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4151dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4152dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4153dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4154dc139313SJoe Perches } 4155dc139313SJoe Perches 415691c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 415791c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 415891c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 415991c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 416091c9afafSAndy Lutomirski WARN("ENOSYS", 416191c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 416291c9afafSAndy Lutomirski } 416391c9afafSAndy Lutomirski 4164653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4165653d4876SAndy Whitcroft# or if closed on same line 41665b57980dSJoe Perches if ($perl_version_ok && 41672d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 41682d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 41692d453e3bSJoe Perches $sline !~ /}/) { 41708d182478SJoe Perches if (ERROR("OPEN_BRACE", 41712d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 41728d182478SJoe Perches $fix) { 41738d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 41748d182478SJoe Perches my $fixed_line = $rawline; 41758d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 41768d182478SJoe Perches my $line1 = $1; 41778d182478SJoe Perches my $line2 = $2; 41788d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 41798d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 41808d182478SJoe Perches if ($line2 !~ /^\s*$/) { 41818d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 41828d182478SJoe Perches } 41838d182478SJoe Perches } 41840a920b5bSAndy Whitcroft } 4185653d4876SAndy Whitcroft 41868905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 41878905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 41888905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 41898d182478SJoe Perches if (ERROR("OPEN_BRACE", 41908d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 41918d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 41928d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 41938d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 41948d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 41958d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 41968d182478SJoe Perches $fixedline = $rawline; 41978d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 41988d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 41998d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42008d182478SJoe Perches } 42018d182478SJoe Perches } 42028905a67cSAndy Whitcroft } 42038905a67cSAndy Whitcroft 42040c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 42053705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 42063705ce5bSJoe Perches if (WARN("SPACING", 42073705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 42083705ce5bSJoe Perches $fix) { 4209194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42103705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 42113705ce5bSJoe Perches } 42120c73b4ebSAndy Whitcroft } 42130c73b4ebSAndy Whitcroft 421431070b5dSJoe Perches# Function pointer declarations 421531070b5dSJoe Perches# check spacing between type, funcptr, and args 421631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 421791f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 421831070b5dSJoe Perches my $declare = $1; 421931070b5dSJoe Perches my $pre_pointer_space = $2; 422031070b5dSJoe Perches my $post_pointer_space = $3; 422131070b5dSJoe Perches my $funcname = $4; 422231070b5dSJoe Perches my $post_funcname_space = $5; 422331070b5dSJoe Perches my $pre_args_space = $6; 422431070b5dSJoe Perches 422591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 422691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 422791f72e9cSJoe Perches# don't need a space so don't warn for those. 422891f72e9cSJoe Perches my $post_declare_space = ""; 422991f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 423091f72e9cSJoe Perches $post_declare_space = $1; 423191f72e9cSJoe Perches $declare = rtrim($declare); 423291f72e9cSJoe Perches } 423391f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 423431070b5dSJoe Perches WARN("SPACING", 423531070b5dSJoe Perches "missing space after return type\n" . $herecurr); 423691f72e9cSJoe Perches $post_declare_space = " "; 423731070b5dSJoe Perches } 423831070b5dSJoe Perches 423931070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 424091f72e9cSJoe Perches# This test is not currently implemented because these declarations are 424191f72e9cSJoe Perches# equivalent to 424291f72e9cSJoe Perches# int foo(int bar, ...) 424391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 424491f72e9cSJoe Perches# 424591f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 424691f72e9cSJoe Perches# WARN("SPACING", 424791f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 424891f72e9cSJoe Perches# } 424931070b5dSJoe Perches 425031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 425131070b5dSJoe Perches if (defined $pre_pointer_space && 425231070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 425331070b5dSJoe Perches WARN("SPACING", 425431070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 425531070b5dSJoe Perches } 425631070b5dSJoe Perches 425731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 425831070b5dSJoe Perches if (defined $post_pointer_space && 425931070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 426031070b5dSJoe Perches WARN("SPACING", 426131070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 426231070b5dSJoe Perches } 426331070b5dSJoe Perches 426431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 426531070b5dSJoe Perches if (defined $post_funcname_space && 426631070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 426731070b5dSJoe Perches WARN("SPACING", 426831070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 426931070b5dSJoe Perches } 427031070b5dSJoe Perches 427131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 427231070b5dSJoe Perches if (defined $pre_args_space && 427331070b5dSJoe Perches $pre_args_space =~ /^\s/) { 427431070b5dSJoe Perches WARN("SPACING", 427531070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 427631070b5dSJoe Perches } 427731070b5dSJoe Perches 427831070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4279194f66fcSJoe Perches $fixed[$fixlinenr] =~ 428091f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 428131070b5dSJoe Perches } 428231070b5dSJoe Perches } 428331070b5dSJoe Perches 42848d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 42858d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4286fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4287fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 42888d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 42898d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 42908d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4291fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 429238dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 42933705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 42943705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 42953705ce5bSJoe Perches $fix) { 4296194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42973705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 42983705ce5bSJoe Perches } 42998d31cfceSAndy Whitcroft } 43008d31cfceSAndy Whitcroft } 43018d31cfceSAndy Whitcroft 4302f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 43036c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4304c2fdda0dSAndy Whitcroft my $name = $1; 4305773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4306773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4307c2fdda0dSAndy Whitcroft 4308c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4309773647a0SAndy Whitcroft if ($name =~ /^(?: 4310773647a0SAndy Whitcroft if|for|while|switch|return|case| 4311773647a0SAndy Whitcroft volatile|__volatile__| 4312773647a0SAndy Whitcroft __attribute__|format|__extension__| 4313773647a0SAndy Whitcroft asm|__asm__)$/x) 4314773647a0SAndy Whitcroft { 4315c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4316c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4317c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4318c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4319773647a0SAndy Whitcroft 4320773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4321c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4322c2fdda0dSAndy Whitcroft 4323c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4324c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4325773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4326c2fdda0dSAndy Whitcroft 4327c2fdda0dSAndy Whitcroft } else { 43283705ce5bSJoe Perches if (WARN("SPACING", 43293705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 43303705ce5bSJoe Perches $fix) { 4331194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43323705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 43333705ce5bSJoe Perches } 4334f0a594c1SAndy Whitcroft } 43356c72ffaaSAndy Whitcroft } 43369a4cad4eSEric Nelson 4337653d4876SAndy Whitcroft# Check operator spacing. 43380a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 43393705ce5bSJoe Perches my $fixed_line = ""; 43403705ce5bSJoe Perches my $line_fixed = 0; 43413705ce5bSJoe Perches 43429c0ca6f9SAndy Whitcroft my $ops = qr{ 43439c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 43449c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 43459c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 43461f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 434784731623SJoe Perches \?:|\?|: 43489c0ca6f9SAndy Whitcroft }x; 4349cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 43503705ce5bSJoe Perches 43513705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 43523705ce5bSJoe Perches## foreach my $el (@elements) { 43533705ce5bSJoe Perches## print("el: <$el>\n"); 43543705ce5bSJoe Perches## } 43553705ce5bSJoe Perches 43563705ce5bSJoe Perches my @fix_elements = (); 435700df344fSAndy Whitcroft my $off = 0; 43586c72ffaaSAndy Whitcroft 43593705ce5bSJoe Perches foreach my $el (@elements) { 43603705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 43613705ce5bSJoe Perches $off += length($el); 43623705ce5bSJoe Perches } 43633705ce5bSJoe Perches 43643705ce5bSJoe Perches $off = 0; 43653705ce5bSJoe Perches 43666c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4367b34c648bSJoe Perches my $last_after = -1; 43686c72ffaaSAndy Whitcroft 43690a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 43703705ce5bSJoe Perches 43713705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 43723705ce5bSJoe Perches 43733705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 43743705ce5bSJoe Perches 43754a0df2efSAndy Whitcroft $off += length($elements[$n]); 43764a0df2efSAndy Whitcroft 437725985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4378773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4379773647a0SAndy Whitcroft my $cc = ''; 4380773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4381773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4382773647a0SAndy Whitcroft } 4383773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4384773647a0SAndy Whitcroft 43854a0df2efSAndy Whitcroft my $a = ''; 43864a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 43874a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4388cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 43894a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 43904a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4391773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 43924a0df2efSAndy Whitcroft 43930a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 43944a0df2efSAndy Whitcroft 43954a0df2efSAndy Whitcroft my $c = ''; 43960a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 43974a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 43984a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4399cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 44004a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 44014a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 44028b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 44034a0df2efSAndy Whitcroft } else { 44044a0df2efSAndy Whitcroft $c = 'E'; 44050a920b5bSAndy Whitcroft } 44060a920b5bSAndy Whitcroft 44074a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 44084a0df2efSAndy Whitcroft 44094a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 44104a0df2efSAndy Whitcroft 44116c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4412de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 44130a920b5bSAndy Whitcroft 441474048ed8SAndy Whitcroft # Pull out the value of this operator. 44156c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 44160a920b5bSAndy Whitcroft 44171f65f947SAndy Whitcroft # Get the full operator variant. 44181f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 44191f65f947SAndy Whitcroft 442013214adfSAndy Whitcroft # Ignore operators passed as parameters. 442113214adfSAndy Whitcroft if ($op_type ne 'V' && 4422d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 442313214adfSAndy Whitcroft 4424cf655043SAndy Whitcroft# # Ignore comments 4425cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 442613214adfSAndy Whitcroft 4427d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 442813214adfSAndy Whitcroft } elsif ($op eq ';') { 4429cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4430cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 44313705ce5bSJoe Perches if (ERROR("SPACING", 44323705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4433b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 44343705ce5bSJoe Perches $line_fixed = 1; 44353705ce5bSJoe Perches } 4436d8aaf121SAndy Whitcroft } 4437d8aaf121SAndy Whitcroft 4438d8aaf121SAndy Whitcroft # // is a comment 4439d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 44400a920b5bSAndy Whitcroft 4441b00e4814SJoe Perches # : when part of a bitfield 4442b00e4814SJoe Perches } elsif ($opv eq ':B') { 4443b00e4814SJoe Perches # skip the bitfield test for now 4444b00e4814SJoe Perches 44451f65f947SAndy Whitcroft # No spaces for: 44461f65f947SAndy Whitcroft # -> 4447b00e4814SJoe Perches } elsif ($op eq '->') { 44484a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 44493705ce5bSJoe Perches if (ERROR("SPACING", 44503705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4451b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44523705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 44533705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 44543705ce5bSJoe Perches } 4455b34c648bSJoe Perches $line_fixed = 1; 44563705ce5bSJoe Perches } 44570a920b5bSAndy Whitcroft } 44580a920b5bSAndy Whitcroft 44592381097bSJoe Perches # , must not have a space before and must have a space on the right. 44600a920b5bSAndy Whitcroft } elsif ($op eq ',') { 44612381097bSJoe Perches my $rtrim_before = 0; 44622381097bSJoe Perches my $space_after = 0; 44632381097bSJoe Perches if ($ctx =~ /Wx./) { 44642381097bSJoe Perches if (ERROR("SPACING", 44652381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 44662381097bSJoe Perches $line_fixed = 1; 44672381097bSJoe Perches $rtrim_before = 1; 44682381097bSJoe Perches } 44692381097bSJoe Perches } 4470cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 44713705ce5bSJoe Perches if (ERROR("SPACING", 44723705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 44733705ce5bSJoe Perches $line_fixed = 1; 4474b34c648bSJoe Perches $last_after = $n; 44752381097bSJoe Perches $space_after = 1; 44762381097bSJoe Perches } 44772381097bSJoe Perches } 44782381097bSJoe Perches if ($rtrim_before || $space_after) { 44792381097bSJoe Perches if ($rtrim_before) { 44802381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44812381097bSJoe Perches } else { 44822381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 44832381097bSJoe Perches } 44842381097bSJoe Perches if ($space_after) { 44852381097bSJoe Perches $good .= " "; 44863705ce5bSJoe Perches } 44870a920b5bSAndy Whitcroft } 44880a920b5bSAndy Whitcroft 44899c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 449074048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 44919c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 44929c0ca6f9SAndy Whitcroft 44939c0ca6f9SAndy Whitcroft # unary operators should have a space before and 44949c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 44959c0ca6f9SAndy Whitcroft # unary operator, or a cast 44969c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 449774048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 44980d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4499cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 45003705ce5bSJoe Perches if (ERROR("SPACING", 45013705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4502b34c648bSJoe Perches if ($n != $last_after + 2) { 4503b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 45043705ce5bSJoe Perches $line_fixed = 1; 45053705ce5bSJoe Perches } 45060a920b5bSAndy Whitcroft } 4507b34c648bSJoe Perches } 4508a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4509171ae1a4SAndy Whitcroft # A unary '*' may be const 4510171ae1a4SAndy Whitcroft 4511171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 45123705ce5bSJoe Perches if (ERROR("SPACING", 45133705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4514b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 45153705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45163705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 45173705ce5bSJoe Perches } 4518b34c648bSJoe Perches $line_fixed = 1; 45193705ce5bSJoe Perches } 45200a920b5bSAndy Whitcroft } 45210a920b5bSAndy Whitcroft 45220a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 45230a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4524773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 45253705ce5bSJoe Perches if (ERROR("SPACING", 45263705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4527b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 45283705ce5bSJoe Perches $line_fixed = 1; 45293705ce5bSJoe Perches } 45300a920b5bSAndy Whitcroft } 4531773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4532773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 45333705ce5bSJoe Perches if (ERROR("SPACING", 45343705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4535b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45363705ce5bSJoe Perches $line_fixed = 1; 45373705ce5bSJoe Perches } 4538653d4876SAndy Whitcroft } 4539773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 45403705ce5bSJoe Perches if (ERROR("SPACING", 45413705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4542b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 45433705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45443705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4545773647a0SAndy Whitcroft } 4546b34c648bSJoe Perches $line_fixed = 1; 45473705ce5bSJoe Perches } 45483705ce5bSJoe Perches } 45490a920b5bSAndy Whitcroft 45500a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 45519c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 45529c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 45539c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4554c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4555c2fdda0dSAndy Whitcroft $op eq '%') 45560a920b5bSAndy Whitcroft { 4557d2e025f3SJoe Perches if ($check) { 4558d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4559d2e025f3SJoe Perches if (CHK("SPACING", 4560d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4561d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4562d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4563d2e025f3SJoe Perches $line_fixed = 1; 4564d2e025f3SJoe Perches } 4565d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4566d2e025f3SJoe Perches if (CHK("SPACING", 4567d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4568d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4569d2e025f3SJoe Perches $line_fixed = 1; 4570d2e025f3SJoe Perches } 4571d2e025f3SJoe Perches } 4572d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 45733705ce5bSJoe Perches if (ERROR("SPACING", 45743705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4575b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4576b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4577b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4578b34c648bSJoe Perches } 45793705ce5bSJoe Perches $line_fixed = 1; 45803705ce5bSJoe Perches } 45810a920b5bSAndy Whitcroft } 45820a920b5bSAndy Whitcroft 45831f65f947SAndy Whitcroft # A colon needs no spaces before when it is 45841f65f947SAndy Whitcroft # terminating a case value or a label. 45851f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 45861f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 45873705ce5bSJoe Perches if (ERROR("SPACING", 45883705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4589b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45903705ce5bSJoe Perches $line_fixed = 1; 45913705ce5bSJoe Perches } 45921f65f947SAndy Whitcroft } 45931f65f947SAndy Whitcroft 45940a920b5bSAndy Whitcroft # All the others need spaces both sides. 4595cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 45961f65f947SAndy Whitcroft my $ok = 0; 45971f65f947SAndy Whitcroft 459822f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 45991f65f947SAndy Whitcroft if (($op eq '<' && 46001f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 46011f65f947SAndy Whitcroft ($op eq '>' && 46021f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 46031f65f947SAndy Whitcroft { 46041f65f947SAndy Whitcroft $ok = 1; 46051f65f947SAndy Whitcroft } 46061f65f947SAndy Whitcroft 4607e0df7e1fSJoe Perches # for asm volatile statements 4608e0df7e1fSJoe Perches # ignore a colon with another 4609e0df7e1fSJoe Perches # colon immediately before or after 4610e0df7e1fSJoe Perches if (($op eq ':') && 4611e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4612e0df7e1fSJoe Perches $ok = 1; 4613e0df7e1fSJoe Perches } 4614e0df7e1fSJoe Perches 461584731623SJoe Perches # messages are ERROR, but ?: are CHK 46161f65f947SAndy Whitcroft if ($ok == 0) { 46170675a8fbSJean Delvare my $msg_level = \&ERROR; 46180675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 461984731623SJoe Perches 46200675a8fbSJean Delvare if (&{$msg_level}("SPACING", 46213705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4622b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4623b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4624b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4625b34c648bSJoe Perches } 46263705ce5bSJoe Perches $line_fixed = 1; 46273705ce5bSJoe Perches } 46280a920b5bSAndy Whitcroft } 462922f2a2efSAndy Whitcroft } 46304a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 46313705ce5bSJoe Perches 46323705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 46333705ce5bSJoe Perches 46343705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 46350a920b5bSAndy Whitcroft } 46363705ce5bSJoe Perches 46373705ce5bSJoe Perches if (($#elements % 2) == 0) { 46383705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 46393705ce5bSJoe Perches } 46403705ce5bSJoe Perches 4641194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4642194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 46433705ce5bSJoe Perches } 46443705ce5bSJoe Perches 46453705ce5bSJoe Perches 46460a920b5bSAndy Whitcroft } 46470a920b5bSAndy Whitcroft 4648786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4649d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4650786b6326SJoe Perches if (WARN("SPACING", 4651786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4652786b6326SJoe Perches $fix) { 4653194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4654786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4655786b6326SJoe Perches } 4656786b6326SJoe Perches } 4657786b6326SJoe Perches 4658f0a594c1SAndy Whitcroft# check for multiple assignments 4659f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4660000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4661000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4662f0a594c1SAndy Whitcroft } 4663f0a594c1SAndy Whitcroft 466422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 466522f2a2efSAndy Whitcroft## # continuation. 466622f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 466722f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 466822f2a2efSAndy Whitcroft## 466922f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 467022f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 467122f2a2efSAndy Whitcroft## my $ln = $line; 467222f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 467322f2a2efSAndy Whitcroft## } 467422f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4675000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4676000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 467722f2a2efSAndy Whitcroft## } 467822f2a2efSAndy Whitcroft## } 4679f0a594c1SAndy Whitcroft 46800a920b5bSAndy Whitcroft#need space before brace following if, while, etc 46816b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 46826ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 46833705ce5bSJoe Perches if (ERROR("SPACING", 46843705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 46853705ce5bSJoe Perches $fix) { 46866ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 46873705ce5bSJoe Perches } 4688de7d4f0eSAndy Whitcroft } 4689de7d4f0eSAndy Whitcroft 4690c4a62ef9SJoe Perches## # check for blank lines before declarations 4691c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4692c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4693c4a62ef9SJoe Perches## WARN("SPACING", 4694c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4695c4a62ef9SJoe Perches## } 4696c4a62ef9SJoe Perches## 4697c4a62ef9SJoe Perches 4698de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4699de7d4f0eSAndy Whitcroft# on the line 470094fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 4701d5e616fcSJoe Perches if (ERROR("SPACING", 4702d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4703d5e616fcSJoe Perches $fix) { 4704194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4705d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4706d5e616fcSJoe Perches } 47070a920b5bSAndy Whitcroft } 47080a920b5bSAndy Whitcroft 470922f2a2efSAndy Whitcroft# check spacing on square brackets 471022f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 47113705ce5bSJoe Perches if (ERROR("SPACING", 47123705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 47133705ce5bSJoe Perches $fix) { 4714194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47153705ce5bSJoe Perches s/\[\s+/\[/; 47163705ce5bSJoe Perches } 471722f2a2efSAndy Whitcroft } 471822f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 47193705ce5bSJoe Perches if (ERROR("SPACING", 47203705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 47213705ce5bSJoe Perches $fix) { 4722194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47233705ce5bSJoe Perches s/\s+\]/\]/; 47243705ce5bSJoe Perches } 472522f2a2efSAndy Whitcroft } 472622f2a2efSAndy Whitcroft 4727c45dcabdSAndy Whitcroft# check spacing on parentheses 47289c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 47299c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 47303705ce5bSJoe Perches if (ERROR("SPACING", 47313705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 47323705ce5bSJoe Perches $fix) { 4733194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47343705ce5bSJoe Perches s/\(\s+/\(/; 47353705ce5bSJoe Perches } 473622f2a2efSAndy Whitcroft } 473713214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4738c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4739c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 47403705ce5bSJoe Perches if (ERROR("SPACING", 47413705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 47423705ce5bSJoe Perches $fix) { 4743194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47443705ce5bSJoe Perches s/\s+\)/\)/; 47453705ce5bSJoe Perches } 474622f2a2efSAndy Whitcroft } 474722f2a2efSAndy Whitcroft 4748e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4749e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4750e2826fd0SJoe Perches 4751e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4752ea4acbb1SJoe Perches my $var = $1; 4753ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4754ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4755ea4acbb1SJoe Perches $fix) { 4756ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4757ea4acbb1SJoe Perches } 4758ea4acbb1SJoe Perches } 4759ea4acbb1SJoe Perches 4760ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4761ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4762ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4763ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4764ea4acbb1SJoe Perches my $var = $2; 4765ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4766ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4767ea4acbb1SJoe Perches $fix) { 4768ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4769ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4770ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4771ea4acbb1SJoe Perches } 4772e2826fd0SJoe Perches } 4773e2826fd0SJoe Perches 477463b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4775a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4776a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 47775b57980dSJoe Perches $perl_version_ok && defined($stat) && 477863b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 477963b7c73eSJoe Perches my $if_stat = $1; 478063b7c73eSJoe Perches my $test = substr($2, 1, -1); 478163b7c73eSJoe Perches my $herectx; 478263b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 478363b7c73eSJoe Perches my $match = $1; 478463b7c73eSJoe Perches # avoid parentheses around potential macro args 478563b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 478663b7c73eSJoe Perches if (!defined($herectx)) { 478763b7c73eSJoe Perches $herectx = $here . "\n"; 478863b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 478963b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 479063b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 479163b7c73eSJoe Perches $herectx .= $rl . "\n"; 479263b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 479363b7c73eSJoe Perches } 479463b7c73eSJoe Perches } 479563b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 479663b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 479763b7c73eSJoe Perches } 479863b7c73eSJoe Perches } 479963b7c73eSJoe Perches 48000a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 48014a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 48020a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 48033705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 48043705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 48053705ce5bSJoe Perches $fix) { 4806194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48073705ce5bSJoe Perches s/^(.)\s+/$1/; 48083705ce5bSJoe Perches } 48090a920b5bSAndy Whitcroft } 48100a920b5bSAndy Whitcroft 48115b9553abSJoe Perches# return is not a function 4812507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4813c45dcabdSAndy Whitcroft my $spacing = $1; 48145b57980dSJoe Perches if ($perl_version_ok && 48155b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 48165b9553abSJoe Perches my $value = $1; 48175b9553abSJoe Perches $value = deparenthesize($value); 48185b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4819000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4820000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 48215b9553abSJoe Perches } 4822c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4823000d1cc1SJoe Perches ERROR("SPACING", 4824000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4825c45dcabdSAndy Whitcroft } 4826c45dcabdSAndy Whitcroft } 4827507e5141SJoe Perches 4828b43ae21bSJoe Perches# unnecessary return in a void function 4829b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4830b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4831b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4832b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4833b43ae21bSJoe Perches $linenr >= 3 && 4834b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4835b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 48369819cf25SJoe Perches WARN("RETURN_VOID", 4837b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 48389819cf25SJoe Perches } 48399819cf25SJoe Perches 4840189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 48415b57980dSJoe Perches if ($perl_version_ok && 4842189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4843189248d8SJoe Perches my $openparens = $1; 4844189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4845189248d8SJoe Perches my $msg = ""; 4846189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4847189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4848189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4849189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4850189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4851189248d8SJoe Perches } 4852189248d8SJoe Perches } 4853189248d8SJoe Perches 4854c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4855c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4856c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4857c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 48585b57980dSJoe Perches if ($perl_version_ok && 4859c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4860c5595fa2SJoe Perches my $lead = $1; 4861c5595fa2SJoe Perches my $const = $2; 4862c5595fa2SJoe Perches my $comp = $3; 4863c5595fa2SJoe Perches my $to = $4; 4864c5595fa2SJoe Perches my $newcomp = $comp; 4865f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4866c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4867c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4868c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4869c5595fa2SJoe Perches $fix) { 4870c5595fa2SJoe Perches if ($comp eq "<") { 4871c5595fa2SJoe Perches $newcomp = ">"; 4872c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4873c5595fa2SJoe Perches $newcomp = ">="; 4874c5595fa2SJoe Perches } elsif ($comp eq ">") { 4875c5595fa2SJoe Perches $newcomp = "<"; 4876c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4877c5595fa2SJoe Perches $newcomp = "<="; 4878c5595fa2SJoe Perches } 4879c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4880c5595fa2SJoe Perches } 4881c5595fa2SJoe Perches } 4882c5595fa2SJoe Perches 4883f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4884f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 488553a3c448SAndy Whitcroft my $name = $1; 488653a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4887000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4888f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 488953a3c448SAndy Whitcroft } 489053a3c448SAndy Whitcroft } 4891c45dcabdSAndy Whitcroft 48920a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 48934a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 48943705ce5bSJoe Perches if (ERROR("SPACING", 48953705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 48963705ce5bSJoe Perches $fix) { 4897194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48983705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 48993705ce5bSJoe Perches } 49000a920b5bSAndy Whitcroft } 49010a920b5bSAndy Whitcroft 4902f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4903f5fe35ddSAndy Whitcroft# statements after the conditional. 4904170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 49053e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 49063e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 49073e469cdcSAndy Whitcroft if (!defined $stat); 4908170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4909170d3a22SAndy Whitcroft $remain_next, $off_next); 4910170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4911170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4912170d3a22SAndy Whitcroft 4913170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4914170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4915170d3a22SAndy Whitcroft # then count those as offsets. 4916170d3a22SAndy Whitcroft my ($whitespace) = 4917170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4918170d3a22SAndy Whitcroft my $offset = 4919170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4920170d3a22SAndy Whitcroft 4921170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4922170d3a22SAndy Whitcroft $offset} = 1; 4923170d3a22SAndy Whitcroft } 4924170d3a22SAndy Whitcroft } 4925170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4926c11230f4SJoe Perches defined($stat) && defined($cond) && 4927170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4928171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 49298905a67cSAndy Whitcroft 4930b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4931000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4932000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 49338905a67cSAndy Whitcroft } 49348905a67cSAndy Whitcroft 49358905a67cSAndy Whitcroft # Find out what is on the end of the line after the 49368905a67cSAndy Whitcroft # conditional. 4937773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 49388905a67cSAndy Whitcroft $s =~ s/\n.*//g; 493913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 494053210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 494153210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4942773647a0SAndy Whitcroft { 4943bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4944bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4945bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 494642bdf74cSHidetoshi Seto my $stat_real = ''; 4947bb44ad39SAndy Whitcroft 494842bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 494942bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4950bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4951bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4952bb44ad39SAndy Whitcroft } 4953bb44ad39SAndy Whitcroft 4954000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4955000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 49568905a67cSAndy Whitcroft } 49578905a67cSAndy Whitcroft } 49588905a67cSAndy Whitcroft 495913214adfSAndy Whitcroft# Check for bitwise tests written as boolean 496013214adfSAndy Whitcroft if ($line =~ / 496113214adfSAndy Whitcroft (?: 496213214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 496313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 496413214adfSAndy Whitcroft (?:\&\&|\|\|) 496513214adfSAndy Whitcroft | 496613214adfSAndy Whitcroft (?:\&\&|\|\|) 496713214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 496813214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 496913214adfSAndy Whitcroft )/x) 497013214adfSAndy Whitcroft { 4971000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4972000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 497313214adfSAndy Whitcroft } 497413214adfSAndy Whitcroft 49758905a67cSAndy Whitcroft# if and else should not have general statements after it 497613214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 497713214adfSAndy Whitcroft my $s = $1; 497813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 497913214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4980000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4981000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 49820a920b5bSAndy Whitcroft } 498313214adfSAndy Whitcroft } 498439667782SAndy Whitcroft# if should not continue a brace 498539667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4986000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4987048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 498839667782SAndy Whitcroft $herecurr); 498939667782SAndy Whitcroft } 4990a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4991a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4992a1080bf8SAndy Whitcroft $line !~ /\G(?: 49933fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4994a1080bf8SAndy Whitcroft \s*return\s+ 4995a1080bf8SAndy Whitcroft )/xg) 4996a1080bf8SAndy Whitcroft { 4997000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4998000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4999a1080bf8SAndy Whitcroft } 50000a920b5bSAndy Whitcroft 50010a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 50020a920b5bSAndy Whitcroft # indent level to be relevant to each other. 50038b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 50040a920b5bSAndy Whitcroft $previndent == $indent) { 50058b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 50068b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 50078b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 50088b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 50098b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 50108b8856f4SJoe Perches my $fixedline = $prevrawline; 50118b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 50128b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 50138b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50148b8856f4SJoe Perches } 50158b8856f4SJoe Perches $fixedline = $rawline; 50168b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 50178b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50188b8856f4SJoe Perches } 50190a920b5bSAndy Whitcroft } 50200a920b5bSAndy Whitcroft 50218b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5022c2fdda0dSAndy Whitcroft $previndent == $indent) { 5023c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5024c2fdda0dSAndy Whitcroft 5025c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5026c2fdda0dSAndy Whitcroft # conditional. 5027773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5028c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5029c2fdda0dSAndy Whitcroft 5030c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 50318b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 50328b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 50338b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 50348b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 50358b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 50368b8856f4SJoe Perches my $fixedline = $prevrawline; 50378b8856f4SJoe Perches my $trailing = $rawline; 50388b8856f4SJoe Perches $trailing =~ s/^\+//; 50398b8856f4SJoe Perches $trailing = trim($trailing); 50408b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 50418b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50428b8856f4SJoe Perches } 5043c2fdda0dSAndy Whitcroft } 5044c2fdda0dSAndy Whitcroft } 5045c2fdda0dSAndy Whitcroft 504695e2c602SJoe Perches#Specific variable tests 5047323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5048323c1260SJoe Perches my $var = $1; 504995e2c602SJoe Perches 505095e2c602SJoe Perches#CamelCase 5051807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5052be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 505322735ce8SJoe Perches#Ignore Page<foo> variants 5054807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5055d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5056d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5057d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5058f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5059f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 50607e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 50617e781f67SJoe Perches my $word = $1; 50627e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5063d8b07710SJoe Perches if ($check) { 5064d8b07710SJoe Perches seed_camelcase_includes(); 5065d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5066d8b07710SJoe Perches seed_camelcase_file($realfile); 5067d8b07710SJoe Perches $camelcase_file_seeded = 1; 5068d8b07710SJoe Perches } 5069d8b07710SJoe Perches } 50707e781f67SJoe Perches if (!defined $camelcase{$word}) { 50717e781f67SJoe Perches $camelcase{$word} = 1; 5072be79794bSJoe Perches CHK("CAMELCASE", 50737e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 50747e781f67SJoe Perches } 5075323c1260SJoe Perches } 5076323c1260SJoe Perches } 50773445686aSJoe Perches } 50780a920b5bSAndy Whitcroft 50790a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5080d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5081d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5082d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5083d5e616fcSJoe Perches $fix) { 5084194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5085d5e616fcSJoe Perches } 50860a920b5bSAndy Whitcroft } 50870a920b5bSAndy Whitcroft 50880e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 50890e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5090c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5091e09dec48SAndy Whitcroft my $file = "$1.h"; 5092e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5093e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5094e09dec48SAndy Whitcroft $realfile ne $checkfile && 50957840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5096c45dcabdSAndy Whitcroft { 50970e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 50980e212e0aSFabian Frederick if ($asminclude > 0) { 5099e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5100000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5101000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5102e09dec48SAndy Whitcroft } else { 5103000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5104000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5105e09dec48SAndy Whitcroft } 51060a920b5bSAndy Whitcroft } 51070a920b5bSAndy Whitcroft } 51080e212e0aSFabian Frederick } 51090a920b5bSAndy Whitcroft 5110653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5111653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5112cf655043SAndy Whitcroft# in a known good container 5113b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5114b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5115d8aaf121SAndy Whitcroft my $ln = $linenr; 5116d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5117c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5118c45dcabdSAndy Whitcroft my $ctx = ''; 511908a2843eSJoe Perches my $has_flow_statement = 0; 512008a2843eSJoe Perches my $has_arg_concat = 0; 5121c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5122f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5123f74bd194SAndy Whitcroft $ctx = $dstat; 5124c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5125a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5126c45dcabdSAndy Whitcroft 512708a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 512862e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 512908a2843eSJoe Perches 5130f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5131f59b64bfSJoe Perches my $define_args = $1; 5132f59b64bfSJoe Perches my $define_stmt = $dstat; 5133f59b64bfSJoe Perches my @def_args = (); 5134f59b64bfSJoe Perches 5135f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5136f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5137f59b64bfSJoe Perches $define_args =~ s/\s*//g; 51388c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5139f59b64bfSJoe Perches @def_args = split(",", $define_args); 5140f59b64bfSJoe Perches } 5141f59b64bfSJoe Perches 5142292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5143c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5144c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5145c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5146c45dcabdSAndy Whitcroft 5147c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 5148bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 5149bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 51506b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 5151bf30d6edSAndy Whitcroft { 5152c45dcabdSAndy Whitcroft } 5153c45dcabdSAndy Whitcroft 5154e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 515533acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 515633acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5157e45bab8eSAndy Whitcroft { 5158e45bab8eSAndy Whitcroft } 5159e45bab8eSAndy Whitcroft 516042e15293SJoe Perches # Make asm volatile uses seem like a generic function 516142e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 516242e15293SJoe Perches 5163c45dcabdSAndy Whitcroft my $exceptions = qr{ 5164c45dcabdSAndy Whitcroft $Declare| 5165c45dcabdSAndy Whitcroft module_param_named| 5166a0a0a7a9SKees Cook MODULE_PARM_DESC| 5167c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5168c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5169383099fdSAndy Whitcroft __typeof__\(| 517022fd2d3eSStefani Seibold union| 517122fd2d3eSStefani Seibold struct| 5172ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 51736b10df42SVladimir Zapolskiy ^\"|\"$| 51746b10df42SVladimir Zapolskiy ^\[ 5175c45dcabdSAndy Whitcroft }x; 51765eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5177f59b64bfSJoe Perches 5178f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5179f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5180e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5181f59b64bfSJoe Perches 5182f74bd194SAndy Whitcroft if ($dstat ne '' && 5183f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5184f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 51853cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5186356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5187f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5188f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5189e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 519072f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5191f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5192f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5193f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 51944e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5195f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5196c45dcabdSAndy Whitcroft { 5197e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5198e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5199e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5200e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5201f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5202f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5203f74bd194SAndy Whitcroft } else { 5204000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5205388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5206d8aaf121SAndy Whitcroft } 5207f59b64bfSJoe Perches 5208f59b64bfSJoe Perches } 52095207649bSJoe Perches 52105207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 52115207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 52125207649bSJoe Perches my $first = 1; 52135207649bSJoe Perches $define_stmt = ""; 52145207649bSJoe Perches foreach my $l (@stmt_array) { 52155207649bSJoe Perches $l =~ s/\\$//; 52165207649bSJoe Perches if ($first) { 52175207649bSJoe Perches $define_stmt = $l; 52185207649bSJoe Perches $first = 0; 52195207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 52205207649bSJoe Perches $define_stmt .= substr($l, 1); 52215207649bSJoe Perches } 52225207649bSJoe Perches } 52235207649bSJoe Perches $define_stmt =~ s/$;//g; 52245207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 52255207649bSJoe Perches $define_stmt = trim($define_stmt); 52265207649bSJoe Perches 5227f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5228f59b64bfSJoe Perches foreach my $arg (@def_args) { 5229f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 52309192d41aSJoe Perches next if ($arg =~ /^type$/i); 52317fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 52326dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 52337fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 52347fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5235d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5236f59b64bfSJoe Perches if ($use_cnt > 1) { 5237f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5238f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5239f59b64bfSJoe Perches } 52409192d41aSJoe Perches# check if any macro arguments may have other precedence issues 52417fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 52429192d41aSJoe Perches ((defined($1) && $1 ne ',') || 52439192d41aSJoe Perches (defined($2) && $2 ne ','))) { 52449192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 52459192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 52469192d41aSJoe Perches } 52470a920b5bSAndy Whitcroft } 52485023d347SJoe Perches 524908a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 525008a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 525108a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 525208a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5253e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 525408a2843eSJoe Perches 525508a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 525608a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 525708a2843eSJoe Perches } 525808a2843eSJoe Perches 5259481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 52605023d347SJoe Perches 52615023d347SJoe Perches } else { 52625023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5263481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5264481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 52655023d347SJoe Perches $line =~ /^\+.*\\$/) { 52665023d347SJoe Perches WARN("LINE_CONTINUATIONS", 52675023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 52685023d347SJoe Perches } 5269653d4876SAndy Whitcroft } 52700a920b5bSAndy Whitcroft 5271b13edf7fSJoe Perches# do {} while (0) macro tests: 5272b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5273b13edf7fSJoe Perches# macro should not end with a semicolon 52745b57980dSJoe Perches if ($perl_version_ok && 5275b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5276b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5277b13edf7fSJoe Perches my $ln = $linenr; 5278b13edf7fSJoe Perches my $cnt = $realcnt; 5279b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5280b13edf7fSJoe Perches my $ctx = ''; 5281b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5282b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5283b13edf7fSJoe Perches $ctx = $dstat; 5284b13edf7fSJoe Perches 5285b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 52861b36b201SJoe Perches $dstat =~ s/$;/ /g; 5287b13edf7fSJoe Perches 5288b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5289b13edf7fSJoe Perches my $stmts = $2; 5290b13edf7fSJoe Perches my $semis = $3; 5291b13edf7fSJoe Perches 5292b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5293b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5294e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5295b13edf7fSJoe Perches 5296ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5297ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5298b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5299b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5300b13edf7fSJoe Perches } 5301b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5302b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5303b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5304b13edf7fSJoe Perches } 5305f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5306f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5307f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5308e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5309f5ef95b1SJoe Perches 5310f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5311f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5312b13edf7fSJoe Perches } 5313b13edf7fSJoe Perches } 5314b13edf7fSJoe Perches 5315f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 531613214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 531713214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5318cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 531913214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5320cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5321cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5322aad4f614SJoe Perches my @allowed = (); 5323aad4f614SJoe Perches my $allow = 0; 532413214adfSAndy Whitcroft my $seen = 0; 5325773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5326cf655043SAndy Whitcroft my $ln = $linenr - 1; 532713214adfSAndy Whitcroft for my $chunk (@chunks) { 532813214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 532913214adfSAndy Whitcroft 5330773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5331773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5332773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5333773647a0SAndy Whitcroft 5334aad4f614SJoe Perches $allowed[$allow] = 0; 5335773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5336773647a0SAndy Whitcroft 5337773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5338773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5339773647a0SAndy Whitcroft 5340773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5341cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5342cf655043SAndy Whitcroft 5343773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 534413214adfSAndy Whitcroft 534513214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 534613214adfSAndy Whitcroft 5347aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5348cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5349cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5350aad4f614SJoe Perches $allowed[$allow] = 1; 535113214adfSAndy Whitcroft } 535213214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5353cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5354aad4f614SJoe Perches $allowed[$allow] = 1; 535513214adfSAndy Whitcroft } 5356cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5357cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5358aad4f614SJoe Perches $allowed[$allow] = 1; 535913214adfSAndy Whitcroft } 5360aad4f614SJoe Perches $allow++; 536113214adfSAndy Whitcroft } 5362aad4f614SJoe Perches if ($seen) { 5363aad4f614SJoe Perches my $sum_allowed = 0; 5364aad4f614SJoe Perches foreach (@allowed) { 5365aad4f614SJoe Perches $sum_allowed += $_; 5366aad4f614SJoe Perches } 5367aad4f614SJoe Perches if ($sum_allowed == 0) { 5368000d1cc1SJoe Perches WARN("BRACES", 5369000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5370aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5371aad4f614SJoe Perches $seen != $allow) { 5372aad4f614SJoe Perches CHK("BRACES", 5373aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5374aad4f614SJoe Perches } 537513214adfSAndy Whitcroft } 537613214adfSAndy Whitcroft } 537713214adfSAndy Whitcroft } 5378773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 537913214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5380cf655043SAndy Whitcroft my $allowed = 0; 5381f0a594c1SAndy Whitcroft 5382cf655043SAndy Whitcroft # Check the pre-context. 5383cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5384cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5385cf655043SAndy Whitcroft $allowed = 1; 5386f0a594c1SAndy Whitcroft } 5387773647a0SAndy Whitcroft 5388773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5389773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5390773647a0SAndy Whitcroft 5391cf655043SAndy Whitcroft # Check the condition. 5392cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5393773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5394cf655043SAndy Whitcroft if (defined $cond) { 5395773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5396cf655043SAndy Whitcroft } 5397cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5398cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5399cf655043SAndy Whitcroft $allowed = 1; 5400cf655043SAndy Whitcroft } 5401cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5402cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5403cf655043SAndy Whitcroft $allowed = 1; 5404cf655043SAndy Whitcroft } 5405cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5406cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5407cf655043SAndy Whitcroft $allowed = 1; 5408cf655043SAndy Whitcroft } 5409cf655043SAndy Whitcroft # Check the post-context. 5410cf655043SAndy Whitcroft if (defined $chunks[1]) { 5411cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5412cf655043SAndy Whitcroft if (defined $cond) { 5413773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5414cf655043SAndy Whitcroft } 5415cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5416cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5417cf655043SAndy Whitcroft $allowed = 1; 5418cf655043SAndy Whitcroft } 5419cf655043SAndy Whitcroft } 5420cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5421f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5422e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5423cf655043SAndy Whitcroft 5424000d1cc1SJoe Perches WARN("BRACES", 5425000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5426f0a594c1SAndy Whitcroft } 5427f0a594c1SAndy Whitcroft } 5428f0a594c1SAndy Whitcroft 5429e4c5babdSJoe Perches# check for single line unbalanced braces 543095330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 543195330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5432e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5433e4c5babdSJoe Perches } 5434e4c5babdSJoe Perches 54350979ae66SJoe Perches# check for unnecessary blank lines around braces 543677b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5437f8e58219SJoe Perches if (CHK("BRACES", 5438f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5439f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5440f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5441f8e58219SJoe Perches } 54420979ae66SJoe Perches } 544377b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5444f8e58219SJoe Perches if (CHK("BRACES", 5445f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5446f8e58219SJoe Perches $fix) { 5447f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5448f8e58219SJoe Perches } 54490979ae66SJoe Perches } 54500979ae66SJoe Perches 54514a0df2efSAndy Whitcroft# no volatiles please 54526c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 54536c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5454000d1cc1SJoe Perches WARN("VOLATILE", 54558c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 54564a0df2efSAndy Whitcroft } 54574a0df2efSAndy Whitcroft 54585e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 54595e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 54605e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 54615e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 546233acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 54635e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 54645e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 54655e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 54665e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 54675e4f6ba5SJoe Perches $fix && 54685e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 54695e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 54705e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 54715e4f6ba5SJoe Perches my $comma_close = ""; 54725e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 54735e4f6ba5SJoe Perches $comma_close = $1; 54745e4f6ba5SJoe Perches } 54755e4f6ba5SJoe Perches 54765e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 54775e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 54785e4f6ba5SJoe Perches my $fixedline = $prevrawline; 54795e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 54805e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 54815e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 54825e4f6ba5SJoe Perches $fixedline = $rawline; 54835e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 54845e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 54855e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 54865e4f6ba5SJoe Perches } 54875e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 54885e4f6ba5SJoe Perches } 54895e4f6ba5SJoe Perches } 54905e4f6ba5SJoe Perches 54915e4f6ba5SJoe Perches# check for missing a space in a string concatenation 54925e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 54935e4f6ba5SJoe Perches WARN('MISSING_SPACE', 54945e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 54955e4f6ba5SJoe Perches } 54965e4f6ba5SJoe Perches 549777cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5498e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5499e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5500e4b7d309SJoe Perches# function declarations 550177cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 550277cb8546SJoe Perches defined($context_function) && 5503e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5504e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 550577cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5506e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 550777cb8546SJoe Perches } 550877cb8546SJoe Perches 55095e4f6ba5SJoe Perches# check for spaces before a quoted newline 55105e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 55115e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 55125e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 55135e4f6ba5SJoe Perches $fix) { 55145e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 55155e4f6ba5SJoe Perches } 55165e4f6ba5SJoe Perches 55175e4f6ba5SJoe Perches } 55185e4f6ba5SJoe Perches 5519f17dba4fSJoe Perches# concatenated string without spaces between elements 552079682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 552179682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 552279682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 552379682c0cSJoe Perches $fix) { 552479682c0cSJoe Perches while ($line =~ /($String)/g) { 552579682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 552679682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 552779682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 552879682c0cSJoe Perches } 552979682c0cSJoe Perches } 5530f17dba4fSJoe Perches } 5531f17dba4fSJoe Perches 553290ad30e5SJoe Perches# uncoalesced string fragments 553333acb54aSJoe Perches if ($line =~ /$String\s*"/) { 553479682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 553579682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 553679682c0cSJoe Perches $fix) { 553779682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 553879682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 553979682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 554079682c0cSJoe Perches } 554179682c0cSJoe Perches } 554290ad30e5SJoe Perches } 554390ad30e5SJoe Perches 5544522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5545522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5546522b837cSAlexey Dobriyan my $show_Z = 1; 55475e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5548522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 55495e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5550522b837cSAlexey Dobriyan # check for %L 5551522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 55525e4f6ba5SJoe Perches WARN("PRINTF_L", 5553522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5554522b837cSAlexey Dobriyan $show_L = 0; 55555e4f6ba5SJoe Perches } 5556522b837cSAlexey Dobriyan # check for %Z 5557522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5558522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5559522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5560522b837cSAlexey Dobriyan $show_Z = 0; 5561522b837cSAlexey Dobriyan } 5562522b837cSAlexey Dobriyan # check for 0x<decimal> 5563522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5564522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 55656e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 55666e300757SJoe Perches } 55675e4f6ba5SJoe Perches } 55685e4f6ba5SJoe Perches 55695e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 55703f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 55715e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 55725e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 55735e4f6ba5SJoe Perches } 55745e4f6ba5SJoe Perches 557500df344fSAndy Whitcroft# warn about #if 0 5576c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 557760f89010SPrakruthi Deepak Heragu WARN("IF_0", 557860f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 557960f89010SPrakruthi Deepak Heragu } 558060f89010SPrakruthi Deepak Heragu 558160f89010SPrakruthi Deepak Heragu# warn about #if 1 558260f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 558360f89010SPrakruthi Deepak Heragu WARN("IF_1", 558460f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 55854a0df2efSAndy Whitcroft } 55864a0df2efSAndy Whitcroft 558703df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 558803df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5589100425deSJoe Perches my $tested = quotemeta($1); 5590100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5591100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5592100425deSJoe Perches my $func = $1; 5593100425deSJoe Perches if (WARN('NEEDLESS_IF', 5594100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5595100425deSJoe Perches $fix) { 5596100425deSJoe Perches my $do_fix = 1; 5597100425deSJoe Perches my $leading_tabs = ""; 5598100425deSJoe Perches my $new_leading_tabs = ""; 5599100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5600100425deSJoe Perches $leading_tabs = $1; 5601100425deSJoe Perches } else { 5602100425deSJoe Perches $do_fix = 0; 5603100425deSJoe Perches } 5604100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5605100425deSJoe Perches $new_leading_tabs = $1; 5606100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5607100425deSJoe Perches $do_fix = 0; 5608100425deSJoe Perches } 5609100425deSJoe Perches } else { 5610100425deSJoe Perches $do_fix = 0; 5611100425deSJoe Perches } 5612100425deSJoe Perches if ($do_fix) { 5613100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5614100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5615100425deSJoe Perches } 5616100425deSJoe Perches } 56174c432a8fSGreg Kroah-Hartman } 56184c432a8fSGreg Kroah-Hartman } 5619f0a594c1SAndy Whitcroft 5620ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5621ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5622ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5623ebfdc409SJoe Perches (defined $1 || defined $3) && 5624ebfdc409SJoe Perches $linenr > 3) { 5625ebfdc409SJoe Perches my $testval = $2; 5626ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5627ebfdc409SJoe Perches 5628ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5629ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5630ebfdc409SJoe Perches 5631e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 5632e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 5633ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5634ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5635ebfdc409SJoe Perches } 5636ebfdc409SJoe Perches } 5637ebfdc409SJoe Perches 5638f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5639dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5640f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5641f78d98f6SJoe Perches my $level = $1; 5642f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5643f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5644f78d98f6SJoe Perches $fix) { 5645f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5646f78d98f6SJoe Perches } 5647f78d98f6SJoe Perches } 5648f78d98f6SJoe Perches 564945c55e92SJoe Perches# check for logging continuations 565045c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 565145c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 565245c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 565345c55e92SJoe Perches } 565445c55e92SJoe Perches 5655abb08a53SJoe Perches# check for mask then right shift without a parentheses 56565b57980dSJoe Perches if ($perl_version_ok && 5657abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5658abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5659abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5660abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5661abb08a53SJoe Perches } 5662abb08a53SJoe Perches 5663b75ac618SJoe Perches# check for pointer comparisons to NULL 56645b57980dSJoe Perches if ($perl_version_ok) { 5665b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5666b75ac618SJoe Perches my $val = $1; 5667b75ac618SJoe Perches my $equal = "!"; 5668b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5669b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5670b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5671b75ac618SJoe Perches $fix) { 5672b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5673b75ac618SJoe Perches } 5674b75ac618SJoe Perches } 5675b75ac618SJoe Perches } 5676b75ac618SJoe Perches 56778716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 56788716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 56798716de38SJoe Perches my $attr = $1; 56808716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 56818716de38SJoe Perches my $ptr = $1; 56828716de38SJoe Perches my $var = $2; 56838716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 56848716de38SJoe Perches ERROR("MISPLACED_INIT", 56858716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 56868716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 56878716de38SJoe Perches WARN("MISPLACED_INIT", 56888716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 56898716de38SJoe Perches $fix) { 5690194f66fcSJoe 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; 56918716de38SJoe Perches } 56928716de38SJoe Perches } 56938716de38SJoe Perches } 56948716de38SJoe Perches 5695e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5696e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5697e970b884SJoe Perches my $attr = $1; 5698e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5699e970b884SJoe Perches my $attr_prefix = $1; 5700e970b884SJoe Perches my $attr_type = $2; 5701e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5702e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5703e970b884SJoe Perches $fix) { 5704194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5705e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5706e970b884SJoe Perches } 5707e970b884SJoe Perches } 5708e970b884SJoe Perches 5709e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5710e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5711e970b884SJoe Perches my $attr = $1; 5712e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5713e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5714e970b884SJoe Perches $fix) { 5715194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5716e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5717e970b884SJoe Perches $lead = rtrim($1); 5718e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5719e970b884SJoe Perches $lead = "${lead}const "; 5720194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5721e970b884SJoe Perches } 5722e970b884SJoe Perches } 5723e970b884SJoe Perches 5724c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5725c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5726c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5727c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5728c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5729c17893c7SJoe Perches $fix) { 5730c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5731c17893c7SJoe Perches } 5732c17893c7SJoe Perches } 5733c17893c7SJoe Perches 5734fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5735fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5736fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5737fbdb8138SJoe Perches my $constant_func = $1; 5738fbdb8138SJoe Perches my $func = $constant_func; 5739fbdb8138SJoe Perches $func =~ s/^__constant_//; 5740fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5741fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5742fbdb8138SJoe Perches $fix) { 5743194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5744fbdb8138SJoe Perches } 5745fbdb8138SJoe Perches } 5746fbdb8138SJoe Perches 57471a15a250SPatrick Pannuto# prefer usleep_range over udelay 574837581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 574943c1d77cSJoe Perches my $delay = $1; 57501a15a250SPatrick Pannuto # ignore udelay's < 10, however 575143c1d77cSJoe Perches if (! ($delay < 10) ) { 5752000d1cc1SJoe Perches CHK("USLEEP_RANGE", 5753458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 575443c1d77cSJoe Perches } 575543c1d77cSJoe Perches if ($delay > 2000) { 575643c1d77cSJoe Perches WARN("LONG_UDELAY", 575743c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 57581a15a250SPatrick Pannuto } 57591a15a250SPatrick Pannuto } 57601a15a250SPatrick Pannuto 576109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 576209ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 576309ef8725SPatrick Pannuto if ($1 < 20) { 5764000d1cc1SJoe Perches WARN("MSLEEP", 5765458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 576609ef8725SPatrick Pannuto } 576709ef8725SPatrick Pannuto } 576809ef8725SPatrick Pannuto 576936ec1939SJoe Perches# check for comparisons of jiffies 577036ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 577136ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 577236ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 577336ec1939SJoe Perches } 577436ec1939SJoe Perches 57759d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 57769d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 57779d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 57789d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 57799d7a34a5SJoe Perches } 57809d7a34a5SJoe Perches 578100df344fSAndy Whitcroft# warn about #ifdefs in C files 5782c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 578300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 578400df344fSAndy Whitcroft# print "$herecurr"; 578500df344fSAndy Whitcroft# $clean = 0; 578600df344fSAndy Whitcroft# } 578700df344fSAndy Whitcroft 578822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5789c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 57903705ce5bSJoe Perches if (ERROR("SPACING", 57913705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 57923705ce5bSJoe Perches $fix) { 5793194f66fcSJoe Perches $fixed[$fixlinenr] =~ 57943705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 57953705ce5bSJoe Perches } 57963705ce5bSJoe Perches 579722f2a2efSAndy Whitcroft } 579822f2a2efSAndy Whitcroft 57994a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5800171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5801171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 58024a0df2efSAndy Whitcroft my $which = $1; 58034a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5804000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5805000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 58064a0df2efSAndy Whitcroft } 58074a0df2efSAndy Whitcroft } 58084a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5809402c2553SMichael S. Tsirkin 5810402c2553SMichael S. Tsirkin my $barriers = qr{ 5811402c2553SMichael S. Tsirkin mb| 5812402c2553SMichael S. Tsirkin rmb| 5813402c2553SMichael S. Tsirkin wmb| 5814402c2553SMichael S. Tsirkin read_barrier_depends 5815402c2553SMichael S. Tsirkin }x; 5816402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5817402c2553SMichael S. Tsirkin mb__before_atomic| 5818402c2553SMichael S. Tsirkin mb__after_atomic| 5819402c2553SMichael S. Tsirkin store_release| 5820402c2553SMichael S. Tsirkin load_acquire| 5821402c2553SMichael S. Tsirkin store_mb| 5822402c2553SMichael S. Tsirkin (?:$barriers) 5823402c2553SMichael S. Tsirkin }x; 5824402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5825402c2553SMichael S. Tsirkin (?:$barriers)| 582643e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 582743e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5828402c2553SMichael S. Tsirkin }x; 5829402c2553SMichael S. Tsirkin 5830402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 58314a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5832c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5833000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 58344a0df2efSAndy Whitcroft } 58354a0df2efSAndy Whitcroft } 58363ad81779SPaul E. McKenney 5837f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5838f4073b0fSMichael S. Tsirkin 5839f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5840f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5841f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5842f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5843f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5844f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5845f4073b0fSMichael S. Tsirkin } 5846f4073b0fSMichael S. Tsirkin 5847cb426e99SJoe Perches# check for waitqueue_active without a comment. 5848cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5849cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5850cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5851cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5852cb426e99SJoe Perches } 5853cb426e99SJoe Perches } 58543ad81779SPaul E. McKenney 585591db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends 585691db2592SPaul E. McKenney if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { 585791db2592SPaul E. McKenney WARN("READ_BARRIER_DEPENDS", 585891db2592SPaul E. McKenney "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); 585991db2592SPaul E. McKenney } 586091db2592SPaul E. McKenney 58614a0df2efSAndy Whitcroft# check of hardware specific defines 5862c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5863000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5864000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 58650a920b5bSAndy Whitcroft } 5866653d4876SAndy Whitcroft 5867596ed45bSJoe Perches# check that the storage class is not after a type 5868596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5869000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5870596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5871596ed45bSJoe Perches } 5872596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5873596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5874596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5875596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5876596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5877596ed45bSJoe Perches WARN("STORAGE_CLASS", 5878596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5879d4977c78STobias Klauser } 5880d4977c78STobias Klauser 5881de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5882de7d4f0eSAndy Whitcroft# storage class and type. 58839c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 58849c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5885000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5886000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5887de7d4f0eSAndy Whitcroft } 5888de7d4f0eSAndy Whitcroft 58898905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 58902b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 58912b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5892d5e616fcSJoe Perches if (WARN("INLINE", 5893d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5894d5e616fcSJoe Perches $fix) { 5895194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5896d5e616fcSJoe Perches 5897d5e616fcSJoe Perches } 58988905a67cSAndy Whitcroft } 58998905a67cSAndy Whitcroft 59003d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 59012b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59022b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5903000d1cc1SJoe Perches WARN("PREFER_PACKED", 5904000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 59053d130fd0SJoe Perches } 59063d130fd0SJoe Perches 590739b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 59082b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59092b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5910000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5911000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 591239b7e287SJoe Perches } 591339b7e287SJoe Perches 5914462811d9SJoe Perches# Check for __attribute__ section, prefer __section 5915462811d9SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5916462811d9SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { 5917462811d9SJoe Perches my $old = substr($rawline, $-[1], $+[1] - $-[1]); 5918462811d9SJoe Perches my $new = substr($old, 1, -1); 5919462811d9SJoe Perches if (WARN("PREFER_SECTION", 5920462811d9SJoe Perches "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && 5921462811d9SJoe Perches $fix) { 5922462811d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; 5923462811d9SJoe Perches } 5924462811d9SJoe Perches } 5925462811d9SJoe Perches 59265f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 59272b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59282b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5929d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5930d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5931d5e616fcSJoe Perches $fix) { 5932194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5933d5e616fcSJoe Perches 5934d5e616fcSJoe Perches } 59355f14d3bdSJoe Perches } 59365f14d3bdSJoe Perches 59376061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 59382b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59392b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5940d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5941d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5942d5e616fcSJoe Perches $fix) { 5943194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5944d5e616fcSJoe Perches } 59456061d949SJoe Perches } 59466061d949SJoe Perches 5947619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 59485b57980dSJoe Perches if ($perl_version_ok && 5949619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5950619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5951619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5952619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5953619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5954619a908aSJoe Perches } 5955619a908aSJoe Perches 5956fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 5957e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5958fd39f904STomas Winkler $realfile !~ m@\btools/@ && 5959e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5960e6176fa4SJoe Perches my $type = $1; 5961e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5962e6176fa4SJoe Perches $type = $1; 5963e6176fa4SJoe Perches my $kernel_type = 'u'; 5964e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5965e6176fa4SJoe Perches $type =~ /(\d+)/; 5966e6176fa4SJoe Perches $kernel_type .= $1; 5967e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5968e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5969e6176fa4SJoe Perches $fix) { 5970e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5971e6176fa4SJoe Perches } 5972e6176fa4SJoe Perches } 5973e6176fa4SJoe Perches } 5974e6176fa4SJoe Perches 5975938224b5SJoe Perches# check for cast of C90 native int or longer types constants 5976938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 5977938224b5SJoe Perches my $cast = $1; 5978938224b5SJoe Perches my $const = $2; 5979938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 5980938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 5981938224b5SJoe Perches $fix) { 5982938224b5SJoe Perches my $suffix = ""; 5983938224b5SJoe Perches my $newconst = $const; 5984938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 5985938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 5986938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 5987938224b5SJoe Perches $suffix .= 'LL'; 5988938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 5989938224b5SJoe Perches $suffix .= 'L'; 5990938224b5SJoe Perches } 5991938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 5992938224b5SJoe Perches } 5993938224b5SJoe Perches } 5994938224b5SJoe Perches 59958f53a9b8SJoe Perches# check for sizeof(&) 59968f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5997000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5998000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 59998f53a9b8SJoe Perches } 60008f53a9b8SJoe Perches 600166c80b60SJoe Perches# check for sizeof without parenthesis 600266c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6003d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6004d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6005d5e616fcSJoe Perches $fix) { 6006194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6007d5e616fcSJoe Perches } 600866c80b60SJoe Perches } 600966c80b60SJoe Perches 601088982feaSJoe Perches# check for struct spinlock declarations 601188982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 601288982feaSJoe Perches WARN("USE_SPINLOCK_T", 601388982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 601488982feaSJoe Perches } 601588982feaSJoe Perches 6016a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 601706668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6018a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6019caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6020caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6021d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6022d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6023d5e616fcSJoe Perches $fix) { 6024194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6025d5e616fcSJoe Perches } 6026a6962d72SJoe Perches } 6027a6962d72SJoe Perches } 6028a6962d72SJoe Perches 60290b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 60305b57980dSJoe Perches if ($perl_version_ok && 60310b523769SJoe Perches defined $stat && 60320b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 60330b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6034e3c6bc95STobin C. Harding my $stat_real; 6035e3c6bc95STobin C. Harding 60360b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 60370b523769SJoe Perches $lc = $lc + $linenr; 60380b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6039ffe07513SJoe Perches my $specifier; 6040ffe07513SJoe Perches my $extension; 60413bd32d6aSSakari Ailus my $qualifier; 6042ffe07513SJoe Perches my $bad_specifier = ""; 60430b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 60440b523769SJoe Perches $fmt =~ s/%%//g; 6045e3c6bc95STobin C. Harding 60463bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6047e3c6bc95STobin C. Harding $specifier = $1; 6048e3c6bc95STobin C. Harding $extension = $2; 60493bd32d6aSSakari Ailus $qualifier = $3; 6050361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 60513bd32d6aSSakari Ailus ($extension eq "f" && 60523bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6053e3c6bc95STobin C. Harding $bad_specifier = $specifier; 60540b523769SJoe Perches last; 60550b523769SJoe Perches } 6056e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6057e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6058e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 60590b523769SJoe Perches } 6060e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6061e3c6bc95STobin 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"); 6062e3c6bc95STobin C. Harding } 6063e3c6bc95STobin C. Harding } 6064e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 60652a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 60661df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 60671df7338aSSergey Senozhatsky my $use = ""; 6068e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 60691df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6070e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 60711df7338aSSergey Senozhatsky } 60722a9f9d85STobin C. Harding 60730b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6074e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6075e3c6bc95STobin C. Harding } 60760b523769SJoe Perches } 60770b523769SJoe Perches } 60780b523769SJoe Perches 6079554e165cSAndy Whitcroft# Check for misused memsets 60805b57980dSJoe Perches if ($perl_version_ok && 6081d1fe9c09SJoe Perches defined $stat && 60829e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6083554e165cSAndy Whitcroft 6084d7c76ba7SJoe Perches my $ms_addr = $2; 6085d1fe9c09SJoe Perches my $ms_val = $7; 6086d1fe9c09SJoe Perches my $ms_size = $12; 6087d7c76ba7SJoe Perches 6088554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6089554e165cSAndy Whitcroft ERROR("MEMSET", 6090d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6091554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6092554e165cSAndy Whitcroft WARN("MEMSET", 6093d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6094d7c76ba7SJoe Perches } 6095d7c76ba7SJoe Perches } 6096d7c76ba7SJoe Perches 609798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 60985b57980dSJoe Perches# if ($perl_version_ok && 6099f333195dSJoe Perches# defined $stat && 6100f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6101f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6102f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6103f333195dSJoe Perches# $fix) { 6104f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6105f333195dSJoe Perches# } 6106f333195dSJoe Perches# } 610798a9bba5SJoe Perches 6108b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 61095b57980dSJoe Perches# if ($perl_version_ok && 6110f333195dSJoe Perches# defined $stat && 6111f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6112f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6113f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6114f333195dSJoe Perches# } 6115b6117d17SMateusz Kulikowski 61168617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 61178617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 61185b57980dSJoe Perches# if ($perl_version_ok && 6119f333195dSJoe Perches# defined $stat && 6120f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6121f333195dSJoe Perches# 6122f333195dSJoe Perches# my $ms_val = $7; 6123f333195dSJoe Perches# 6124f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6125f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6126f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6127f333195dSJoe Perches# $fix) { 6128f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6129f333195dSJoe Perches# } 6130f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6131f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6132f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6133f333195dSJoe Perches# $fix) { 6134f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6135f333195dSJoe Perches# } 6136f333195dSJoe Perches# } 6137f333195dSJoe Perches# } 61388617cd09SMateusz Kulikowski 6139d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 61405b57980dSJoe Perches if ($perl_version_ok && 6141d1fe9c09SJoe Perches defined $stat && 6142d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6143d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6144d7c76ba7SJoe Perches my $call = $1; 6145d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6146d7c76ba7SJoe Perches my $arg1 = $3; 6147d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6148d1fe9c09SJoe Perches my $arg2 = $8; 6149d7c76ba7SJoe Perches my $cast; 6150d7c76ba7SJoe Perches 6151d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6152d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6153d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6154d7c76ba7SJoe Perches $cast = $cast1; 6155d7c76ba7SJoe Perches } else { 6156d7c76ba7SJoe Perches $cast = $cast2; 6157d7c76ba7SJoe Perches } 6158d7c76ba7SJoe Perches WARN("MINMAX", 6159d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6160554e165cSAndy Whitcroft } 6161554e165cSAndy Whitcroft } 6162554e165cSAndy Whitcroft 61634a273195SJoe Perches# check usleep_range arguments 61645b57980dSJoe Perches if ($perl_version_ok && 61654a273195SJoe Perches defined $stat && 61664a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 61674a273195SJoe Perches my $min = $1; 61684a273195SJoe Perches my $max = $7; 61694a273195SJoe Perches if ($min eq $max) { 61704a273195SJoe Perches WARN("USLEEP_RANGE", 6171458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 61724a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 61734a273195SJoe Perches $min > $max) { 61744a273195SJoe Perches WARN("USLEEP_RANGE", 6175458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 61764a273195SJoe Perches } 61774a273195SJoe Perches } 61784a273195SJoe Perches 6179823b794cSJoe Perches# check for naked sscanf 61805b57980dSJoe Perches if ($perl_version_ok && 6181823b794cSJoe Perches defined $stat && 61826c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6183823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6184823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6185823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6186823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6187823b794cSJoe Perches $lc = $lc + $linenr; 61882a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6189823b794cSJoe Perches WARN("NAKED_SSCANF", 6190823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6191823b794cSJoe Perches } 6192823b794cSJoe Perches 6193afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 61945b57980dSJoe Perches if ($perl_version_ok && 6195afc819abSJoe Perches defined $stat && 6196afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6197afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6198afc819abSJoe Perches $lc = $lc + $linenr; 61992a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6200afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6201afc819abSJoe Perches my $format = $6; 6202afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6203afc819abSJoe Perches if ($count == 1 && 6204afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6205afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6206afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6207afc819abSJoe Perches } 6208afc819abSJoe Perches } 6209afc819abSJoe Perches } 6210afc819abSJoe Perches 621170dc8a48SJoe Perches# check for new externs in .h files. 621270dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 621370dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6214d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 621570dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 621670dc8a48SJoe Perches $fix) { 6217194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 621870dc8a48SJoe Perches } 621970dc8a48SJoe Perches } 622070dc8a48SJoe Perches 6221de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6222171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6223c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6224171ae1a4SAndy Whitcroft { 6225c45dcabdSAndy Whitcroft my $function_name = $1; 6226c45dcabdSAndy Whitcroft my $paren_space = $2; 6227171ae1a4SAndy Whitcroft 6228171ae1a4SAndy Whitcroft my $s = $stat; 6229171ae1a4SAndy Whitcroft if (defined $cond) { 6230171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6231171ae1a4SAndy Whitcroft } 6232c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 6233c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 6234c45dcabdSAndy Whitcroft { 6235000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6236000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6237de7d4f0eSAndy Whitcroft } 6238de7d4f0eSAndy Whitcroft 6239171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6240000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6241000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6242171ae1a4SAndy Whitcroft } 62439c9ba34eSAndy Whitcroft 62449c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 62459c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 62469c9ba34eSAndy Whitcroft { 6247000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6248000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6249171ae1a4SAndy Whitcroft } 6250171ae1a4SAndy Whitcroft 6251a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6252a0ad7596SJoe Perches if (defined $stat && 625325bdda2bSMiles Chen $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6254ca0d8929SJoe Perches $1 ne "void") { 6255ca0d8929SJoe Perches my $args = trim($1); 6256ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6257ca0d8929SJoe Perches my $arg = trim($1); 6258ca0d8929SJoe Perches if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6259ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6260ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6261ca0d8929SJoe Perches } 6262ca0d8929SJoe Perches } 6263ca0d8929SJoe Perches } 6264ca0d8929SJoe Perches 6265a0ad7596SJoe Perches# check for function definitions 62665b57980dSJoe Perches if ($perl_version_ok && 6267a0ad7596SJoe Perches defined $stat && 6268a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6269a0ad7596SJoe Perches $context_function = $1; 6270a0ad7596SJoe Perches 6271a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6272a0ad7596SJoe Perches my $ok = 0; 6273a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6274a0ad7596SJoe Perches my $herectx = $here . "\n"; 6275a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6276a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6277a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6278a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6279a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6280a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6281a0ad7596SJoe Perches } 6282a0ad7596SJoe Perches if (!$ok) { 6283a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6284a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6285a0ad7596SJoe Perches } 6286a0ad7596SJoe Perches } 6287a0ad7596SJoe Perches 6288de7d4f0eSAndy Whitcroft# checks for new __setup's 6289de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6290de7d4f0eSAndy Whitcroft my $name = $1; 6291de7d4f0eSAndy Whitcroft 6292de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6293000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 62948c27ceffSMauro Carvalho Chehab "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); 6295de7d4f0eSAndy Whitcroft } 6296653d4876SAndy Whitcroft } 62979c0ca6f9SAndy Whitcroft 6298e29a70f1SJoe Perches# check for pointless casting of alloc functions 6299e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6300000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6301000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 63029c0ca6f9SAndy Whitcroft } 630313214adfSAndy Whitcroft 6304a640d25cSJoe Perches# alloc style 6305a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 63065b57980dSJoe Perches if ($perl_version_ok && 6307e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6308a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6309a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6310a640d25cSJoe Perches } 6311a640d25cSJoe Perches 631260a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 63135b57980dSJoe Perches if ($perl_version_ok && 63141b4a2ed4SJoe Perches defined $stat && 63151b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 631660a55369SJoe Perches my $oldfunc = $3; 631760a55369SJoe Perches my $a1 = $4; 631860a55369SJoe Perches my $a2 = $10; 631960a55369SJoe Perches my $newfunc = "kmalloc_array"; 632060a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 632160a55369SJoe Perches my $r1 = $a1; 632260a55369SJoe Perches my $r2 = $a2; 632360a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 632460a55369SJoe Perches $r1 = $a2; 632560a55369SJoe Perches $r2 = $a1; 632660a55369SJoe Perches } 6327e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6328e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 63291b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6330e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6331e3d95a2aSTobin C. Harding 6332e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 63331b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 63341b4a2ed4SJoe Perches $cnt == 1 && 6335e367455aSJoe Perches $fix) { 6336194f66fcSJoe 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; 633760a55369SJoe Perches } 633860a55369SJoe Perches } 633960a55369SJoe Perches } 634060a55369SJoe Perches 6341972fdea2SJoe Perches# check for krealloc arg reuse 63425b57980dSJoe Perches if ($perl_version_ok && 63434cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 63444cab63ceSJoe Perches $1 eq $3) { 6345972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6346972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6347972fdea2SJoe Perches } 6348972fdea2SJoe Perches 63495ce59ae0SJoe Perches# check for alloc argument mismatch 63505ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 63515ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 63525ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 63535ce59ae0SJoe Perches } 63545ce59ae0SJoe Perches 6355caf2a54fSJoe Perches# check for multiple semicolons 6356caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6357d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6358d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6359d5e616fcSJoe Perches $fix) { 6360194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6361d5e616fcSJoe Perches } 6362d1e2ad07SJoe Perches } 6363d1e2ad07SJoe Perches 6364cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6365cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6366cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 63670ab90191SJoe Perches my $ull = ""; 63680ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 63690ab90191SJoe Perches if (CHK("BIT_MACRO", 63700ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 63710ab90191SJoe Perches $fix) { 63720ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 63730ab90191SJoe Perches } 63740ab90191SJoe Perches } 63750ab90191SJoe Perches 63762d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 63772d632745SJoe 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*$/) { 63782d632745SJoe Perches my $config = $1; 63792d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 63802d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 63812d632745SJoe Perches $fix) { 63822d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 63832d632745SJoe Perches } 63842d632745SJoe Perches } 63852d632745SJoe Perches 6386e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6387c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6388c34c09a8SJoe Perches my $has_break = 0; 6389c34c09a8SJoe Perches my $has_statement = 0; 6390c34c09a8SJoe Perches my $count = 0; 6391c34c09a8SJoe Perches my $prevline = $linenr; 6392e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6393c34c09a8SJoe Perches $prevline--; 6394c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6395c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6396c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6397c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6398c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6399c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6400c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6401c34c09a8SJoe Perches $has_statement = 1; 6402c34c09a8SJoe Perches $count++; 6403258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6404c34c09a8SJoe Perches } 6405c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6406c34c09a8SJoe Perches WARN("MISSING_BREAK", 6407224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6408c34c09a8SJoe Perches } 6409c34c09a8SJoe Perches } 6410c34c09a8SJoe Perches 6411d1e2ad07SJoe Perches# check for switch/default statements without a break; 64125b57980dSJoe Perches if ($perl_version_ok && 6413d1e2ad07SJoe Perches defined $stat && 6414d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6415d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6416e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6417e3d95a2aSTobin C. Harding 6418d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6419d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6420caf2a54fSJoe Perches } 6421caf2a54fSJoe Perches 642213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6423d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6424d5e616fcSJoe Perches if (WARN("USE_FUNC", 6425d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6426d5e616fcSJoe Perches $fix) { 6427194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6428d5e616fcSJoe Perches } 642913214adfSAndy Whitcroft } 6430773647a0SAndy Whitcroft 643162ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 643262ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 643362ec818fSJoe Perches ERROR("DATE_TIME", 643462ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 643562ec818fSJoe Perches } 643662ec818fSJoe Perches 64372c92488aSJoe Perches# check for use of yield() 64382c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 64392c92488aSJoe Perches WARN("YIELD", 64402c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 64412c92488aSJoe Perches } 64422c92488aSJoe Perches 6443179f8f40SJoe Perches# check for comparisons against true and false 6444179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6445179f8f40SJoe Perches my $lead = $1; 6446179f8f40SJoe Perches my $arg = $2; 6447179f8f40SJoe Perches my $test = $3; 6448179f8f40SJoe Perches my $otype = $4; 6449179f8f40SJoe Perches my $trail = $5; 6450179f8f40SJoe Perches my $op = "!"; 6451179f8f40SJoe Perches 6452179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6453179f8f40SJoe Perches 6454179f8f40SJoe Perches my $type = lc($otype); 6455179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6456179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6457179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6458179f8f40SJoe Perches $op = ""; 6459179f8f40SJoe Perches } 6460179f8f40SJoe Perches 6461179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6462179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6463179f8f40SJoe Perches 6464179f8f40SJoe Perches## maybe suggesting a correct construct would better 6465179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6466179f8f40SJoe Perches 6467179f8f40SJoe Perches } 6468179f8f40SJoe Perches } 6469179f8f40SJoe Perches 64704882720bSThomas Gleixner# check for semaphores initialized locked 64714882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6472000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6473000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6474773647a0SAndy Whitcroft } 64756712d858SJoe Perches 647667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 647767d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6478000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 647967d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6480773647a0SAndy Whitcroft } 64816712d858SJoe Perches 6482ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6483f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6484000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6485ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6486f3db6639SMichael Ellerman } 64876712d858SJoe Perches 64883d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 64893d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 64903d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 64913d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 64923d709ab5SPaul E. McKenney } 64933d709ab5SPaul E. McKenney 64949189c7e7SJoe Perches# check for deprecated apis 64959189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 64969189c7e7SJoe Perches my $deprecated_api = $1; 64979189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 64989189c7e7SJoe Perches WARN("DEPRECATED_API", 64999189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 65009189c7e7SJoe Perches } 65019189c7e7SJoe Perches 65020f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6503d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 65046903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6505d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6506000d1cc1SJoe Perches WARN("CONST_STRUCT", 6507d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 65082b6db5cbSAndy Whitcroft } 6509773647a0SAndy Whitcroft 6510773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6511773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6512773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6513c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6514c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6515171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6516171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6517171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6518773647a0SAndy Whitcroft { 6519000d1cc1SJoe Perches WARN("NR_CPUS", 6520000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6521773647a0SAndy Whitcroft } 65229c9ba34eSAndy Whitcroft 652352ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 652452ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 652552ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 652652ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 652752ea8506SJoe Perches } 652852ea8506SJoe Perches 6529acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 65305b57980dSJoe Perches if ($perl_version_ok && 6531acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6532acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6533acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6534acd9362cSJoe Perches } 6535acd9362cSJoe Perches 6536de3f186fSDenis Efremov# nested likely/unlikely calls 6537de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 6538de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 6539de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 6540de3f186fSDenis Efremov } 6541de3f186fSDenis Efremov 6542691d77b6SAndy Whitcroft# whine mightly about in_atomic 6543691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6544691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6545000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6546000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6547f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6548000d1cc1SJoe Perches WARN("IN_ATOMIC", 6549000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6550691d77b6SAndy Whitcroft } 6551691d77b6SAndy Whitcroft } 65521704f47bSPeter Zijlstra 65530f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 65540f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 65550f5225b0SPeter Zijlstra ERROR("LOCKING", 65560f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 65570f5225b0SPeter Zijlstra } 65580f5225b0SPeter Zijlstra 65591704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 65601704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 65611704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 65621704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 65631704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 65641704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6565000d1cc1SJoe Perches ERROR("LOCKDEP", 6566000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 65671704f47bSPeter Zijlstra } 65681704f47bSPeter Zijlstra } 656988f8831cSDave Jones 6570b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6571b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6572000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6573000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 657488f8831cSDave Jones } 65752435880fSJoe Perches 657600180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 657700180468SJoe Perches# and whether or not function naming is typical and if 657800180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 65795b57980dSJoe Perches if ($perl_version_ok && 658000180468SJoe Perches defined $stat && 658100180468SJoe 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*\)/) { 658200180468SJoe Perches my $var = $1; 658300180468SJoe Perches my $perms = $2; 658400180468SJoe Perches my $show = $3; 658500180468SJoe Perches my $store = $4; 658600180468SJoe Perches my $octal_perms = perms_to_octal($perms); 658700180468SJoe Perches if ($show =~ /^${var}_show$/ && 658800180468SJoe Perches $store =~ /^${var}_store$/ && 658900180468SJoe Perches $octal_perms eq "0644") { 659000180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 659100180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 659200180468SJoe Perches $fix) { 659300180468SJoe 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})/; 659400180468SJoe Perches } 659500180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 659600180468SJoe Perches $store =~ /^NULL$/ && 659700180468SJoe Perches $octal_perms eq "0444") { 659800180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 659900180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 660000180468SJoe Perches $fix) { 660100180468SJoe 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})/; 660200180468SJoe Perches } 660300180468SJoe Perches } elsif ($show =~ /^NULL$/ && 660400180468SJoe Perches $store =~ /^${var}_store$/ && 660500180468SJoe Perches $octal_perms eq "0200") { 660600180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 660700180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 660800180468SJoe Perches $fix) { 660900180468SJoe 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})/; 661000180468SJoe Perches } 661100180468SJoe Perches } elsif ($octal_perms eq "0644" || 661200180468SJoe Perches $octal_perms eq "0444" || 661300180468SJoe Perches $octal_perms eq "0200") { 661400180468SJoe Perches my $newshow = "$show"; 661500180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 661600180468SJoe Perches my $newstore = $store; 661700180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 661800180468SJoe Perches my $rename = ""; 661900180468SJoe Perches if ($show ne $newshow) { 662000180468SJoe Perches $rename .= " '$show' to '$newshow'"; 662100180468SJoe Perches } 662200180468SJoe Perches if ($store ne $newstore) { 662300180468SJoe Perches $rename .= " '$store' to '$newstore'"; 662400180468SJoe Perches } 662500180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 662600180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 662700180468SJoe Perches } else { 662800180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 662900180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 663000180468SJoe Perches } 663100180468SJoe Perches } 663200180468SJoe Perches 6633515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6634515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 663573121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 663673121534SJoe Perches# specific definition of not visible in sysfs. 663773121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 663873121534SJoe Perches# use the default permissions 66395b57980dSJoe Perches if ($perl_version_ok && 6640459cf0aeSJoe Perches defined $stat && 6641515a235eSJoe Perches $line =~ /$mode_perms_search/) { 66422435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 66432435880fSJoe Perches my $func = $entry->[0]; 66442435880fSJoe Perches my $arg_pos = $entry->[1]; 66452435880fSJoe Perches 6646459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6647459cf0aeSJoe Perches $lc = $lc + $linenr; 66482a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6649459cf0aeSJoe Perches 66502435880fSJoe Perches my $skip_args = ""; 66512435880fSJoe Perches if ($arg_pos > 1) { 66522435880fSJoe Perches $arg_pos--; 66532435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 66542435880fSJoe Perches } 6655f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6656459cf0aeSJoe Perches if ($stat =~ /$test/) { 66572435880fSJoe Perches my $val = $1; 66582435880fSJoe Perches $val = $6 if ($skip_args ne ""); 665973121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 666073121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 666173121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 66622435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6663459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6664f90774e1SJoe Perches } 6665f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6666c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6667459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 66682435880fSJoe Perches } 6669459cf0aeSJoe Perches } 6670459cf0aeSJoe Perches } 6671459cf0aeSJoe Perches } 6672459cf0aeSJoe Perches 6673459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6674bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 667500180468SJoe Perches my $oval = $1; 667600180468SJoe Perches my $octal = perms_to_octal($oval); 6677f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6678459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6679f90774e1SJoe Perches $fix) { 668000180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 66812435880fSJoe Perches } 668213214adfSAndy Whitcroft } 66835a6d20ceSBjorn Andersson 66845a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 66855a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 66865a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 66875a6d20ceSBjorn Andersson my $valid_licenses = qr{ 66885a6d20ceSBjorn Andersson GPL| 66895a6d20ceSBjorn Andersson GPL\ v2| 66905a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 66915a6d20ceSBjorn Andersson Dual\ BSD/GPL| 66925a6d20ceSBjorn Andersson Dual\ MIT/GPL| 66935a6d20ceSBjorn Andersson Dual\ MPL/GPL| 66945a6d20ceSBjorn Andersson Proprietary 66955a6d20ceSBjorn Andersson }x; 66965a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 66975a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 66985a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 66995a6d20ceSBjorn Andersson } 67005a6d20ceSBjorn Andersson } 67016a8d76cbSMatteo Croce 67026a8d76cbSMatteo Croce# check for sysctl duplicate constants 67036a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 67046a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 67056a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 67066a8d76cbSMatteo Croce } 6707515a235eSJoe Perches } 670813214adfSAndy Whitcroft 670913214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 671013214adfSAndy Whitcroft # so just keep quiet. 671113214adfSAndy Whitcroft if ($#rawlines == -1) { 671213214adfSAndy Whitcroft exit(0); 67130a920b5bSAndy Whitcroft } 67140a920b5bSAndy Whitcroft 67158905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 67168905a67cSAndy Whitcroft # things that appear to be patches. 67178905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 67188905a67cSAndy Whitcroft exit(0); 67198905a67cSAndy Whitcroft } 67208905a67cSAndy Whitcroft 67218905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 67228905a67cSAndy Whitcroft # just keep quiet. 67238905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 67248905a67cSAndy Whitcroft exit(0); 67258905a67cSAndy Whitcroft } 67268905a67cSAndy Whitcroft 6727a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6728000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6729000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 67300a920b5bSAndy Whitcroft } 6731cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6732cd261496SGeert Uytterhoeven if ($signoff == 0) { 6733000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6734000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 6735cd261496SGeert Uytterhoeven } elsif (!$authorsignoff) { 6736cd261496SGeert Uytterhoeven WARN("NO_AUTHOR_SIGN_OFF", 6737cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 6738cd261496SGeert Uytterhoeven } 67390a920b5bSAndy Whitcroft } 67400a920b5bSAndy Whitcroft 6741f0a594c1SAndy Whitcroft print report_dump(); 674213214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 674313214adfSAndy Whitcroft print "$filename " if ($summary_file); 67446c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 67456c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 67466c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 67476c72ffaaSAndy Whitcroft } 67488905a67cSAndy Whitcroft 6749d2c0a235SAndy Whitcroft if ($quiet == 0) { 6750ef212196SJoe Perches # If there were any defects found and not already fixing them 6751ef212196SJoe Perches if (!$clean and !$fix) { 6752ef212196SJoe Perches print << "EOM" 6753ef212196SJoe Perches 6754ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6755ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6756ef212196SJoe PerchesEOM 6757ef212196SJoe Perches } 6758d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6759d2c0a235SAndy Whitcroft # then suggest that. 6760d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6761b0781216SMike Frysinger $rpt_cleaners = 0; 6762d8469f16SJoe Perches print << "EOM" 6763d8469f16SJoe Perches 6764d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6765d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6766d8469f16SJoe PerchesEOM 6767d2c0a235SAndy Whitcroft } 6768d2c0a235SAndy Whitcroft } 6769d2c0a235SAndy Whitcroft 6770d752fcc8SJoe Perches if ($clean == 0 && $fix && 6771d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6772d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 67739624b8d6SJoe Perches my $newfile = $filename; 67749624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 67753705ce5bSJoe Perches my $linecount = 0; 67763705ce5bSJoe Perches my $f; 67773705ce5bSJoe Perches 6778d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6779d752fcc8SJoe Perches 67803705ce5bSJoe Perches open($f, '>', $newfile) 67813705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 67823705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 67833705ce5bSJoe Perches $linecount++; 67843705ce5bSJoe Perches if ($file) { 67853705ce5bSJoe Perches if ($linecount > 3) { 67863705ce5bSJoe Perches $fixed_line =~ s/^\+//; 67873705ce5bSJoe Perches print $f $fixed_line . "\n"; 67883705ce5bSJoe Perches } 67893705ce5bSJoe Perches } else { 67903705ce5bSJoe Perches print $f $fixed_line . "\n"; 67913705ce5bSJoe Perches } 67923705ce5bSJoe Perches } 67933705ce5bSJoe Perches close($f); 67943705ce5bSJoe Perches 67953705ce5bSJoe Perches if (!$quiet) { 67963705ce5bSJoe Perches print << "EOM"; 6797d8469f16SJoe Perches 67983705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 67993705ce5bSJoe Perches 68003705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 68013705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 68023705ce5bSJoe Perches 68033705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 68043705ce5bSJoe PerchesNo warranties, expressed or implied... 68053705ce5bSJoe PerchesEOM 68063705ce5bSJoe Perches } 68073705ce5bSJoe Perches } 68083705ce5bSJoe Perches 6809d8469f16SJoe Perches if ($quiet == 0) { 6810d8469f16SJoe Perches print "\n"; 6811d8469f16SJoe Perches if ($clean == 1) { 6812d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6813d8469f16SJoe Perches } else { 6814d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 68150a920b5bSAndy Whitcroft } 68160a920b5bSAndy Whitcroft } 68170a920b5bSAndy Whitcroft return $clean; 68180a920b5bSAndy Whitcroft} 6819