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 2297f36d3eb8SJoe Perchessub get_raw_comment { 2298f36d3eb8SJoe Perches my ($line, $rawline) = @_; 2299f36d3eb8SJoe Perches my $comment = ''; 2300f36d3eb8SJoe Perches 2301f36d3eb8SJoe Perches for my $i (0 .. (length($line) - 1)) { 2302f36d3eb8SJoe Perches if (substr($line, $i, 1) eq "$;") { 2303f36d3eb8SJoe Perches $comment .= substr($rawline, $i, 1); 2304f36d3eb8SJoe Perches } 2305f36d3eb8SJoe Perches } 2306f36d3eb8SJoe Perches 2307f36d3eb8SJoe Perches return $comment; 2308f36d3eb8SJoe Perches} 2309f36d3eb8SJoe Perches 23100a920b5bSAndy Whitcroftsub process { 23110a920b5bSAndy Whitcroft my $filename = shift; 23120a920b5bSAndy Whitcroft 23130a920b5bSAndy Whitcroft my $linenr=0; 23140a920b5bSAndy Whitcroft my $prevline=""; 2315c2fdda0dSAndy Whitcroft my $prevrawline=""; 23160a920b5bSAndy Whitcroft my $stashline=""; 2317c2fdda0dSAndy Whitcroft my $stashrawline=""; 23180a920b5bSAndy Whitcroft 23194a0df2efSAndy Whitcroft my $length; 23200a920b5bSAndy Whitcroft my $indent; 23210a920b5bSAndy Whitcroft my $previndent=0; 23220a920b5bSAndy Whitcroft my $stashindent=0; 23230a920b5bSAndy Whitcroft 2324de7d4f0eSAndy Whitcroft our $clean = 1; 23250a920b5bSAndy Whitcroft my $signoff = 0; 2326cd261496SGeert Uytterhoeven my $author = ''; 2327cd261496SGeert Uytterhoeven my $authorsignoff = 0; 23280a920b5bSAndy Whitcroft my $is_patch = 0; 2329133712a2SRob Herring my $is_binding_patch = -1; 233029ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 233115662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 2332ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2333490b292cSJoe Perches my $commit_log_lines = 0; #Number of commit log lines 2334bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 23352a076f40SJoe Perches my $commit_log_long_line = 0; 2336e518e9a5SJoe Perches my $commit_log_has_diff = 0; 233713f1937eSJoe Perches my $reported_maintainer_file = 0; 2338fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2339fa64205dSPasi Savanainen 2340365dd4eaSJoe Perches my $last_blank_line = 0; 23415e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2342365dd4eaSJoe Perches 234313214adfSAndy Whitcroft our @report = (); 23446c72ffaaSAndy Whitcroft our $cnt_lines = 0; 23456c72ffaaSAndy Whitcroft our $cnt_error = 0; 23466c72ffaaSAndy Whitcroft our $cnt_warn = 0; 23476c72ffaaSAndy Whitcroft our $cnt_chk = 0; 23486c72ffaaSAndy Whitcroft 23490a920b5bSAndy Whitcroft # Trace the real file/line as we go. 23500a920b5bSAndy Whitcroft my $realfile = ''; 23510a920b5bSAndy Whitcroft my $realline = 0; 23520a920b5bSAndy Whitcroft my $realcnt = 0; 23530a920b5bSAndy Whitcroft my $here = ''; 235477cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 23550a920b5bSAndy Whitcroft my $in_comment = 0; 2356c2fdda0dSAndy Whitcroft my $comment_edge = 0; 23570a920b5bSAndy Whitcroft my $first_line = 0; 23581e855726SWolfram Sang my $p1_prefix = ''; 23590a920b5bSAndy Whitcroft 236013214adfSAndy Whitcroft my $prev_values = 'E'; 236113214adfSAndy Whitcroft 236213214adfSAndy Whitcroft # suppression flags 2363773647a0SAndy Whitcroft my %suppress_ifbraces; 2364170d3a22SAndy Whitcroft my %suppress_whiletrailers; 23652b474a1aSAndy Whitcroft my %suppress_export; 23663e469cdcSAndy Whitcroft my $suppress_statement = 0; 2367653d4876SAndy Whitcroft 23687e51f197SJoe Perches my %signatures = (); 2369323c1260SJoe Perches 2370c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2371de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2372c2fdda0dSAndy Whitcroft # 2373de7d4f0eSAndy Whitcroft my @setup_docs = (); 2374de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2375773647a0SAndy Whitcroft 2376d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2377d8b07710SJoe Perches 23789f3a8992SRob Herring my $checklicenseline = 1; 23799f3a8992SRob Herring 2380773647a0SAndy Whitcroft sanitise_line_reset(); 2381c2fdda0dSAndy Whitcroft my $line; 2382c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2383773647a0SAndy Whitcroft $linenr++; 2384773647a0SAndy Whitcroft $line = $rawline; 2385c2fdda0dSAndy Whitcroft 23863705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 23873705ce5bSJoe Perches 2388773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2389de7d4f0eSAndy Whitcroft $setup_docs = 0; 23908c27ceffSMauro Carvalho Chehab if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { 2391de7d4f0eSAndy Whitcroft $setup_docs = 1; 2392de7d4f0eSAndy Whitcroft } 2393773647a0SAndy Whitcroft #next; 2394de7d4f0eSAndy Whitcroft } 239574fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2396773647a0SAndy Whitcroft $realline=$1-1; 2397773647a0SAndy Whitcroft if (defined $2) { 2398773647a0SAndy Whitcroft $realcnt=$3+1; 2399773647a0SAndy Whitcroft } else { 2400773647a0SAndy Whitcroft $realcnt=1+1; 2401773647a0SAndy Whitcroft } 2402c45dcabdSAndy Whitcroft $in_comment = 0; 2403773647a0SAndy Whitcroft 2404773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2405773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2406773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2407773647a0SAndy Whitcroft # at context start. 2408773647a0SAndy Whitcroft my $edge; 240901fa9147SAndy Whitcroft my $cnt = $realcnt; 241001fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 241101fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 241201fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 241301fa9147SAndy Whitcroft $cnt--; 241401fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2415721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2416fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2417fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2418fae17daeSAndy Whitcroft ($edge) = $1; 2419fae17daeSAndy Whitcroft last; 2420fae17daeSAndy Whitcroft } 2421773647a0SAndy Whitcroft } 2422773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2423773647a0SAndy Whitcroft $in_comment = 1; 2424773647a0SAndy Whitcroft } 2425773647a0SAndy Whitcroft 2426773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2427773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2428773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2429773647a0SAndy Whitcroft if (!defined $edge && 243083242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2431773647a0SAndy Whitcroft { 2432773647a0SAndy Whitcroft $in_comment = 1; 2433773647a0SAndy Whitcroft } 2434773647a0SAndy Whitcroft 2435773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2436773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2437773647a0SAndy Whitcroft 2438171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2439773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2440171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2441773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2442773647a0SAndy Whitcroft } 2443773647a0SAndy Whitcroft push(@lines, $line); 2444773647a0SAndy Whitcroft 2445773647a0SAndy Whitcroft if ($realcnt > 1) { 2446773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2447773647a0SAndy Whitcroft } else { 2448773647a0SAndy Whitcroft $realcnt = 0; 2449773647a0SAndy Whitcroft } 2450773647a0SAndy Whitcroft 2451773647a0SAndy Whitcroft #print "==>$rawline\n"; 2452773647a0SAndy Whitcroft #print "-->$line\n"; 2453de7d4f0eSAndy Whitcroft 2454de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2455de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2456de7d4f0eSAndy Whitcroft } 2457de7d4f0eSAndy Whitcroft } 2458de7d4f0eSAndy Whitcroft 24596c72ffaaSAndy Whitcroft $prefix = ''; 24606c72ffaaSAndy Whitcroft 2461773647a0SAndy Whitcroft $realcnt = 0; 2462773647a0SAndy Whitcroft $linenr = 0; 2463194f66fcSJoe Perches $fixlinenr = -1; 24640a920b5bSAndy Whitcroft foreach my $line (@lines) { 24650a920b5bSAndy Whitcroft $linenr++; 2466194f66fcSJoe Perches $fixlinenr++; 24671b5539b1SJoe Perches my $sline = $line; #copy of $line 24681b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 24690a920b5bSAndy Whitcroft 2470c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 2471f36d3eb8SJoe Perches my $raw_comment = get_raw_comment($line, $rawline); 24726c72ffaaSAndy Whitcroft 247312c253abSJoe Perches# check if it's a mode change, rename or start of a patch 247412c253abSJoe Perches if (!$in_commit_log && 247512c253abSJoe Perches ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || 247612c253abSJoe Perches ($line =~ /^rename (?:from|to) \S+\s*$/ || 247712c253abSJoe Perches $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { 247812c253abSJoe Perches $is_patch = 1; 247912c253abSJoe Perches } 248012c253abSJoe Perches 24810a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2482e518e9a5SJoe Perches if (!$in_commit_log && 248374fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 248474fd4f34SJoe Perches my $context = $4; 24850a920b5bSAndy Whitcroft $is_patch = 1; 24864a0df2efSAndy Whitcroft $first_line = $linenr + 1; 24870a920b5bSAndy Whitcroft $realline=$1-1; 24880a920b5bSAndy Whitcroft if (defined $2) { 24890a920b5bSAndy Whitcroft $realcnt=$3+1; 24900a920b5bSAndy Whitcroft } else { 24910a920b5bSAndy Whitcroft $realcnt=1+1; 24920a920b5bSAndy Whitcroft } 2493c2fdda0dSAndy Whitcroft annotate_reset(); 249413214adfSAndy Whitcroft $prev_values = 'E'; 249513214adfSAndy Whitcroft 2496773647a0SAndy Whitcroft %suppress_ifbraces = (); 2497170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 24982b474a1aSAndy Whitcroft %suppress_export = (); 24993e469cdcSAndy Whitcroft $suppress_statement = 0; 250074fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 250174fd4f34SJoe Perches $context_function = $1; 250274fd4f34SJoe Perches } else { 250374fd4f34SJoe Perches undef $context_function; 250474fd4f34SJoe Perches } 25050a920b5bSAndy Whitcroft next; 25060a920b5bSAndy Whitcroft 25074a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 25084a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 25094a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2510773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 25110a920b5bSAndy Whitcroft $realline++; 2512d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 25130a920b5bSAndy Whitcroft 25144a0df2efSAndy Whitcroft # Measure the line length and indent. 2515c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 25160a920b5bSAndy Whitcroft 25170a920b5bSAndy Whitcroft # Track the previous line. 25180a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 25190a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2520c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2521c2fdda0dSAndy Whitcroft 2522773647a0SAndy Whitcroft #warn "line<$line>\n"; 25236c72ffaaSAndy Whitcroft 2524d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2525d8aaf121SAndy Whitcroft $realcnt--; 25260a920b5bSAndy Whitcroft } 25270a920b5bSAndy Whitcroft 2528cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2529cc77cdcaSAndy Whitcroft 25306c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 25316c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2532773647a0SAndy Whitcroft 25332ac73b4fSJoe Perches my $found_file = 0; 2534773647a0SAndy Whitcroft # extract the filename as it passes 25353bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 25363bf9a009SRabin Vincent $realfile = $1; 25372b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2538270c49a0SJoe Perches $in_commit_log = 0; 25392ac73b4fSJoe Perches $found_file = 1; 25403bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2541773647a0SAndy Whitcroft $realfile = $1; 25422b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2543270c49a0SJoe Perches $in_commit_log = 0; 25441e855726SWolfram Sang 25451e855726SWolfram Sang $p1_prefix = $1; 2546e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2547e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2548000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2549000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 25501e855726SWolfram Sang } 2551773647a0SAndy Whitcroft 2552c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2553000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2554000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2555773647a0SAndy Whitcroft } 25562ac73b4fSJoe Perches $found_file = 1; 25572ac73b4fSJoe Perches } 25582ac73b4fSJoe Perches 255934d8815fSJoe Perches#make up the handle for any error we report on this line 256034d8815fSJoe Perches if ($showfile) { 256134d8815fSJoe Perches $prefix = "$realfile:$realline: " 256234d8815fSJoe Perches } elsif ($emacs) { 25637d3a9f67SJoe Perches if ($file) { 25647d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 25657d3a9f67SJoe Perches } else { 256634d8815fSJoe Perches $prefix = "$filename:$linenr: "; 256734d8815fSJoe Perches } 25687d3a9f67SJoe Perches } 256934d8815fSJoe Perches 25702ac73b4fSJoe Perches if ($found_file) { 257185b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 257285b0ee18SJoe Perches WARN("OBSOLETE", 257385b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 257485b0ee18SJoe Perches } 25757bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 25762ac73b4fSJoe Perches $check = 1; 25772ac73b4fSJoe Perches } else { 25782ac73b4fSJoe Perches $check = $check_orig; 25792ac73b4fSJoe Perches } 25809f3a8992SRob Herring $checklicenseline = 1; 2581133712a2SRob Herring 2582133712a2SRob Herring if ($realfile !~ /^MAINTAINERS/) { 2583133712a2SRob Herring my $last_binding_patch = $is_binding_patch; 2584133712a2SRob Herring 2585133712a2SRob Herring $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; 2586133712a2SRob Herring 2587133712a2SRob Herring if (($last_binding_patch != -1) && 2588133712a2SRob Herring ($last_binding_patch ^ $is_binding_patch)) { 2589133712a2SRob Herring WARN("DT_SPLIT_BINDING_PATCH", 2590133712a2SRob Herring "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n"); 2591133712a2SRob Herring } 2592133712a2SRob Herring } 2593133712a2SRob Herring 2594773647a0SAndy Whitcroft next; 2595773647a0SAndy Whitcroft } 2596773647a0SAndy Whitcroft 2597389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 25980a920b5bSAndy Whitcroft 2599c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2600c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2601c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 26020a920b5bSAndy Whitcroft 26036c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 26046c72ffaaSAndy Whitcroft 2605490b292cSJoe Perches# Verify the existence of a commit log if appropriate 2606490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines 2607490b292cSJoe Perches if ($in_commit_log) { 2608490b292cSJoe Perches if ($line !~ /^\s*$/) { 2609490b292cSJoe Perches $commit_log_lines++; #could be a $signature 2610490b292cSJoe Perches } 2611490b292cSJoe Perches } elsif ($has_commit_log && $commit_log_lines < 2) { 2612490b292cSJoe Perches WARN("COMMIT_MESSAGE", 2613490b292cSJoe Perches "Missing commit description - Add an appropriate one\n"); 2614490b292cSJoe Perches $commit_log_lines = 2; #warn only once 2615490b292cSJoe Perches } 2616490b292cSJoe Perches 2617e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2618e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2619e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2620e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2621e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2622e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2623e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2624e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2625e518e9a5SJoe Perches $commit_log_has_diff = 1; 2626e518e9a5SJoe Perches } 2627e518e9a5SJoe Perches 26283bf9a009SRabin Vincent# Check for incorrect file permissions 26293bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 26303bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 263104db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 263204db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2633000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2634000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 26353bf9a009SRabin Vincent } 26363bf9a009SRabin Vincent } 26373bf9a009SRabin Vincent 2638cd261496SGeert Uytterhoeven# Check the patch for a From: 2639cd261496SGeert Uytterhoeven if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { 2640cd261496SGeert Uytterhoeven $author = $1; 2641cd261496SGeert Uytterhoeven $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); 2642cd261496SGeert Uytterhoeven $author =~ s/"//g; 2643dfa05c28SJoe Perches $author = reformat_email($author); 2644cd261496SGeert Uytterhoeven } 2645cd261496SGeert Uytterhoeven 264620112475SJoe Perches# Check the patch for a signoff: 2647dfa05c28SJoe Perches if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { 26484a0df2efSAndy Whitcroft $signoff++; 264915662b3eSJoe Perches $in_commit_log = 0; 2650cd261496SGeert Uytterhoeven if ($author ne '') { 2651dfa05c28SJoe Perches if (same_email_addresses($1, $author)) { 2652cd261496SGeert Uytterhoeven $authorsignoff = 1; 2653cd261496SGeert Uytterhoeven } 2654cd261496SGeert Uytterhoeven } 26550a920b5bSAndy Whitcroft } 265620112475SJoe Perches 2657e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2658e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2659e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2660e0d975b1SJoe Perches $reported_maintainer_file = 1; 2661e0d975b1SJoe Perches } 2662e0d975b1SJoe Perches 266320112475SJoe Perches# Check signature styles 2664270c49a0SJoe Perches if (!$in_header_lines && 2665ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 266620112475SJoe Perches my $space_before = $1; 266720112475SJoe Perches my $sign_off = $2; 266820112475SJoe Perches my $space_after = $3; 266920112475SJoe Perches my $email = $4; 267020112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 267120112475SJoe Perches 2672ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2673ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2674ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2675ce0338dfSJoe Perches } 267620112475SJoe Perches if (defined $space_before && $space_before ne "") { 26773705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26783705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 26793705ce5bSJoe Perches $fix) { 2680194f66fcSJoe Perches $fixed[$fixlinenr] = 26813705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26823705ce5bSJoe Perches } 268320112475SJoe Perches } 268420112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 26853705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26863705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 26873705ce5bSJoe Perches $fix) { 2688194f66fcSJoe Perches $fixed[$fixlinenr] = 26893705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26903705ce5bSJoe Perches } 26913705ce5bSJoe Perches 269220112475SJoe Perches } 269320112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 26943705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 26953705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 26963705ce5bSJoe Perches $fix) { 2697194f66fcSJoe Perches $fixed[$fixlinenr] = 26983705ce5bSJoe Perches "$ucfirst_sign_off $email"; 26993705ce5bSJoe Perches } 270020112475SJoe Perches } 270120112475SJoe Perches 2702dfa05c28SJoe Perches my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); 270320112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 270420112475SJoe Perches if ($suggested_email eq "") { 2705000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2706000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 270720112475SJoe Perches } else { 270820112475SJoe Perches my $dequoted = $suggested_email; 270920112475SJoe Perches $dequoted =~ s/^"//; 271020112475SJoe Perches $dequoted =~ s/" </ </; 271120112475SJoe Perches # Don't force email to have quotes 271220112475SJoe Perches # Allow just an angle bracketed address 2713dfa05c28SJoe Perches if (!same_email_addresses($email, $suggested_email)) { 2714000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2715000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 271620112475SJoe Perches } 27170a920b5bSAndy Whitcroft } 27187e51f197SJoe Perches 27197e51f197SJoe Perches# Check for duplicate signatures 27207e51f197SJoe Perches my $sig_nospace = $line; 27217e51f197SJoe Perches $sig_nospace =~ s/\s//g; 27227e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 27237e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 27247e51f197SJoe Perches WARN("BAD_SIGN_OFF", 27257e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 27267e51f197SJoe Perches } else { 27277e51f197SJoe Perches $signatures{$sig_nospace} = 1; 27287e51f197SJoe Perches } 27296c5d24eeSSean Christopherson 27306c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email 27316c5d24eeSSean Christopherson if ($sign_off =~ /^co-developed-by:$/i) { 27326c5d24eeSSean Christopherson if ($email eq $author) { 27336c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27346c5d24eeSSean Christopherson "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); 27356c5d24eeSSean Christopherson } 27366c5d24eeSSean Christopherson if (!defined $lines[$linenr]) { 27376c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27386c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); 27396c5d24eeSSean Christopherson } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { 27406c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27416c5d24eeSSean Christopherson "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27426c5d24eeSSean Christopherson } elsif ($1 ne $email) { 27436c5d24eeSSean Christopherson WARN("BAD_SIGN_OFF", 27446c5d24eeSSean Christopherson "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); 27456c5d24eeSSean Christopherson } 27466c5d24eeSSean Christopherson } 27470a920b5bSAndy Whitcroft } 27480a920b5bSAndy Whitcroft 2749a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2750a2fe16b9SJoe Perches if ($in_header_lines && 2751a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2752a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2753a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2754a2fe16b9SJoe Perches } 2755a2fe16b9SJoe Perches 27567ebd05efSChristopher Covington# Check for unwanted Gerrit info 27577ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 27587ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 27597ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 27607ebd05efSChristopher Covington } 27617ebd05efSChristopher Covington 2762369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2763369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2764369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2765369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2766369c8dd3SJoe Perches # timestamp 2767634cffccSJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || 2768634cffccSJoe Perches $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || 2769634cffccSJoe Perches $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { 2770634cffccSJoe Perches # stack dump address styles 2771369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2772369c8dd3SJoe Perches } 2773369c8dd3SJoe Perches 27742a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 27752a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2776bf4daf12SJoe Perches length($line) > 75 && 2777bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2778bf4daf12SJoe Perches # file delta changes 2779bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2780bf4daf12SJoe Perches # filename then : 2781bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2782bf4daf12SJoe Perches # A Fixes: or Link: line 2783bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 27842a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 27852a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 27862a076f40SJoe Perches $commit_log_long_line = 1; 27872a076f40SJoe Perches } 27882a076f40SJoe Perches 2789bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2790bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2791bf4daf12SJoe Perches $line =~ /^\s*$/) { 2792bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2793bf4daf12SJoe Perches } 2794bf4daf12SJoe Perches 27950d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2796369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2797a8972573SJohn Hubbard $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && 2798e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2799fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2800aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2801369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2802bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2803fe043ea1SJoe Perches my $init_char = "c"; 2804fe043ea1SJoe Perches my $orig_commit = ""; 28050d7835fcSJoe Perches my $short = 1; 28060d7835fcSJoe Perches my $long = 0; 28070d7835fcSJoe Perches my $case = 1; 28080d7835fcSJoe Perches my $space = 1; 28090d7835fcSJoe Perches my $hasdesc = 0; 281019c146a6SJoe Perches my $hasparens = 0; 28110d7835fcSJoe Perches my $id = '0123456789ab'; 28120d7835fcSJoe Perches my $orig_desc = "commit description"; 28130d7835fcSJoe Perches my $description = ""; 28140d7835fcSJoe Perches 2815fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2816fe043ea1SJoe Perches $init_char = $1; 2817fe043ea1SJoe Perches $orig_commit = lc($2); 2818fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2819fe043ea1SJoe Perches $orig_commit = lc($1); 2820fe043ea1SJoe Perches } 2821fe043ea1SJoe Perches 28220d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 28230d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 28240d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 28250d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 28260d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 28270d7835fcSJoe Perches $orig_desc = $1; 282819c146a6SJoe Perches $hasparens = 1; 28290d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 28300d7835fcSJoe Perches defined $rawlines[$linenr] && 28310d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 28320d7835fcSJoe Perches $orig_desc = $1; 283319c146a6SJoe Perches $hasparens = 1; 2834b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2835b671fde0SJoe Perches defined $rawlines[$linenr] && 2836b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2837b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2838b671fde0SJoe Perches $orig_desc = $1; 2839b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2840b671fde0SJoe Perches $orig_desc .= " " . $1; 284119c146a6SJoe Perches $hasparens = 1; 28420d7835fcSJoe Perches } 28430d7835fcSJoe Perches 28440d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 28450d7835fcSJoe Perches $id, $orig_desc); 28460d7835fcSJoe Perches 2847948b133aSHeinrich Schuchardt if (defined($id) && 2848948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2849d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 28500d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 28510d7835fcSJoe Perches } 2852d311cd44SJoe Perches } 2853d311cd44SJoe Perches 285413f1937eSJoe Perches# Check for added, moved or deleted files 285513f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 285613f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 285713f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 285813f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 285913f1937eSJoe Perches (defined($1) || defined($2))))) { 2860a82603a8SAndrew Jeffery $is_patch = 1; 286113f1937eSJoe Perches $reported_maintainer_file = 1; 286213f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 286313f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 286413f1937eSJoe Perches } 286513f1937eSJoe Perches 2866e400edb1SRob Herring# Check for adding new DT bindings not in schema format 2867e400edb1SRob Herring if (!$in_commit_log && 2868e400edb1SRob Herring ($line =~ /^new file mode\s*\d+\s*$/) && 2869e400edb1SRob Herring ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { 2870e400edb1SRob Herring WARN("DT_SCHEMA_BINDING_PATCH", 2871e400edb1SRob Herring "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); 2872e400edb1SRob Herring } 2873e400edb1SRob Herring 287400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 28758905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2876000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2877000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 28786c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2879de7d4f0eSAndy Whitcroft } 2880de7d4f0eSAndy Whitcroft 2881de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2882de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2883171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2884171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2885171ae1a4SAndy Whitcroft 2886171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2887171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2888171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2889171ae1a4SAndy Whitcroft 289034d99219SJoe Perches CHK("INVALID_UTF8", 2891000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 289200df344fSAndy Whitcroft } 28930a920b5bSAndy Whitcroft 289415662b3eSJoe Perches# Check if it's the start of a commit log 289515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 289615662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2897eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2898eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 289915662b3eSJoe Perches $in_header_lines = 0; 290015662b3eSJoe Perches $in_commit_log = 1; 2901ed43c4e5SAllen Hubbe $has_commit_log = 1; 290215662b3eSJoe Perches } 290315662b3eSJoe Perches 2904fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2905fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2906fa64205dSPasi Savanainen if ($in_header_lines && 2907fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2908fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2909fa64205dSPasi Savanainen $non_utf8_charset = 1; 2910fa64205dSPasi Savanainen } 2911fa64205dSPasi Savanainen 2912fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 291315662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2914fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 291515662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 291615662b3eSJoe Perches } 291715662b3eSJoe Perches 2918d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2919d6430f71SJoe Perches if ($tree && $in_commit_log) { 2920d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2921d6430f71SJoe Perches my $file = $1; 2922d6430f71SJoe Perches 2923d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2924d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2925d6430f71SJoe Perches # 2926d6430f71SJoe Perches } else { 2927d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2928d6430f71SJoe Perches } 2929d6430f71SJoe Perches } 2930d6430f71SJoe Perches } 2931d6430f71SJoe Perches 293266b47b4aSKees Cook# Check for various typo / spelling mistakes 293366d7a382SJoe Perches if (defined($misspellings) && 293466d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2935ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 293666b47b4aSKees Cook my $typo = $1; 293766b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 293866b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 293966b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 29400675a8fbSJean Delvare my $msg_level = \&WARN; 29410675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29420675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 294366b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 294466b47b4aSKees Cook $fix) { 294566b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 294666b47b4aSKees Cook } 294766b47b4aSKees Cook } 294866b47b4aSKees Cook } 294966b47b4aSKees Cook 2950a8dd86bfSMatteo Croce# check for invalid commit id 2951a8dd86bfSMatteo Croce if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { 2952a8dd86bfSMatteo Croce my $id; 2953a8dd86bfSMatteo Croce my $description; 2954a8dd86bfSMatteo Croce ($id, $description) = git_commit_info($2, undef, undef); 2955a8dd86bfSMatteo Croce if (!defined($id)) { 2956a8dd86bfSMatteo Croce WARN("UNKNOWN_COMMIT_ID", 2957a8dd86bfSMatteo Croce "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); 2958a8dd86bfSMatteo Croce } 2959a8dd86bfSMatteo Croce } 2960a8dd86bfSMatteo Croce 296130670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 296230670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 296300df344fSAndy Whitcroft 29640a920b5bSAndy Whitcroft#trailing whitespace 29659c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2966c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2967d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2968d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2969d5e616fcSJoe Perches $fix) { 2970194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2971d5e616fcSJoe Perches } 2972c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2973c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29743705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 29753705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 29763705ce5bSJoe Perches $fix) { 2977194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 29783705ce5bSJoe Perches } 29793705ce5bSJoe Perches 2980d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 29810a920b5bSAndy Whitcroft } 29825368df20SAndy Whitcroft 29834783f894SJosh Triplett# Check for FSF mailing addresses. 2984109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 29851bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 29863e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 29873e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 29884783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 29890675a8fbSJean Delvare my $msg_level = \&ERROR; 29900675a8fbSJean Delvare $msg_level = \&CHK if ($file); 29910675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 29924783f894SJosh 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) 29934783f894SJosh Triplett } 29944783f894SJosh Triplett 29953354957aSAndi Kleen# check for Kconfig help text having a real description 29969fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 29979fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 29983354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 2999678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 3000678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 3001678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 3002678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 30033354957aSAndi Kleen my $length = 0; 30049fe287d7SAndy Whitcroft my $cnt = $realcnt; 30059fe287d7SAndy Whitcroft my $ln = $linenr + 1; 30069fe287d7SAndy Whitcroft my $f; 3007a1385803SAndy Whitcroft my $is_start = 0; 30089fe287d7SAndy Whitcroft my $is_end = 0; 3009a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 30109fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 30119fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 30129fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 30139fe287d7SAndy Whitcroft 30149fe287d7SAndy Whitcroft next if ($f =~ /^-/); 30158d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 3016a1385803SAndy Whitcroft 301786adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 3018a1385803SAndy Whitcroft $is_start = 1; 301984af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 302084af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 302184af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 302284af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 302384af7a61SUlf Magnusson } 3024a1385803SAndy Whitcroft $length = -1; 3025a1385803SAndy Whitcroft } 3026a1385803SAndy Whitcroft 30279fe287d7SAndy Whitcroft $f =~ s/^.//; 30283354957aSAndi Kleen $f =~ s/#.*//; 30293354957aSAndi Kleen $f =~ s/^\s+//; 30303354957aSAndi Kleen next if ($f =~ /^$/); 3031678ae162SUlf Magnusson 3032678ae162SUlf Magnusson # This only checks context lines in the patch 3033678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 3034678ae162SUlf Magnusson # positives, even though some of these are 3035678ae162SUlf Magnusson # common words in help texts 3036678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 3037678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 30389fe287d7SAndy Whitcroft $is_end = 1; 30399fe287d7SAndy Whitcroft last; 30409fe287d7SAndy Whitcroft } 30413354957aSAndi Kleen $length++; 30423354957aSAndi Kleen } 304356193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 3044000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 304556193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 304656193274SVadim Bendebury } 3047a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 30483354957aSAndi Kleen } 30493354957aSAndi Kleen 3050628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form 3051628f91a2SJoe Perches if ($realfile =~ /^MAINTAINERS$/ && 3052628f91a2SJoe Perches $rawline =~ /^\+[A-Z]:/ && 3053628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 3054628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 3055628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 3056628f91a2SJoe Perches $fix) { 3057628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 3058628f91a2SJoe Perches } 3059628f91a2SJoe Perches } 3060628f91a2SJoe Perches 3061327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 3062327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 3063327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 3064327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 3065327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 3066327953e9SChristoph Jaeger } 3067327953e9SChristoph Jaeger 3068c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 3069c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 3070c68e5878SArnaud Lacombe my $flag = $1; 3071c68e5878SArnaud Lacombe my $replacement = { 3072c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 3073c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 3074c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 3075c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 3076c68e5878SArnaud Lacombe }; 3077c68e5878SArnaud Lacombe 3078c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 3079c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 3080c68e5878SArnaud Lacombe } 3081c68e5878SArnaud Lacombe 3082bff5da43SRob Herring# check for DT compatible documentation 30837dd05b38SFlorian Vaussard if (defined $root && 30847dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 30857dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 30867dd05b38SFlorian Vaussard 3087bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 3088bff5da43SRob Herring 3089cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 3090852d095dSRob Herring my $vp_file = $dt_path . "vendor-prefixes.yaml"; 3091cc93319bSFlorian Vaussard 3092bff5da43SRob Herring foreach my $compat (@compats) { 3093bff5da43SRob Herring my $compat2 = $compat; 3094185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 3095185d566bSRob Herring my $compat3 = $compat; 3096185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 3097185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 3098bff5da43SRob Herring if ( $? >> 8 ) { 3099bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3100bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 3101bff5da43SRob Herring } 3102bff5da43SRob Herring 31034fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 31044fbf32a6SFlorian Vaussard my $vendor = $1; 3105852d095dSRob Herring `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; 3106bff5da43SRob Herring if ( $? >> 8 ) { 3107bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 3108cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 3109bff5da43SRob Herring } 3110bff5da43SRob Herring } 3111bff5da43SRob Herring } 3112bff5da43SRob Herring 31139f3a8992SRob Herring# check for using SPDX license tag at beginning of files 31149f3a8992SRob Herring if ($realline == $checklicenseline) { 31159f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 31169f3a8992SRob Herring $checklicenseline = 2; 31179f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 31189f3a8992SRob Herring my $comment = ""; 31199f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 31209f3a8992SRob Herring $comment = '/*'; 31219f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 31229f3a8992SRob Herring $comment = '//'; 3123c8df0ab6SLubomir Rintel } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { 31249f3a8992SRob Herring $comment = '#'; 31259f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 31269f3a8992SRob Herring $comment = '..'; 31279f3a8992SRob Herring } 31289f3a8992SRob Herring 3129fdf13693SJoe Perches# check SPDX comment style for .[chsS] files 3130fdf13693SJoe Perches if ($realfile =~ /\.[chsS]$/ && 3131fdf13693SJoe Perches $rawline =~ /SPDX-License-Identifier:/ && 3132ffbce897SJoe Perches $rawline !~ m@^\+\s*\Q$comment\E\s*@) { 3133fdf13693SJoe Perches WARN("SPDX_LICENSE_TAG", 3134fdf13693SJoe Perches "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); 3135fdf13693SJoe Perches } 3136fdf13693SJoe Perches 31379f3a8992SRob Herring if ($comment !~ /^$/ && 3138ffbce897SJoe Perches $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { 31399f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 31409f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 31413b6e8ac9SJoe Perches } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { 31423b6e8ac9SJoe Perches my $spdx_license = $1; 31433b6e8ac9SJoe Perches if (!is_SPDX_License_valid($spdx_license)) { 31443b6e8ac9SJoe Perches WARN("SPDX_LICENSE_TAG", 31453b6e8ac9SJoe Perches "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); 31463b6e8ac9SJoe Perches } 31479f3a8992SRob Herring } 31489f3a8992SRob Herring } 31499f3a8992SRob Herring } 31509f3a8992SRob Herring 31515368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 3152d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 31535368df20SAndy Whitcroft 3154a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number 3155a8da38a9SJoe Perches if ($realline != $checklicenseline && 3156a8da38a9SJoe Perches $rawline =~ /\bSPDX-License-Identifier:/ && 3157a8da38a9SJoe Perches substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { 3158a8da38a9SJoe Perches WARN("SPDX_LICENSE_TAG", 3159a8da38a9SJoe Perches "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); 3160a8da38a9SJoe Perches } 3161a8da38a9SJoe Perches 316247e0c88bSJoe Perches# line length limit (with some exclusions) 316347e0c88bSJoe Perches# 316447e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 316547e0c88bSJoe Perches# logging functions like pr_info that end in a string 316647e0c88bSJoe Perches# lines with a single string 316747e0c88bSJoe Perches# #defines that are a single string 31682e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 316947e0c88bSJoe Perches# 317047e0c88bSJoe Perches# There are 3 different line length message types: 3171ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 317247e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 317347e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 317447e0c88bSJoe Perches# 317547e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 317647e0c88bSJoe Perches# 317747e0c88bSJoe Perches 3178b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 317947e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 318047e0c88bSJoe Perches 318147e0c88bSJoe Perches # Check the allowed long line types first 318247e0c88bSJoe Perches 318347e0c88bSJoe Perches # logging functions that end in a string that starts 318447e0c88bSJoe Perches # before $max_line_length 318547e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 318647e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 318747e0c88bSJoe Perches $msg_type = ""; 318847e0c88bSJoe Perches 318947e0c88bSJoe Perches # lines with only strings (w/ possible termination) 319047e0c88bSJoe Perches # #defines with only strings 319147e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 319247e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 319347e0c88bSJoe Perches $msg_type = ""; 319447e0c88bSJoe Perches 3195cc147506SJoe Perches # More special cases 3196cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3197cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3198d560a5f8SJoe Perches $msg_type = ""; 3199d560a5f8SJoe Perches 32002e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 32012e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 32022e4bbbc5SAndreas Brauchli $msg_type = ""; 32032e4bbbc5SAndreas Brauchli 320447e0c88bSJoe Perches # Otherwise set the alternate message types 320547e0c88bSJoe Perches 320647e0c88bSJoe Perches # a comment starts before $max_line_length 320747e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 320847e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 320947e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 321047e0c88bSJoe Perches 321147e0c88bSJoe Perches # a quoted string starts before $max_line_length 321247e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 321347e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 321447e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 321547e0c88bSJoe Perches } 321647e0c88bSJoe Perches 321747e0c88bSJoe Perches if ($msg_type ne "" && 321847e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 321947e0c88bSJoe Perches WARN($msg_type, 32206cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 32210a920b5bSAndy Whitcroft } 322247e0c88bSJoe Perches } 32230a920b5bSAndy Whitcroft 32248905a67cSAndy Whitcroft# check for adding lines without a newline. 32258905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3226000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3227000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 32288905a67cSAndy Whitcroft } 32298905a67cSAndy Whitcroft 3230b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3231de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 32320a920b5bSAndy Whitcroft 32330a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 32340a920b5bSAndy Whitcroft# more than 8 must use tabs. 3235c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3236c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3237c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3238d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 32393705ce5bSJoe Perches if (ERROR("CODE_INDENT", 32403705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 32413705ce5bSJoe Perches $fix) { 3242194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 32433705ce5bSJoe Perches } 32440a920b5bSAndy Whitcroft } 32450a920b5bSAndy Whitcroft 324608e44365SAlberto Panizzo# check for space before tabs. 324708e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 324808e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32493705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 32503705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 32513705ce5bSJoe Perches $fix) { 3252194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3253d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 3254194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3255c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 32563705ce5bSJoe Perches } 325708e44365SAlberto Panizzo } 325808e44365SAlberto Panizzo 32596a487211SJoe Perches# check for assignments on the start of a line 32606a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 32616a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 32626a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 32636a487211SJoe Perches } 32646a487211SJoe Perches 3265d1fe9c09SJoe Perches# check for && or || at the start of a line 3266d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3267d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3268d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3269d1fe9c09SJoe Perches } 3270d1fe9c09SJoe Perches 3271a91e8994SJoe Perches# check indentation starts on a tab stop 32725b57980dSJoe Perches if ($perl_version_ok && 3273bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3274a91e8994SJoe Perches my $indent = length($1); 3275a91e8994SJoe Perches if ($indent % 8) { 3276a91e8994SJoe Perches if (WARN("TABSTOP", 3277a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3278a91e8994SJoe Perches $fix) { 3279a91e8994SJoe Perches $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; 3280a91e8994SJoe Perches } 3281a91e8994SJoe Perches } 3282a91e8994SJoe Perches } 3283a91e8994SJoe Perches 3284d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 32855b57980dSJoe Perches if ($perl_version_ok && 3286fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3287d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3288d1fe9c09SJoe Perches my $oldindent = $1; 3289d1fe9c09SJoe Perches my $rest = $2; 3290d1fe9c09SJoe Perches 3291d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3292d1fe9c09SJoe Perches if ($pos >= 0) { 3293b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3294b34a26f3SJoe Perches my $newindent = $2; 3295d1fe9c09SJoe Perches 3296d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3297d1fe9c09SJoe Perches "\t" x ($pos / 8) . 3298d1fe9c09SJoe Perches " " x ($pos % 8); 3299d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3300d1fe9c09SJoe Perches 3301d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3302d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 33033705ce5bSJoe Perches 33043705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 33053705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 33063705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3307194f66fcSJoe Perches $fixed[$fixlinenr] =~ 33083705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 33093705ce5bSJoe Perches } 3310d1fe9c09SJoe Perches } 3311d1fe9c09SJoe Perches } 3312d1fe9c09SJoe Perches } 3313d1fe9c09SJoe Perches 33146ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 33156ab3a970SJoe Perches# avoid checking a few false positives: 33166ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 33176ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 33186ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 33196ab3a970SJoe Perches# multiline macros that define functions 33206ab3a970SJoe Perches# known attributes or the __attribute__ keyword 33216ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 33226ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 33233705ce5bSJoe Perches if (CHK("SPACING", 3324f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 33253705ce5bSJoe Perches $fix) { 3326194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3327f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 33283705ce5bSJoe Perches } 3329aad4f614SJoe Perches } 3330aad4f614SJoe Perches 333186406b1cSJoe Perches# Block comment styles 333286406b1cSJoe Perches# Networking with an initial /* 333305880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3334fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 333585ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 333685ad978cSJoe Perches $realline > 2) { 333705880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 333805880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 333905880600SJoe Perches } 334005880600SJoe Perches 334186406b1cSJoe Perches# Block comments use * on subsequent lines 334286406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 334386406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3344a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 334561135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3346a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 334786406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 334886406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3349a605e32eSJoe Perches } 3350a605e32eSJoe Perches 335186406b1cSJoe Perches# Block comments use */ on trailing lines 335286406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3353c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3354c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3355c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 335686406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 335786406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 335805880600SJoe Perches } 335905880600SJoe Perches 336008eb9b80SJoe Perches# Block comment * alignment 336108eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3362af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3363af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3364af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 336508eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3366af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3367af207524SJoe Perches my $oldindent; 336808eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3369af207524SJoe Perches if (defined($1)) { 3370af207524SJoe Perches $oldindent = expand_tabs($1); 3371af207524SJoe Perches } else { 3372af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3373af207524SJoe Perches $oldindent = expand_tabs($1); 3374af207524SJoe Perches } 337508eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 337608eb9b80SJoe Perches my $newindent = $1; 337708eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3378af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 337908eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 338008eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 338108eb9b80SJoe Perches } 338208eb9b80SJoe Perches } 338308eb9b80SJoe Perches 33847f619191SJoe Perches# check for missing blank lines after struct/union declarations 33857f619191SJoe Perches# with exceptions for various attributes and macros 33867f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 33877f619191SJoe Perches $line =~ /^\+/ && 33887f619191SJoe Perches !($line =~ /^\+\s*$/ || 33897f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 33907f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 33917f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 33927f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 33937f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 33947f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 33950bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 33967f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3397d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3398d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3399d752fcc8SJoe Perches $fix) { 3400f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3401d752fcc8SJoe Perches } 34027f619191SJoe Perches } 34037f619191SJoe Perches 3404365dd4eaSJoe Perches# check for multiple consecutive blank lines 3405365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3406365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3407365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3408d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3409d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3410d752fcc8SJoe Perches $fix) { 3411f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3412d752fcc8SJoe Perches } 3413d752fcc8SJoe Perches 3414365dd4eaSJoe Perches $last_blank_line = $linenr; 3415365dd4eaSJoe Perches } 3416365dd4eaSJoe Perches 34173b617e3bSJoe Perches# check for missing blank lines after declarations 34183f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 34193f7bac03SJoe Perches # actual declarations 34203f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34215a4e1fd3SJoe Perches # function pointer declarations 34225a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34233f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34243f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34253f7bac03SJoe Perches # known declaration macros 34263f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 34273f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 34283f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 34293f7bac03SJoe Perches # other possible extensions of declaration lines 34303f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 34313f7bac03SJoe Perches # not starting a section or a macro "\" extended line 34323f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 34333f7bac03SJoe Perches # looks like a declaration 34343f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 34355a4e1fd3SJoe Perches # function pointer declarations 34365a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 34373f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 34383f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 34393f7bac03SJoe Perches # known declaration macros 34403f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 34413f7bac03SJoe Perches # start of struct or union or enum 3442328b5f41SJoe Perches $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || 34433f7bac03SJoe Perches # start or end of block or continuation of declaration 34443f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 34453f7bac03SJoe Perches # bitfield continuation 34463f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 34473f7bac03SJoe Perches # other possible extensions of declaration lines 34483f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 34493f7bac03SJoe Perches # indentation of previous and current line are the same 34503f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3451d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3452d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3453d752fcc8SJoe Perches $fix) { 3454f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3455d752fcc8SJoe Perches } 34563b617e3bSJoe Perches } 34573b617e3bSJoe Perches 34585f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 34596b4c5bebSAndy Whitcroft# Exceptions: 34606b4c5bebSAndy Whitcroft# 1) within comments 34616b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 34626b4c5bebSAndy Whitcroft# 3) hanging labels 34633705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 34645f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 34653705ce5bSJoe Perches if (WARN("LEADING_SPACE", 34663705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 34673705ce5bSJoe Perches $fix) { 3468194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 34693705ce5bSJoe Perches } 34705f7ddae6SRaffaele Recalcati } 34715f7ddae6SRaffaele Recalcati 3472b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3473b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3474b9ea10d6SAndy Whitcroft 34755751a24eSJoe Perches# check for unusual line ending [ or ( 34765751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 34775751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 34785751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 34795751a24eSJoe Perches } 34805751a24eSJoe Perches 34814dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 34824dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 34834dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 34844dbed76fSJoe Perches $context_function = $1; 34854dbed76fSJoe Perches } 34864dbed76fSJoe Perches 34874dbed76fSJoe Perches# check if this appears to be the end of function declaration 34884dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 34894dbed76fSJoe Perches undef $context_function; 34904dbed76fSJoe Perches } 34914dbed76fSJoe Perches 3492032a4c0fSJoe Perches# check indentation of any line with a bare else 3493840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3494032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3495032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3496032a4c0fSJoe Perches my $tabs = length($1) + 1; 3497840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3498840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3499840080a0SJoe Perches defined $lines[$linenr] && 3500840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3501032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3502032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3503032a4c0fSJoe Perches } 3504032a4c0fSJoe Perches } 3505032a4c0fSJoe Perches 3506c00df19aSJoe Perches# check indentation of a line with a break; 3507c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3508c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3509c00df19aSJoe Perches my $tabs = $1; 3510c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3511c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3512c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3513c00df19aSJoe Perches } 3514c00df19aSJoe Perches } 3515c00df19aSJoe Perches 3516c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3517cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3518000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3519000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3520c2fdda0dSAndy Whitcroft } 352122f2a2efSAndy Whitcroft 352256e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 352356e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 352456e77d70SJoe Perches WARN("HOTPLUG_SECTION", 352556e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 352656e77d70SJoe Perches } 352756e77d70SJoe Perches 35289c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 35292b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 35302b474a1aSAndy Whitcroft $realline_next); 35313e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3532ca819864SJoe Perches if ($linenr > $suppress_statement && 35331b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3534170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3535f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3536171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3537171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3538171ae1a4SAndy Whitcroft 35393e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 35403e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 35413e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 35423e469cdcSAndy Whitcroft # until we hit end of it. 35433e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 35443e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 35453e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 35463e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 35473e469cdcSAndy Whitcroft } 3548f74bd194SAndy Whitcroft 35492b474a1aSAndy Whitcroft # Find the real next line. 35502b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 35512b474a1aSAndy Whitcroft if (defined $realline_next && 35522b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 35532b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 35542b474a1aSAndy Whitcroft $realline_next++; 35552b474a1aSAndy Whitcroft } 35562b474a1aSAndy Whitcroft 3557171ae1a4SAndy Whitcroft my $s = $stat; 3558171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3559cf655043SAndy Whitcroft 3560c2fdda0dSAndy Whitcroft # Ignore goto labels. 3561171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3562c2fdda0dSAndy Whitcroft 3563c2fdda0dSAndy Whitcroft # Ignore functions being called 3564171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3565c2fdda0dSAndy Whitcroft 3566463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3567463f2864SAndy Whitcroft 3568c45dcabdSAndy Whitcroft # declarations always start with types 3569d2506586SAndy 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) { 3570c45dcabdSAndy Whitcroft my $type = $1; 3571c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3572c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3573c45dcabdSAndy Whitcroft 35746c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3575a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3576c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3577c2fdda0dSAndy Whitcroft } 35788905a67cSAndy Whitcroft 35796c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 358065863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3581c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 35829c0ca6f9SAndy Whitcroft } 35838905a67cSAndy Whitcroft 35848905a67cSAndy Whitcroft # Check for any sort of function declaration. 35858905a67cSAndy Whitcroft # int foo(something bar, other baz); 35868905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3587171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 35888905a67cSAndy Whitcroft my ($name_len) = length($1); 35898905a67cSAndy Whitcroft 3590cf655043SAndy Whitcroft my $ctx = $s; 3591773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 35928905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3593cf655043SAndy Whitcroft 35948905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3595c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 35968905a67cSAndy Whitcroft 3597c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 35988905a67cSAndy Whitcroft } 35998905a67cSAndy Whitcroft } 36008905a67cSAndy Whitcroft } 36018905a67cSAndy Whitcroft 36029c0ca6f9SAndy Whitcroft } 36039c0ca6f9SAndy Whitcroft 360400df344fSAndy Whitcroft# 360500df344fSAndy Whitcroft# Checks which may be anchored in the context. 360600df344fSAndy Whitcroft# 360700df344fSAndy Whitcroft 360800df344fSAndy Whitcroft# Check for switch () and associated case and default 360900df344fSAndy Whitcroft# statements should be at the same indent. 361000df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 361100df344fSAndy Whitcroft my $err = ''; 361200df344fSAndy Whitcroft my $sep = ''; 361300df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 361400df344fSAndy Whitcroft shift(@ctx); 361500df344fSAndy Whitcroft for my $ctx (@ctx) { 361600df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 361700df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 361800df344fSAndy Whitcroft $indent != $cindent) { 361900df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 362000df344fSAndy Whitcroft $sep = ''; 362100df344fSAndy Whitcroft } else { 362200df344fSAndy Whitcroft $sep = "[...]\n"; 362300df344fSAndy Whitcroft } 362400df344fSAndy Whitcroft } 362500df344fSAndy Whitcroft if ($err ne '') { 3626000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3627000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3628de7d4f0eSAndy Whitcroft } 3629de7d4f0eSAndy Whitcroft } 3630de7d4f0eSAndy Whitcroft 3631de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3632de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 36330fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3634773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3635773647a0SAndy Whitcroft 36369c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 36378eef05ddSJoe Perches 36388eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 36398eef05ddSJoe Perches WARN("DEEP_INDENTATION", 36408eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 36418eef05ddSJoe Perches } 36428eef05ddSJoe Perches 3643de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3644de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3645de7d4f0eSAndy Whitcroft 3646548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3647548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3648de7d4f0eSAndy Whitcroft 3649548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3650548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3651548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3652548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3653548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3654773647a0SAndy Whitcroft $ctx_ln++; 3655773647a0SAndy Whitcroft } 3656548596d5SAndy Whitcroft 365753210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 365853210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3659773647a0SAndy Whitcroft 3660773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3661000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3662000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 366301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 366400df344fSAndy Whitcroft } 3665773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3666773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3667773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3668773647a0SAndy Whitcroft { 36699c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 36709c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3671000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3672000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 367301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 36749c0ca6f9SAndy Whitcroft } 36759c0ca6f9SAndy Whitcroft } 367600df344fSAndy Whitcroft } 367700df344fSAndy Whitcroft 36784d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3679f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 36803e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 36813e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 36823e469cdcSAndy Whitcroft if (!defined $stat); 36834d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 36844d001e4dSAndy Whitcroft 36854d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 36864d001e4dSAndy Whitcroft 36879f5af480SJoe Perches # remove inline comments 36889f5af480SJoe Perches $s =~ s/$;/ /g; 36899f5af480SJoe Perches $c =~ s/$;/ /g; 36904d001e4dSAndy Whitcroft 36914d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 36926f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 36936f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 36944d001e4dSAndy Whitcroft 36959f5af480SJoe Perches # Make sure we remove the line prefixes as we have 36969f5af480SJoe Perches # none on the first line, and are going to readd them 36979f5af480SJoe Perches # where necessary. 36989f5af480SJoe Perches $s =~ s/\n./\n/gs; 36999f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 37009f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 37019f5af480SJoe Perches } 37029f5af480SJoe Perches 37034d001e4dSAndy Whitcroft # We want to check the first line inside the block 37044d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 37054d001e4dSAndy Whitcroft # 1) any blank line termination 37064d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 37074d001e4dSAndy Whitcroft # 3) any do (...) { 37084d001e4dSAndy Whitcroft my $continuation = 0; 37094d001e4dSAndy Whitcroft my $check = 0; 37104d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 37114d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 37124d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 37134d001e4dSAndy Whitcroft $continuation = 1; 37144d001e4dSAndy Whitcroft } 37159bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 37164d001e4dSAndy Whitcroft $check = 1; 37174d001e4dSAndy Whitcroft $cond_lines++; 37184d001e4dSAndy Whitcroft } 37194d001e4dSAndy Whitcroft 37204d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 37214d001e4dSAndy Whitcroft # preprocessor statement. 37224d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 37234d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 37244d001e4dSAndy Whitcroft $check = 0; 37254d001e4dSAndy Whitcroft } 37264d001e4dSAndy Whitcroft 37279bd49efeSAndy Whitcroft my $cond_ptr = -1; 3728740504c6SAndy Whitcroft $continuation = 0; 37299bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 37309bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 37314d001e4dSAndy Whitcroft 3732f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3733f16fa28fSAndy Whitcroft # is not linear. 3734f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3735f16fa28fSAndy Whitcroft $check = 0; 3736f16fa28fSAndy Whitcroft } 3737f16fa28fSAndy Whitcroft 37389bd49efeSAndy Whitcroft # Ignore: 37399bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 37409bd49efeSAndy Whitcroft # 2) preprocessor lines, and 37419bd49efeSAndy Whitcroft # 3) labels. 3742740504c6SAndy Whitcroft if ($continuation || 3743740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 37449bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 37459bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3746740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 374730dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 37489bd49efeSAndy Whitcroft $cond_lines++; 37499bd49efeSAndy Whitcroft } 37504d001e4dSAndy Whitcroft } 375130dad6ebSAndy Whitcroft } 37524d001e4dSAndy Whitcroft 37534d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 37544d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 37554d001e4dSAndy Whitcroft 37564d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 37574d001e4dSAndy Whitcroft # this is not this patch's fault. 37584d001e4dSAndy Whitcroft if (!defined($stat_real) || 37594d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 37604d001e4dSAndy Whitcroft $check = 0; 37614d001e4dSAndy Whitcroft } 37624d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 37634d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 37644d001e4dSAndy Whitcroft } 37654d001e4dSAndy Whitcroft 37669bd49efeSAndy 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"; 37674d001e4dSAndy Whitcroft 37689f5af480SJoe Perches if ($check && $s ne '' && 37699f5af480SJoe Perches (($sindent % 8) != 0 || 37709f5af480SJoe Perches ($sindent < $indent) || 3771f6950a73SJoe Perches ($sindent == $indent && 3772f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 37739f5af480SJoe Perches ($sindent > $indent + 8))) { 3774000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3775000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 37764d001e4dSAndy Whitcroft } 37774d001e4dSAndy Whitcroft } 37784d001e4dSAndy Whitcroft 37796c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 37806c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 37811f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 37821f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 37836c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3784c2fdda0dSAndy Whitcroft if ($dbg_values) { 3785c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3786cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3787cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 37881f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3789c2fdda0dSAndy Whitcroft } 37906c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 37916c72ffaaSAndy Whitcroft 379200df344fSAndy Whitcroft#ignore lines not being added 37933705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 379400df344fSAndy Whitcroft 379511ca40a0SJoe Perches# check for dereferences that span multiple lines 379611ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 379711ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 379811ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 379911ca40a0SJoe Perches my $ref = $1; 380011ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 380111ca40a0SJoe Perches $ref .= $1; 380211ca40a0SJoe Perches $ref =~ s/\s//g; 380311ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 380411ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 380511ca40a0SJoe Perches } 380611ca40a0SJoe Perches 3807a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3808c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3809a1ce18e4SJoe Perches my $type = $1; 3810a1ce18e4SJoe Perches my $var = $2; 3811207a8e84SJoe Perches $var = "" if (!defined $var); 3812207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3813a1ce18e4SJoe Perches my $sign = $1; 3814a1ce18e4SJoe Perches my $pointer = $2; 3815a1ce18e4SJoe Perches 3816a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3817a1ce18e4SJoe Perches 3818a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3819a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3820a1ce18e4SJoe Perches $fix) { 3821a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3822207a8e84SJoe Perches my $comp_pointer = $pointer; 3823207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3824207a8e84SJoe Perches $decl .= $comp_pointer; 3825207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3826207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3827a1ce18e4SJoe Perches } 3828a1ce18e4SJoe Perches } 3829a1ce18e4SJoe Perches } 3830a1ce18e4SJoe Perches 3831653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 38327429c690SAndy Whitcroft if ($dbg_type) { 38337429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3834000d1cc1SJoe Perches ERROR("TEST_TYPE", 3835000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 38367429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3837000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3838000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 38397429c690SAndy Whitcroft } 3840653d4876SAndy Whitcroft next; 3841653d4876SAndy Whitcroft } 3842a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3843a1ef277eSAndy Whitcroft if ($dbg_attr) { 38449360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3845000d1cc1SJoe Perches ERROR("TEST_ATTR", 3846000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 38479360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3848000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3849000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3850a1ef277eSAndy Whitcroft } 3851a1ef277eSAndy Whitcroft next; 3852a1ef277eSAndy Whitcroft } 3853653d4876SAndy Whitcroft 3854f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 385599423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 385699423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3857d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3858d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3859f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3860f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3861f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3862d752fcc8SJoe Perches my $fixedline = $prevrawline; 3863d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3864f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3865d752fcc8SJoe Perches $fixedline = $line; 38668d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3867f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3868d752fcc8SJoe Perches } 3869f0a594c1SAndy Whitcroft } 3870f0a594c1SAndy Whitcroft 387100df344fSAndy Whitcroft# 387200df344fSAndy Whitcroft# Checks which are anchored on the added line. 387300df344fSAndy Whitcroft# 387400df344fSAndy Whitcroft 3875653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3876c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3877653d4876SAndy Whitcroft my $path = $1; 3878653d4876SAndy Whitcroft if ($path =~ m{//}) { 3879000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3880495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3881495e9d84SJoe Perches } 3882495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3883495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3884495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3885653d4876SAndy Whitcroft } 3886653d4876SAndy Whitcroft } 3887653d4876SAndy Whitcroft 388800df344fSAndy Whitcroft# no C99 // comments 388900df344fSAndy Whitcroft if ($line =~ m{//}) { 38903705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 38913705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 38923705ce5bSJoe Perches $fix) { 3893194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 38943705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 38953705ce5bSJoe Perches my $comment = trim($1); 3896194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 38973705ce5bSJoe Perches } 38983705ce5bSJoe Perches } 389900df344fSAndy Whitcroft } 390000df344fSAndy Whitcroft # Remove C99 comments. 39010a920b5bSAndy Whitcroft $line =~ s@//.*@@; 39026c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 39030a920b5bSAndy Whitcroft 39042b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 39052b474a1aSAndy Whitcroft# the whole statement. 39062b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 39072b474a1aSAndy Whitcroft if (defined $realline_next && 39082b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 39092b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 39102b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 39112b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 39123cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 39133cbf62dfSAndy Whitcroft # a prefix: 39143cbf62dfSAndy Whitcroft # XXX(foo); 39153cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3916653d4876SAndy Whitcroft my $name = $1; 391787a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 39183cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 39193cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 39203cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 39213cbf62dfSAndy Whitcroft 39223cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 39232b474a1aSAndy Whitcroft \n.}\s*$| 392448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 392548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 392648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 39272b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 39282b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 392948012058SAndy Whitcroft )/x) { 39302b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 39312b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 39322b474a1aSAndy Whitcroft } else { 39332b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 39340a920b5bSAndy Whitcroft } 39350a920b5bSAndy Whitcroft } 39362b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 39372b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 39382b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 39392b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 39402b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 39412b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 39422b474a1aSAndy Whitcroft } 39432b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 39442b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3945000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3946000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 39472b474a1aSAndy Whitcroft } 39480a920b5bSAndy Whitcroft 39495150bda4SJoe Eloff# check for global initialisers. 39506d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3951d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 39526d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3953d5e616fcSJoe Perches $fix) { 39546d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3955d5e616fcSJoe Perches } 3956f0a594c1SAndy Whitcroft } 39570a920b5bSAndy Whitcroft# check for static initialisers. 39586d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3959d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 39606d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3961d5e616fcSJoe Perches $herecurr) && 3962d5e616fcSJoe Perches $fix) { 39636d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3964d5e616fcSJoe Perches } 39650a920b5bSAndy Whitcroft } 39660a920b5bSAndy Whitcroft 39671813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 39681813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 39691813087dSJoe Perches my $tmp = trim($1); 39701813087dSJoe Perches WARN("MISORDERED_TYPE", 39711813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 39721813087dSJoe Perches } 39731813087dSJoe Perches 3974809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long 3975809e082eSJoe Perches while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { 3976809e082eSJoe Perches my $type = trim($1); 3977809e082eSJoe Perches next if ($type !~ /\bint\b/); 3978809e082eSJoe Perches next if ($type !~ /\b(?:short|long\s+long|long)\b/); 3979809e082eSJoe Perches my $new_type = $type; 3980809e082eSJoe Perches $new_type =~ s/\b\s*int\s*\b/ /; 3981809e082eSJoe Perches $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; 3982809e082eSJoe Perches $new_type =~ s/^const\s+//; 3983809e082eSJoe Perches $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); 3984809e082eSJoe Perches $new_type = "const $new_type" if ($type =~ /^const\b/); 3985809e082eSJoe Perches $new_type =~ s/\s+/ /g; 3986809e082eSJoe Perches $new_type = trim($new_type); 3987809e082eSJoe Perches if (WARN("UNNECESSARY_INT", 3988809e082eSJoe Perches "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && 3989809e082eSJoe Perches $fix) { 3990809e082eSJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; 3991809e082eSJoe Perches } 3992809e082eSJoe Perches } 3993809e082eSJoe Perches 3994cb710ecaSJoe Perches# check for static const char * arrays. 3995cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3996000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3997000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3998cb710ecaSJoe Perches $herecurr); 3999cb710ecaSJoe Perches } 4000cb710ecaSJoe Perches 400177b8c0a8SJoe Perches# check for initialized const char arrays that should be static const 400277b8c0a8SJoe Perches if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { 400377b8c0a8SJoe Perches if (WARN("STATIC_CONST_CHAR_ARRAY", 400477b8c0a8SJoe Perches "const array should probably be static const\n" . $herecurr) && 400577b8c0a8SJoe Perches $fix) { 400677b8c0a8SJoe Perches $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; 400777b8c0a8SJoe Perches } 400877b8c0a8SJoe Perches } 400977b8c0a8SJoe Perches 4010cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 4011cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 4012000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 4013000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 4014cb710ecaSJoe Perches $herecurr); 4015cb710ecaSJoe Perches } 4016cb710ecaSJoe Perches 4017ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 4018ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 4019ab7e23f3SJoe Perches my $found = $1; 4020ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 4021ab7e23f3SJoe Perches WARN("CONST_CONST", 4022ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 4023ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 4024ab7e23f3SJoe Perches WARN("CONST_CONST", 4025ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 4026ab7e23f3SJoe Perches } 4027ab7e23f3SJoe Perches } 4028ab7e23f3SJoe Perches 40299b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 40309b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 40319b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 40329b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 40339b0fa60dSJoe Perches $herecurr); 40349b0fa60dSJoe Perches } 40359b0fa60dSJoe Perches 4036b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 4037b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 4038b598b670SJoe Perches my $array = $1; 4039b598b670SJoe 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*\))@) { 4040b598b670SJoe Perches my $array_div = $1; 4041b598b670SJoe Perches if (WARN("ARRAY_SIZE", 4042b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 4043b598b670SJoe Perches $fix) { 4044b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 4045b598b670SJoe Perches } 4046b598b670SJoe Perches } 4047b598b670SJoe Perches } 4048b598b670SJoe Perches 4049b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 4050b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 4051b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 4052b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 4053b36190c5SJoe Perches $fix) { 4054194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 4055b36190c5SJoe Perches } 4056b36190c5SJoe Perches } 4057b36190c5SJoe Perches 4058653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 4059653d4876SAndy Whitcroft# make sense. 4060653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 40618054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 4062c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 40638ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 406446d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 4065000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 4066000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 40670a920b5bSAndy Whitcroft } 40680a920b5bSAndy Whitcroft 40690a920b5bSAndy Whitcroft# * goes on variable not on type 407065863862SAndy Whitcroft # (char*[ const]) 4071bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 4072bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 40733705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 4074d8aaf121SAndy Whitcroft 407565863862SAndy Whitcroft # Should start with a space. 407665863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 407765863862SAndy Whitcroft # Should not end with a space. 407865863862SAndy Whitcroft $to =~ s/\s+$//; 407965863862SAndy Whitcroft # '*'s should not have spaces between. 4080f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 408165863862SAndy Whitcroft } 4082d8aaf121SAndy Whitcroft 40833705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 408465863862SAndy Whitcroft if ($from ne $to) { 40853705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 40863705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 40873705ce5bSJoe Perches $fix) { 40883705ce5bSJoe Perches my $sub_from = $ident; 40893705ce5bSJoe Perches my $sub_to = $ident; 40903705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4091194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40923705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 40933705ce5bSJoe Perches } 409465863862SAndy Whitcroft } 4095bfcb2cc7SAndy Whitcroft } 4096bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 4097bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 40983705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 4099d8aaf121SAndy Whitcroft 410065863862SAndy Whitcroft # Should start with a space. 410165863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 410265863862SAndy Whitcroft # Should not end with a space. 410365863862SAndy Whitcroft $to =~ s/\s+$//; 410465863862SAndy Whitcroft # '*'s should not have spaces between. 4105f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 410665863862SAndy Whitcroft } 410765863862SAndy Whitcroft # Modifiers should have spaces. 410865863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 410965863862SAndy Whitcroft 41103705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 4111667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 41123705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 41133705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 41143705ce5bSJoe Perches $fix) { 41153705ce5bSJoe Perches 41163705ce5bSJoe Perches my $sub_from = $match; 41173705ce5bSJoe Perches my $sub_to = $match; 41183705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 4119194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41203705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 41213705ce5bSJoe Perches } 412265863862SAndy Whitcroft } 41230a920b5bSAndy Whitcroft } 41240a920b5bSAndy Whitcroft 41259d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 41269d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 41270675a8fbSJean Delvare my $msg_level = \&WARN; 41280675a8fbSJean Delvare $msg_level = \&CHK if ($file); 41290675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 41309d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 41319d3e3c70SJoe Perches } 41320a920b5bSAndy Whitcroft 41339d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 41348905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 4135000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 4136000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 41378905a67cSAndy Whitcroft } 41388905a67cSAndy Whitcroft 413917441227SJoe Perches# check for uses of printk_ratelimit 414017441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 4141000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 4142000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 414317441227SJoe Perches } 414417441227SJoe Perches 4145eeef5733SJoe Perches# printk should use KERN_* levels 4146eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 4147000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 4148eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 414900df344fSAndy Whitcroft } 41500a920b5bSAndy Whitcroft 4151243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 4152243f3803SJoe Perches my $orig = $1; 4153243f3803SJoe Perches my $level = lc($orig); 4154243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 41558f26b837SJoe Perches my $level2 = $level; 41568f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 4157243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 4158daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 4159243f3803SJoe Perches } 4160243f3803SJoe Perches 4161dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 4162dc139313SJoe Perches my $orig = $1; 4163dc139313SJoe Perches my $level = lc($orig); 4164dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 4165dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 4166dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 4167dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 4168dc139313SJoe Perches } 4169dc139313SJoe Perches 417091c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 417191c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 417291c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 417391c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 417491c9afafSAndy Lutomirski WARN("ENOSYS", 417591c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 417691c9afafSAndy Lutomirski } 417791c9afafSAndy Lutomirski 4178653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 4179653d4876SAndy Whitcroft# or if closed on same line 41805b57980dSJoe Perches if ($perl_version_ok && 41812d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 41822d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 41832d453e3bSJoe Perches $sline !~ /}/) { 41848d182478SJoe Perches if (ERROR("OPEN_BRACE", 41852d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 41868d182478SJoe Perches $fix) { 41878d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 41888d182478SJoe Perches my $fixed_line = $rawline; 41898d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 41908d182478SJoe Perches my $line1 = $1; 41918d182478SJoe Perches my $line2 = $2; 41928d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 41938d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 41948d182478SJoe Perches if ($line2 !~ /^\s*$/) { 41958d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 41968d182478SJoe Perches } 41978d182478SJoe Perches } 41980a920b5bSAndy Whitcroft } 4199653d4876SAndy Whitcroft 42008905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 42018905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 42028905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 42038d182478SJoe Perches if (ERROR("OPEN_BRACE", 42048d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 42058d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 42068d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 42078d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 42088d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 42098d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42108d182478SJoe Perches $fixedline = $rawline; 42118d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 42128d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 42138d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42148d182478SJoe Perches } 42158d182478SJoe Perches } 42168905a67cSAndy Whitcroft } 42178905a67cSAndy Whitcroft 42180c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 42193705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 42203705ce5bSJoe Perches if (WARN("SPACING", 42213705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 42223705ce5bSJoe Perches $fix) { 4223194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42243705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 42253705ce5bSJoe Perches } 42260c73b4ebSAndy Whitcroft } 42270c73b4ebSAndy Whitcroft 422831070b5dSJoe Perches# Function pointer declarations 422931070b5dSJoe Perches# check spacing between type, funcptr, and args 423031070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 423191f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 423231070b5dSJoe Perches my $declare = $1; 423331070b5dSJoe Perches my $pre_pointer_space = $2; 423431070b5dSJoe Perches my $post_pointer_space = $3; 423531070b5dSJoe Perches my $funcname = $4; 423631070b5dSJoe Perches my $post_funcname_space = $5; 423731070b5dSJoe Perches my $pre_args_space = $6; 423831070b5dSJoe Perches 423991f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 424091f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 424191f72e9cSJoe Perches# don't need a space so don't warn for those. 424291f72e9cSJoe Perches my $post_declare_space = ""; 424391f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 424491f72e9cSJoe Perches $post_declare_space = $1; 424591f72e9cSJoe Perches $declare = rtrim($declare); 424691f72e9cSJoe Perches } 424791f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 424831070b5dSJoe Perches WARN("SPACING", 424931070b5dSJoe Perches "missing space after return type\n" . $herecurr); 425091f72e9cSJoe Perches $post_declare_space = " "; 425131070b5dSJoe Perches } 425231070b5dSJoe Perches 425331070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 425491f72e9cSJoe Perches# This test is not currently implemented because these declarations are 425591f72e9cSJoe Perches# equivalent to 425691f72e9cSJoe Perches# int foo(int bar, ...) 425791f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 425891f72e9cSJoe Perches# 425991f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 426091f72e9cSJoe Perches# WARN("SPACING", 426191f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 426291f72e9cSJoe Perches# } 426331070b5dSJoe Perches 426431070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 426531070b5dSJoe Perches if (defined $pre_pointer_space && 426631070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 426731070b5dSJoe Perches WARN("SPACING", 426831070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 426931070b5dSJoe Perches } 427031070b5dSJoe Perches 427131070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 427231070b5dSJoe Perches if (defined $post_pointer_space && 427331070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 427431070b5dSJoe Perches WARN("SPACING", 427531070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 427631070b5dSJoe Perches } 427731070b5dSJoe Perches 427831070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 427931070b5dSJoe Perches if (defined $post_funcname_space && 428031070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 428131070b5dSJoe Perches WARN("SPACING", 428231070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 428331070b5dSJoe Perches } 428431070b5dSJoe Perches 428531070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 428631070b5dSJoe Perches if (defined $pre_args_space && 428731070b5dSJoe Perches $pre_args_space =~ /^\s/) { 428831070b5dSJoe Perches WARN("SPACING", 428931070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 429031070b5dSJoe Perches } 429131070b5dSJoe Perches 429231070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4293194f66fcSJoe Perches $fixed[$fixlinenr] =~ 429491f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 429531070b5dSJoe Perches } 429631070b5dSJoe Perches } 429731070b5dSJoe Perches 42988d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 42998d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4300fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4301fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 43028d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 43038d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 43048d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4305fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 430638dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 43073705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 43083705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 43093705ce5bSJoe Perches $fix) { 4310194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43113705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 43123705ce5bSJoe Perches } 43138d31cfceSAndy Whitcroft } 43148d31cfceSAndy Whitcroft } 43158d31cfceSAndy Whitcroft 4316f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 43176c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4318c2fdda0dSAndy Whitcroft my $name = $1; 4319773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4320773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4321c2fdda0dSAndy Whitcroft 4322c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4323773647a0SAndy Whitcroft if ($name =~ /^(?: 4324773647a0SAndy Whitcroft if|for|while|switch|return|case| 4325773647a0SAndy Whitcroft volatile|__volatile__| 4326773647a0SAndy Whitcroft __attribute__|format|__extension__| 4327773647a0SAndy Whitcroft asm|__asm__)$/x) 4328773647a0SAndy Whitcroft { 4329c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4330c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4331c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4332c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4333773647a0SAndy Whitcroft 4334773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4335c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4336c2fdda0dSAndy Whitcroft 4337c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4338c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4339773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4340c2fdda0dSAndy Whitcroft 4341c2fdda0dSAndy Whitcroft } else { 43423705ce5bSJoe Perches if (WARN("SPACING", 43433705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 43443705ce5bSJoe Perches $fix) { 4345194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43463705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 43473705ce5bSJoe Perches } 4348f0a594c1SAndy Whitcroft } 43496c72ffaaSAndy Whitcroft } 43509a4cad4eSEric Nelson 4351653d4876SAndy Whitcroft# Check operator spacing. 43520a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 43533705ce5bSJoe Perches my $fixed_line = ""; 43543705ce5bSJoe Perches my $line_fixed = 0; 43553705ce5bSJoe Perches 43569c0ca6f9SAndy Whitcroft my $ops = qr{ 43579c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 43589c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 43599c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 43601f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 436184731623SJoe Perches \?:|\?|: 43629c0ca6f9SAndy Whitcroft }x; 4363cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 43643705ce5bSJoe Perches 43653705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 43663705ce5bSJoe Perches## foreach my $el (@elements) { 43673705ce5bSJoe Perches## print("el: <$el>\n"); 43683705ce5bSJoe Perches## } 43693705ce5bSJoe Perches 43703705ce5bSJoe Perches my @fix_elements = (); 437100df344fSAndy Whitcroft my $off = 0; 43726c72ffaaSAndy Whitcroft 43733705ce5bSJoe Perches foreach my $el (@elements) { 43743705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 43753705ce5bSJoe Perches $off += length($el); 43763705ce5bSJoe Perches } 43773705ce5bSJoe Perches 43783705ce5bSJoe Perches $off = 0; 43793705ce5bSJoe Perches 43806c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4381b34c648bSJoe Perches my $last_after = -1; 43826c72ffaaSAndy Whitcroft 43830a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 43843705ce5bSJoe Perches 43853705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 43863705ce5bSJoe Perches 43873705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 43883705ce5bSJoe Perches 43894a0df2efSAndy Whitcroft $off += length($elements[$n]); 43904a0df2efSAndy Whitcroft 439125985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4392773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4393773647a0SAndy Whitcroft my $cc = ''; 4394773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4395773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4396773647a0SAndy Whitcroft } 4397773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4398773647a0SAndy Whitcroft 43994a0df2efSAndy Whitcroft my $a = ''; 44004a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 44014a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4402cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 44034a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 44044a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4405773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 44064a0df2efSAndy Whitcroft 44070a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 44084a0df2efSAndy Whitcroft 44094a0df2efSAndy Whitcroft my $c = ''; 44100a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 44114a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 44124a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4413cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 44144a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 44154a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 44168b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 44174a0df2efSAndy Whitcroft } else { 44184a0df2efSAndy Whitcroft $c = 'E'; 44190a920b5bSAndy Whitcroft } 44200a920b5bSAndy Whitcroft 44214a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 44224a0df2efSAndy Whitcroft 44234a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 44244a0df2efSAndy Whitcroft 44256c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4426de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 44270a920b5bSAndy Whitcroft 442874048ed8SAndy Whitcroft # Pull out the value of this operator. 44296c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 44300a920b5bSAndy Whitcroft 44311f65f947SAndy Whitcroft # Get the full operator variant. 44321f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 44331f65f947SAndy Whitcroft 443413214adfSAndy Whitcroft # Ignore operators passed as parameters. 443513214adfSAndy Whitcroft if ($op_type ne 'V' && 4436d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 443713214adfSAndy Whitcroft 4438cf655043SAndy Whitcroft# # Ignore comments 4439cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 444013214adfSAndy Whitcroft 4441d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 444213214adfSAndy Whitcroft } elsif ($op eq ';') { 4443cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4444cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 44453705ce5bSJoe Perches if (ERROR("SPACING", 44463705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4447b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 44483705ce5bSJoe Perches $line_fixed = 1; 44493705ce5bSJoe Perches } 4450d8aaf121SAndy Whitcroft } 4451d8aaf121SAndy Whitcroft 4452d8aaf121SAndy Whitcroft # // is a comment 4453d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 44540a920b5bSAndy Whitcroft 4455b00e4814SJoe Perches # : when part of a bitfield 4456b00e4814SJoe Perches } elsif ($opv eq ':B') { 4457b00e4814SJoe Perches # skip the bitfield test for now 4458b00e4814SJoe Perches 44591f65f947SAndy Whitcroft # No spaces for: 44601f65f947SAndy Whitcroft # -> 4461b00e4814SJoe Perches } elsif ($op eq '->') { 44624a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 44633705ce5bSJoe Perches if (ERROR("SPACING", 44643705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4465b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44663705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 44673705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 44683705ce5bSJoe Perches } 4469b34c648bSJoe Perches $line_fixed = 1; 44703705ce5bSJoe Perches } 44710a920b5bSAndy Whitcroft } 44720a920b5bSAndy Whitcroft 44732381097bSJoe Perches # , must not have a space before and must have a space on the right. 44740a920b5bSAndy Whitcroft } elsif ($op eq ',') { 44752381097bSJoe Perches my $rtrim_before = 0; 44762381097bSJoe Perches my $space_after = 0; 44772381097bSJoe Perches if ($ctx =~ /Wx./) { 44782381097bSJoe Perches if (ERROR("SPACING", 44792381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 44802381097bSJoe Perches $line_fixed = 1; 44812381097bSJoe Perches $rtrim_before = 1; 44822381097bSJoe Perches } 44832381097bSJoe Perches } 4484cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 44853705ce5bSJoe Perches if (ERROR("SPACING", 44863705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 44873705ce5bSJoe Perches $line_fixed = 1; 4488b34c648bSJoe Perches $last_after = $n; 44892381097bSJoe Perches $space_after = 1; 44902381097bSJoe Perches } 44912381097bSJoe Perches } 44922381097bSJoe Perches if ($rtrim_before || $space_after) { 44932381097bSJoe Perches if ($rtrim_before) { 44942381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 44952381097bSJoe Perches } else { 44962381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 44972381097bSJoe Perches } 44982381097bSJoe Perches if ($space_after) { 44992381097bSJoe Perches $good .= " "; 45003705ce5bSJoe Perches } 45010a920b5bSAndy Whitcroft } 45020a920b5bSAndy Whitcroft 45039c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 450474048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 45059c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 45069c0ca6f9SAndy Whitcroft 45079c0ca6f9SAndy Whitcroft # unary operators should have a space before and 45089c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 45099c0ca6f9SAndy Whitcroft # unary operator, or a cast 45109c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 451174048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 45120d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4513cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 45143705ce5bSJoe Perches if (ERROR("SPACING", 45153705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4516b34c648bSJoe Perches if ($n != $last_after + 2) { 4517b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 45183705ce5bSJoe Perches $line_fixed = 1; 45193705ce5bSJoe Perches } 45200a920b5bSAndy Whitcroft } 4521b34c648bSJoe Perches } 4522a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4523171ae1a4SAndy Whitcroft # A unary '*' may be const 4524171ae1a4SAndy Whitcroft 4525171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 45263705ce5bSJoe Perches if (ERROR("SPACING", 45273705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4528b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 45293705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45303705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 45313705ce5bSJoe Perches } 4532b34c648bSJoe Perches $line_fixed = 1; 45333705ce5bSJoe Perches } 45340a920b5bSAndy Whitcroft } 45350a920b5bSAndy Whitcroft 45360a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 45370a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4538773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 45393705ce5bSJoe Perches if (ERROR("SPACING", 45403705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4541b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 45423705ce5bSJoe Perches $line_fixed = 1; 45433705ce5bSJoe Perches } 45440a920b5bSAndy Whitcroft } 4545773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4546773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 45473705ce5bSJoe Perches if (ERROR("SPACING", 45483705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4549b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 45503705ce5bSJoe Perches $line_fixed = 1; 45513705ce5bSJoe Perches } 4552653d4876SAndy Whitcroft } 4553773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 45543705ce5bSJoe Perches if (ERROR("SPACING", 45553705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4556b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 45573705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 45583705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4559773647a0SAndy Whitcroft } 4560b34c648bSJoe Perches $line_fixed = 1; 45613705ce5bSJoe Perches } 45623705ce5bSJoe Perches } 45630a920b5bSAndy Whitcroft 45640a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 45659c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 45669c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 45679c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4568c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4569c2fdda0dSAndy Whitcroft $op eq '%') 45700a920b5bSAndy Whitcroft { 4571d2e025f3SJoe Perches if ($check) { 4572d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4573d2e025f3SJoe Perches if (CHK("SPACING", 4574d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4575d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4576d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4577d2e025f3SJoe Perches $line_fixed = 1; 4578d2e025f3SJoe Perches } 4579d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4580d2e025f3SJoe Perches if (CHK("SPACING", 4581d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4582d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4583d2e025f3SJoe Perches $line_fixed = 1; 4584d2e025f3SJoe Perches } 4585d2e025f3SJoe Perches } 4586d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 45873705ce5bSJoe Perches if (ERROR("SPACING", 45883705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4589b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4590b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4591b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4592b34c648bSJoe Perches } 45933705ce5bSJoe Perches $line_fixed = 1; 45943705ce5bSJoe Perches } 45950a920b5bSAndy Whitcroft } 45960a920b5bSAndy Whitcroft 45971f65f947SAndy Whitcroft # A colon needs no spaces before when it is 45981f65f947SAndy Whitcroft # terminating a case value or a label. 45991f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 46001f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 46013705ce5bSJoe Perches if (ERROR("SPACING", 46023705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4603b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 46043705ce5bSJoe Perches $line_fixed = 1; 46053705ce5bSJoe Perches } 46061f65f947SAndy Whitcroft } 46071f65f947SAndy Whitcroft 46080a920b5bSAndy Whitcroft # All the others need spaces both sides. 4609cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 46101f65f947SAndy Whitcroft my $ok = 0; 46111f65f947SAndy Whitcroft 461222f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 46131f65f947SAndy Whitcroft if (($op eq '<' && 46141f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 46151f65f947SAndy Whitcroft ($op eq '>' && 46161f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 46171f65f947SAndy Whitcroft { 46181f65f947SAndy Whitcroft $ok = 1; 46191f65f947SAndy Whitcroft } 46201f65f947SAndy Whitcroft 4621e0df7e1fSJoe Perches # for asm volatile statements 4622e0df7e1fSJoe Perches # ignore a colon with another 4623e0df7e1fSJoe Perches # colon immediately before or after 4624e0df7e1fSJoe Perches if (($op eq ':') && 4625e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4626e0df7e1fSJoe Perches $ok = 1; 4627e0df7e1fSJoe Perches } 4628e0df7e1fSJoe Perches 462984731623SJoe Perches # messages are ERROR, but ?: are CHK 46301f65f947SAndy Whitcroft if ($ok == 0) { 46310675a8fbSJean Delvare my $msg_level = \&ERROR; 46320675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 463384731623SJoe Perches 46340675a8fbSJean Delvare if (&{$msg_level}("SPACING", 46353705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4636b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4637b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4638b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4639b34c648bSJoe Perches } 46403705ce5bSJoe Perches $line_fixed = 1; 46413705ce5bSJoe Perches } 46420a920b5bSAndy Whitcroft } 464322f2a2efSAndy Whitcroft } 46444a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 46453705ce5bSJoe Perches 46463705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 46473705ce5bSJoe Perches 46483705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 46490a920b5bSAndy Whitcroft } 46503705ce5bSJoe Perches 46513705ce5bSJoe Perches if (($#elements % 2) == 0) { 46523705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 46533705ce5bSJoe Perches } 46543705ce5bSJoe Perches 4655194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4656194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 46573705ce5bSJoe Perches } 46583705ce5bSJoe Perches 46593705ce5bSJoe Perches 46600a920b5bSAndy Whitcroft } 46610a920b5bSAndy Whitcroft 4662786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4663d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4664786b6326SJoe Perches if (WARN("SPACING", 4665786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4666786b6326SJoe Perches $fix) { 4667194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4668786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4669786b6326SJoe Perches } 4670786b6326SJoe Perches } 4671786b6326SJoe Perches 4672f0a594c1SAndy Whitcroft# check for multiple assignments 4673f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4674000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4675000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4676f0a594c1SAndy Whitcroft } 4677f0a594c1SAndy Whitcroft 467822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 467922f2a2efSAndy Whitcroft## # continuation. 468022f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 468122f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 468222f2a2efSAndy Whitcroft## 468322f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 468422f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 468522f2a2efSAndy Whitcroft## my $ln = $line; 468622f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 468722f2a2efSAndy Whitcroft## } 468822f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4689000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4690000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 469122f2a2efSAndy Whitcroft## } 469222f2a2efSAndy Whitcroft## } 4693f0a594c1SAndy Whitcroft 46940a920b5bSAndy Whitcroft#need space before brace following if, while, etc 46956b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 46966ad724e2SMichal Zylowski $line =~ /\b(?:else|do)\{/) { 46973705ce5bSJoe Perches if (ERROR("SPACING", 46983705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 46993705ce5bSJoe Perches $fix) { 47006ad724e2SMichal Zylowski $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; 47013705ce5bSJoe Perches } 4702de7d4f0eSAndy Whitcroft } 4703de7d4f0eSAndy Whitcroft 4704c4a62ef9SJoe Perches## # check for blank lines before declarations 4705c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4706c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4707c4a62ef9SJoe Perches## WARN("SPACING", 4708c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4709c4a62ef9SJoe Perches## } 4710c4a62ef9SJoe Perches## 4711c4a62ef9SJoe Perches 4712de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4713de7d4f0eSAndy Whitcroft# on the line 471494fb9845SJoe Perches if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { 4715d5e616fcSJoe Perches if (ERROR("SPACING", 4716d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4717d5e616fcSJoe Perches $fix) { 4718194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4719d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4720d5e616fcSJoe Perches } 47210a920b5bSAndy Whitcroft } 47220a920b5bSAndy Whitcroft 472322f2a2efSAndy Whitcroft# check spacing on square brackets 472422f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 47253705ce5bSJoe Perches if (ERROR("SPACING", 47263705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 47273705ce5bSJoe Perches $fix) { 4728194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47293705ce5bSJoe Perches s/\[\s+/\[/; 47303705ce5bSJoe Perches } 473122f2a2efSAndy Whitcroft } 473222f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 47333705ce5bSJoe Perches if (ERROR("SPACING", 47343705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 47353705ce5bSJoe Perches $fix) { 4736194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47373705ce5bSJoe Perches s/\s+\]/\]/; 47383705ce5bSJoe Perches } 473922f2a2efSAndy Whitcroft } 474022f2a2efSAndy Whitcroft 4741c45dcabdSAndy Whitcroft# check spacing on parentheses 47429c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 47439c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 47443705ce5bSJoe Perches if (ERROR("SPACING", 47453705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 47463705ce5bSJoe Perches $fix) { 4747194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47483705ce5bSJoe Perches s/\(\s+/\(/; 47493705ce5bSJoe Perches } 475022f2a2efSAndy Whitcroft } 475113214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4752c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4753c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 47543705ce5bSJoe Perches if (ERROR("SPACING", 47553705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 47563705ce5bSJoe Perches $fix) { 4757194f66fcSJoe Perches $fixed[$fixlinenr] =~ 47583705ce5bSJoe Perches s/\s+\)/\)/; 47593705ce5bSJoe Perches } 476022f2a2efSAndy Whitcroft } 476122f2a2efSAndy Whitcroft 4762e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4763e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4764e2826fd0SJoe Perches 4765e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4766ea4acbb1SJoe Perches my $var = $1; 4767ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4768ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4769ea4acbb1SJoe Perches $fix) { 4770ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4771ea4acbb1SJoe Perches } 4772ea4acbb1SJoe Perches } 4773ea4acbb1SJoe Perches 4774ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4775ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4776ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4777ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4778ea4acbb1SJoe Perches my $var = $2; 4779ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4780ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4781ea4acbb1SJoe Perches $fix) { 4782ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4783ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4784ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4785ea4acbb1SJoe Perches } 4786e2826fd0SJoe Perches } 4787e2826fd0SJoe Perches 478863b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4789a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4790a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 47915b57980dSJoe Perches $perl_version_ok && defined($stat) && 479263b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 479363b7c73eSJoe Perches my $if_stat = $1; 479463b7c73eSJoe Perches my $test = substr($2, 1, -1); 479563b7c73eSJoe Perches my $herectx; 479663b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 479763b7c73eSJoe Perches my $match = $1; 479863b7c73eSJoe Perches # avoid parentheses around potential macro args 479963b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 480063b7c73eSJoe Perches if (!defined($herectx)) { 480163b7c73eSJoe Perches $herectx = $here . "\n"; 480263b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 480363b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 480463b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 480563b7c73eSJoe Perches $herectx .= $rl . "\n"; 480663b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 480763b7c73eSJoe Perches } 480863b7c73eSJoe Perches } 480963b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 481063b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 481163b7c73eSJoe Perches } 481263b7c73eSJoe Perches } 481363b7c73eSJoe Perches 48140a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 48154a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 48160a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 48173705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 48183705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 48193705ce5bSJoe Perches $fix) { 4820194f66fcSJoe Perches $fixed[$fixlinenr] =~ 48213705ce5bSJoe Perches s/^(.)\s+/$1/; 48223705ce5bSJoe Perches } 48230a920b5bSAndy Whitcroft } 48240a920b5bSAndy Whitcroft 48255b9553abSJoe Perches# return is not a function 4826507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4827c45dcabdSAndy Whitcroft my $spacing = $1; 48285b57980dSJoe Perches if ($perl_version_ok && 48295b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 48305b9553abSJoe Perches my $value = $1; 48315b9553abSJoe Perches $value = deparenthesize($value); 48325b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4833000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4834000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 48355b9553abSJoe Perches } 4836c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4837000d1cc1SJoe Perches ERROR("SPACING", 4838000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4839c45dcabdSAndy Whitcroft } 4840c45dcabdSAndy Whitcroft } 4841507e5141SJoe Perches 4842b43ae21bSJoe Perches# unnecessary return in a void function 4843b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4844b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4845b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4846b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4847b43ae21bSJoe Perches $linenr >= 3 && 4848b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4849b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 48509819cf25SJoe Perches WARN("RETURN_VOID", 4851b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 48529819cf25SJoe Perches } 48539819cf25SJoe Perches 4854189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 48555b57980dSJoe Perches if ($perl_version_ok && 4856189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4857189248d8SJoe Perches my $openparens = $1; 4858189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4859189248d8SJoe Perches my $msg = ""; 4860189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4861189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4862189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4863189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4864189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4865189248d8SJoe Perches } 4866189248d8SJoe Perches } 4867189248d8SJoe Perches 4868c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4869c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4870c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4871c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 48725b57980dSJoe Perches if ($perl_version_ok && 4873c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4874c5595fa2SJoe Perches my $lead = $1; 4875c5595fa2SJoe Perches my $const = $2; 4876c5595fa2SJoe Perches my $comp = $3; 4877c5595fa2SJoe Perches my $to = $4; 4878c5595fa2SJoe Perches my $newcomp = $comp; 4879f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4880c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4881c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4882c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4883c5595fa2SJoe Perches $fix) { 4884c5595fa2SJoe Perches if ($comp eq "<") { 4885c5595fa2SJoe Perches $newcomp = ">"; 4886c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4887c5595fa2SJoe Perches $newcomp = ">="; 4888c5595fa2SJoe Perches } elsif ($comp eq ">") { 4889c5595fa2SJoe Perches $newcomp = "<"; 4890c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4891c5595fa2SJoe Perches $newcomp = "<="; 4892c5595fa2SJoe Perches } 4893c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4894c5595fa2SJoe Perches } 4895c5595fa2SJoe Perches } 4896c5595fa2SJoe Perches 4897f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4898f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 489953a3c448SAndy Whitcroft my $name = $1; 490053a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4901000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4902f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 490353a3c448SAndy Whitcroft } 490453a3c448SAndy Whitcroft } 4905c45dcabdSAndy Whitcroft 49060a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 49074a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 49083705ce5bSJoe Perches if (ERROR("SPACING", 49093705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 49103705ce5bSJoe Perches $fix) { 4911194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49123705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 49133705ce5bSJoe Perches } 49140a920b5bSAndy Whitcroft } 49150a920b5bSAndy Whitcroft 4916f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4917f5fe35ddSAndy Whitcroft# statements after the conditional. 4918170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 49193e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 49203e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 49213e469cdcSAndy Whitcroft if (!defined $stat); 4922170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4923170d3a22SAndy Whitcroft $remain_next, $off_next); 4924170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4925170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4926170d3a22SAndy Whitcroft 4927170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4928170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4929170d3a22SAndy Whitcroft # then count those as offsets. 4930170d3a22SAndy Whitcroft my ($whitespace) = 4931170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4932170d3a22SAndy Whitcroft my $offset = 4933170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4934170d3a22SAndy Whitcroft 4935170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4936170d3a22SAndy Whitcroft $offset} = 1; 4937170d3a22SAndy Whitcroft } 4938170d3a22SAndy Whitcroft } 4939170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4940c11230f4SJoe Perches defined($stat) && defined($cond) && 4941170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4942171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 49438905a67cSAndy Whitcroft 4944b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4945000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4946000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 49478905a67cSAndy Whitcroft } 49488905a67cSAndy Whitcroft 49498905a67cSAndy Whitcroft # Find out what is on the end of the line after the 49508905a67cSAndy Whitcroft # conditional. 4951773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 49528905a67cSAndy Whitcroft $s =~ s/\n.*//g; 495313214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 495453210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 495553210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4956773647a0SAndy Whitcroft { 4957bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4958bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4959bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 496042bdf74cSHidetoshi Seto my $stat_real = ''; 4961bb44ad39SAndy Whitcroft 496242bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 496342bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4964bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4965bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4966bb44ad39SAndy Whitcroft } 4967bb44ad39SAndy Whitcroft 4968000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4969000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 49708905a67cSAndy Whitcroft } 49718905a67cSAndy Whitcroft } 49728905a67cSAndy Whitcroft 497313214adfSAndy Whitcroft# Check for bitwise tests written as boolean 497413214adfSAndy Whitcroft if ($line =~ / 497513214adfSAndy Whitcroft (?: 497613214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 497713214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 497813214adfSAndy Whitcroft (?:\&\&|\|\|) 497913214adfSAndy Whitcroft | 498013214adfSAndy Whitcroft (?:\&\&|\|\|) 498113214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 498213214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 498313214adfSAndy Whitcroft )/x) 498413214adfSAndy Whitcroft { 4985000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4986000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 498713214adfSAndy Whitcroft } 498813214adfSAndy Whitcroft 49898905a67cSAndy Whitcroft# if and else should not have general statements after it 499013214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 499113214adfSAndy Whitcroft my $s = $1; 499213214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 499313214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4994000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4995000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 49960a920b5bSAndy Whitcroft } 499713214adfSAndy Whitcroft } 499839667782SAndy Whitcroft# if should not continue a brace 499939667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 5000000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5001048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 500239667782SAndy Whitcroft $herecurr); 500339667782SAndy Whitcroft } 5004a1080bf8SAndy Whitcroft# case and default should not have general statements after them 5005a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 5006a1080bf8SAndy Whitcroft $line !~ /\G(?: 50073fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 5008a1080bf8SAndy Whitcroft \s*return\s+ 5009a1080bf8SAndy Whitcroft )/xg) 5010a1080bf8SAndy Whitcroft { 5011000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 5012000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 5013a1080bf8SAndy Whitcroft } 50140a920b5bSAndy Whitcroft 50150a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 50160a920b5bSAndy Whitcroft # indent level to be relevant to each other. 50178b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 50180a920b5bSAndy Whitcroft $previndent == $indent) { 50198b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 50208b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 50218b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 50228b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 50238b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 50248b8856f4SJoe Perches my $fixedline = $prevrawline; 50258b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 50268b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 50278b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50288b8856f4SJoe Perches } 50298b8856f4SJoe Perches $fixedline = $rawline; 50308b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 50318b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50328b8856f4SJoe Perches } 50330a920b5bSAndy Whitcroft } 50340a920b5bSAndy Whitcroft 50358b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 5036c2fdda0dSAndy Whitcroft $previndent == $indent) { 5037c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 5038c2fdda0dSAndy Whitcroft 5039c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 5040c2fdda0dSAndy Whitcroft # conditional. 5041773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 5042c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 5043c2fdda0dSAndy Whitcroft 5044c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 50458b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 50468b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 50478b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 50488b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 50498b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 50508b8856f4SJoe Perches my $fixedline = $prevrawline; 50518b8856f4SJoe Perches my $trailing = $rawline; 50528b8856f4SJoe Perches $trailing =~ s/^\+//; 50538b8856f4SJoe Perches $trailing = trim($trailing); 50548b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 50558b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 50568b8856f4SJoe Perches } 5057c2fdda0dSAndy Whitcroft } 5058c2fdda0dSAndy Whitcroft } 5059c2fdda0dSAndy Whitcroft 506095e2c602SJoe Perches#Specific variable tests 5061323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 5062323c1260SJoe Perches my $var = $1; 506395e2c602SJoe Perches 506495e2c602SJoe Perches#CamelCase 5065807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 5066be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 506722735ce8SJoe Perches#Ignore Page<foo> variants 5068807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 5069d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB 5070d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) 5071d439e6a5SJoe Perches $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && 5072f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 5073f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 50747e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 50757e781f67SJoe Perches my $word = $1; 50767e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 5077d8b07710SJoe Perches if ($check) { 5078d8b07710SJoe Perches seed_camelcase_includes(); 5079d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 5080d8b07710SJoe Perches seed_camelcase_file($realfile); 5081d8b07710SJoe Perches $camelcase_file_seeded = 1; 5082d8b07710SJoe Perches } 5083d8b07710SJoe Perches } 50847e781f67SJoe Perches if (!defined $camelcase{$word}) { 50857e781f67SJoe Perches $camelcase{$word} = 1; 5086be79794bSJoe Perches CHK("CAMELCASE", 50877e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 50887e781f67SJoe Perches } 5089323c1260SJoe Perches } 5090323c1260SJoe Perches } 50913445686aSJoe Perches } 50920a920b5bSAndy Whitcroft 50930a920b5bSAndy Whitcroft#no spaces allowed after \ in define 5094d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 5095d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 5096d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 5097d5e616fcSJoe Perches $fix) { 5098194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 5099d5e616fcSJoe Perches } 51000a920b5bSAndy Whitcroft } 51010a920b5bSAndy Whitcroft 51020e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 51030e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 5104c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 5105e09dec48SAndy Whitcroft my $file = "$1.h"; 5106e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 5107e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 5108e09dec48SAndy Whitcroft $realfile ne $checkfile && 51097840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 5110c45dcabdSAndy Whitcroft { 51110e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 51120e212e0aSFabian Frederick if ($asminclude > 0) { 5113e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 5114000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 5115000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5116e09dec48SAndy Whitcroft } else { 5117000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 5118000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 5119e09dec48SAndy Whitcroft } 51200a920b5bSAndy Whitcroft } 51210a920b5bSAndy Whitcroft } 51220e212e0aSFabian Frederick } 51230a920b5bSAndy Whitcroft 5124653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 5125653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 5126cf655043SAndy Whitcroft# in a known good container 5127b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 5128b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 5129d8aaf121SAndy Whitcroft my $ln = $linenr; 5130d8aaf121SAndy Whitcroft my $cnt = $realcnt; 5131c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 5132c45dcabdSAndy Whitcroft my $ctx = ''; 513308a2843eSJoe Perches my $has_flow_statement = 0; 513408a2843eSJoe Perches my $has_arg_concat = 0; 5135c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 5136f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 5137f74bd194SAndy Whitcroft $ctx = $dstat; 5138c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 5139a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 5140c45dcabdSAndy Whitcroft 514108a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 514262e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 514308a2843eSJoe Perches 5144f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 5145f59b64bfSJoe Perches my $define_args = $1; 5146f59b64bfSJoe Perches my $define_stmt = $dstat; 5147f59b64bfSJoe Perches my @def_args = (); 5148f59b64bfSJoe Perches 5149f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 5150f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 5151f59b64bfSJoe Perches $define_args =~ s/\s*//g; 51528c8c45cfSJoe Perches $define_args =~ s/\\\+?//g; 5153f59b64bfSJoe Perches @def_args = split(",", $define_args); 5154f59b64bfSJoe Perches } 5155f59b64bfSJoe Perches 5156292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 5157c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 5158c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 5159c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 5160c45dcabdSAndy Whitcroft 5161c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 5162bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 5163bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 51646b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 5165bf30d6edSAndy Whitcroft { 5166c45dcabdSAndy Whitcroft } 5167c45dcabdSAndy Whitcroft 5168*342d3d2fSAntonio Borneo # Flatten any obvious string concatenation. 516933acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 517033acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 5171e45bab8eSAndy Whitcroft { 5172e45bab8eSAndy Whitcroft } 5173e45bab8eSAndy Whitcroft 517442e15293SJoe Perches # Make asm volatile uses seem like a generic function 517542e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 517642e15293SJoe Perches 5177c45dcabdSAndy Whitcroft my $exceptions = qr{ 5178c45dcabdSAndy Whitcroft $Declare| 5179c45dcabdSAndy Whitcroft module_param_named| 5180a0a0a7a9SKees Cook MODULE_PARM_DESC| 5181c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 5182c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 5183383099fdSAndy Whitcroft __typeof__\(| 518422fd2d3eSStefani Seibold union| 518522fd2d3eSStefani Seibold struct| 5186ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 51876b10df42SVladimir Zapolskiy ^\"|\"$| 51886b10df42SVladimir Zapolskiy ^\[ 5189c45dcabdSAndy Whitcroft }x; 51905eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 5191f59b64bfSJoe Perches 5192f59b64bfSJoe Perches $ctx =~ s/\n*$//; 5193f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 5194e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 5195f59b64bfSJoe Perches 5196f74bd194SAndy Whitcroft if ($dstat ne '' && 5197f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 5198f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 51993cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 5200356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 5201f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 5202f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 5203e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 520472f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5205f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5206f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5207f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 52084e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5209f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5210c45dcabdSAndy Whitcroft { 5211e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5212e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5213e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5214e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5215f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5216f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5217f74bd194SAndy Whitcroft } else { 5218000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5219388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5220d8aaf121SAndy Whitcroft } 5221f59b64bfSJoe Perches 5222f59b64bfSJoe Perches } 52235207649bSJoe Perches 52245207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 52255207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 52265207649bSJoe Perches my $first = 1; 52275207649bSJoe Perches $define_stmt = ""; 52285207649bSJoe Perches foreach my $l (@stmt_array) { 52295207649bSJoe Perches $l =~ s/\\$//; 52305207649bSJoe Perches if ($first) { 52315207649bSJoe Perches $define_stmt = $l; 52325207649bSJoe Perches $first = 0; 52335207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 52345207649bSJoe Perches $define_stmt .= substr($l, 1); 52355207649bSJoe Perches } 52365207649bSJoe Perches } 52375207649bSJoe Perches $define_stmt =~ s/$;//g; 52385207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 52395207649bSJoe Perches $define_stmt = trim($define_stmt); 52405207649bSJoe Perches 5241f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5242f59b64bfSJoe Perches foreach my $arg (@def_args) { 5243f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 52449192d41aSJoe Perches next if ($arg =~ /^type$/i); 52457fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 52466dba824eSBrendan Jackman $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 52477fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 52487fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5249d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5250f59b64bfSJoe Perches if ($use_cnt > 1) { 5251f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5252f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5253f59b64bfSJoe Perches } 52549192d41aSJoe Perches# check if any macro arguments may have other precedence issues 52557fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 52569192d41aSJoe Perches ((defined($1) && $1 ne ',') || 52579192d41aSJoe Perches (defined($2) && $2 ne ','))) { 52589192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 52599192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 52609192d41aSJoe Perches } 52610a920b5bSAndy Whitcroft } 52625023d347SJoe Perches 526308a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 526408a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 526508a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 526608a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5267e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 526808a2843eSJoe Perches 526908a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 527008a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 527108a2843eSJoe Perches } 527208a2843eSJoe Perches 5273481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 52745023d347SJoe Perches 52755023d347SJoe Perches } else { 52765023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5277481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5278481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 52795023d347SJoe Perches $line =~ /^\+.*\\$/) { 52805023d347SJoe Perches WARN("LINE_CONTINUATIONS", 52815023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 52825023d347SJoe Perches } 5283653d4876SAndy Whitcroft } 52840a920b5bSAndy Whitcroft 5285b13edf7fSJoe Perches# do {} while (0) macro tests: 5286b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5287b13edf7fSJoe Perches# macro should not end with a semicolon 52885b57980dSJoe Perches if ($perl_version_ok && 5289b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5290b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5291b13edf7fSJoe Perches my $ln = $linenr; 5292b13edf7fSJoe Perches my $cnt = $realcnt; 5293b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5294b13edf7fSJoe Perches my $ctx = ''; 5295b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5296b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5297b13edf7fSJoe Perches $ctx = $dstat; 5298b13edf7fSJoe Perches 5299b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 53001b36b201SJoe Perches $dstat =~ s/$;/ /g; 5301b13edf7fSJoe Perches 5302b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5303b13edf7fSJoe Perches my $stmts = $2; 5304b13edf7fSJoe Perches my $semis = $3; 5305b13edf7fSJoe Perches 5306b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5307b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5308e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5309b13edf7fSJoe Perches 5310ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5311ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5312b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5313b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5314b13edf7fSJoe Perches } 5315b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5316b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5317b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5318b13edf7fSJoe Perches } 5319f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5320f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5321f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5322e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5323f5ef95b1SJoe Perches 5324f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5325f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5326b13edf7fSJoe Perches } 5327b13edf7fSJoe Perches } 5328b13edf7fSJoe Perches 5329f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 533013214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 533113214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5332cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 533313214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5334cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5335cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5336aad4f614SJoe Perches my @allowed = (); 5337aad4f614SJoe Perches my $allow = 0; 533813214adfSAndy Whitcroft my $seen = 0; 5339773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5340cf655043SAndy Whitcroft my $ln = $linenr - 1; 534113214adfSAndy Whitcroft for my $chunk (@chunks) { 534213214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 534313214adfSAndy Whitcroft 5344773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5345773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5346773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5347773647a0SAndy Whitcroft 5348aad4f614SJoe Perches $allowed[$allow] = 0; 5349773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5350773647a0SAndy Whitcroft 5351773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5352773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5353773647a0SAndy Whitcroft 5354773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5355cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5356cf655043SAndy Whitcroft 5357773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 535813214adfSAndy Whitcroft 535913214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 536013214adfSAndy Whitcroft 5361aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5362cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5363cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5364aad4f614SJoe Perches $allowed[$allow] = 1; 536513214adfSAndy Whitcroft } 536613214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5367cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5368aad4f614SJoe Perches $allowed[$allow] = 1; 536913214adfSAndy Whitcroft } 5370cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5371cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5372aad4f614SJoe Perches $allowed[$allow] = 1; 537313214adfSAndy Whitcroft } 5374aad4f614SJoe Perches $allow++; 537513214adfSAndy Whitcroft } 5376aad4f614SJoe Perches if ($seen) { 5377aad4f614SJoe Perches my $sum_allowed = 0; 5378aad4f614SJoe Perches foreach (@allowed) { 5379aad4f614SJoe Perches $sum_allowed += $_; 5380aad4f614SJoe Perches } 5381aad4f614SJoe Perches if ($sum_allowed == 0) { 5382000d1cc1SJoe Perches WARN("BRACES", 5383000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5384aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5385aad4f614SJoe Perches $seen != $allow) { 5386aad4f614SJoe Perches CHK("BRACES", 5387aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5388aad4f614SJoe Perches } 538913214adfSAndy Whitcroft } 539013214adfSAndy Whitcroft } 539113214adfSAndy Whitcroft } 5392773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 539313214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5394cf655043SAndy Whitcroft my $allowed = 0; 5395f0a594c1SAndy Whitcroft 5396cf655043SAndy Whitcroft # Check the pre-context. 5397cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5398cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5399cf655043SAndy Whitcroft $allowed = 1; 5400f0a594c1SAndy Whitcroft } 5401773647a0SAndy Whitcroft 5402773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5403773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5404773647a0SAndy Whitcroft 5405cf655043SAndy Whitcroft # Check the condition. 5406cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5407773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5408cf655043SAndy Whitcroft if (defined $cond) { 5409773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5410cf655043SAndy Whitcroft } 5411cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5412cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5413cf655043SAndy Whitcroft $allowed = 1; 5414cf655043SAndy Whitcroft } 5415cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5416cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5417cf655043SAndy Whitcroft $allowed = 1; 5418cf655043SAndy Whitcroft } 5419cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5420cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5421cf655043SAndy Whitcroft $allowed = 1; 5422cf655043SAndy Whitcroft } 5423cf655043SAndy Whitcroft # Check the post-context. 5424cf655043SAndy Whitcroft if (defined $chunks[1]) { 5425cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5426cf655043SAndy Whitcroft if (defined $cond) { 5427773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5428cf655043SAndy Whitcroft } 5429cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5430cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5431cf655043SAndy Whitcroft $allowed = 1; 5432cf655043SAndy Whitcroft } 5433cf655043SAndy Whitcroft } 5434cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5435f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5436e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5437cf655043SAndy Whitcroft 5438000d1cc1SJoe Perches WARN("BRACES", 5439000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5440f0a594c1SAndy Whitcroft } 5441f0a594c1SAndy Whitcroft } 5442f0a594c1SAndy Whitcroft 5443e4c5babdSJoe Perches# check for single line unbalanced braces 544495330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 544595330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5446e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5447e4c5babdSJoe Perches } 5448e4c5babdSJoe Perches 54490979ae66SJoe Perches# check for unnecessary blank lines around braces 545077b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5451f8e58219SJoe Perches if (CHK("BRACES", 5452f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5453f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5454f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5455f8e58219SJoe Perches } 54560979ae66SJoe Perches } 545777b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5458f8e58219SJoe Perches if (CHK("BRACES", 5459f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5460f8e58219SJoe Perches $fix) { 5461f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5462f8e58219SJoe Perches } 54630979ae66SJoe Perches } 54640979ae66SJoe Perches 54654a0df2efSAndy Whitcroft# no volatiles please 54666c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 54676c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5468000d1cc1SJoe Perches WARN("VOLATILE", 54698c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 54704a0df2efSAndy Whitcroft } 54714a0df2efSAndy Whitcroft 54725e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 54735e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 54745e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 54755e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 547633acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 54775e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 54785e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 54795e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 54805e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 54815e4f6ba5SJoe Perches $fix && 54825e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 54835e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 54845e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 54855e4f6ba5SJoe Perches my $comma_close = ""; 54865e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 54875e4f6ba5SJoe Perches $comma_close = $1; 54885e4f6ba5SJoe Perches } 54895e4f6ba5SJoe Perches 54905e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 54915e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 54925e4f6ba5SJoe Perches my $fixedline = $prevrawline; 54935e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 54945e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 54955e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 54965e4f6ba5SJoe Perches $fixedline = $rawline; 54975e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 54985e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 54995e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 55005e4f6ba5SJoe Perches } 55015e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 55025e4f6ba5SJoe Perches } 55035e4f6ba5SJoe Perches } 55045e4f6ba5SJoe Perches 55055e4f6ba5SJoe Perches# check for missing a space in a string concatenation 55065e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 55075e4f6ba5SJoe Perches WARN('MISSING_SPACE', 55085e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 55095e4f6ba5SJoe Perches } 55105e4f6ba5SJoe Perches 551177cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5512e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5513e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5514e4b7d309SJoe Perches# function declarations 551577cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 551677cb8546SJoe Perches defined($context_function) && 5517e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5518e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 551977cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5520e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 552177cb8546SJoe Perches } 552277cb8546SJoe Perches 55235e4f6ba5SJoe Perches# check for spaces before a quoted newline 55245e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 55255e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 55265e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 55275e4f6ba5SJoe Perches $fix) { 55285e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 55295e4f6ba5SJoe Perches } 55305e4f6ba5SJoe Perches 55315e4f6ba5SJoe Perches } 55325e4f6ba5SJoe Perches 5533f17dba4fSJoe Perches# concatenated string without spaces between elements 553479682c0cSJoe Perches if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { 553579682c0cSJoe Perches if (CHK("CONCATENATED_STRING", 553679682c0cSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr) && 553779682c0cSJoe Perches $fix) { 553879682c0cSJoe Perches while ($line =~ /($String)/g) { 553979682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 554079682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; 554179682c0cSJoe Perches $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; 554279682c0cSJoe Perches } 554379682c0cSJoe Perches } 5544f17dba4fSJoe Perches } 5545f17dba4fSJoe Perches 554690ad30e5SJoe Perches# uncoalesced string fragments 554733acb54aSJoe Perches if ($line =~ /$String\s*"/) { 554879682c0cSJoe Perches if (WARN("STRING_FRAGMENTS", 554979682c0cSJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr) && 555079682c0cSJoe Perches $fix) { 555179682c0cSJoe Perches while ($line =~ /($String)(?=\s*")/g) { 555279682c0cSJoe Perches my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); 555379682c0cSJoe Perches $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; 555479682c0cSJoe Perches } 555579682c0cSJoe Perches } 555690ad30e5SJoe Perches } 555790ad30e5SJoe Perches 5558522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5559522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5560522b837cSAlexey Dobriyan my $show_Z = 1; 55615e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5562522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 55635e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5564522b837cSAlexey Dobriyan # check for %L 5565522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 55665e4f6ba5SJoe Perches WARN("PRINTF_L", 5567522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5568522b837cSAlexey Dobriyan $show_L = 0; 55695e4f6ba5SJoe Perches } 5570522b837cSAlexey Dobriyan # check for %Z 5571522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5572522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5573522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5574522b837cSAlexey Dobriyan $show_Z = 0; 5575522b837cSAlexey Dobriyan } 5576522b837cSAlexey Dobriyan # check for 0x<decimal> 5577522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5578522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 55796e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 55806e300757SJoe Perches } 55815e4f6ba5SJoe Perches } 55825e4f6ba5SJoe Perches 55835e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 55843f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 55855e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 55865e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 55875e4f6ba5SJoe Perches } 55885e4f6ba5SJoe Perches 558900df344fSAndy Whitcroft# warn about #if 0 5590c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 559160f89010SPrakruthi Deepak Heragu WARN("IF_0", 559260f89010SPrakruthi Deepak Heragu "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); 559360f89010SPrakruthi Deepak Heragu } 559460f89010SPrakruthi Deepak Heragu 559560f89010SPrakruthi Deepak Heragu# warn about #if 1 559660f89010SPrakruthi Deepak Heragu if ($line =~ /^.\s*\#\s*if\s+1\b/) { 559760f89010SPrakruthi Deepak Heragu WARN("IF_1", 559860f89010SPrakruthi Deepak Heragu "Consider removing the #if 1 and its #endif\n" . $herecurr); 55994a0df2efSAndy Whitcroft } 56004a0df2efSAndy Whitcroft 560103df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 560203df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5603100425deSJoe Perches my $tested = quotemeta($1); 5604100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5605100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5606100425deSJoe Perches my $func = $1; 5607100425deSJoe Perches if (WARN('NEEDLESS_IF', 5608100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5609100425deSJoe Perches $fix) { 5610100425deSJoe Perches my $do_fix = 1; 5611100425deSJoe Perches my $leading_tabs = ""; 5612100425deSJoe Perches my $new_leading_tabs = ""; 5613100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5614100425deSJoe Perches $leading_tabs = $1; 5615100425deSJoe Perches } else { 5616100425deSJoe Perches $do_fix = 0; 5617100425deSJoe Perches } 5618100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5619100425deSJoe Perches $new_leading_tabs = $1; 5620100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5621100425deSJoe Perches $do_fix = 0; 5622100425deSJoe Perches } 5623100425deSJoe Perches } else { 5624100425deSJoe Perches $do_fix = 0; 5625100425deSJoe Perches } 5626100425deSJoe Perches if ($do_fix) { 5627100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5628100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5629100425deSJoe Perches } 5630100425deSJoe Perches } 56314c432a8fSGreg Kroah-Hartman } 56324c432a8fSGreg Kroah-Hartman } 5633f0a594c1SAndy Whitcroft 5634ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5635ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5636ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5637ebfdc409SJoe Perches (defined $1 || defined $3) && 5638ebfdc409SJoe Perches $linenr > 3) { 5639ebfdc409SJoe Perches my $testval = $2; 5640ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5641ebfdc409SJoe Perches 5642ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5643ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5644ebfdc409SJoe Perches 5645e29a70f1SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && 5646e29a70f1SJoe Perches $s !~ /\b__GFP_NOWARN\b/ ) { 5647ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5648ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5649ebfdc409SJoe Perches } 5650ebfdc409SJoe Perches } 5651ebfdc409SJoe Perches 5652f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5653dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5654f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5655f78d98f6SJoe Perches my $level = $1; 5656f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5657f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5658f78d98f6SJoe Perches $fix) { 5659f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5660f78d98f6SJoe Perches } 5661f78d98f6SJoe Perches } 5662f78d98f6SJoe Perches 566345c55e92SJoe Perches# check for logging continuations 566445c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 566545c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 566645c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 566745c55e92SJoe Perches } 566845c55e92SJoe Perches 5669abb08a53SJoe Perches# check for mask then right shift without a parentheses 56705b57980dSJoe Perches if ($perl_version_ok && 5671abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5672abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5673abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5674abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5675abb08a53SJoe Perches } 5676abb08a53SJoe Perches 5677b75ac618SJoe Perches# check for pointer comparisons to NULL 56785b57980dSJoe Perches if ($perl_version_ok) { 5679b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5680b75ac618SJoe Perches my $val = $1; 5681b75ac618SJoe Perches my $equal = "!"; 5682b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5683b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5684b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5685b75ac618SJoe Perches $fix) { 5686b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5687b75ac618SJoe Perches } 5688b75ac618SJoe Perches } 5689b75ac618SJoe Perches } 5690b75ac618SJoe Perches 56918716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 56928716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 56938716de38SJoe Perches my $attr = $1; 56948716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 56958716de38SJoe Perches my $ptr = $1; 56968716de38SJoe Perches my $var = $2; 56978716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 56988716de38SJoe Perches ERROR("MISPLACED_INIT", 56998716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 57008716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 57018716de38SJoe Perches WARN("MISPLACED_INIT", 57028716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 57038716de38SJoe Perches $fix) { 5704194f66fcSJoe 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; 57058716de38SJoe Perches } 57068716de38SJoe Perches } 57078716de38SJoe Perches } 57088716de38SJoe Perches 5709e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5710e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5711e970b884SJoe Perches my $attr = $1; 5712e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5713e970b884SJoe Perches my $attr_prefix = $1; 5714e970b884SJoe Perches my $attr_type = $2; 5715e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5716e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5717e970b884SJoe Perches $fix) { 5718194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5719e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5720e970b884SJoe Perches } 5721e970b884SJoe Perches } 5722e970b884SJoe Perches 5723e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5724e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5725e970b884SJoe Perches my $attr = $1; 5726e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5727e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5728e970b884SJoe Perches $fix) { 5729194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5730e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5731e970b884SJoe Perches $lead = rtrim($1); 5732e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5733e970b884SJoe Perches $lead = "${lead}const "; 5734194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5735e970b884SJoe Perches } 5736e970b884SJoe Perches } 5737e970b884SJoe Perches 5738c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5739c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5740c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5741c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5742c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5743c17893c7SJoe Perches $fix) { 5744c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5745c17893c7SJoe Perches } 5746c17893c7SJoe Perches } 5747c17893c7SJoe Perches 5748fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5749fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5750fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5751fbdb8138SJoe Perches my $constant_func = $1; 5752fbdb8138SJoe Perches my $func = $constant_func; 5753fbdb8138SJoe Perches $func =~ s/^__constant_//; 5754fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5755fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5756fbdb8138SJoe Perches $fix) { 5757194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5758fbdb8138SJoe Perches } 5759fbdb8138SJoe Perches } 5760fbdb8138SJoe Perches 57611a15a250SPatrick Pannuto# prefer usleep_range over udelay 576237581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 576343c1d77cSJoe Perches my $delay = $1; 57641a15a250SPatrick Pannuto # ignore udelay's < 10, however 576543c1d77cSJoe Perches if (! ($delay < 10) ) { 5766000d1cc1SJoe Perches CHK("USLEEP_RANGE", 5767458f69efSMauro Carvalho Chehab "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); 576843c1d77cSJoe Perches } 576943c1d77cSJoe Perches if ($delay > 2000) { 577043c1d77cSJoe Perches WARN("LONG_UDELAY", 577143c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 57721a15a250SPatrick Pannuto } 57731a15a250SPatrick Pannuto } 57741a15a250SPatrick Pannuto 577509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 577609ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 577709ef8725SPatrick Pannuto if ($1 < 20) { 5778000d1cc1SJoe Perches WARN("MSLEEP", 5779458f69efSMauro Carvalho Chehab "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); 578009ef8725SPatrick Pannuto } 578109ef8725SPatrick Pannuto } 578209ef8725SPatrick Pannuto 578336ec1939SJoe Perches# check for comparisons of jiffies 578436ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 578536ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 578636ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 578736ec1939SJoe Perches } 578836ec1939SJoe Perches 57899d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 57909d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 57919d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 57929d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 57939d7a34a5SJoe Perches } 57949d7a34a5SJoe Perches 579500df344fSAndy Whitcroft# warn about #ifdefs in C files 5796c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 579700df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 579800df344fSAndy Whitcroft# print "$herecurr"; 579900df344fSAndy Whitcroft# $clean = 0; 580000df344fSAndy Whitcroft# } 580100df344fSAndy Whitcroft 580222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5803c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 58043705ce5bSJoe Perches if (ERROR("SPACING", 58053705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 58063705ce5bSJoe Perches $fix) { 5807194f66fcSJoe Perches $fixed[$fixlinenr] =~ 58083705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 58093705ce5bSJoe Perches } 58103705ce5bSJoe Perches 581122f2a2efSAndy Whitcroft } 581222f2a2efSAndy Whitcroft 58134a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5814171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5815171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 58164a0df2efSAndy Whitcroft my $which = $1; 58174a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5818000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5819000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 58204a0df2efSAndy Whitcroft } 58214a0df2efSAndy Whitcroft } 58224a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5823402c2553SMichael S. Tsirkin 5824402c2553SMichael S. Tsirkin my $barriers = qr{ 5825402c2553SMichael S. Tsirkin mb| 5826402c2553SMichael S. Tsirkin rmb| 5827402c2553SMichael S. Tsirkin wmb| 5828402c2553SMichael S. Tsirkin read_barrier_depends 5829402c2553SMichael S. Tsirkin }x; 5830402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5831402c2553SMichael S. Tsirkin mb__before_atomic| 5832402c2553SMichael S. Tsirkin mb__after_atomic| 5833402c2553SMichael S. Tsirkin store_release| 5834402c2553SMichael S. Tsirkin load_acquire| 5835402c2553SMichael S. Tsirkin store_mb| 5836402c2553SMichael S. Tsirkin (?:$barriers) 5837402c2553SMichael S. Tsirkin }x; 5838402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5839402c2553SMichael S. Tsirkin (?:$barriers)| 584043e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 584143e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5842402c2553SMichael S. Tsirkin }x; 5843402c2553SMichael S. Tsirkin 5844402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 58454a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5846c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5847000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 58484a0df2efSAndy Whitcroft } 58494a0df2efSAndy Whitcroft } 58503ad81779SPaul E. McKenney 5851f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5852f4073b0fSMichael S. Tsirkin 5853f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5854f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5855f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5856f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5857f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5858f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5859f4073b0fSMichael S. Tsirkin } 5860f4073b0fSMichael S. Tsirkin 5861cb426e99SJoe Perches# check for waitqueue_active without a comment. 5862cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5863cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5864cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5865cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5866cb426e99SJoe Perches } 5867cb426e99SJoe Perches } 58683ad81779SPaul E. McKenney 586991db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends 587091db2592SPaul E. McKenney if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { 587191db2592SPaul E. McKenney WARN("READ_BARRIER_DEPENDS", 587291db2592SPaul E. McKenney "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); 587391db2592SPaul E. McKenney } 587491db2592SPaul E. McKenney 58754a0df2efSAndy Whitcroft# check of hardware specific defines 5876c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5877000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5878000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 58790a920b5bSAndy Whitcroft } 5880653d4876SAndy Whitcroft 5881596ed45bSJoe Perches# check that the storage class is not after a type 5882596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5883000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5884596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5885596ed45bSJoe Perches } 5886596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5887596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5888596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5889596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5890596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5891596ed45bSJoe Perches WARN("STORAGE_CLASS", 5892596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5893d4977c78STobias Klauser } 5894d4977c78STobias Klauser 5895de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5896de7d4f0eSAndy Whitcroft# storage class and type. 58979c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 58989c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5899000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5900000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5901de7d4f0eSAndy Whitcroft } 5902de7d4f0eSAndy Whitcroft 59038905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 59042b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59052b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5906d5e616fcSJoe Perches if (WARN("INLINE", 5907d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5908d5e616fcSJoe Perches $fix) { 5909194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5910d5e616fcSJoe Perches 5911d5e616fcSJoe Perches } 59128905a67cSAndy Whitcroft } 59138905a67cSAndy Whitcroft 59143d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 59152b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59162b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5917000d1cc1SJoe Perches WARN("PREFER_PACKED", 5918000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 59193d130fd0SJoe Perches } 59203d130fd0SJoe Perches 592139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 59222b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59232b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5924000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5925000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 592639b7e287SJoe Perches } 592739b7e287SJoe Perches 5928462811d9SJoe Perches# Check for __attribute__ section, prefer __section 5929462811d9SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5930462811d9SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { 5931462811d9SJoe Perches my $old = substr($rawline, $-[1], $+[1] - $-[1]); 5932462811d9SJoe Perches my $new = substr($old, 1, -1); 5933462811d9SJoe Perches if (WARN("PREFER_SECTION", 5934462811d9SJoe Perches "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && 5935462811d9SJoe Perches $fix) { 5936462811d9SJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; 5937462811d9SJoe Perches } 5938462811d9SJoe Perches } 5939462811d9SJoe Perches 59405f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 59412b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59422b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5943d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5944d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5945d5e616fcSJoe Perches $fix) { 5946194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5947d5e616fcSJoe Perches 5948d5e616fcSJoe Perches } 59495f14d3bdSJoe Perches } 59505f14d3bdSJoe Perches 59516061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 59522b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 59532b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5954d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5955d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5956d5e616fcSJoe Perches $fix) { 5957194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5958d5e616fcSJoe Perches } 59596061d949SJoe Perches } 59606061d949SJoe Perches 5961619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 59625b57980dSJoe Perches if ($perl_version_ok && 5963619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5964619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5965619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5966619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5967619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5968619a908aSJoe Perches } 5969619a908aSJoe Perches 5970fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 5971e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5972fd39f904STomas Winkler $realfile !~ m@\btools/@ && 5973e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5974e6176fa4SJoe Perches my $type = $1; 5975e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5976e6176fa4SJoe Perches $type = $1; 5977e6176fa4SJoe Perches my $kernel_type = 'u'; 5978e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5979e6176fa4SJoe Perches $type =~ /(\d+)/; 5980e6176fa4SJoe Perches $kernel_type .= $1; 5981e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5982e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5983e6176fa4SJoe Perches $fix) { 5984e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5985e6176fa4SJoe Perches } 5986e6176fa4SJoe Perches } 5987e6176fa4SJoe Perches } 5988e6176fa4SJoe Perches 5989938224b5SJoe Perches# check for cast of C90 native int or longer types constants 5990938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 5991938224b5SJoe Perches my $cast = $1; 5992938224b5SJoe Perches my $const = $2; 5993938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 5994938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 5995938224b5SJoe Perches $fix) { 5996938224b5SJoe Perches my $suffix = ""; 5997938224b5SJoe Perches my $newconst = $const; 5998938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 5999938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 6000938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 6001938224b5SJoe Perches $suffix .= 'LL'; 6002938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 6003938224b5SJoe Perches $suffix .= 'L'; 6004938224b5SJoe Perches } 6005938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 6006938224b5SJoe Perches } 6007938224b5SJoe Perches } 6008938224b5SJoe Perches 60098f53a9b8SJoe Perches# check for sizeof(&) 60108f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 6011000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 6012000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 60138f53a9b8SJoe Perches } 60148f53a9b8SJoe Perches 601566c80b60SJoe Perches# check for sizeof without parenthesis 601666c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 6017d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 6018d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 6019d5e616fcSJoe Perches $fix) { 6020194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 6021d5e616fcSJoe Perches } 602266c80b60SJoe Perches } 602366c80b60SJoe Perches 602488982feaSJoe Perches# check for struct spinlock declarations 602588982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 602688982feaSJoe Perches WARN("USE_SPINLOCK_T", 602788982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 602888982feaSJoe Perches } 602988982feaSJoe Perches 6030a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 603106668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 6032a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 6033caac1d5fSHeba Aamer $fmt =~ s/%%//g; 6034caac1d5fSHeba Aamer if ($fmt !~ /%/) { 6035d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 6036d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 6037d5e616fcSJoe Perches $fix) { 6038194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 6039d5e616fcSJoe Perches } 6040a6962d72SJoe Perches } 6041a6962d72SJoe Perches } 6042a6962d72SJoe Perches 60430b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 60445b57980dSJoe Perches if ($perl_version_ok && 60450b523769SJoe Perches defined $stat && 60460b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 60470b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 6048e3c6bc95STobin C. Harding my $stat_real; 6049e3c6bc95STobin C. Harding 60500b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 60510b523769SJoe Perches $lc = $lc + $linenr; 60520b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 6053ffe07513SJoe Perches my $specifier; 6054ffe07513SJoe Perches my $extension; 60553bd32d6aSSakari Ailus my $qualifier; 6056ffe07513SJoe Perches my $bad_specifier = ""; 60570b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 60580b523769SJoe Perches $fmt =~ s/%%//g; 6059e3c6bc95STobin C. Harding 60603bd32d6aSSakari Ailus while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { 6061e3c6bc95STobin C. Harding $specifier = $1; 6062e3c6bc95STobin C. Harding $extension = $2; 60633bd32d6aSSakari Ailus $qualifier = $3; 6064361b0d28SLinus Torvalds if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || 60653bd32d6aSSakari Ailus ($extension eq "f" && 60663bd32d6aSSakari Ailus defined $qualifier && $qualifier !~ /^w/)) { 6067e3c6bc95STobin C. Harding $bad_specifier = $specifier; 60680b523769SJoe Perches last; 60690b523769SJoe Perches } 6070e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 6071e3c6bc95STobin C. Harding if (!defined($stat_real)) { 6072e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 60730b523769SJoe Perches } 6074e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 6075e3c6bc95STobin 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"); 6076e3c6bc95STobin C. Harding } 6077e3c6bc95STobin C. Harding } 6078e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 60792a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 60801df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 60811df7338aSSergey Senozhatsky my $use = ""; 6082e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 60831df7338aSSergey Senozhatsky $use = " - use %pS instead"; 6084e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 60851df7338aSSergey Senozhatsky } 60862a9f9d85STobin C. Harding 60870b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 6088e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 6089e3c6bc95STobin C. Harding } 60900b523769SJoe Perches } 60910b523769SJoe Perches } 60920b523769SJoe Perches 6093554e165cSAndy Whitcroft# Check for misused memsets 60945b57980dSJoe Perches if ($perl_version_ok && 6095d1fe9c09SJoe Perches defined $stat && 60969e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 6097554e165cSAndy Whitcroft 6098d7c76ba7SJoe Perches my $ms_addr = $2; 6099d1fe9c09SJoe Perches my $ms_val = $7; 6100d1fe9c09SJoe Perches my $ms_size = $12; 6101d7c76ba7SJoe Perches 6102554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 6103554e165cSAndy Whitcroft ERROR("MEMSET", 6104d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 6105554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 6106554e165cSAndy Whitcroft WARN("MEMSET", 6107d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 6108d7c76ba7SJoe Perches } 6109d7c76ba7SJoe Perches } 6110d7c76ba7SJoe Perches 611198a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 61125b57980dSJoe Perches# if ($perl_version_ok && 6113f333195dSJoe Perches# defined $stat && 6114f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6115f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 6116f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 6117f333195dSJoe Perches# $fix) { 6118f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 6119f333195dSJoe Perches# } 6120f333195dSJoe Perches# } 612198a9bba5SJoe Perches 6122b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 61235b57980dSJoe Perches# if ($perl_version_ok && 6124f333195dSJoe Perches# defined $stat && 6125f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6126f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 6127f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 6128f333195dSJoe Perches# } 6129b6117d17SMateusz Kulikowski 61308617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 61318617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 61325b57980dSJoe Perches# if ($perl_version_ok && 6133f333195dSJoe Perches# defined $stat && 6134f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 6135f333195dSJoe Perches# 6136f333195dSJoe Perches# my $ms_val = $7; 6137f333195dSJoe Perches# 6138f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 6139f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 6140f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 6141f333195dSJoe Perches# $fix) { 6142f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 6143f333195dSJoe Perches# } 6144f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 6145f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 6146f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 6147f333195dSJoe Perches# $fix) { 6148f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 6149f333195dSJoe Perches# } 6150f333195dSJoe Perches# } 6151f333195dSJoe Perches# } 61528617cd09SMateusz Kulikowski 6153d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 61545b57980dSJoe Perches if ($perl_version_ok && 6155d1fe9c09SJoe Perches defined $stat && 6156d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 6157d1fe9c09SJoe Perches if (defined $2 || defined $7) { 6158d7c76ba7SJoe Perches my $call = $1; 6159d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 6160d7c76ba7SJoe Perches my $arg1 = $3; 6161d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 6162d1fe9c09SJoe Perches my $arg2 = $8; 6163d7c76ba7SJoe Perches my $cast; 6164d7c76ba7SJoe Perches 6165d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 6166d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 6167d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 6168d7c76ba7SJoe Perches $cast = $cast1; 6169d7c76ba7SJoe Perches } else { 6170d7c76ba7SJoe Perches $cast = $cast2; 6171d7c76ba7SJoe Perches } 6172d7c76ba7SJoe Perches WARN("MINMAX", 6173d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 6174554e165cSAndy Whitcroft } 6175554e165cSAndy Whitcroft } 6176554e165cSAndy Whitcroft 61774a273195SJoe Perches# check usleep_range arguments 61785b57980dSJoe Perches if ($perl_version_ok && 61794a273195SJoe Perches defined $stat && 61804a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 61814a273195SJoe Perches my $min = $1; 61824a273195SJoe Perches my $max = $7; 61834a273195SJoe Perches if ($min eq $max) { 61844a273195SJoe Perches WARN("USLEEP_RANGE", 6185458f69efSMauro Carvalho Chehab "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 61864a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 61874a273195SJoe Perches $min > $max) { 61884a273195SJoe Perches WARN("USLEEP_RANGE", 6189458f69efSMauro Carvalho Chehab "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); 61904a273195SJoe Perches } 61914a273195SJoe Perches } 61924a273195SJoe Perches 6193823b794cSJoe Perches# check for naked sscanf 61945b57980dSJoe Perches if ($perl_version_ok && 6195823b794cSJoe Perches defined $stat && 61966c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 6197823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 6198823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 6199823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 6200823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 6201823b794cSJoe Perches $lc = $lc + $linenr; 62022a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6203823b794cSJoe Perches WARN("NAKED_SSCANF", 6204823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 6205823b794cSJoe Perches } 6206823b794cSJoe Perches 6207afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 62085b57980dSJoe Perches if ($perl_version_ok && 6209afc819abSJoe Perches defined $stat && 6210afc819abSJoe Perches $line =~ /\bsscanf\b/) { 6211afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 6212afc819abSJoe Perches $lc = $lc + $linenr; 62132a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6214afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 6215afc819abSJoe Perches my $format = $6; 6216afc819abSJoe Perches my $count = $format =~ tr@%@%@; 6217afc819abSJoe Perches if ($count == 1 && 6218afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 6219afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 6220afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 6221afc819abSJoe Perches } 6222afc819abSJoe Perches } 6223afc819abSJoe Perches } 6224afc819abSJoe Perches 622570dc8a48SJoe Perches# check for new externs in .h files. 622670dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 622770dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 6228d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 622970dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 623070dc8a48SJoe Perches $fix) { 6231194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 623270dc8a48SJoe Perches } 623370dc8a48SJoe Perches } 623470dc8a48SJoe Perches 6235de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6236171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6237c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6238171ae1a4SAndy Whitcroft { 6239c45dcabdSAndy Whitcroft my $function_name = $1; 6240c45dcabdSAndy Whitcroft my $paren_space = $2; 6241171ae1a4SAndy Whitcroft 6242171ae1a4SAndy Whitcroft my $s = $stat; 6243171ae1a4SAndy Whitcroft if (defined $cond) { 6244171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6245171ae1a4SAndy Whitcroft } 6246c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 6247c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 6248c45dcabdSAndy Whitcroft { 6249000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6250000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6251de7d4f0eSAndy Whitcroft } 6252de7d4f0eSAndy Whitcroft 6253171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6254000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6255000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6256171ae1a4SAndy Whitcroft } 62579c9ba34eSAndy Whitcroft 62589c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 62599c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 62609c9ba34eSAndy Whitcroft { 6261000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6262000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6263171ae1a4SAndy Whitcroft } 6264171ae1a4SAndy Whitcroft 6265a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6266a0ad7596SJoe Perches if (defined $stat && 626725bdda2bSMiles Chen $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6268ca0d8929SJoe Perches $1 ne "void") { 6269ca0d8929SJoe Perches my $args = trim($1); 6270ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6271ca0d8929SJoe Perches my $arg = trim($1); 6272ca0d8929SJoe Perches if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6273ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6274ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6275ca0d8929SJoe Perches } 6276ca0d8929SJoe Perches } 6277ca0d8929SJoe Perches } 6278ca0d8929SJoe Perches 6279a0ad7596SJoe Perches# check for function definitions 62805b57980dSJoe Perches if ($perl_version_ok && 6281a0ad7596SJoe Perches defined $stat && 6282a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6283a0ad7596SJoe Perches $context_function = $1; 6284a0ad7596SJoe Perches 6285a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6286a0ad7596SJoe Perches my $ok = 0; 6287a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6288a0ad7596SJoe Perches my $herectx = $here . "\n"; 6289a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6290a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6291a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6292a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6293a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6294a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6295a0ad7596SJoe Perches } 6296a0ad7596SJoe Perches if (!$ok) { 6297a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6298a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6299a0ad7596SJoe Perches } 6300a0ad7596SJoe Perches } 6301a0ad7596SJoe Perches 6302de7d4f0eSAndy Whitcroft# checks for new __setup's 6303de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6304de7d4f0eSAndy Whitcroft my $name = $1; 6305de7d4f0eSAndy Whitcroft 6306de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6307000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 63088c27ceffSMauro Carvalho Chehab "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); 6309de7d4f0eSAndy Whitcroft } 6310653d4876SAndy Whitcroft } 63119c0ca6f9SAndy Whitcroft 6312e29a70f1SJoe Perches# check for pointless casting of alloc functions 6313e29a70f1SJoe Perches if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { 6314000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6315000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 63169c0ca6f9SAndy Whitcroft } 631713214adfSAndy Whitcroft 6318a640d25cSJoe Perches# alloc style 6319a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 63205b57980dSJoe Perches if ($perl_version_ok && 6321e29a70f1SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6322a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6323a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6324a640d25cSJoe Perches } 6325a640d25cSJoe Perches 632660a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 63275b57980dSJoe Perches if ($perl_version_ok && 63281b4a2ed4SJoe Perches defined $stat && 63291b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 633060a55369SJoe Perches my $oldfunc = $3; 633160a55369SJoe Perches my $a1 = $4; 633260a55369SJoe Perches my $a2 = $10; 633360a55369SJoe Perches my $newfunc = "kmalloc_array"; 633460a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 633560a55369SJoe Perches my $r1 = $a1; 633660a55369SJoe Perches my $r2 = $a2; 633760a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 633860a55369SJoe Perches $r1 = $a2; 633960a55369SJoe Perches $r2 = $a1; 634060a55369SJoe Perches } 6341e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6342e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 63431b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6344e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6345e3d95a2aSTobin C. Harding 6346e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 63471b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 63481b4a2ed4SJoe Perches $cnt == 1 && 6349e367455aSJoe Perches $fix) { 6350194f66fcSJoe 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; 635160a55369SJoe Perches } 635260a55369SJoe Perches } 635360a55369SJoe Perches } 635460a55369SJoe Perches 6355972fdea2SJoe Perches# check for krealloc arg reuse 63565b57980dSJoe Perches if ($perl_version_ok && 63574cab63ceSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && 63584cab63ceSJoe Perches $1 eq $3) { 6359972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6360972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6361972fdea2SJoe Perches } 6362972fdea2SJoe Perches 63635ce59ae0SJoe Perches# check for alloc argument mismatch 63645ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 63655ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 63665ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 63675ce59ae0SJoe Perches } 63685ce59ae0SJoe Perches 6369caf2a54fSJoe Perches# check for multiple semicolons 6370caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6371d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6372d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6373d5e616fcSJoe Perches $fix) { 6374194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6375d5e616fcSJoe Perches } 6376d1e2ad07SJoe Perches } 6377d1e2ad07SJoe Perches 6378cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6379cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6380cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 63810ab90191SJoe Perches my $ull = ""; 63820ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 63830ab90191SJoe Perches if (CHK("BIT_MACRO", 63840ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 63850ab90191SJoe Perches $fix) { 63860ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 63870ab90191SJoe Perches } 63880ab90191SJoe Perches } 63890ab90191SJoe Perches 63902d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 63912d632745SJoe 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*$/) { 63922d632745SJoe Perches my $config = $1; 63932d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 63942d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 63952d632745SJoe Perches $fix) { 63962d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 63972d632745SJoe Perches } 63982d632745SJoe Perches } 63992d632745SJoe Perches 6400e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6401c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6402c34c09a8SJoe Perches my $has_break = 0; 6403c34c09a8SJoe Perches my $has_statement = 0; 6404c34c09a8SJoe Perches my $count = 0; 6405c34c09a8SJoe Perches my $prevline = $linenr; 6406e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6407c34c09a8SJoe Perches $prevline--; 6408c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6409c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6410c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6411c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6412c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6413c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6414c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6415c34c09a8SJoe Perches $has_statement = 1; 6416c34c09a8SJoe Perches $count++; 6417258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6418c34c09a8SJoe Perches } 6419c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6420c34c09a8SJoe Perches WARN("MISSING_BREAK", 6421224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6422c34c09a8SJoe Perches } 6423c34c09a8SJoe Perches } 6424c34c09a8SJoe Perches 6425f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough; 6426f36d3eb8SJoe Perches my @fallthroughs = ( 6427f36d3eb8SJoe Perches 'fallthrough', 6428f36d3eb8SJoe Perches '@fallthrough@', 6429f36d3eb8SJoe Perches 'lint -fallthrough[ \t]*', 6430f36d3eb8SJoe Perches 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', 6431f36d3eb8SJoe Perches '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', 6432f36d3eb8SJoe Perches 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6433f36d3eb8SJoe Perches 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', 6434f36d3eb8SJoe Perches ); 6435f36d3eb8SJoe Perches if ($raw_comment ne '') { 6436f36d3eb8SJoe Perches foreach my $ft (@fallthroughs) { 6437f36d3eb8SJoe Perches if ($raw_comment =~ /$ft/) { 6438f36d3eb8SJoe Perches my $msg_level = \&WARN; 6439f36d3eb8SJoe Perches $msg_level = \&CHK if ($file); 6440f36d3eb8SJoe Perches &{$msg_level}("PREFER_FALLTHROUGH", 6441f36d3eb8SJoe Perches "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); 6442f36d3eb8SJoe Perches last; 6443f36d3eb8SJoe Perches } 6444f36d3eb8SJoe Perches } 6445f36d3eb8SJoe Perches } 6446f36d3eb8SJoe Perches 6447d1e2ad07SJoe Perches# check for switch/default statements without a break; 64485b57980dSJoe Perches if ($perl_version_ok && 6449d1e2ad07SJoe Perches defined $stat && 6450d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6451d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6452e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6453e3d95a2aSTobin C. Harding 6454d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6455d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6456caf2a54fSJoe Perches } 6457caf2a54fSJoe Perches 645813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6459d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6460d5e616fcSJoe Perches if (WARN("USE_FUNC", 6461d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6462d5e616fcSJoe Perches $fix) { 6463194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6464d5e616fcSJoe Perches } 646513214adfSAndy Whitcroft } 6466773647a0SAndy Whitcroft 646762ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 646862ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 646962ec818fSJoe Perches ERROR("DATE_TIME", 647062ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 647162ec818fSJoe Perches } 647262ec818fSJoe Perches 64732c92488aSJoe Perches# check for use of yield() 64742c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 64752c92488aSJoe Perches WARN("YIELD", 64762c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 64772c92488aSJoe Perches } 64782c92488aSJoe Perches 6479179f8f40SJoe Perches# check for comparisons against true and false 6480179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6481179f8f40SJoe Perches my $lead = $1; 6482179f8f40SJoe Perches my $arg = $2; 6483179f8f40SJoe Perches my $test = $3; 6484179f8f40SJoe Perches my $otype = $4; 6485179f8f40SJoe Perches my $trail = $5; 6486179f8f40SJoe Perches my $op = "!"; 6487179f8f40SJoe Perches 6488179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6489179f8f40SJoe Perches 6490179f8f40SJoe Perches my $type = lc($otype); 6491179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6492179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6493179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6494179f8f40SJoe Perches $op = ""; 6495179f8f40SJoe Perches } 6496179f8f40SJoe Perches 6497179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6498179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6499179f8f40SJoe Perches 6500179f8f40SJoe Perches## maybe suggesting a correct construct would better 6501179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6502179f8f40SJoe Perches 6503179f8f40SJoe Perches } 6504179f8f40SJoe Perches } 6505179f8f40SJoe Perches 65064882720bSThomas Gleixner# check for semaphores initialized locked 65074882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6508000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6509000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6510773647a0SAndy Whitcroft } 65116712d858SJoe Perches 651267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 651367d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6514000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 651567d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6516773647a0SAndy Whitcroft } 65176712d858SJoe Perches 6518ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6519f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6520000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6521ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6522f3db6639SMichael Ellerman } 65236712d858SJoe Perches 65243d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead 65253d709ab5SPaul E. McKenney if ($line =~ /\bspin_is_locked\(/) { 65263d709ab5SPaul E. McKenney WARN("USE_LOCKDEP", 65273d709ab5SPaul E. McKenney "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); 65283d709ab5SPaul E. McKenney } 65293d709ab5SPaul E. McKenney 65309189c7e7SJoe Perches# check for deprecated apis 65319189c7e7SJoe Perches if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { 65329189c7e7SJoe Perches my $deprecated_api = $1; 65339189c7e7SJoe Perches my $new_api = $deprecated_apis{$deprecated_api}; 65349189c7e7SJoe Perches WARN("DEPRECATED_API", 65359189c7e7SJoe Perches "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); 65369189c7e7SJoe Perches } 65379189c7e7SJoe Perches 65380f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6539d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 65406903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6541d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6542000d1cc1SJoe Perches WARN("CONST_STRUCT", 6543d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 65442b6db5cbSAndy Whitcroft } 6545773647a0SAndy Whitcroft 6546773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6547773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6548773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6549c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6550c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6551171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6552171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6553171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6554773647a0SAndy Whitcroft { 6555000d1cc1SJoe Perches WARN("NR_CPUS", 6556000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6557773647a0SAndy Whitcroft } 65589c9ba34eSAndy Whitcroft 655952ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 656052ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 656152ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 656252ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 656352ea8506SJoe Perches } 656452ea8506SJoe Perches 6565acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 65665b57980dSJoe Perches if ($perl_version_ok && 6567acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6568acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6569acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6570acd9362cSJoe Perches } 6571acd9362cSJoe Perches 6572de3f186fSDenis Efremov# nested likely/unlikely calls 6573de3f186fSDenis Efremov if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { 6574de3f186fSDenis Efremov WARN("LIKELY_MISUSE", 6575de3f186fSDenis Efremov "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); 6576de3f186fSDenis Efremov } 6577de3f186fSDenis Efremov 6578691d77b6SAndy Whitcroft# whine mightly about in_atomic 6579691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6580691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6581000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6582000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6583f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6584000d1cc1SJoe Perches WARN("IN_ATOMIC", 6585000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6586691d77b6SAndy Whitcroft } 6587691d77b6SAndy Whitcroft } 65881704f47bSPeter Zijlstra 65890f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 65900f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 65910f5225b0SPeter Zijlstra ERROR("LOCKING", 65920f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 65930f5225b0SPeter Zijlstra } 65940f5225b0SPeter Zijlstra 65951704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 65961704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 65971704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 65981704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 65991704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 66001704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6601000d1cc1SJoe Perches ERROR("LOCKDEP", 6602000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 66031704f47bSPeter Zijlstra } 66041704f47bSPeter Zijlstra } 660588f8831cSDave Jones 6606b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6607b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6608000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6609000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 661088f8831cSDave Jones } 66112435880fSJoe Perches 661200180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 661300180468SJoe Perches# and whether or not function naming is typical and if 661400180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 66155b57980dSJoe Perches if ($perl_version_ok && 661600180468SJoe Perches defined $stat && 661700180468SJoe 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*\)/) { 661800180468SJoe Perches my $var = $1; 661900180468SJoe Perches my $perms = $2; 662000180468SJoe Perches my $show = $3; 662100180468SJoe Perches my $store = $4; 662200180468SJoe Perches my $octal_perms = perms_to_octal($perms); 662300180468SJoe Perches if ($show =~ /^${var}_show$/ && 662400180468SJoe Perches $store =~ /^${var}_store$/ && 662500180468SJoe Perches $octal_perms eq "0644") { 662600180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 662700180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 662800180468SJoe Perches $fix) { 662900180468SJoe 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})/; 663000180468SJoe Perches } 663100180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 663200180468SJoe Perches $store =~ /^NULL$/ && 663300180468SJoe Perches $octal_perms eq "0444") { 663400180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 663500180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 663600180468SJoe Perches $fix) { 663700180468SJoe 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})/; 663800180468SJoe Perches } 663900180468SJoe Perches } elsif ($show =~ /^NULL$/ && 664000180468SJoe Perches $store =~ /^${var}_store$/ && 664100180468SJoe Perches $octal_perms eq "0200") { 664200180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 664300180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 664400180468SJoe Perches $fix) { 664500180468SJoe 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})/; 664600180468SJoe Perches } 664700180468SJoe Perches } elsif ($octal_perms eq "0644" || 664800180468SJoe Perches $octal_perms eq "0444" || 664900180468SJoe Perches $octal_perms eq "0200") { 665000180468SJoe Perches my $newshow = "$show"; 665100180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 665200180468SJoe Perches my $newstore = $store; 665300180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 665400180468SJoe Perches my $rename = ""; 665500180468SJoe Perches if ($show ne $newshow) { 665600180468SJoe Perches $rename .= " '$show' to '$newshow'"; 665700180468SJoe Perches } 665800180468SJoe Perches if ($store ne $newstore) { 665900180468SJoe Perches $rename .= " '$store' to '$newstore'"; 666000180468SJoe Perches } 666100180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 666200180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 666300180468SJoe Perches } else { 666400180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 666500180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 666600180468SJoe Perches } 666700180468SJoe Perches } 666800180468SJoe Perches 6669515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6670515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 667173121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 667273121534SJoe Perches# specific definition of not visible in sysfs. 667373121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 667473121534SJoe Perches# use the default permissions 66755b57980dSJoe Perches if ($perl_version_ok && 6676459cf0aeSJoe Perches defined $stat && 6677515a235eSJoe Perches $line =~ /$mode_perms_search/) { 66782435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 66792435880fSJoe Perches my $func = $entry->[0]; 66802435880fSJoe Perches my $arg_pos = $entry->[1]; 66812435880fSJoe Perches 6682459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6683459cf0aeSJoe Perches $lc = $lc + $linenr; 66842a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6685459cf0aeSJoe Perches 66862435880fSJoe Perches my $skip_args = ""; 66872435880fSJoe Perches if ($arg_pos > 1) { 66882435880fSJoe Perches $arg_pos--; 66892435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 66902435880fSJoe Perches } 6691f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6692459cf0aeSJoe Perches if ($stat =~ /$test/) { 66932435880fSJoe Perches my $val = $1; 66942435880fSJoe Perches $val = $6 if ($skip_args ne ""); 669573121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 669673121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 669773121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 66982435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6699459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6700f90774e1SJoe Perches } 6701f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6702c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6703459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 67042435880fSJoe Perches } 6705459cf0aeSJoe Perches } 6706459cf0aeSJoe Perches } 6707459cf0aeSJoe Perches } 6708459cf0aeSJoe Perches 6709459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6710bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 671100180468SJoe Perches my $oval = $1; 671200180468SJoe Perches my $octal = perms_to_octal($oval); 6713f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6714459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6715f90774e1SJoe Perches $fix) { 671600180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 67172435880fSJoe Perches } 671813214adfSAndy Whitcroft } 67195a6d20ceSBjorn Andersson 67205a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 67215a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 67225a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 67235a6d20ceSBjorn Andersson my $valid_licenses = qr{ 67245a6d20ceSBjorn Andersson GPL| 67255a6d20ceSBjorn Andersson GPL\ v2| 67265a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 67275a6d20ceSBjorn Andersson Dual\ BSD/GPL| 67285a6d20ceSBjorn Andersson Dual\ MIT/GPL| 67295a6d20ceSBjorn Andersson Dual\ MPL/GPL| 67305a6d20ceSBjorn Andersson Proprietary 67315a6d20ceSBjorn Andersson }x; 67325a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 67335a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 67345a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 67355a6d20ceSBjorn Andersson } 67365a6d20ceSBjorn Andersson } 67376a8d76cbSMatteo Croce 67386a8d76cbSMatteo Croce# check for sysctl duplicate constants 67396a8d76cbSMatteo Croce if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 67406a8d76cbSMatteo Croce WARN("DUPLICATED_SYSCTL_CONST", 67416a8d76cbSMatteo Croce "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 67426a8d76cbSMatteo Croce } 6743515a235eSJoe Perches } 674413214adfSAndy Whitcroft 674513214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 674613214adfSAndy Whitcroft # so just keep quiet. 674713214adfSAndy Whitcroft if ($#rawlines == -1) { 674813214adfSAndy Whitcroft exit(0); 67490a920b5bSAndy Whitcroft } 67500a920b5bSAndy Whitcroft 67518905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 67528905a67cSAndy Whitcroft # things that appear to be patches. 67538905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 67548905a67cSAndy Whitcroft exit(0); 67558905a67cSAndy Whitcroft } 67568905a67cSAndy Whitcroft 67578905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 67588905a67cSAndy Whitcroft # just keep quiet. 67598905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 67608905a67cSAndy Whitcroft exit(0); 67618905a67cSAndy Whitcroft } 67628905a67cSAndy Whitcroft 6763a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6764000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6765000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 67660a920b5bSAndy Whitcroft } 6767cd261496SGeert Uytterhoeven if ($is_patch && $has_commit_log && $chk_signoff) { 6768cd261496SGeert Uytterhoeven if ($signoff == 0) { 6769000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6770000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 6771cd261496SGeert Uytterhoeven } elsif (!$authorsignoff) { 6772cd261496SGeert Uytterhoeven WARN("NO_AUTHOR_SIGN_OFF", 6773cd261496SGeert Uytterhoeven "Missing Signed-off-by: line by nominal patch author '$author'\n"); 6774cd261496SGeert Uytterhoeven } 67750a920b5bSAndy Whitcroft } 67760a920b5bSAndy Whitcroft 6777f0a594c1SAndy Whitcroft print report_dump(); 677813214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 677913214adfSAndy Whitcroft print "$filename " if ($summary_file); 67806c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 67816c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 67826c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 67836c72ffaaSAndy Whitcroft } 67848905a67cSAndy Whitcroft 6785d2c0a235SAndy Whitcroft if ($quiet == 0) { 6786ef212196SJoe Perches # If there were any defects found and not already fixing them 6787ef212196SJoe Perches if (!$clean and !$fix) { 6788ef212196SJoe Perches print << "EOM" 6789ef212196SJoe Perches 6790ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6791ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6792ef212196SJoe PerchesEOM 6793ef212196SJoe Perches } 6794d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6795d2c0a235SAndy Whitcroft # then suggest that. 6796d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6797b0781216SMike Frysinger $rpt_cleaners = 0; 6798d8469f16SJoe Perches print << "EOM" 6799d8469f16SJoe Perches 6800d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6801d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6802d8469f16SJoe PerchesEOM 6803d2c0a235SAndy Whitcroft } 6804d2c0a235SAndy Whitcroft } 6805d2c0a235SAndy Whitcroft 6806d752fcc8SJoe Perches if ($clean == 0 && $fix && 6807d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6808d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 68099624b8d6SJoe Perches my $newfile = $filename; 68109624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 68113705ce5bSJoe Perches my $linecount = 0; 68123705ce5bSJoe Perches my $f; 68133705ce5bSJoe Perches 6814d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6815d752fcc8SJoe Perches 68163705ce5bSJoe Perches open($f, '>', $newfile) 68173705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 68183705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 68193705ce5bSJoe Perches $linecount++; 68203705ce5bSJoe Perches if ($file) { 68213705ce5bSJoe Perches if ($linecount > 3) { 68223705ce5bSJoe Perches $fixed_line =~ s/^\+//; 68233705ce5bSJoe Perches print $f $fixed_line . "\n"; 68243705ce5bSJoe Perches } 68253705ce5bSJoe Perches } else { 68263705ce5bSJoe Perches print $f $fixed_line . "\n"; 68273705ce5bSJoe Perches } 68283705ce5bSJoe Perches } 68293705ce5bSJoe Perches close($f); 68303705ce5bSJoe Perches 68313705ce5bSJoe Perches if (!$quiet) { 68323705ce5bSJoe Perches print << "EOM"; 6833d8469f16SJoe Perches 68343705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 68353705ce5bSJoe Perches 68363705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 68373705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 68383705ce5bSJoe Perches 68393705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 68403705ce5bSJoe PerchesNo warranties, expressed or implied... 68413705ce5bSJoe PerchesEOM 68423705ce5bSJoe Perches } 68433705ce5bSJoe Perches } 68443705ce5bSJoe Perches 6845d8469f16SJoe Perches if ($quiet == 0) { 6846d8469f16SJoe Perches print "\n"; 6847d8469f16SJoe Perches if ($clean == 1) { 6848d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6849d8469f16SJoe Perches } else { 6850d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 68510a920b5bSAndy Whitcroft } 68520a920b5bSAndy Whitcroft } 68530a920b5bSAndy Whitcroft return $clean; 68540a920b5bSAndy Whitcroft} 6855