1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl 2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit) 300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit) 42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) 5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]> 60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2 70a920b5bSAndy Whitcroft 80a920b5bSAndy Whitcroftuse strict; 9cb77f0d6SKamil Rytarowskiuse warnings; 10c707a81dSJoe Perchesuse POSIX; 1136061e38SJoe Perchesuse File::Basename; 1236061e38SJoe Perchesuse Cwd 'abs_path'; 1357230297SJoe Perchesuse Term::ANSIColor qw(:constants); 140a920b5bSAndy Whitcroft 150a920b5bSAndy Whitcroftmy $P = $0; 1636061e38SJoe Perchesmy $D = dirname(abs_path($P)); 170a920b5bSAndy Whitcroft 18000d1cc1SJoe Perchesmy $V = '0.32'; 190a920b5bSAndy Whitcroft 200a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 210a920b5bSAndy Whitcroft 220a920b5bSAndy Whitcroftmy $quiet = 0; 230a920b5bSAndy Whitcroftmy $tree = 1; 240a920b5bSAndy Whitcroftmy $chk_signoff = 1; 250a920b5bSAndy Whitcroftmy $chk_patch = 1; 26773647a0SAndy Whitcroftmy $tst_only; 276c72ffaaSAndy Whitcroftmy $emacs = 0; 288905a67cSAndy Whitcroftmy $terse = 0; 2934d8815fSJoe Perchesmy $showfile = 0; 306c72ffaaSAndy Whitcroftmy $file = 0; 314a593c34SDu, Changbinmy $git = 0; 320dea9f1eSJoe Perchesmy %git_commits = (); 336c72ffaaSAndy Whitcroftmy $check = 0; 342ac73b4fSJoe Perchesmy $check_orig = 0; 358905a67cSAndy Whitcroftmy $summary = 1; 368905a67cSAndy Whitcroftmy $mailback = 0; 3713214adfSAndy Whitcroftmy $summary_file = 0; 38000d1cc1SJoe Perchesmy $show_types = 0; 393beb42ecSJoe Perchesmy $list_types = 0; 403705ce5bSJoe Perchesmy $fix = 0; 419624b8d6SJoe Perchesmy $fix_inplace = 0; 426c72ffaaSAndy Whitcroftmy $root; 43c2fdda0dSAndy Whitcroftmy %debug; 443445686aSJoe Perchesmy %camelcase = (); 4591bfe484SJoe Perchesmy %use_type = (); 4691bfe484SJoe Perchesmy @use = (); 4791bfe484SJoe Perchesmy %ignore_type = (); 48000d1cc1SJoe Perchesmy @ignore = (); 4977f5b10aSHannes Edermy $help = 0; 50000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 516cd7f386SJoe Perchesmy $max_line_length = 80; 52d62a201fSDave Hansenmy $ignore_perl_version = 0; 53d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5456193274SVadim Bendeburymy $min_conf_desc_length = 4; 5566b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 56ebfd7d62SJoe Perchesmy $codespell = 0; 57f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 58bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch"; 5975ad8c57SJerome Forissiermy $typedefsfile = ""; 60737c0767SJohn Brooksmy $color = "auto"; 61dadf680dSJoe Perchesmy $allow_c99_comments = 1; 6277f5b10aSHannes Eder 6377f5b10aSHannes Edersub help { 6477f5b10aSHannes Eder my ($exitcode) = @_; 6577f5b10aSHannes Eder 6677f5b10aSHannes Eder print << "EOM"; 6777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 6877f5b10aSHannes EderVersion: $V 6977f5b10aSHannes Eder 7077f5b10aSHannes EderOptions: 7177f5b10aSHannes Eder -q, --quiet quiet 7277f5b10aSHannes Eder --no-tree run without a kernel tree 7377f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 7477f5b10aSHannes Eder --patch treat FILE as patchfile (default) 7577f5b10aSHannes Eder --emacs emacs compile window format 7677f5b10aSHannes Eder --terse one line per report 7734d8815fSJoe Perches --showfile emit diffed file position, not input file position 784a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 794a593c34SDu, Changbin single git commit with: 804a593c34SDu, Changbin <rev> 814a593c34SDu, Changbin <rev>^ 824a593c34SDu, Changbin <rev>~n 834a593c34SDu, Changbin multiple git commits with: 844a593c34SDu, Changbin <rev1>..<rev2> 854a593c34SDu, Changbin <rev1>...<rev2> 864a593c34SDu, Changbin <rev>-<count> 874a593c34SDu, Changbin git merges are ignored 8877f5b10aSHannes Eder -f, --file treat FILE as regular source file 8977f5b10aSHannes Eder --subjective, --strict enable more subjective tests 903beb42ecSJoe Perches --list-types list the possible message types 9191bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 92000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 933beb42ecSJoe Perches --show-types show the specific message type in the output 946cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 9556193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 9677f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 9777f5b10aSHannes Eder --no-summary suppress the per-file summary 9877f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 9977f5b10aSHannes Eder --summary-file include the filename in summary 10077f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 10177f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 10277f5b10aSHannes Eder is all off) 10377f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 10477f5b10aSHannes Eder literally 1053705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1063705ce5bSJoe Perches If correctable single-line errors exist, create 1073705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1083705ce5bSJoe Perches with potential errors corrected to the preferred 1093705ce5bSJoe Perches checkpatch style 1109624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1119624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1129624b8d6SJoe Perches file. It's your fault if there's no backup or git 113d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 114d62a201fSDave Hansen runtime errors. 115ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 116f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 117ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 11875ad8c57SJerome Forissier --typedefsfile Read additional types from this file 119737c0767SJohn Brooks --color[=WHEN] Use colors 'always', 'never', or only when output 120737c0767SJohn Brooks is a terminal ('auto'). Default is 'auto'. 12177f5b10aSHannes Eder -h, --help, --version display this help and exit 12277f5b10aSHannes Eder 12377f5b10aSHannes EderWhen FILE is - read standard input. 12477f5b10aSHannes EderEOM 12577f5b10aSHannes Eder 12677f5b10aSHannes Eder exit($exitcode); 12777f5b10aSHannes Eder} 12877f5b10aSHannes Eder 1293beb42ecSJoe Perchessub uniq { 1303beb42ecSJoe Perches my %seen; 1313beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1323beb42ecSJoe Perches} 1333beb42ecSJoe Perches 1343beb42ecSJoe Perchessub list_types { 1353beb42ecSJoe Perches my ($exitcode) = @_; 1363beb42ecSJoe Perches 1373beb42ecSJoe Perches my $count = 0; 1383beb42ecSJoe Perches 1393beb42ecSJoe Perches local $/ = undef; 1403beb42ecSJoe Perches 1413beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1423beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1433beb42ecSJoe Perches 1443beb42ecSJoe Perches my $text = <$script>; 1453beb42ecSJoe Perches close($script); 1463beb42ecSJoe Perches 1473beb42ecSJoe Perches my @types = (); 1480547fa58SJean Delvare # Also catch when type or level is passed through a variable 1490547fa58SJean Delvare for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { 1503beb42ecSJoe Perches push (@types, $_); 1513beb42ecSJoe Perches } 1523beb42ecSJoe Perches @types = sort(uniq(@types)); 1533beb42ecSJoe Perches print("#\tMessage type\n\n"); 1543beb42ecSJoe Perches foreach my $type (@types) { 1553beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1563beb42ecSJoe Perches } 1573beb42ecSJoe Perches 1583beb42ecSJoe Perches exit($exitcode); 1593beb42ecSJoe Perches} 1603beb42ecSJoe Perches 161000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 162000d1cc1SJoe Perchesif (-f $conf) { 163000d1cc1SJoe Perches my @conf_args; 164000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 165000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 166000d1cc1SJoe Perches 167000d1cc1SJoe Perches while (<$conffile>) { 168000d1cc1SJoe Perches my $line = $_; 169000d1cc1SJoe Perches 170000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 171000d1cc1SJoe Perches $line =~ s/^\s*//g; 172000d1cc1SJoe Perches $line =~ s/\s+/ /g; 173000d1cc1SJoe Perches 174000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 175000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 176000d1cc1SJoe Perches 177000d1cc1SJoe Perches my @words = split(" ", $line); 178000d1cc1SJoe Perches foreach my $word (@words) { 179000d1cc1SJoe Perches last if ($word =~ m/^#/); 180000d1cc1SJoe Perches push (@conf_args, $word); 181000d1cc1SJoe Perches } 182000d1cc1SJoe Perches } 183000d1cc1SJoe Perches close($conffile); 184000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 185000d1cc1SJoe Perches} 186000d1cc1SJoe Perches 187737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space. 188737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments 189737c0767SJohn Brooksforeach (@ARGV) { 190737c0767SJohn Brooks if ($_ eq "--color" || $_ eq "-color") { 191737c0767SJohn Brooks $_ = "--color=$color"; 192737c0767SJohn Brooks } 193737c0767SJohn Brooks} 194737c0767SJohn Brooks 1950a920b5bSAndy WhitcroftGetOptions( 1966c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1970a920b5bSAndy Whitcroft 'tree!' => \$tree, 1980a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1990a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 2006c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 2018905a67cSAndy Whitcroft 'terse!' => \$terse, 20234d8815fSJoe Perches 'showfile!' => \$showfile, 20377f5b10aSHannes Eder 'f|file!' => \$file, 2044a593c34SDu, Changbin 'g|git!' => \$git, 2056c72ffaaSAndy Whitcroft 'subjective!' => \$check, 2066c72ffaaSAndy Whitcroft 'strict!' => \$check, 207000d1cc1SJoe Perches 'ignore=s' => \@ignore, 20891bfe484SJoe Perches 'types=s' => \@use, 209000d1cc1SJoe Perches 'show-types!' => \$show_types, 2103beb42ecSJoe Perches 'list-types!' => \$list_types, 2116cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 21256193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 2136c72ffaaSAndy Whitcroft 'root=s' => \$root, 2148905a67cSAndy Whitcroft 'summary!' => \$summary, 2158905a67cSAndy Whitcroft 'mailback!' => \$mailback, 21613214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2173705ce5bSJoe Perches 'fix!' => \$fix, 2189624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 219d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 220c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 221773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 222ebfd7d62SJoe Perches 'codespell!' => \$codespell, 223ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 22475ad8c57SJerome Forissier 'typedefsfile=s' => \$typedefsfile, 225737c0767SJohn Brooks 'color=s' => \$color, 226737c0767SJohn Brooks 'no-color' => \$color, #keep old behaviors of -nocolor 227737c0767SJohn Brooks 'nocolor' => \$color, #keep old behaviors of -nocolor 22877f5b10aSHannes Eder 'h|help' => \$help, 22977f5b10aSHannes Eder 'version' => \$help 23077f5b10aSHannes Eder) or help(1); 23177f5b10aSHannes Eder 23277f5b10aSHannes Ederhelp(0) if ($help); 2330a920b5bSAndy Whitcroft 2343beb42ecSJoe Percheslist_types(0) if ($list_types); 2353beb42ecSJoe Perches 2369624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2372ac73b4fSJoe Perches$check_orig = $check; 2389624b8d6SJoe Perches 2390a920b5bSAndy Whitcroftmy $exit = 0; 2400a920b5bSAndy Whitcroft 241d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 242d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 243d62a201fSDave Hansen if (!$ignore_perl_version) { 244d62a201fSDave Hansen exit(1); 245d62a201fSDave Hansen } 246d62a201fSDave Hansen} 247d62a201fSDave Hansen 24845107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin 2490a920b5bSAndy Whitcroftif ($#ARGV < 0) { 25045107ff6SAllen Hubbe push(@ARGV, '-'); 2510a920b5bSAndy Whitcroft} 2520a920b5bSAndy Whitcroft 253737c0767SJohn Brooksif ($color =~ /^[01]$/) { 254737c0767SJohn Brooks $color = !$color; 255737c0767SJohn Brooks} elsif ($color =~ /^always$/i) { 256737c0767SJohn Brooks $color = 1; 257737c0767SJohn Brooks} elsif ($color =~ /^never$/i) { 258737c0767SJohn Brooks $color = 0; 259737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) { 260737c0767SJohn Brooks $color = (-t STDOUT); 261737c0767SJohn Brooks} else { 262737c0767SJohn Brooks die "Invalid color mode: $color\n"; 263737c0767SJohn Brooks} 264737c0767SJohn Brooks 26591bfe484SJoe Perchessub hash_save_array_words { 26691bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 26791bfe484SJoe Perches 26891bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 26991bfe484SJoe Perches foreach my $word (@array) { 270000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 271000d1cc1SJoe Perches $word =~ s/^\s*//g; 272000d1cc1SJoe Perches $word =~ s/\s+/ /g; 273000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 274000d1cc1SJoe Perches 275000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 276000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 277000d1cc1SJoe Perches 27891bfe484SJoe Perches $hashRef->{$word}++; 279000d1cc1SJoe Perches } 28091bfe484SJoe Perches} 28191bfe484SJoe Perches 28291bfe484SJoe Perchessub hash_show_words { 28391bfe484SJoe Perches my ($hashRef, $prefix) = @_; 28491bfe484SJoe Perches 2853c816e49SJoe Perches if (keys %$hashRef) { 286d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 28758cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 28891bfe484SJoe Perches print " $word"; 28991bfe484SJoe Perches } 290d8469f16SJoe Perches print "\n"; 29191bfe484SJoe Perches } 29291bfe484SJoe Perches} 29391bfe484SJoe Perches 29491bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 29591bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 296000d1cc1SJoe Perches 297c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 298c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 2997429c690SAndy Whitcroftmy $dbg_type = 0; 300a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 301c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 30221caa13cSAndy Whitcroft ## no critic 30321caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 30421caa13cSAndy Whitcroft die "$@" if ($@); 305c2fdda0dSAndy Whitcroft} 306c2fdda0dSAndy Whitcroft 307d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 308d2c0a235SAndy Whitcroft 3098905a67cSAndy Whitcroftif ($terse) { 3108905a67cSAndy Whitcroft $emacs = 1; 3118905a67cSAndy Whitcroft $quiet++; 3128905a67cSAndy Whitcroft} 3138905a67cSAndy Whitcroft 3146c72ffaaSAndy Whitcroftif ($tree) { 3156c72ffaaSAndy Whitcroft if (defined $root) { 3166c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 3176c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 3186c72ffaaSAndy Whitcroft } 3196c72ffaaSAndy Whitcroft } else { 3206c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 3216c72ffaaSAndy Whitcroft $root = '.'; 3226c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 3236c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 3246c72ffaaSAndy Whitcroft $root = $1; 3256c72ffaaSAndy Whitcroft } 3266c72ffaaSAndy Whitcroft } 3276c72ffaaSAndy Whitcroft 3286c72ffaaSAndy Whitcroft if (!defined $root) { 3290a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3300a920b5bSAndy Whitcroft exit(2); 3310a920b5bSAndy Whitcroft } 3326c72ffaaSAndy Whitcroft} 3336c72ffaaSAndy Whitcroft 3346c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3356c72ffaaSAndy Whitcroft 3362ceb532bSAndy Whitcroftour $Ident = qr{ 3372ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3382ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3392ceb532bSAndy Whitcroft }x; 3406c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3416c72ffaaSAndy Whitcroftour $Sparse = qr{ 3426c72ffaaSAndy Whitcroft __user| 3436c72ffaaSAndy Whitcroft __kernel| 3446c72ffaaSAndy Whitcroft __force| 3456c72ffaaSAndy Whitcroft __iomem| 3466c72ffaaSAndy Whitcroft __must_check| 3476c72ffaaSAndy Whitcroft __init_refok| 348417495edSAndy Whitcroft __kprobes| 349165e72a6SSven Eckelmann __ref| 350ad315455SBoqun Feng __rcu| 351ad315455SBoqun Feng __private 3526c72ffaaSAndy Whitcroft }x; 353e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 354e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 355e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 356e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 357e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3588716de38SJoe Perches 35952131292SWolfram Sang# Notes to $Attribute: 36052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3616c72ffaaSAndy Whitcroftour $Attribute = qr{ 3626c72ffaaSAndy Whitcroft const| 36303f1df7dSJoe Perches __percpu| 36403f1df7dSJoe Perches __nocast| 36503f1df7dSJoe Perches __safe| 36646d832f5SMichael S. Tsirkin __bitwise| 36703f1df7dSJoe Perches __packed__| 36803f1df7dSJoe Perches __packed2__| 36903f1df7dSJoe Perches __naked| 37003f1df7dSJoe Perches __maybe_unused| 37103f1df7dSJoe Perches __always_unused| 37203f1df7dSJoe Perches __noreturn| 37303f1df7dSJoe Perches __used| 37403f1df7dSJoe Perches __cold| 375e23ef1f3SJoe Perches __pure| 37603f1df7dSJoe Perches __noclone| 37703f1df7dSJoe Perches __deprecated| 3786c72ffaaSAndy Whitcroft __read_mostly| 3796c72ffaaSAndy Whitcroft __kprobes| 3808716de38SJoe Perches $InitAttribute| 38124e1d81aSAndy Whitcroft ____cacheline_aligned| 38224e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3835fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3845fe3af11SAndy Whitcroft __weak 3856c72ffaaSAndy Whitcroft }x; 386c45dcabdSAndy Whitcroftour $Modifier; 38791cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 3886c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 3896c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 3906c72ffaaSAndy Whitcroft 39195e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 39295e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 39395e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 39495e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 3952435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 396c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 397326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 398326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 399326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 40074349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 4012435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 402326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 403447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 40423f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 4056c72ffaaSAndy Whitcroftour $Operators = qr{ 4066c72ffaaSAndy Whitcroft <=|>=|==|!=| 4076c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 40823f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 4096c72ffaaSAndy Whitcroft }x; 4106c72ffaaSAndy Whitcroft 41191cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 41291cb5195SJoe Perches 413ab7e23f3SJoe Perchesour $BasicType; 4148905a67cSAndy Whitcroftour $NonptrType; 4151813087dSJoe Perchesour $NonptrTypeMisordered; 4168716de38SJoe Perchesour $NonptrTypeWithAttr; 4178905a67cSAndy Whitcroftour $Type; 4181813087dSJoe Perchesour $TypeMisordered; 4198905a67cSAndy Whitcroftour $Declare; 4201813087dSJoe Perchesour $DeclareMisordered; 4218905a67cSAndy Whitcroft 42215662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 42315662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 424171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 425171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 426171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 427171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 428171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 429171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 430171ae1a4SAndy Whitcroft}x; 431171ae1a4SAndy Whitcroft 43215662b3eSJoe Perchesour $UTF8 = qr{ 43315662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 43415662b3eSJoe Perches | $NON_ASCII_UTF8 43515662b3eSJoe Perches}x; 43615662b3eSJoe Perches 437e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 438021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 439021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 440021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 441021158b4SJoe Perches)}; 442e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 443fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4448ed22cadSAndy Whitcroft atomic_t 4458ed22cadSAndy Whitcroft)}; 446e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 447e6176fa4SJoe Perches $typeC99Typedefs\b| 448e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 449e6176fa4SJoe Perches $typeKernelTypedefs\b 450e6176fa4SJoe Perches)}; 4518ed22cadSAndy Whitcroft 4526d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4536d32f7a3SJoe Perches 454691e669bSJoe Perchesour $logFunctions = qr{(?x: 455758d7aadSMiles Chen printk(?:_ratelimited|_once|_deferred_once|_deferred|)| 4567d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 45787bd499aSJoe Perches TP_printk| 4586e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 459b0531722SJoe Perches panic| 46006668727SJoe Perches MODULE_[A-Z_]+| 46106668727SJoe Perches seq_vprintf|seq_printf|seq_puts 462691e669bSJoe Perches)}; 463691e669bSJoe Perches 46420112475SJoe Perchesour $signature_tags = qr{(?xi: 46520112475SJoe Perches Signed-off-by:| 46620112475SJoe Perches Acked-by:| 46720112475SJoe Perches Tested-by:| 46820112475SJoe Perches Reviewed-by:| 46920112475SJoe Perches Reported-by:| 4708543ae12SMugunthan V N Suggested-by:| 47120112475SJoe Perches To:| 47220112475SJoe Perches Cc: 47320112475SJoe Perches)}; 47420112475SJoe Perches 4751813087dSJoe Perchesour @typeListMisordered = ( 4761813087dSJoe Perches qr{char\s+(?:un)?signed}, 4771813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 4781813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 4791813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 4801813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 4811813087dSJoe Perches qr{short\s+(?:un)?signed}, 4821813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 4831813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 4841813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 4851813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 4861813087dSJoe Perches qr{int\s+(?:un)?signed}, 4871813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 4881813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 4891813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 4901813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 4911813087dSJoe Perches qr{long\s+(?:un)?signed}, 4921813087dSJoe Perches); 4931813087dSJoe Perches 4948905a67cSAndy Whitcroftour @typeList = ( 4958905a67cSAndy Whitcroft qr{void}, 4960c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 4970c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 4980c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 4990c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 5000c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 5010c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 5020c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 5030c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 5040c773d9dSJoe Perches qr{(?:un)?signed}, 5058905a67cSAndy Whitcroft qr{float}, 5068905a67cSAndy Whitcroft qr{double}, 5078905a67cSAndy Whitcroft qr{bool}, 5088905a67cSAndy Whitcroft qr{struct\s+$Ident}, 5098905a67cSAndy Whitcroft qr{union\s+$Ident}, 5108905a67cSAndy Whitcroft qr{enum\s+$Ident}, 5118905a67cSAndy Whitcroft qr{${Ident}_t}, 5128905a67cSAndy Whitcroft qr{${Ident}_handler}, 5138905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 5141813087dSJoe Perches @typeListMisordered, 5158905a67cSAndy Whitcroft); 516938224b5SJoe Perches 517938224b5SJoe Perchesour $C90_int_types = qr{(?x: 518938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 519938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 520938224b5SJoe Perches long\s+long\s+(?:un)?signed| 521938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 522938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 523938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 524938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 525938224b5SJoe Perches 526938224b5SJoe Perches long\s+int\s+(?:un)?signed| 527938224b5SJoe Perches long\s+(?:un)?signed\s+int| 528938224b5SJoe Perches long\s+(?:un)?signed| 529938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 530938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 531938224b5SJoe Perches int\s+long\s+(?:un)?signed| 532938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 533938224b5SJoe Perches 534938224b5SJoe Perches int\s+(?:un)?signed| 535938224b5SJoe Perches (?:(?:un)?signed\s+)?int 536938224b5SJoe Perches)}; 537938224b5SJoe Perches 538485ff23eSAlex Dowadour @typeListFile = (); 5398716de38SJoe Perchesour @typeListWithAttr = ( 5408716de38SJoe Perches @typeList, 5418716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5428716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5438716de38SJoe Perches); 5448716de38SJoe Perches 545c45dcabdSAndy Whitcroftour @modifierList = ( 546c45dcabdSAndy Whitcroft qr{fastcall}, 547c45dcabdSAndy Whitcroft); 548485ff23eSAlex Dowadour @modifierListFile = (); 5498905a67cSAndy Whitcroft 5502435880fSJoe Perchesour @mode_permission_funcs = ( 5512435880fSJoe Perches ["module_param", 3], 5522435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5532435880fSJoe Perches ["module_param_array_named", 5], 5542435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5552435880fSJoe Perches ["proc_create(?:_data|)", 2], 556459cf0aeSJoe Perches ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], 557459cf0aeSJoe Perches ["IIO_DEV_ATTR_[A-Z_]+", 1], 558459cf0aeSJoe Perches ["SENSOR_(?:DEVICE_|)ATTR_2", 2], 559459cf0aeSJoe Perches ["SENSOR_TEMPLATE(?:_2|)", 3], 560459cf0aeSJoe Perches ["__ATTR", 2], 5612435880fSJoe Perches); 5622435880fSJoe Perches 563515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 564515a235eSJoe Perchesour $mode_perms_search = ""; 565515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 566515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 567515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 568515a235eSJoe Perches} 56900180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})"; 570515a235eSJoe Perches 571b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 572b392c64fSJoe Perches S_IWUGO | 573b392c64fSJoe Perches S_IWOTH | 574b392c64fSJoe Perches S_IRWXUGO | 575b392c64fSJoe Perches S_IALLUGO | 576b392c64fSJoe Perches 0[0-7][0-7][2367] 577b392c64fSJoe Perches}x; 578b392c64fSJoe Perches 579f90774e1SJoe Perchesour %mode_permission_string_types = ( 580f90774e1SJoe Perches "S_IRWXU" => 0700, 581f90774e1SJoe Perches "S_IRUSR" => 0400, 582f90774e1SJoe Perches "S_IWUSR" => 0200, 583f90774e1SJoe Perches "S_IXUSR" => 0100, 584f90774e1SJoe Perches "S_IRWXG" => 0070, 585f90774e1SJoe Perches "S_IRGRP" => 0040, 586f90774e1SJoe Perches "S_IWGRP" => 0020, 587f90774e1SJoe Perches "S_IXGRP" => 0010, 588f90774e1SJoe Perches "S_IRWXO" => 0007, 589f90774e1SJoe Perches "S_IROTH" => 0004, 590f90774e1SJoe Perches "S_IWOTH" => 0002, 591f90774e1SJoe Perches "S_IXOTH" => 0001, 592f90774e1SJoe Perches "S_IRWXUGO" => 0777, 593f90774e1SJoe Perches "S_IRUGO" => 0444, 594f90774e1SJoe Perches "S_IWUGO" => 0222, 595f90774e1SJoe Perches "S_IXUGO" => 0111, 596f90774e1SJoe Perches); 597f90774e1SJoe Perches 598f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below 599f90774e1SJoe Perchesour $mode_perms_string_search = ""; 600f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) { 601f90774e1SJoe Perches $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); 602f90774e1SJoe Perches $mode_perms_string_search .= $entry; 603f90774e1SJoe Perches} 60400180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; 60500180468SJoe Perchesour $multi_mode_perms_string_search = qr{ 60600180468SJoe Perches ${single_mode_perms_string_search} 60700180468SJoe Perches (?:\s*\|\s*${single_mode_perms_string_search})* 60800180468SJoe Perches}x; 60900180468SJoe Perches 61000180468SJoe Perchessub perms_to_octal { 61100180468SJoe Perches my ($string) = @_; 61200180468SJoe Perches 61300180468SJoe Perches return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); 61400180468SJoe Perches 61500180468SJoe Perches my $val = ""; 61600180468SJoe Perches my $oval = ""; 61700180468SJoe Perches my $to = 0; 61800180468SJoe Perches my $curpos = 0; 61900180468SJoe Perches my $lastpos = 0; 62000180468SJoe Perches while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { 62100180468SJoe Perches $curpos = pos($string); 62200180468SJoe Perches my $match = $2; 62300180468SJoe Perches my $omatch = $1; 62400180468SJoe Perches last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); 62500180468SJoe Perches $lastpos = $curpos; 62600180468SJoe Perches $to |= $mode_permission_string_types{$match}; 62700180468SJoe Perches $val .= '\s*\|\s*' if ($val ne ""); 62800180468SJoe Perches $val .= $match; 62900180468SJoe Perches $oval .= $omatch; 63000180468SJoe Perches } 63100180468SJoe Perches $oval =~ s/^\s*\|\s*//; 63200180468SJoe Perches $oval =~ s/\s*\|\s*$//; 63300180468SJoe Perches return sprintf("%04o", $to); 63400180468SJoe Perches} 635f90774e1SJoe Perches 6367840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 6377840a94cSWolfram Sang irq| 638cdcee686SSergey Ryazanov memory| 639cdcee686SSergey Ryazanov time| 640cdcee686SSergey Ryazanov reboot 6417840a94cSWolfram Sang)}; 6427840a94cSWolfram Sang# memory.h: ARM has a custom one 6437840a94cSWolfram Sang 64466b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 64566b47b4aSKees Cookmy $misspellings; 64666b47b4aSKees Cookmy %spelling_fix; 64736061e38SJoe Perches 64836061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 64966b47b4aSKees Cook while (<$spelling>) { 65066b47b4aSKees Cook my $line = $_; 65166b47b4aSKees Cook 65266b47b4aSKees Cook $line =~ s/\s*\n?$//g; 65366b47b4aSKees Cook $line =~ s/^\s*//g; 65466b47b4aSKees Cook 65566b47b4aSKees Cook next if ($line =~ m/^\s*#/); 65666b47b4aSKees Cook next if ($line =~ m/^\s*$/); 65766b47b4aSKees Cook 65866b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 65966b47b4aSKees Cook 66066b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 66166b47b4aSKees Cook } 66266b47b4aSKees Cook close($spelling); 66336061e38SJoe Perches} else { 66436061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 66536061e38SJoe Perches} 66666b47b4aSKees Cook 667ebfd7d62SJoe Perchesif ($codespell) { 668ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 669ebfd7d62SJoe Perches while (<$spelling>) { 670ebfd7d62SJoe Perches my $line = $_; 671ebfd7d62SJoe Perches 672ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 673ebfd7d62SJoe Perches $line =~ s/^\s*//g; 674ebfd7d62SJoe Perches 675ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 676ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 677ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 678ebfd7d62SJoe Perches 679ebfd7d62SJoe Perches $line =~ s/,.*$//; 680ebfd7d62SJoe Perches 681ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 682ebfd7d62SJoe Perches 683ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 684ebfd7d62SJoe Perches } 685ebfd7d62SJoe Perches close($spelling); 686ebfd7d62SJoe Perches } else { 687ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 688ebfd7d62SJoe Perches } 689ebfd7d62SJoe Perches} 690ebfd7d62SJoe Perches 691ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 692ebfd7d62SJoe Perches 69375ad8c57SJerome Forissiersub read_words { 69475ad8c57SJerome Forissier my ($wordsRef, $file) = @_; 69575ad8c57SJerome Forissier 69675ad8c57SJerome Forissier if (open(my $words, '<', $file)) { 69775ad8c57SJerome Forissier while (<$words>) { 698bf1fa1daSJoe Perches my $line = $_; 699bf1fa1daSJoe Perches 700bf1fa1daSJoe Perches $line =~ s/\s*\n?$//g; 701bf1fa1daSJoe Perches $line =~ s/^\s*//g; 702bf1fa1daSJoe Perches 703bf1fa1daSJoe Perches next if ($line =~ m/^\s*#/); 704bf1fa1daSJoe Perches next if ($line =~ m/^\s*$/); 705bf1fa1daSJoe Perches if ($line =~ /\s/) { 70675ad8c57SJerome Forissier print("$file: '$line' invalid - ignored\n"); 707bf1fa1daSJoe Perches next; 708bf1fa1daSJoe Perches } 709bf1fa1daSJoe Perches 71075ad8c57SJerome Forissier $$wordsRef .= '|' if ($$wordsRef ne ""); 71175ad8c57SJerome Forissier $$wordsRef .= $line; 712bf1fa1daSJoe Perches } 71375ad8c57SJerome Forissier close($file); 71475ad8c57SJerome Forissier return 1; 715bf1fa1daSJoe Perches } 716bf1fa1daSJoe Perches 71775ad8c57SJerome Forissier return 0; 71875ad8c57SJerome Forissier} 71975ad8c57SJerome Forissier 72075ad8c57SJerome Forissiermy $const_structs = ""; 72175ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile) 72275ad8c57SJerome Forissier or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; 72375ad8c57SJerome Forissier 72475ad8c57SJerome Forissiermy $typeOtherTypedefs = ""; 72575ad8c57SJerome Forissierif (length($typedefsfile)) { 72675ad8c57SJerome Forissier read_words(\$typeOtherTypedefs, $typedefsfile) 72775ad8c57SJerome Forissier or warn "No additional types will be considered - file '$typedefsfile': $!\n"; 72875ad8c57SJerome Forissier} 72975ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); 73075ad8c57SJerome Forissier 7318905a67cSAndy Whitcroftsub build_types { 732485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 733485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 7341813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 7358716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 736c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 737ab7e23f3SJoe Perches $BasicType = qr{ 738ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 739ab7e23f3SJoe Perches (?:${all}\b) 740ab7e23f3SJoe Perches }x; 7418905a67cSAndy Whitcroft $NonptrType = qr{ 742d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 743cf655043SAndy Whitcroft (?: 7446b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 7458ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 746c45dcabdSAndy Whitcroft (?:${all}\b) 747cf655043SAndy Whitcroft ) 748c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 7498905a67cSAndy Whitcroft }x; 7501813087dSJoe Perches $NonptrTypeMisordered = qr{ 7511813087dSJoe Perches (?:$Modifier\s+|const\s+)* 7521813087dSJoe Perches (?: 7531813087dSJoe Perches (?:${Misordered}\b) 7541813087dSJoe Perches ) 7551813087dSJoe Perches (?:\s+$Modifier|\s+const)* 7561813087dSJoe Perches }x; 7578716de38SJoe Perches $NonptrTypeWithAttr = qr{ 7588716de38SJoe Perches (?:$Modifier\s+|const\s+)* 7598716de38SJoe Perches (?: 7608716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 7618716de38SJoe Perches (?:$typeTypedefs\b)| 7628716de38SJoe Perches (?:${allWithAttr}\b) 7638716de38SJoe Perches ) 7648716de38SJoe Perches (?:\s+$Modifier|\s+const)* 7658716de38SJoe Perches }x; 7668905a67cSAndy Whitcroft $Type = qr{ 767c45dcabdSAndy Whitcroft $NonptrType 7681574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 769c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 7708905a67cSAndy Whitcroft }x; 7711813087dSJoe Perches $TypeMisordered = qr{ 7721813087dSJoe Perches $NonptrTypeMisordered 7731813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 7741813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 7751813087dSJoe Perches }x; 77691cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 7771813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 7788905a67cSAndy Whitcroft} 7798905a67cSAndy Whitcroftbuild_types(); 7806c72ffaaSAndy Whitcroft 7817d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 782d1fe9c09SJoe Perches 783d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 784d1fe9c09SJoe Perches# requires at least perl version v5.10.0 785d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 786d1fe9c09SJoe Perches 787d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 7882435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 789c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 7907d2367afSJoe Perches 791f8422308SJoe Perchesour $declaration_macros = qr{(?x: 7923e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 793fe658f94SSteffen Maier (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| 7943d102fc0SGilad Ben-Yossef (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(| 7953d102fc0SGilad Ben-Yossef (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( 796f8422308SJoe Perches)}; 797f8422308SJoe Perches 7987d2367afSJoe Perchessub deparenthesize { 7997d2367afSJoe Perches my ($string) = @_; 8007d2367afSJoe Perches return "" if (!defined($string)); 8015b9553abSJoe Perches 8025b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 8035b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 8045b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 8055b9553abSJoe Perches } 8065b9553abSJoe Perches 8077d2367afSJoe Perches $string =~ s@\s+@ @g; 8085b9553abSJoe Perches 8097d2367afSJoe Perches return $string; 8107d2367afSJoe Perches} 8117d2367afSJoe Perches 8123445686aSJoe Perchessub seed_camelcase_file { 8133445686aSJoe Perches my ($file) = @_; 8143445686aSJoe Perches 8153445686aSJoe Perches return if (!(-f $file)); 8163445686aSJoe Perches 8173445686aSJoe Perches local $/; 8183445686aSJoe Perches 8193445686aSJoe Perches open(my $include_file, '<', "$file") 8203445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 8213445686aSJoe Perches my $text = <$include_file>; 8223445686aSJoe Perches close($include_file); 8233445686aSJoe Perches 8243445686aSJoe Perches my @lines = split('\n', $text); 8253445686aSJoe Perches 8263445686aSJoe Perches foreach my $line (@lines) { 8273445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 8283445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 8293445686aSJoe Perches $camelcase{$1} = 1; 83011ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 83111ea516aSJoe Perches $camelcase{$1} = 1; 83211ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 8333445686aSJoe Perches $camelcase{$1} = 1; 8343445686aSJoe Perches } 8353445686aSJoe Perches } 8363445686aSJoe Perches} 8373445686aSJoe Perches 83885b0ee18SJoe Perchessub is_maintained_obsolete { 83985b0ee18SJoe Perches my ($filename) = @_; 84085b0ee18SJoe Perches 841f2c19c2fSJerome Forissier return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); 84285b0ee18SJoe Perches 8430616efa4SJoe Perches my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; 84485b0ee18SJoe Perches 84585b0ee18SJoe Perches return $status =~ /obsolete/i; 84685b0ee18SJoe Perches} 84785b0ee18SJoe Perches 8483445686aSJoe Perchesmy $camelcase_seeded = 0; 8493445686aSJoe Perchessub seed_camelcase_includes { 8503445686aSJoe Perches return if ($camelcase_seeded); 8513445686aSJoe Perches 8523445686aSJoe Perches my $files; 853c707a81dSJoe Perches my $camelcase_cache = ""; 854c707a81dSJoe Perches my @include_files = (); 855c707a81dSJoe Perches 856c707a81dSJoe Perches $camelcase_seeded = 1; 857351b2a1fSJoe Perches 8583645e328SRichard Genoud if (-e ".git") { 859351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 860351b2a1fSJoe Perches chomp $git_last_include_commit; 861c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 862c707a81dSJoe Perches } else { 863c707a81dSJoe Perches my $last_mod_date = 0; 864c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 865c707a81dSJoe Perches @include_files = split('\n', $files); 866c707a81dSJoe Perches foreach my $file (@include_files) { 867c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 868c707a81dSJoe Perches localtime((stat $file)[9])); 869c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 870c707a81dSJoe Perches } 871c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 872c707a81dSJoe Perches } 873c707a81dSJoe Perches 874c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 875c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 876c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 877351b2a1fSJoe Perches while (<$camelcase_file>) { 878351b2a1fSJoe Perches chomp; 879351b2a1fSJoe Perches $camelcase{$_} = 1; 880351b2a1fSJoe Perches } 881351b2a1fSJoe Perches close($camelcase_file); 882351b2a1fSJoe Perches 883351b2a1fSJoe Perches return; 884351b2a1fSJoe Perches } 885c707a81dSJoe Perches 8863645e328SRichard Genoud if (-e ".git") { 887c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 888c707a81dSJoe Perches @include_files = split('\n', $files); 8893445686aSJoe Perches } 890c707a81dSJoe Perches 8913445686aSJoe Perches foreach my $file (@include_files) { 8923445686aSJoe Perches seed_camelcase_file($file); 8933445686aSJoe Perches } 894351b2a1fSJoe Perches 895c707a81dSJoe Perches if ($camelcase_cache ne "") { 896351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 897c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 898c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 899351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 900351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 901351b2a1fSJoe Perches } 902351b2a1fSJoe Perches close($camelcase_file); 903351b2a1fSJoe Perches } 9043445686aSJoe Perches} 9053445686aSJoe Perches 906d311cd44SJoe Perchessub git_commit_info { 907d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 908d311cd44SJoe Perches 909d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 910d311cd44SJoe Perches 911d311cd44SJoe Perches my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; 912d311cd44SJoe Perches $output =~ s/^\s*//gm; 913d311cd44SJoe Perches my @lines = split("\n", $output); 914d311cd44SJoe Perches 9150d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 9160d7835fcSJoe Perches 917d311cd44SJoe Perches if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { 918d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 919d311cd44SJoe Perches# all matching commit ids, but it's very slow... 920d311cd44SJoe Perches# 921d311cd44SJoe Perches# echo "checking commits $1..." 922d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 923d311cd44SJoe Perches# while read line ; do 924d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 925d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 926d311cd44SJoe Perches# done 927d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 928948b133aSHeinrich Schuchardt $id = undef; 929d311cd44SJoe Perches } else { 930d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 931d311cd44SJoe Perches $desc = substr($lines[0], 41); 932d311cd44SJoe Perches } 933d311cd44SJoe Perches 934d311cd44SJoe Perches return ($id, $desc); 935d311cd44SJoe Perches} 936d311cd44SJoe Perches 9376c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 9380a920b5bSAndy Whitcroft 93900df344fSAndy Whitcroftmy @rawlines = (); 940c2fdda0dSAndy Whitcroftmy @lines = (); 9413705ce5bSJoe Perchesmy @fixed = (); 942d752fcc8SJoe Perchesmy @fixed_inserted = (); 943d752fcc8SJoe Perchesmy @fixed_deleted = (); 944194f66fcSJoe Perchesmy $fixlinenr = -1; 945194f66fcSJoe Perches 9464a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 9474a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 9484a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 9494a593c34SDu, Changbin 9504a593c34SDu, Changbinif ($git) { 9514a593c34SDu, Changbin my @commits = (); 9520dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 9534a593c34SDu, Changbin my $git_range; 95428898fd1SJoe Perches if ($commit_expr =~ m/^(.*)-(\d+)$/) { 95528898fd1SJoe Perches $git_range = "-$2 $1"; 9564a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 9574a593c34SDu, Changbin $git_range = "$commit_expr"; 9584a593c34SDu, Changbin } else { 9590dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 9600dea9f1eSJoe Perches } 9610dea9f1eSJoe Perches my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 9620dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 96328898fd1SJoe Perches $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; 96428898fd1SJoe Perches next if (!defined($1) || !defined($2)); 9650dea9f1eSJoe Perches my $sha1 = $1; 9660dea9f1eSJoe Perches my $subject = $2; 9670dea9f1eSJoe Perches unshift(@commits, $sha1); 9680dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 9694a593c34SDu, Changbin } 9704a593c34SDu, Changbin } 9714a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 9724a593c34SDu, Changbin @ARGV = @commits; 9734a593c34SDu, Changbin} 9744a593c34SDu, Changbin 975c2fdda0dSAndy Whitcroftmy $vname; 9766c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 97721caa13cSAndy Whitcroft my $FILE; 9784a593c34SDu, Changbin if ($git) { 9794a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 9804a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 9814a593c34SDu, Changbin } elsif ($file) { 98221caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 9836c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 98421caa13cSAndy Whitcroft } elsif ($filename eq '-') { 98521caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 9866c72ffaaSAndy Whitcroft } else { 98721caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 9886c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 9896c72ffaaSAndy Whitcroft } 990c2fdda0dSAndy Whitcroft if ($filename eq '-') { 991c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 9924a593c34SDu, Changbin } elsif ($git) { 9930dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 994c2fdda0dSAndy Whitcroft } else { 995c2fdda0dSAndy Whitcroft $vname = $filename; 996c2fdda0dSAndy Whitcroft } 99721caa13cSAndy Whitcroft while (<$FILE>) { 9980a920b5bSAndy Whitcroft chomp; 99900df344fSAndy Whitcroft push(@rawlines, $_); 10006c72ffaaSAndy Whitcroft } 100121caa13cSAndy Whitcroft close($FILE); 1002d8469f16SJoe Perches 1003d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 1004d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1005d8469f16SJoe Perches print "$vname\n"; 1006d8469f16SJoe Perches print '-' x length($vname) . "\n"; 1007d8469f16SJoe Perches } 1008d8469f16SJoe Perches 1009c2fdda0dSAndy Whitcroft if (!process($filename)) { 10100a920b5bSAndy Whitcroft $exit = 1; 10110a920b5bSAndy Whitcroft } 101200df344fSAndy Whitcroft @rawlines = (); 101313214adfSAndy Whitcroft @lines = (); 10143705ce5bSJoe Perches @fixed = (); 1015d752fcc8SJoe Perches @fixed_inserted = (); 1016d752fcc8SJoe Perches @fixed_deleted = (); 1017194f66fcSJoe Perches $fixlinenr = -1; 1018485ff23eSAlex Dowad @modifierListFile = (); 1019485ff23eSAlex Dowad @typeListFile = (); 1020485ff23eSAlex Dowad build_types(); 10210a920b5bSAndy Whitcroft} 10220a920b5bSAndy Whitcroft 1023d8469f16SJoe Perchesif (!$quiet) { 10243c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 10253c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 10263c816e49SJoe Perches 1027d8469f16SJoe Perches if ($^V lt 5.10.0) { 1028d8469f16SJoe Perches print << "EOM" 1029d8469f16SJoe Perches 1030d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 1031d8469f16SJoe Perches An upgrade to at least perl v5.10.0 is suggested. 1032d8469f16SJoe PerchesEOM 1033d8469f16SJoe Perches } 1034d8469f16SJoe Perches if ($exit) { 1035d8469f16SJoe Perches print << "EOM" 1036d8469f16SJoe Perches 1037d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 1038d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 1039d8469f16SJoe PerchesEOM 1040d8469f16SJoe Perches } 1041d8469f16SJoe Perches} 1042d8469f16SJoe Perches 10430a920b5bSAndy Whitcroftexit($exit); 10440a920b5bSAndy Whitcroft 10450a920b5bSAndy Whitcroftsub top_of_kernel_tree { 10466c72ffaaSAndy Whitcroft my ($root) = @_; 10476c72ffaaSAndy Whitcroft 10486c72ffaaSAndy Whitcroft my @tree_check = ( 10496c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 10506c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 10516c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 10526c72ffaaSAndy Whitcroft ); 10536c72ffaaSAndy Whitcroft 10546c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 10556c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 10560a920b5bSAndy Whitcroft return 0; 10570a920b5bSAndy Whitcroft } 10586c72ffaaSAndy Whitcroft } 10596c72ffaaSAndy Whitcroft return 1; 10606c72ffaaSAndy Whitcroft} 10610a920b5bSAndy Whitcroft 106220112475SJoe Perchessub parse_email { 106320112475SJoe Perches my ($formatted_email) = @_; 106420112475SJoe Perches 106520112475SJoe Perches my $name = ""; 106620112475SJoe Perches my $address = ""; 106720112475SJoe Perches my $comment = ""; 106820112475SJoe Perches 106920112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 107020112475SJoe Perches $name = $1; 107120112475SJoe Perches $address = $2; 107220112475SJoe Perches $comment = $3 if defined $3; 107320112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 107420112475SJoe Perches $address = $1; 107520112475SJoe Perches $comment = $2 if defined $2; 107620112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 107720112475SJoe Perches $address = $1; 107820112475SJoe Perches $comment = $2 if defined $2; 107985e12066SJoe Perches $formatted_email =~ s/\Q$address\E.*$//; 108020112475SJoe Perches $name = $formatted_email; 10813705ce5bSJoe Perches $name = trim($name); 108220112475SJoe Perches $name =~ s/^\"|\"$//g; 108320112475SJoe Perches # If there's a name left after stripping spaces and 108420112475SJoe Perches # leading quotes, and the address doesn't have both 108520112475SJoe Perches # leading and trailing angle brackets, the address 108620112475SJoe Perches # is invalid. ie: 108720112475SJoe Perches # "joe smith [email protected]" bad 108820112475SJoe Perches # "joe smith <[email protected]" bad 108920112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 109020112475SJoe Perches $name = ""; 109120112475SJoe Perches $address = ""; 109220112475SJoe Perches $comment = ""; 109320112475SJoe Perches } 109420112475SJoe Perches } 109520112475SJoe Perches 10963705ce5bSJoe Perches $name = trim($name); 109720112475SJoe Perches $name =~ s/^\"|\"$//g; 10983705ce5bSJoe Perches $address = trim($address); 109920112475SJoe Perches $address =~ s/^\<|\>$//g; 110020112475SJoe Perches 110120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 110220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 110320112475SJoe Perches $name = "\"$name\""; 110420112475SJoe Perches } 110520112475SJoe Perches 110620112475SJoe Perches return ($name, $address, $comment); 110720112475SJoe Perches} 110820112475SJoe Perches 110920112475SJoe Perchessub format_email { 111020112475SJoe Perches my ($name, $address) = @_; 111120112475SJoe Perches 111220112475SJoe Perches my $formatted_email; 111320112475SJoe Perches 11143705ce5bSJoe Perches $name = trim($name); 111520112475SJoe Perches $name =~ s/^\"|\"$//g; 11163705ce5bSJoe Perches $address = trim($address); 111720112475SJoe Perches 111820112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 111920112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 112020112475SJoe Perches $name = "\"$name\""; 112120112475SJoe Perches } 112220112475SJoe Perches 112320112475SJoe Perches if ("$name" eq "") { 112420112475SJoe Perches $formatted_email = "$address"; 112520112475SJoe Perches } else { 112620112475SJoe Perches $formatted_email = "$name <$address>"; 112720112475SJoe Perches } 112820112475SJoe Perches 112920112475SJoe Perches return $formatted_email; 113020112475SJoe Perches} 113120112475SJoe Perches 1132d311cd44SJoe Perchessub which { 1133d311cd44SJoe Perches my ($bin) = @_; 1134d311cd44SJoe Perches 1135d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 1136d311cd44SJoe Perches if (-e "$path/$bin") { 1137d311cd44SJoe Perches return "$path/$bin"; 1138d311cd44SJoe Perches } 1139d311cd44SJoe Perches } 1140d311cd44SJoe Perches 1141d311cd44SJoe Perches return ""; 1142d311cd44SJoe Perches} 1143d311cd44SJoe Perches 1144000d1cc1SJoe Perchessub which_conf { 1145000d1cc1SJoe Perches my ($conf) = @_; 1146000d1cc1SJoe Perches 1147000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1148000d1cc1SJoe Perches if (-e "$path/$conf") { 1149000d1cc1SJoe Perches return "$path/$conf"; 1150000d1cc1SJoe Perches } 1151000d1cc1SJoe Perches } 1152000d1cc1SJoe Perches 1153000d1cc1SJoe Perches return ""; 1154000d1cc1SJoe Perches} 1155000d1cc1SJoe Perches 11560a920b5bSAndy Whitcroftsub expand_tabs { 11570a920b5bSAndy Whitcroft my ($str) = @_; 11580a920b5bSAndy Whitcroft 11590a920b5bSAndy Whitcroft my $res = ''; 11600a920b5bSAndy Whitcroft my $n = 0; 11610a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 11620a920b5bSAndy Whitcroft if ($c eq "\t") { 11630a920b5bSAndy Whitcroft $res .= ' '; 11640a920b5bSAndy Whitcroft $n++; 11650a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 11660a920b5bSAndy Whitcroft $res .= ' '; 11670a920b5bSAndy Whitcroft } 11680a920b5bSAndy Whitcroft next; 11690a920b5bSAndy Whitcroft } 11700a920b5bSAndy Whitcroft $res .= $c; 11710a920b5bSAndy Whitcroft $n++; 11720a920b5bSAndy Whitcroft } 11730a920b5bSAndy Whitcroft 11740a920b5bSAndy Whitcroft return $res; 11750a920b5bSAndy Whitcroft} 11766c72ffaaSAndy Whitcroftsub copy_spacing { 1177773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 11786c72ffaaSAndy Whitcroft return $res; 11796c72ffaaSAndy Whitcroft} 11800a920b5bSAndy Whitcroft 11814a0df2efSAndy Whitcroftsub line_stats { 11824a0df2efSAndy Whitcroft my ($line) = @_; 11834a0df2efSAndy Whitcroft 11844a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 11854a0df2efSAndy Whitcroft $line =~ s/^.//; 11864a0df2efSAndy Whitcroft $line = expand_tabs($line); 11874a0df2efSAndy Whitcroft 11884a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 11894a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 11904a0df2efSAndy Whitcroft 11914a0df2efSAndy Whitcroft return (length($line), length($white)); 11924a0df2efSAndy Whitcroft} 11934a0df2efSAndy Whitcroft 1194773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1195773647a0SAndy Whitcroft 1196773647a0SAndy Whitcroftsub sanitise_line_reset { 1197773647a0SAndy Whitcroft my ($in_comment) = @_; 1198773647a0SAndy Whitcroft 1199773647a0SAndy Whitcroft if ($in_comment) { 1200773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1201773647a0SAndy Whitcroft } else { 1202773647a0SAndy Whitcroft $sanitise_quote = ''; 1203773647a0SAndy Whitcroft } 1204773647a0SAndy Whitcroft} 120500df344fSAndy Whitcroftsub sanitise_line { 120600df344fSAndy Whitcroft my ($line) = @_; 120700df344fSAndy Whitcroft 120800df344fSAndy Whitcroft my $res = ''; 120900df344fSAndy Whitcroft my $l = ''; 121000df344fSAndy Whitcroft 1211c2fdda0dSAndy Whitcroft my $qlen = 0; 1212773647a0SAndy Whitcroft my $off = 0; 1213773647a0SAndy Whitcroft my $c; 121400df344fSAndy Whitcroft 1215773647a0SAndy Whitcroft # Always copy over the diff marker. 1216773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1217773647a0SAndy Whitcroft 1218773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1219773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1220773647a0SAndy Whitcroft 12218d2e11b2SClaudio Fontana # Comments we are whacking completely including the begin 1222773647a0SAndy Whitcroft # and end, all to $;. 1223773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1224773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1225773647a0SAndy Whitcroft 1226773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1227773647a0SAndy Whitcroft $off++; 122800df344fSAndy Whitcroft next; 1229773647a0SAndy Whitcroft } 123081bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1231773647a0SAndy Whitcroft $sanitise_quote = ''; 1232773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1233773647a0SAndy Whitcroft $off++; 1234773647a0SAndy Whitcroft next; 1235773647a0SAndy Whitcroft } 1236113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1237113f04a8SDaniel Walker $sanitise_quote = '//'; 1238113f04a8SDaniel Walker 1239113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1240113f04a8SDaniel Walker $off++; 1241113f04a8SDaniel Walker next; 1242113f04a8SDaniel Walker } 1243773647a0SAndy Whitcroft 1244773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1245773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1246773647a0SAndy Whitcroft $c eq "\\") { 1247773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1248773647a0SAndy Whitcroft $off++; 1249773647a0SAndy Whitcroft next; 1250773647a0SAndy Whitcroft } 1251773647a0SAndy Whitcroft # Regular quotes. 1252773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1253773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1254773647a0SAndy Whitcroft $sanitise_quote = $c; 1255773647a0SAndy Whitcroft 1256773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1257773647a0SAndy Whitcroft next; 1258773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1259773647a0SAndy Whitcroft $sanitise_quote = ''; 126000df344fSAndy Whitcroft } 126100df344fSAndy Whitcroft } 1262773647a0SAndy Whitcroft 1263fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1264773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1265773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1266113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1267113f04a8SDaniel Walker substr($res, $off, 1, $;); 1268773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1269773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 127000df344fSAndy Whitcroft } else { 1271773647a0SAndy Whitcroft substr($res, $off, 1, $c); 127200df344fSAndy Whitcroft } 1273c2fdda0dSAndy Whitcroft } 1274c2fdda0dSAndy Whitcroft 1275113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1276113f04a8SDaniel Walker $sanitise_quote = ''; 1277113f04a8SDaniel Walker } 1278113f04a8SDaniel Walker 1279c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1280c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1281c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1282c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1283c2fdda0dSAndy Whitcroft 1284c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1285c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1286c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1287c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1288c2fdda0dSAndy Whitcroft } 1289c2fdda0dSAndy Whitcroft 1290dadf680dSJoe Perches if ($allow_c99_comments && $res =~ m@(//.*$)@) { 1291dadf680dSJoe Perches my $match = $1; 1292dadf680dSJoe Perches $res =~ s/\Q$match\E/"$;" x length($match)/e; 1293dadf680dSJoe Perches } 1294dadf680dSJoe Perches 129500df344fSAndy Whitcroft return $res; 129600df344fSAndy Whitcroft} 129700df344fSAndy Whitcroft 1298a6962d72SJoe Perchessub get_quoted_string { 1299a6962d72SJoe Perches my ($line, $rawline) = @_; 1300a6962d72SJoe Perches 1301478b1799SJoe Perches return "" if (!defined($line) || !defined($rawline)); 130233acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1303a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1304a6962d72SJoe Perches} 1305a6962d72SJoe Perches 13068905a67cSAndy Whitcroftsub ctx_statement_block { 13078905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 13088905a67cSAndy Whitcroft my $line = $linenr - 1; 13098905a67cSAndy Whitcroft my $blk = ''; 13108905a67cSAndy Whitcroft my $soff = $off; 13118905a67cSAndy Whitcroft my $coff = $off - 1; 1312773647a0SAndy Whitcroft my $coff_set = 0; 13138905a67cSAndy Whitcroft 131413214adfSAndy Whitcroft my $loff = 0; 131513214adfSAndy Whitcroft 13168905a67cSAndy Whitcroft my $type = ''; 13178905a67cSAndy Whitcroft my $level = 0; 1318a2750645SAndy Whitcroft my @stack = (); 1319cf655043SAndy Whitcroft my $p; 13208905a67cSAndy Whitcroft my $c; 13218905a67cSAndy Whitcroft my $len = 0; 132213214adfSAndy Whitcroft 132313214adfSAndy Whitcroft my $remainder; 13248905a67cSAndy Whitcroft while (1) { 1325a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1326a2750645SAndy Whitcroft 1327773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 13288905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 13298905a67cSAndy Whitcroft # context. 13308905a67cSAndy Whitcroft if ($off >= $len) { 13318905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1332dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1333c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 13348905a67cSAndy Whitcroft $remain--; 133513214adfSAndy Whitcroft $loff = $len; 1336c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 13378905a67cSAndy Whitcroft $len = length($blk); 13388905a67cSAndy Whitcroft $line++; 13398905a67cSAndy Whitcroft last; 13408905a67cSAndy Whitcroft } 13418905a67cSAndy Whitcroft # Bail if there is no further context. 13428905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 134313214adfSAndy Whitcroft if ($off >= $len) { 13448905a67cSAndy Whitcroft last; 13458905a67cSAndy Whitcroft } 1346f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1347f74bd194SAndy Whitcroft $level++; 1348f74bd194SAndy Whitcroft $type = '#'; 1349f74bd194SAndy Whitcroft } 13508905a67cSAndy Whitcroft } 1351cf655043SAndy Whitcroft $p = $c; 13528905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 135313214adfSAndy Whitcroft $remainder = substr($blk, $off); 13548905a67cSAndy Whitcroft 1355773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 13564635f4fbSAndy Whitcroft 13574635f4fbSAndy Whitcroft # Handle nested #if/#else. 13584635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 13594635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 13604635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 13614635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 13624635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 13634635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 13644635f4fbSAndy Whitcroft } 13654635f4fbSAndy Whitcroft 13668905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 13678905a67cSAndy Whitcroft # outermost level. 13688905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 13698905a67cSAndy Whitcroft last; 13708905a67cSAndy Whitcroft } 13718905a67cSAndy Whitcroft 137213214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1373773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1374773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1375773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1376773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1377773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1378773647a0SAndy Whitcroft $coff_set = 1; 1379773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1380773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 138113214adfSAndy Whitcroft } 138213214adfSAndy Whitcroft 13838905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 13848905a67cSAndy Whitcroft $level++; 13858905a67cSAndy Whitcroft $type = '('; 13868905a67cSAndy Whitcroft } 13878905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 13888905a67cSAndy Whitcroft $level--; 13898905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 13908905a67cSAndy Whitcroft 13918905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 13928905a67cSAndy Whitcroft $coff = $off; 1393773647a0SAndy Whitcroft $coff_set = 1; 1394773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 13958905a67cSAndy Whitcroft } 13968905a67cSAndy Whitcroft } 13978905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 13988905a67cSAndy Whitcroft $level++; 13998905a67cSAndy Whitcroft $type = '{'; 14008905a67cSAndy Whitcroft } 14018905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 14028905a67cSAndy Whitcroft $level--; 14038905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 14048905a67cSAndy Whitcroft 14058905a67cSAndy Whitcroft if ($level == 0) { 1406b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1407b998e001SPatrick Pannuto $off++; 1408b998e001SPatrick Pannuto } 14098905a67cSAndy Whitcroft last; 14108905a67cSAndy Whitcroft } 14118905a67cSAndy Whitcroft } 1412f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1413f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1414f74bd194SAndy Whitcroft $level--; 1415f74bd194SAndy Whitcroft $type = ''; 1416f74bd194SAndy Whitcroft $off++; 1417f74bd194SAndy Whitcroft last; 1418f74bd194SAndy Whitcroft } 14198905a67cSAndy Whitcroft $off++; 14208905a67cSAndy Whitcroft } 1421a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 142213214adfSAndy Whitcroft if ($off == $len) { 1423a3bb97a7SAndy Whitcroft $loff = $len + 1; 142413214adfSAndy Whitcroft $line++; 142513214adfSAndy Whitcroft $remain--; 142613214adfSAndy Whitcroft } 14278905a67cSAndy Whitcroft 14288905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 14298905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 14308905a67cSAndy Whitcroft 14318905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 14328905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 14338905a67cSAndy Whitcroft 1434773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 143513214adfSAndy Whitcroft 143613214adfSAndy Whitcroft return ($statement, $condition, 143713214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 143813214adfSAndy Whitcroft} 143913214adfSAndy Whitcroft 1440cf655043SAndy Whitcroftsub statement_lines { 1441cf655043SAndy Whitcroft my ($stmt) = @_; 1442cf655043SAndy Whitcroft 1443cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1444cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1445cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1446cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1447cf655043SAndy Whitcroft 1448cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1449cf655043SAndy Whitcroft 1450cf655043SAndy Whitcroft return $#stmt_lines + 2; 1451cf655043SAndy Whitcroft} 1452cf655043SAndy Whitcroft 1453cf655043SAndy Whitcroftsub statement_rawlines { 1454cf655043SAndy Whitcroft my ($stmt) = @_; 1455cf655043SAndy Whitcroft 1456cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1457cf655043SAndy Whitcroft 1458cf655043SAndy Whitcroft return $#stmt_lines + 2; 1459cf655043SAndy Whitcroft} 1460cf655043SAndy Whitcroft 1461cf655043SAndy Whitcroftsub statement_block_size { 1462cf655043SAndy Whitcroft my ($stmt) = @_; 1463cf655043SAndy Whitcroft 1464cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1465cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1466cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1467cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1468cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1469cf655043SAndy Whitcroft 1470cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1471cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1472cf655043SAndy Whitcroft 1473cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1474cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1475cf655043SAndy Whitcroft 1476cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1477cf655043SAndy Whitcroft return $stmt_lines; 1478cf655043SAndy Whitcroft } else { 1479cf655043SAndy Whitcroft return $stmt_statements; 1480cf655043SAndy Whitcroft } 1481cf655043SAndy Whitcroft} 1482cf655043SAndy Whitcroft 148313214adfSAndy Whitcroftsub ctx_statement_full { 148413214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 148513214adfSAndy Whitcroft my ($statement, $condition, $level); 148613214adfSAndy Whitcroft 148713214adfSAndy Whitcroft my (@chunks); 148813214adfSAndy Whitcroft 1489cf655043SAndy Whitcroft # Grab the first conditional/block pair. 149013214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 149113214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1492773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 149313214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1494cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1495cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1496cf655043SAndy Whitcroft } 1497cf655043SAndy Whitcroft 1498cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1499cf655043SAndy Whitcroft # could continue the statement. 1500cf655043SAndy Whitcroft for (;;) { 150113214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 150213214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1503cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1504773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1505cf655043SAndy Whitcroft #print "C: push\n"; 1506cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 150713214adfSAndy Whitcroft } 150813214adfSAndy Whitcroft 150913214adfSAndy Whitcroft return ($level, $linenr, @chunks); 15108905a67cSAndy Whitcroft} 15118905a67cSAndy Whitcroft 15124a0df2efSAndy Whitcroftsub ctx_block_get { 1513f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 15144a0df2efSAndy Whitcroft my $line; 15154a0df2efSAndy Whitcroft my $start = $linenr - 1; 15164a0df2efSAndy Whitcroft my $blk = ''; 15174a0df2efSAndy Whitcroft my @o; 15184a0df2efSAndy Whitcroft my @c; 15194a0df2efSAndy Whitcroft my @res = (); 15204a0df2efSAndy Whitcroft 1521f0a594c1SAndy Whitcroft my $level = 0; 15224635f4fbSAndy Whitcroft my @stack = ($level); 152300df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 152400df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 152500df344fSAndy Whitcroft $remain--; 152600df344fSAndy Whitcroft 152700df344fSAndy Whitcroft $blk .= $rawlines[$line]; 15284635f4fbSAndy Whitcroft 15294635f4fbSAndy Whitcroft # Handle nested #if/#else. 153001464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 15314635f4fbSAndy Whitcroft push(@stack, $level); 153201464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 15334635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 153401464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 15354635f4fbSAndy Whitcroft $level = pop(@stack); 15364635f4fbSAndy Whitcroft } 15374635f4fbSAndy Whitcroft 153801464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1539f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1540f0a594c1SAndy Whitcroft if ($off > 0) { 1541f0a594c1SAndy Whitcroft $off--; 1542f0a594c1SAndy Whitcroft next; 1543f0a594c1SAndy Whitcroft } 15444a0df2efSAndy Whitcroft 1545f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1546f0a594c1SAndy Whitcroft $level--; 1547f0a594c1SAndy Whitcroft last if ($level == 0); 1548f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1549f0a594c1SAndy Whitcroft $level++; 1550f0a594c1SAndy Whitcroft } 1551f0a594c1SAndy Whitcroft } 15524a0df2efSAndy Whitcroft 1553f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 155400df344fSAndy Whitcroft push(@res, $rawlines[$line]); 15554a0df2efSAndy Whitcroft } 15564a0df2efSAndy Whitcroft 1557f0a594c1SAndy Whitcroft last if ($level == 0); 15584a0df2efSAndy Whitcroft } 15594a0df2efSAndy Whitcroft 1560f0a594c1SAndy Whitcroft return ($level, @res); 15614a0df2efSAndy Whitcroft} 15624a0df2efSAndy Whitcroftsub ctx_block_outer { 15634a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 15644a0df2efSAndy Whitcroft 1565f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1566f0a594c1SAndy Whitcroft return @r; 15674a0df2efSAndy Whitcroft} 15684a0df2efSAndy Whitcroftsub ctx_block { 15694a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 15704a0df2efSAndy Whitcroft 1571f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1572f0a594c1SAndy Whitcroft return @r; 1573653d4876SAndy Whitcroft} 1574653d4876SAndy Whitcroftsub ctx_statement { 1575f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1576f0a594c1SAndy Whitcroft 1577f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1578f0a594c1SAndy Whitcroft return @r; 1579f0a594c1SAndy Whitcroft} 1580f0a594c1SAndy Whitcroftsub ctx_block_level { 1581653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1582653d4876SAndy Whitcroft 1583f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 15844a0df2efSAndy Whitcroft} 15859c0ca6f9SAndy Whitcroftsub ctx_statement_level { 15869c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 15879c0ca6f9SAndy Whitcroft 15889c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 15899c0ca6f9SAndy Whitcroft} 15904a0df2efSAndy Whitcroft 15914a0df2efSAndy Whitcroftsub ctx_locate_comment { 15924a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 15934a0df2efSAndy Whitcroft 15944a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1595beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 15964a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 15974a0df2efSAndy Whitcroft 15984a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 15994a0df2efSAndy Whitcroft # comment. 16004a0df2efSAndy Whitcroft my $in_comment = 0; 16014a0df2efSAndy Whitcroft $current_comment = ''; 16024a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 160300df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 160400df344fSAndy Whitcroft #warn " $line\n"; 16054a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 16064a0df2efSAndy Whitcroft $in_comment = 1; 16074a0df2efSAndy Whitcroft } 16084a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 16094a0df2efSAndy Whitcroft $in_comment = 1; 16104a0df2efSAndy Whitcroft } 16114a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 16124a0df2efSAndy Whitcroft $current_comment = ''; 16134a0df2efSAndy Whitcroft } 16144a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 16154a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 16164a0df2efSAndy Whitcroft $in_comment = 0; 16174a0df2efSAndy Whitcroft } 16184a0df2efSAndy Whitcroft } 16194a0df2efSAndy Whitcroft 16204a0df2efSAndy Whitcroft chomp($current_comment); 16214a0df2efSAndy Whitcroft return($current_comment); 16224a0df2efSAndy Whitcroft} 16234a0df2efSAndy Whitcroftsub ctx_has_comment { 16244a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 16254a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 16264a0df2efSAndy Whitcroft 162700df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 16284a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 16294a0df2efSAndy Whitcroft 16304a0df2efSAndy Whitcroft return ($cmt ne ''); 16314a0df2efSAndy Whitcroft} 16324a0df2efSAndy Whitcroft 16334d001e4dSAndy Whitcroftsub raw_line { 16344d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 16354d001e4dSAndy Whitcroft 16364d001e4dSAndy Whitcroft my $offset = $linenr - 1; 16374d001e4dSAndy Whitcroft $cnt++; 16384d001e4dSAndy Whitcroft 16394d001e4dSAndy Whitcroft my $line; 16404d001e4dSAndy Whitcroft while ($cnt) { 16414d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 16424d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 16434d001e4dSAndy Whitcroft $cnt--; 16444d001e4dSAndy Whitcroft } 16454d001e4dSAndy Whitcroft 16464d001e4dSAndy Whitcroft return $line; 16474d001e4dSAndy Whitcroft} 16484d001e4dSAndy Whitcroft 16492a9f9d85STobin C. Hardingsub get_stat_real { 16502a9f9d85STobin C. Harding my ($linenr, $lc) = @_; 16512a9f9d85STobin C. Harding 16522a9f9d85STobin C. Harding my $stat_real = raw_line($linenr, 0); 16532a9f9d85STobin C. Harding for (my $count = $linenr + 1; $count <= $lc; $count++) { 16542a9f9d85STobin C. Harding $stat_real = $stat_real . "\n" . raw_line($count, 0); 16552a9f9d85STobin C. Harding } 16562a9f9d85STobin C. Harding 16572a9f9d85STobin C. Harding return $stat_real; 16582a9f9d85STobin C. Harding} 16592a9f9d85STobin C. Harding 1660e3d95a2aSTobin C. Hardingsub get_stat_here { 1661e3d95a2aSTobin C. Harding my ($linenr, $cnt, $here) = @_; 1662e3d95a2aSTobin C. Harding 1663e3d95a2aSTobin C. Harding my $herectx = $here . "\n"; 1664e3d95a2aSTobin C. Harding for (my $n = 0; $n < $cnt; $n++) { 1665e3d95a2aSTobin C. Harding $herectx .= raw_line($linenr, $n) . "\n"; 1666e3d95a2aSTobin C. Harding } 1667e3d95a2aSTobin C. Harding 1668e3d95a2aSTobin C. Harding return $herectx; 1669e3d95a2aSTobin C. Harding} 1670e3d95a2aSTobin C. Harding 16710a920b5bSAndy Whitcroftsub cat_vet { 16720a920b5bSAndy Whitcroft my ($vet) = @_; 16739c0ca6f9SAndy Whitcroft my ($res, $coded); 16740a920b5bSAndy Whitcroft 16759c0ca6f9SAndy Whitcroft $res = ''; 16766c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 16776c72ffaaSAndy Whitcroft $res .= $1; 16786c72ffaaSAndy Whitcroft if ($2 ne '') { 16799c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 16806c72ffaaSAndy Whitcroft $res .= $coded; 16816c72ffaaSAndy Whitcroft } 16829c0ca6f9SAndy Whitcroft } 16839c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 16840a920b5bSAndy Whitcroft 16859c0ca6f9SAndy Whitcroft return $res; 16860a920b5bSAndy Whitcroft} 16870a920b5bSAndy Whitcroft 1688c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1689cf655043SAndy Whitcroftmy $av_pending; 1690c2fdda0dSAndy Whitcroftmy @av_paren_type; 16911f65f947SAndy Whitcroftmy $av_pend_colon; 1692c2fdda0dSAndy Whitcroft 1693c2fdda0dSAndy Whitcroftsub annotate_reset { 1694c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1695cf655043SAndy Whitcroft $av_pending = '_'; 1696cf655043SAndy Whitcroft @av_paren_type = ('E'); 16971f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1698c2fdda0dSAndy Whitcroft} 1699c2fdda0dSAndy Whitcroft 17006c72ffaaSAndy Whitcroftsub annotate_values { 17016c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 17026c72ffaaSAndy Whitcroft 17036c72ffaaSAndy Whitcroft my $res; 17041f65f947SAndy Whitcroft my $var = '_' x length($stream); 17056c72ffaaSAndy Whitcroft my $cur = $stream; 17066c72ffaaSAndy Whitcroft 1707c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 17086c72ffaaSAndy Whitcroft 17096c72ffaaSAndy Whitcroft while (length($cur)) { 1710773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1711cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1712171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 17136c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1714c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1715c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1716cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1717c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 17186c72ffaaSAndy Whitcroft } 17196c72ffaaSAndy Whitcroft 1720c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 17219446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 17229446ef56SAndy Whitcroft push(@av_paren_type, $type); 1723addcdceaSAndy Whitcroft $type = 'c'; 17249446ef56SAndy Whitcroft 1725e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1726c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 17276c72ffaaSAndy Whitcroft $type = 'T'; 17286c72ffaaSAndy Whitcroft 1729389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1730389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1731389a2fe5SAndy Whitcroft $type = 'T'; 1732389a2fe5SAndy Whitcroft 1733c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1734171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1735c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1736171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1737171ae1a4SAndy Whitcroft if ($2 ne '') { 1738cf655043SAndy Whitcroft $av_pending = 'N'; 1739171ae1a4SAndy Whitcroft } 1740171ae1a4SAndy Whitcroft $type = 'E'; 1741171ae1a4SAndy Whitcroft 1742c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1743171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1744171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1745171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 17466c72ffaaSAndy Whitcroft 1747c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1748cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1749c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1750cf655043SAndy Whitcroft 1751cf655043SAndy Whitcroft push(@av_paren_type, $type); 1752cf655043SAndy Whitcroft push(@av_paren_type, $type); 1753171ae1a4SAndy Whitcroft $type = 'E'; 1754cf655043SAndy Whitcroft 1755c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1756cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1757cf655043SAndy Whitcroft $av_preprocessor = 1; 1758cf655043SAndy Whitcroft 1759cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1760cf655043SAndy Whitcroft 1761171ae1a4SAndy Whitcroft $type = 'E'; 1762cf655043SAndy Whitcroft 1763c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1764cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1765cf655043SAndy Whitcroft 1766cf655043SAndy Whitcroft $av_preprocessor = 1; 1767cf655043SAndy Whitcroft 1768cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1769cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1770cf655043SAndy Whitcroft pop(@av_paren_type); 1771cf655043SAndy Whitcroft push(@av_paren_type, $type); 1772171ae1a4SAndy Whitcroft $type = 'E'; 17736c72ffaaSAndy Whitcroft 17746c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1775c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 17766c72ffaaSAndy Whitcroft 1777171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1778171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1779171ae1a4SAndy Whitcroft $av_pending = $type; 1780171ae1a4SAndy Whitcroft $type = 'N'; 1781171ae1a4SAndy Whitcroft 17826c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1783c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 17846c72ffaaSAndy Whitcroft if (defined $2) { 1785cf655043SAndy Whitcroft $av_pending = 'V'; 17866c72ffaaSAndy Whitcroft } 17876c72ffaaSAndy Whitcroft $type = 'N'; 17886c72ffaaSAndy Whitcroft 178914b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1790c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 179114b111c1SAndy Whitcroft $av_pending = 'E'; 17926c72ffaaSAndy Whitcroft $type = 'N'; 17936c72ffaaSAndy Whitcroft 17941f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 17951f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 17961f65f947SAndy Whitcroft $av_pend_colon = 'C'; 17971f65f947SAndy Whitcroft $type = 'N'; 17981f65f947SAndy Whitcroft 179914b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1800c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 18016c72ffaaSAndy Whitcroft $type = 'N'; 18026c72ffaaSAndy Whitcroft 18036c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1804c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1805cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1806cf655043SAndy Whitcroft $av_pending = '_'; 18076c72ffaaSAndy Whitcroft $type = 'N'; 18086c72ffaaSAndy Whitcroft 18096c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1810cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1811cf655043SAndy Whitcroft if ($new_type ne '_') { 1812cf655043SAndy Whitcroft $type = $new_type; 1813c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1814c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 18156c72ffaaSAndy Whitcroft } else { 1816c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 18176c72ffaaSAndy Whitcroft } 18186c72ffaaSAndy Whitcroft 1819c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1820c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1821c8cb2ca3SAndy Whitcroft $type = 'V'; 1822cf655043SAndy Whitcroft $av_pending = 'V'; 18236c72ffaaSAndy Whitcroft 18248e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 18258e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 18261f65f947SAndy Whitcroft $av_pend_colon = 'B'; 18278e761b04SAndy Whitcroft } elsif ($type eq 'E') { 18288e761b04SAndy Whitcroft $av_pend_colon = 'L'; 18291f65f947SAndy Whitcroft } 18301f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 18311f65f947SAndy Whitcroft $type = 'V'; 18321f65f947SAndy Whitcroft 18336c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1834c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 18356c72ffaaSAndy Whitcroft $type = 'V'; 18366c72ffaaSAndy Whitcroft 18376c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1838c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 18396c72ffaaSAndy Whitcroft $type = 'N'; 18406c72ffaaSAndy Whitcroft 1841cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1842c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 184313214adfSAndy Whitcroft $type = 'E'; 18441f65f947SAndy Whitcroft $av_pend_colon = 'O'; 184513214adfSAndy Whitcroft 18468e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 18478e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 18488e761b04SAndy Whitcroft $type = 'C'; 18498e761b04SAndy Whitcroft 18501f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 18511f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 18521f65f947SAndy Whitcroft $type = 'N'; 18531f65f947SAndy Whitcroft 18541f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 18551f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 18561f65f947SAndy Whitcroft 18571f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 18581f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 18591f65f947SAndy Whitcroft $type = 'E'; 18601f65f947SAndy Whitcroft } else { 18611f65f947SAndy Whitcroft $type = 'N'; 18621f65f947SAndy Whitcroft } 18631f65f947SAndy Whitcroft $av_pend_colon = 'O'; 18641f65f947SAndy Whitcroft 18658e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 186613214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 18676c72ffaaSAndy Whitcroft $type = 'N'; 18686c72ffaaSAndy Whitcroft 18690d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 187074048ed8SAndy Whitcroft my $variant; 187174048ed8SAndy Whitcroft 187274048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 187374048ed8SAndy Whitcroft if ($type eq 'V') { 187474048ed8SAndy Whitcroft $variant = 'B'; 187574048ed8SAndy Whitcroft } else { 187674048ed8SAndy Whitcroft $variant = 'U'; 187774048ed8SAndy Whitcroft } 187874048ed8SAndy Whitcroft 187974048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 188074048ed8SAndy Whitcroft $type = 'N'; 188174048ed8SAndy Whitcroft 18826c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1883c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 18846c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 18856c72ffaaSAndy Whitcroft $type = 'N'; 18866c72ffaaSAndy Whitcroft } 18876c72ffaaSAndy Whitcroft 18886c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1889c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 18906c72ffaaSAndy Whitcroft } 18916c72ffaaSAndy Whitcroft if (defined $1) { 18926c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 18936c72ffaaSAndy Whitcroft $res .= $type x length($1); 18946c72ffaaSAndy Whitcroft } 18956c72ffaaSAndy Whitcroft } 18966c72ffaaSAndy Whitcroft 18971f65f947SAndy Whitcroft return ($res, $var); 18986c72ffaaSAndy Whitcroft} 18996c72ffaaSAndy Whitcroft 19008905a67cSAndy Whitcroftsub possible { 190113214adfSAndy Whitcroft my ($possible, $line) = @_; 19029a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 19030776e594SAndy Whitcroft ^(?: 19040776e594SAndy Whitcroft $Modifier| 19050776e594SAndy Whitcroft $Storage| 19060776e594SAndy Whitcroft $Type| 19079a974fdbSAndy Whitcroft DEFINE_\S+ 19089a974fdbSAndy Whitcroft )$| 19099a974fdbSAndy Whitcroft ^(?: 19100776e594SAndy Whitcroft goto| 19110776e594SAndy Whitcroft return| 19120776e594SAndy Whitcroft case| 19130776e594SAndy Whitcroft else| 19140776e594SAndy Whitcroft asm|__asm__| 191589a88353SAndy Whitcroft do| 191689a88353SAndy Whitcroft \#| 191789a88353SAndy Whitcroft \#\#| 19189a974fdbSAndy Whitcroft )(?:\s|$)| 19190776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 19209a974fdbSAndy Whitcroft )}x; 19219a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 19229a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1923c45dcabdSAndy Whitcroft # Check for modifiers. 1924c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1925c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1926c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1927c45dcabdSAndy Whitcroft 1928c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1929c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1930d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 19319a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1932d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1933485ff23eSAlex Dowad push(@modifierListFile, $modifier); 1934d2506586SAndy Whitcroft } 19359a974fdbSAndy Whitcroft } 1936c45dcabdSAndy Whitcroft 1937c45dcabdSAndy Whitcroft } else { 193813214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1939485ff23eSAlex Dowad push(@typeListFile, $possible); 1940c45dcabdSAndy Whitcroft } 19418905a67cSAndy Whitcroft build_types(); 19420776e594SAndy Whitcroft } else { 19430776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 19448905a67cSAndy Whitcroft } 19458905a67cSAndy Whitcroft} 19468905a67cSAndy Whitcroft 19476c72ffaaSAndy Whitcroftmy $prefix = ''; 19486c72ffaaSAndy Whitcroft 1949000d1cc1SJoe Perchessub show_type { 1950cbec18afSJoe Perches my ($type) = @_; 195191bfe484SJoe Perches 1952522b837cSAlexey Dobriyan $type =~ tr/[a-z]/[A-Z]/; 1953522b837cSAlexey Dobriyan 1954cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1955cbec18afSJoe Perches 1956cbec18afSJoe Perches return !defined $ignore_type{$type}; 1957000d1cc1SJoe Perches} 1958000d1cc1SJoe Perches 1959f0a594c1SAndy Whitcroftsub report { 1960cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1961cbec18afSJoe Perches 1962cbec18afSJoe Perches if (!show_type($type) || 1963cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1964773647a0SAndy Whitcroft return 0; 1965773647a0SAndy Whitcroft } 196657230297SJoe Perches my $output = ''; 1967737c0767SJohn Brooks if ($color) { 196857230297SJoe Perches if ($level eq 'ERROR') { 196957230297SJoe Perches $output .= RED; 197057230297SJoe Perches } elsif ($level eq 'WARNING') { 197157230297SJoe Perches $output .= YELLOW; 1972000d1cc1SJoe Perches } else { 197357230297SJoe Perches $output .= GREEN; 1974000d1cc1SJoe Perches } 197557230297SJoe Perches } 197657230297SJoe Perches $output .= $prefix . $level . ':'; 197757230297SJoe Perches if ($show_types) { 1978737c0767SJohn Brooks $output .= BLUE if ($color); 197957230297SJoe Perches $output .= "$type:"; 198057230297SJoe Perches } 1981737c0767SJohn Brooks $output .= RESET if ($color); 198257230297SJoe Perches $output .= ' ' . $msg . "\n"; 198334d8815fSJoe Perches 198434d8815fSJoe Perches if ($showfile) { 198534d8815fSJoe Perches my @lines = split("\n", $output, -1); 198634d8815fSJoe Perches splice(@lines, 1, 1); 198734d8815fSJoe Perches $output = join("\n", @lines); 198834d8815fSJoe Perches } 198957230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 19908905a67cSAndy Whitcroft 199157230297SJoe Perches push(our @report, $output); 1992773647a0SAndy Whitcroft 1993773647a0SAndy Whitcroft return 1; 1994f0a594c1SAndy Whitcroft} 1995cbec18afSJoe Perches 1996f0a594c1SAndy Whitcroftsub report_dump { 199713214adfSAndy Whitcroft our @report; 1998f0a594c1SAndy Whitcroft} 1999000d1cc1SJoe Perches 2000d752fcc8SJoe Perchessub fixup_current_range { 2001d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 2002d752fcc8SJoe Perches 2003d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 2004d752fcc8SJoe Perches my $o = $1; 2005d752fcc8SJoe Perches my $l = $2; 2006d752fcc8SJoe Perches my $no = $o + $offset; 2007d752fcc8SJoe Perches my $nl = $l + $length; 2008d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 2009d752fcc8SJoe Perches } 2010d752fcc8SJoe Perches} 2011d752fcc8SJoe Perches 2012d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 2013d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 2014d752fcc8SJoe Perches 2015d752fcc8SJoe Perches my $range_last_linenr = 0; 2016d752fcc8SJoe Perches my $delta_offset = 0; 2017d752fcc8SJoe Perches 2018d752fcc8SJoe Perches my $old_linenr = 0; 2019d752fcc8SJoe Perches my $new_linenr = 0; 2020d752fcc8SJoe Perches 2021d752fcc8SJoe Perches my $next_insert = 0; 2022d752fcc8SJoe Perches my $next_delete = 0; 2023d752fcc8SJoe Perches 2024d752fcc8SJoe Perches my @lines = (); 2025d752fcc8SJoe Perches 2026d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 2027d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 2028d752fcc8SJoe Perches 2029d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 2030d752fcc8SJoe Perches my $save_line = 1; 2031d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 2032323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 2033d752fcc8SJoe Perches $delta_offset = 0; 2034d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 2035d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 2036d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 2037d752fcc8SJoe Perches } 2038d752fcc8SJoe Perches 2039d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 2040d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 2041d752fcc8SJoe Perches $save_line = 0; 2042d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 2043d752fcc8SJoe Perches } 2044d752fcc8SJoe Perches 2045d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 2046d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 2047d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 2048d752fcc8SJoe Perches $new_linenr++; 2049d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 2050d752fcc8SJoe Perches } 2051d752fcc8SJoe Perches 2052d752fcc8SJoe Perches if ($save_line) { 2053d752fcc8SJoe Perches push(@lines, $line); 2054d752fcc8SJoe Perches $new_linenr++; 2055d752fcc8SJoe Perches } 2056d752fcc8SJoe Perches 2057d752fcc8SJoe Perches $old_linenr++; 2058d752fcc8SJoe Perches } 2059d752fcc8SJoe Perches 2060d752fcc8SJoe Perches return @lines; 2061d752fcc8SJoe Perches} 2062d752fcc8SJoe Perches 2063f2d7e4d4SJoe Perchessub fix_insert_line { 2064f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2065f2d7e4d4SJoe Perches 2066f2d7e4d4SJoe Perches my $inserted = { 2067f2d7e4d4SJoe Perches LINENR => $linenr, 2068f2d7e4d4SJoe Perches LINE => $line, 2069f2d7e4d4SJoe Perches }; 2070f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 2071f2d7e4d4SJoe Perches} 2072f2d7e4d4SJoe Perches 2073f2d7e4d4SJoe Perchessub fix_delete_line { 2074f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 2075f2d7e4d4SJoe Perches 2076f2d7e4d4SJoe Perches my $deleted = { 2077f2d7e4d4SJoe Perches LINENR => $linenr, 2078f2d7e4d4SJoe Perches LINE => $line, 2079f2d7e4d4SJoe Perches }; 2080f2d7e4d4SJoe Perches 2081f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 2082f2d7e4d4SJoe Perches} 2083f2d7e4d4SJoe Perches 2084de7d4f0eSAndy Whitcroftsub ERROR { 2085cbec18afSJoe Perches my ($type, $msg) = @_; 2086cbec18afSJoe Perches 2087cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 2088de7d4f0eSAndy Whitcroft our $clean = 0; 20896c72ffaaSAndy Whitcroft our $cnt_error++; 20903705ce5bSJoe Perches return 1; 2091de7d4f0eSAndy Whitcroft } 20923705ce5bSJoe Perches return 0; 2093773647a0SAndy Whitcroft} 2094de7d4f0eSAndy Whitcroftsub WARN { 2095cbec18afSJoe Perches my ($type, $msg) = @_; 2096cbec18afSJoe Perches 2097cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 2098de7d4f0eSAndy Whitcroft our $clean = 0; 20996c72ffaaSAndy Whitcroft our $cnt_warn++; 21003705ce5bSJoe Perches return 1; 2101de7d4f0eSAndy Whitcroft } 21023705ce5bSJoe Perches return 0; 2103773647a0SAndy Whitcroft} 2104de7d4f0eSAndy Whitcroftsub CHK { 2105cbec18afSJoe Perches my ($type, $msg) = @_; 2106cbec18afSJoe Perches 2107cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 2108de7d4f0eSAndy Whitcroft our $clean = 0; 21096c72ffaaSAndy Whitcroft our $cnt_chk++; 21103705ce5bSJoe Perches return 1; 21116c72ffaaSAndy Whitcroft } 21123705ce5bSJoe Perches return 0; 2113de7d4f0eSAndy Whitcroft} 2114de7d4f0eSAndy Whitcroft 21156ecd9674SAndy Whitcroftsub check_absolute_file { 21166ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 21176ecd9674SAndy Whitcroft my $file = $absolute; 21186ecd9674SAndy Whitcroft 21196ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 21206ecd9674SAndy Whitcroft 21216ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 21226ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 21236ecd9674SAndy Whitcroft if (-f "$root/$file") { 21246ecd9674SAndy Whitcroft ##print "file<$file>\n"; 21256ecd9674SAndy Whitcroft last; 21266ecd9674SAndy Whitcroft } 21276ecd9674SAndy Whitcroft } 21286ecd9674SAndy Whitcroft if (! -f _) { 21296ecd9674SAndy Whitcroft return 0; 21306ecd9674SAndy Whitcroft } 21316ecd9674SAndy Whitcroft 21326ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 21336ecd9674SAndy Whitcroft my $prefix = $absolute; 21346ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 21356ecd9674SAndy Whitcroft 21366ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 21376ecd9674SAndy Whitcroft if ($prefix ne ".../") { 2138000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 2139000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 21406ecd9674SAndy Whitcroft } 21416ecd9674SAndy Whitcroft} 21426ecd9674SAndy Whitcroft 21433705ce5bSJoe Perchessub trim { 21443705ce5bSJoe Perches my ($string) = @_; 21453705ce5bSJoe Perches 2146b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 2147b34c648bSJoe Perches 2148b34c648bSJoe Perches return $string; 2149b34c648bSJoe Perches} 2150b34c648bSJoe Perches 2151b34c648bSJoe Perchessub ltrim { 2152b34c648bSJoe Perches my ($string) = @_; 2153b34c648bSJoe Perches 2154b34c648bSJoe Perches $string =~ s/^\s+//; 2155b34c648bSJoe Perches 2156b34c648bSJoe Perches return $string; 2157b34c648bSJoe Perches} 2158b34c648bSJoe Perches 2159b34c648bSJoe Perchessub rtrim { 2160b34c648bSJoe Perches my ($string) = @_; 2161b34c648bSJoe Perches 2162b34c648bSJoe Perches $string =~ s/\s+$//; 21633705ce5bSJoe Perches 21643705ce5bSJoe Perches return $string; 21653705ce5bSJoe Perches} 21663705ce5bSJoe Perches 216752ea8506SJoe Perchessub string_find_replace { 216852ea8506SJoe Perches my ($string, $find, $replace) = @_; 216952ea8506SJoe Perches 217052ea8506SJoe Perches $string =~ s/$find/$replace/g; 217152ea8506SJoe Perches 217252ea8506SJoe Perches return $string; 217352ea8506SJoe Perches} 217452ea8506SJoe Perches 21753705ce5bSJoe Perchessub tabify { 21763705ce5bSJoe Perches my ($leading) = @_; 21773705ce5bSJoe Perches 21783705ce5bSJoe Perches my $source_indent = 8; 21793705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 21803705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 21813705ce5bSJoe Perches 21823705ce5bSJoe Perches #convert leading spaces to tabs 21833705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 21843705ce5bSJoe Perches #Remove spaces before a tab 21853705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 21863705ce5bSJoe Perches 21873705ce5bSJoe Perches return "$leading"; 21883705ce5bSJoe Perches} 21893705ce5bSJoe Perches 2190d1fe9c09SJoe Perchessub pos_last_openparen { 2191d1fe9c09SJoe Perches my ($line) = @_; 2192d1fe9c09SJoe Perches 2193d1fe9c09SJoe Perches my $pos = 0; 2194d1fe9c09SJoe Perches 2195d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2196d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2197d1fe9c09SJoe Perches 2198d1fe9c09SJoe Perches my $last_openparen = 0; 2199d1fe9c09SJoe Perches 2200d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2201d1fe9c09SJoe Perches return -1; 2202d1fe9c09SJoe Perches } 2203d1fe9c09SJoe Perches 2204d1fe9c09SJoe Perches my $len = length($line); 2205d1fe9c09SJoe Perches 2206d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2207d1fe9c09SJoe Perches my $string = substr($line, $pos); 2208d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2209d1fe9c09SJoe Perches $pos += length($1) - 1; 2210d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2211d1fe9c09SJoe Perches $last_openparen = $pos; 2212d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2213d1fe9c09SJoe Perches last; 2214d1fe9c09SJoe Perches } 2215d1fe9c09SJoe Perches } 2216d1fe9c09SJoe Perches 221791cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2218d1fe9c09SJoe Perches} 2219d1fe9c09SJoe Perches 22200a920b5bSAndy Whitcroftsub process { 22210a920b5bSAndy Whitcroft my $filename = shift; 22220a920b5bSAndy Whitcroft 22230a920b5bSAndy Whitcroft my $linenr=0; 22240a920b5bSAndy Whitcroft my $prevline=""; 2225c2fdda0dSAndy Whitcroft my $prevrawline=""; 22260a920b5bSAndy Whitcroft my $stashline=""; 2227c2fdda0dSAndy Whitcroft my $stashrawline=""; 22280a920b5bSAndy Whitcroft 22294a0df2efSAndy Whitcroft my $length; 22300a920b5bSAndy Whitcroft my $indent; 22310a920b5bSAndy Whitcroft my $previndent=0; 22320a920b5bSAndy Whitcroft my $stashindent=0; 22330a920b5bSAndy Whitcroft 2234de7d4f0eSAndy Whitcroft our $clean = 1; 22350a920b5bSAndy Whitcroft my $signoff = 0; 22360a920b5bSAndy Whitcroft my $is_patch = 0; 223729ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 223815662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 2239ed43c4e5SAllen Hubbe my $has_commit_log = 0; #Encountered lines before patch 2240bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 22412a076f40SJoe Perches my $commit_log_long_line = 0; 2242e518e9a5SJoe Perches my $commit_log_has_diff = 0; 224313f1937eSJoe Perches my $reported_maintainer_file = 0; 2244fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2245fa64205dSPasi Savanainen 2246365dd4eaSJoe Perches my $last_blank_line = 0; 22475e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2248365dd4eaSJoe Perches 224913214adfSAndy Whitcroft our @report = (); 22506c72ffaaSAndy Whitcroft our $cnt_lines = 0; 22516c72ffaaSAndy Whitcroft our $cnt_error = 0; 22526c72ffaaSAndy Whitcroft our $cnt_warn = 0; 22536c72ffaaSAndy Whitcroft our $cnt_chk = 0; 22546c72ffaaSAndy Whitcroft 22550a920b5bSAndy Whitcroft # Trace the real file/line as we go. 22560a920b5bSAndy Whitcroft my $realfile = ''; 22570a920b5bSAndy Whitcroft my $realline = 0; 22580a920b5bSAndy Whitcroft my $realcnt = 0; 22590a920b5bSAndy Whitcroft my $here = ''; 226077cb8546SJoe Perches my $context_function; #undef'd unless there's a known function 22610a920b5bSAndy Whitcroft my $in_comment = 0; 2262c2fdda0dSAndy Whitcroft my $comment_edge = 0; 22630a920b5bSAndy Whitcroft my $first_line = 0; 22641e855726SWolfram Sang my $p1_prefix = ''; 22650a920b5bSAndy Whitcroft 226613214adfSAndy Whitcroft my $prev_values = 'E'; 226713214adfSAndy Whitcroft 226813214adfSAndy Whitcroft # suppression flags 2269773647a0SAndy Whitcroft my %suppress_ifbraces; 2270170d3a22SAndy Whitcroft my %suppress_whiletrailers; 22712b474a1aSAndy Whitcroft my %suppress_export; 22723e469cdcSAndy Whitcroft my $suppress_statement = 0; 2273653d4876SAndy Whitcroft 22747e51f197SJoe Perches my %signatures = (); 2275323c1260SJoe Perches 2276c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2277de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2278c2fdda0dSAndy Whitcroft # 2279de7d4f0eSAndy Whitcroft my @setup_docs = (); 2280de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2281773647a0SAndy Whitcroft 2282d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2283d8b07710SJoe Perches 22849f3a8992SRob Herring my $checklicenseline = 1; 22859f3a8992SRob Herring 2286773647a0SAndy Whitcroft sanitise_line_reset(); 2287c2fdda0dSAndy Whitcroft my $line; 2288c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2289773647a0SAndy Whitcroft $linenr++; 2290773647a0SAndy Whitcroft $line = $rawline; 2291c2fdda0dSAndy Whitcroft 22923705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 22933705ce5bSJoe Perches 2294773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2295de7d4f0eSAndy Whitcroft $setup_docs = 0; 22968c27ceffSMauro Carvalho Chehab if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { 2297de7d4f0eSAndy Whitcroft $setup_docs = 1; 2298de7d4f0eSAndy Whitcroft } 2299773647a0SAndy Whitcroft #next; 2300de7d4f0eSAndy Whitcroft } 230174fd4f34SJoe Perches if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2302773647a0SAndy Whitcroft $realline=$1-1; 2303773647a0SAndy Whitcroft if (defined $2) { 2304773647a0SAndy Whitcroft $realcnt=$3+1; 2305773647a0SAndy Whitcroft } else { 2306773647a0SAndy Whitcroft $realcnt=1+1; 2307773647a0SAndy Whitcroft } 2308c45dcabdSAndy Whitcroft $in_comment = 0; 2309773647a0SAndy Whitcroft 2310773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2311773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2312773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2313773647a0SAndy Whitcroft # at context start. 2314773647a0SAndy Whitcroft my $edge; 231501fa9147SAndy Whitcroft my $cnt = $realcnt; 231601fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 231701fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 231801fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 231901fa9147SAndy Whitcroft $cnt--; 232001fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2321721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2322fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2323fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2324fae17daeSAndy Whitcroft ($edge) = $1; 2325fae17daeSAndy Whitcroft last; 2326fae17daeSAndy Whitcroft } 2327773647a0SAndy Whitcroft } 2328773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2329773647a0SAndy Whitcroft $in_comment = 1; 2330773647a0SAndy Whitcroft } 2331773647a0SAndy Whitcroft 2332773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2333773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2334773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2335773647a0SAndy Whitcroft if (!defined $edge && 233683242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2337773647a0SAndy Whitcroft { 2338773647a0SAndy Whitcroft $in_comment = 1; 2339773647a0SAndy Whitcroft } 2340773647a0SAndy Whitcroft 2341773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2342773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2343773647a0SAndy Whitcroft 2344171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2345773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2346171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2347773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2348773647a0SAndy Whitcroft } 2349773647a0SAndy Whitcroft push(@lines, $line); 2350773647a0SAndy Whitcroft 2351773647a0SAndy Whitcroft if ($realcnt > 1) { 2352773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2353773647a0SAndy Whitcroft } else { 2354773647a0SAndy Whitcroft $realcnt = 0; 2355773647a0SAndy Whitcroft } 2356773647a0SAndy Whitcroft 2357773647a0SAndy Whitcroft #print "==>$rawline\n"; 2358773647a0SAndy Whitcroft #print "-->$line\n"; 2359de7d4f0eSAndy Whitcroft 2360de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2361de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2362de7d4f0eSAndy Whitcroft } 2363de7d4f0eSAndy Whitcroft } 2364de7d4f0eSAndy Whitcroft 23656c72ffaaSAndy Whitcroft $prefix = ''; 23666c72ffaaSAndy Whitcroft 2367773647a0SAndy Whitcroft $realcnt = 0; 2368773647a0SAndy Whitcroft $linenr = 0; 2369194f66fcSJoe Perches $fixlinenr = -1; 23700a920b5bSAndy Whitcroft foreach my $line (@lines) { 23710a920b5bSAndy Whitcroft $linenr++; 2372194f66fcSJoe Perches $fixlinenr++; 23731b5539b1SJoe Perches my $sline = $line; #copy of $line 23741b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 23750a920b5bSAndy Whitcroft 2376c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 23776c72ffaaSAndy Whitcroft 23780a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2379e518e9a5SJoe Perches if (!$in_commit_log && 238074fd4f34SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { 238174fd4f34SJoe Perches my $context = $4; 23820a920b5bSAndy Whitcroft $is_patch = 1; 23834a0df2efSAndy Whitcroft $first_line = $linenr + 1; 23840a920b5bSAndy Whitcroft $realline=$1-1; 23850a920b5bSAndy Whitcroft if (defined $2) { 23860a920b5bSAndy Whitcroft $realcnt=$3+1; 23870a920b5bSAndy Whitcroft } else { 23880a920b5bSAndy Whitcroft $realcnt=1+1; 23890a920b5bSAndy Whitcroft } 2390c2fdda0dSAndy Whitcroft annotate_reset(); 239113214adfSAndy Whitcroft $prev_values = 'E'; 239213214adfSAndy Whitcroft 2393773647a0SAndy Whitcroft %suppress_ifbraces = (); 2394170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 23952b474a1aSAndy Whitcroft %suppress_export = (); 23963e469cdcSAndy Whitcroft $suppress_statement = 0; 239774fd4f34SJoe Perches if ($context =~ /\b(\w+)\s*\(/) { 239874fd4f34SJoe Perches $context_function = $1; 239974fd4f34SJoe Perches } else { 240074fd4f34SJoe Perches undef $context_function; 240174fd4f34SJoe Perches } 24020a920b5bSAndy Whitcroft next; 24030a920b5bSAndy Whitcroft 24044a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 24054a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 24064a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2407773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 24080a920b5bSAndy Whitcroft $realline++; 2409d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 24100a920b5bSAndy Whitcroft 24114a0df2efSAndy Whitcroft # Measure the line length and indent. 2412c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 24130a920b5bSAndy Whitcroft 24140a920b5bSAndy Whitcroft # Track the previous line. 24150a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 24160a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2417c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2418c2fdda0dSAndy Whitcroft 2419773647a0SAndy Whitcroft #warn "line<$line>\n"; 24206c72ffaaSAndy Whitcroft 2421d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2422d8aaf121SAndy Whitcroft $realcnt--; 24230a920b5bSAndy Whitcroft } 24240a920b5bSAndy Whitcroft 2425cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2426cc77cdcaSAndy Whitcroft 24276c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 24286c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2429773647a0SAndy Whitcroft 24302ac73b4fSJoe Perches my $found_file = 0; 2431773647a0SAndy Whitcroft # extract the filename as it passes 24323bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 24333bf9a009SRabin Vincent $realfile = $1; 24342b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2435270c49a0SJoe Perches $in_commit_log = 0; 24362ac73b4fSJoe Perches $found_file = 1; 24373bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2438773647a0SAndy Whitcroft $realfile = $1; 24392b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2440270c49a0SJoe Perches $in_commit_log = 0; 24411e855726SWolfram Sang 24421e855726SWolfram Sang $p1_prefix = $1; 2443e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2444e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2445000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2446000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 24471e855726SWolfram Sang } 2448773647a0SAndy Whitcroft 2449c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2450000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2451000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2452773647a0SAndy Whitcroft } 24532ac73b4fSJoe Perches $found_file = 1; 24542ac73b4fSJoe Perches } 24552ac73b4fSJoe Perches 245634d8815fSJoe Perches#make up the handle for any error we report on this line 245734d8815fSJoe Perches if ($showfile) { 245834d8815fSJoe Perches $prefix = "$realfile:$realline: " 245934d8815fSJoe Perches } elsif ($emacs) { 24607d3a9f67SJoe Perches if ($file) { 24617d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 24627d3a9f67SJoe Perches } else { 246334d8815fSJoe Perches $prefix = "$filename:$linenr: "; 246434d8815fSJoe Perches } 24657d3a9f67SJoe Perches } 246634d8815fSJoe Perches 24672ac73b4fSJoe Perches if ($found_file) { 246885b0ee18SJoe Perches if (is_maintained_obsolete($realfile)) { 246985b0ee18SJoe Perches WARN("OBSOLETE", 247085b0ee18SJoe Perches "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); 247185b0ee18SJoe Perches } 24727bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 24732ac73b4fSJoe Perches $check = 1; 24742ac73b4fSJoe Perches } else { 24752ac73b4fSJoe Perches $check = $check_orig; 24762ac73b4fSJoe Perches } 24779f3a8992SRob Herring $checklicenseline = 1; 2478773647a0SAndy Whitcroft next; 2479773647a0SAndy Whitcroft } 2480773647a0SAndy Whitcroft 2481389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 24820a920b5bSAndy Whitcroft 2483c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2484c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2485c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 24860a920b5bSAndy Whitcroft 24876c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 24886c72ffaaSAndy Whitcroft 2489e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2490e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2491e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2492e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2493e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2494e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2495e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2496e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2497e518e9a5SJoe Perches $commit_log_has_diff = 1; 2498e518e9a5SJoe Perches } 2499e518e9a5SJoe Perches 25003bf9a009SRabin Vincent# Check for incorrect file permissions 25013bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 25023bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 250304db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 250404db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2505000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2506000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 25073bf9a009SRabin Vincent } 25083bf9a009SRabin Vincent } 25093bf9a009SRabin Vincent 251020112475SJoe Perches# Check the patch for a signoff: 2511d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 25124a0df2efSAndy Whitcroft $signoff++; 251315662b3eSJoe Perches $in_commit_log = 0; 25140a920b5bSAndy Whitcroft } 251520112475SJoe Perches 2516e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2517e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2518e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2519e0d975b1SJoe Perches $reported_maintainer_file = 1; 2520e0d975b1SJoe Perches } 2521e0d975b1SJoe Perches 252220112475SJoe Perches# Check signature styles 2523270c49a0SJoe Perches if (!$in_header_lines && 2524ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 252520112475SJoe Perches my $space_before = $1; 252620112475SJoe Perches my $sign_off = $2; 252720112475SJoe Perches my $space_after = $3; 252820112475SJoe Perches my $email = $4; 252920112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 253020112475SJoe Perches 2531ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2532ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2533ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2534ce0338dfSJoe Perches } 253520112475SJoe Perches if (defined $space_before && $space_before ne "") { 25363705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25373705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 25383705ce5bSJoe Perches $fix) { 2539194f66fcSJoe Perches $fixed[$fixlinenr] = 25403705ce5bSJoe Perches "$ucfirst_sign_off $email"; 25413705ce5bSJoe Perches } 254220112475SJoe Perches } 254320112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 25443705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25453705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 25463705ce5bSJoe Perches $fix) { 2547194f66fcSJoe Perches $fixed[$fixlinenr] = 25483705ce5bSJoe Perches "$ucfirst_sign_off $email"; 25493705ce5bSJoe Perches } 25503705ce5bSJoe Perches 255120112475SJoe Perches } 255220112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 25533705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 25543705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 25553705ce5bSJoe Perches $fix) { 2556194f66fcSJoe Perches $fixed[$fixlinenr] = 25573705ce5bSJoe Perches "$ucfirst_sign_off $email"; 25583705ce5bSJoe Perches } 255920112475SJoe Perches } 256020112475SJoe Perches 256120112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 256220112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 256320112475SJoe Perches if ($suggested_email eq "") { 2564000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2565000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 256620112475SJoe Perches } else { 256720112475SJoe Perches my $dequoted = $suggested_email; 256820112475SJoe Perches $dequoted =~ s/^"//; 256920112475SJoe Perches $dequoted =~ s/" </ </; 257020112475SJoe Perches # Don't force email to have quotes 257120112475SJoe Perches # Allow just an angle bracketed address 257220112475SJoe Perches if ("$dequoted$comment" ne $email && 257320112475SJoe Perches "<$email_address>$comment" ne $email && 257420112475SJoe Perches "$suggested_email$comment" ne $email) { 2575000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2576000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 257720112475SJoe Perches } 25780a920b5bSAndy Whitcroft } 25797e51f197SJoe Perches 25807e51f197SJoe Perches# Check for duplicate signatures 25817e51f197SJoe Perches my $sig_nospace = $line; 25827e51f197SJoe Perches $sig_nospace =~ s/\s//g; 25837e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 25847e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 25857e51f197SJoe Perches WARN("BAD_SIGN_OFF", 25867e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 25877e51f197SJoe Perches } else { 25887e51f197SJoe Perches $signatures{$sig_nospace} = 1; 25897e51f197SJoe Perches } 25900a920b5bSAndy Whitcroft } 25910a920b5bSAndy Whitcroft 2592a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2593a2fe16b9SJoe Perches if ($in_header_lines && 2594a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2595a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2596a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2597a2fe16b9SJoe Perches } 2598a2fe16b9SJoe Perches 25999b3189ebSJoe Perches# Check for old stable address 26009b3189ebSJoe Perches if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { 26019b3189ebSJoe Perches ERROR("STABLE_ADDRESS", 26029b3189ebSJoe Perches "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); 26039b3189ebSJoe Perches } 26049b3189ebSJoe Perches 26057ebd05efSChristopher Covington# Check for unwanted Gerrit info 26067ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 26077ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 26087ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 26097ebd05efSChristopher Covington } 26107ebd05efSChristopher Covington 2611369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2612369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2613369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2614369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2615369c8dd3SJoe Perches # timestamp 2616369c8dd3SJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) { 2617369c8dd3SJoe Perches # stack dump address 2618369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2619369c8dd3SJoe Perches } 2620369c8dd3SJoe Perches 26212a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 26222a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2623bf4daf12SJoe Perches length($line) > 75 && 2624bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2625bf4daf12SJoe Perches # file delta changes 2626bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2627bf4daf12SJoe Perches # filename then : 2628bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2629bf4daf12SJoe Perches # A Fixes: or Link: line 2630bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 26312a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 26322a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 26332a076f40SJoe Perches $commit_log_long_line = 1; 26342a076f40SJoe Perches } 26352a076f40SJoe Perches 2636bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2637bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2638bf4daf12SJoe Perches $line =~ /^\s*$/) { 2639bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2640bf4daf12SJoe Perches } 2641bf4daf12SJoe Perches 26420d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2643369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2644aab38f51SJoe Perches $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i && 2645e882dbfcSWei Wang $line !~ /^This reverts commit [0-9a-f]{7,40}/ && 2646fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2647aab38f51SJoe Perches ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && 2648369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2649bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2650fe043ea1SJoe Perches my $init_char = "c"; 2651fe043ea1SJoe Perches my $orig_commit = ""; 26520d7835fcSJoe Perches my $short = 1; 26530d7835fcSJoe Perches my $long = 0; 26540d7835fcSJoe Perches my $case = 1; 26550d7835fcSJoe Perches my $space = 1; 26560d7835fcSJoe Perches my $hasdesc = 0; 265719c146a6SJoe Perches my $hasparens = 0; 26580d7835fcSJoe Perches my $id = '0123456789ab'; 26590d7835fcSJoe Perches my $orig_desc = "commit description"; 26600d7835fcSJoe Perches my $description = ""; 26610d7835fcSJoe Perches 2662fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2663fe043ea1SJoe Perches $init_char = $1; 2664fe043ea1SJoe Perches $orig_commit = lc($2); 2665fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2666fe043ea1SJoe Perches $orig_commit = lc($1); 2667fe043ea1SJoe Perches } 2668fe043ea1SJoe Perches 26690d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 26700d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 26710d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 26720d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 26730d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 26740d7835fcSJoe Perches $orig_desc = $1; 267519c146a6SJoe Perches $hasparens = 1; 26760d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 26770d7835fcSJoe Perches defined $rawlines[$linenr] && 26780d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 26790d7835fcSJoe Perches $orig_desc = $1; 268019c146a6SJoe Perches $hasparens = 1; 2681b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2682b671fde0SJoe Perches defined $rawlines[$linenr] && 2683b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2684b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2685b671fde0SJoe Perches $orig_desc = $1; 2686b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2687b671fde0SJoe Perches $orig_desc .= " " . $1; 268819c146a6SJoe Perches $hasparens = 1; 26890d7835fcSJoe Perches } 26900d7835fcSJoe Perches 26910d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 26920d7835fcSJoe Perches $id, $orig_desc); 26930d7835fcSJoe Perches 2694948b133aSHeinrich Schuchardt if (defined($id) && 2695948b133aSHeinrich Schuchardt ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { 2696d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 26970d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 26980d7835fcSJoe Perches } 2699d311cd44SJoe Perches } 2700d311cd44SJoe Perches 270113f1937eSJoe Perches# Check for added, moved or deleted files 270213f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 270313f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 270413f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 270513f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 270613f1937eSJoe Perches (defined($1) || defined($2))))) { 2707a82603a8SAndrew Jeffery $is_patch = 1; 270813f1937eSJoe Perches $reported_maintainer_file = 1; 270913f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 271013f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 271113f1937eSJoe Perches } 271213f1937eSJoe Perches 271300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 27148905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2715000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2716000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 27176c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2718de7d4f0eSAndy Whitcroft } 2719de7d4f0eSAndy Whitcroft 2720de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2721de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2722171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2723171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2724171ae1a4SAndy Whitcroft 2725171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2726171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2727171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2728171ae1a4SAndy Whitcroft 272934d99219SJoe Perches CHK("INVALID_UTF8", 2730000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 273100df344fSAndy Whitcroft } 27320a920b5bSAndy Whitcroft 273315662b3eSJoe Perches# Check if it's the start of a commit log 273415662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 273515662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 2736eb3a58deSJoe Perches !($rawline =~ /^\s+(?:\S|$)/ || 2737eb3a58deSJoe Perches $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { 273815662b3eSJoe Perches $in_header_lines = 0; 273915662b3eSJoe Perches $in_commit_log = 1; 2740ed43c4e5SAllen Hubbe $has_commit_log = 1; 274115662b3eSJoe Perches } 274215662b3eSJoe Perches 2743fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2744fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2745fa64205dSPasi Savanainen if ($in_header_lines && 2746fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2747fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2748fa64205dSPasi Savanainen $non_utf8_charset = 1; 2749fa64205dSPasi Savanainen } 2750fa64205dSPasi Savanainen 2751fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 275215662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2753fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 275415662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 275515662b3eSJoe Perches } 275615662b3eSJoe Perches 2757d6430f71SJoe Perches# Check for absolute kernel paths in commit message 2758d6430f71SJoe Perches if ($tree && $in_commit_log) { 2759d6430f71SJoe Perches while ($line =~ m{(?:^|\s)(/\S*)}g) { 2760d6430f71SJoe Perches my $file = $1; 2761d6430f71SJoe Perches 2762d6430f71SJoe Perches if ($file =~ m{^(.*?)(?::\d+)+:?$} && 2763d6430f71SJoe Perches check_absolute_file($1, $herecurr)) { 2764d6430f71SJoe Perches # 2765d6430f71SJoe Perches } else { 2766d6430f71SJoe Perches check_absolute_file($file, $herecurr); 2767d6430f71SJoe Perches } 2768d6430f71SJoe Perches } 2769d6430f71SJoe Perches } 2770d6430f71SJoe Perches 277166b47b4aSKees Cook# Check for various typo / spelling mistakes 277266d7a382SJoe Perches if (defined($misspellings) && 277366d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2774ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 277566b47b4aSKees Cook my $typo = $1; 277666b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 277766b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 277866b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 27790675a8fbSJean Delvare my $msg_level = \&WARN; 27800675a8fbSJean Delvare $msg_level = \&CHK if ($file); 27810675a8fbSJean Delvare if (&{$msg_level}("TYPO_SPELLING", 278266b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 278366b47b4aSKees Cook $fix) { 278466b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 278566b47b4aSKees Cook } 278666b47b4aSKees Cook } 278766b47b4aSKees Cook } 278866b47b4aSKees Cook 278930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 279030670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 279100df344fSAndy Whitcroft 27920a920b5bSAndy Whitcroft#trailing whitespace 27939c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2794c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2795d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2796d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2797d5e616fcSJoe Perches $fix) { 2798194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2799d5e616fcSJoe Perches } 2800c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2801c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28023705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 28033705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 28043705ce5bSJoe Perches $fix) { 2805194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 28063705ce5bSJoe Perches } 28073705ce5bSJoe Perches 2808d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 28090a920b5bSAndy Whitcroft } 28105368df20SAndy Whitcroft 28114783f894SJosh Triplett# Check for FSF mailing addresses. 2812109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 28131bde561eSMatthew Wilcox $rawline =~ /\b675\s+Mass\s+Ave/i || 28143e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 28153e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 28164783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28170675a8fbSJean Delvare my $msg_level = \&ERROR; 28180675a8fbSJean Delvare $msg_level = \&CHK if ($file); 28190675a8fbSJean Delvare &{$msg_level}("FSF_MAILING_ADDRESS", 28204783f894SJosh 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) 28214783f894SJosh Triplett } 28224783f894SJosh Triplett 28233354957aSAndi Kleen# check for Kconfig help text having a real description 28249fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 28259fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 28263354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 2827678ae162SUlf Magnusson # 'choice' is usually the last thing on the line (though 2828678ae162SUlf Magnusson # Kconfig supports named choices), so use a word boundary 2829678ae162SUlf Magnusson # (\b) rather than a whitespace character (\s) 2830678ae162SUlf Magnusson $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { 28313354957aSAndi Kleen my $length = 0; 28329fe287d7SAndy Whitcroft my $cnt = $realcnt; 28339fe287d7SAndy Whitcroft my $ln = $linenr + 1; 28349fe287d7SAndy Whitcroft my $f; 2835a1385803SAndy Whitcroft my $is_start = 0; 28369fe287d7SAndy Whitcroft my $is_end = 0; 2837a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 28389fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 28399fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 28409fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 28419fe287d7SAndy Whitcroft 28429fe287d7SAndy Whitcroft next if ($f =~ /^-/); 28438d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2844a1385803SAndy Whitcroft 284586adf1a0SUlf Magnusson if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { 2846a1385803SAndy Whitcroft $is_start = 1; 284784af7a61SUlf Magnusson } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { 284884af7a61SUlf Magnusson if ($lines[$ln - 1] =~ "---help---") { 284984af7a61SUlf Magnusson WARN("CONFIG_DESCRIPTION", 285084af7a61SUlf Magnusson "prefer 'help' over '---help---' for new help texts\n" . $herecurr); 285184af7a61SUlf Magnusson } 2852a1385803SAndy Whitcroft $length = -1; 2853a1385803SAndy Whitcroft } 2854a1385803SAndy Whitcroft 28559fe287d7SAndy Whitcroft $f =~ s/^.//; 28563354957aSAndi Kleen $f =~ s/#.*//; 28573354957aSAndi Kleen $f =~ s/^\s+//; 28583354957aSAndi Kleen next if ($f =~ /^$/); 2859678ae162SUlf Magnusson 2860678ae162SUlf Magnusson # This only checks context lines in the patch 2861678ae162SUlf Magnusson # and so hopefully shouldn't trigger false 2862678ae162SUlf Magnusson # positives, even though some of these are 2863678ae162SUlf Magnusson # common words in help texts 2864678ae162SUlf Magnusson if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| 2865678ae162SUlf Magnusson if|endif|menu|endmenu|source)\b/x) { 28669fe287d7SAndy Whitcroft $is_end = 1; 28679fe287d7SAndy Whitcroft last; 28689fe287d7SAndy Whitcroft } 28693354957aSAndi Kleen $length++; 28703354957aSAndi Kleen } 287156193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 2872000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 287356193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 287456193274SVadim Bendebury } 2875a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 28763354957aSAndi Kleen } 28773354957aSAndi Kleen 2878628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form 2879628f91a2SJoe Perches if ($realfile =~ /^MAINTAINERS$/ && 2880628f91a2SJoe Perches $rawline =~ /^\+[A-Z]:/ && 2881628f91a2SJoe Perches $rawline !~ /^\+[A-Z]:\t\S/) { 2882628f91a2SJoe Perches if (WARN("MAINTAINERS_STYLE", 2883628f91a2SJoe Perches "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && 2884628f91a2SJoe Perches $fix) { 2885628f91a2SJoe Perches $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; 2886628f91a2SJoe Perches } 2887628f91a2SJoe Perches } 2888628f91a2SJoe Perches 2889327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 2890327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 2891327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 2892327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 2893327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 2894327953e9SChristoph Jaeger } 2895327953e9SChristoph Jaeger 2896c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2897c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2898c68e5878SArnaud Lacombe my $flag = $1; 2899c68e5878SArnaud Lacombe my $replacement = { 2900c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2901c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2902c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2903c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2904c68e5878SArnaud Lacombe }; 2905c68e5878SArnaud Lacombe 2906c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2907c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2908c68e5878SArnaud Lacombe } 2909c68e5878SArnaud Lacombe 2910bff5da43SRob Herring# check for DT compatible documentation 29117dd05b38SFlorian Vaussard if (defined $root && 29127dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 29137dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 29147dd05b38SFlorian Vaussard 2915bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2916bff5da43SRob Herring 2917cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2918cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2919cc93319bSFlorian Vaussard 2920bff5da43SRob Herring foreach my $compat (@compats) { 2921bff5da43SRob Herring my $compat2 = $compat; 2922185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2923185d566bSRob Herring my $compat3 = $compat; 2924185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2925185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2926bff5da43SRob Herring if ( $? >> 8 ) { 2927bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2928bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2929bff5da43SRob Herring } 2930bff5da43SRob Herring 29314fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 29324fbf32a6SFlorian Vaussard my $vendor = $1; 2933cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2934bff5da43SRob Herring if ( $? >> 8 ) { 2935bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2936cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2937bff5da43SRob Herring } 2938bff5da43SRob Herring } 2939bff5da43SRob Herring } 2940bff5da43SRob Herring 29419f3a8992SRob Herring# check for using SPDX license tag at beginning of files 29429f3a8992SRob Herring if ($realline == $checklicenseline) { 29439f3a8992SRob Herring if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { 29449f3a8992SRob Herring $checklicenseline = 2; 29459f3a8992SRob Herring } elsif ($rawline =~ /^\+/) { 29469f3a8992SRob Herring my $comment = ""; 29479f3a8992SRob Herring if ($realfile =~ /\.(h|s|S)$/) { 29489f3a8992SRob Herring $comment = '/*'; 29499f3a8992SRob Herring } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { 29509f3a8992SRob Herring $comment = '//'; 29519f3a8992SRob Herring } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) { 29529f3a8992SRob Herring $comment = '#'; 29539f3a8992SRob Herring } elsif ($realfile =~ /\.rst$/) { 29549f3a8992SRob Herring $comment = '..'; 29559f3a8992SRob Herring } 29569f3a8992SRob Herring 29579f3a8992SRob Herring if ($comment !~ /^$/ && 29589f3a8992SRob Herring $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) { 29599f3a8992SRob Herring WARN("SPDX_LICENSE_TAG", 29609f3a8992SRob Herring "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); 29619f3a8992SRob Herring } 29629f3a8992SRob Herring } 29639f3a8992SRob Herring } 29649f3a8992SRob Herring 29655368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 2966d6430f71SJoe Perches next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); 29675368df20SAndy Whitcroft 296847e0c88bSJoe Perches# line length limit (with some exclusions) 296947e0c88bSJoe Perches# 297047e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 297147e0c88bSJoe Perches# logging functions like pr_info that end in a string 297247e0c88bSJoe Perches# lines with a single string 297347e0c88bSJoe Perches# #defines that are a single string 29742e4bbbc5SAndreas Brauchli# lines with an RFC3986 like URL 297547e0c88bSJoe Perches# 297647e0c88bSJoe Perches# There are 3 different line length message types: 2977ab1ecabfSJean Delvare# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length 297847e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 297947e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 298047e0c88bSJoe Perches# 298147e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 298247e0c88bSJoe Perches# 298347e0c88bSJoe Perches 2984b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 298547e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 298647e0c88bSJoe Perches 298747e0c88bSJoe Perches # Check the allowed long line types first 298847e0c88bSJoe Perches 298947e0c88bSJoe Perches # logging functions that end in a string that starts 299047e0c88bSJoe Perches # before $max_line_length 299147e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 299247e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 299347e0c88bSJoe Perches $msg_type = ""; 299447e0c88bSJoe Perches 299547e0c88bSJoe Perches # lines with only strings (w/ possible termination) 299647e0c88bSJoe Perches # #defines with only strings 299747e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 299847e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 299947e0c88bSJoe Perches $msg_type = ""; 300047e0c88bSJoe Perches 3001cc147506SJoe Perches # More special cases 3002cc147506SJoe Perches } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || 3003cc147506SJoe Perches $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { 3004d560a5f8SJoe Perches $msg_type = ""; 3005d560a5f8SJoe Perches 30062e4bbbc5SAndreas Brauchli # URL ($rawline is used in case the URL is in a comment) 30072e4bbbc5SAndreas Brauchli } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { 30082e4bbbc5SAndreas Brauchli $msg_type = ""; 30092e4bbbc5SAndreas Brauchli 301047e0c88bSJoe Perches # Otherwise set the alternate message types 301147e0c88bSJoe Perches 301247e0c88bSJoe Perches # a comment starts before $max_line_length 301347e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 301447e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 301547e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 301647e0c88bSJoe Perches 301747e0c88bSJoe Perches # a quoted string starts before $max_line_length 301847e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 301947e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 302047e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 302147e0c88bSJoe Perches } 302247e0c88bSJoe Perches 302347e0c88bSJoe Perches if ($msg_type ne "" && 302447e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 302547e0c88bSJoe Perches WARN($msg_type, 30266cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 30270a920b5bSAndy Whitcroft } 302847e0c88bSJoe Perches } 30290a920b5bSAndy Whitcroft 30308905a67cSAndy Whitcroft# check for adding lines without a newline. 30318905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 3032000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 3033000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 30348905a67cSAndy Whitcroft } 30358905a67cSAndy Whitcroft 3036b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 3037de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 30380a920b5bSAndy Whitcroft 30390a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 30400a920b5bSAndy Whitcroft# more than 8 must use tabs. 3041c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 3042c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 3043c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 3044d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 30453705ce5bSJoe Perches if (ERROR("CODE_INDENT", 30463705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 30473705ce5bSJoe Perches $fix) { 3048194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 30493705ce5bSJoe Perches } 30500a920b5bSAndy Whitcroft } 30510a920b5bSAndy Whitcroft 305208e44365SAlberto Panizzo# check for space before tabs. 305308e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 305408e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 30553705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 30563705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 30573705ce5bSJoe Perches $fix) { 3058194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3059d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 3060194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 3061c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 30623705ce5bSJoe Perches } 306308e44365SAlberto Panizzo } 306408e44365SAlberto Panizzo 30656a487211SJoe Perches# check for assignments on the start of a line 30666a487211SJoe Perches if ($sline =~ /^\+\s+($Assignment)[^=]/) { 30676a487211SJoe Perches CHK("ASSIGNMENT_CONTINUATIONS", 30686a487211SJoe Perches "Assignment operator '$1' should be on the previous line\n" . $hereprev); 30696a487211SJoe Perches } 30706a487211SJoe Perches 3071d1fe9c09SJoe Perches# check for && or || at the start of a line 3072d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 3073d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 3074d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 3075d1fe9c09SJoe Perches } 3076d1fe9c09SJoe Perches 3077a91e8994SJoe Perches# check indentation starts on a tab stop 3078a91e8994SJoe Perches if ($^V && $^V ge 5.10.0 && 3079bd49111fSJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { 3080a91e8994SJoe Perches my $indent = length($1); 3081a91e8994SJoe Perches if ($indent % 8) { 3082a91e8994SJoe Perches if (WARN("TABSTOP", 3083a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 3084a91e8994SJoe Perches $fix) { 3085a91e8994SJoe Perches $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; 3086a91e8994SJoe Perches } 3087a91e8994SJoe Perches } 3088a91e8994SJoe Perches } 3089a91e8994SJoe Perches 3090d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 3091d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3092fd71f632SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 3093d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 3094d1fe9c09SJoe Perches my $oldindent = $1; 3095d1fe9c09SJoe Perches my $rest = $2; 3096d1fe9c09SJoe Perches 3097d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 3098d1fe9c09SJoe Perches if ($pos >= 0) { 3099b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 3100b34a26f3SJoe Perches my $newindent = $2; 3101d1fe9c09SJoe Perches 3102d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 3103d1fe9c09SJoe Perches "\t" x ($pos / 8) . 3104d1fe9c09SJoe Perches " " x ($pos % 8); 3105d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 3106d1fe9c09SJoe Perches 3107d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 3108d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 31093705ce5bSJoe Perches 31103705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 31113705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 31123705ce5bSJoe Perches $fix && $line =~ /^\+/) { 3113194f66fcSJoe Perches $fixed[$fixlinenr] =~ 31143705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 31153705ce5bSJoe Perches } 3116d1fe9c09SJoe Perches } 3117d1fe9c09SJoe Perches } 3118d1fe9c09SJoe Perches } 3119d1fe9c09SJoe Perches 31206ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 31216ab3a970SJoe Perches# avoid checking a few false positives: 31226ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 31236ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 31246ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 31256ab3a970SJoe Perches# multiline macros that define functions 31266ab3a970SJoe Perches# known attributes or the __attribute__ keyword 31276ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 31286ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 31293705ce5bSJoe Perches if (CHK("SPACING", 3130f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 31313705ce5bSJoe Perches $fix) { 3132194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3133f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 31343705ce5bSJoe Perches } 3135aad4f614SJoe Perches } 3136aad4f614SJoe Perches 313786406b1cSJoe Perches# Block comment styles 313886406b1cSJoe Perches# Networking with an initial /* 313905880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 3140fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 314185ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 314285ad978cSJoe Perches $realline > 2) { 314305880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 314405880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 314505880600SJoe Perches } 314605880600SJoe Perches 314786406b1cSJoe Perches# Block comments use * on subsequent lines 314886406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 314986406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 3150a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 315161135e96SJoe Perches $rawline =~ /^\+/ && #line is new 3152a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 315386406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 315486406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 3155a605e32eSJoe Perches } 3156a605e32eSJoe Perches 315786406b1cSJoe Perches# Block comments use */ on trailing lines 315886406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 3159c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 3160c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 3161c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 316286406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 316386406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 316405880600SJoe Perches } 316505880600SJoe Perches 316608eb9b80SJoe Perches# Block comment * alignment 316708eb9b80SJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 3168af207524SJoe Perches $line =~ /^\+[ \t]*$;/ && #leading comment 3169af207524SJoe Perches $rawline =~ /^\+[ \t]*\*/ && #leading * 3170af207524SJoe Perches (($prevrawline =~ /^\+.*?\/\*/ && #leading /* 317108eb9b80SJoe Perches $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ 3172af207524SJoe Perches $prevrawline =~ /^\+[ \t]*\*/)) { #leading * 3173af207524SJoe Perches my $oldindent; 317408eb9b80SJoe Perches $prevrawline =~ m@^\+([ \t]*/?)\*@; 3175af207524SJoe Perches if (defined($1)) { 3176af207524SJoe Perches $oldindent = expand_tabs($1); 3177af207524SJoe Perches } else { 3178af207524SJoe Perches $prevrawline =~ m@^\+(.*/?)\*@; 3179af207524SJoe Perches $oldindent = expand_tabs($1); 3180af207524SJoe Perches } 318108eb9b80SJoe Perches $rawline =~ m@^\+([ \t]*)\*@; 318208eb9b80SJoe Perches my $newindent = $1; 318308eb9b80SJoe Perches $newindent = expand_tabs($newindent); 3184af207524SJoe Perches if (length($oldindent) ne length($newindent)) { 318508eb9b80SJoe Perches WARN("BLOCK_COMMENT_STYLE", 318608eb9b80SJoe Perches "Block comments should align the * on each line\n" . $hereprev); 318708eb9b80SJoe Perches } 318808eb9b80SJoe Perches } 318908eb9b80SJoe Perches 31907f619191SJoe Perches# check for missing blank lines after struct/union declarations 31917f619191SJoe Perches# with exceptions for various attributes and macros 31927f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 31937f619191SJoe Perches $line =~ /^\+/ && 31947f619191SJoe Perches !($line =~ /^\+\s*$/ || 31957f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 31967f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 31977f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 31987f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 31997f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 32007f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 32010bc989ffSMasahiro Yamada $line =~ /^\+\s*builtin_[\w_]*driver/ || 32027f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 3203d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3204d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 3205d752fcc8SJoe Perches $fix) { 3206f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3207d752fcc8SJoe Perches } 32087f619191SJoe Perches } 32097f619191SJoe Perches 3210365dd4eaSJoe Perches# check for multiple consecutive blank lines 3211365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 3212365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 3213365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 3214d752fcc8SJoe Perches if (CHK("LINE_SPACING", 3215d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 3216d752fcc8SJoe Perches $fix) { 3217f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3218d752fcc8SJoe Perches } 3219d752fcc8SJoe Perches 3220365dd4eaSJoe Perches $last_blank_line = $linenr; 3221365dd4eaSJoe Perches } 3222365dd4eaSJoe Perches 32233b617e3bSJoe Perches# check for missing blank lines after declarations 32243f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 32253f7bac03SJoe Perches # actual declarations 32263f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 32275a4e1fd3SJoe Perches # function pointer declarations 32285a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 32293f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 32303f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 32313f7bac03SJoe Perches # known declaration macros 32323f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 32333f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 32343f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 32353f7bac03SJoe Perches # other possible extensions of declaration lines 32363f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 32373f7bac03SJoe Perches # not starting a section or a macro "\" extended line 32383f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 32393f7bac03SJoe Perches # looks like a declaration 32403f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 32415a4e1fd3SJoe Perches # function pointer declarations 32425a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 32433f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 32443f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 32453f7bac03SJoe Perches # known declaration macros 32463f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 32473f7bac03SJoe Perches # start of struct or union or enum 32483b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 32493f7bac03SJoe Perches # start or end of block or continuation of declaration 32503f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 32513f7bac03SJoe Perches # bitfield continuation 32523f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 32533f7bac03SJoe Perches # other possible extensions of declaration lines 32543f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 32553f7bac03SJoe Perches # indentation of previous and current line are the same 32563f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 3257d752fcc8SJoe Perches if (WARN("LINE_SPACING", 3258d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3259d752fcc8SJoe Perches $fix) { 3260f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3261d752fcc8SJoe Perches } 32623b617e3bSJoe Perches } 32633b617e3bSJoe Perches 32645f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 32656b4c5bebSAndy Whitcroft# Exceptions: 32666b4c5bebSAndy Whitcroft# 1) within comments 32676b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 32686b4c5bebSAndy Whitcroft# 3) hanging labels 32693705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 32705f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 32713705ce5bSJoe Perches if (WARN("LEADING_SPACE", 32723705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 32733705ce5bSJoe Perches $fix) { 3274194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 32753705ce5bSJoe Perches } 32765f7ddae6SRaffaele Recalcati } 32775f7ddae6SRaffaele Recalcati 3278b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3279b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3280b9ea10d6SAndy Whitcroft 32815751a24eSJoe Perches# check for unusual line ending [ or ( 32825751a24eSJoe Perches if ($line =~ /^\+.*([\[\(])\s*$/) { 32835751a24eSJoe Perches CHK("OPEN_ENDED_LINE", 32845751a24eSJoe Perches "Lines should not end with a '$1'\n" . $herecurr); 32855751a24eSJoe Perches } 32865751a24eSJoe Perches 32874dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name 32884dbed76fSJoe Perches if ($sline =~ /^\+\{\s*$/ && 32894dbed76fSJoe Perches $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { 32904dbed76fSJoe Perches $context_function = $1; 32914dbed76fSJoe Perches } 32924dbed76fSJoe Perches 32934dbed76fSJoe Perches# check if this appears to be the end of function declaration 32944dbed76fSJoe Perches if ($sline =~ /^\+\}\s*$/) { 32954dbed76fSJoe Perches undef $context_function; 32964dbed76fSJoe Perches } 32974dbed76fSJoe Perches 3298032a4c0fSJoe Perches# check indentation of any line with a bare else 3299840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3300032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3301032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3302032a4c0fSJoe Perches my $tabs = length($1) + 1; 3303840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3304840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3305840080a0SJoe Perches defined $lines[$linenr] && 3306840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3307032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3308032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3309032a4c0fSJoe Perches } 3310032a4c0fSJoe Perches } 3311032a4c0fSJoe Perches 3312c00df19aSJoe Perches# check indentation of a line with a break; 3313c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3314c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3315c00df19aSJoe Perches my $tabs = $1; 3316c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3317c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3318c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3319c00df19aSJoe Perches } 3320c00df19aSJoe Perches } 3321c00df19aSJoe Perches 3322c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3323cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3324000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3325000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3326c2fdda0dSAndy Whitcroft } 332722f2a2efSAndy Whitcroft 332856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 332956e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 333056e77d70SJoe Perches WARN("HOTPLUG_SECTION", 333156e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 333256e77d70SJoe Perches } 333356e77d70SJoe Perches 33349c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 33352b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 33362b474a1aSAndy Whitcroft $realline_next); 33373e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 3338ca819864SJoe Perches if ($linenr > $suppress_statement && 33391b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3340170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3341f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3342171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3343171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3344171ae1a4SAndy Whitcroft 33453e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 33463e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 33473e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 33483e469cdcSAndy Whitcroft # until we hit end of it. 33493e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 33503e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 33513e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 33523e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 33533e469cdcSAndy Whitcroft } 3354f74bd194SAndy Whitcroft 33552b474a1aSAndy Whitcroft # Find the real next line. 33562b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 33572b474a1aSAndy Whitcroft if (defined $realline_next && 33582b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 33592b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 33602b474a1aSAndy Whitcroft $realline_next++; 33612b474a1aSAndy Whitcroft } 33622b474a1aSAndy Whitcroft 3363171ae1a4SAndy Whitcroft my $s = $stat; 3364171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3365cf655043SAndy Whitcroft 3366c2fdda0dSAndy Whitcroft # Ignore goto labels. 3367171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3368c2fdda0dSAndy Whitcroft 3369c2fdda0dSAndy Whitcroft # Ignore functions being called 3370171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3371c2fdda0dSAndy Whitcroft 3372463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3373463f2864SAndy Whitcroft 3374c45dcabdSAndy Whitcroft # declarations always start with types 3375d2506586SAndy 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) { 3376c45dcabdSAndy Whitcroft my $type = $1; 3377c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3378c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3379c45dcabdSAndy Whitcroft 33806c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3381a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3382c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3383c2fdda0dSAndy Whitcroft } 33848905a67cSAndy Whitcroft 33856c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 338665863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3387c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 33889c0ca6f9SAndy Whitcroft } 33898905a67cSAndy Whitcroft 33908905a67cSAndy Whitcroft # Check for any sort of function declaration. 33918905a67cSAndy Whitcroft # int foo(something bar, other baz); 33928905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3393171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 33948905a67cSAndy Whitcroft my ($name_len) = length($1); 33958905a67cSAndy Whitcroft 3396cf655043SAndy Whitcroft my $ctx = $s; 3397773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 33988905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3399cf655043SAndy Whitcroft 34008905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3401c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 34028905a67cSAndy Whitcroft 3403c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 34048905a67cSAndy Whitcroft } 34058905a67cSAndy Whitcroft } 34068905a67cSAndy Whitcroft } 34078905a67cSAndy Whitcroft 34089c0ca6f9SAndy Whitcroft } 34099c0ca6f9SAndy Whitcroft 341000df344fSAndy Whitcroft# 341100df344fSAndy Whitcroft# Checks which may be anchored in the context. 341200df344fSAndy Whitcroft# 341300df344fSAndy Whitcroft 341400df344fSAndy Whitcroft# Check for switch () and associated case and default 341500df344fSAndy Whitcroft# statements should be at the same indent. 341600df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 341700df344fSAndy Whitcroft my $err = ''; 341800df344fSAndy Whitcroft my $sep = ''; 341900df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 342000df344fSAndy Whitcroft shift(@ctx); 342100df344fSAndy Whitcroft for my $ctx (@ctx) { 342200df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 342300df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 342400df344fSAndy Whitcroft $indent != $cindent) { 342500df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 342600df344fSAndy Whitcroft $sep = ''; 342700df344fSAndy Whitcroft } else { 342800df344fSAndy Whitcroft $sep = "[...]\n"; 342900df344fSAndy Whitcroft } 343000df344fSAndy Whitcroft } 343100df344fSAndy Whitcroft if ($err ne '') { 3432000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3433000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3434de7d4f0eSAndy Whitcroft } 3435de7d4f0eSAndy Whitcroft } 3436de7d4f0eSAndy Whitcroft 3437de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3438de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 34390fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3440773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3441773647a0SAndy Whitcroft 34429c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 34438eef05ddSJoe Perches 34448eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 34458eef05ddSJoe Perches WARN("DEEP_INDENTATION", 34468eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 34478eef05ddSJoe Perches } 34488eef05ddSJoe Perches 3449de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3450de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3451de7d4f0eSAndy Whitcroft 3452548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3453548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3454de7d4f0eSAndy Whitcroft 3455548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3456548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3457548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3458548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3459548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3460773647a0SAndy Whitcroft $ctx_ln++; 3461773647a0SAndy Whitcroft } 3462548596d5SAndy Whitcroft 346353210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 346453210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3465773647a0SAndy Whitcroft 3466773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3467000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3468000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 346901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 347000df344fSAndy Whitcroft } 3471773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3472773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3473773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3474773647a0SAndy Whitcroft { 34759c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 34769c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3477000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3478000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 347901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 34809c0ca6f9SAndy Whitcroft } 34819c0ca6f9SAndy Whitcroft } 348200df344fSAndy Whitcroft } 348300df344fSAndy Whitcroft 34844d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 3485f6950a73SJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 34863e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 34873e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 34883e469cdcSAndy Whitcroft if (!defined $stat); 34894d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 34904d001e4dSAndy Whitcroft 34914d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 34924d001e4dSAndy Whitcroft 34939f5af480SJoe Perches # remove inline comments 34949f5af480SJoe Perches $s =~ s/$;/ /g; 34959f5af480SJoe Perches $c =~ s/$;/ /g; 34964d001e4dSAndy Whitcroft 34974d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 34986f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 34996f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 35004d001e4dSAndy Whitcroft 35019f5af480SJoe Perches # Make sure we remove the line prefixes as we have 35029f5af480SJoe Perches # none on the first line, and are going to readd them 35039f5af480SJoe Perches # where necessary. 35049f5af480SJoe Perches $s =~ s/\n./\n/gs; 35059f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 35069f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 35079f5af480SJoe Perches } 35089f5af480SJoe Perches 35094d001e4dSAndy Whitcroft # We want to check the first line inside the block 35104d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 35114d001e4dSAndy Whitcroft # 1) any blank line termination 35124d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 35134d001e4dSAndy Whitcroft # 3) any do (...) { 35144d001e4dSAndy Whitcroft my $continuation = 0; 35154d001e4dSAndy Whitcroft my $check = 0; 35164d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 35174d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 35184d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 35194d001e4dSAndy Whitcroft $continuation = 1; 35204d001e4dSAndy Whitcroft } 35219bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 35224d001e4dSAndy Whitcroft $check = 1; 35234d001e4dSAndy Whitcroft $cond_lines++; 35244d001e4dSAndy Whitcroft } 35254d001e4dSAndy Whitcroft 35264d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 35274d001e4dSAndy Whitcroft # preprocessor statement. 35284d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 35294d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 35304d001e4dSAndy Whitcroft $check = 0; 35314d001e4dSAndy Whitcroft } 35324d001e4dSAndy Whitcroft 35339bd49efeSAndy Whitcroft my $cond_ptr = -1; 3534740504c6SAndy Whitcroft $continuation = 0; 35359bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 35369bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 35374d001e4dSAndy Whitcroft 3538f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3539f16fa28fSAndy Whitcroft # is not linear. 3540f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3541f16fa28fSAndy Whitcroft $check = 0; 3542f16fa28fSAndy Whitcroft } 3543f16fa28fSAndy Whitcroft 35449bd49efeSAndy Whitcroft # Ignore: 35459bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 35469bd49efeSAndy Whitcroft # 2) preprocessor lines, and 35479bd49efeSAndy Whitcroft # 3) labels. 3548740504c6SAndy Whitcroft if ($continuation || 3549740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 35509bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 35519bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3552740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 355330dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 35549bd49efeSAndy Whitcroft $cond_lines++; 35559bd49efeSAndy Whitcroft } 35564d001e4dSAndy Whitcroft } 355730dad6ebSAndy Whitcroft } 35584d001e4dSAndy Whitcroft 35594d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 35604d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 35614d001e4dSAndy Whitcroft 35624d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 35634d001e4dSAndy Whitcroft # this is not this patch's fault. 35644d001e4dSAndy Whitcroft if (!defined($stat_real) || 35654d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 35664d001e4dSAndy Whitcroft $check = 0; 35674d001e4dSAndy Whitcroft } 35684d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 35694d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 35704d001e4dSAndy Whitcroft } 35714d001e4dSAndy Whitcroft 35729bd49efeSAndy 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"; 35734d001e4dSAndy Whitcroft 35749f5af480SJoe Perches if ($check && $s ne '' && 35759f5af480SJoe Perches (($sindent % 8) != 0 || 35769f5af480SJoe Perches ($sindent < $indent) || 3577f6950a73SJoe Perches ($sindent == $indent && 3578f6950a73SJoe Perches ($s !~ /^\s*(?:\}|\{|else\b)/)) || 35799f5af480SJoe Perches ($sindent > $indent + 8))) { 3580000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3581000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 35824d001e4dSAndy Whitcroft } 35834d001e4dSAndy Whitcroft } 35844d001e4dSAndy Whitcroft 35856c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 35866c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 35871f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 35881f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 35896c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3590c2fdda0dSAndy Whitcroft if ($dbg_values) { 3591c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3592cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3593cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 35941f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3595c2fdda0dSAndy Whitcroft } 35966c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 35976c72ffaaSAndy Whitcroft 359800df344fSAndy Whitcroft#ignore lines not being added 35993705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 360000df344fSAndy Whitcroft 360111ca40a0SJoe Perches# check for dereferences that span multiple lines 360211ca40a0SJoe Perches if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && 360311ca40a0SJoe Perches $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { 360411ca40a0SJoe Perches $prevline =~ /($Lval\s*(?:\.|->))\s*$/; 360511ca40a0SJoe Perches my $ref = $1; 360611ca40a0SJoe Perches $line =~ /^.\s*($Lval)/; 360711ca40a0SJoe Perches $ref .= $1; 360811ca40a0SJoe Perches $ref =~ s/\s//g; 360911ca40a0SJoe Perches WARN("MULTILINE_DEREFERENCE", 361011ca40a0SJoe Perches "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); 361111ca40a0SJoe Perches } 361211ca40a0SJoe Perches 3613a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3614c8447115SJoe Perches while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3615a1ce18e4SJoe Perches my $type = $1; 3616a1ce18e4SJoe Perches my $var = $2; 3617207a8e84SJoe Perches $var = "" if (!defined $var); 3618207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3619a1ce18e4SJoe Perches my $sign = $1; 3620a1ce18e4SJoe Perches my $pointer = $2; 3621a1ce18e4SJoe Perches 3622a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3623a1ce18e4SJoe Perches 3624a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3625a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3626a1ce18e4SJoe Perches $fix) { 3627a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3628207a8e84SJoe Perches my $comp_pointer = $pointer; 3629207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3630207a8e84SJoe Perches $decl .= $comp_pointer; 3631207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3632207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3633a1ce18e4SJoe Perches } 3634a1ce18e4SJoe Perches } 3635a1ce18e4SJoe Perches } 3636a1ce18e4SJoe Perches 3637653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 36387429c690SAndy Whitcroft if ($dbg_type) { 36397429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3640000d1cc1SJoe Perches ERROR("TEST_TYPE", 3641000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 36427429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3643000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3644000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 36457429c690SAndy Whitcroft } 3646653d4876SAndy Whitcroft next; 3647653d4876SAndy Whitcroft } 3648a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3649a1ef277eSAndy Whitcroft if ($dbg_attr) { 36509360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3651000d1cc1SJoe Perches ERROR("TEST_ATTR", 3652000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 36539360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3654000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3655000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3656a1ef277eSAndy Whitcroft } 3657a1ef277eSAndy Whitcroft next; 3658a1ef277eSAndy Whitcroft } 3659653d4876SAndy Whitcroft 3660f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 366199423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 366299423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3663d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3664d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3665f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3666f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3667f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3668d752fcc8SJoe Perches my $fixedline = $prevrawline; 3669d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3670f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3671d752fcc8SJoe Perches $fixedline = $line; 36728d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1/; 3673f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3674d752fcc8SJoe Perches } 3675f0a594c1SAndy Whitcroft } 3676f0a594c1SAndy Whitcroft 367700df344fSAndy Whitcroft# 367800df344fSAndy Whitcroft# Checks which are anchored on the added line. 367900df344fSAndy Whitcroft# 368000df344fSAndy Whitcroft 3681653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3682c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3683653d4876SAndy Whitcroft my $path = $1; 3684653d4876SAndy Whitcroft if ($path =~ m{//}) { 3685000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3686495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3687495e9d84SJoe Perches } 3688495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3689495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3690495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3691653d4876SAndy Whitcroft } 3692653d4876SAndy Whitcroft } 3693653d4876SAndy Whitcroft 369400df344fSAndy Whitcroft# no C99 // comments 369500df344fSAndy Whitcroft if ($line =~ m{//}) { 36963705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 36973705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 36983705ce5bSJoe Perches $fix) { 3699194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 37003705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 37013705ce5bSJoe Perches my $comment = trim($1); 3702194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 37033705ce5bSJoe Perches } 37043705ce5bSJoe Perches } 370500df344fSAndy Whitcroft } 370600df344fSAndy Whitcroft # Remove C99 comments. 37070a920b5bSAndy Whitcroft $line =~ s@//.*@@; 37086c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 37090a920b5bSAndy Whitcroft 37102b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 37112b474a1aSAndy Whitcroft# the whole statement. 37122b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 37132b474a1aSAndy Whitcroft if (defined $realline_next && 37142b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 37152b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 37162b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 37172b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 37183cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 37193cbf62dfSAndy Whitcroft # a prefix: 37203cbf62dfSAndy Whitcroft # XXX(foo); 37213cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3722653d4876SAndy Whitcroft my $name = $1; 372387a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 37243cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 37253cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 37263cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 37273cbf62dfSAndy Whitcroft 37283cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 37292b474a1aSAndy Whitcroft \n.}\s*$| 373048012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 373148012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 373248012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 37332b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 37342b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 373548012058SAndy Whitcroft )/x) { 37362b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 37372b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 37382b474a1aSAndy Whitcroft } else { 37392b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 37400a920b5bSAndy Whitcroft } 37410a920b5bSAndy Whitcroft } 37422b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 37432b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 37442b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 37452b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 37462b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 37472b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 37482b474a1aSAndy Whitcroft } 37492b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 37502b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3751000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3752000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 37532b474a1aSAndy Whitcroft } 37540a920b5bSAndy Whitcroft 37555150bda4SJoe Eloff# check for global initialisers. 37566d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3757d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 37586d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3759d5e616fcSJoe Perches $fix) { 37606d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3761d5e616fcSJoe Perches } 3762f0a594c1SAndy Whitcroft } 37630a920b5bSAndy Whitcroft# check for static initialisers. 37646d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3765d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 37666d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3767d5e616fcSJoe Perches $herecurr) && 3768d5e616fcSJoe Perches $fix) { 37696d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3770d5e616fcSJoe Perches } 37710a920b5bSAndy Whitcroft } 37720a920b5bSAndy Whitcroft 37731813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 37741813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 37751813087dSJoe Perches my $tmp = trim($1); 37761813087dSJoe Perches WARN("MISORDERED_TYPE", 37771813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 37781813087dSJoe Perches } 37791813087dSJoe Perches 3780cb710ecaSJoe Perches# check for static const char * arrays. 3781cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3782000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3783000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3784cb710ecaSJoe Perches $herecurr); 3785cb710ecaSJoe Perches } 3786cb710ecaSJoe Perches 3787cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3788cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3789000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3790000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3791cb710ecaSJoe Perches $herecurr); 3792cb710ecaSJoe Perches } 3793cb710ecaSJoe Perches 3794ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 3795ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3796ab7e23f3SJoe Perches my $found = $1; 3797ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3798ab7e23f3SJoe Perches WARN("CONST_CONST", 3799ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3800ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3801ab7e23f3SJoe Perches WARN("CONST_CONST", 3802ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 3803ab7e23f3SJoe Perches } 3804ab7e23f3SJoe Perches } 3805ab7e23f3SJoe Perches 38069b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 38079b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 38089b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 38099b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 38109b0fa60dSJoe Perches $herecurr); 38119b0fa60dSJoe Perches } 38129b0fa60dSJoe Perches 3813b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 3814b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 3815b598b670SJoe Perches my $array = $1; 3816b598b670SJoe 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*\))@) { 3817b598b670SJoe Perches my $array_div = $1; 3818b598b670SJoe Perches if (WARN("ARRAY_SIZE", 3819b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 3820b598b670SJoe Perches $fix) { 3821b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 3822b598b670SJoe Perches } 3823b598b670SJoe Perches } 3824b598b670SJoe Perches } 3825b598b670SJoe Perches 3826b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 3827b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3828b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 3829b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 3830b36190c5SJoe Perches $fix) { 3831194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 3832b36190c5SJoe Perches } 3833b36190c5SJoe Perches } 3834b36190c5SJoe Perches 3835653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 3836653d4876SAndy Whitcroft# make sense. 3837653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 38388054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3839c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 38408ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 384146d832f5SMichael S. Tsirkin $line !~ /\b__bitwise\b/) { 3842000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 3843000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 38440a920b5bSAndy Whitcroft } 38450a920b5bSAndy Whitcroft 38460a920b5bSAndy Whitcroft# * goes on variable not on type 384765863862SAndy Whitcroft # (char*[ const]) 3848bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 3849bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 38503705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 3851d8aaf121SAndy Whitcroft 385265863862SAndy Whitcroft # Should start with a space. 385365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 385465863862SAndy Whitcroft # Should not end with a space. 385565863862SAndy Whitcroft $to =~ s/\s+$//; 385665863862SAndy Whitcroft # '*'s should not have spaces between. 3857f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 385865863862SAndy Whitcroft } 3859d8aaf121SAndy Whitcroft 38603705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 386165863862SAndy Whitcroft if ($from ne $to) { 38623705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 38633705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 38643705ce5bSJoe Perches $fix) { 38653705ce5bSJoe Perches my $sub_from = $ident; 38663705ce5bSJoe Perches my $sub_to = $ident; 38673705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3868194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38693705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 38703705ce5bSJoe Perches } 387165863862SAndy Whitcroft } 3872bfcb2cc7SAndy Whitcroft } 3873bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 3874bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 38753705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 3876d8aaf121SAndy Whitcroft 387765863862SAndy Whitcroft # Should start with a space. 387865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 387965863862SAndy Whitcroft # Should not end with a space. 388065863862SAndy Whitcroft $to =~ s/\s+$//; 388165863862SAndy Whitcroft # '*'s should not have spaces between. 3882f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 388365863862SAndy Whitcroft } 388465863862SAndy Whitcroft # Modifiers should have spaces. 388565863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 388665863862SAndy Whitcroft 38873705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 3888667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 38893705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 38903705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 38913705ce5bSJoe Perches $fix) { 38923705ce5bSJoe Perches 38933705ce5bSJoe Perches my $sub_from = $match; 38943705ce5bSJoe Perches my $sub_to = $match; 38953705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3896194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38973705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 38983705ce5bSJoe Perches } 389965863862SAndy Whitcroft } 39000a920b5bSAndy Whitcroft } 39010a920b5bSAndy Whitcroft 39029d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 39039d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 39040675a8fbSJean Delvare my $msg_level = \&WARN; 39050675a8fbSJean Delvare $msg_level = \&CHK if ($file); 39060675a8fbSJean Delvare &{$msg_level}("AVOID_BUG", 39079d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 39089d3e3c70SJoe Perches } 39090a920b5bSAndy Whitcroft 39109d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 39118905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 3912000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 3913000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 39148905a67cSAndy Whitcroft } 39158905a67cSAndy Whitcroft 391617441227SJoe Perches# check for uses of printk_ratelimit 391717441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 3918000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 3919000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 392017441227SJoe Perches } 392117441227SJoe Perches 3922eeef5733SJoe Perches# printk should use KERN_* levels 3923eeef5733SJoe Perches if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { 3924000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 3925eeef5733SJoe Perches "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); 392600df344fSAndy Whitcroft } 39270a920b5bSAndy Whitcroft 3928243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 3929243f3803SJoe Perches my $orig = $1; 3930243f3803SJoe Perches my $level = lc($orig); 3931243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 39328f26b837SJoe Perches my $level2 = $level; 39338f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 3934243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 3935daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 3936243f3803SJoe Perches } 3937243f3803SJoe Perches 3938243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 3939d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 3940d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 3941d5e616fcSJoe Perches $fix) { 3942194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3943d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 3944d5e616fcSJoe Perches } 3945243f3803SJoe Perches } 3946243f3803SJoe Perches 3947dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 3948dc139313SJoe Perches my $orig = $1; 3949dc139313SJoe Perches my $level = lc($orig); 3950dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 3951dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 3952dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 3953dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 3954dc139313SJoe Perches } 3955dc139313SJoe Perches 395691c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 395791c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 395891c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 395991c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 396091c9afafSAndy Lutomirski WARN("ENOSYS", 396191c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 396291c9afafSAndy Lutomirski } 396391c9afafSAndy Lutomirski 3964653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 3965653d4876SAndy Whitcroft# or if closed on same line 39662d453e3bSJoe Perches if ($^V && $^V ge 5.10.0 && 39672d453e3bSJoe Perches $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && 39682d453e3bSJoe Perches $sline !~ /\#\s*define\b.*do\s*\{/ && 39692d453e3bSJoe Perches $sline !~ /}/) { 39708d182478SJoe Perches if (ERROR("OPEN_BRACE", 39712d453e3bSJoe Perches "open brace '{' following function definitions go on the next line\n" . $herecurr) && 39728d182478SJoe Perches $fix) { 39738d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 39748d182478SJoe Perches my $fixed_line = $rawline; 39758d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 39768d182478SJoe Perches my $line1 = $1; 39778d182478SJoe Perches my $line2 = $2; 39788d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 39798d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 39808d182478SJoe Perches if ($line2 !~ /^\s*$/) { 39818d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 39828d182478SJoe Perches } 39838d182478SJoe Perches } 39840a920b5bSAndy Whitcroft } 3985653d4876SAndy Whitcroft 39868905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 39878905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 39888905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 39898d182478SJoe Perches if (ERROR("OPEN_BRACE", 39908d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 39918d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 39928d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 39938d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 39948d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 39958d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 39968d182478SJoe Perches $fixedline = $rawline; 39978d81ae05SCyril Bur $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 39988d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 39998d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40008d182478SJoe Perches } 40018d182478SJoe Perches } 40028905a67cSAndy Whitcroft } 40038905a67cSAndy Whitcroft 40040c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 40053705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 40063705ce5bSJoe Perches if (WARN("SPACING", 40073705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 40083705ce5bSJoe Perches $fix) { 4009194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40103705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 40113705ce5bSJoe Perches } 40120c73b4ebSAndy Whitcroft } 40130c73b4ebSAndy Whitcroft 401431070b5dSJoe Perches# Function pointer declarations 401531070b5dSJoe Perches# check spacing between type, funcptr, and args 401631070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 401791f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 401831070b5dSJoe Perches my $declare = $1; 401931070b5dSJoe Perches my $pre_pointer_space = $2; 402031070b5dSJoe Perches my $post_pointer_space = $3; 402131070b5dSJoe Perches my $funcname = $4; 402231070b5dSJoe Perches my $post_funcname_space = $5; 402331070b5dSJoe Perches my $pre_args_space = $6; 402431070b5dSJoe Perches 402591f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 402691f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 402791f72e9cSJoe Perches# don't need a space so don't warn for those. 402891f72e9cSJoe Perches my $post_declare_space = ""; 402991f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 403091f72e9cSJoe Perches $post_declare_space = $1; 403191f72e9cSJoe Perches $declare = rtrim($declare); 403291f72e9cSJoe Perches } 403391f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 403431070b5dSJoe Perches WARN("SPACING", 403531070b5dSJoe Perches "missing space after return type\n" . $herecurr); 403691f72e9cSJoe Perches $post_declare_space = " "; 403731070b5dSJoe Perches } 403831070b5dSJoe Perches 403931070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 404091f72e9cSJoe Perches# This test is not currently implemented because these declarations are 404191f72e9cSJoe Perches# equivalent to 404291f72e9cSJoe Perches# int foo(int bar, ...) 404391f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 404491f72e9cSJoe Perches# 404591f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 404691f72e9cSJoe Perches# WARN("SPACING", 404791f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 404891f72e9cSJoe Perches# } 404931070b5dSJoe Perches 405031070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 405131070b5dSJoe Perches if (defined $pre_pointer_space && 405231070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 405331070b5dSJoe Perches WARN("SPACING", 405431070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 405531070b5dSJoe Perches } 405631070b5dSJoe Perches 405731070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 405831070b5dSJoe Perches if (defined $post_pointer_space && 405931070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 406031070b5dSJoe Perches WARN("SPACING", 406131070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 406231070b5dSJoe Perches } 406331070b5dSJoe Perches 406431070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 406531070b5dSJoe Perches if (defined $post_funcname_space && 406631070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 406731070b5dSJoe Perches WARN("SPACING", 406831070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 406931070b5dSJoe Perches } 407031070b5dSJoe Perches 407131070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 407231070b5dSJoe Perches if (defined $pre_args_space && 407331070b5dSJoe Perches $pre_args_space =~ /^\s/) { 407431070b5dSJoe Perches WARN("SPACING", 407531070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 407631070b5dSJoe Perches } 407731070b5dSJoe Perches 407831070b5dSJoe Perches if (show_type("SPACING") && $fix) { 4079194f66fcSJoe Perches $fixed[$fixlinenr] =~ 408091f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 408131070b5dSJoe Perches } 408231070b5dSJoe Perches } 408331070b5dSJoe Perches 40848d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 40858d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 4086fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 4087fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 40888d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 40898d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 40908d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 4091fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 409238dca988SHeinrich Schuchardt $prefix !~ /[{,:]\s+$/) { 40933705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 40943705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 40953705ce5bSJoe Perches $fix) { 4096194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40973705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 40983705ce5bSJoe Perches } 40998d31cfceSAndy Whitcroft } 41008d31cfceSAndy Whitcroft } 41018d31cfceSAndy Whitcroft 4102f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 41036c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 4104c2fdda0dSAndy Whitcroft my $name = $1; 4105773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 4106773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 4107c2fdda0dSAndy Whitcroft 4108c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 4109773647a0SAndy Whitcroft if ($name =~ /^(?: 4110773647a0SAndy Whitcroft if|for|while|switch|return|case| 4111773647a0SAndy Whitcroft volatile|__volatile__| 4112773647a0SAndy Whitcroft __attribute__|format|__extension__| 4113773647a0SAndy Whitcroft asm|__asm__)$/x) 4114773647a0SAndy Whitcroft { 4115c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 4116c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 4117c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 4118c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 4119773647a0SAndy Whitcroft 4120773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 4121c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 4122c2fdda0dSAndy Whitcroft 4123c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 4124c2fdda0dSAndy Whitcroft # likely a typedef for a function. 4125773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 4126c2fdda0dSAndy Whitcroft 4127c2fdda0dSAndy Whitcroft } else { 41283705ce5bSJoe Perches if (WARN("SPACING", 41293705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 41303705ce5bSJoe Perches $fix) { 4131194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41323705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 41333705ce5bSJoe Perches } 4134f0a594c1SAndy Whitcroft } 41356c72ffaaSAndy Whitcroft } 41369a4cad4eSEric Nelson 4137653d4876SAndy Whitcroft# Check operator spacing. 41380a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 41393705ce5bSJoe Perches my $fixed_line = ""; 41403705ce5bSJoe Perches my $line_fixed = 0; 41413705ce5bSJoe Perches 41429c0ca6f9SAndy Whitcroft my $ops = qr{ 41439c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 41449c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 41459c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 41461f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 414784731623SJoe Perches \?:|\?|: 41489c0ca6f9SAndy Whitcroft }x; 4149cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 41503705ce5bSJoe Perches 41513705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 41523705ce5bSJoe Perches## foreach my $el (@elements) { 41533705ce5bSJoe Perches## print("el: <$el>\n"); 41543705ce5bSJoe Perches## } 41553705ce5bSJoe Perches 41563705ce5bSJoe Perches my @fix_elements = (); 415700df344fSAndy Whitcroft my $off = 0; 41586c72ffaaSAndy Whitcroft 41593705ce5bSJoe Perches foreach my $el (@elements) { 41603705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 41613705ce5bSJoe Perches $off += length($el); 41623705ce5bSJoe Perches } 41633705ce5bSJoe Perches 41643705ce5bSJoe Perches $off = 0; 41653705ce5bSJoe Perches 41666c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 4167b34c648bSJoe Perches my $last_after = -1; 41686c72ffaaSAndy Whitcroft 41690a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 41703705ce5bSJoe Perches 41713705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 41723705ce5bSJoe Perches 41733705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 41743705ce5bSJoe Perches 41754a0df2efSAndy Whitcroft $off += length($elements[$n]); 41764a0df2efSAndy Whitcroft 417725985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 4178773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 4179773647a0SAndy Whitcroft my $cc = ''; 4180773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 4181773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 4182773647a0SAndy Whitcroft } 4183773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 4184773647a0SAndy Whitcroft 41854a0df2efSAndy Whitcroft my $a = ''; 41864a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 41874a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 4188cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 41894a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 41904a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 4191773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 41924a0df2efSAndy Whitcroft 41930a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 41944a0df2efSAndy Whitcroft 41954a0df2efSAndy Whitcroft my $c = ''; 41960a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 41974a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 41984a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 4199cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 42004a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 42014a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 42028b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 42034a0df2efSAndy Whitcroft } else { 42044a0df2efSAndy Whitcroft $c = 'E'; 42050a920b5bSAndy Whitcroft } 42060a920b5bSAndy Whitcroft 42074a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 42084a0df2efSAndy Whitcroft 42094a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 42104a0df2efSAndy Whitcroft 42116c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 4212de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 42130a920b5bSAndy Whitcroft 421474048ed8SAndy Whitcroft # Pull out the value of this operator. 42156c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 42160a920b5bSAndy Whitcroft 42171f65f947SAndy Whitcroft # Get the full operator variant. 42181f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 42191f65f947SAndy Whitcroft 422013214adfSAndy Whitcroft # Ignore operators passed as parameters. 422113214adfSAndy Whitcroft if ($op_type ne 'V' && 4222d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 422313214adfSAndy Whitcroft 4224cf655043SAndy Whitcroft# # Ignore comments 4225cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 422613214adfSAndy Whitcroft 4227d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 422813214adfSAndy Whitcroft } elsif ($op eq ';') { 4229cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 4230cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 42313705ce5bSJoe Perches if (ERROR("SPACING", 42323705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 4233b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 42343705ce5bSJoe Perches $line_fixed = 1; 42353705ce5bSJoe Perches } 4236d8aaf121SAndy Whitcroft } 4237d8aaf121SAndy Whitcroft 4238d8aaf121SAndy Whitcroft # // is a comment 4239d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 42400a920b5bSAndy Whitcroft 4241b00e4814SJoe Perches # : when part of a bitfield 4242b00e4814SJoe Perches } elsif ($opv eq ':B') { 4243b00e4814SJoe Perches # skip the bitfield test for now 4244b00e4814SJoe Perches 42451f65f947SAndy Whitcroft # No spaces for: 42461f65f947SAndy Whitcroft # -> 4247b00e4814SJoe Perches } elsif ($op eq '->') { 42484a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 42493705ce5bSJoe Perches if (ERROR("SPACING", 42503705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4251b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 42523705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 42533705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 42543705ce5bSJoe Perches } 4255b34c648bSJoe Perches $line_fixed = 1; 42563705ce5bSJoe Perches } 42570a920b5bSAndy Whitcroft } 42580a920b5bSAndy Whitcroft 42592381097bSJoe Perches # , must not have a space before and must have a space on the right. 42600a920b5bSAndy Whitcroft } elsif ($op eq ',') { 42612381097bSJoe Perches my $rtrim_before = 0; 42622381097bSJoe Perches my $space_after = 0; 42632381097bSJoe Perches if ($ctx =~ /Wx./) { 42642381097bSJoe Perches if (ERROR("SPACING", 42652381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 42662381097bSJoe Perches $line_fixed = 1; 42672381097bSJoe Perches $rtrim_before = 1; 42682381097bSJoe Perches } 42692381097bSJoe Perches } 4270cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 42713705ce5bSJoe Perches if (ERROR("SPACING", 42723705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 42733705ce5bSJoe Perches $line_fixed = 1; 4274b34c648bSJoe Perches $last_after = $n; 42752381097bSJoe Perches $space_after = 1; 42762381097bSJoe Perches } 42772381097bSJoe Perches } 42782381097bSJoe Perches if ($rtrim_before || $space_after) { 42792381097bSJoe Perches if ($rtrim_before) { 42802381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 42812381097bSJoe Perches } else { 42822381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 42832381097bSJoe Perches } 42842381097bSJoe Perches if ($space_after) { 42852381097bSJoe Perches $good .= " "; 42863705ce5bSJoe Perches } 42870a920b5bSAndy Whitcroft } 42880a920b5bSAndy Whitcroft 42899c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 429074048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 42919c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 42929c0ca6f9SAndy Whitcroft 42939c0ca6f9SAndy Whitcroft # unary operators should have a space before and 42949c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 42959c0ca6f9SAndy Whitcroft # unary operator, or a cast 42969c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 429774048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 42980d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4299cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 43003705ce5bSJoe Perches if (ERROR("SPACING", 43013705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4302b34c648bSJoe Perches if ($n != $last_after + 2) { 4303b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 43043705ce5bSJoe Perches $line_fixed = 1; 43053705ce5bSJoe Perches } 43060a920b5bSAndy Whitcroft } 4307b34c648bSJoe Perches } 4308a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4309171ae1a4SAndy Whitcroft # A unary '*' may be const 4310171ae1a4SAndy Whitcroft 4311171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 43123705ce5bSJoe Perches if (ERROR("SPACING", 43133705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4314b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 43153705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 43163705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 43173705ce5bSJoe Perches } 4318b34c648bSJoe Perches $line_fixed = 1; 43193705ce5bSJoe Perches } 43200a920b5bSAndy Whitcroft } 43210a920b5bSAndy Whitcroft 43220a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 43230a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4324773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 43253705ce5bSJoe Perches if (ERROR("SPACING", 43263705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4327b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 43283705ce5bSJoe Perches $line_fixed = 1; 43293705ce5bSJoe Perches } 43300a920b5bSAndy Whitcroft } 4331773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4332773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 43333705ce5bSJoe Perches if (ERROR("SPACING", 43343705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4335b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 43363705ce5bSJoe Perches $line_fixed = 1; 43373705ce5bSJoe Perches } 4338653d4876SAndy Whitcroft } 4339773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 43403705ce5bSJoe Perches if (ERROR("SPACING", 43413705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4342b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 43433705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 43443705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4345773647a0SAndy Whitcroft } 4346b34c648bSJoe Perches $line_fixed = 1; 43473705ce5bSJoe Perches } 43483705ce5bSJoe Perches } 43490a920b5bSAndy Whitcroft 43500a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 43519c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 43529c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 43539c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4354c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4355c2fdda0dSAndy Whitcroft $op eq '%') 43560a920b5bSAndy Whitcroft { 4357d2e025f3SJoe Perches if ($check) { 4358d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4359d2e025f3SJoe Perches if (CHK("SPACING", 4360d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4361d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4362d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4363d2e025f3SJoe Perches $line_fixed = 1; 4364d2e025f3SJoe Perches } 4365d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4366d2e025f3SJoe Perches if (CHK("SPACING", 4367d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4368d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4369d2e025f3SJoe Perches $line_fixed = 1; 4370d2e025f3SJoe Perches } 4371d2e025f3SJoe Perches } 4372d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 43733705ce5bSJoe Perches if (ERROR("SPACING", 43743705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4375b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4376b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4377b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4378b34c648bSJoe Perches } 43793705ce5bSJoe Perches $line_fixed = 1; 43803705ce5bSJoe Perches } 43810a920b5bSAndy Whitcroft } 43820a920b5bSAndy Whitcroft 43831f65f947SAndy Whitcroft # A colon needs no spaces before when it is 43841f65f947SAndy Whitcroft # terminating a case value or a label. 43851f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 43861f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 43873705ce5bSJoe Perches if (ERROR("SPACING", 43883705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4389b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 43903705ce5bSJoe Perches $line_fixed = 1; 43913705ce5bSJoe Perches } 43921f65f947SAndy Whitcroft } 43931f65f947SAndy Whitcroft 43940a920b5bSAndy Whitcroft # All the others need spaces both sides. 4395cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 43961f65f947SAndy Whitcroft my $ok = 0; 43971f65f947SAndy Whitcroft 439822f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 43991f65f947SAndy Whitcroft if (($op eq '<' && 44001f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 44011f65f947SAndy Whitcroft ($op eq '>' && 44021f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 44031f65f947SAndy Whitcroft { 44041f65f947SAndy Whitcroft $ok = 1; 44051f65f947SAndy Whitcroft } 44061f65f947SAndy Whitcroft 4407e0df7e1fSJoe Perches # for asm volatile statements 4408e0df7e1fSJoe Perches # ignore a colon with another 4409e0df7e1fSJoe Perches # colon immediately before or after 4410e0df7e1fSJoe Perches if (($op eq ':') && 4411e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4412e0df7e1fSJoe Perches $ok = 1; 4413e0df7e1fSJoe Perches } 4414e0df7e1fSJoe Perches 441584731623SJoe Perches # messages are ERROR, but ?: are CHK 44161f65f947SAndy Whitcroft if ($ok == 0) { 44170675a8fbSJean Delvare my $msg_level = \&ERROR; 44180675a8fbSJean Delvare $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 441984731623SJoe Perches 44200675a8fbSJean Delvare if (&{$msg_level}("SPACING", 44213705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4422b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4423b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4424b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4425b34c648bSJoe Perches } 44263705ce5bSJoe Perches $line_fixed = 1; 44273705ce5bSJoe Perches } 44280a920b5bSAndy Whitcroft } 442922f2a2efSAndy Whitcroft } 44304a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 44313705ce5bSJoe Perches 44323705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 44333705ce5bSJoe Perches 44343705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 44350a920b5bSAndy Whitcroft } 44363705ce5bSJoe Perches 44373705ce5bSJoe Perches if (($#elements % 2) == 0) { 44383705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 44393705ce5bSJoe Perches } 44403705ce5bSJoe Perches 4441194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4442194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 44433705ce5bSJoe Perches } 44443705ce5bSJoe Perches 44453705ce5bSJoe Perches 44460a920b5bSAndy Whitcroft } 44470a920b5bSAndy Whitcroft 4448786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4449d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4450786b6326SJoe Perches if (WARN("SPACING", 4451786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4452786b6326SJoe Perches $fix) { 4453194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4454786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4455786b6326SJoe Perches } 4456786b6326SJoe Perches } 4457786b6326SJoe Perches 4458f0a594c1SAndy Whitcroft# check for multiple assignments 4459f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4460000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4461000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4462f0a594c1SAndy Whitcroft } 4463f0a594c1SAndy Whitcroft 446422f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 446522f2a2efSAndy Whitcroft## # continuation. 446622f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 446722f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 446822f2a2efSAndy Whitcroft## 446922f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 447022f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 447122f2a2efSAndy Whitcroft## my $ln = $line; 447222f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 447322f2a2efSAndy Whitcroft## } 447422f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4475000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4476000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 447722f2a2efSAndy Whitcroft## } 447822f2a2efSAndy Whitcroft## } 4479f0a594c1SAndy Whitcroft 44800a920b5bSAndy Whitcroft#need space before brace following if, while, etc 44816b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 44824e5d56bdSEddie Kovsky $line =~ /do\{/) { 44833705ce5bSJoe Perches if (ERROR("SPACING", 44843705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 44853705ce5bSJoe Perches $fix) { 44868d81ae05SCyril Bur $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/; 44873705ce5bSJoe Perches } 4488de7d4f0eSAndy Whitcroft } 4489de7d4f0eSAndy Whitcroft 4490c4a62ef9SJoe Perches## # check for blank lines before declarations 4491c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4492c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4493c4a62ef9SJoe Perches## WARN("SPACING", 4494c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4495c4a62ef9SJoe Perches## } 4496c4a62ef9SJoe Perches## 4497c4a62ef9SJoe Perches 4498de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4499de7d4f0eSAndy Whitcroft# on the line 4500de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 4501d5e616fcSJoe Perches if (ERROR("SPACING", 4502d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4503d5e616fcSJoe Perches $fix) { 4504194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4505d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4506d5e616fcSJoe Perches } 45070a920b5bSAndy Whitcroft } 45080a920b5bSAndy Whitcroft 450922f2a2efSAndy Whitcroft# check spacing on square brackets 451022f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 45113705ce5bSJoe Perches if (ERROR("SPACING", 45123705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 45133705ce5bSJoe Perches $fix) { 4514194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45153705ce5bSJoe Perches s/\[\s+/\[/; 45163705ce5bSJoe Perches } 451722f2a2efSAndy Whitcroft } 451822f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 45193705ce5bSJoe Perches if (ERROR("SPACING", 45203705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 45213705ce5bSJoe Perches $fix) { 4522194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45233705ce5bSJoe Perches s/\s+\]/\]/; 45243705ce5bSJoe Perches } 452522f2a2efSAndy Whitcroft } 452622f2a2efSAndy Whitcroft 4527c45dcabdSAndy Whitcroft# check spacing on parentheses 45289c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 45299c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 45303705ce5bSJoe Perches if (ERROR("SPACING", 45313705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 45323705ce5bSJoe Perches $fix) { 4533194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45343705ce5bSJoe Perches s/\(\s+/\(/; 45353705ce5bSJoe Perches } 453622f2a2efSAndy Whitcroft } 453713214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4538c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4539c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 45403705ce5bSJoe Perches if (ERROR("SPACING", 45413705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 45423705ce5bSJoe Perches $fix) { 4543194f66fcSJoe Perches $fixed[$fixlinenr] =~ 45443705ce5bSJoe Perches s/\s+\)/\)/; 45453705ce5bSJoe Perches } 454622f2a2efSAndy Whitcroft } 454722f2a2efSAndy Whitcroft 4548e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4549e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4550e2826fd0SJoe Perches 4551e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4552ea4acbb1SJoe Perches my $var = $1; 4553ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4554ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4555ea4acbb1SJoe Perches $fix) { 4556ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4557ea4acbb1SJoe Perches } 4558ea4acbb1SJoe Perches } 4559ea4acbb1SJoe Perches 4560ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4561ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4562ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4563ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4564ea4acbb1SJoe Perches my $var = $2; 4565ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4566ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4567ea4acbb1SJoe Perches $fix) { 4568ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4569ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4570ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4571ea4acbb1SJoe Perches } 4572e2826fd0SJoe Perches } 4573e2826fd0SJoe Perches 457463b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses 4575a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict 4576a032aa4cSJoe Perches if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && 4577a032aa4cSJoe Perches $^V && $^V ge 5.10.0 && defined($stat) && 457863b7c73eSJoe Perches $stat =~ /(^.\s*if\s*($balanced_parens))/) { 457963b7c73eSJoe Perches my $if_stat = $1; 458063b7c73eSJoe Perches my $test = substr($2, 1, -1); 458163b7c73eSJoe Perches my $herectx; 458263b7c73eSJoe Perches while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { 458363b7c73eSJoe Perches my $match = $1; 458463b7c73eSJoe Perches # avoid parentheses around potential macro args 458563b7c73eSJoe Perches next if ($match =~ /^\s*\w+\s*$/); 458663b7c73eSJoe Perches if (!defined($herectx)) { 458763b7c73eSJoe Perches $herectx = $here . "\n"; 458863b7c73eSJoe Perches my $cnt = statement_rawlines($if_stat); 458963b7c73eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 459063b7c73eSJoe Perches my $rl = raw_line($linenr, $n); 459163b7c73eSJoe Perches $herectx .= $rl . "\n"; 459263b7c73eSJoe Perches last if $rl =~ /^[ \+].*\{/; 459363b7c73eSJoe Perches } 459463b7c73eSJoe Perches } 459563b7c73eSJoe Perches CHK("UNNECESSARY_PARENTHESES", 459663b7c73eSJoe Perches "Unnecessary parentheses around '$match'\n" . $herectx); 459763b7c73eSJoe Perches } 459863b7c73eSJoe Perches } 459963b7c73eSJoe Perches 46000a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 46014a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 46020a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 46033705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 46043705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 46053705ce5bSJoe Perches $fix) { 4606194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46073705ce5bSJoe Perches s/^(.)\s+/$1/; 46083705ce5bSJoe Perches } 46090a920b5bSAndy Whitcroft } 46100a920b5bSAndy Whitcroft 46115b9553abSJoe Perches# return is not a function 4612507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4613c45dcabdSAndy Whitcroft my $spacing = $1; 4614507e5141SJoe Perches if ($^V && $^V ge 5.10.0 && 46155b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 46165b9553abSJoe Perches my $value = $1; 46175b9553abSJoe Perches $value = deparenthesize($value); 46185b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4619000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4620000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 46215b9553abSJoe Perches } 4622c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4623000d1cc1SJoe Perches ERROR("SPACING", 4624000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4625c45dcabdSAndy Whitcroft } 4626c45dcabdSAndy Whitcroft } 4627507e5141SJoe Perches 4628b43ae21bSJoe Perches# unnecessary return in a void function 4629b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4630b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4631b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4632b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4633b43ae21bSJoe Perches $linenr >= 3 && 4634b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4635b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 46369819cf25SJoe Perches WARN("RETURN_VOID", 4637b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 46389819cf25SJoe Perches } 46399819cf25SJoe Perches 4640189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 4641189248d8SJoe Perches if ($^V && $^V ge 5.10.0 && 4642189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4643189248d8SJoe Perches my $openparens = $1; 4644189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4645189248d8SJoe Perches my $msg = ""; 4646189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4647189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4648189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4649189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4650189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4651189248d8SJoe Perches } 4652189248d8SJoe Perches } 4653189248d8SJoe Perches 4654c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4655c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4656c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4657c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 4658c5595fa2SJoe Perches if ($^V && $^V ge 5.10.0 && 4659c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4660c5595fa2SJoe Perches my $lead = $1; 4661c5595fa2SJoe Perches my $const = $2; 4662c5595fa2SJoe Perches my $comp = $3; 4663c5595fa2SJoe Perches my $to = $4; 4664c5595fa2SJoe Perches my $newcomp = $comp; 4665f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4666c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4667c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4668c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4669c5595fa2SJoe Perches $fix) { 4670c5595fa2SJoe Perches if ($comp eq "<") { 4671c5595fa2SJoe Perches $newcomp = ">"; 4672c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4673c5595fa2SJoe Perches $newcomp = ">="; 4674c5595fa2SJoe Perches } elsif ($comp eq ">") { 4675c5595fa2SJoe Perches $newcomp = "<"; 4676c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4677c5595fa2SJoe Perches $newcomp = "<="; 4678c5595fa2SJoe Perches } 4679c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4680c5595fa2SJoe Perches } 4681c5595fa2SJoe Perches } 4682c5595fa2SJoe Perches 4683f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4684f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 468553a3c448SAndy Whitcroft my $name = $1; 468653a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4687000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4688f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 468953a3c448SAndy Whitcroft } 469053a3c448SAndy Whitcroft } 4691c45dcabdSAndy Whitcroft 46920a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 46934a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 46943705ce5bSJoe Perches if (ERROR("SPACING", 46953705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 46963705ce5bSJoe Perches $fix) { 4697194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46983705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 46993705ce5bSJoe Perches } 47000a920b5bSAndy Whitcroft } 47010a920b5bSAndy Whitcroft 4702f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4703f5fe35ddSAndy Whitcroft# statements after the conditional. 4704170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 47053e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 47063e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 47073e469cdcSAndy Whitcroft if (!defined $stat); 4708170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4709170d3a22SAndy Whitcroft $remain_next, $off_next); 4710170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4711170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4712170d3a22SAndy Whitcroft 4713170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4714170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4715170d3a22SAndy Whitcroft # then count those as offsets. 4716170d3a22SAndy Whitcroft my ($whitespace) = 4717170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4718170d3a22SAndy Whitcroft my $offset = 4719170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4720170d3a22SAndy Whitcroft 4721170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4722170d3a22SAndy Whitcroft $offset} = 1; 4723170d3a22SAndy Whitcroft } 4724170d3a22SAndy Whitcroft } 4725170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4726c11230f4SJoe Perches defined($stat) && defined($cond) && 4727170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4728171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 47298905a67cSAndy Whitcroft 4730b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4731000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4732000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 47338905a67cSAndy Whitcroft } 47348905a67cSAndy Whitcroft 47358905a67cSAndy Whitcroft # Find out what is on the end of the line after the 47368905a67cSAndy Whitcroft # conditional. 4737773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 47388905a67cSAndy Whitcroft $s =~ s/\n.*//g; 473913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 474053210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 474153210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4742773647a0SAndy Whitcroft { 4743bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4744bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4745bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 474642bdf74cSHidetoshi Seto my $stat_real = ''; 4747bb44ad39SAndy Whitcroft 474842bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 474942bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4750bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4751bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4752bb44ad39SAndy Whitcroft } 4753bb44ad39SAndy Whitcroft 4754000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4755000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 47568905a67cSAndy Whitcroft } 47578905a67cSAndy Whitcroft } 47588905a67cSAndy Whitcroft 475913214adfSAndy Whitcroft# Check for bitwise tests written as boolean 476013214adfSAndy Whitcroft if ($line =~ / 476113214adfSAndy Whitcroft (?: 476213214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 476313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 476413214adfSAndy Whitcroft (?:\&\&|\|\|) 476513214adfSAndy Whitcroft | 476613214adfSAndy Whitcroft (?:\&\&|\|\|) 476713214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 476813214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 476913214adfSAndy Whitcroft )/x) 477013214adfSAndy Whitcroft { 4771000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4772000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 477313214adfSAndy Whitcroft } 477413214adfSAndy Whitcroft 47758905a67cSAndy Whitcroft# if and else should not have general statements after it 477613214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 477713214adfSAndy Whitcroft my $s = $1; 477813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 477913214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4780000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4781000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 47820a920b5bSAndy Whitcroft } 478313214adfSAndy Whitcroft } 478439667782SAndy Whitcroft# if should not continue a brace 478539667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4786000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4787048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 478839667782SAndy Whitcroft $herecurr); 478939667782SAndy Whitcroft } 4790a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4791a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4792a1080bf8SAndy Whitcroft $line !~ /\G(?: 47933fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4794a1080bf8SAndy Whitcroft \s*return\s+ 4795a1080bf8SAndy Whitcroft )/xg) 4796a1080bf8SAndy Whitcroft { 4797000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4798000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4799a1080bf8SAndy Whitcroft } 48000a920b5bSAndy Whitcroft 48010a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 48020a920b5bSAndy Whitcroft # indent level to be relevant to each other. 48038b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 48040a920b5bSAndy Whitcroft $previndent == $indent) { 48058b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 48068b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 48078b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 48088b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 48098b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 48108b8856f4SJoe Perches my $fixedline = $prevrawline; 48118b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 48128b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 48138b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48148b8856f4SJoe Perches } 48158b8856f4SJoe Perches $fixedline = $rawline; 48168b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 48178b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48188b8856f4SJoe Perches } 48190a920b5bSAndy Whitcroft } 48200a920b5bSAndy Whitcroft 48218b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 4822c2fdda0dSAndy Whitcroft $previndent == $indent) { 4823c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 4824c2fdda0dSAndy Whitcroft 4825c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 4826c2fdda0dSAndy Whitcroft # conditional. 4827773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 4828c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 4829c2fdda0dSAndy Whitcroft 4830c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 48318b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 48328b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 48338b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 48348b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 48358b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 48368b8856f4SJoe Perches my $fixedline = $prevrawline; 48378b8856f4SJoe Perches my $trailing = $rawline; 48388b8856f4SJoe Perches $trailing =~ s/^\+//; 48398b8856f4SJoe Perches $trailing = trim($trailing); 48408b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 48418b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 48428b8856f4SJoe Perches } 4843c2fdda0dSAndy Whitcroft } 4844c2fdda0dSAndy Whitcroft } 4845c2fdda0dSAndy Whitcroft 484695e2c602SJoe Perches#Specific variable tests 4847323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 4848323c1260SJoe Perches my $var = $1; 484995e2c602SJoe Perches 485095e2c602SJoe Perches#gcc binary extension 485195e2c602SJoe Perches if ($var =~ /^$Binary$/) { 4852d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 4853d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 4854d5e616fcSJoe Perches $fix) { 4855d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 4856194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4857d5e616fcSJoe Perches s/\b$var\b/$hexval/; 4858d5e616fcSJoe Perches } 485995e2c602SJoe Perches } 486095e2c602SJoe Perches 486195e2c602SJoe Perches#CamelCase 4862807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 4863be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 486422735ce8SJoe Perches#Ignore Page<foo> variants 4865807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 486622735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4867f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && 4868f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 4869f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 48707e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 48717e781f67SJoe Perches my $word = $1; 48727e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4873d8b07710SJoe Perches if ($check) { 4874d8b07710SJoe Perches seed_camelcase_includes(); 4875d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 4876d8b07710SJoe Perches seed_camelcase_file($realfile); 4877d8b07710SJoe Perches $camelcase_file_seeded = 1; 4878d8b07710SJoe Perches } 4879d8b07710SJoe Perches } 48807e781f67SJoe Perches if (!defined $camelcase{$word}) { 48817e781f67SJoe Perches $camelcase{$word} = 1; 4882be79794bSJoe Perches CHK("CAMELCASE", 48837e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 48847e781f67SJoe Perches } 4885323c1260SJoe Perches } 4886323c1260SJoe Perches } 48873445686aSJoe Perches } 48880a920b5bSAndy Whitcroft 48890a920b5bSAndy Whitcroft#no spaces allowed after \ in define 4890d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 4891d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 4892d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 4893d5e616fcSJoe Perches $fix) { 4894194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 4895d5e616fcSJoe Perches } 48960a920b5bSAndy Whitcroft } 48970a920b5bSAndy Whitcroft 48980e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 48990e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 4900c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4901e09dec48SAndy Whitcroft my $file = "$1.h"; 4902e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 4903e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 4904e09dec48SAndy Whitcroft $realfile ne $checkfile && 49057840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 4906c45dcabdSAndy Whitcroft { 49070e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 49080e212e0aSFabian Frederick if ($asminclude > 0) { 4909e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 4910000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 4911000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4912e09dec48SAndy Whitcroft } else { 4913000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 4914000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4915e09dec48SAndy Whitcroft } 49160a920b5bSAndy Whitcroft } 49170a920b5bSAndy Whitcroft } 49180e212e0aSFabian Frederick } 49190a920b5bSAndy Whitcroft 4920653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 4921653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 4922cf655043SAndy Whitcroft# in a known good container 4923b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 4924b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 4925d8aaf121SAndy Whitcroft my $ln = $linenr; 4926d8aaf121SAndy Whitcroft my $cnt = $realcnt; 4927c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 4928c45dcabdSAndy Whitcroft my $ctx = ''; 492908a2843eSJoe Perches my $has_flow_statement = 0; 493008a2843eSJoe Perches my $has_arg_concat = 0; 4931c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 4932f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4933f74bd194SAndy Whitcroft $ctx = $dstat; 4934c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4935a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4936c45dcabdSAndy Whitcroft 493708a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 493862e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 493908a2843eSJoe Perches 4940f59b64bfSJoe Perches $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; 4941f59b64bfSJoe Perches my $define_args = $1; 4942f59b64bfSJoe Perches my $define_stmt = $dstat; 4943f59b64bfSJoe Perches my @def_args = (); 4944f59b64bfSJoe Perches 4945f59b64bfSJoe Perches if (defined $define_args && $define_args ne "") { 4946f59b64bfSJoe Perches $define_args = substr($define_args, 1, length($define_args) - 2); 4947f59b64bfSJoe Perches $define_args =~ s/\s*//g; 4948f59b64bfSJoe Perches @def_args = split(",", $define_args); 4949f59b64bfSJoe Perches } 4950f59b64bfSJoe Perches 4951292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 4952c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 4953c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 4954c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 4955c45dcabdSAndy Whitcroft 4956c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 4957bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 4958bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 49596b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 4960bf30d6edSAndy Whitcroft { 4961c45dcabdSAndy Whitcroft } 4962c45dcabdSAndy Whitcroft 4963e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 496433acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 496533acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 4966e45bab8eSAndy Whitcroft { 4967e45bab8eSAndy Whitcroft } 4968e45bab8eSAndy Whitcroft 496942e15293SJoe Perches # Make asm volatile uses seem like a generic function 497042e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 497142e15293SJoe Perches 4972c45dcabdSAndy Whitcroft my $exceptions = qr{ 4973c45dcabdSAndy Whitcroft $Declare| 4974c45dcabdSAndy Whitcroft module_param_named| 4975a0a0a7a9SKees Cook MODULE_PARM_DESC| 4976c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 4977c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 4978383099fdSAndy Whitcroft __typeof__\(| 497922fd2d3eSStefani Seibold union| 498022fd2d3eSStefani Seibold struct| 4981ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 49826b10df42SVladimir Zapolskiy ^\"|\"$| 49836b10df42SVladimir Zapolskiy ^\[ 4984c45dcabdSAndy Whitcroft }x; 49855eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 4986f59b64bfSJoe Perches 4987f59b64bfSJoe Perches $ctx =~ s/\n*$//; 4988f59b64bfSJoe Perches my $stmt_cnt = statement_rawlines($ctx); 4989e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $stmt_cnt, $here); 4990f59b64bfSJoe Perches 4991f74bd194SAndy Whitcroft if ($dstat ne '' && 4992f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 4993f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 49943cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 4995356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 4996f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 4997f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 4998e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 499972f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 5000f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 5001f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 5002f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 50034e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 5004f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 5005c45dcabdSAndy Whitcroft { 5006e795556aSJoe Perches if ($dstat =~ /^\s*if\b/) { 5007e795556aSJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5008e795556aSJoe Perches "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); 5009e795556aSJoe Perches } elsif ($dstat =~ /;/) { 5010f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 5011f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 5012f74bd194SAndy Whitcroft } else { 5013000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 5014388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 5015d8aaf121SAndy Whitcroft } 5016f59b64bfSJoe Perches 5017f59b64bfSJoe Perches } 50185207649bSJoe Perches 50195207649bSJoe Perches # Make $define_stmt single line, comment-free, etc 50205207649bSJoe Perches my @stmt_array = split('\n', $define_stmt); 50215207649bSJoe Perches my $first = 1; 50225207649bSJoe Perches $define_stmt = ""; 50235207649bSJoe Perches foreach my $l (@stmt_array) { 50245207649bSJoe Perches $l =~ s/\\$//; 50255207649bSJoe Perches if ($first) { 50265207649bSJoe Perches $define_stmt = $l; 50275207649bSJoe Perches $first = 0; 50285207649bSJoe Perches } elsif ($l =~ /^[\+ ]/) { 50295207649bSJoe Perches $define_stmt .= substr($l, 1); 50305207649bSJoe Perches } 50315207649bSJoe Perches } 50325207649bSJoe Perches $define_stmt =~ s/$;//g; 50335207649bSJoe Perches $define_stmt =~ s/\s+/ /g; 50345207649bSJoe Perches $define_stmt = trim($define_stmt); 50355207649bSJoe Perches 5036f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type') 5037f59b64bfSJoe Perches foreach my $arg (@def_args) { 5038f59b64bfSJoe Perches next if ($arg =~ /\.\.\./); 50399192d41aSJoe Perches next if ($arg =~ /^type$/i); 50407fe528a2SJoe Perches my $tmp_stmt = $define_stmt; 50417fe528a2SJoe Perches $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; 50427fe528a2SJoe Perches $tmp_stmt =~ s/\#+\s*$arg\b//g; 50437fe528a2SJoe Perches $tmp_stmt =~ s/\b$arg\s*\#\#//g; 5044*d41362edSJoe Perches my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; 5045f59b64bfSJoe Perches if ($use_cnt > 1) { 5046f59b64bfSJoe Perches CHK("MACRO_ARG_REUSE", 5047f59b64bfSJoe Perches "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); 5048f59b64bfSJoe Perches } 50499192d41aSJoe Perches# check if any macro arguments may have other precedence issues 50507fe528a2SJoe Perches if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && 50519192d41aSJoe Perches ((defined($1) && $1 ne ',') || 50529192d41aSJoe Perches (defined($2) && $2 ne ','))) { 50539192d41aSJoe Perches CHK("MACRO_ARG_PRECEDENCE", 50549192d41aSJoe Perches "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); 50559192d41aSJoe Perches } 50560a920b5bSAndy Whitcroft } 50575023d347SJoe Perches 505808a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 505908a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 506008a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 506108a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 5062e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 506308a2843eSJoe Perches 506408a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 506508a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 506608a2843eSJoe Perches } 506708a2843eSJoe Perches 5068481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 50695023d347SJoe Perches 50705023d347SJoe Perches } else { 50715023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 5072481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 5073481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 50745023d347SJoe Perches $line =~ /^\+.*\\$/) { 50755023d347SJoe Perches WARN("LINE_CONTINUATIONS", 50765023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 50775023d347SJoe Perches } 5078653d4876SAndy Whitcroft } 50790a920b5bSAndy Whitcroft 5080b13edf7fSJoe Perches# do {} while (0) macro tests: 5081b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 5082b13edf7fSJoe Perches# macro should not end with a semicolon 5083b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 5084b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 5085b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 5086b13edf7fSJoe Perches my $ln = $linenr; 5087b13edf7fSJoe Perches my $cnt = $realcnt; 5088b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 5089b13edf7fSJoe Perches my $ctx = ''; 5090b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 5091b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 5092b13edf7fSJoe Perches $ctx = $dstat; 5093b13edf7fSJoe Perches 5094b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 50951b36b201SJoe Perches $dstat =~ s/$;/ /g; 5096b13edf7fSJoe Perches 5097b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 5098b13edf7fSJoe Perches my $stmts = $2; 5099b13edf7fSJoe Perches my $semis = $3; 5100b13edf7fSJoe Perches 5101b13edf7fSJoe Perches $ctx =~ s/\n*$//; 5102b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 5103e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5104b13edf7fSJoe Perches 5105ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 5106ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 5107b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 5108b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 5109b13edf7fSJoe Perches } 5110b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 5111b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 5112b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 5113b13edf7fSJoe Perches } 5114f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 5115f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 5116f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 5117e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5118f5ef95b1SJoe Perches 5119f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 5120f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 5121b13edf7fSJoe Perches } 5122b13edf7fSJoe Perches } 5123b13edf7fSJoe Perches 5124080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 5125080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 5126080ba929SMike Frysinger# . 5127080ba929SMike Frysinger# ALIGN(...) 5128080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 5129080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 5130000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 5131000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 5132080ba929SMike Frysinger } 5133080ba929SMike Frysinger 5134f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 513513214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 513613214adfSAndy Whitcroft my ($level, $endln, @chunks) = 5137cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 513813214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 5139cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 5140cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 5141aad4f614SJoe Perches my @allowed = (); 5142aad4f614SJoe Perches my $allow = 0; 514313214adfSAndy Whitcroft my $seen = 0; 5144773647a0SAndy Whitcroft my $herectx = $here . "\n"; 5145cf655043SAndy Whitcroft my $ln = $linenr - 1; 514613214adfSAndy Whitcroft for my $chunk (@chunks) { 514713214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 514813214adfSAndy Whitcroft 5149773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 5150773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 5151773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 5152773647a0SAndy Whitcroft 5153aad4f614SJoe Perches $allowed[$allow] = 0; 5154773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 5155773647a0SAndy Whitcroft 5156773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 5157773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 5158773647a0SAndy Whitcroft 5159773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 5160cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 5161cf655043SAndy Whitcroft 5162773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 516313214adfSAndy Whitcroft 516413214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 516513214adfSAndy Whitcroft 5166aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 5167cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5168cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5169aad4f614SJoe Perches $allowed[$allow] = 1; 517013214adfSAndy Whitcroft } 517113214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5172cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5173aad4f614SJoe Perches $allowed[$allow] = 1; 517413214adfSAndy Whitcroft } 5175cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5176cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5177aad4f614SJoe Perches $allowed[$allow] = 1; 517813214adfSAndy Whitcroft } 5179aad4f614SJoe Perches $allow++; 518013214adfSAndy Whitcroft } 5181aad4f614SJoe Perches if ($seen) { 5182aad4f614SJoe Perches my $sum_allowed = 0; 5183aad4f614SJoe Perches foreach (@allowed) { 5184aad4f614SJoe Perches $sum_allowed += $_; 5185aad4f614SJoe Perches } 5186aad4f614SJoe Perches if ($sum_allowed == 0) { 5187000d1cc1SJoe Perches WARN("BRACES", 5188000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 5189aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 5190aad4f614SJoe Perches $seen != $allow) { 5191aad4f614SJoe Perches CHK("BRACES", 5192aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 5193aad4f614SJoe Perches } 519413214adfSAndy Whitcroft } 519513214adfSAndy Whitcroft } 519613214adfSAndy Whitcroft } 5197773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 519813214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 5199cf655043SAndy Whitcroft my $allowed = 0; 5200f0a594c1SAndy Whitcroft 5201cf655043SAndy Whitcroft # Check the pre-context. 5202cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 5203cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 5204cf655043SAndy Whitcroft $allowed = 1; 5205f0a594c1SAndy Whitcroft } 5206773647a0SAndy Whitcroft 5207773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 5208773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 5209773647a0SAndy Whitcroft 5210cf655043SAndy Whitcroft # Check the condition. 5211cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 5212773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 5213cf655043SAndy Whitcroft if (defined $cond) { 5214773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5215cf655043SAndy Whitcroft } 5216cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 5217cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 5218cf655043SAndy Whitcroft $allowed = 1; 5219cf655043SAndy Whitcroft } 5220cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 5221cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 5222cf655043SAndy Whitcroft $allowed = 1; 5223cf655043SAndy Whitcroft } 5224cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 5225cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 5226cf655043SAndy Whitcroft $allowed = 1; 5227cf655043SAndy Whitcroft } 5228cf655043SAndy Whitcroft # Check the post-context. 5229cf655043SAndy Whitcroft if (defined $chunks[1]) { 5230cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 5231cf655043SAndy Whitcroft if (defined $cond) { 5232773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 5233cf655043SAndy Whitcroft } 5234cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 5235cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 5236cf655043SAndy Whitcroft $allowed = 1; 5237cf655043SAndy Whitcroft } 5238cf655043SAndy Whitcroft } 5239cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 5240f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 5241e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 5242cf655043SAndy Whitcroft 5243000d1cc1SJoe Perches WARN("BRACES", 5244000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 5245f0a594c1SAndy Whitcroft } 5246f0a594c1SAndy Whitcroft } 5247f0a594c1SAndy Whitcroft 5248e4c5babdSJoe Perches# check for single line unbalanced braces 524995330473SSven Eckelmann if ($sline =~ /^.\s*\}\s*else\s*$/ || 525095330473SSven Eckelmann $sline =~ /^.\s*else\s*\{\s*$/) { 5251e4c5babdSJoe Perches CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); 5252e4c5babdSJoe Perches } 5253e4c5babdSJoe Perches 52540979ae66SJoe Perches# check for unnecessary blank lines around braces 525577b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 5256f8e58219SJoe Perches if (CHK("BRACES", 5257f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 5258f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 5259f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5260f8e58219SJoe Perches } 52610979ae66SJoe Perches } 526277b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 5263f8e58219SJoe Perches if (CHK("BRACES", 5264f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 5265f8e58219SJoe Perches $fix) { 5266f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 5267f8e58219SJoe Perches } 52680979ae66SJoe Perches } 52690979ae66SJoe Perches 52704a0df2efSAndy Whitcroft# no volatiles please 52716c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 52726c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 5273000d1cc1SJoe Perches WARN("VOLATILE", 52748c27ceffSMauro Carvalho Chehab "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); 52754a0df2efSAndy Whitcroft } 52764a0df2efSAndy Whitcroft 52775e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 52785e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 52795e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 52805e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 528133acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 52825e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 52835e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 52845e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 52855e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 52865e4f6ba5SJoe Perches $fix && 52875e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 52885e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 52895e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 52905e4f6ba5SJoe Perches my $comma_close = ""; 52915e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 52925e4f6ba5SJoe Perches $comma_close = $1; 52935e4f6ba5SJoe Perches } 52945e4f6ba5SJoe Perches 52955e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 52965e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 52975e4f6ba5SJoe Perches my $fixedline = $prevrawline; 52985e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 52995e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 53005e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 53015e4f6ba5SJoe Perches $fixedline = $rawline; 53025e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 53035e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 53045e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 53055e4f6ba5SJoe Perches } 53065e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 53075e4f6ba5SJoe Perches } 53085e4f6ba5SJoe Perches } 53095e4f6ba5SJoe Perches 53105e4f6ba5SJoe Perches# check for missing a space in a string concatenation 53115e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 53125e4f6ba5SJoe Perches WARN('MISSING_SPACE', 53135e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 53145e4f6ba5SJoe Perches } 53155e4f6ba5SJoe Perches 531677cb8546SJoe Perches# check for an embedded function name in a string when the function is known 5317e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch 5318e4b7d309SJoe Perches# context providing the function name or a single line form for in-file 5319e4b7d309SJoe Perches# function declarations 532077cb8546SJoe Perches if ($line =~ /^\+.*$String/ && 532177cb8546SJoe Perches defined($context_function) && 5322e4b7d309SJoe Perches get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && 5323e4b7d309SJoe Perches length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { 532477cb8546SJoe Perches WARN("EMBEDDED_FUNCTION_NAME", 5325e4b7d309SJoe Perches "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); 532677cb8546SJoe Perches } 532777cb8546SJoe Perches 53285e4f6ba5SJoe Perches# check for spaces before a quoted newline 53295e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 53305e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 53315e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 53325e4f6ba5SJoe Perches $fix) { 53335e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 53345e4f6ba5SJoe Perches } 53355e4f6ba5SJoe Perches 53365e4f6ba5SJoe Perches } 53375e4f6ba5SJoe Perches 5338f17dba4fSJoe Perches# concatenated string without spaces between elements 533933acb54aSJoe Perches if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) { 5340f17dba4fSJoe Perches CHK("CONCATENATED_STRING", 5341f17dba4fSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr); 5342f17dba4fSJoe Perches } 5343f17dba4fSJoe Perches 534490ad30e5SJoe Perches# uncoalesced string fragments 534533acb54aSJoe Perches if ($line =~ /$String\s*"/) { 534690ad30e5SJoe Perches WARN("STRING_FRAGMENTS", 534790ad30e5SJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr); 534890ad30e5SJoe Perches } 534990ad30e5SJoe Perches 5350522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats 5351522b837cSAlexey Dobriyan my $show_L = 1; #don't show the same defect twice 5352522b837cSAlexey Dobriyan my $show_Z = 1; 53535e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 5354522b837cSAlexey Dobriyan my $string = substr($rawline, $-[1], $+[1] - $-[1]); 53555e4f6ba5SJoe Perches $string =~ s/%%/__/g; 5356522b837cSAlexey Dobriyan # check for %L 5357522b837cSAlexey Dobriyan if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { 53585e4f6ba5SJoe Perches WARN("PRINTF_L", 5359522b837cSAlexey Dobriyan "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); 5360522b837cSAlexey Dobriyan $show_L = 0; 53615e4f6ba5SJoe Perches } 5362522b837cSAlexey Dobriyan # check for %Z 5363522b837cSAlexey Dobriyan if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { 5364522b837cSAlexey Dobriyan WARN("PRINTF_Z", 5365522b837cSAlexey Dobriyan "%Z$1 is non-standard C, use %z$1\n" . $herecurr); 5366522b837cSAlexey Dobriyan $show_Z = 0; 5367522b837cSAlexey Dobriyan } 5368522b837cSAlexey Dobriyan # check for 0x<decimal> 5369522b837cSAlexey Dobriyan if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { 5370522b837cSAlexey Dobriyan ERROR("PRINTF_0XDECIMAL", 53716e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 53726e300757SJoe Perches } 53735e4f6ba5SJoe Perches } 53745e4f6ba5SJoe Perches 53755e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 53763f7f335dSJoe Perches if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { 53775e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 53785e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 53795e4f6ba5SJoe Perches } 53805e4f6ba5SJoe Perches 538100df344fSAndy Whitcroft# warn about #if 0 5382c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 5383000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 5384000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 5385de7d4f0eSAndy Whitcroft $herecurr); 53864a0df2efSAndy Whitcroft } 53874a0df2efSAndy Whitcroft 538803df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 538903df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5390100425deSJoe Perches my $tested = quotemeta($1); 5391100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5392100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5393100425deSJoe Perches my $func = $1; 5394100425deSJoe Perches if (WARN('NEEDLESS_IF', 5395100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5396100425deSJoe Perches $fix) { 5397100425deSJoe Perches my $do_fix = 1; 5398100425deSJoe Perches my $leading_tabs = ""; 5399100425deSJoe Perches my $new_leading_tabs = ""; 5400100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5401100425deSJoe Perches $leading_tabs = $1; 5402100425deSJoe Perches } else { 5403100425deSJoe Perches $do_fix = 0; 5404100425deSJoe Perches } 5405100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5406100425deSJoe Perches $new_leading_tabs = $1; 5407100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5408100425deSJoe Perches $do_fix = 0; 5409100425deSJoe Perches } 5410100425deSJoe Perches } else { 5411100425deSJoe Perches $do_fix = 0; 5412100425deSJoe Perches } 5413100425deSJoe Perches if ($do_fix) { 5414100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5415100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5416100425deSJoe Perches } 5417100425deSJoe Perches } 54184c432a8fSGreg Kroah-Hartman } 54194c432a8fSGreg Kroah-Hartman } 5420f0a594c1SAndy Whitcroft 5421ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5422ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5423ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5424ebfdc409SJoe Perches (defined $1 || defined $3) && 5425ebfdc409SJoe Perches $linenr > 3) { 5426ebfdc409SJoe Perches my $testval = $2; 5427ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5428ebfdc409SJoe Perches 5429ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5430ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5431ebfdc409SJoe Perches 5432fb0d0e08SJoe Perches if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) { 5433ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5434ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5435ebfdc409SJoe Perches } 5436ebfdc409SJoe Perches } 5437ebfdc409SJoe Perches 5438f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5439dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5440f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5441f78d98f6SJoe Perches my $level = $1; 5442f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5443f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5444f78d98f6SJoe Perches $fix) { 5445f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5446f78d98f6SJoe Perches } 5447f78d98f6SJoe Perches } 5448f78d98f6SJoe Perches 544945c55e92SJoe Perches# check for logging continuations 545045c55e92SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { 545145c55e92SJoe Perches WARN("LOGGING_CONTINUATION", 545245c55e92SJoe Perches "Avoid logging continuation uses where feasible\n" . $herecurr); 545345c55e92SJoe Perches } 545445c55e92SJoe Perches 5455abb08a53SJoe Perches# check for mask then right shift without a parentheses 5456abb08a53SJoe Perches if ($^V && $^V ge 5.10.0 && 5457abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5458abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5459abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5460abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5461abb08a53SJoe Perches } 5462abb08a53SJoe Perches 5463b75ac618SJoe Perches# check for pointer comparisons to NULL 5464b75ac618SJoe Perches if ($^V && $^V ge 5.10.0) { 5465b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5466b75ac618SJoe Perches my $val = $1; 5467b75ac618SJoe Perches my $equal = "!"; 5468b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5469b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5470b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5471b75ac618SJoe Perches $fix) { 5472b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5473b75ac618SJoe Perches } 5474b75ac618SJoe Perches } 5475b75ac618SJoe Perches } 5476b75ac618SJoe Perches 54778716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 54788716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 54798716de38SJoe Perches my $attr = $1; 54808716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 54818716de38SJoe Perches my $ptr = $1; 54828716de38SJoe Perches my $var = $2; 54838716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 54848716de38SJoe Perches ERROR("MISPLACED_INIT", 54858716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 54868716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 54878716de38SJoe Perches WARN("MISPLACED_INIT", 54888716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 54898716de38SJoe Perches $fix) { 5490194f66fcSJoe 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; 54918716de38SJoe Perches } 54928716de38SJoe Perches } 54938716de38SJoe Perches } 54948716de38SJoe Perches 5495e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5496e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5497e970b884SJoe Perches my $attr = $1; 5498e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5499e970b884SJoe Perches my $attr_prefix = $1; 5500e970b884SJoe Perches my $attr_type = $2; 5501e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5502e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5503e970b884SJoe Perches $fix) { 5504194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5505e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5506e970b884SJoe Perches } 5507e970b884SJoe Perches } 5508e970b884SJoe Perches 5509e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5510e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5511e970b884SJoe Perches my $attr = $1; 5512e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5513e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5514e970b884SJoe Perches $fix) { 5515194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5516e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5517e970b884SJoe Perches $lead = rtrim($1); 5518e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5519e970b884SJoe Perches $lead = "${lead}const "; 5520194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5521e970b884SJoe Perches } 5522e970b884SJoe Perches } 5523e970b884SJoe Perches 5524c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5525c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5526c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5527c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5528c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5529c17893c7SJoe Perches $fix) { 5530c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5531c17893c7SJoe Perches } 5532c17893c7SJoe Perches } 5533c17893c7SJoe Perches 5534fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5535fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5536fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5537fbdb8138SJoe Perches my $constant_func = $1; 5538fbdb8138SJoe Perches my $func = $constant_func; 5539fbdb8138SJoe Perches $func =~ s/^__constant_//; 5540fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5541fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5542fbdb8138SJoe Perches $fix) { 5543194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5544fbdb8138SJoe Perches } 5545fbdb8138SJoe Perches } 5546fbdb8138SJoe Perches 55471a15a250SPatrick Pannuto# prefer usleep_range over udelay 554837581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 554943c1d77cSJoe Perches my $delay = $1; 55501a15a250SPatrick Pannuto # ignore udelay's < 10, however 555143c1d77cSJoe Perches if (! ($delay < 10) ) { 5552000d1cc1SJoe Perches CHK("USLEEP_RANGE", 555343c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 555443c1d77cSJoe Perches } 555543c1d77cSJoe Perches if ($delay > 2000) { 555643c1d77cSJoe Perches WARN("LONG_UDELAY", 555743c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 55581a15a250SPatrick Pannuto } 55591a15a250SPatrick Pannuto } 55601a15a250SPatrick Pannuto 556109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 556209ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 556309ef8725SPatrick Pannuto if ($1 < 20) { 5564000d1cc1SJoe Perches WARN("MSLEEP", 556543c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 556609ef8725SPatrick Pannuto } 556709ef8725SPatrick Pannuto } 556809ef8725SPatrick Pannuto 556936ec1939SJoe Perches# check for comparisons of jiffies 557036ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 557136ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 557236ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 557336ec1939SJoe Perches } 557436ec1939SJoe Perches 55759d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 55769d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 55779d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 55789d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 55799d7a34a5SJoe Perches } 55809d7a34a5SJoe Perches 558100df344fSAndy Whitcroft# warn about #ifdefs in C files 5582c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 558300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 558400df344fSAndy Whitcroft# print "$herecurr"; 558500df344fSAndy Whitcroft# $clean = 0; 558600df344fSAndy Whitcroft# } 558700df344fSAndy Whitcroft 558822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5589c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 55903705ce5bSJoe Perches if (ERROR("SPACING", 55913705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 55923705ce5bSJoe Perches $fix) { 5593194f66fcSJoe Perches $fixed[$fixlinenr] =~ 55943705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 55953705ce5bSJoe Perches } 55963705ce5bSJoe Perches 559722f2a2efSAndy Whitcroft } 559822f2a2efSAndy Whitcroft 55994a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5600171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5601171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 56024a0df2efSAndy Whitcroft my $which = $1; 56034a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5604000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5605000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 56064a0df2efSAndy Whitcroft } 56074a0df2efSAndy Whitcroft } 56084a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5609402c2553SMichael S. Tsirkin 5610402c2553SMichael S. Tsirkin my $barriers = qr{ 5611402c2553SMichael S. Tsirkin mb| 5612402c2553SMichael S. Tsirkin rmb| 5613402c2553SMichael S. Tsirkin wmb| 5614402c2553SMichael S. Tsirkin read_barrier_depends 5615402c2553SMichael S. Tsirkin }x; 5616402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5617402c2553SMichael S. Tsirkin mb__before_atomic| 5618402c2553SMichael S. Tsirkin mb__after_atomic| 5619402c2553SMichael S. Tsirkin store_release| 5620402c2553SMichael S. Tsirkin load_acquire| 5621402c2553SMichael S. Tsirkin store_mb| 5622402c2553SMichael S. Tsirkin (?:$barriers) 5623402c2553SMichael S. Tsirkin }x; 5624402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5625402c2553SMichael S. Tsirkin (?:$barriers)| 562643e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 562743e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5628402c2553SMichael S. Tsirkin }x; 5629402c2553SMichael S. Tsirkin 5630402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 56314a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5632c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5633000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 56344a0df2efSAndy Whitcroft } 56354a0df2efSAndy Whitcroft } 56363ad81779SPaul E. McKenney 5637f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5638f4073b0fSMichael S. Tsirkin 5639f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5640f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5641f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5642f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5643f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5644f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5645f4073b0fSMichael S. Tsirkin } 5646f4073b0fSMichael S. Tsirkin 5647cb426e99SJoe Perches# check for waitqueue_active without a comment. 5648cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5649cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5650cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5651cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5652cb426e99SJoe Perches } 5653cb426e99SJoe Perches } 56543ad81779SPaul E. McKenney 565591db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends 565691db2592SPaul E. McKenney if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { 565791db2592SPaul E. McKenney WARN("READ_BARRIER_DEPENDS", 565891db2592SPaul E. McKenney "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); 565991db2592SPaul E. McKenney } 566091db2592SPaul E. McKenney 56614a0df2efSAndy Whitcroft# check of hardware specific defines 5662c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5663000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5664000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 56650a920b5bSAndy Whitcroft } 5666653d4876SAndy Whitcroft 5667596ed45bSJoe Perches# check that the storage class is not after a type 5668596ed45bSJoe Perches if ($line =~ /\b($Type)\s+($Storage)\b/) { 5669000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5670596ed45bSJoe Perches "storage class '$2' should be located before type '$1'\n" . $herecurr); 5671596ed45bSJoe Perches } 5672596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration 5673596ed45bSJoe Perches if ($line =~ /\b$Storage\b/ && 5674596ed45bSJoe Perches $line !~ /^.\s*$Storage/ && 5675596ed45bSJoe Perches $line =~ /^.\s*(.+?)\$Storage\s/ && 5676596ed45bSJoe Perches $1 !~ /[\,\)]\s*$/) { 5677596ed45bSJoe Perches WARN("STORAGE_CLASS", 5678596ed45bSJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr); 5679d4977c78STobias Klauser } 5680d4977c78STobias Klauser 5681de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5682de7d4f0eSAndy Whitcroft# storage class and type. 56839c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 56849c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5685000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5686000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5687de7d4f0eSAndy Whitcroft } 5688de7d4f0eSAndy Whitcroft 56898905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 56902b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 56912b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5692d5e616fcSJoe Perches if (WARN("INLINE", 5693d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5694d5e616fcSJoe Perches $fix) { 5695194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5696d5e616fcSJoe Perches 5697d5e616fcSJoe Perches } 56988905a67cSAndy Whitcroft } 56998905a67cSAndy Whitcroft 57003d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 57012b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57022b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5703000d1cc1SJoe Perches WARN("PREFER_PACKED", 5704000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 57053d130fd0SJoe Perches } 57063d130fd0SJoe Perches 570739b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 57082b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57092b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5710000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5711000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 571239b7e287SJoe Perches } 571339b7e287SJoe Perches 57145f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 57152b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57162b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5717d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5718d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5719d5e616fcSJoe Perches $fix) { 5720194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5721d5e616fcSJoe Perches 5722d5e616fcSJoe Perches } 57235f14d3bdSJoe Perches } 57245f14d3bdSJoe Perches 57256061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 57262b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 57272b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5728d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5729d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5730d5e616fcSJoe Perches $fix) { 5731194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5732d5e616fcSJoe Perches } 57336061d949SJoe Perches } 57346061d949SJoe Perches 5735619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 5736619a908aSJoe Perches if ($^V && $^V ge 5.10.0 && 5737619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5738619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5739619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5740619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5741619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5742619a908aSJoe Perches } 5743619a908aSJoe Perches 5744fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/ 5745e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5746fd39f904STomas Winkler $realfile !~ m@\btools/@ && 5747e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5748e6176fa4SJoe Perches my $type = $1; 5749e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5750e6176fa4SJoe Perches $type = $1; 5751e6176fa4SJoe Perches my $kernel_type = 'u'; 5752e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5753e6176fa4SJoe Perches $type =~ /(\d+)/; 5754e6176fa4SJoe Perches $kernel_type .= $1; 5755e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5756e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5757e6176fa4SJoe Perches $fix) { 5758e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5759e6176fa4SJoe Perches } 5760e6176fa4SJoe Perches } 5761e6176fa4SJoe Perches } 5762e6176fa4SJoe Perches 5763938224b5SJoe Perches# check for cast of C90 native int or longer types constants 5764938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 5765938224b5SJoe Perches my $cast = $1; 5766938224b5SJoe Perches my $const = $2; 5767938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 5768938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 5769938224b5SJoe Perches $fix) { 5770938224b5SJoe Perches my $suffix = ""; 5771938224b5SJoe Perches my $newconst = $const; 5772938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 5773938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 5774938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 5775938224b5SJoe Perches $suffix .= 'LL'; 5776938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 5777938224b5SJoe Perches $suffix .= 'L'; 5778938224b5SJoe Perches } 5779938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 5780938224b5SJoe Perches } 5781938224b5SJoe Perches } 5782938224b5SJoe Perches 57838f53a9b8SJoe Perches# check for sizeof(&) 57848f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5785000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5786000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 57878f53a9b8SJoe Perches } 57888f53a9b8SJoe Perches 578966c80b60SJoe Perches# check for sizeof without parenthesis 579066c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 5791d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 5792d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 5793d5e616fcSJoe Perches $fix) { 5794194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 5795d5e616fcSJoe Perches } 579666c80b60SJoe Perches } 579766c80b60SJoe Perches 579888982feaSJoe Perches# check for struct spinlock declarations 579988982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 580088982feaSJoe Perches WARN("USE_SPINLOCK_T", 580188982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 580288982feaSJoe Perches } 580388982feaSJoe Perches 5804a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 580506668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 5806a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 5807caac1d5fSHeba Aamer $fmt =~ s/%%//g; 5808caac1d5fSHeba Aamer if ($fmt !~ /%/) { 5809d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 5810d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 5811d5e616fcSJoe Perches $fix) { 5812194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 5813d5e616fcSJoe Perches } 5814a6962d72SJoe Perches } 5815a6962d72SJoe Perches } 5816a6962d72SJoe Perches 58170b523769SJoe Perches# check for vsprintf extension %p<foo> misuses 58180b523769SJoe Perches if ($^V && $^V ge 5.10.0 && 58190b523769SJoe Perches defined $stat && 58200b523769SJoe Perches $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && 58210b523769SJoe Perches $1 !~ /^_*volatile_*$/) { 5822e3c6bc95STobin C. Harding my $specifier; 5823e3c6bc95STobin C. Harding my $extension; 5824e3c6bc95STobin C. Harding my $bad_specifier = ""; 5825e3c6bc95STobin C. Harding my $stat_real; 5826e3c6bc95STobin C. Harding 58270b523769SJoe Perches my $lc = $stat =~ tr@\n@@; 58280b523769SJoe Perches $lc = $lc + $linenr; 58290b523769SJoe Perches for (my $count = $linenr; $count <= $lc; $count++) { 58300b523769SJoe Perches my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); 58310b523769SJoe Perches $fmt =~ s/%%//g; 5832e3c6bc95STobin C. Harding 5833e3c6bc95STobin C. Harding while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) { 5834e3c6bc95STobin C. Harding $specifier = $1; 5835e3c6bc95STobin C. Harding $extension = $2; 5836e3c6bc95STobin C. Harding if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) { 5837e3c6bc95STobin C. Harding $bad_specifier = $specifier; 58380b523769SJoe Perches last; 58390b523769SJoe Perches } 5840e3c6bc95STobin C. Harding if ($extension eq "x" && !defined($stat_real)) { 5841e3c6bc95STobin C. Harding if (!defined($stat_real)) { 5842e3c6bc95STobin C. Harding $stat_real = get_stat_real($linenr, $lc); 58430b523769SJoe Perches } 5844e3c6bc95STobin C. Harding WARN("VSPRINTF_SPECIFIER_PX", 5845e3c6bc95STobin 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"); 5846e3c6bc95STobin C. Harding } 5847e3c6bc95STobin C. Harding } 5848e3c6bc95STobin C. Harding if ($bad_specifier ne "") { 58492a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 58501df7338aSSergey Senozhatsky my $ext_type = "Invalid"; 58511df7338aSSergey Senozhatsky my $use = ""; 5852e3c6bc95STobin C. Harding if ($bad_specifier =~ /p[Ff]/) { 58531df7338aSSergey Senozhatsky $ext_type = "Deprecated"; 58541df7338aSSergey Senozhatsky $use = " - use %pS instead"; 5855e3c6bc95STobin C. Harding $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); 58561df7338aSSergey Senozhatsky } 58572a9f9d85STobin C. Harding 58580b523769SJoe Perches WARN("VSPRINTF_POINTER_EXTENSION", 5859e3c6bc95STobin C. Harding "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); 5860e3c6bc95STobin C. Harding } 58610b523769SJoe Perches } 58620b523769SJoe Perches } 58630b523769SJoe Perches 5864554e165cSAndy Whitcroft# Check for misused memsets 5865d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5866d1fe9c09SJoe Perches defined $stat && 58679e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 5868554e165cSAndy Whitcroft 5869d7c76ba7SJoe Perches my $ms_addr = $2; 5870d1fe9c09SJoe Perches my $ms_val = $7; 5871d1fe9c09SJoe Perches my $ms_size = $12; 5872d7c76ba7SJoe Perches 5873554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 5874554e165cSAndy Whitcroft ERROR("MEMSET", 5875d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 5876554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 5877554e165cSAndy Whitcroft WARN("MEMSET", 5878d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 5879d7c76ba7SJoe Perches } 5880d7c76ba7SJoe Perches } 5881d7c76ba7SJoe Perches 588298a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 5883f333195dSJoe Perches# if ($^V && $^V ge 5.10.0 && 5884f333195dSJoe Perches# defined $stat && 5885f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5886f333195dSJoe Perches# if (WARN("PREFER_ETHER_ADDR_COPY", 5887f333195dSJoe Perches# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 5888f333195dSJoe Perches# $fix) { 5889f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 5890f333195dSJoe Perches# } 5891f333195dSJoe Perches# } 589298a9bba5SJoe Perches 5893b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 5894f333195dSJoe Perches# if ($^V && $^V ge 5.10.0 && 5895f333195dSJoe Perches# defined $stat && 5896f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5897f333195dSJoe Perches# WARN("PREFER_ETHER_ADDR_EQUAL", 5898f333195dSJoe Perches# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 5899f333195dSJoe Perches# } 5900b6117d17SMateusz Kulikowski 59018617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 59028617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 5903f333195dSJoe Perches# if ($^V && $^V ge 5.10.0 && 5904f333195dSJoe Perches# defined $stat && 5905f333195dSJoe Perches# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5906f333195dSJoe Perches# 5907f333195dSJoe Perches# my $ms_val = $7; 5908f333195dSJoe Perches# 5909f333195dSJoe Perches# if ($ms_val =~ /^(?:0x|)0+$/i) { 5910f333195dSJoe Perches# if (WARN("PREFER_ETH_ZERO_ADDR", 5911f333195dSJoe Perches# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 5912f333195dSJoe Perches# $fix) { 5913f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 5914f333195dSJoe Perches# } 5915f333195dSJoe Perches# } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 5916f333195dSJoe Perches# if (WARN("PREFER_ETH_BROADCAST_ADDR", 5917f333195dSJoe Perches# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 5918f333195dSJoe Perches# $fix) { 5919f333195dSJoe Perches# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 5920f333195dSJoe Perches# } 5921f333195dSJoe Perches# } 5922f333195dSJoe Perches# } 59238617cd09SMateusz Kulikowski 5924d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 5925d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5926d1fe9c09SJoe Perches defined $stat && 5927d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 5928d1fe9c09SJoe Perches if (defined $2 || defined $7) { 5929d7c76ba7SJoe Perches my $call = $1; 5930d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 5931d7c76ba7SJoe Perches my $arg1 = $3; 5932d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 5933d1fe9c09SJoe Perches my $arg2 = $8; 5934d7c76ba7SJoe Perches my $cast; 5935d7c76ba7SJoe Perches 5936d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 5937d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 5938d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 5939d7c76ba7SJoe Perches $cast = $cast1; 5940d7c76ba7SJoe Perches } else { 5941d7c76ba7SJoe Perches $cast = $cast2; 5942d7c76ba7SJoe Perches } 5943d7c76ba7SJoe Perches WARN("MINMAX", 5944d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 5945554e165cSAndy Whitcroft } 5946554e165cSAndy Whitcroft } 5947554e165cSAndy Whitcroft 59484a273195SJoe Perches# check usleep_range arguments 59494a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 59504a273195SJoe Perches defined $stat && 59514a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 59524a273195SJoe Perches my $min = $1; 59534a273195SJoe Perches my $max = $7; 59544a273195SJoe Perches if ($min eq $max) { 59554a273195SJoe Perches WARN("USLEEP_RANGE", 59564a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 59574a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 59584a273195SJoe Perches $min > $max) { 59594a273195SJoe Perches WARN("USLEEP_RANGE", 59604a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 59614a273195SJoe Perches } 59624a273195SJoe Perches } 59634a273195SJoe Perches 5964823b794cSJoe Perches# check for naked sscanf 5965823b794cSJoe Perches if ($^V && $^V ge 5.10.0 && 5966823b794cSJoe Perches defined $stat && 59676c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 5968823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 5969823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 5970823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 5971823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 5972823b794cSJoe Perches $lc = $lc + $linenr; 59732a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 5974823b794cSJoe Perches WARN("NAKED_SSCANF", 5975823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 5976823b794cSJoe Perches } 5977823b794cSJoe Perches 5978afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 5979afc819abSJoe Perches if ($^V && $^V ge 5.10.0 && 5980afc819abSJoe Perches defined $stat && 5981afc819abSJoe Perches $line =~ /\bsscanf\b/) { 5982afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 5983afc819abSJoe Perches $lc = $lc + $linenr; 59842a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 5985afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 5986afc819abSJoe Perches my $format = $6; 5987afc819abSJoe Perches my $count = $format =~ tr@%@%@; 5988afc819abSJoe Perches if ($count == 1 && 5989afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 5990afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 5991afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 5992afc819abSJoe Perches } 5993afc819abSJoe Perches } 5994afc819abSJoe Perches } 5995afc819abSJoe Perches 599670dc8a48SJoe Perches# check for new externs in .h files. 599770dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 599870dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 5999d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 600070dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 600170dc8a48SJoe Perches $fix) { 6002194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 600370dc8a48SJoe Perches } 600470dc8a48SJoe Perches } 600570dc8a48SJoe Perches 6006de7d4f0eSAndy Whitcroft# check for new externs in .c files. 6007171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 6008c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 6009171ae1a4SAndy Whitcroft { 6010c45dcabdSAndy Whitcroft my $function_name = $1; 6011c45dcabdSAndy Whitcroft my $paren_space = $2; 6012171ae1a4SAndy Whitcroft 6013171ae1a4SAndy Whitcroft my $s = $stat; 6014171ae1a4SAndy Whitcroft if (defined $cond) { 6015171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 6016171ae1a4SAndy Whitcroft } 6017c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 6018c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 6019c45dcabdSAndy Whitcroft { 6020000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6021000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6022de7d4f0eSAndy Whitcroft } 6023de7d4f0eSAndy Whitcroft 6024171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 6025000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 6026000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 6027171ae1a4SAndy Whitcroft } 60289c9ba34eSAndy Whitcroft 60299c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 60309c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 60319c9ba34eSAndy Whitcroft { 6032000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 6033000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 6034171ae1a4SAndy Whitcroft } 6035171ae1a4SAndy Whitcroft 6036a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names 6037a0ad7596SJoe Perches if (defined $stat && 603825bdda2bSMiles Chen $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && 6039ca0d8929SJoe Perches $1 ne "void") { 6040ca0d8929SJoe Perches my $args = trim($1); 6041ca0d8929SJoe Perches while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { 6042ca0d8929SJoe Perches my $arg = trim($1); 6043ca0d8929SJoe Perches if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { 6044ca0d8929SJoe Perches WARN("FUNCTION_ARGUMENTS", 6045ca0d8929SJoe Perches "function definition argument '$arg' should also have an identifier name\n" . $herecurr); 6046ca0d8929SJoe Perches } 6047ca0d8929SJoe Perches } 6048ca0d8929SJoe Perches } 6049ca0d8929SJoe Perches 6050a0ad7596SJoe Perches# check for function definitions 6051a0ad7596SJoe Perches if ($^V && $^V ge 5.10.0 && 6052a0ad7596SJoe Perches defined $stat && 6053a0ad7596SJoe Perches $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { 6054a0ad7596SJoe Perches $context_function = $1; 6055a0ad7596SJoe Perches 6056a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace 6057a0ad7596SJoe Perches my $ok = 0; 6058a0ad7596SJoe Perches my $cnt = statement_rawlines($stat); 6059a0ad7596SJoe Perches my $herectx = $here . "\n"; 6060a0ad7596SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 6061a0ad7596SJoe Perches my $rl = raw_line($linenr, $n); 6062a0ad7596SJoe Perches $herectx .= $rl . "\n"; 6063a0ad7596SJoe Perches $ok = 1 if ($rl =~ /^[ \+]\{/); 6064a0ad7596SJoe Perches $ok = 1 if ($rl =~ /\{/ && $n == 0); 6065a0ad7596SJoe Perches last if $rl =~ /^[ \+].*\{/; 6066a0ad7596SJoe Perches } 6067a0ad7596SJoe Perches if (!$ok) { 6068a0ad7596SJoe Perches ERROR("OPEN_BRACE", 6069a0ad7596SJoe Perches "open brace '{' following function definitions go on the next line\n" . $herectx); 6070a0ad7596SJoe Perches } 6071a0ad7596SJoe Perches } 6072a0ad7596SJoe Perches 6073de7d4f0eSAndy Whitcroft# checks for new __setup's 6074de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 6075de7d4f0eSAndy Whitcroft my $name = $1; 6076de7d4f0eSAndy Whitcroft 6077de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 6078000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 60798c27ceffSMauro Carvalho Chehab "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); 6080de7d4f0eSAndy Whitcroft } 6081653d4876SAndy Whitcroft } 60829c0ca6f9SAndy Whitcroft 60839c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 6084caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 6085000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 6086000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 60879c0ca6f9SAndy Whitcroft } 608813214adfSAndy Whitcroft 6089a640d25cSJoe Perches# alloc style 6090a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 6091a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 6092a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 6093a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 6094a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 6095a640d25cSJoe Perches } 6096a640d25cSJoe Perches 609760a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 609860a55369SJoe Perches if ($^V && $^V ge 5.10.0 && 60991b4a2ed4SJoe Perches defined $stat && 61001b4a2ed4SJoe Perches $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 610160a55369SJoe Perches my $oldfunc = $3; 610260a55369SJoe Perches my $a1 = $4; 610360a55369SJoe Perches my $a2 = $10; 610460a55369SJoe Perches my $newfunc = "kmalloc_array"; 610560a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 610660a55369SJoe Perches my $r1 = $a1; 610760a55369SJoe Perches my $r2 = $a2; 610860a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 610960a55369SJoe Perches $r1 = $a2; 611060a55369SJoe Perches $r2 = $a1; 611160a55369SJoe Perches } 6112e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 6113e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 61141b4a2ed4SJoe Perches my $cnt = statement_rawlines($stat); 6115e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6116e3d95a2aSTobin C. Harding 6117e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 61181b4a2ed4SJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && 61191b4a2ed4SJoe Perches $cnt == 1 && 6120e367455aSJoe Perches $fix) { 6121194f66fcSJoe 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; 612260a55369SJoe Perches } 612360a55369SJoe Perches } 612460a55369SJoe Perches } 612560a55369SJoe Perches 6126972fdea2SJoe Perches# check for krealloc arg reuse 6127972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 6128972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 6129972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 6130972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 6131972fdea2SJoe Perches } 6132972fdea2SJoe Perches 61335ce59ae0SJoe Perches# check for alloc argument mismatch 61345ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 61355ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 61365ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 61375ce59ae0SJoe Perches } 61385ce59ae0SJoe Perches 6139caf2a54fSJoe Perches# check for multiple semicolons 6140caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 6141d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 6142d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 6143d5e616fcSJoe Perches $fix) { 6144194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 6145d5e616fcSJoe Perches } 6146d1e2ad07SJoe Perches } 6147d1e2ad07SJoe Perches 6148cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi 6149cec3aaa5STomas Winkler if ($realfile !~ m@^include/uapi/@ && 6150cec3aaa5STomas Winkler $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 61510ab90191SJoe Perches my $ull = ""; 61520ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 61530ab90191SJoe Perches if (CHK("BIT_MACRO", 61540ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 61550ab90191SJoe Perches $fix) { 61560ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 61570ab90191SJoe Perches } 61580ab90191SJoe Perches } 61590ab90191SJoe Perches 61602d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 61612d632745SJoe 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*$/) { 61622d632745SJoe Perches my $config = $1; 61632d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 61642d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 61652d632745SJoe Perches $fix) { 61662d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 61672d632745SJoe Perches } 61682d632745SJoe Perches } 61692d632745SJoe Perches 6170e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 6171c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 6172c34c09a8SJoe Perches my $has_break = 0; 6173c34c09a8SJoe Perches my $has_statement = 0; 6174c34c09a8SJoe Perches my $count = 0; 6175c34c09a8SJoe Perches my $prevline = $linenr; 6176e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 6177c34c09a8SJoe Perches $prevline--; 6178c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 6179c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 6180c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 6181c34c09a8SJoe Perches next if ($fline =~ /^\-/); 6182c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 6183c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 6184c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 6185c34c09a8SJoe Perches $has_statement = 1; 6186c34c09a8SJoe Perches $count++; 6187258f79d5SHeinrich Schuchardt $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); 6188c34c09a8SJoe Perches } 6189c34c09a8SJoe Perches if (!$has_break && $has_statement) { 6190c34c09a8SJoe Perches WARN("MISSING_BREAK", 6191224236d9SAndrew Morton "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); 6192c34c09a8SJoe Perches } 6193c34c09a8SJoe Perches } 6194c34c09a8SJoe Perches 6195d1e2ad07SJoe Perches# check for switch/default statements without a break; 6196d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 6197d1e2ad07SJoe Perches defined $stat && 6198d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 6199d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 6200e3d95a2aSTobin C. Harding my $herectx = get_stat_here($linenr, $cnt, $here); 6201e3d95a2aSTobin C. Harding 6202d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 6203d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 6204caf2a54fSJoe Perches } 6205caf2a54fSJoe Perches 620613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 6207d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 6208d5e616fcSJoe Perches if (WARN("USE_FUNC", 6209d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 6210d5e616fcSJoe Perches $fix) { 6211194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 6212d5e616fcSJoe Perches } 621313214adfSAndy Whitcroft } 6214773647a0SAndy Whitcroft 621562ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 621662ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 621762ec818fSJoe Perches ERROR("DATE_TIME", 621862ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 621962ec818fSJoe Perches } 622062ec818fSJoe Perches 62212c92488aSJoe Perches# check for use of yield() 62222c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 62232c92488aSJoe Perches WARN("YIELD", 62242c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 62252c92488aSJoe Perches } 62262c92488aSJoe Perches 6227179f8f40SJoe Perches# check for comparisons against true and false 6228179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 6229179f8f40SJoe Perches my $lead = $1; 6230179f8f40SJoe Perches my $arg = $2; 6231179f8f40SJoe Perches my $test = $3; 6232179f8f40SJoe Perches my $otype = $4; 6233179f8f40SJoe Perches my $trail = $5; 6234179f8f40SJoe Perches my $op = "!"; 6235179f8f40SJoe Perches 6236179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 6237179f8f40SJoe Perches 6238179f8f40SJoe Perches my $type = lc($otype); 6239179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 6240179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 6241179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 6242179f8f40SJoe Perches $op = ""; 6243179f8f40SJoe Perches } 6244179f8f40SJoe Perches 6245179f8f40SJoe Perches CHK("BOOL_COMPARISON", 6246179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 6247179f8f40SJoe Perches 6248179f8f40SJoe Perches## maybe suggesting a correct construct would better 6249179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 6250179f8f40SJoe Perches 6251179f8f40SJoe Perches } 6252179f8f40SJoe Perches } 6253179f8f40SJoe Perches 62545d430902SJoe Perches# check for bool bitfields 62555d430902SJoe Perches if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) { 62565d430902SJoe Perches WARN("BOOL_BITFIELD", 62575d430902SJoe Perches "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr); 62585d430902SJoe Perches } 62595d430902SJoe Perches 62604882720bSThomas Gleixner# check for semaphores initialized locked 62614882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 6262000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 6263000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 6264773647a0SAndy Whitcroft } 62656712d858SJoe Perches 626667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 626767d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 6268000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 626967d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 6270773647a0SAndy Whitcroft } 62716712d858SJoe Perches 6272ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 6273f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 6274000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 6275ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 6276f3db6639SMichael Ellerman } 62776712d858SJoe Perches 62780f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 6279d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {' 62806903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 6281d9190e4eSJoe Perches $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { 6282000d1cc1SJoe Perches WARN("CONST_STRUCT", 6283d9190e4eSJoe Perches "struct $1 should normally be const\n" . $herecurr); 62842b6db5cbSAndy Whitcroft } 6285773647a0SAndy Whitcroft 6286773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 6287773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 6288773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 6289c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 6290c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 6291171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 6292171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 6293171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 6294773647a0SAndy Whitcroft { 6295000d1cc1SJoe Perches WARN("NR_CPUS", 6296000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 6297773647a0SAndy Whitcroft } 62989c9ba34eSAndy Whitcroft 629952ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 630052ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 630152ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 630252ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 630352ea8506SJoe Perches } 630452ea8506SJoe Perches 6305acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 6306acd9362cSJoe Perches if ($^V && $^V ge 5.10.0 && 6307acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 6308acd9362cSJoe Perches WARN("LIKELY_MISUSE", 6309acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 6310acd9362cSJoe Perches } 6311acd9362cSJoe Perches 6312691d77b6SAndy Whitcroft# whine mightly about in_atomic 6313691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 6314691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 6315000d1cc1SJoe Perches ERROR("IN_ATOMIC", 6316000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 6317f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 6318000d1cc1SJoe Perches WARN("IN_ATOMIC", 6319000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 6320691d77b6SAndy Whitcroft } 6321691d77b6SAndy Whitcroft } 63221704f47bSPeter Zijlstra 63230f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage 63240f5225b0SPeter Zijlstra if ($line =~ /mutex_trylock_recursive/) { 63250f5225b0SPeter Zijlstra ERROR("LOCKING", 63260f5225b0SPeter Zijlstra "recursive locking is bad, do not use this ever.\n" . $herecurr); 63270f5225b0SPeter Zijlstra } 63280f5225b0SPeter Zijlstra 63291704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 63301704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 63311704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 63321704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 63331704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 63341704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 6335000d1cc1SJoe Perches ERROR("LOCKDEP", 6336000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 63371704f47bSPeter Zijlstra } 63381704f47bSPeter Zijlstra } 633988f8831cSDave Jones 6340b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 6341b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 6342000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 6343000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 634488f8831cSDave Jones } 63452435880fSJoe Perches 634600180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> 634700180468SJoe Perches# and whether or not function naming is typical and if 634800180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too 634900180468SJoe Perches if ($^V && $^V ge 5.10.0 && 635000180468SJoe Perches defined $stat && 635100180468SJoe 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*\)/) { 635200180468SJoe Perches my $var = $1; 635300180468SJoe Perches my $perms = $2; 635400180468SJoe Perches my $show = $3; 635500180468SJoe Perches my $store = $4; 635600180468SJoe Perches my $octal_perms = perms_to_octal($perms); 635700180468SJoe Perches if ($show =~ /^${var}_show$/ && 635800180468SJoe Perches $store =~ /^${var}_store$/ && 635900180468SJoe Perches $octal_perms eq "0644") { 636000180468SJoe Perches if (WARN("DEVICE_ATTR_RW", 636100180468SJoe Perches "Use DEVICE_ATTR_RW\n" . $herecurr) && 636200180468SJoe Perches $fix) { 636300180468SJoe 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})/; 636400180468SJoe Perches } 636500180468SJoe Perches } elsif ($show =~ /^${var}_show$/ && 636600180468SJoe Perches $store =~ /^NULL$/ && 636700180468SJoe Perches $octal_perms eq "0444") { 636800180468SJoe Perches if (WARN("DEVICE_ATTR_RO", 636900180468SJoe Perches "Use DEVICE_ATTR_RO\n" . $herecurr) && 637000180468SJoe Perches $fix) { 637100180468SJoe 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})/; 637200180468SJoe Perches } 637300180468SJoe Perches } elsif ($show =~ /^NULL$/ && 637400180468SJoe Perches $store =~ /^${var}_store$/ && 637500180468SJoe Perches $octal_perms eq "0200") { 637600180468SJoe Perches if (WARN("DEVICE_ATTR_WO", 637700180468SJoe Perches "Use DEVICE_ATTR_WO\n" . $herecurr) && 637800180468SJoe Perches $fix) { 637900180468SJoe 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})/; 638000180468SJoe Perches } 638100180468SJoe Perches } elsif ($octal_perms eq "0644" || 638200180468SJoe Perches $octal_perms eq "0444" || 638300180468SJoe Perches $octal_perms eq "0200") { 638400180468SJoe Perches my $newshow = "$show"; 638500180468SJoe Perches $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); 638600180468SJoe Perches my $newstore = $store; 638700180468SJoe Perches $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); 638800180468SJoe Perches my $rename = ""; 638900180468SJoe Perches if ($show ne $newshow) { 639000180468SJoe Perches $rename .= " '$show' to '$newshow'"; 639100180468SJoe Perches } 639200180468SJoe Perches if ($store ne $newstore) { 639300180468SJoe Perches $rename .= " '$store' to '$newstore'"; 639400180468SJoe Perches } 639500180468SJoe Perches WARN("DEVICE_ATTR_FUNCTIONS", 639600180468SJoe Perches "Consider renaming function(s)$rename\n" . $herecurr); 639700180468SJoe Perches } else { 639800180468SJoe Perches WARN("DEVICE_ATTR_PERMS", 639900180468SJoe Perches "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); 640000180468SJoe Perches } 640100180468SJoe Perches } 640200180468SJoe Perches 6403515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 6404515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 640573121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a 640673121534SJoe Perches# specific definition of not visible in sysfs. 640773121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means 640873121534SJoe Perches# use the default permissions 6409515a235eSJoe Perches if ($^V && $^V ge 5.10.0 && 6410459cf0aeSJoe Perches defined $stat && 6411515a235eSJoe Perches $line =~ /$mode_perms_search/) { 64122435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 64132435880fSJoe Perches my $func = $entry->[0]; 64142435880fSJoe Perches my $arg_pos = $entry->[1]; 64152435880fSJoe Perches 6416459cf0aeSJoe Perches my $lc = $stat =~ tr@\n@@; 6417459cf0aeSJoe Perches $lc = $lc + $linenr; 64182a9f9d85STobin C. Harding my $stat_real = get_stat_real($linenr, $lc); 6419459cf0aeSJoe Perches 64202435880fSJoe Perches my $skip_args = ""; 64212435880fSJoe Perches if ($arg_pos > 1) { 64222435880fSJoe Perches $arg_pos--; 64232435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 64242435880fSJoe Perches } 6425f90774e1SJoe Perches my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; 6426459cf0aeSJoe Perches if ($stat =~ /$test/) { 64272435880fSJoe Perches my $val = $1; 64282435880fSJoe Perches $val = $6 if ($skip_args ne ""); 642973121534SJoe Perches if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && 643073121534SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 643173121534SJoe Perches ($val =~ /^$Octal$/ && length($val) ne 4))) { 64322435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 6433459cf0aeSJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); 6434f90774e1SJoe Perches } 6435f90774e1SJoe Perches if ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6436c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6437459cf0aeSJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); 64382435880fSJoe Perches } 6439459cf0aeSJoe Perches } 6440459cf0aeSJoe Perches } 6441459cf0aeSJoe Perches } 6442459cf0aeSJoe Perches 6443459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability 6444bc22d9a7SJoe Perches while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { 644500180468SJoe Perches my $oval = $1; 644600180468SJoe Perches my $octal = perms_to_octal($oval); 6447f90774e1SJoe Perches if (WARN("SYMBOLIC_PERMS", 6448459cf0aeSJoe Perches "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && 6449f90774e1SJoe Perches $fix) { 645000180468SJoe Perches $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; 64512435880fSJoe Perches } 645213214adfSAndy Whitcroft } 64535a6d20ceSBjorn Andersson 64545a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 64555a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 64565a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 64575a6d20ceSBjorn Andersson my $valid_licenses = qr{ 64585a6d20ceSBjorn Andersson GPL| 64595a6d20ceSBjorn Andersson GPL\ v2| 64605a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 64615a6d20ceSBjorn Andersson Dual\ BSD/GPL| 64625a6d20ceSBjorn Andersson Dual\ MIT/GPL| 64635a6d20ceSBjorn Andersson Dual\ MPL/GPL| 64645a6d20ceSBjorn Andersson Proprietary 64655a6d20ceSBjorn Andersson }x; 64665a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 64675a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 64685a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 64695a6d20ceSBjorn Andersson } 64705a6d20ceSBjorn Andersson } 6471515a235eSJoe Perches } 647213214adfSAndy Whitcroft 647313214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 647413214adfSAndy Whitcroft # so just keep quiet. 647513214adfSAndy Whitcroft if ($#rawlines == -1) { 647613214adfSAndy Whitcroft exit(0); 64770a920b5bSAndy Whitcroft } 64780a920b5bSAndy Whitcroft 64798905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 64808905a67cSAndy Whitcroft # things that appear to be patches. 64818905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 64828905a67cSAndy Whitcroft exit(0); 64838905a67cSAndy Whitcroft } 64848905a67cSAndy Whitcroft 64858905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 64868905a67cSAndy Whitcroft # just keep quiet. 64878905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 64888905a67cSAndy Whitcroft exit(0); 64898905a67cSAndy Whitcroft } 64908905a67cSAndy Whitcroft 6491a08ffbefSStafford Horne if (!$is_patch && $filename !~ /cover-letter\.patch$/) { 6492000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6493000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 64940a920b5bSAndy Whitcroft } 6495ed43c4e5SAllen Hubbe if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) { 6496000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6497000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 64980a920b5bSAndy Whitcroft } 64990a920b5bSAndy Whitcroft 6500f0a594c1SAndy Whitcroft print report_dump(); 650113214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 650213214adfSAndy Whitcroft print "$filename " if ($summary_file); 65036c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 65046c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 65056c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 65066c72ffaaSAndy Whitcroft } 65078905a67cSAndy Whitcroft 6508d2c0a235SAndy Whitcroft if ($quiet == 0) { 6509ef212196SJoe Perches # If there were any defects found and not already fixing them 6510ef212196SJoe Perches if (!$clean and !$fix) { 6511ef212196SJoe Perches print << "EOM" 6512ef212196SJoe Perches 6513ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6514ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6515ef212196SJoe PerchesEOM 6516ef212196SJoe Perches } 6517d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6518d2c0a235SAndy Whitcroft # then suggest that. 6519d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6520b0781216SMike Frysinger $rpt_cleaners = 0; 6521d8469f16SJoe Perches print << "EOM" 6522d8469f16SJoe Perches 6523d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6524d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6525d8469f16SJoe PerchesEOM 6526d2c0a235SAndy Whitcroft } 6527d2c0a235SAndy Whitcroft } 6528d2c0a235SAndy Whitcroft 6529d752fcc8SJoe Perches if ($clean == 0 && $fix && 6530d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6531d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 65329624b8d6SJoe Perches my $newfile = $filename; 65339624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 65343705ce5bSJoe Perches my $linecount = 0; 65353705ce5bSJoe Perches my $f; 65363705ce5bSJoe Perches 6537d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6538d752fcc8SJoe Perches 65393705ce5bSJoe Perches open($f, '>', $newfile) 65403705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 65413705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 65423705ce5bSJoe Perches $linecount++; 65433705ce5bSJoe Perches if ($file) { 65443705ce5bSJoe Perches if ($linecount > 3) { 65453705ce5bSJoe Perches $fixed_line =~ s/^\+//; 65463705ce5bSJoe Perches print $f $fixed_line . "\n"; 65473705ce5bSJoe Perches } 65483705ce5bSJoe Perches } else { 65493705ce5bSJoe Perches print $f $fixed_line . "\n"; 65503705ce5bSJoe Perches } 65513705ce5bSJoe Perches } 65523705ce5bSJoe Perches close($f); 65533705ce5bSJoe Perches 65543705ce5bSJoe Perches if (!$quiet) { 65553705ce5bSJoe Perches print << "EOM"; 6556d8469f16SJoe Perches 65573705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 65583705ce5bSJoe Perches 65593705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 65603705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 65613705ce5bSJoe Perches 65623705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 65633705ce5bSJoe PerchesNo warranties, expressed or implied... 65643705ce5bSJoe PerchesEOM 65653705ce5bSJoe Perches } 65663705ce5bSJoe Perches } 65673705ce5bSJoe Perches 6568d8469f16SJoe Perches if ($quiet == 0) { 6569d8469f16SJoe Perches print "\n"; 6570d8469f16SJoe Perches if ($clean == 1) { 6571d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6572d8469f16SJoe Perches } else { 6573d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 65740a920b5bSAndy Whitcroft } 65750a920b5bSAndy Whitcroft } 65760a920b5bSAndy Whitcroft return $clean; 65770a920b5bSAndy Whitcroft} 6578