10a920b5bSAndy Whitcroft#!/usr/bin/perl -w 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; 9c707a81dSJoe Perchesuse POSIX; 100a920b5bSAndy Whitcroft 110a920b5bSAndy Whitcroftmy $P = $0; 1200df344fSAndy Whitcroft$P =~ s@.*/@@g; 130a920b5bSAndy Whitcroft 14000d1cc1SJoe Perchesmy $V = '0.32'; 150a920b5bSAndy Whitcroft 160a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 170a920b5bSAndy Whitcroft 180a920b5bSAndy Whitcroftmy $quiet = 0; 190a920b5bSAndy Whitcroftmy $tree = 1; 200a920b5bSAndy Whitcroftmy $chk_signoff = 1; 210a920b5bSAndy Whitcroftmy $chk_patch = 1; 22773647a0SAndy Whitcroftmy $tst_only; 236c72ffaaSAndy Whitcroftmy $emacs = 0; 248905a67cSAndy Whitcroftmy $terse = 0; 256c72ffaaSAndy Whitcroftmy $file = 0; 266c72ffaaSAndy Whitcroftmy $check = 0; 278905a67cSAndy Whitcroftmy $summary = 1; 288905a67cSAndy Whitcroftmy $mailback = 0; 2913214adfSAndy Whitcroftmy $summary_file = 0; 30000d1cc1SJoe Perchesmy $show_types = 0; 313705ce5bSJoe Perchesmy $fix = 0; 326c72ffaaSAndy Whitcroftmy $root; 33c2fdda0dSAndy Whitcroftmy %debug; 343445686aSJoe Perchesmy %camelcase = (); 3591bfe484SJoe Perchesmy %use_type = (); 3691bfe484SJoe Perchesmy @use = (); 3791bfe484SJoe Perchesmy %ignore_type = (); 38000d1cc1SJoe Perchesmy @ignore = (); 3977f5b10aSHannes Edermy $help = 0; 40000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 416cd7f386SJoe Perchesmy $max_line_length = 80; 42d62a201fSDave Hansenmy $ignore_perl_version = 0; 43d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 4477f5b10aSHannes Eder 4577f5b10aSHannes Edersub help { 4677f5b10aSHannes Eder my ($exitcode) = @_; 4777f5b10aSHannes Eder 4877f5b10aSHannes Eder print << "EOM"; 4977f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 5077f5b10aSHannes EderVersion: $V 5177f5b10aSHannes Eder 5277f5b10aSHannes EderOptions: 5377f5b10aSHannes Eder -q, --quiet quiet 5477f5b10aSHannes Eder --no-tree run without a kernel tree 5577f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 5677f5b10aSHannes Eder --patch treat FILE as patchfile (default) 5777f5b10aSHannes Eder --emacs emacs compile window format 5877f5b10aSHannes Eder --terse one line per report 5977f5b10aSHannes Eder -f, --file treat FILE as regular source file 6077f5b10aSHannes Eder --subjective, --strict enable more subjective tests 6191bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 62000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 636cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 64000d1cc1SJoe Perches --show-types show the message "types" in the output 6577f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 6677f5b10aSHannes Eder --no-summary suppress the per-file summary 6777f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 6877f5b10aSHannes Eder --summary-file include the filename in summary 6977f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 7077f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 7177f5b10aSHannes Eder is all off) 7277f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 7377f5b10aSHannes Eder literally 743705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 753705ce5bSJoe Perches If correctable single-line errors exist, create 763705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 773705ce5bSJoe Perches with potential errors corrected to the preferred 783705ce5bSJoe Perches checkpatch style 79d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 80d62a201fSDave Hansen runtime errors. 8177f5b10aSHannes Eder -h, --help, --version display this help and exit 8277f5b10aSHannes Eder 8377f5b10aSHannes EderWhen FILE is - read standard input. 8477f5b10aSHannes EderEOM 8577f5b10aSHannes Eder 8677f5b10aSHannes Eder exit($exitcode); 8777f5b10aSHannes Eder} 8877f5b10aSHannes Eder 89000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 90000d1cc1SJoe Perchesif (-f $conf) { 91000d1cc1SJoe Perches my @conf_args; 92000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 93000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 94000d1cc1SJoe Perches 95000d1cc1SJoe Perches while (<$conffile>) { 96000d1cc1SJoe Perches my $line = $_; 97000d1cc1SJoe Perches 98000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 99000d1cc1SJoe Perches $line =~ s/^\s*//g; 100000d1cc1SJoe Perches $line =~ s/\s+/ /g; 101000d1cc1SJoe Perches 102000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 103000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 104000d1cc1SJoe Perches 105000d1cc1SJoe Perches my @words = split(" ", $line); 106000d1cc1SJoe Perches foreach my $word (@words) { 107000d1cc1SJoe Perches last if ($word =~ m/^#/); 108000d1cc1SJoe Perches push (@conf_args, $word); 109000d1cc1SJoe Perches } 110000d1cc1SJoe Perches } 111000d1cc1SJoe Perches close($conffile); 112000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 113000d1cc1SJoe Perches} 114000d1cc1SJoe Perches 1150a920b5bSAndy WhitcroftGetOptions( 1166c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1170a920b5bSAndy Whitcroft 'tree!' => \$tree, 1180a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1190a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1206c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1218905a67cSAndy Whitcroft 'terse!' => \$terse, 12277f5b10aSHannes Eder 'f|file!' => \$file, 1236c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1246c72ffaaSAndy Whitcroft 'strict!' => \$check, 125000d1cc1SJoe Perches 'ignore=s' => \@ignore, 12691bfe484SJoe Perches 'types=s' => \@use, 127000d1cc1SJoe Perches 'show-types!' => \$show_types, 1286cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 1296c72ffaaSAndy Whitcroft 'root=s' => \$root, 1308905a67cSAndy Whitcroft 'summary!' => \$summary, 1318905a67cSAndy Whitcroft 'mailback!' => \$mailback, 13213214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 1333705ce5bSJoe Perches 'fix!' => \$fix, 134d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 135c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 136773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 13777f5b10aSHannes Eder 'h|help' => \$help, 13877f5b10aSHannes Eder 'version' => \$help 13977f5b10aSHannes Eder) or help(1); 14077f5b10aSHannes Eder 14177f5b10aSHannes Ederhelp(0) if ($help); 1420a920b5bSAndy Whitcroft 1430a920b5bSAndy Whitcroftmy $exit = 0; 1440a920b5bSAndy Whitcroft 145d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 146d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 147d62a201fSDave Hansen if (!$ignore_perl_version) { 148d62a201fSDave Hansen exit(1); 149d62a201fSDave Hansen } 150d62a201fSDave Hansen} 151d62a201fSDave Hansen 1520a920b5bSAndy Whitcroftif ($#ARGV < 0) { 15377f5b10aSHannes Eder print "$P: no input files\n"; 1540a920b5bSAndy Whitcroft exit(1); 1550a920b5bSAndy Whitcroft} 1560a920b5bSAndy Whitcroft 15791bfe484SJoe Perchessub hash_save_array_words { 15891bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 15991bfe484SJoe Perches 16091bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 16191bfe484SJoe Perches foreach my $word (@array) { 162000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 163000d1cc1SJoe Perches $word =~ s/^\s*//g; 164000d1cc1SJoe Perches $word =~ s/\s+/ /g; 165000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 166000d1cc1SJoe Perches 167000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 168000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 169000d1cc1SJoe Perches 17091bfe484SJoe Perches $hashRef->{$word}++; 171000d1cc1SJoe Perches } 17291bfe484SJoe Perches} 17391bfe484SJoe Perches 17491bfe484SJoe Perchessub hash_show_words { 17591bfe484SJoe Perches my ($hashRef, $prefix) = @_; 17691bfe484SJoe Perches 177*58cb3cf6SJoe Perches if ($quiet == 0 && keys %$hashRef) { 17891bfe484SJoe Perches print "NOTE: $prefix message types:"; 179*58cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 18091bfe484SJoe Perches print " $word"; 18191bfe484SJoe Perches } 18291bfe484SJoe Perches print "\n\n"; 18391bfe484SJoe Perches } 18491bfe484SJoe Perches} 18591bfe484SJoe Perches 18691bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 18791bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 188000d1cc1SJoe Perches 189c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 190c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 1917429c690SAndy Whitcroftmy $dbg_type = 0; 192a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 193c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 19421caa13cSAndy Whitcroft ## no critic 19521caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 19621caa13cSAndy Whitcroft die "$@" if ($@); 197c2fdda0dSAndy Whitcroft} 198c2fdda0dSAndy Whitcroft 199d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 200d2c0a235SAndy Whitcroft 2018905a67cSAndy Whitcroftif ($terse) { 2028905a67cSAndy Whitcroft $emacs = 1; 2038905a67cSAndy Whitcroft $quiet++; 2048905a67cSAndy Whitcroft} 2058905a67cSAndy Whitcroft 2066c72ffaaSAndy Whitcroftif ($tree) { 2076c72ffaaSAndy Whitcroft if (defined $root) { 2086c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 2096c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 2106c72ffaaSAndy Whitcroft } 2116c72ffaaSAndy Whitcroft } else { 2126c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 2136c72ffaaSAndy Whitcroft $root = '.'; 2146c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 2156c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 2166c72ffaaSAndy Whitcroft $root = $1; 2176c72ffaaSAndy Whitcroft } 2186c72ffaaSAndy Whitcroft } 2196c72ffaaSAndy Whitcroft 2206c72ffaaSAndy Whitcroft if (!defined $root) { 2210a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 2220a920b5bSAndy Whitcroft exit(2); 2230a920b5bSAndy Whitcroft } 2246c72ffaaSAndy Whitcroft} 2256c72ffaaSAndy Whitcroft 2266c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 2276c72ffaaSAndy Whitcroft 2282ceb532bSAndy Whitcroftour $Ident = qr{ 2292ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 2302ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 2312ceb532bSAndy Whitcroft }x; 2326c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 2336c72ffaaSAndy Whitcroftour $Sparse = qr{ 2346c72ffaaSAndy Whitcroft __user| 2356c72ffaaSAndy Whitcroft __kernel| 2366c72ffaaSAndy Whitcroft __force| 2376c72ffaaSAndy Whitcroft __iomem| 2386c72ffaaSAndy Whitcroft __must_check| 2396c72ffaaSAndy Whitcroft __init_refok| 240417495edSAndy Whitcroft __kprobes| 241165e72a6SSven Eckelmann __ref| 242165e72a6SSven Eckelmann __rcu 2436c72ffaaSAndy Whitcroft }x; 24452131292SWolfram Sang 24552131292SWolfram Sang# Notes to $Attribute: 24652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 2476c72ffaaSAndy Whitcroftour $Attribute = qr{ 2486c72ffaaSAndy Whitcroft const| 24903f1df7dSJoe Perches __percpu| 25003f1df7dSJoe Perches __nocast| 25103f1df7dSJoe Perches __safe| 25203f1df7dSJoe Perches __bitwise__| 25303f1df7dSJoe Perches __packed__| 25403f1df7dSJoe Perches __packed2__| 25503f1df7dSJoe Perches __naked| 25603f1df7dSJoe Perches __maybe_unused| 25703f1df7dSJoe Perches __always_unused| 25803f1df7dSJoe Perches __noreturn| 25903f1df7dSJoe Perches __used| 26003f1df7dSJoe Perches __cold| 26103f1df7dSJoe Perches __noclone| 26203f1df7dSJoe Perches __deprecated| 2636c72ffaaSAndy Whitcroft __read_mostly| 2646c72ffaaSAndy Whitcroft __kprobes| 26552131292SWolfram Sang __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| 26624e1d81aSAndy Whitcroft ____cacheline_aligned| 26724e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 2685fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 2695fe3af11SAndy Whitcroft __weak 2706c72ffaaSAndy Whitcroft }x; 271c45dcabdSAndy Whitcroftour $Modifier; 2726c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 2736c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 2746c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 2756c72ffaaSAndy Whitcroft 27695e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 27795e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 27895e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 27995e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 280326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 281326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 282326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 28374349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 28495e2c602SJoe Perchesour $Constant = qr{$Float|$Binary|$Hex|$Int}; 285326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 28686f9d059SAndy Whitcroftour $Compare = qr{<=|>=|==|!=|<|>}; 28723f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 2886c72ffaaSAndy Whitcroftour $Operators = qr{ 2896c72ffaaSAndy Whitcroft <=|>=|==|!=| 2906c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 29123f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 2926c72ffaaSAndy Whitcroft }x; 2936c72ffaaSAndy Whitcroft 2948905a67cSAndy Whitcroftour $NonptrType; 2958905a67cSAndy Whitcroftour $Type; 2968905a67cSAndy Whitcroftour $Declare; 2978905a67cSAndy Whitcroft 29815662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 29915662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 300171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 301171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 302171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 303171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 304171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 305171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 306171ae1a4SAndy Whitcroft}x; 307171ae1a4SAndy Whitcroft 30815662b3eSJoe Perchesour $UTF8 = qr{ 30915662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 31015662b3eSJoe Perches | $NON_ASCII_UTF8 31115662b3eSJoe Perches}x; 31215662b3eSJoe Perches 3138ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 314fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 3158ed22cadSAndy Whitcroft atomic_t 3168ed22cadSAndy Whitcroft)}; 3178ed22cadSAndy Whitcroft 318691e669bSJoe Perchesour $logFunctions = qr{(?x: 3196e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 3207d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 3216e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 322b0531722SJoe Perches panic| 323b0531722SJoe Perches MODULE_[A-Z_]+ 324691e669bSJoe Perches)}; 325691e669bSJoe Perches 32620112475SJoe Perchesour $signature_tags = qr{(?xi: 32720112475SJoe Perches Signed-off-by:| 32820112475SJoe Perches Acked-by:| 32920112475SJoe Perches Tested-by:| 33020112475SJoe Perches Reviewed-by:| 33120112475SJoe Perches Reported-by:| 3328543ae12SMugunthan V N Suggested-by:| 33320112475SJoe Perches To:| 33420112475SJoe Perches Cc: 33520112475SJoe Perches)}; 33620112475SJoe Perches 3378905a67cSAndy Whitcroftour @typeList = ( 3388905a67cSAndy Whitcroft qr{void}, 339c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 340c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 341c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 342c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 343c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 344c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 345c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 3468905a67cSAndy Whitcroft qr{unsigned}, 3478905a67cSAndy Whitcroft qr{float}, 3488905a67cSAndy Whitcroft qr{double}, 3498905a67cSAndy Whitcroft qr{bool}, 3508905a67cSAndy Whitcroft qr{struct\s+$Ident}, 3518905a67cSAndy Whitcroft qr{union\s+$Ident}, 3528905a67cSAndy Whitcroft qr{enum\s+$Ident}, 3538905a67cSAndy Whitcroft qr{${Ident}_t}, 3548905a67cSAndy Whitcroft qr{${Ident}_handler}, 3558905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 3568905a67cSAndy Whitcroft); 357c45dcabdSAndy Whitcroftour @modifierList = ( 358c45dcabdSAndy Whitcroft qr{fastcall}, 359c45dcabdSAndy Whitcroft); 3608905a67cSAndy Whitcroft 3617840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 3627840a94cSWolfram Sang irq| 3637840a94cSWolfram Sang memory 3647840a94cSWolfram Sang)}; 3657840a94cSWolfram Sang# memory.h: ARM has a custom one 3667840a94cSWolfram Sang 3678905a67cSAndy Whitcroftsub build_types { 368d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 369d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 370c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 3718905a67cSAndy Whitcroft $NonptrType = qr{ 372d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 373cf655043SAndy Whitcroft (?: 3746b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 3758ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 376c45dcabdSAndy Whitcroft (?:${all}\b) 377cf655043SAndy Whitcroft ) 378c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 3798905a67cSAndy Whitcroft }x; 3808905a67cSAndy Whitcroft $Type = qr{ 381c45dcabdSAndy Whitcroft $NonptrType 382b337d8b8SAndy Whitcroft (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 383c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 3848905a67cSAndy Whitcroft }x; 3858905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 3868905a67cSAndy Whitcroft} 3878905a67cSAndy Whitcroftbuild_types(); 3886c72ffaaSAndy Whitcroft 3897d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 390d1fe9c09SJoe Perches 391d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 392d1fe9c09SJoe Perches# requires at least perl version v5.10.0 393d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 394d1fe9c09SJoe Perches 395d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 396d1fe9c09SJoe Perchesour $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; 397d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; 3987d2367afSJoe Perches 3997d2367afSJoe Perchessub deparenthesize { 4007d2367afSJoe Perches my ($string) = @_; 4017d2367afSJoe Perches return "" if (!defined($string)); 4027d2367afSJoe Perches $string =~ s@^\s*\(\s*@@g; 4037d2367afSJoe Perches $string =~ s@\s*\)\s*$@@g; 4047d2367afSJoe Perches $string =~ s@\s+@ @g; 4057d2367afSJoe Perches return $string; 4067d2367afSJoe Perches} 4077d2367afSJoe Perches 4083445686aSJoe Perchessub seed_camelcase_file { 4093445686aSJoe Perches my ($file) = @_; 4103445686aSJoe Perches 4113445686aSJoe Perches return if (!(-f $file)); 4123445686aSJoe Perches 4133445686aSJoe Perches local $/; 4143445686aSJoe Perches 4153445686aSJoe Perches open(my $include_file, '<', "$file") 4163445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 4173445686aSJoe Perches my $text = <$include_file>; 4183445686aSJoe Perches close($include_file); 4193445686aSJoe Perches 4203445686aSJoe Perches my @lines = split('\n', $text); 4213445686aSJoe Perches 4223445686aSJoe Perches foreach my $line (@lines) { 4233445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 4243445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 4253445686aSJoe Perches $camelcase{$1} = 1; 4263445686aSJoe Perches } 4273445686aSJoe Perches elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) { 4283445686aSJoe Perches $camelcase{$1} = 1; 4293445686aSJoe Perches } 4303445686aSJoe Perches } 4313445686aSJoe Perches} 4323445686aSJoe Perches 4333445686aSJoe Perchesmy $camelcase_seeded = 0; 4343445686aSJoe Perchessub seed_camelcase_includes { 4353445686aSJoe Perches return if ($camelcase_seeded); 4363445686aSJoe Perches 4373445686aSJoe Perches my $files; 438c707a81dSJoe Perches my $camelcase_cache = ""; 439c707a81dSJoe Perches my @include_files = (); 440c707a81dSJoe Perches 441c707a81dSJoe Perches $camelcase_seeded = 1; 442351b2a1fSJoe Perches 4433445686aSJoe Perches if (-d ".git") { 444351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 445351b2a1fSJoe Perches chomp $git_last_include_commit; 446c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 447c707a81dSJoe Perches } else { 448c707a81dSJoe Perches my $last_mod_date = 0; 449c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 450c707a81dSJoe Perches @include_files = split('\n', $files); 451c707a81dSJoe Perches foreach my $file (@include_files) { 452c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 453c707a81dSJoe Perches localtime((stat $file)[9])); 454c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 455c707a81dSJoe Perches } 456c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 457c707a81dSJoe Perches } 458c707a81dSJoe Perches 459c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 460c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 461c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 462351b2a1fSJoe Perches while (<$camelcase_file>) { 463351b2a1fSJoe Perches chomp; 464351b2a1fSJoe Perches $camelcase{$_} = 1; 465351b2a1fSJoe Perches } 466351b2a1fSJoe Perches close($camelcase_file); 467351b2a1fSJoe Perches 468351b2a1fSJoe Perches return; 469351b2a1fSJoe Perches } 470c707a81dSJoe Perches 471c707a81dSJoe Perches if (-d ".git") { 472c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 473c707a81dSJoe Perches @include_files = split('\n', $files); 4743445686aSJoe Perches } 475c707a81dSJoe Perches 4763445686aSJoe Perches foreach my $file (@include_files) { 4773445686aSJoe Perches seed_camelcase_file($file); 4783445686aSJoe Perches } 479351b2a1fSJoe Perches 480c707a81dSJoe Perches if ($camelcase_cache ne "") { 481351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 482c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 483c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 484351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 485351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 486351b2a1fSJoe Perches } 487351b2a1fSJoe Perches close($camelcase_file); 488351b2a1fSJoe Perches } 4893445686aSJoe Perches} 4903445686aSJoe Perches 4916c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 4920a920b5bSAndy Whitcroft 49300df344fSAndy Whitcroftmy @rawlines = (); 494c2fdda0dSAndy Whitcroftmy @lines = (); 4953705ce5bSJoe Perchesmy @fixed = (); 496c2fdda0dSAndy Whitcroftmy $vname; 4976c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 49821caa13cSAndy Whitcroft my $FILE; 4996c72ffaaSAndy Whitcroft if ($file) { 50021caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 5016c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 50221caa13cSAndy Whitcroft } elsif ($filename eq '-') { 50321caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 5046c72ffaaSAndy Whitcroft } else { 50521caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 5066c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 5076c72ffaaSAndy Whitcroft } 508c2fdda0dSAndy Whitcroft if ($filename eq '-') { 509c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 510c2fdda0dSAndy Whitcroft } else { 511c2fdda0dSAndy Whitcroft $vname = $filename; 512c2fdda0dSAndy Whitcroft } 51321caa13cSAndy Whitcroft while (<$FILE>) { 5140a920b5bSAndy Whitcroft chomp; 51500df344fSAndy Whitcroft push(@rawlines, $_); 5166c72ffaaSAndy Whitcroft } 51721caa13cSAndy Whitcroft close($FILE); 518c2fdda0dSAndy Whitcroft if (!process($filename)) { 5190a920b5bSAndy Whitcroft $exit = 1; 5200a920b5bSAndy Whitcroft } 52100df344fSAndy Whitcroft @rawlines = (); 52213214adfSAndy Whitcroft @lines = (); 5233705ce5bSJoe Perches @fixed = (); 5240a920b5bSAndy Whitcroft} 5250a920b5bSAndy Whitcroft 5260a920b5bSAndy Whitcroftexit($exit); 5270a920b5bSAndy Whitcroft 5280a920b5bSAndy Whitcroftsub top_of_kernel_tree { 5296c72ffaaSAndy Whitcroft my ($root) = @_; 5306c72ffaaSAndy Whitcroft 5316c72ffaaSAndy Whitcroft my @tree_check = ( 5326c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 5336c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 5346c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 5356c72ffaaSAndy Whitcroft ); 5366c72ffaaSAndy Whitcroft 5376c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 5386c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 5390a920b5bSAndy Whitcroft return 0; 5400a920b5bSAndy Whitcroft } 5416c72ffaaSAndy Whitcroft } 5426c72ffaaSAndy Whitcroft return 1; 5436c72ffaaSAndy Whitcroft} 5440a920b5bSAndy Whitcroft 54520112475SJoe Perchessub parse_email { 54620112475SJoe Perches my ($formatted_email) = @_; 54720112475SJoe Perches 54820112475SJoe Perches my $name = ""; 54920112475SJoe Perches my $address = ""; 55020112475SJoe Perches my $comment = ""; 55120112475SJoe Perches 55220112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 55320112475SJoe Perches $name = $1; 55420112475SJoe Perches $address = $2; 55520112475SJoe Perches $comment = $3 if defined $3; 55620112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 55720112475SJoe Perches $address = $1; 55820112475SJoe Perches $comment = $2 if defined $2; 55920112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 56020112475SJoe Perches $address = $1; 56120112475SJoe Perches $comment = $2 if defined $2; 56220112475SJoe Perches $formatted_email =~ s/$address.*$//; 56320112475SJoe Perches $name = $formatted_email; 5643705ce5bSJoe Perches $name = trim($name); 56520112475SJoe Perches $name =~ s/^\"|\"$//g; 56620112475SJoe Perches # If there's a name left after stripping spaces and 56720112475SJoe Perches # leading quotes, and the address doesn't have both 56820112475SJoe Perches # leading and trailing angle brackets, the address 56920112475SJoe Perches # is invalid. ie: 57020112475SJoe Perches # "joe smith [email protected]" bad 57120112475SJoe Perches # "joe smith <[email protected]" bad 57220112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 57320112475SJoe Perches $name = ""; 57420112475SJoe Perches $address = ""; 57520112475SJoe Perches $comment = ""; 57620112475SJoe Perches } 57720112475SJoe Perches } 57820112475SJoe Perches 5793705ce5bSJoe Perches $name = trim($name); 58020112475SJoe Perches $name =~ s/^\"|\"$//g; 5813705ce5bSJoe Perches $address = trim($address); 58220112475SJoe Perches $address =~ s/^\<|\>$//g; 58320112475SJoe Perches 58420112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 58520112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 58620112475SJoe Perches $name = "\"$name\""; 58720112475SJoe Perches } 58820112475SJoe Perches 58920112475SJoe Perches return ($name, $address, $comment); 59020112475SJoe Perches} 59120112475SJoe Perches 59220112475SJoe Perchessub format_email { 59320112475SJoe Perches my ($name, $address) = @_; 59420112475SJoe Perches 59520112475SJoe Perches my $formatted_email; 59620112475SJoe Perches 5973705ce5bSJoe Perches $name = trim($name); 59820112475SJoe Perches $name =~ s/^\"|\"$//g; 5993705ce5bSJoe Perches $address = trim($address); 60020112475SJoe Perches 60120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 60220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 60320112475SJoe Perches $name = "\"$name\""; 60420112475SJoe Perches } 60520112475SJoe Perches 60620112475SJoe Perches if ("$name" eq "") { 60720112475SJoe Perches $formatted_email = "$address"; 60820112475SJoe Perches } else { 60920112475SJoe Perches $formatted_email = "$name <$address>"; 61020112475SJoe Perches } 61120112475SJoe Perches 61220112475SJoe Perches return $formatted_email; 61320112475SJoe Perches} 61420112475SJoe Perches 615000d1cc1SJoe Perchessub which_conf { 616000d1cc1SJoe Perches my ($conf) = @_; 617000d1cc1SJoe Perches 618000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 619000d1cc1SJoe Perches if (-e "$path/$conf") { 620000d1cc1SJoe Perches return "$path/$conf"; 621000d1cc1SJoe Perches } 622000d1cc1SJoe Perches } 623000d1cc1SJoe Perches 624000d1cc1SJoe Perches return ""; 625000d1cc1SJoe Perches} 626000d1cc1SJoe Perches 6270a920b5bSAndy Whitcroftsub expand_tabs { 6280a920b5bSAndy Whitcroft my ($str) = @_; 6290a920b5bSAndy Whitcroft 6300a920b5bSAndy Whitcroft my $res = ''; 6310a920b5bSAndy Whitcroft my $n = 0; 6320a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 6330a920b5bSAndy Whitcroft if ($c eq "\t") { 6340a920b5bSAndy Whitcroft $res .= ' '; 6350a920b5bSAndy Whitcroft $n++; 6360a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 6370a920b5bSAndy Whitcroft $res .= ' '; 6380a920b5bSAndy Whitcroft } 6390a920b5bSAndy Whitcroft next; 6400a920b5bSAndy Whitcroft } 6410a920b5bSAndy Whitcroft $res .= $c; 6420a920b5bSAndy Whitcroft $n++; 6430a920b5bSAndy Whitcroft } 6440a920b5bSAndy Whitcroft 6450a920b5bSAndy Whitcroft return $res; 6460a920b5bSAndy Whitcroft} 6476c72ffaaSAndy Whitcroftsub copy_spacing { 648773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 6496c72ffaaSAndy Whitcroft return $res; 6506c72ffaaSAndy Whitcroft} 6510a920b5bSAndy Whitcroft 6524a0df2efSAndy Whitcroftsub line_stats { 6534a0df2efSAndy Whitcroft my ($line) = @_; 6544a0df2efSAndy Whitcroft 6554a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 6564a0df2efSAndy Whitcroft $line =~ s/^.//; 6574a0df2efSAndy Whitcroft $line = expand_tabs($line); 6584a0df2efSAndy Whitcroft 6594a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 6604a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 6614a0df2efSAndy Whitcroft 6624a0df2efSAndy Whitcroft return (length($line), length($white)); 6634a0df2efSAndy Whitcroft} 6644a0df2efSAndy Whitcroft 665773647a0SAndy Whitcroftmy $sanitise_quote = ''; 666773647a0SAndy Whitcroft 667773647a0SAndy Whitcroftsub sanitise_line_reset { 668773647a0SAndy Whitcroft my ($in_comment) = @_; 669773647a0SAndy Whitcroft 670773647a0SAndy Whitcroft if ($in_comment) { 671773647a0SAndy Whitcroft $sanitise_quote = '*/'; 672773647a0SAndy Whitcroft } else { 673773647a0SAndy Whitcroft $sanitise_quote = ''; 674773647a0SAndy Whitcroft } 675773647a0SAndy Whitcroft} 67600df344fSAndy Whitcroftsub sanitise_line { 67700df344fSAndy Whitcroft my ($line) = @_; 67800df344fSAndy Whitcroft 67900df344fSAndy Whitcroft my $res = ''; 68000df344fSAndy Whitcroft my $l = ''; 68100df344fSAndy Whitcroft 682c2fdda0dSAndy Whitcroft my $qlen = 0; 683773647a0SAndy Whitcroft my $off = 0; 684773647a0SAndy Whitcroft my $c; 68500df344fSAndy Whitcroft 686773647a0SAndy Whitcroft # Always copy over the diff marker. 687773647a0SAndy Whitcroft $res = substr($line, 0, 1); 688773647a0SAndy Whitcroft 689773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 690773647a0SAndy Whitcroft $c = substr($line, $off, 1); 691773647a0SAndy Whitcroft 692773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 693773647a0SAndy Whitcroft # and end, all to $;. 694773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 695773647a0SAndy Whitcroft $sanitise_quote = '*/'; 696773647a0SAndy Whitcroft 697773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 698773647a0SAndy Whitcroft $off++; 69900df344fSAndy Whitcroft next; 700773647a0SAndy Whitcroft } 70181bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 702773647a0SAndy Whitcroft $sanitise_quote = ''; 703773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 704773647a0SAndy Whitcroft $off++; 705773647a0SAndy Whitcroft next; 706773647a0SAndy Whitcroft } 707113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 708113f04a8SDaniel Walker $sanitise_quote = '//'; 709113f04a8SDaniel Walker 710113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 711113f04a8SDaniel Walker $off++; 712113f04a8SDaniel Walker next; 713113f04a8SDaniel Walker } 714773647a0SAndy Whitcroft 715773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 716773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 717773647a0SAndy Whitcroft $c eq "\\") { 718773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 719773647a0SAndy Whitcroft $off++; 720773647a0SAndy Whitcroft next; 721773647a0SAndy Whitcroft } 722773647a0SAndy Whitcroft # Regular quotes. 723773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 724773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 725773647a0SAndy Whitcroft $sanitise_quote = $c; 726773647a0SAndy Whitcroft 727773647a0SAndy Whitcroft substr($res, $off, 1, $c); 728773647a0SAndy Whitcroft next; 729773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 730773647a0SAndy Whitcroft $sanitise_quote = ''; 73100df344fSAndy Whitcroft } 73200df344fSAndy Whitcroft } 733773647a0SAndy Whitcroft 734fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 735773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 736773647a0SAndy Whitcroft substr($res, $off, 1, $;); 737113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 738113f04a8SDaniel Walker substr($res, $off, 1, $;); 739773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 740773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 74100df344fSAndy Whitcroft } else { 742773647a0SAndy Whitcroft substr($res, $off, 1, $c); 74300df344fSAndy Whitcroft } 744c2fdda0dSAndy Whitcroft } 745c2fdda0dSAndy Whitcroft 746113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 747113f04a8SDaniel Walker $sanitise_quote = ''; 748113f04a8SDaniel Walker } 749113f04a8SDaniel Walker 750c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 751c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 752c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 753c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 754c2fdda0dSAndy Whitcroft 755c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 756c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 757c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 758c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 759c2fdda0dSAndy Whitcroft } 760c2fdda0dSAndy Whitcroft 76100df344fSAndy Whitcroft return $res; 76200df344fSAndy Whitcroft} 76300df344fSAndy Whitcroft 764a6962d72SJoe Perchessub get_quoted_string { 765a6962d72SJoe Perches my ($line, $rawline) = @_; 766a6962d72SJoe Perches 767a6962d72SJoe Perches return "" if ($line !~ m/(\"[X]+\")/g); 768a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 769a6962d72SJoe Perches} 770a6962d72SJoe Perches 7718905a67cSAndy Whitcroftsub ctx_statement_block { 7728905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 7738905a67cSAndy Whitcroft my $line = $linenr - 1; 7748905a67cSAndy Whitcroft my $blk = ''; 7758905a67cSAndy Whitcroft my $soff = $off; 7768905a67cSAndy Whitcroft my $coff = $off - 1; 777773647a0SAndy Whitcroft my $coff_set = 0; 7788905a67cSAndy Whitcroft 77913214adfSAndy Whitcroft my $loff = 0; 78013214adfSAndy Whitcroft 7818905a67cSAndy Whitcroft my $type = ''; 7828905a67cSAndy Whitcroft my $level = 0; 783a2750645SAndy Whitcroft my @stack = (); 784cf655043SAndy Whitcroft my $p; 7858905a67cSAndy Whitcroft my $c; 7868905a67cSAndy Whitcroft my $len = 0; 78713214adfSAndy Whitcroft 78813214adfSAndy Whitcroft my $remainder; 7898905a67cSAndy Whitcroft while (1) { 790a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 791a2750645SAndy Whitcroft 792773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 7938905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 7948905a67cSAndy Whitcroft # context. 7958905a67cSAndy Whitcroft if ($off >= $len) { 7968905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 797dea33496SAndy Whitcroft last if (!defined $lines[$line]); 798c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 7998905a67cSAndy Whitcroft $remain--; 80013214adfSAndy Whitcroft $loff = $len; 801c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 8028905a67cSAndy Whitcroft $len = length($blk); 8038905a67cSAndy Whitcroft $line++; 8048905a67cSAndy Whitcroft last; 8058905a67cSAndy Whitcroft } 8068905a67cSAndy Whitcroft # Bail if there is no further context. 8078905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 80813214adfSAndy Whitcroft if ($off >= $len) { 8098905a67cSAndy Whitcroft last; 8108905a67cSAndy Whitcroft } 811f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 812f74bd194SAndy Whitcroft $level++; 813f74bd194SAndy Whitcroft $type = '#'; 814f74bd194SAndy Whitcroft } 8158905a67cSAndy Whitcroft } 816cf655043SAndy Whitcroft $p = $c; 8178905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 81813214adfSAndy Whitcroft $remainder = substr($blk, $off); 8198905a67cSAndy Whitcroft 820773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 8214635f4fbSAndy Whitcroft 8224635f4fbSAndy Whitcroft # Handle nested #if/#else. 8234635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 8244635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 8254635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 8264635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 8274635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 8284635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 8294635f4fbSAndy Whitcroft } 8304635f4fbSAndy Whitcroft 8318905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 8328905a67cSAndy Whitcroft # outermost level. 8338905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 8348905a67cSAndy Whitcroft last; 8358905a67cSAndy Whitcroft } 8368905a67cSAndy Whitcroft 83713214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 838773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 839773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 840773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 841773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 842773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 843773647a0SAndy Whitcroft $coff_set = 1; 844773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 845773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 84613214adfSAndy Whitcroft } 84713214adfSAndy Whitcroft 8488905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 8498905a67cSAndy Whitcroft $level++; 8508905a67cSAndy Whitcroft $type = '('; 8518905a67cSAndy Whitcroft } 8528905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 8538905a67cSAndy Whitcroft $level--; 8548905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 8558905a67cSAndy Whitcroft 8568905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 8578905a67cSAndy Whitcroft $coff = $off; 858773647a0SAndy Whitcroft $coff_set = 1; 859773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 8608905a67cSAndy Whitcroft } 8618905a67cSAndy Whitcroft } 8628905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 8638905a67cSAndy Whitcroft $level++; 8648905a67cSAndy Whitcroft $type = '{'; 8658905a67cSAndy Whitcroft } 8668905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 8678905a67cSAndy Whitcroft $level--; 8688905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 8698905a67cSAndy Whitcroft 8708905a67cSAndy Whitcroft if ($level == 0) { 871b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 872b998e001SPatrick Pannuto $off++; 873b998e001SPatrick Pannuto } 8748905a67cSAndy Whitcroft last; 8758905a67cSAndy Whitcroft } 8768905a67cSAndy Whitcroft } 877f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 878f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 879f74bd194SAndy Whitcroft $level--; 880f74bd194SAndy Whitcroft $type = ''; 881f74bd194SAndy Whitcroft $off++; 882f74bd194SAndy Whitcroft last; 883f74bd194SAndy Whitcroft } 8848905a67cSAndy Whitcroft $off++; 8858905a67cSAndy Whitcroft } 886a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 88713214adfSAndy Whitcroft if ($off == $len) { 888a3bb97a7SAndy Whitcroft $loff = $len + 1; 88913214adfSAndy Whitcroft $line++; 89013214adfSAndy Whitcroft $remain--; 89113214adfSAndy Whitcroft } 8928905a67cSAndy Whitcroft 8938905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 8948905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 8958905a67cSAndy Whitcroft 8968905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 8978905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 8988905a67cSAndy Whitcroft 899773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 90013214adfSAndy Whitcroft 90113214adfSAndy Whitcroft return ($statement, $condition, 90213214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 90313214adfSAndy Whitcroft} 90413214adfSAndy Whitcroft 905cf655043SAndy Whitcroftsub statement_lines { 906cf655043SAndy Whitcroft my ($stmt) = @_; 907cf655043SAndy Whitcroft 908cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 909cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 910cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 911cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 912cf655043SAndy Whitcroft 913cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 914cf655043SAndy Whitcroft 915cf655043SAndy Whitcroft return $#stmt_lines + 2; 916cf655043SAndy Whitcroft} 917cf655043SAndy Whitcroft 918cf655043SAndy Whitcroftsub statement_rawlines { 919cf655043SAndy Whitcroft my ($stmt) = @_; 920cf655043SAndy Whitcroft 921cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 922cf655043SAndy Whitcroft 923cf655043SAndy Whitcroft return $#stmt_lines + 2; 924cf655043SAndy Whitcroft} 925cf655043SAndy Whitcroft 926cf655043SAndy Whitcroftsub statement_block_size { 927cf655043SAndy Whitcroft my ($stmt) = @_; 928cf655043SAndy Whitcroft 929cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 930cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 931cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 932cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 933cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 934cf655043SAndy Whitcroft 935cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 936cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 937cf655043SAndy Whitcroft 938cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 939cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 940cf655043SAndy Whitcroft 941cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 942cf655043SAndy Whitcroft return $stmt_lines; 943cf655043SAndy Whitcroft } else { 944cf655043SAndy Whitcroft return $stmt_statements; 945cf655043SAndy Whitcroft } 946cf655043SAndy Whitcroft} 947cf655043SAndy Whitcroft 94813214adfSAndy Whitcroftsub ctx_statement_full { 94913214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 95013214adfSAndy Whitcroft my ($statement, $condition, $level); 95113214adfSAndy Whitcroft 95213214adfSAndy Whitcroft my (@chunks); 95313214adfSAndy Whitcroft 954cf655043SAndy Whitcroft # Grab the first conditional/block pair. 95513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 95613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 957773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 95813214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 959cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 960cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 961cf655043SAndy Whitcroft } 962cf655043SAndy Whitcroft 963cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 964cf655043SAndy Whitcroft # could continue the statement. 965cf655043SAndy Whitcroft for (;;) { 96613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 96713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 968cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 969773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 970cf655043SAndy Whitcroft #print "C: push\n"; 971cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 97213214adfSAndy Whitcroft } 97313214adfSAndy Whitcroft 97413214adfSAndy Whitcroft return ($level, $linenr, @chunks); 9758905a67cSAndy Whitcroft} 9768905a67cSAndy Whitcroft 9774a0df2efSAndy Whitcroftsub ctx_block_get { 978f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 9794a0df2efSAndy Whitcroft my $line; 9804a0df2efSAndy Whitcroft my $start = $linenr - 1; 9814a0df2efSAndy Whitcroft my $blk = ''; 9824a0df2efSAndy Whitcroft my @o; 9834a0df2efSAndy Whitcroft my @c; 9844a0df2efSAndy Whitcroft my @res = (); 9854a0df2efSAndy Whitcroft 986f0a594c1SAndy Whitcroft my $level = 0; 9874635f4fbSAndy Whitcroft my @stack = ($level); 98800df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 98900df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 99000df344fSAndy Whitcroft $remain--; 99100df344fSAndy Whitcroft 99200df344fSAndy Whitcroft $blk .= $rawlines[$line]; 9934635f4fbSAndy Whitcroft 9944635f4fbSAndy Whitcroft # Handle nested #if/#else. 99501464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 9964635f4fbSAndy Whitcroft push(@stack, $level); 99701464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 9984635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 99901464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 10004635f4fbSAndy Whitcroft $level = pop(@stack); 10014635f4fbSAndy Whitcroft } 10024635f4fbSAndy Whitcroft 100301464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1004f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1005f0a594c1SAndy Whitcroft if ($off > 0) { 1006f0a594c1SAndy Whitcroft $off--; 1007f0a594c1SAndy Whitcroft next; 1008f0a594c1SAndy Whitcroft } 10094a0df2efSAndy Whitcroft 1010f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1011f0a594c1SAndy Whitcroft $level--; 1012f0a594c1SAndy Whitcroft last if ($level == 0); 1013f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1014f0a594c1SAndy Whitcroft $level++; 1015f0a594c1SAndy Whitcroft } 1016f0a594c1SAndy Whitcroft } 10174a0df2efSAndy Whitcroft 1018f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 101900df344fSAndy Whitcroft push(@res, $rawlines[$line]); 10204a0df2efSAndy Whitcroft } 10214a0df2efSAndy Whitcroft 1022f0a594c1SAndy Whitcroft last if ($level == 0); 10234a0df2efSAndy Whitcroft } 10244a0df2efSAndy Whitcroft 1025f0a594c1SAndy Whitcroft return ($level, @res); 10264a0df2efSAndy Whitcroft} 10274a0df2efSAndy Whitcroftsub ctx_block_outer { 10284a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10294a0df2efSAndy Whitcroft 1030f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1031f0a594c1SAndy Whitcroft return @r; 10324a0df2efSAndy Whitcroft} 10334a0df2efSAndy Whitcroftsub ctx_block { 10344a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10354a0df2efSAndy Whitcroft 1036f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1037f0a594c1SAndy Whitcroft return @r; 1038653d4876SAndy Whitcroft} 1039653d4876SAndy Whitcroftsub ctx_statement { 1040f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1041f0a594c1SAndy Whitcroft 1042f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1043f0a594c1SAndy Whitcroft return @r; 1044f0a594c1SAndy Whitcroft} 1045f0a594c1SAndy Whitcroftsub ctx_block_level { 1046653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1047653d4876SAndy Whitcroft 1048f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 10494a0df2efSAndy Whitcroft} 10509c0ca6f9SAndy Whitcroftsub ctx_statement_level { 10519c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 10529c0ca6f9SAndy Whitcroft 10539c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 10549c0ca6f9SAndy Whitcroft} 10554a0df2efSAndy Whitcroft 10564a0df2efSAndy Whitcroftsub ctx_locate_comment { 10574a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 10584a0df2efSAndy Whitcroft 10594a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1060beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 10614a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 10624a0df2efSAndy Whitcroft 10634a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 10644a0df2efSAndy Whitcroft # comment. 10654a0df2efSAndy Whitcroft my $in_comment = 0; 10664a0df2efSAndy Whitcroft $current_comment = ''; 10674a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 106800df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 106900df344fSAndy Whitcroft #warn " $line\n"; 10704a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 10714a0df2efSAndy Whitcroft $in_comment = 1; 10724a0df2efSAndy Whitcroft } 10734a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 10744a0df2efSAndy Whitcroft $in_comment = 1; 10754a0df2efSAndy Whitcroft } 10764a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 10774a0df2efSAndy Whitcroft $current_comment = ''; 10784a0df2efSAndy Whitcroft } 10794a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 10804a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 10814a0df2efSAndy Whitcroft $in_comment = 0; 10824a0df2efSAndy Whitcroft } 10834a0df2efSAndy Whitcroft } 10844a0df2efSAndy Whitcroft 10854a0df2efSAndy Whitcroft chomp($current_comment); 10864a0df2efSAndy Whitcroft return($current_comment); 10874a0df2efSAndy Whitcroft} 10884a0df2efSAndy Whitcroftsub ctx_has_comment { 10894a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 10904a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 10914a0df2efSAndy Whitcroft 109200df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 10934a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 10944a0df2efSAndy Whitcroft 10954a0df2efSAndy Whitcroft return ($cmt ne ''); 10964a0df2efSAndy Whitcroft} 10974a0df2efSAndy Whitcroft 10984d001e4dSAndy Whitcroftsub raw_line { 10994d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 11004d001e4dSAndy Whitcroft 11014d001e4dSAndy Whitcroft my $offset = $linenr - 1; 11024d001e4dSAndy Whitcroft $cnt++; 11034d001e4dSAndy Whitcroft 11044d001e4dSAndy Whitcroft my $line; 11054d001e4dSAndy Whitcroft while ($cnt) { 11064d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 11074d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 11084d001e4dSAndy Whitcroft $cnt--; 11094d001e4dSAndy Whitcroft } 11104d001e4dSAndy Whitcroft 11114d001e4dSAndy Whitcroft return $line; 11124d001e4dSAndy Whitcroft} 11134d001e4dSAndy Whitcroft 11140a920b5bSAndy Whitcroftsub cat_vet { 11150a920b5bSAndy Whitcroft my ($vet) = @_; 11169c0ca6f9SAndy Whitcroft my ($res, $coded); 11170a920b5bSAndy Whitcroft 11189c0ca6f9SAndy Whitcroft $res = ''; 11196c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 11206c72ffaaSAndy Whitcroft $res .= $1; 11216c72ffaaSAndy Whitcroft if ($2 ne '') { 11229c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 11236c72ffaaSAndy Whitcroft $res .= $coded; 11246c72ffaaSAndy Whitcroft } 11259c0ca6f9SAndy Whitcroft } 11269c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 11270a920b5bSAndy Whitcroft 11289c0ca6f9SAndy Whitcroft return $res; 11290a920b5bSAndy Whitcroft} 11300a920b5bSAndy Whitcroft 1131c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1132cf655043SAndy Whitcroftmy $av_pending; 1133c2fdda0dSAndy Whitcroftmy @av_paren_type; 11341f65f947SAndy Whitcroftmy $av_pend_colon; 1135c2fdda0dSAndy Whitcroft 1136c2fdda0dSAndy Whitcroftsub annotate_reset { 1137c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1138cf655043SAndy Whitcroft $av_pending = '_'; 1139cf655043SAndy Whitcroft @av_paren_type = ('E'); 11401f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1141c2fdda0dSAndy Whitcroft} 1142c2fdda0dSAndy Whitcroft 11436c72ffaaSAndy Whitcroftsub annotate_values { 11446c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 11456c72ffaaSAndy Whitcroft 11466c72ffaaSAndy Whitcroft my $res; 11471f65f947SAndy Whitcroft my $var = '_' x length($stream); 11486c72ffaaSAndy Whitcroft my $cur = $stream; 11496c72ffaaSAndy Whitcroft 1150c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 11516c72ffaaSAndy Whitcroft 11526c72ffaaSAndy Whitcroft while (length($cur)) { 1153773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1154cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1155171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 11566c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1157c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1158c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1159cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1160c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 11616c72ffaaSAndy Whitcroft } 11626c72ffaaSAndy Whitcroft 1163c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 11649446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 11659446ef56SAndy Whitcroft push(@av_paren_type, $type); 1166addcdceaSAndy Whitcroft $type = 'c'; 11679446ef56SAndy Whitcroft 1168e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1169c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 11706c72ffaaSAndy Whitcroft $type = 'T'; 11716c72ffaaSAndy Whitcroft 1172389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1173389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1174389a2fe5SAndy Whitcroft $type = 'T'; 1175389a2fe5SAndy Whitcroft 1176c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1177171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1178c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1179171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1180171ae1a4SAndy Whitcroft if ($2 ne '') { 1181cf655043SAndy Whitcroft $av_pending = 'N'; 1182171ae1a4SAndy Whitcroft } 1183171ae1a4SAndy Whitcroft $type = 'E'; 1184171ae1a4SAndy Whitcroft 1185c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1186171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1187171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1188171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 11896c72ffaaSAndy Whitcroft 1190c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1191cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1192c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1193cf655043SAndy Whitcroft 1194cf655043SAndy Whitcroft push(@av_paren_type, $type); 1195cf655043SAndy Whitcroft push(@av_paren_type, $type); 1196171ae1a4SAndy Whitcroft $type = 'E'; 1197cf655043SAndy Whitcroft 1198c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1199cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1200cf655043SAndy Whitcroft $av_preprocessor = 1; 1201cf655043SAndy Whitcroft 1202cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1203cf655043SAndy Whitcroft 1204171ae1a4SAndy Whitcroft $type = 'E'; 1205cf655043SAndy Whitcroft 1206c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1207cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1208cf655043SAndy Whitcroft 1209cf655043SAndy Whitcroft $av_preprocessor = 1; 1210cf655043SAndy Whitcroft 1211cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1212cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1213cf655043SAndy Whitcroft pop(@av_paren_type); 1214cf655043SAndy Whitcroft push(@av_paren_type, $type); 1215171ae1a4SAndy Whitcroft $type = 'E'; 12166c72ffaaSAndy Whitcroft 12176c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1218c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 12196c72ffaaSAndy Whitcroft 1220171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1221171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1222171ae1a4SAndy Whitcroft $av_pending = $type; 1223171ae1a4SAndy Whitcroft $type = 'N'; 1224171ae1a4SAndy Whitcroft 12256c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1226c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 12276c72ffaaSAndy Whitcroft if (defined $2) { 1228cf655043SAndy Whitcroft $av_pending = 'V'; 12296c72ffaaSAndy Whitcroft } 12306c72ffaaSAndy Whitcroft $type = 'N'; 12316c72ffaaSAndy Whitcroft 123214b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1233c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 123414b111c1SAndy Whitcroft $av_pending = 'E'; 12356c72ffaaSAndy Whitcroft $type = 'N'; 12366c72ffaaSAndy Whitcroft 12371f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 12381f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 12391f65f947SAndy Whitcroft $av_pend_colon = 'C'; 12401f65f947SAndy Whitcroft $type = 'N'; 12411f65f947SAndy Whitcroft 124214b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1243c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 12446c72ffaaSAndy Whitcroft $type = 'N'; 12456c72ffaaSAndy Whitcroft 12466c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1247c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1248cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1249cf655043SAndy Whitcroft $av_pending = '_'; 12506c72ffaaSAndy Whitcroft $type = 'N'; 12516c72ffaaSAndy Whitcroft 12526c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1253cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1254cf655043SAndy Whitcroft if ($new_type ne '_') { 1255cf655043SAndy Whitcroft $type = $new_type; 1256c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1257c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 12586c72ffaaSAndy Whitcroft } else { 1259c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 12606c72ffaaSAndy Whitcroft } 12616c72ffaaSAndy Whitcroft 1262c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1263c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1264c8cb2ca3SAndy Whitcroft $type = 'V'; 1265cf655043SAndy Whitcroft $av_pending = 'V'; 12666c72ffaaSAndy Whitcroft 12678e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 12688e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 12691f65f947SAndy Whitcroft $av_pend_colon = 'B'; 12708e761b04SAndy Whitcroft } elsif ($type eq 'E') { 12718e761b04SAndy Whitcroft $av_pend_colon = 'L'; 12721f65f947SAndy Whitcroft } 12731f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 12741f65f947SAndy Whitcroft $type = 'V'; 12751f65f947SAndy Whitcroft 12766c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1277c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 12786c72ffaaSAndy Whitcroft $type = 'V'; 12796c72ffaaSAndy Whitcroft 12806c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1281c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 12826c72ffaaSAndy Whitcroft $type = 'N'; 12836c72ffaaSAndy Whitcroft 1284cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1285c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 128613214adfSAndy Whitcroft $type = 'E'; 12871f65f947SAndy Whitcroft $av_pend_colon = 'O'; 128813214adfSAndy Whitcroft 12898e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 12908e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 12918e761b04SAndy Whitcroft $type = 'C'; 12928e761b04SAndy Whitcroft 12931f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 12941f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 12951f65f947SAndy Whitcroft $type = 'N'; 12961f65f947SAndy Whitcroft 12971f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 12981f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 12991f65f947SAndy Whitcroft 13001f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 13011f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 13021f65f947SAndy Whitcroft $type = 'E'; 13031f65f947SAndy Whitcroft } else { 13041f65f947SAndy Whitcroft $type = 'N'; 13051f65f947SAndy Whitcroft } 13061f65f947SAndy Whitcroft $av_pend_colon = 'O'; 13071f65f947SAndy Whitcroft 13088e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 130913214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 13106c72ffaaSAndy Whitcroft $type = 'N'; 13116c72ffaaSAndy Whitcroft 13120d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 131374048ed8SAndy Whitcroft my $variant; 131474048ed8SAndy Whitcroft 131574048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 131674048ed8SAndy Whitcroft if ($type eq 'V') { 131774048ed8SAndy Whitcroft $variant = 'B'; 131874048ed8SAndy Whitcroft } else { 131974048ed8SAndy Whitcroft $variant = 'U'; 132074048ed8SAndy Whitcroft } 132174048ed8SAndy Whitcroft 132274048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 132374048ed8SAndy Whitcroft $type = 'N'; 132474048ed8SAndy Whitcroft 13256c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1326c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 13276c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 13286c72ffaaSAndy Whitcroft $type = 'N'; 13296c72ffaaSAndy Whitcroft } 13306c72ffaaSAndy Whitcroft 13316c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1332c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 13336c72ffaaSAndy Whitcroft } 13346c72ffaaSAndy Whitcroft if (defined $1) { 13356c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 13366c72ffaaSAndy Whitcroft $res .= $type x length($1); 13376c72ffaaSAndy Whitcroft } 13386c72ffaaSAndy Whitcroft } 13396c72ffaaSAndy Whitcroft 13401f65f947SAndy Whitcroft return ($res, $var); 13416c72ffaaSAndy Whitcroft} 13426c72ffaaSAndy Whitcroft 13438905a67cSAndy Whitcroftsub possible { 134413214adfSAndy Whitcroft my ($possible, $line) = @_; 13459a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 13460776e594SAndy Whitcroft ^(?: 13470776e594SAndy Whitcroft $Modifier| 13480776e594SAndy Whitcroft $Storage| 13490776e594SAndy Whitcroft $Type| 13509a974fdbSAndy Whitcroft DEFINE_\S+ 13519a974fdbSAndy Whitcroft )$| 13529a974fdbSAndy Whitcroft ^(?: 13530776e594SAndy Whitcroft goto| 13540776e594SAndy Whitcroft return| 13550776e594SAndy Whitcroft case| 13560776e594SAndy Whitcroft else| 13570776e594SAndy Whitcroft asm|__asm__| 135889a88353SAndy Whitcroft do| 135989a88353SAndy Whitcroft \#| 136089a88353SAndy Whitcroft \#\#| 13619a974fdbSAndy Whitcroft )(?:\s|$)| 13620776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 13639a974fdbSAndy Whitcroft )}x; 13649a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 13659a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1366c45dcabdSAndy Whitcroft # Check for modifiers. 1367c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1368c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1369c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1370c45dcabdSAndy Whitcroft 1371c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1372c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1373d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 13749a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1375d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1376d2506586SAndy Whitcroft push(@modifierList, $modifier); 1377d2506586SAndy Whitcroft } 13789a974fdbSAndy Whitcroft } 1379c45dcabdSAndy Whitcroft 1380c45dcabdSAndy Whitcroft } else { 138113214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 13828905a67cSAndy Whitcroft push(@typeList, $possible); 1383c45dcabdSAndy Whitcroft } 13848905a67cSAndy Whitcroft build_types(); 13850776e594SAndy Whitcroft } else { 13860776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 13878905a67cSAndy Whitcroft } 13888905a67cSAndy Whitcroft} 13898905a67cSAndy Whitcroft 13906c72ffaaSAndy Whitcroftmy $prefix = ''; 13916c72ffaaSAndy Whitcroft 1392000d1cc1SJoe Perchessub show_type { 139391bfe484SJoe Perches return defined $use_type{$_[0]} if (scalar keys %use_type > 0); 139491bfe484SJoe Perches 1395000d1cc1SJoe Perches return !defined $ignore_type{$_[0]}; 1396000d1cc1SJoe Perches} 1397000d1cc1SJoe Perches 1398f0a594c1SAndy Whitcroftsub report { 1399000d1cc1SJoe Perches if (!show_type($_[1]) || 1400000d1cc1SJoe Perches (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { 1401773647a0SAndy Whitcroft return 0; 1402773647a0SAndy Whitcroft } 1403000d1cc1SJoe Perches my $line; 1404000d1cc1SJoe Perches if ($show_types) { 1405000d1cc1SJoe Perches $line = "$prefix$_[0]:$_[1]: $_[2]\n"; 1406000d1cc1SJoe Perches } else { 1407000d1cc1SJoe Perches $line = "$prefix$_[0]: $_[2]\n"; 1408000d1cc1SJoe Perches } 14098905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 14108905a67cSAndy Whitcroft 141113214adfSAndy Whitcroft push(our @report, $line); 1412773647a0SAndy Whitcroft 1413773647a0SAndy Whitcroft return 1; 1414f0a594c1SAndy Whitcroft} 1415f0a594c1SAndy Whitcroftsub report_dump { 141613214adfSAndy Whitcroft our @report; 1417f0a594c1SAndy Whitcroft} 1418000d1cc1SJoe Perches 1419de7d4f0eSAndy Whitcroftsub ERROR { 1420000d1cc1SJoe Perches if (report("ERROR", $_[0], $_[1])) { 1421de7d4f0eSAndy Whitcroft our $clean = 0; 14226c72ffaaSAndy Whitcroft our $cnt_error++; 14233705ce5bSJoe Perches return 1; 1424de7d4f0eSAndy Whitcroft } 14253705ce5bSJoe Perches return 0; 1426773647a0SAndy Whitcroft} 1427de7d4f0eSAndy Whitcroftsub WARN { 1428000d1cc1SJoe Perches if (report("WARNING", $_[0], $_[1])) { 1429de7d4f0eSAndy Whitcroft our $clean = 0; 14306c72ffaaSAndy Whitcroft our $cnt_warn++; 14313705ce5bSJoe Perches return 1; 1432de7d4f0eSAndy Whitcroft } 14333705ce5bSJoe Perches return 0; 1434773647a0SAndy Whitcroft} 1435de7d4f0eSAndy Whitcroftsub CHK { 1436000d1cc1SJoe Perches if ($check && report("CHECK", $_[0], $_[1])) { 1437de7d4f0eSAndy Whitcroft our $clean = 0; 14386c72ffaaSAndy Whitcroft our $cnt_chk++; 14393705ce5bSJoe Perches return 1; 14406c72ffaaSAndy Whitcroft } 14413705ce5bSJoe Perches return 0; 1442de7d4f0eSAndy Whitcroft} 1443de7d4f0eSAndy Whitcroft 14446ecd9674SAndy Whitcroftsub check_absolute_file { 14456ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 14466ecd9674SAndy Whitcroft my $file = $absolute; 14476ecd9674SAndy Whitcroft 14486ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 14496ecd9674SAndy Whitcroft 14506ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 14516ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 14526ecd9674SAndy Whitcroft if (-f "$root/$file") { 14536ecd9674SAndy Whitcroft ##print "file<$file>\n"; 14546ecd9674SAndy Whitcroft last; 14556ecd9674SAndy Whitcroft } 14566ecd9674SAndy Whitcroft } 14576ecd9674SAndy Whitcroft if (! -f _) { 14586ecd9674SAndy Whitcroft return 0; 14596ecd9674SAndy Whitcroft } 14606ecd9674SAndy Whitcroft 14616ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 14626ecd9674SAndy Whitcroft my $prefix = $absolute; 14636ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 14646ecd9674SAndy Whitcroft 14656ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 14666ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1467000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1468000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 14696ecd9674SAndy Whitcroft } 14706ecd9674SAndy Whitcroft} 14716ecd9674SAndy Whitcroft 14723705ce5bSJoe Perchessub trim { 14733705ce5bSJoe Perches my ($string) = @_; 14743705ce5bSJoe Perches 1475b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1476b34c648bSJoe Perches 1477b34c648bSJoe Perches return $string; 1478b34c648bSJoe Perches} 1479b34c648bSJoe Perches 1480b34c648bSJoe Perchessub ltrim { 1481b34c648bSJoe Perches my ($string) = @_; 1482b34c648bSJoe Perches 1483b34c648bSJoe Perches $string =~ s/^\s+//; 1484b34c648bSJoe Perches 1485b34c648bSJoe Perches return $string; 1486b34c648bSJoe Perches} 1487b34c648bSJoe Perches 1488b34c648bSJoe Perchessub rtrim { 1489b34c648bSJoe Perches my ($string) = @_; 1490b34c648bSJoe Perches 1491b34c648bSJoe Perches $string =~ s/\s+$//; 14923705ce5bSJoe Perches 14933705ce5bSJoe Perches return $string; 14943705ce5bSJoe Perches} 14953705ce5bSJoe Perches 14963705ce5bSJoe Perchessub tabify { 14973705ce5bSJoe Perches my ($leading) = @_; 14983705ce5bSJoe Perches 14993705ce5bSJoe Perches my $source_indent = 8; 15003705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 15013705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 15023705ce5bSJoe Perches 15033705ce5bSJoe Perches #convert leading spaces to tabs 15043705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 15053705ce5bSJoe Perches #Remove spaces before a tab 15063705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 15073705ce5bSJoe Perches 15083705ce5bSJoe Perches return "$leading"; 15093705ce5bSJoe Perches} 15103705ce5bSJoe Perches 1511d1fe9c09SJoe Perchessub pos_last_openparen { 1512d1fe9c09SJoe Perches my ($line) = @_; 1513d1fe9c09SJoe Perches 1514d1fe9c09SJoe Perches my $pos = 0; 1515d1fe9c09SJoe Perches 1516d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1517d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1518d1fe9c09SJoe Perches 1519d1fe9c09SJoe Perches my $last_openparen = 0; 1520d1fe9c09SJoe Perches 1521d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1522d1fe9c09SJoe Perches return -1; 1523d1fe9c09SJoe Perches } 1524d1fe9c09SJoe Perches 1525d1fe9c09SJoe Perches my $len = length($line); 1526d1fe9c09SJoe Perches 1527d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1528d1fe9c09SJoe Perches my $string = substr($line, $pos); 1529d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1530d1fe9c09SJoe Perches $pos += length($1) - 1; 1531d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1532d1fe9c09SJoe Perches $last_openparen = $pos; 1533d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1534d1fe9c09SJoe Perches last; 1535d1fe9c09SJoe Perches } 1536d1fe9c09SJoe Perches } 1537d1fe9c09SJoe Perches 1538d1fe9c09SJoe Perches return $last_openparen + 1; 1539d1fe9c09SJoe Perches} 1540d1fe9c09SJoe Perches 15410a920b5bSAndy Whitcroftsub process { 15420a920b5bSAndy Whitcroft my $filename = shift; 15430a920b5bSAndy Whitcroft 15440a920b5bSAndy Whitcroft my $linenr=0; 15450a920b5bSAndy Whitcroft my $prevline=""; 1546c2fdda0dSAndy Whitcroft my $prevrawline=""; 15470a920b5bSAndy Whitcroft my $stashline=""; 1548c2fdda0dSAndy Whitcroft my $stashrawline=""; 15490a920b5bSAndy Whitcroft 15504a0df2efSAndy Whitcroft my $length; 15510a920b5bSAndy Whitcroft my $indent; 15520a920b5bSAndy Whitcroft my $previndent=0; 15530a920b5bSAndy Whitcroft my $stashindent=0; 15540a920b5bSAndy Whitcroft 1555de7d4f0eSAndy Whitcroft our $clean = 1; 15560a920b5bSAndy Whitcroft my $signoff = 0; 15570a920b5bSAndy Whitcroft my $is_patch = 0; 15580a920b5bSAndy Whitcroft 155915662b3eSJoe Perches my $in_header_lines = 1; 156015662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 156115662b3eSJoe Perches 1562fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1563fa64205dSPasi Savanainen 156413214adfSAndy Whitcroft our @report = (); 15656c72ffaaSAndy Whitcroft our $cnt_lines = 0; 15666c72ffaaSAndy Whitcroft our $cnt_error = 0; 15676c72ffaaSAndy Whitcroft our $cnt_warn = 0; 15686c72ffaaSAndy Whitcroft our $cnt_chk = 0; 15696c72ffaaSAndy Whitcroft 15700a920b5bSAndy Whitcroft # Trace the real file/line as we go. 15710a920b5bSAndy Whitcroft my $realfile = ''; 15720a920b5bSAndy Whitcroft my $realline = 0; 15730a920b5bSAndy Whitcroft my $realcnt = 0; 15740a920b5bSAndy Whitcroft my $here = ''; 15750a920b5bSAndy Whitcroft my $in_comment = 0; 1576c2fdda0dSAndy Whitcroft my $comment_edge = 0; 15770a920b5bSAndy Whitcroft my $first_line = 0; 15781e855726SWolfram Sang my $p1_prefix = ''; 15790a920b5bSAndy Whitcroft 158013214adfSAndy Whitcroft my $prev_values = 'E'; 158113214adfSAndy Whitcroft 158213214adfSAndy Whitcroft # suppression flags 1583773647a0SAndy Whitcroft my %suppress_ifbraces; 1584170d3a22SAndy Whitcroft my %suppress_whiletrailers; 15852b474a1aSAndy Whitcroft my %suppress_export; 15863e469cdcSAndy Whitcroft my $suppress_statement = 0; 1587653d4876SAndy Whitcroft 15887e51f197SJoe Perches my %signatures = (); 1589323c1260SJoe Perches 1590c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1591de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1592c2fdda0dSAndy Whitcroft # 1593de7d4f0eSAndy Whitcroft my @setup_docs = (); 1594de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1595773647a0SAndy Whitcroft 1596773647a0SAndy Whitcroft sanitise_line_reset(); 1597c2fdda0dSAndy Whitcroft my $line; 1598c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1599773647a0SAndy Whitcroft $linenr++; 1600773647a0SAndy Whitcroft $line = $rawline; 1601c2fdda0dSAndy Whitcroft 16023705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 16033705ce5bSJoe Perches 1604773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1605de7d4f0eSAndy Whitcroft $setup_docs = 0; 1606de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1607de7d4f0eSAndy Whitcroft $setup_docs = 1; 1608de7d4f0eSAndy Whitcroft } 1609773647a0SAndy Whitcroft #next; 1610de7d4f0eSAndy Whitcroft } 1611773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1612773647a0SAndy Whitcroft $realline=$1-1; 1613773647a0SAndy Whitcroft if (defined $2) { 1614773647a0SAndy Whitcroft $realcnt=$3+1; 1615773647a0SAndy Whitcroft } else { 1616773647a0SAndy Whitcroft $realcnt=1+1; 1617773647a0SAndy Whitcroft } 1618c45dcabdSAndy Whitcroft $in_comment = 0; 1619773647a0SAndy Whitcroft 1620773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1621773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1622773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1623773647a0SAndy Whitcroft # at context start. 1624773647a0SAndy Whitcroft my $edge; 162501fa9147SAndy Whitcroft my $cnt = $realcnt; 162601fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 162701fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 162801fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 162901fa9147SAndy Whitcroft $cnt--; 163001fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1631721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1632fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1633fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1634fae17daeSAndy Whitcroft ($edge) = $1; 1635fae17daeSAndy Whitcroft last; 1636fae17daeSAndy Whitcroft } 1637773647a0SAndy Whitcroft } 1638773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1639773647a0SAndy Whitcroft $in_comment = 1; 1640773647a0SAndy Whitcroft } 1641773647a0SAndy Whitcroft 1642773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1643773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1644773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1645773647a0SAndy Whitcroft if (!defined $edge && 164683242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1647773647a0SAndy Whitcroft { 1648773647a0SAndy Whitcroft $in_comment = 1; 1649773647a0SAndy Whitcroft } 1650773647a0SAndy Whitcroft 1651773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1652773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1653773647a0SAndy Whitcroft 1654171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1655773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1656171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1657773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1658773647a0SAndy Whitcroft } 1659773647a0SAndy Whitcroft push(@lines, $line); 1660773647a0SAndy Whitcroft 1661773647a0SAndy Whitcroft if ($realcnt > 1) { 1662773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1663773647a0SAndy Whitcroft } else { 1664773647a0SAndy Whitcroft $realcnt = 0; 1665773647a0SAndy Whitcroft } 1666773647a0SAndy Whitcroft 1667773647a0SAndy Whitcroft #print "==>$rawline\n"; 1668773647a0SAndy Whitcroft #print "-->$line\n"; 1669de7d4f0eSAndy Whitcroft 1670de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1671de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1672de7d4f0eSAndy Whitcroft } 1673de7d4f0eSAndy Whitcroft } 1674de7d4f0eSAndy Whitcroft 16756c72ffaaSAndy Whitcroft $prefix = ''; 16766c72ffaaSAndy Whitcroft 1677773647a0SAndy Whitcroft $realcnt = 0; 1678773647a0SAndy Whitcroft $linenr = 0; 16790a920b5bSAndy Whitcroft foreach my $line (@lines) { 16800a920b5bSAndy Whitcroft $linenr++; 16811b5539b1SJoe Perches my $sline = $line; #copy of $line 16821b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 16830a920b5bSAndy Whitcroft 1684c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 16856c72ffaaSAndy Whitcroft 16860a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 16876c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 16880a920b5bSAndy Whitcroft $is_patch = 1; 16894a0df2efSAndy Whitcroft $first_line = $linenr + 1; 16900a920b5bSAndy Whitcroft $realline=$1-1; 16910a920b5bSAndy Whitcroft if (defined $2) { 16920a920b5bSAndy Whitcroft $realcnt=$3+1; 16930a920b5bSAndy Whitcroft } else { 16940a920b5bSAndy Whitcroft $realcnt=1+1; 16950a920b5bSAndy Whitcroft } 1696c2fdda0dSAndy Whitcroft annotate_reset(); 169713214adfSAndy Whitcroft $prev_values = 'E'; 169813214adfSAndy Whitcroft 1699773647a0SAndy Whitcroft %suppress_ifbraces = (); 1700170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 17012b474a1aSAndy Whitcroft %suppress_export = (); 17023e469cdcSAndy Whitcroft $suppress_statement = 0; 17030a920b5bSAndy Whitcroft next; 17040a920b5bSAndy Whitcroft 17054a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 17064a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 17074a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1708773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 17090a920b5bSAndy Whitcroft $realline++; 1710d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 17110a920b5bSAndy Whitcroft 17124a0df2efSAndy Whitcroft # Measure the line length and indent. 1713c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 17140a920b5bSAndy Whitcroft 17150a920b5bSAndy Whitcroft # Track the previous line. 17160a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 17170a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1718c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1719c2fdda0dSAndy Whitcroft 1720773647a0SAndy Whitcroft #warn "line<$line>\n"; 17216c72ffaaSAndy Whitcroft 1722d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1723d8aaf121SAndy Whitcroft $realcnt--; 17240a920b5bSAndy Whitcroft } 17250a920b5bSAndy Whitcroft 1726cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1727cc77cdcaSAndy Whitcroft 17280a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1729773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1730773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1731773647a0SAndy Whitcroft 17326c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 17336c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1734773647a0SAndy Whitcroft 1735773647a0SAndy Whitcroft # extract the filename as it passes 17363bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 17373bf9a009SRabin Vincent $realfile = $1; 17383bf9a009SRabin Vincent $realfile =~ s@^([^/]*)/@@; 1739270c49a0SJoe Perches $in_commit_log = 0; 17403bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1741773647a0SAndy Whitcroft $realfile = $1; 17421e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 1743270c49a0SJoe Perches $in_commit_log = 0; 17441e855726SWolfram Sang 17451e855726SWolfram Sang $p1_prefix = $1; 1746e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1747e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 1748000d1cc1SJoe Perches WARN("PATCH_PREFIX", 1749000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 17501e855726SWolfram Sang } 1751773647a0SAndy Whitcroft 1752c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1753000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 1754000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1755773647a0SAndy Whitcroft } 1756773647a0SAndy Whitcroft next; 1757773647a0SAndy Whitcroft } 1758773647a0SAndy Whitcroft 1759389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 17600a920b5bSAndy Whitcroft 1761c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1762c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1763c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 17640a920b5bSAndy Whitcroft 17656c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 17666c72ffaaSAndy Whitcroft 17673bf9a009SRabin Vincent# Check for incorrect file permissions 17683bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 17693bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 177004db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 177104db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 1772000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 1773000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 17743bf9a009SRabin Vincent } 17753bf9a009SRabin Vincent } 17763bf9a009SRabin Vincent 177720112475SJoe Perches# Check the patch for a signoff: 1778d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 17794a0df2efSAndy Whitcroft $signoff++; 178015662b3eSJoe Perches $in_commit_log = 0; 17810a920b5bSAndy Whitcroft } 178220112475SJoe Perches 178320112475SJoe Perches# Check signature styles 1784270c49a0SJoe Perches if (!$in_header_lines && 1785ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 178620112475SJoe Perches my $space_before = $1; 178720112475SJoe Perches my $sign_off = $2; 178820112475SJoe Perches my $space_after = $3; 178920112475SJoe Perches my $email = $4; 179020112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 179120112475SJoe Perches 1792ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 1793ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 1794ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 1795ce0338dfSJoe Perches } 179620112475SJoe Perches if (defined $space_before && $space_before ne "") { 17973705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 17983705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 17993705ce5bSJoe Perches $fix) { 18003705ce5bSJoe Perches $fixed[$linenr - 1] = 18013705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18023705ce5bSJoe Perches } 180320112475SJoe Perches } 180420112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 18053705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18063705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 18073705ce5bSJoe Perches $fix) { 18083705ce5bSJoe Perches $fixed[$linenr - 1] = 18093705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18103705ce5bSJoe Perches } 18113705ce5bSJoe Perches 181220112475SJoe Perches } 181320112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 18143705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18153705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 18163705ce5bSJoe Perches $fix) { 18173705ce5bSJoe Perches $fixed[$linenr - 1] = 18183705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18193705ce5bSJoe Perches } 182020112475SJoe Perches } 182120112475SJoe Perches 182220112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 182320112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 182420112475SJoe Perches if ($suggested_email eq "") { 1825000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 1826000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 182720112475SJoe Perches } else { 182820112475SJoe Perches my $dequoted = $suggested_email; 182920112475SJoe Perches $dequoted =~ s/^"//; 183020112475SJoe Perches $dequoted =~ s/" </ </; 183120112475SJoe Perches # Don't force email to have quotes 183220112475SJoe Perches # Allow just an angle bracketed address 183320112475SJoe Perches if ("$dequoted$comment" ne $email && 183420112475SJoe Perches "<$email_address>$comment" ne $email && 183520112475SJoe Perches "$suggested_email$comment" ne $email) { 1836000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1837000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 183820112475SJoe Perches } 18390a920b5bSAndy Whitcroft } 18407e51f197SJoe Perches 18417e51f197SJoe Perches# Check for duplicate signatures 18427e51f197SJoe Perches my $sig_nospace = $line; 18437e51f197SJoe Perches $sig_nospace =~ s/\s//g; 18447e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 18457e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 18467e51f197SJoe Perches WARN("BAD_SIGN_OFF", 18477e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 18487e51f197SJoe Perches } else { 18497e51f197SJoe Perches $signatures{$sig_nospace} = 1; 18507e51f197SJoe Perches } 18510a920b5bSAndy Whitcroft } 18520a920b5bSAndy Whitcroft 185300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 18548905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1855000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 1856000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 18576c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1858de7d4f0eSAndy Whitcroft } 1859de7d4f0eSAndy Whitcroft 18606ecd9674SAndy Whitcroft# Check for absolute kernel paths. 18616ecd9674SAndy Whitcroft if ($tree) { 18626ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 18636ecd9674SAndy Whitcroft my $file = $1; 18646ecd9674SAndy Whitcroft 18656ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 18666ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 18676ecd9674SAndy Whitcroft # 18686ecd9674SAndy Whitcroft } else { 18696ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 18706ecd9674SAndy Whitcroft } 18716ecd9674SAndy Whitcroft } 18726ecd9674SAndy Whitcroft } 18736ecd9674SAndy Whitcroft 1874de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1875de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1876171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1877171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1878171ae1a4SAndy Whitcroft 1879171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1880171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1881171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1882171ae1a4SAndy Whitcroft 188334d99219SJoe Perches CHK("INVALID_UTF8", 1884000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 188500df344fSAndy Whitcroft } 18860a920b5bSAndy Whitcroft 188715662b3eSJoe Perches# Check if it's the start of a commit log 188815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 188915662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 1890270c49a0SJoe Perches $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { 189115662b3eSJoe Perches $in_header_lines = 0; 189215662b3eSJoe Perches $in_commit_log = 1; 189315662b3eSJoe Perches } 189415662b3eSJoe Perches 1895fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 1896fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 1897fa64205dSPasi Savanainen if ($in_header_lines && 1898fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 1899fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 1900fa64205dSPasi Savanainen $non_utf8_charset = 1; 1901fa64205dSPasi Savanainen } 1902fa64205dSPasi Savanainen 1903fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 190415662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 1905fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 190615662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 190715662b3eSJoe Perches } 190815662b3eSJoe Perches 190930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 191030670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 191100df344fSAndy Whitcroft 19120a920b5bSAndy Whitcroft#trailing whitespace 19139c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1914c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1915d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 1916d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 1917d5e616fcSJoe Perches $fix) { 1918d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/[\s\015]+$//; 1919d5e616fcSJoe Perches } 1920c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1921c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 19223705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 19233705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 19243705ce5bSJoe Perches $fix) { 1925d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 19263705ce5bSJoe Perches } 19273705ce5bSJoe Perches 1928d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 19290a920b5bSAndy Whitcroft } 19305368df20SAndy Whitcroft 19313354957aSAndi Kleen# check for Kconfig help text having a real description 19329fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 19339fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 19343354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 1935a1385803SAndy Whitcroft $line =~ /.\s*config\s+/) { 19363354957aSAndi Kleen my $length = 0; 19379fe287d7SAndy Whitcroft my $cnt = $realcnt; 19389fe287d7SAndy Whitcroft my $ln = $linenr + 1; 19399fe287d7SAndy Whitcroft my $f; 1940a1385803SAndy Whitcroft my $is_start = 0; 19419fe287d7SAndy Whitcroft my $is_end = 0; 1942a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 19439fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 19449fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 19459fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 19469fe287d7SAndy Whitcroft 19479fe287d7SAndy Whitcroft next if ($f =~ /^-/); 1948a1385803SAndy Whitcroft 1949a1385803SAndy Whitcroft if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { 1950a1385803SAndy Whitcroft $is_start = 1; 1951a1385803SAndy Whitcroft } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { 1952a1385803SAndy Whitcroft $length = -1; 1953a1385803SAndy Whitcroft } 1954a1385803SAndy Whitcroft 19559fe287d7SAndy Whitcroft $f =~ s/^.//; 19563354957aSAndi Kleen $f =~ s/#.*//; 19573354957aSAndi Kleen $f =~ s/^\s+//; 19583354957aSAndi Kleen next if ($f =~ /^$/); 19599fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 19609fe287d7SAndy Whitcroft $is_end = 1; 19619fe287d7SAndy Whitcroft last; 19629fe287d7SAndy Whitcroft } 19633354957aSAndi Kleen $length++; 19643354957aSAndi Kleen } 1965000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 1966a1385803SAndy Whitcroft "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 1967a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 19683354957aSAndi Kleen } 19693354957aSAndi Kleen 19701ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 19711ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 19721ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 19731ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 19741ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 19751ba8dfd1SKees Cook } 19761ba8dfd1SKees Cook 1977c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 1978c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 1979c68e5878SArnaud Lacombe my $flag = $1; 1980c68e5878SArnaud Lacombe my $replacement = { 1981c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 1982c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 1983c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 1984c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 1985c68e5878SArnaud Lacombe }; 1986c68e5878SArnaud Lacombe 1987c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 1988c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 1989c68e5878SArnaud Lacombe } 1990c68e5878SArnaud Lacombe 19915368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 19925368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 19935368df20SAndy Whitcroft 19946cd7f386SJoe Perches#line length limit 1995c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1996f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 19970fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 19988bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 19996cd7f386SJoe Perches $length > $max_line_length) 2000c45dcabdSAndy Whitcroft { 2001000d1cc1SJoe Perches WARN("LONG_LINE", 20026cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 20030a920b5bSAndy Whitcroft } 20040a920b5bSAndy Whitcroft 2005ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability 2006ca56dc09SJosh Triplett# to grep for the string. Limited to strings used as parameters (those 2007ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false 2008ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per 2009ca56dc09SJosh Triplett# line of the string. Make an exception when the previous string ends in a 2010ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline 2011ca56dc09SJosh Triplett# assembly to indent the instruction on the following line). 2012ca56dc09SJosh Triplett if ($line =~ /^\+\s*"/ && 2013ca56dc09SJosh Triplett $prevline =~ /"\s*$/ && 2014ca56dc09SJosh Triplett $prevline =~ /\(/ && 2015ca56dc09SJosh Triplett $prevrawline !~ /\\n(?:\\t)*"\s*$/) { 2016ca56dc09SJosh Triplett WARN("SPLIT_STRING", 2017ca56dc09SJosh Triplett "quoted string split across lines\n" . $hereprev); 2018ca56dc09SJosh Triplett } 2019ca56dc09SJosh Triplett 20205e79d96eSJoe Perches# check for spaces before a quoted newline 20215e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 20223705ce5bSJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 20233705ce5bSJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 20243705ce5bSJoe Perches $fix) { 20253705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 20263705ce5bSJoe Perches } 20273705ce5bSJoe Perches 20285e79d96eSJoe Perches } 20295e79d96eSJoe Perches 20308905a67cSAndy Whitcroft# check for adding lines without a newline. 20318905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2032000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2033000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 20348905a67cSAndy Whitcroft } 20358905a67cSAndy Whitcroft 203642e41c54SMike Frysinger# Blackfin: use hi/lo macros 203742e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 203842e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 203942e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2040000d1cc1SJoe Perches ERROR("LO_MACRO", 2041000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 204242e41c54SMike Frysinger } 204342e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 204442e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2045000d1cc1SJoe Perches ERROR("HI_MACRO", 2046000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 204742e41c54SMike Frysinger } 204842e41c54SMike Frysinger } 204942e41c54SMike Frysinger 2050b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2051b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 20520a920b5bSAndy Whitcroft 20530a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 20540a920b5bSAndy Whitcroft# more than 8 must use tabs. 2055c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2056c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2057c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2058d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 20593705ce5bSJoe Perches if (ERROR("CODE_INDENT", 20603705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 20613705ce5bSJoe Perches $fix) { 20623705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 20633705ce5bSJoe Perches } 20640a920b5bSAndy Whitcroft } 20650a920b5bSAndy Whitcroft 206608e44365SAlberto Panizzo# check for space before tabs. 206708e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 206808e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 20693705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 20703705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 20713705ce5bSJoe Perches $fix) { 20723705ce5bSJoe Perches $fixed[$linenr - 1] =~ 20733705ce5bSJoe Perches s/(^\+.*) +\t/$1\t/; 20743705ce5bSJoe Perches } 207508e44365SAlberto Panizzo } 207608e44365SAlberto Panizzo 2077d1fe9c09SJoe Perches# check for && or || at the start of a line 2078d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2079d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2080d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2081d1fe9c09SJoe Perches } 2082d1fe9c09SJoe Perches 2083d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2084d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 2085d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { 2086d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2087d1fe9c09SJoe Perches my $oldindent = $1; 2088d1fe9c09SJoe Perches my $rest = $2; 2089d1fe9c09SJoe Perches 2090d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2091d1fe9c09SJoe Perches if ($pos >= 0) { 2092b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2093b34a26f3SJoe Perches my $newindent = $2; 2094d1fe9c09SJoe Perches 2095d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2096d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2097d1fe9c09SJoe Perches " " x ($pos % 8); 2098d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2099d1fe9c09SJoe Perches 2100d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2101d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 21023705ce5bSJoe Perches 21033705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 21043705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 21053705ce5bSJoe Perches $fix && $line =~ /^\+/) { 21063705ce5bSJoe Perches $fixed[$linenr - 1] =~ 21073705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 21083705ce5bSJoe Perches } 2109d1fe9c09SJoe Perches } 2110d1fe9c09SJoe Perches } 2111d1fe9c09SJoe Perches } 2112d1fe9c09SJoe Perches 211323f780c9SJoe Perches if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { 21143705ce5bSJoe Perches if (CHK("SPACING", 21153705ce5bSJoe Perches "No space is necessary after a cast\n" . $hereprev) && 21163705ce5bSJoe Perches $fix) { 21173705ce5bSJoe Perches $fixed[$linenr - 1] =~ 21183705ce5bSJoe Perches s/^(\+.*\*[ \t]*\))[ \t]+/$1/; 21193705ce5bSJoe Perches } 2120aad4f614SJoe Perches } 2121aad4f614SJoe Perches 212205880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2123fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 2124fdb4bcd6SJoe Perches $rawline =~ /^\+[ \t]*\*/) { 212505880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 212605880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 212705880600SJoe Perches } 212805880600SJoe Perches 212905880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2130a605e32eSJoe Perches $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* 2131a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 213261135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2133a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 2134a605e32eSJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2135a605e32eSJoe Perches "networking block comments start with * on subsequent lines\n" . $hereprev); 2136a605e32eSJoe Perches } 2137a605e32eSJoe Perches 2138a605e32eSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2139c24f9f19SJoe Perches $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2140c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2141c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2142c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 214305880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 214405880600SJoe Perches "networking block comments put the trailing */ on a separate line\n" . $herecurr); 214505880600SJoe Perches } 214605880600SJoe Perches 21475f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 21486b4c5bebSAndy Whitcroft# Exceptions: 21496b4c5bebSAndy Whitcroft# 1) within comments 21506b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 21516b4c5bebSAndy Whitcroft# 3) hanging labels 21523705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 21535f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 21543705ce5bSJoe Perches if (WARN("LEADING_SPACE", 21553705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 21563705ce5bSJoe Perches $fix) { 21573705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 21583705ce5bSJoe Perches } 21595f7ddae6SRaffaele Recalcati } 21605f7ddae6SRaffaele Recalcati 2161b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 2162b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 2163b9ea10d6SAndy Whitcroft 21641ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 21651ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 21661ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 21671ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 21681ba8dfd1SKees Cook } 21691ba8dfd1SKees Cook 2170c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 2171cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 2172000d1cc1SJoe Perches WARN("CVS_KEYWORD", 2173000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 2174c2fdda0dSAndy Whitcroft } 217522f2a2efSAndy Whitcroft 217642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 217742e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 217842e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2179000d1cc1SJoe Perches ERROR("CSYNC", 2180000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 218142e41c54SMike Frysinger } 218242e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 218342e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2184000d1cc1SJoe Perches ERROR("SSYNC", 2185000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 218642e41c54SMike Frysinger } 218742e41c54SMike Frysinger 218856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 218956e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 219056e77d70SJoe Perches WARN("HOTPLUG_SECTION", 219156e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 219256e77d70SJoe Perches } 219356e77d70SJoe Perches 21949c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 21952b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 21962b474a1aSAndy Whitcroft $realline_next); 21973e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 21983e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 21991b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 2200170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2201f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2202171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 2203171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 2204171ae1a4SAndy Whitcroft 22053e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 22063e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 22073e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 22083e469cdcSAndy Whitcroft # until we hit end of it. 22093e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 22103e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 22113e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 22123e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 22133e469cdcSAndy Whitcroft } 2214f74bd194SAndy Whitcroft 22152b474a1aSAndy Whitcroft # Find the real next line. 22162b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 22172b474a1aSAndy Whitcroft if (defined $realline_next && 22182b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 22192b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 22202b474a1aSAndy Whitcroft $realline_next++; 22212b474a1aSAndy Whitcroft } 22222b474a1aSAndy Whitcroft 2223171ae1a4SAndy Whitcroft my $s = $stat; 2224171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 2225cf655043SAndy Whitcroft 2226c2fdda0dSAndy Whitcroft # Ignore goto labels. 2227171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 2228c2fdda0dSAndy Whitcroft 2229c2fdda0dSAndy Whitcroft # Ignore functions being called 2230171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 2231c2fdda0dSAndy Whitcroft 2232463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 2233463f2864SAndy Whitcroft 2234c45dcabdSAndy Whitcroft # declarations always start with types 2235d2506586SAndy 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) { 2236c45dcabdSAndy Whitcroft my $type = $1; 2237c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 2238c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 2239c45dcabdSAndy Whitcroft 22406c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 2241a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 2242c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 2243c2fdda0dSAndy Whitcroft } 22448905a67cSAndy Whitcroft 22456c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 224665863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 2247c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 22489c0ca6f9SAndy Whitcroft } 22498905a67cSAndy Whitcroft 22508905a67cSAndy Whitcroft # Check for any sort of function declaration. 22518905a67cSAndy Whitcroft # int foo(something bar, other baz); 22528905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 2253171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 22548905a67cSAndy Whitcroft my ($name_len) = length($1); 22558905a67cSAndy Whitcroft 2256cf655043SAndy Whitcroft my $ctx = $s; 2257773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 22588905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 2259cf655043SAndy Whitcroft 22608905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 2261c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 22628905a67cSAndy Whitcroft 2263c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 22648905a67cSAndy Whitcroft } 22658905a67cSAndy Whitcroft } 22668905a67cSAndy Whitcroft } 22678905a67cSAndy Whitcroft 22689c0ca6f9SAndy Whitcroft } 22699c0ca6f9SAndy Whitcroft 227000df344fSAndy Whitcroft# 227100df344fSAndy Whitcroft# Checks which may be anchored in the context. 227200df344fSAndy Whitcroft# 227300df344fSAndy Whitcroft 227400df344fSAndy Whitcroft# Check for switch () and associated case and default 227500df344fSAndy Whitcroft# statements should be at the same indent. 227600df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 227700df344fSAndy Whitcroft my $err = ''; 227800df344fSAndy Whitcroft my $sep = ''; 227900df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 228000df344fSAndy Whitcroft shift(@ctx); 228100df344fSAndy Whitcroft for my $ctx (@ctx) { 228200df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 228300df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 228400df344fSAndy Whitcroft $indent != $cindent) { 228500df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 228600df344fSAndy Whitcroft $sep = ''; 228700df344fSAndy Whitcroft } else { 228800df344fSAndy Whitcroft $sep = "[...]\n"; 228900df344fSAndy Whitcroft } 229000df344fSAndy Whitcroft } 229100df344fSAndy Whitcroft if ($err ne '') { 2292000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2293000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2294de7d4f0eSAndy Whitcroft } 2295de7d4f0eSAndy Whitcroft } 2296de7d4f0eSAndy Whitcroft 2297de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2298de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 2299c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2300773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2301773647a0SAndy Whitcroft 23029c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 23038eef05ddSJoe Perches 23048eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 23058eef05ddSJoe Perches WARN("DEEP_INDENTATION", 23068eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 23078eef05ddSJoe Perches } 23088eef05ddSJoe Perches 2309de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2310de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 2311de7d4f0eSAndy Whitcroft 2312548596d5SAndy Whitcroft my $ctx_ln = $linenr; 2313548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 2314de7d4f0eSAndy Whitcroft 2315548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2316548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 2317548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 2318548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2319548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2320773647a0SAndy Whitcroft $ctx_ln++; 2321773647a0SAndy Whitcroft } 2322548596d5SAndy Whitcroft 232353210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 232453210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2325773647a0SAndy Whitcroft 2326773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 2327000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2328000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 232901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 233000df344fSAndy Whitcroft } 2331773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2332773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 2333773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 2334773647a0SAndy Whitcroft { 23359c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 23369c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 2337000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 2338000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 233901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 23409c0ca6f9SAndy Whitcroft } 23419c0ca6f9SAndy Whitcroft } 234200df344fSAndy Whitcroft } 234300df344fSAndy Whitcroft 23444d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 23454d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 23463e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 23473e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 23483e469cdcSAndy Whitcroft if (!defined $stat); 23494d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 23504d001e4dSAndy Whitcroft 23514d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 23524d001e4dSAndy Whitcroft 23534d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 23544d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 23554d001e4dSAndy Whitcroft # where necessary. 23564d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 23574d001e4dSAndy Whitcroft 23584d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 23596f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 23606f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 23614d001e4dSAndy Whitcroft 23624d001e4dSAndy Whitcroft # We want to check the first line inside the block 23634d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 23644d001e4dSAndy Whitcroft # 1) any blank line termination 23654d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 23664d001e4dSAndy Whitcroft # 3) any do (...) { 23674d001e4dSAndy Whitcroft my $continuation = 0; 23684d001e4dSAndy Whitcroft my $check = 0; 23694d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 23704d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 23714d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 23724d001e4dSAndy Whitcroft $continuation = 1; 23734d001e4dSAndy Whitcroft } 23749bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 23754d001e4dSAndy Whitcroft $check = 1; 23764d001e4dSAndy Whitcroft $cond_lines++; 23774d001e4dSAndy Whitcroft } 23784d001e4dSAndy Whitcroft 23794d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 23804d001e4dSAndy Whitcroft # preprocessor statement. 23814d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 23824d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 23834d001e4dSAndy Whitcroft $check = 0; 23844d001e4dSAndy Whitcroft } 23854d001e4dSAndy Whitcroft 23869bd49efeSAndy Whitcroft my $cond_ptr = -1; 2387740504c6SAndy Whitcroft $continuation = 0; 23889bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 23899bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 23904d001e4dSAndy Whitcroft 2391f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 2392f16fa28fSAndy Whitcroft # is not linear. 2393f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2394f16fa28fSAndy Whitcroft $check = 0; 2395f16fa28fSAndy Whitcroft } 2396f16fa28fSAndy Whitcroft 23979bd49efeSAndy Whitcroft # Ignore: 23989bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 23999bd49efeSAndy Whitcroft # 2) preprocessor lines, and 24009bd49efeSAndy Whitcroft # 3) labels. 2401740504c6SAndy Whitcroft if ($continuation || 2402740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 24039bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 24049bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 2405740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 240630dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 24079bd49efeSAndy Whitcroft $cond_lines++; 24089bd49efeSAndy Whitcroft } 24094d001e4dSAndy Whitcroft } 241030dad6ebSAndy Whitcroft } 24114d001e4dSAndy Whitcroft 24124d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 24134d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 24144d001e4dSAndy Whitcroft 24154d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 24164d001e4dSAndy Whitcroft # this is not this patch's fault. 24174d001e4dSAndy Whitcroft if (!defined($stat_real) || 24184d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 24194d001e4dSAndy Whitcroft $check = 0; 24204d001e4dSAndy Whitcroft } 24214d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 24224d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 24234d001e4dSAndy Whitcroft } 24244d001e4dSAndy Whitcroft 24259bd49efeSAndy 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"; 24264d001e4dSAndy Whitcroft 24274d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 24284d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 2429000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 2430000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 24314d001e4dSAndy Whitcroft } 24324d001e4dSAndy Whitcroft } 24334d001e4dSAndy Whitcroft 24346c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 24356c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 24361f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 24371f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 24386c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 2439c2fdda0dSAndy Whitcroft if ($dbg_values) { 2440c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 2441cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 2442cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 24431f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 2444c2fdda0dSAndy Whitcroft } 24456c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 24466c72ffaaSAndy Whitcroft 244700df344fSAndy Whitcroft#ignore lines not being added 24483705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 244900df344fSAndy Whitcroft 2450653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 24517429c690SAndy Whitcroft if ($dbg_type) { 24527429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 2453000d1cc1SJoe Perches ERROR("TEST_TYPE", 2454000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 24557429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2456000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 2457000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 24587429c690SAndy Whitcroft } 2459653d4876SAndy Whitcroft next; 2460653d4876SAndy Whitcroft } 2461a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2462a1ef277eSAndy Whitcroft if ($dbg_attr) { 24639360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2464000d1cc1SJoe Perches ERROR("TEST_ATTR", 2465000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 24669360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2467000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2468000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2469a1ef277eSAndy Whitcroft } 2470a1ef277eSAndy Whitcroft next; 2471a1ef277eSAndy Whitcroft } 2472653d4876SAndy Whitcroft 2473f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 247499423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 247599423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2476000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2477000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . $hereprev); 2478f0a594c1SAndy Whitcroft } 2479f0a594c1SAndy Whitcroft 248000df344fSAndy Whitcroft# 248100df344fSAndy Whitcroft# Checks which are anchored on the added line. 248200df344fSAndy Whitcroft# 248300df344fSAndy Whitcroft 2484653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2485c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2486653d4876SAndy Whitcroft my $path = $1; 2487653d4876SAndy Whitcroft if ($path =~ m{//}) { 2488000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2489495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 2490495e9d84SJoe Perches } 2491495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 2492495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 2493495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 2494653d4876SAndy Whitcroft } 2495653d4876SAndy Whitcroft } 2496653d4876SAndy Whitcroft 249700df344fSAndy Whitcroft# no C99 // comments 249800df344fSAndy Whitcroft if ($line =~ m{//}) { 24993705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 25003705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 25013705ce5bSJoe Perches $fix) { 25023705ce5bSJoe Perches my $line = $fixed[$linenr - 1]; 25033705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 25043705ce5bSJoe Perches my $comment = trim($1); 25053705ce5bSJoe Perches $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; 25063705ce5bSJoe Perches } 25073705ce5bSJoe Perches } 250800df344fSAndy Whitcroft } 250900df344fSAndy Whitcroft # Remove C99 comments. 25100a920b5bSAndy Whitcroft $line =~ s@//.*@@; 25116c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 25120a920b5bSAndy Whitcroft 25132b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 25142b474a1aSAndy Whitcroft# the whole statement. 25152b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 25162b474a1aSAndy Whitcroft if (defined $realline_next && 25172b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 25182b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 25192b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 25202b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 25213cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 25223cbf62dfSAndy Whitcroft # a prefix: 25233cbf62dfSAndy Whitcroft # XXX(foo); 25243cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 2525653d4876SAndy Whitcroft my $name = $1; 252687a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 25273cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 25283cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 25293cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 25303cbf62dfSAndy Whitcroft 25313cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 25322b474a1aSAndy Whitcroft \n.}\s*$| 253348012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 253448012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 253548012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 25362b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 25372b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 253848012058SAndy Whitcroft )/x) { 25392b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 25402b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 25412b474a1aSAndy Whitcroft } else { 25422b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 25430a920b5bSAndy Whitcroft } 25440a920b5bSAndy Whitcroft } 25452b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 25462b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 25472b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 25482b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 25492b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 25502b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 25512b474a1aSAndy Whitcroft } 25522b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 25532b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 2554000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 2555000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 25562b474a1aSAndy Whitcroft } 25570a920b5bSAndy Whitcroft 25585150bda4SJoe Eloff# check for global initialisers. 2559d5e616fcSJoe Perches if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { 2560d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 2561000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 2562d5e616fcSJoe Perches $herecurr) && 2563d5e616fcSJoe Perches $fix) { 2564d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; 2565d5e616fcSJoe Perches } 2566f0a594c1SAndy Whitcroft } 25670a920b5bSAndy Whitcroft# check for static initialisers. 2568d5e616fcSJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2569d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 2570000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 2571d5e616fcSJoe Perches $herecurr) && 2572d5e616fcSJoe Perches $fix) { 2573d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; 2574d5e616fcSJoe Perches } 25750a920b5bSAndy Whitcroft } 25760a920b5bSAndy Whitcroft 2577cb710ecaSJoe Perches# check for static const char * arrays. 2578cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 2579000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2580000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 2581cb710ecaSJoe Perches $herecurr); 2582cb710ecaSJoe Perches } 2583cb710ecaSJoe Perches 2584cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 2585cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 2586000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2587000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 2588cb710ecaSJoe Perches $herecurr); 2589cb710ecaSJoe Perches } 2590cb710ecaSJoe Perches 259193ed0e2dSJoe Perches# check for declarations of struct pci_device_id 259293ed0e2dSJoe Perches if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { 2593000d1cc1SJoe Perches WARN("DEFINE_PCI_DEVICE_TABLE", 2594000d1cc1SJoe Perches "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); 259593ed0e2dSJoe Perches } 259693ed0e2dSJoe Perches 2597653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 2598653d4876SAndy Whitcroft# make sense. 2599653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 26008054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 2601c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 26028ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 2603653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 2604000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 2605000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 26060a920b5bSAndy Whitcroft } 26070a920b5bSAndy Whitcroft 26080a920b5bSAndy Whitcroft# * goes on variable not on type 260965863862SAndy Whitcroft # (char*[ const]) 2610bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2611bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 26123705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 2613d8aaf121SAndy Whitcroft 261465863862SAndy Whitcroft # Should start with a space. 261565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 261665863862SAndy Whitcroft # Should not end with a space. 261765863862SAndy Whitcroft $to =~ s/\s+$//; 261865863862SAndy Whitcroft # '*'s should not have spaces between. 2619f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 262065863862SAndy Whitcroft } 2621d8aaf121SAndy Whitcroft 26223705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 262365863862SAndy Whitcroft if ($from ne $to) { 26243705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 26253705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 26263705ce5bSJoe Perches $fix) { 26273705ce5bSJoe Perches my $sub_from = $ident; 26283705ce5bSJoe Perches my $sub_to = $ident; 26293705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 26303705ce5bSJoe Perches $fixed[$linenr - 1] =~ 26313705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 26323705ce5bSJoe Perches } 263365863862SAndy Whitcroft } 2634bfcb2cc7SAndy Whitcroft } 2635bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2636bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 26373705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 2638d8aaf121SAndy Whitcroft 263965863862SAndy Whitcroft # Should start with a space. 264065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 264165863862SAndy Whitcroft # Should not end with a space. 264265863862SAndy Whitcroft $to =~ s/\s+$//; 264365863862SAndy Whitcroft # '*'s should not have spaces between. 2644f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 264565863862SAndy Whitcroft } 264665863862SAndy Whitcroft # Modifiers should have spaces. 264765863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 264865863862SAndy Whitcroft 26493705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 2650667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 26513705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 26523705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 26533705ce5bSJoe Perches $fix) { 26543705ce5bSJoe Perches 26553705ce5bSJoe Perches my $sub_from = $match; 26563705ce5bSJoe Perches my $sub_to = $match; 26573705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 26583705ce5bSJoe Perches $fixed[$linenr - 1] =~ 26593705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 26603705ce5bSJoe Perches } 266165863862SAndy Whitcroft } 26620a920b5bSAndy Whitcroft } 26630a920b5bSAndy Whitcroft 26640a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 26650a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 26660a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 26670a920b5bSAndy Whitcroft# print "$herecurr"; 26680a920b5bSAndy Whitcroft# $clean = 0; 26690a920b5bSAndy Whitcroft# } 26700a920b5bSAndy Whitcroft 26718905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 2672000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 2673000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 26748905a67cSAndy Whitcroft } 26758905a67cSAndy Whitcroft 267617441227SJoe Perches# check for uses of printk_ratelimit 267717441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 2678000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 2679000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 268017441227SJoe Perches } 268117441227SJoe Perches 268200df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 268300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 268400df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 268525985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 268600df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 2687f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 268800df344fSAndy Whitcroft my $ok = 0; 268900df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 269000df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 269125985edcSLucas De Marchi # we have a preceding printk if it ends 269200df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 269300df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 269400df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 269500df344fSAndy Whitcroft $ok = 1; 269600df344fSAndy Whitcroft } 269700df344fSAndy Whitcroft last; 269800df344fSAndy Whitcroft } 269900df344fSAndy Whitcroft } 270000df344fSAndy Whitcroft if ($ok == 0) { 2701000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 2702000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 27030a920b5bSAndy Whitcroft } 270400df344fSAndy Whitcroft } 27050a920b5bSAndy Whitcroft 2706243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 2707243f3803SJoe Perches my $orig = $1; 2708243f3803SJoe Perches my $level = lc($orig); 2709243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 27108f26b837SJoe Perches my $level2 = $level; 27118f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 2712243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 27138f26b837SJoe Perches "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 2714243f3803SJoe Perches } 2715243f3803SJoe Perches 2716243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 2717d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 2718d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 2719d5e616fcSJoe Perches $fix) { 2720d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 2721d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 2722d5e616fcSJoe Perches } 2723243f3803SJoe Perches } 2724243f3803SJoe Perches 2725dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 2726dc139313SJoe Perches my $orig = $1; 2727dc139313SJoe Perches my $level = lc($orig); 2728dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 2729dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 2730dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 2731dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 2732dc139313SJoe Perches } 2733dc139313SJoe Perches 2734653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 2735653d4876SAndy Whitcroft# or if closed on same line 2736c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2737c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 2738000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2739000d1cc1SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr); 27400a920b5bSAndy Whitcroft } 2741653d4876SAndy Whitcroft 27428905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 27438905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 27448905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2745000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2746000d1cc1SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev); 27478905a67cSAndy Whitcroft } 27488905a67cSAndy Whitcroft 27490c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 27503705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 27513705ce5bSJoe Perches if (WARN("SPACING", 27523705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 27533705ce5bSJoe Perches $fix) { 27543705ce5bSJoe Perches $fixed[$linenr - 1] =~ 27553705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 27563705ce5bSJoe Perches } 27570c73b4ebSAndy Whitcroft } 27580c73b4ebSAndy Whitcroft 27598d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 27608d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 2761fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2762fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 27638d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 27648d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 27658d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 2766fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 2767daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 27683705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 27693705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 27703705ce5bSJoe Perches $fix) { 27713705ce5bSJoe Perches $fixed[$linenr - 1] =~ 27723705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 27733705ce5bSJoe Perches } 27748d31cfceSAndy Whitcroft } 27758d31cfceSAndy Whitcroft } 27768d31cfceSAndy Whitcroft 2777f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 27786c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 2779c2fdda0dSAndy Whitcroft my $name = $1; 2780773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 2781773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 2782c2fdda0dSAndy Whitcroft 2783c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 2784773647a0SAndy Whitcroft if ($name =~ /^(?: 2785773647a0SAndy Whitcroft if|for|while|switch|return|case| 2786773647a0SAndy Whitcroft volatile|__volatile__| 2787773647a0SAndy Whitcroft __attribute__|format|__extension__| 2788773647a0SAndy Whitcroft asm|__asm__)$/x) 2789773647a0SAndy Whitcroft { 2790c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 2791c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 2792c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2793c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2794773647a0SAndy Whitcroft 2795773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2796c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2797c2fdda0dSAndy Whitcroft 2798c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2799c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2800773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2801c2fdda0dSAndy Whitcroft 2802c2fdda0dSAndy Whitcroft } else { 28033705ce5bSJoe Perches if (WARN("SPACING", 28043705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 28053705ce5bSJoe Perches $fix) { 28063705ce5bSJoe Perches $fixed[$linenr - 1] =~ 28073705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 28083705ce5bSJoe Perches } 2809f0a594c1SAndy Whitcroft } 28106c72ffaaSAndy Whitcroft } 28119a4cad4eSEric Nelson 2812653d4876SAndy Whitcroft# Check operator spacing. 28130a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 28143705ce5bSJoe Perches my $fixed_line = ""; 28153705ce5bSJoe Perches my $line_fixed = 0; 28163705ce5bSJoe Perches 28179c0ca6f9SAndy Whitcroft my $ops = qr{ 28189c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 28199c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 28209c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 28211f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 28221f65f947SAndy Whitcroft \?|: 28239c0ca6f9SAndy Whitcroft }x; 2824cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 28253705ce5bSJoe Perches 28263705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 28273705ce5bSJoe Perches## foreach my $el (@elements) { 28283705ce5bSJoe Perches## print("el: <$el>\n"); 28293705ce5bSJoe Perches## } 28303705ce5bSJoe Perches 28313705ce5bSJoe Perches my @fix_elements = (); 283200df344fSAndy Whitcroft my $off = 0; 28336c72ffaaSAndy Whitcroft 28343705ce5bSJoe Perches foreach my $el (@elements) { 28353705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 28363705ce5bSJoe Perches $off += length($el); 28373705ce5bSJoe Perches } 28383705ce5bSJoe Perches 28393705ce5bSJoe Perches $off = 0; 28403705ce5bSJoe Perches 28416c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 2842b34c648bSJoe Perches my $last_after = -1; 28436c72ffaaSAndy Whitcroft 28440a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 28453705ce5bSJoe Perches 28463705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 28473705ce5bSJoe Perches 28483705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 28493705ce5bSJoe Perches 28504a0df2efSAndy Whitcroft $off += length($elements[$n]); 28514a0df2efSAndy Whitcroft 285225985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 2853773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 2854773647a0SAndy Whitcroft my $cc = ''; 2855773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 2856773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 2857773647a0SAndy Whitcroft } 2858773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 2859773647a0SAndy Whitcroft 28604a0df2efSAndy Whitcroft my $a = ''; 28614a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 28624a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 2863cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 28644a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 28654a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 2866773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 28674a0df2efSAndy Whitcroft 28680a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 28694a0df2efSAndy Whitcroft 28704a0df2efSAndy Whitcroft my $c = ''; 28710a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 28724a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 28734a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2874cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 28754a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 28764a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 28778b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 28784a0df2efSAndy Whitcroft } else { 28794a0df2efSAndy Whitcroft $c = 'E'; 28800a920b5bSAndy Whitcroft } 28810a920b5bSAndy Whitcroft 28824a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 28834a0df2efSAndy Whitcroft 28844a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 28854a0df2efSAndy Whitcroft 28866c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 2887de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 28880a920b5bSAndy Whitcroft 288974048ed8SAndy Whitcroft # Pull out the value of this operator. 28906c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 28910a920b5bSAndy Whitcroft 28921f65f947SAndy Whitcroft # Get the full operator variant. 28931f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 28941f65f947SAndy Whitcroft 289513214adfSAndy Whitcroft # Ignore operators passed as parameters. 289613214adfSAndy Whitcroft if ($op_type ne 'V' && 289713214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 289813214adfSAndy Whitcroft 2899cf655043SAndy Whitcroft# # Ignore comments 2900cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 290113214adfSAndy Whitcroft 2902d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 290313214adfSAndy Whitcroft } elsif ($op eq ';') { 2904cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 2905cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 29063705ce5bSJoe Perches if (ERROR("SPACING", 29073705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 2908b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 29093705ce5bSJoe Perches $line_fixed = 1; 29103705ce5bSJoe Perches } 2911d8aaf121SAndy Whitcroft } 2912d8aaf121SAndy Whitcroft 2913d8aaf121SAndy Whitcroft # // is a comment 2914d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 29150a920b5bSAndy Whitcroft 29161f65f947SAndy Whitcroft # No spaces for: 29171f65f947SAndy Whitcroft # -> 29181f65f947SAndy Whitcroft # : when part of a bitfield 29191f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 29204a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 29213705ce5bSJoe Perches if (ERROR("SPACING", 29223705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 2923b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 29243705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 29253705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 29263705ce5bSJoe Perches } 2927b34c648bSJoe Perches $line_fixed = 1; 29283705ce5bSJoe Perches } 29290a920b5bSAndy Whitcroft } 29300a920b5bSAndy Whitcroft 29310a920b5bSAndy Whitcroft # , must have a space on the right. 29320a920b5bSAndy Whitcroft } elsif ($op eq ',') { 2933cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 29343705ce5bSJoe Perches if (ERROR("SPACING", 29353705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 2936b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 29373705ce5bSJoe Perches $line_fixed = 1; 2938b34c648bSJoe Perches $last_after = $n; 29393705ce5bSJoe Perches } 29400a920b5bSAndy Whitcroft } 29410a920b5bSAndy Whitcroft 29429c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 294374048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 29449c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 29459c0ca6f9SAndy Whitcroft 29469c0ca6f9SAndy Whitcroft # unary operators should have a space before and 29479c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 29489c0ca6f9SAndy Whitcroft # unary operator, or a cast 29499c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 295074048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 29510d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 2952cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 29533705ce5bSJoe Perches if (ERROR("SPACING", 29543705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 2955b34c648bSJoe Perches if ($n != $last_after + 2) { 2956b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 29573705ce5bSJoe Perches $line_fixed = 1; 29583705ce5bSJoe Perches } 29590a920b5bSAndy Whitcroft } 2960b34c648bSJoe Perches } 2961a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2962171ae1a4SAndy Whitcroft # A unary '*' may be const 2963171ae1a4SAndy Whitcroft 2964171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 29653705ce5bSJoe Perches if (ERROR("SPACING", 29663705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 2967b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 29683705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 29693705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 29703705ce5bSJoe Perches } 2971b34c648bSJoe Perches $line_fixed = 1; 29723705ce5bSJoe Perches } 29730a920b5bSAndy Whitcroft } 29740a920b5bSAndy Whitcroft 29750a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 29760a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 2977773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 29783705ce5bSJoe Perches if (ERROR("SPACING", 29793705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 2980b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 29813705ce5bSJoe Perches $line_fixed = 1; 29823705ce5bSJoe Perches } 29830a920b5bSAndy Whitcroft } 2984773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 2985773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 29863705ce5bSJoe Perches if (ERROR("SPACING", 29873705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 2988b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 29893705ce5bSJoe Perches $line_fixed = 1; 29903705ce5bSJoe Perches } 2991653d4876SAndy Whitcroft } 2992773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 29933705ce5bSJoe Perches if (ERROR("SPACING", 29943705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 2995b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 29963705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 29973705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 2998773647a0SAndy Whitcroft } 2999b34c648bSJoe Perches $line_fixed = 1; 30003705ce5bSJoe Perches } 30013705ce5bSJoe Perches } 30020a920b5bSAndy Whitcroft 30030a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 30049c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 30059c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 30069c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 3007c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 3008c2fdda0dSAndy Whitcroft $op eq '%') 30090a920b5bSAndy Whitcroft { 3010773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 30113705ce5bSJoe Perches if (ERROR("SPACING", 30123705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 3013b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3014b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3015b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3016b34c648bSJoe Perches } 30173705ce5bSJoe Perches $line_fixed = 1; 30183705ce5bSJoe Perches } 30190a920b5bSAndy Whitcroft } 30200a920b5bSAndy Whitcroft 30211f65f947SAndy Whitcroft # A colon needs no spaces before when it is 30221f65f947SAndy Whitcroft # terminating a case value or a label. 30231f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 30241f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 30253705ce5bSJoe Perches if (ERROR("SPACING", 30263705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3027b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 30283705ce5bSJoe Perches $line_fixed = 1; 30293705ce5bSJoe Perches } 30301f65f947SAndy Whitcroft } 30311f65f947SAndy Whitcroft 30320a920b5bSAndy Whitcroft # All the others need spaces both sides. 3033cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 30341f65f947SAndy Whitcroft my $ok = 0; 30351f65f947SAndy Whitcroft 303622f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 30371f65f947SAndy Whitcroft if (($op eq '<' && 30381f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 30391f65f947SAndy Whitcroft ($op eq '>' && 30401f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 30411f65f947SAndy Whitcroft { 30421f65f947SAndy Whitcroft $ok = 1; 30431f65f947SAndy Whitcroft } 30441f65f947SAndy Whitcroft 30451f65f947SAndy Whitcroft # Ignore ?: 30461f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 30471f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 30481f65f947SAndy Whitcroft $ok = 1; 30491f65f947SAndy Whitcroft } 30501f65f947SAndy Whitcroft 30511f65f947SAndy Whitcroft if ($ok == 0) { 30523705ce5bSJoe Perches if (ERROR("SPACING", 30533705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 3054b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3055b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3056b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3057b34c648bSJoe Perches } 30583705ce5bSJoe Perches $line_fixed = 1; 30593705ce5bSJoe Perches } 30600a920b5bSAndy Whitcroft } 306122f2a2efSAndy Whitcroft } 30624a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 30633705ce5bSJoe Perches 30643705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 30653705ce5bSJoe Perches 30663705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 30670a920b5bSAndy Whitcroft } 30683705ce5bSJoe Perches 30693705ce5bSJoe Perches if (($#elements % 2) == 0) { 30703705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 30713705ce5bSJoe Perches } 30723705ce5bSJoe Perches 30733705ce5bSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { 30743705ce5bSJoe Perches $fixed[$linenr - 1] = $fixed_line; 30753705ce5bSJoe Perches } 30763705ce5bSJoe Perches 30773705ce5bSJoe Perches 30780a920b5bSAndy Whitcroft } 30790a920b5bSAndy Whitcroft 3080786b6326SJoe Perches# check for whitespace before a non-naked semicolon 3081786b6326SJoe Perches if ($line =~ /^\+.*\S\s+;/) { 3082786b6326SJoe Perches if (WARN("SPACING", 3083786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 3084786b6326SJoe Perches $fix) { 3085786b6326SJoe Perches 1 while $fixed[$linenr - 1] =~ 3086786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 3087786b6326SJoe Perches } 3088786b6326SJoe Perches } 3089786b6326SJoe Perches 3090f0a594c1SAndy Whitcroft# check for multiple assignments 3091f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 3092000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 3093000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 3094f0a594c1SAndy Whitcroft } 3095f0a594c1SAndy Whitcroft 309622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 309722f2a2efSAndy Whitcroft## # continuation. 309822f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 309922f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 310022f2a2efSAndy Whitcroft## 310122f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 310222f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 310322f2a2efSAndy Whitcroft## my $ln = $line; 310422f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 310522f2a2efSAndy Whitcroft## } 310622f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 3107000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 3108000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 310922f2a2efSAndy Whitcroft## } 311022f2a2efSAndy Whitcroft## } 3111f0a594c1SAndy Whitcroft 31120a920b5bSAndy Whitcroft#need space before brace following if, while, etc 311322f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 311422f2a2efSAndy Whitcroft $line =~ /do{/) { 31153705ce5bSJoe Perches if (ERROR("SPACING", 31163705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 31173705ce5bSJoe Perches $fix) { 3118d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/; 31193705ce5bSJoe Perches } 3120de7d4f0eSAndy Whitcroft } 3121de7d4f0eSAndy Whitcroft 3122c4a62ef9SJoe Perches## # check for blank lines before declarations 3123c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 3124c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 3125c4a62ef9SJoe Perches## WARN("SPACING", 3126c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 3127c4a62ef9SJoe Perches## } 3128c4a62ef9SJoe Perches## 3129c4a62ef9SJoe Perches 3130de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 3131de7d4f0eSAndy Whitcroft# on the line 3132de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 3133d5e616fcSJoe Perches if (ERROR("SPACING", 3134d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 3135d5e616fcSJoe Perches $fix) { 3136d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3137d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 3138d5e616fcSJoe Perches } 31390a920b5bSAndy Whitcroft } 31400a920b5bSAndy Whitcroft 314122f2a2efSAndy Whitcroft# check spacing on square brackets 314222f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 31433705ce5bSJoe Perches if (ERROR("SPACING", 31443705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 31453705ce5bSJoe Perches $fix) { 31463705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31473705ce5bSJoe Perches s/\[\s+/\[/; 31483705ce5bSJoe Perches } 314922f2a2efSAndy Whitcroft } 315022f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 31513705ce5bSJoe Perches if (ERROR("SPACING", 31523705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 31533705ce5bSJoe Perches $fix) { 31543705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31553705ce5bSJoe Perches s/\s+\]/\]/; 31563705ce5bSJoe Perches } 315722f2a2efSAndy Whitcroft } 315822f2a2efSAndy Whitcroft 3159c45dcabdSAndy Whitcroft# check spacing on parentheses 31609c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 31619c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 31623705ce5bSJoe Perches if (ERROR("SPACING", 31633705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 31643705ce5bSJoe Perches $fix) { 31653705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31663705ce5bSJoe Perches s/\(\s+/\(/; 31673705ce5bSJoe Perches } 316822f2a2efSAndy Whitcroft } 316913214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 3170c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 3171c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 31723705ce5bSJoe Perches if (ERROR("SPACING", 31733705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 31743705ce5bSJoe Perches $fix) { 31753705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31763705ce5bSJoe Perches s/\s+\)/\)/; 31773705ce5bSJoe Perches } 317822f2a2efSAndy Whitcroft } 317922f2a2efSAndy Whitcroft 31800a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 31814a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 31820a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 31833705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 31843705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 31853705ce5bSJoe Perches $fix) { 31863705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31873705ce5bSJoe Perches s/^(.)\s+/$1/; 31883705ce5bSJoe Perches } 31890a920b5bSAndy Whitcroft } 31900a920b5bSAndy Whitcroft 3191c45dcabdSAndy Whitcroft# Return is not a function. 3192c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 3193c45dcabdSAndy Whitcroft my $spacing = $1; 3194c45dcabdSAndy Whitcroft my $value = $2; 3195c45dcabdSAndy Whitcroft 319686f9d059SAndy Whitcroft # Flatten any parentheses 3197fb2d2c1bSAndy Whitcroft $value =~ s/\(/ \(/g; 3198fb2d2c1bSAndy Whitcroft $value =~ s/\)/\) /g; 3199e01886adSAndy Whitcroft while ($value =~ s/\[[^\[\]]*\]/1/ || 320063f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 320163f17f89SAndy Whitcroft $Compare\s* 320263f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 320363f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 3204c45dcabdSAndy Whitcroft } 3205fb2d2c1bSAndy Whitcroft#print "value<$value>\n"; 3206fb2d2c1bSAndy Whitcroft if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 3207000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 3208000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 3209c45dcabdSAndy Whitcroft 3210c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 3211000d1cc1SJoe Perches ERROR("SPACING", 3212000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 3213c45dcabdSAndy Whitcroft } 3214c45dcabdSAndy Whitcroft } 321553a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 321653a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 321753a3c448SAndy Whitcroft my $name = $1; 321853a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 3219000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 3220000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 322153a3c448SAndy Whitcroft } 322253a3c448SAndy Whitcroft } 3223c45dcabdSAndy Whitcroft 32240a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 32254a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 32263705ce5bSJoe Perches if (ERROR("SPACING", 32273705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 32283705ce5bSJoe Perches $fix) { 32293705ce5bSJoe Perches $fixed[$linenr - 1] =~ 32303705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 32313705ce5bSJoe Perches } 32320a920b5bSAndy Whitcroft } 32330a920b5bSAndy Whitcroft 3234f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 3235f5fe35ddSAndy Whitcroft# statements after the conditional. 3236170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 32373e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 32383e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 32393e469cdcSAndy Whitcroft if (!defined $stat); 3240170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 3241170d3a22SAndy Whitcroft $remain_next, $off_next); 3242170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 3243170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 3244170d3a22SAndy Whitcroft 3245170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 3246170d3a22SAndy Whitcroft # If the statement carries leading newlines, 3247170d3a22SAndy Whitcroft # then count those as offsets. 3248170d3a22SAndy Whitcroft my ($whitespace) = 3249170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 3250170d3a22SAndy Whitcroft my $offset = 3251170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 3252170d3a22SAndy Whitcroft 3253170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 3254170d3a22SAndy Whitcroft $offset} = 1; 3255170d3a22SAndy Whitcroft } 3256170d3a22SAndy Whitcroft } 3257170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 3258170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 3259171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 32608905a67cSAndy Whitcroft 3261b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 3262000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 3263000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 32648905a67cSAndy Whitcroft } 32658905a67cSAndy Whitcroft 32668905a67cSAndy Whitcroft # Find out what is on the end of the line after the 32678905a67cSAndy Whitcroft # conditional. 3268773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 32698905a67cSAndy Whitcroft $s =~ s/\n.*//g; 327013214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 327153210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 327253210168SAndy Whitcroft $c !~ /}\s*while\s*/) 3273773647a0SAndy Whitcroft { 3274bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 3275bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 3276bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 327742bdf74cSHidetoshi Seto my $stat_real = ''; 3278bb44ad39SAndy Whitcroft 327942bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 328042bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 3281bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 3282bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 3283bb44ad39SAndy Whitcroft } 3284bb44ad39SAndy Whitcroft 3285000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3286000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 32878905a67cSAndy Whitcroft } 32888905a67cSAndy Whitcroft } 32898905a67cSAndy Whitcroft 329013214adfSAndy Whitcroft# Check for bitwise tests written as boolean 329113214adfSAndy Whitcroft if ($line =~ / 329213214adfSAndy Whitcroft (?: 329313214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 329413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 329513214adfSAndy Whitcroft (?:\&\&|\|\|) 329613214adfSAndy Whitcroft | 329713214adfSAndy Whitcroft (?:\&\&|\|\|) 329813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 329913214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 330013214adfSAndy Whitcroft )/x) 330113214adfSAndy Whitcroft { 3302000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 3303000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 330413214adfSAndy Whitcroft } 330513214adfSAndy Whitcroft 33068905a67cSAndy Whitcroft# if and else should not have general statements after it 330713214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 330813214adfSAndy Whitcroft my $s = $1; 330913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 331013214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 3311000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3312000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 33130a920b5bSAndy Whitcroft } 331413214adfSAndy Whitcroft } 331539667782SAndy Whitcroft# if should not continue a brace 331639667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 3317000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3318000d1cc1SJoe Perches "trailing statements should be on next line\n" . 331939667782SAndy Whitcroft $herecurr); 332039667782SAndy Whitcroft } 3321a1080bf8SAndy Whitcroft# case and default should not have general statements after them 3322a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 3323a1080bf8SAndy Whitcroft $line !~ /\G(?: 33243fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 3325a1080bf8SAndy Whitcroft \s*return\s+ 3326a1080bf8SAndy Whitcroft )/xg) 3327a1080bf8SAndy Whitcroft { 3328000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3329000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 3330a1080bf8SAndy Whitcroft } 33310a920b5bSAndy Whitcroft 33320a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 33330a920b5bSAndy Whitcroft # indent level to be relevant to each other. 33340a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 33350a920b5bSAndy Whitcroft $previndent == $indent) { 3336000d1cc1SJoe Perches ERROR("ELSE_AFTER_BRACE", 3337000d1cc1SJoe Perches "else should follow close brace '}'\n" . $hereprev); 33380a920b5bSAndy Whitcroft } 33390a920b5bSAndy Whitcroft 3340c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 3341c2fdda0dSAndy Whitcroft $previndent == $indent) { 3342c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 3343c2fdda0dSAndy Whitcroft 3344c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 3345c2fdda0dSAndy Whitcroft # conditional. 3346773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 3347c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 3348c2fdda0dSAndy Whitcroft 3349c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 3350000d1cc1SJoe Perches ERROR("WHILE_AFTER_BRACE", 3351000d1cc1SJoe Perches "while should follow close brace '}'\n" . $hereprev); 3352c2fdda0dSAndy Whitcroft } 3353c2fdda0dSAndy Whitcroft } 3354c2fdda0dSAndy Whitcroft 335595e2c602SJoe Perches#Specific variable tests 3356323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 3357323c1260SJoe Perches my $var = $1; 335895e2c602SJoe Perches 335995e2c602SJoe Perches#gcc binary extension 336095e2c602SJoe Perches if ($var =~ /^$Binary$/) { 3361d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 3362d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 3363d5e616fcSJoe Perches $fix) { 3364d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 3365d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3366d5e616fcSJoe Perches s/\b$var\b/$hexval/; 3367d5e616fcSJoe Perches } 336895e2c602SJoe Perches } 336995e2c602SJoe Perches 337095e2c602SJoe Perches#CamelCase 3371807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 3372be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 337322735ce8SJoe Perches#Ignore Page<foo> variants 3374807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 337522735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 33763445686aSJoe Perches $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { 33777e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 33787e781f67SJoe Perches my $word = $1; 33797e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 33803445686aSJoe Perches seed_camelcase_includes() if ($check); 33817e781f67SJoe Perches if (!defined $camelcase{$word}) { 33827e781f67SJoe Perches $camelcase{$word} = 1; 3383be79794bSJoe Perches CHK("CAMELCASE", 33847e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 33857e781f67SJoe Perches } 3386323c1260SJoe Perches } 3387323c1260SJoe Perches } 33883445686aSJoe Perches } 33890a920b5bSAndy Whitcroft 33900a920b5bSAndy Whitcroft#no spaces allowed after \ in define 3391d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 3392d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 3393d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 3394d5e616fcSJoe Perches $fix) { 3395d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 3396d5e616fcSJoe Perches } 33970a920b5bSAndy Whitcroft } 33980a920b5bSAndy Whitcroft 3399653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 3400c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 3401e09dec48SAndy Whitcroft my $file = "$1.h"; 3402e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 3403e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 3404e09dec48SAndy Whitcroft $realfile ne $checkfile && 34057840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 3406c45dcabdSAndy Whitcroft { 3407e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 3408000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 3409000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3410e09dec48SAndy Whitcroft } else { 3411000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 3412000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3413e09dec48SAndy Whitcroft } 34140a920b5bSAndy Whitcroft } 34150a920b5bSAndy Whitcroft } 34160a920b5bSAndy Whitcroft 3417653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 3418653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 3419cf655043SAndy Whitcroft# in a known good container 3420b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 3421b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 3422d8aaf121SAndy Whitcroft my $ln = $linenr; 3423d8aaf121SAndy Whitcroft my $cnt = $realcnt; 3424c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 3425c45dcabdSAndy Whitcroft my $ctx = ''; 3426c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 3427f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3428f74bd194SAndy Whitcroft $ctx = $dstat; 3429c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 3430a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 3431c45dcabdSAndy Whitcroft 3432f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 3433292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 3434c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 3435c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 3436c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 3437c45dcabdSAndy Whitcroft 3438c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 3439bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 3440bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 3441c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 3442bf30d6edSAndy Whitcroft { 3443c45dcabdSAndy Whitcroft } 3444c45dcabdSAndy Whitcroft 3445e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 3446e45bab8eSAndy Whitcroft while ($dstat =~ s/("X*")\s*$Ident/$1/ || 3447e45bab8eSAndy Whitcroft $dstat =~ s/$Ident\s*("X*")/$1/) 3448e45bab8eSAndy Whitcroft { 3449e45bab8eSAndy Whitcroft } 3450e45bab8eSAndy Whitcroft 3451c45dcabdSAndy Whitcroft my $exceptions = qr{ 3452c45dcabdSAndy Whitcroft $Declare| 3453c45dcabdSAndy Whitcroft module_param_named| 3454a0a0a7a9SKees Cook MODULE_PARM_DESC| 3455c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 3456c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 3457383099fdSAndy Whitcroft __typeof__\(| 345822fd2d3eSStefani Seibold union| 345922fd2d3eSStefani Seibold struct| 3460ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 3461ea71a0a0SAndy Whitcroft ^\"|\"$ 3462c45dcabdSAndy Whitcroft }x; 34635eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 3464f74bd194SAndy Whitcroft if ($dstat ne '' && 3465f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 3466f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 34673cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 3468b9df76acSAndy Whitcroft $dstat !~ /^'X'$/ && # character constants 3469f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 3470f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 3471e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 347272f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 3473f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 3474f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 3475f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 3476f95a7e6aSJoe Perches $dstat !~ /^\({/ && # ({... 3477f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 3478c45dcabdSAndy Whitcroft { 3479f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 3480f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 3481f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 3482f74bd194SAndy Whitcroft 3483f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 3484f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 3485c45dcabdSAndy Whitcroft } 3486c45dcabdSAndy Whitcroft 3487f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 3488f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 3489f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 3490f74bd194SAndy Whitcroft } else { 3491000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 3492f74bd194SAndy Whitcroft "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 3493d8aaf121SAndy Whitcroft } 34940a920b5bSAndy Whitcroft } 34955023d347SJoe Perches 3496481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 34975023d347SJoe Perches 34985023d347SJoe Perches } else { 34995023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 3500481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 3501481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 35025023d347SJoe Perches $line =~ /^\+.*\\$/) { 35035023d347SJoe Perches WARN("LINE_CONTINUATIONS", 35045023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 35055023d347SJoe Perches } 3506653d4876SAndy Whitcroft } 35070a920b5bSAndy Whitcroft 3508b13edf7fSJoe Perches# do {} while (0) macro tests: 3509b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 3510b13edf7fSJoe Perches# macro should not end with a semicolon 3511b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 3512b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 3513b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 3514b13edf7fSJoe Perches my $ln = $linenr; 3515b13edf7fSJoe Perches my $cnt = $realcnt; 3516b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 3517b13edf7fSJoe Perches my $ctx = ''; 3518b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 3519b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 3520b13edf7fSJoe Perches $ctx = $dstat; 3521b13edf7fSJoe Perches 3522b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 3523b13edf7fSJoe Perches 3524b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 3525b13edf7fSJoe Perches my $stmts = $2; 3526b13edf7fSJoe Perches my $semis = $3; 3527b13edf7fSJoe Perches 3528b13edf7fSJoe Perches $ctx =~ s/\n*$//; 3529b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 3530b13edf7fSJoe Perches my $herectx = $here . "\n"; 3531b13edf7fSJoe Perches 3532b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 3533b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 3534b13edf7fSJoe Perches } 3535b13edf7fSJoe Perches 3536ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 3537ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 3538b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 3539b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 3540b13edf7fSJoe Perches } 3541b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 3542b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 3543b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 3544b13edf7fSJoe Perches } 3545b13edf7fSJoe Perches } 3546b13edf7fSJoe Perches } 3547b13edf7fSJoe Perches 3548080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 3549080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 3550080ba929SMike Frysinger# . 3551080ba929SMike Frysinger# ALIGN(...) 3552080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 3553080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 3554000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 3555000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 3556080ba929SMike Frysinger } 3557080ba929SMike Frysinger 3558f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 355913214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 356013214adfSAndy Whitcroft my ($level, $endln, @chunks) = 3561cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 356213214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 3563cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 3564cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 3565aad4f614SJoe Perches my @allowed = (); 3566aad4f614SJoe Perches my $allow = 0; 356713214adfSAndy Whitcroft my $seen = 0; 3568773647a0SAndy Whitcroft my $herectx = $here . "\n"; 3569cf655043SAndy Whitcroft my $ln = $linenr - 1; 357013214adfSAndy Whitcroft for my $chunk (@chunks) { 357113214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 357213214adfSAndy Whitcroft 3573773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 3574773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 3575773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 3576773647a0SAndy Whitcroft 3577aad4f614SJoe Perches $allowed[$allow] = 0; 3578773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 3579773647a0SAndy Whitcroft 3580773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 3581773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 3582773647a0SAndy Whitcroft 3583773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 3584cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 3585cf655043SAndy Whitcroft 3586773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 358713214adfSAndy Whitcroft 358813214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 358913214adfSAndy Whitcroft 3590aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 3591cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3592cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3593aad4f614SJoe Perches $allowed[$allow] = 1; 359413214adfSAndy Whitcroft } 359513214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3596cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3597aad4f614SJoe Perches $allowed[$allow] = 1; 359813214adfSAndy Whitcroft } 3599cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3600cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3601aad4f614SJoe Perches $allowed[$allow] = 1; 360213214adfSAndy Whitcroft } 3603aad4f614SJoe Perches $allow++; 360413214adfSAndy Whitcroft } 3605aad4f614SJoe Perches if ($seen) { 3606aad4f614SJoe Perches my $sum_allowed = 0; 3607aad4f614SJoe Perches foreach (@allowed) { 3608aad4f614SJoe Perches $sum_allowed += $_; 3609aad4f614SJoe Perches } 3610aad4f614SJoe Perches if ($sum_allowed == 0) { 3611000d1cc1SJoe Perches WARN("BRACES", 3612000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 3613aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 3614aad4f614SJoe Perches $seen != $allow) { 3615aad4f614SJoe Perches CHK("BRACES", 3616aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 3617aad4f614SJoe Perches } 361813214adfSAndy Whitcroft } 361913214adfSAndy Whitcroft } 362013214adfSAndy Whitcroft } 3621773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 362213214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 3623cf655043SAndy Whitcroft my $allowed = 0; 3624f0a594c1SAndy Whitcroft 3625cf655043SAndy Whitcroft # Check the pre-context. 3626cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 3627cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 3628cf655043SAndy Whitcroft $allowed = 1; 3629f0a594c1SAndy Whitcroft } 3630773647a0SAndy Whitcroft 3631773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 3632773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 3633773647a0SAndy Whitcroft 3634cf655043SAndy Whitcroft # Check the condition. 3635cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 3636773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 3637cf655043SAndy Whitcroft if (defined $cond) { 3638773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3639cf655043SAndy Whitcroft } 3640cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3641cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3642cf655043SAndy Whitcroft $allowed = 1; 3643cf655043SAndy Whitcroft } 3644cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3645cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3646cf655043SAndy Whitcroft $allowed = 1; 3647cf655043SAndy Whitcroft } 3648cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3649cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3650cf655043SAndy Whitcroft $allowed = 1; 3651cf655043SAndy Whitcroft } 3652cf655043SAndy Whitcroft # Check the post-context. 3653cf655043SAndy Whitcroft if (defined $chunks[1]) { 3654cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 3655cf655043SAndy Whitcroft if (defined $cond) { 3656773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3657cf655043SAndy Whitcroft } 3658cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 3659cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 3660cf655043SAndy Whitcroft $allowed = 1; 3661cf655043SAndy Whitcroft } 3662cf655043SAndy Whitcroft } 3663cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 366469932487SJustin P. Mattock my $herectx = $here . "\n"; 3665f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 3666cf655043SAndy Whitcroft 3667f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 366869932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 3669cf655043SAndy Whitcroft } 3670cf655043SAndy Whitcroft 3671000d1cc1SJoe Perches WARN("BRACES", 3672000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 3673f0a594c1SAndy Whitcroft } 3674f0a594c1SAndy Whitcroft } 3675f0a594c1SAndy Whitcroft 36760979ae66SJoe Perches# check for unnecessary blank lines around braces 367777b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 36780979ae66SJoe Perches CHK("BRACES", 36790979ae66SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 36800979ae66SJoe Perches } 368177b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 36820979ae66SJoe Perches CHK("BRACES", 36830979ae66SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 36840979ae66SJoe Perches } 36850979ae66SJoe Perches 36864a0df2efSAndy Whitcroft# no volatiles please 36876c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 36886c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 3689000d1cc1SJoe Perches WARN("VOLATILE", 3690000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 36914a0df2efSAndy Whitcroft } 36924a0df2efSAndy Whitcroft 369300df344fSAndy Whitcroft# warn about #if 0 3694c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 3695000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 3696000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 3697de7d4f0eSAndy Whitcroft $herecurr); 36984a0df2efSAndy Whitcroft } 36994a0df2efSAndy Whitcroft 370003df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 370103df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 370203df4b51SAndy Whitcroft my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 370303df4b51SAndy Whitcroft if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 370403df4b51SAndy Whitcroft WARN('NEEDLESS_IF', 370503df4b51SAndy Whitcroft "$1(NULL) is safe this check is probably not required\n" . $hereprev); 37064c432a8fSGreg Kroah-Hartman } 37074c432a8fSGreg Kroah-Hartman } 3708f0a594c1SAndy Whitcroft 37091a15a250SPatrick Pannuto# prefer usleep_range over udelay 371037581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 37111a15a250SPatrick Pannuto # ignore udelay's < 10, however 371237581c28SBruce Allan if (! ($1 < 10) ) { 3713000d1cc1SJoe Perches CHK("USLEEP_RANGE", 3714000d1cc1SJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); 37151a15a250SPatrick Pannuto } 37161a15a250SPatrick Pannuto } 37171a15a250SPatrick Pannuto 371809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 371909ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 372009ef8725SPatrick Pannuto if ($1 < 20) { 3721000d1cc1SJoe Perches WARN("MSLEEP", 3722000d1cc1SJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); 372309ef8725SPatrick Pannuto } 372409ef8725SPatrick Pannuto } 372509ef8725SPatrick Pannuto 372636ec1939SJoe Perches# check for comparisons of jiffies 372736ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 372836ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 372936ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 373036ec1939SJoe Perches } 373136ec1939SJoe Perches 37329d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 37339d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 37349d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 37359d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 37369d7a34a5SJoe Perches } 37379d7a34a5SJoe Perches 373800df344fSAndy Whitcroft# warn about #ifdefs in C files 3739c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 374000df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 374100df344fSAndy Whitcroft# print "$herecurr"; 374200df344fSAndy Whitcroft# $clean = 0; 374300df344fSAndy Whitcroft# } 374400df344fSAndy Whitcroft 374522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 3746c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 37473705ce5bSJoe Perches if (ERROR("SPACING", 37483705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 37493705ce5bSJoe Perches $fix) { 37503705ce5bSJoe Perches $fixed[$linenr - 1] =~ 37513705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 37523705ce5bSJoe Perches } 37533705ce5bSJoe Perches 375422f2a2efSAndy Whitcroft } 375522f2a2efSAndy Whitcroft 37564a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 3757171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 3758171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 37594a0df2efSAndy Whitcroft my $which = $1; 37604a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3761000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 3762000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 37634a0df2efSAndy Whitcroft } 37644a0df2efSAndy Whitcroft } 37654a0df2efSAndy Whitcroft# check for memory barriers without a comment. 37664a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 37674a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3768000d1cc1SJoe Perches CHK("MEMORY_BARRIER", 3769000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 37704a0df2efSAndy Whitcroft } 37714a0df2efSAndy Whitcroft } 37724a0df2efSAndy Whitcroft# check of hardware specific defines 3773c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 3774000d1cc1SJoe Perches CHK("ARCH_DEFINES", 3775000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 37760a920b5bSAndy Whitcroft } 3777653d4876SAndy Whitcroft 3778d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 3779d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 3780000d1cc1SJoe Perches WARN("STORAGE_CLASS", 3781000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 3782d4977c78STobias Klauser } 3783d4977c78STobias Klauser 3784de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 3785de7d4f0eSAndy Whitcroft# storage class and type. 37869c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 37879c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 3788000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 3789000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 3790de7d4f0eSAndy Whitcroft } 3791de7d4f0eSAndy Whitcroft 37928905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 37938905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 3794d5e616fcSJoe Perches if (WARN("INLINE", 3795d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 3796d5e616fcSJoe Perches $fix) { 3797d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/; 3798d5e616fcSJoe Perches 3799d5e616fcSJoe Perches } 38008905a67cSAndy Whitcroft } 38018905a67cSAndy Whitcroft 38023d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 38033d130fd0SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 3804000d1cc1SJoe Perches WARN("PREFER_PACKED", 3805000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 38063d130fd0SJoe Perches } 38073d130fd0SJoe Perches 380839b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 380939b7e287SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 3810000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 3811000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 381239b7e287SJoe Perches } 381339b7e287SJoe Perches 38145f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 38155f14d3bdSJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 3816d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 3817d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 3818d5e616fcSJoe Perches $fix) { 3819d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 3820d5e616fcSJoe Perches 3821d5e616fcSJoe Perches } 38225f14d3bdSJoe Perches } 38235f14d3bdSJoe Perches 38246061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 38256061d949SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 3826d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 3827d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 3828d5e616fcSJoe Perches $fix) { 3829d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 3830d5e616fcSJoe Perches } 38316061d949SJoe Perches } 38326061d949SJoe Perches 38338f53a9b8SJoe Perches# check for sizeof(&) 38348f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 3835000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 3836000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 38378f53a9b8SJoe Perches } 38388f53a9b8SJoe Perches 383966c80b60SJoe Perches# check for sizeof without parenthesis 384066c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 3841d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 3842d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 3843d5e616fcSJoe Perches $fix) { 3844d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 3845d5e616fcSJoe Perches } 384666c80b60SJoe Perches } 384766c80b60SJoe Perches 3848428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 3849428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 3850000d1cc1SJoe Perches WARN("LINE_CONTINUATIONS", 3851000d1cc1SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 3852428e2fdcSJoe Perches } 3853428e2fdcSJoe Perches 385488982feaSJoe Perches# check for struct spinlock declarations 385588982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 385688982feaSJoe Perches WARN("USE_SPINLOCK_T", 385788982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 385888982feaSJoe Perches } 385988982feaSJoe Perches 3860a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 3861a6962d72SJoe Perches if ($line =~ /\bseq_printf\s*\(/) { 3862a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 3863a6962d72SJoe Perches if ($fmt !~ /[^\\]\%/) { 3864d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 3865d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 3866d5e616fcSJoe Perches $fix) { 3867d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/; 3868d5e616fcSJoe Perches } 3869a6962d72SJoe Perches } 3870a6962d72SJoe Perches } 3871a6962d72SJoe Perches 3872554e165cSAndy Whitcroft# Check for misused memsets 3873d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3874d1fe9c09SJoe Perches defined $stat && 3875d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 3876554e165cSAndy Whitcroft 3877d7c76ba7SJoe Perches my $ms_addr = $2; 3878d1fe9c09SJoe Perches my $ms_val = $7; 3879d1fe9c09SJoe Perches my $ms_size = $12; 3880d7c76ba7SJoe Perches 3881554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 3882554e165cSAndy Whitcroft ERROR("MEMSET", 3883d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 3884554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 3885554e165cSAndy Whitcroft WARN("MEMSET", 3886d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 3887d7c76ba7SJoe Perches } 3888d7c76ba7SJoe Perches } 3889d7c76ba7SJoe Perches 3890d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 3891d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3892d1fe9c09SJoe Perches defined $stat && 3893d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 3894d1fe9c09SJoe Perches if (defined $2 || defined $7) { 3895d7c76ba7SJoe Perches my $call = $1; 3896d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 3897d7c76ba7SJoe Perches my $arg1 = $3; 3898d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 3899d1fe9c09SJoe Perches my $arg2 = $8; 3900d7c76ba7SJoe Perches my $cast; 3901d7c76ba7SJoe Perches 3902d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 3903d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 3904d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 3905d7c76ba7SJoe Perches $cast = $cast1; 3906d7c76ba7SJoe Perches } else { 3907d7c76ba7SJoe Perches $cast = $cast2; 3908d7c76ba7SJoe Perches } 3909d7c76ba7SJoe Perches WARN("MINMAX", 3910d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 3911554e165cSAndy Whitcroft } 3912554e165cSAndy Whitcroft } 3913554e165cSAndy Whitcroft 39144a273195SJoe Perches# check usleep_range arguments 39154a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 39164a273195SJoe Perches defined $stat && 39174a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 39184a273195SJoe Perches my $min = $1; 39194a273195SJoe Perches my $max = $7; 39204a273195SJoe Perches if ($min eq $max) { 39214a273195SJoe Perches WARN("USLEEP_RANGE", 39224a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 39234a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 39244a273195SJoe Perches $min > $max) { 39254a273195SJoe Perches WARN("USLEEP_RANGE", 39264a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 39274a273195SJoe Perches } 39284a273195SJoe Perches } 39294a273195SJoe Perches 393070dc8a48SJoe Perches# check for new externs in .h files. 393170dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 393270dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 393370dc8a48SJoe Perches if (WARN("AVOID_EXTERNS", 393470dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 393570dc8a48SJoe Perches $fix) { 393670dc8a48SJoe Perches $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 393770dc8a48SJoe Perches } 393870dc8a48SJoe Perches } 393970dc8a48SJoe Perches 3940de7d4f0eSAndy Whitcroft# check for new externs in .c files. 3941171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 3942c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3943171ae1a4SAndy Whitcroft { 3944c45dcabdSAndy Whitcroft my $function_name = $1; 3945c45dcabdSAndy Whitcroft my $paren_space = $2; 3946171ae1a4SAndy Whitcroft 3947171ae1a4SAndy Whitcroft my $s = $stat; 3948171ae1a4SAndy Whitcroft if (defined $cond) { 3949171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 3950171ae1a4SAndy Whitcroft } 3951c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 3952c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 3953c45dcabdSAndy Whitcroft { 3954000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3955000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3956de7d4f0eSAndy Whitcroft } 3957de7d4f0eSAndy Whitcroft 3958171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 3959000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 3960000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 3961171ae1a4SAndy Whitcroft } 39629c9ba34eSAndy Whitcroft 39639c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 39649c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 39659c9ba34eSAndy Whitcroft { 3966000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3967000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3968171ae1a4SAndy Whitcroft } 3969171ae1a4SAndy Whitcroft 3970de7d4f0eSAndy Whitcroft# checks for new __setup's 3971de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 3972de7d4f0eSAndy Whitcroft my $name = $1; 3973de7d4f0eSAndy Whitcroft 3974de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 3975000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 3976000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 3977de7d4f0eSAndy Whitcroft } 3978653d4876SAndy Whitcroft } 39799c0ca6f9SAndy Whitcroft 39809c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 3981caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 3982000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 3983000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 39849c0ca6f9SAndy Whitcroft } 398513214adfSAndy Whitcroft 3986a640d25cSJoe Perches# alloc style 3987a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 3988a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 3989a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 3990a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 3991a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 3992a640d25cSJoe Perches } 3993a640d25cSJoe Perches 3994972fdea2SJoe Perches# check for krealloc arg reuse 3995972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 3996972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 3997972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 3998972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 3999972fdea2SJoe Perches } 4000972fdea2SJoe Perches 40015ce59ae0SJoe Perches# check for alloc argument mismatch 40025ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 40035ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 40045ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 40055ce59ae0SJoe Perches } 40065ce59ae0SJoe Perches 4007caf2a54fSJoe Perches# check for multiple semicolons 4008caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 4009d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 4010d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 4011d5e616fcSJoe Perches $fix) { 4012d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g; 4013d5e616fcSJoe Perches } 4014d1e2ad07SJoe Perches } 4015d1e2ad07SJoe Perches 4016d1e2ad07SJoe Perches# check for switch/default statements without a break; 4017d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 4018d1e2ad07SJoe Perches defined $stat && 4019d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 4020d1e2ad07SJoe Perches my $ctx = ''; 4021d1e2ad07SJoe Perches my $herectx = $here . "\n"; 4022d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 4023d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4024d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4025d1e2ad07SJoe Perches } 4026d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 4027d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 4028caf2a54fSJoe Perches } 4029caf2a54fSJoe Perches 403013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 4031d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 4032d5e616fcSJoe Perches if (WARN("USE_FUNC", 4033d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 4034d5e616fcSJoe Perches $fix) { 4035d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g; 4036d5e616fcSJoe Perches } 403713214adfSAndy Whitcroft } 4038773647a0SAndy Whitcroft 40392c92488aSJoe Perches# check for use of yield() 40402c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 40412c92488aSJoe Perches WARN("YIELD", 40422c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 40432c92488aSJoe Perches } 40442c92488aSJoe Perches 4045179f8f40SJoe Perches# check for comparisons against true and false 4046179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 4047179f8f40SJoe Perches my $lead = $1; 4048179f8f40SJoe Perches my $arg = $2; 4049179f8f40SJoe Perches my $test = $3; 4050179f8f40SJoe Perches my $otype = $4; 4051179f8f40SJoe Perches my $trail = $5; 4052179f8f40SJoe Perches my $op = "!"; 4053179f8f40SJoe Perches 4054179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 4055179f8f40SJoe Perches 4056179f8f40SJoe Perches my $type = lc($otype); 4057179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 4058179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 4059179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 4060179f8f40SJoe Perches $op = ""; 4061179f8f40SJoe Perches } 4062179f8f40SJoe Perches 4063179f8f40SJoe Perches CHK("BOOL_COMPARISON", 4064179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 4065179f8f40SJoe Perches 4066179f8f40SJoe Perches## maybe suggesting a correct construct would better 4067179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 4068179f8f40SJoe Perches 4069179f8f40SJoe Perches } 4070179f8f40SJoe Perches } 4071179f8f40SJoe Perches 40724882720bSThomas Gleixner# check for semaphores initialized locked 40734882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 4074000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 4075000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 4076773647a0SAndy Whitcroft } 40776712d858SJoe Perches 407867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 407967d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 4080000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 408167d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 4082773647a0SAndy Whitcroft } 40836712d858SJoe Perches 4084f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 4085f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 4086000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 4087000d1cc1SJoe Perches "please use device_initcall() instead of __initcall()\n" . $herecurr); 4088f3db6639SMichael Ellerman } 40896712d858SJoe Perches 409079404849SEmese Revfy# check for various ops structs, ensure they are const. 409179404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 409279404849SEmese Revfy address_space_operations| 409379404849SEmese Revfy backlight_ops| 409479404849SEmese Revfy block_device_operations| 409579404849SEmese Revfy dentry_operations| 409679404849SEmese Revfy dev_pm_ops| 409779404849SEmese Revfy dma_map_ops| 409879404849SEmese Revfy extent_io_ops| 409979404849SEmese Revfy file_lock_operations| 410079404849SEmese Revfy file_operations| 410179404849SEmese Revfy hv_ops| 410279404849SEmese Revfy ide_dma_ops| 410379404849SEmese Revfy intel_dvo_dev_ops| 410479404849SEmese Revfy item_operations| 410579404849SEmese Revfy iwl_ops| 410679404849SEmese Revfy kgdb_arch| 410779404849SEmese Revfy kgdb_io| 410879404849SEmese Revfy kset_uevent_ops| 410979404849SEmese Revfy lock_manager_operations| 411079404849SEmese Revfy microcode_ops| 411179404849SEmese Revfy mtrr_ops| 411279404849SEmese Revfy neigh_ops| 411379404849SEmese Revfy nlmsvc_binding| 411479404849SEmese Revfy pci_raw_ops| 411579404849SEmese Revfy pipe_buf_operations| 411679404849SEmese Revfy platform_hibernation_ops| 411779404849SEmese Revfy platform_suspend_ops| 411879404849SEmese Revfy proto_ops| 411979404849SEmese Revfy rpc_pipe_ops| 412079404849SEmese Revfy seq_operations| 412179404849SEmese Revfy snd_ac97_build_ops| 412279404849SEmese Revfy soc_pcmcia_socket_ops| 412379404849SEmese Revfy stacktrace_ops| 412479404849SEmese Revfy sysfs_ops| 412579404849SEmese Revfy tty_operations| 412679404849SEmese Revfy usb_mon_operations| 412779404849SEmese Revfy wd_ops}x; 41286903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 412979404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 4130000d1cc1SJoe Perches WARN("CONST_STRUCT", 4131000d1cc1SJoe Perches "struct $1 should normally be const\n" . 41326903ffb2SAndy Whitcroft $herecurr); 41332b6db5cbSAndy Whitcroft } 4134773647a0SAndy Whitcroft 4135773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 4136773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 4137773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 4138c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 4139c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 4140171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 4141171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 4142171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 4143773647a0SAndy Whitcroft { 4144000d1cc1SJoe Perches WARN("NR_CPUS", 4145000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 4146773647a0SAndy Whitcroft } 41479c9ba34eSAndy Whitcroft 41489c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 41499c9ba34eSAndy Whitcroft my $string; 41509c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 41519c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 41522a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 41539c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 4154000d1cc1SJoe Perches WARN("PRINTF_L", 4155000d1cc1SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 41569c9ba34eSAndy Whitcroft last; 41579c9ba34eSAndy Whitcroft } 41589c9ba34eSAndy Whitcroft } 4159691d77b6SAndy Whitcroft 4160691d77b6SAndy Whitcroft# whine mightly about in_atomic 4161691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 4162691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 4163000d1cc1SJoe Perches ERROR("IN_ATOMIC", 4164000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 4165f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 4166000d1cc1SJoe Perches WARN("IN_ATOMIC", 4167000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 4168691d77b6SAndy Whitcroft } 4169691d77b6SAndy Whitcroft } 41701704f47bSPeter Zijlstra 41711704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 41721704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 41731704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 41741704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 41751704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 41761704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 4177000d1cc1SJoe Perches ERROR("LOCKDEP", 4178000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 41791704f47bSPeter Zijlstra } 41801704f47bSPeter Zijlstra } 418188f8831cSDave Jones 418288f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 418388f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 4184000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 4185000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 418688f8831cSDave Jones } 418713214adfSAndy Whitcroft } 418813214adfSAndy Whitcroft 418913214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 419013214adfSAndy Whitcroft # so just keep quiet. 419113214adfSAndy Whitcroft if ($#rawlines == -1) { 419213214adfSAndy Whitcroft exit(0); 41930a920b5bSAndy Whitcroft } 41940a920b5bSAndy Whitcroft 41958905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 41968905a67cSAndy Whitcroft # things that appear to be patches. 41978905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 41988905a67cSAndy Whitcroft exit(0); 41998905a67cSAndy Whitcroft } 42008905a67cSAndy Whitcroft 42018905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 42028905a67cSAndy Whitcroft # just keep quiet. 42038905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 42048905a67cSAndy Whitcroft exit(0); 42058905a67cSAndy Whitcroft } 42068905a67cSAndy Whitcroft 42078905a67cSAndy Whitcroft if (!$is_patch) { 4208000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 4209000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 42100a920b5bSAndy Whitcroft } 42110a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 4212000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 4213000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 42140a920b5bSAndy Whitcroft } 42150a920b5bSAndy Whitcroft 4216f0a594c1SAndy Whitcroft print report_dump(); 421713214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 421813214adfSAndy Whitcroft print "$filename " if ($summary_file); 42196c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 42206c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 42216c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 42228905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 42236c72ffaaSAndy Whitcroft } 42248905a67cSAndy Whitcroft 4225d2c0a235SAndy Whitcroft if ($quiet == 0) { 4226d1fe9c09SJoe Perches 4227d1fe9c09SJoe Perches if ($^V lt 5.10.0) { 4228d1fe9c09SJoe Perches print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); 4229d1fe9c09SJoe Perches print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); 4230d1fe9c09SJoe Perches } 4231d1fe9c09SJoe Perches 4232d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 4233d2c0a235SAndy Whitcroft # then suggest that. 4234d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 4235d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 4236d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 4237b0781216SMike Frysinger $rpt_cleaners = 0; 4238d2c0a235SAndy Whitcroft } 4239d2c0a235SAndy Whitcroft } 4240d2c0a235SAndy Whitcroft 424191bfe484SJoe Perches hash_show_words(\%use_type, "Used"); 424291bfe484SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 4243000d1cc1SJoe Perches 42443705ce5bSJoe Perches if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { 42453705ce5bSJoe Perches my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; 42463705ce5bSJoe Perches my $linecount = 0; 42473705ce5bSJoe Perches my $f; 42483705ce5bSJoe Perches 42493705ce5bSJoe Perches open($f, '>', $newfile) 42503705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 42513705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 42523705ce5bSJoe Perches $linecount++; 42533705ce5bSJoe Perches if ($file) { 42543705ce5bSJoe Perches if ($linecount > 3) { 42553705ce5bSJoe Perches $fixed_line =~ s/^\+//; 42563705ce5bSJoe Perches print $f $fixed_line. "\n"; 42573705ce5bSJoe Perches } 42583705ce5bSJoe Perches } else { 42593705ce5bSJoe Perches print $f $fixed_line . "\n"; 42603705ce5bSJoe Perches } 42613705ce5bSJoe Perches } 42623705ce5bSJoe Perches close($f); 42633705ce5bSJoe Perches 42643705ce5bSJoe Perches if (!$quiet) { 42653705ce5bSJoe Perches print << "EOM"; 42663705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 42673705ce5bSJoe Perches 42683705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 42693705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 42703705ce5bSJoe Perches 42713705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 42723705ce5bSJoe PerchesNo warranties, expressed or implied... 42733705ce5bSJoe Perches 42743705ce5bSJoe PerchesEOM 42753705ce5bSJoe Perches } 42763705ce5bSJoe Perches } 42773705ce5bSJoe Perches 42780a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 4279c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 42800a920b5bSAndy Whitcroft } 42810a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 4282000d1cc1SJoe Perches print << "EOM"; 4283000d1cc1SJoe Perches$vname has style problems, please review. 4284000d1cc1SJoe Perches 4285000d1cc1SJoe PerchesIf any of these errors are false positives, please report 4286000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 4287000d1cc1SJoe PerchesEOM 42880a920b5bSAndy Whitcroft } 428913214adfSAndy Whitcroft 42900a920b5bSAndy Whitcroft return $clean; 42910a920b5bSAndy Whitcroft} 4292