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; 90a920b5bSAndy Whitcroft 100a920b5bSAndy Whitcroftmy $P = $0; 1100df344fSAndy Whitcroft$P =~ s@.*/@@g; 120a920b5bSAndy Whitcroft 13000d1cc1SJoe Perchesmy $V = '0.32'; 140a920b5bSAndy Whitcroft 150a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 160a920b5bSAndy Whitcroft 170a920b5bSAndy Whitcroftmy $quiet = 0; 180a920b5bSAndy Whitcroftmy $tree = 1; 190a920b5bSAndy Whitcroftmy $chk_signoff = 1; 200a920b5bSAndy Whitcroftmy $chk_patch = 1; 21773647a0SAndy Whitcroftmy $tst_only; 226c72ffaaSAndy Whitcroftmy $emacs = 0; 238905a67cSAndy Whitcroftmy $terse = 0; 246c72ffaaSAndy Whitcroftmy $file = 0; 256c72ffaaSAndy Whitcroftmy $check = 0; 268905a67cSAndy Whitcroftmy $summary = 1; 278905a67cSAndy Whitcroftmy $mailback = 0; 2813214adfSAndy Whitcroftmy $summary_file = 0; 29000d1cc1SJoe Perchesmy $show_types = 0; 306c72ffaaSAndy Whitcroftmy $root; 31c2fdda0dSAndy Whitcroftmy %debug; 32000d1cc1SJoe Perchesmy %ignore_type = (); 33000d1cc1SJoe Perchesmy @ignore = (); 3477f5b10aSHannes Edermy $help = 0; 35000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 3677f5b10aSHannes Eder 3777f5b10aSHannes Edersub help { 3877f5b10aSHannes Eder my ($exitcode) = @_; 3977f5b10aSHannes Eder 4077f5b10aSHannes Eder print << "EOM"; 4177f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 4277f5b10aSHannes EderVersion: $V 4377f5b10aSHannes Eder 4477f5b10aSHannes EderOptions: 4577f5b10aSHannes Eder -q, --quiet quiet 4677f5b10aSHannes Eder --no-tree run without a kernel tree 4777f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 4877f5b10aSHannes Eder --patch treat FILE as patchfile (default) 4977f5b10aSHannes Eder --emacs emacs compile window format 5077f5b10aSHannes Eder --terse one line per report 5177f5b10aSHannes Eder -f, --file treat FILE as regular source file 5277f5b10aSHannes Eder --subjective, --strict enable more subjective tests 53000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 54000d1cc1SJoe Perches --show-types show the message "types" in the output 5577f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 5677f5b10aSHannes Eder --no-summary suppress the per-file summary 5777f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 5877f5b10aSHannes Eder --summary-file include the filename in summary 5977f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 6077f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 6177f5b10aSHannes Eder is all off) 6277f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 6377f5b10aSHannes Eder literally 6477f5b10aSHannes Eder -h, --help, --version display this help and exit 6577f5b10aSHannes Eder 6677f5b10aSHannes EderWhen FILE is - read standard input. 6777f5b10aSHannes EderEOM 6877f5b10aSHannes Eder 6977f5b10aSHannes Eder exit($exitcode); 7077f5b10aSHannes Eder} 7177f5b10aSHannes Eder 72000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 73000d1cc1SJoe Perchesif (-f $conf) { 74000d1cc1SJoe Perches my @conf_args; 75000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 76000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 77000d1cc1SJoe Perches 78000d1cc1SJoe Perches while (<$conffile>) { 79000d1cc1SJoe Perches my $line = $_; 80000d1cc1SJoe Perches 81000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 82000d1cc1SJoe Perches $line =~ s/^\s*//g; 83000d1cc1SJoe Perches $line =~ s/\s+/ /g; 84000d1cc1SJoe Perches 85000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 86000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 87000d1cc1SJoe Perches 88000d1cc1SJoe Perches my @words = split(" ", $line); 89000d1cc1SJoe Perches foreach my $word (@words) { 90000d1cc1SJoe Perches last if ($word =~ m/^#/); 91000d1cc1SJoe Perches push (@conf_args, $word); 92000d1cc1SJoe Perches } 93000d1cc1SJoe Perches } 94000d1cc1SJoe Perches close($conffile); 95000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 96000d1cc1SJoe Perches} 97000d1cc1SJoe Perches 980a920b5bSAndy WhitcroftGetOptions( 996c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1000a920b5bSAndy Whitcroft 'tree!' => \$tree, 1010a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1020a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1036c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1048905a67cSAndy Whitcroft 'terse!' => \$terse, 10577f5b10aSHannes Eder 'f|file!' => \$file, 1066c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1076c72ffaaSAndy Whitcroft 'strict!' => \$check, 108000d1cc1SJoe Perches 'ignore=s' => \@ignore, 109000d1cc1SJoe Perches 'show-types!' => \$show_types, 1106c72ffaaSAndy Whitcroft 'root=s' => \$root, 1118905a67cSAndy Whitcroft 'summary!' => \$summary, 1128905a67cSAndy Whitcroft 'mailback!' => \$mailback, 11313214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 11413214adfSAndy Whitcroft 115c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 116773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 11777f5b10aSHannes Eder 'h|help' => \$help, 11877f5b10aSHannes Eder 'version' => \$help 11977f5b10aSHannes Eder) or help(1); 12077f5b10aSHannes Eder 12177f5b10aSHannes Ederhelp(0) if ($help); 1220a920b5bSAndy Whitcroft 1230a920b5bSAndy Whitcroftmy $exit = 0; 1240a920b5bSAndy Whitcroft 1250a920b5bSAndy Whitcroftif ($#ARGV < 0) { 12677f5b10aSHannes Eder print "$P: no input files\n"; 1270a920b5bSAndy Whitcroft exit(1); 1280a920b5bSAndy Whitcroft} 1290a920b5bSAndy Whitcroft 130000d1cc1SJoe Perches@ignore = split(/,/, join(',',@ignore)); 131000d1cc1SJoe Perchesforeach my $word (@ignore) { 132000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 133000d1cc1SJoe Perches $word =~ s/^\s*//g; 134000d1cc1SJoe Perches $word =~ s/\s+/ /g; 135000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 136000d1cc1SJoe Perches 137000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 138000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 139000d1cc1SJoe Perches 140000d1cc1SJoe Perches $ignore_type{$word}++; 141000d1cc1SJoe Perches} 142000d1cc1SJoe Perches 143c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 144c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 1457429c690SAndy Whitcroftmy $dbg_type = 0; 146a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 147c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 14821caa13cSAndy Whitcroft ## no critic 14921caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 15021caa13cSAndy Whitcroft die "$@" if ($@); 151c2fdda0dSAndy Whitcroft} 152c2fdda0dSAndy Whitcroft 153d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 154d2c0a235SAndy Whitcroft 1558905a67cSAndy Whitcroftif ($terse) { 1568905a67cSAndy Whitcroft $emacs = 1; 1578905a67cSAndy Whitcroft $quiet++; 1588905a67cSAndy Whitcroft} 1598905a67cSAndy Whitcroft 1606c72ffaaSAndy Whitcroftif ($tree) { 1616c72ffaaSAndy Whitcroft if (defined $root) { 1626c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 1636c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 1646c72ffaaSAndy Whitcroft } 1656c72ffaaSAndy Whitcroft } else { 1666c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 1676c72ffaaSAndy Whitcroft $root = '.'; 1686c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 1696c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 1706c72ffaaSAndy Whitcroft $root = $1; 1716c72ffaaSAndy Whitcroft } 1726c72ffaaSAndy Whitcroft } 1736c72ffaaSAndy Whitcroft 1746c72ffaaSAndy Whitcroft if (!defined $root) { 1750a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 1760a920b5bSAndy Whitcroft exit(2); 1770a920b5bSAndy Whitcroft } 1786c72ffaaSAndy Whitcroft} 1796c72ffaaSAndy Whitcroft 1806c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 1816c72ffaaSAndy Whitcroft 1822ceb532bSAndy Whitcroftour $Ident = qr{ 1832ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 1842ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 1852ceb532bSAndy Whitcroft }x; 1866c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 1876c72ffaaSAndy Whitcroftour $Sparse = qr{ 1886c72ffaaSAndy Whitcroft __user| 1896c72ffaaSAndy Whitcroft __kernel| 1906c72ffaaSAndy Whitcroft __force| 1916c72ffaaSAndy Whitcroft __iomem| 1926c72ffaaSAndy Whitcroft __must_check| 1936c72ffaaSAndy Whitcroft __init_refok| 194417495edSAndy Whitcroft __kprobes| 195165e72a6SSven Eckelmann __ref| 196165e72a6SSven Eckelmann __rcu 1976c72ffaaSAndy Whitcroft }x; 19852131292SWolfram Sang 19952131292SWolfram Sang# Notes to $Attribute: 20052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 2016c72ffaaSAndy Whitcroftour $Attribute = qr{ 2026c72ffaaSAndy Whitcroft const| 20303f1df7dSJoe Perches __percpu| 20403f1df7dSJoe Perches __nocast| 20503f1df7dSJoe Perches __safe| 20603f1df7dSJoe Perches __bitwise__| 20703f1df7dSJoe Perches __packed__| 20803f1df7dSJoe Perches __packed2__| 20903f1df7dSJoe Perches __naked| 21003f1df7dSJoe Perches __maybe_unused| 21103f1df7dSJoe Perches __always_unused| 21203f1df7dSJoe Perches __noreturn| 21303f1df7dSJoe Perches __used| 21403f1df7dSJoe Perches __cold| 21503f1df7dSJoe Perches __noclone| 21603f1df7dSJoe Perches __deprecated| 2176c72ffaaSAndy Whitcroft __read_mostly| 2186c72ffaaSAndy Whitcroft __kprobes| 21952131292SWolfram Sang __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| 22024e1d81aSAndy Whitcroft ____cacheline_aligned| 22124e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 2225fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 2235fe3af11SAndy Whitcroft __weak 2246c72ffaaSAndy Whitcroft }x; 225c45dcabdSAndy Whitcroftour $Modifier; 2266c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 2276c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 2286c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 2296c72ffaaSAndy Whitcroft 2306c72ffaaSAndy Whitcroftour $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 2316c72ffaaSAndy Whitcroftour $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 23286f9d059SAndy Whitcroftour $Compare = qr{<=|>=|==|!=|<|>}; 2336c72ffaaSAndy Whitcroftour $Operators = qr{ 2346c72ffaaSAndy Whitcroft <=|>=|==|!=| 2356c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 236c2fdda0dSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 2376c72ffaaSAndy Whitcroft }x; 2386c72ffaaSAndy Whitcroft 2398905a67cSAndy Whitcroftour $NonptrType; 2408905a67cSAndy Whitcroftour $Type; 2418905a67cSAndy Whitcroftour $Declare; 2428905a67cSAndy Whitcroft 243171ae1a4SAndy Whitcroftour $UTF8 = qr { 244171ae1a4SAndy Whitcroft [\x09\x0A\x0D\x20-\x7E] # ASCII 245171ae1a4SAndy Whitcroft | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 246171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 247171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 248171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 249171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 250171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 251171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 252171ae1a4SAndy Whitcroft}x; 253171ae1a4SAndy Whitcroft 2548ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 255fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 2568ed22cadSAndy Whitcroft atomic_t 2578ed22cadSAndy Whitcroft)}; 2588ed22cadSAndy Whitcroft 259691e669bSJoe Perchesour $logFunctions = qr{(?x: 2606e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 2616e60c02eSJoe Perches [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 2626e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 263b0531722SJoe Perches panic| 264b0531722SJoe Perches MODULE_[A-Z_]+ 265691e669bSJoe Perches)}; 266691e669bSJoe Perches 26720112475SJoe Perchesour $signature_tags = qr{(?xi: 26820112475SJoe Perches Signed-off-by:| 26920112475SJoe Perches Acked-by:| 27020112475SJoe Perches Tested-by:| 27120112475SJoe Perches Reviewed-by:| 27220112475SJoe Perches Reported-by:| 27320112475SJoe Perches To:| 27420112475SJoe Perches Cc: 27520112475SJoe Perches)}; 27620112475SJoe Perches 2778905a67cSAndy Whitcroftour @typeList = ( 2788905a67cSAndy Whitcroft qr{void}, 279c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 280c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 281c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 282c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 283c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 284c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 285c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 2868905a67cSAndy Whitcroft qr{unsigned}, 2878905a67cSAndy Whitcroft qr{float}, 2888905a67cSAndy Whitcroft qr{double}, 2898905a67cSAndy Whitcroft qr{bool}, 2908905a67cSAndy Whitcroft qr{struct\s+$Ident}, 2918905a67cSAndy Whitcroft qr{union\s+$Ident}, 2928905a67cSAndy Whitcroft qr{enum\s+$Ident}, 2938905a67cSAndy Whitcroft qr{${Ident}_t}, 2948905a67cSAndy Whitcroft qr{${Ident}_handler}, 2958905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 2968905a67cSAndy Whitcroft); 297c45dcabdSAndy Whitcroftour @modifierList = ( 298c45dcabdSAndy Whitcroft qr{fastcall}, 299c45dcabdSAndy Whitcroft); 3008905a67cSAndy Whitcroft 3017840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 3027840a94cSWolfram Sang irq| 3037840a94cSWolfram Sang memory 3047840a94cSWolfram Sang)}; 3057840a94cSWolfram Sang# memory.h: ARM has a custom one 3067840a94cSWolfram Sang 3078905a67cSAndy Whitcroftsub build_types { 308d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 309d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 310c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 3118905a67cSAndy Whitcroft $NonptrType = qr{ 312d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 313cf655043SAndy Whitcroft (?: 314c45dcabdSAndy Whitcroft (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 3158ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 316c45dcabdSAndy Whitcroft (?:${all}\b) 317cf655043SAndy Whitcroft ) 318c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 3198905a67cSAndy Whitcroft }x; 3208905a67cSAndy Whitcroft $Type = qr{ 321c45dcabdSAndy Whitcroft $NonptrType 32265863862SAndy Whitcroft (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? 323c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 3248905a67cSAndy Whitcroft }x; 3258905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 3268905a67cSAndy Whitcroft} 3278905a67cSAndy Whitcroftbuild_types(); 3286c72ffaaSAndy Whitcroft 3297d2367afSJoe Perchesour $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/; 3307d2367afSJoe Perches 3317d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 3327d2367afSJoe Perchesour $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*}; 3337d2367afSJoe Perches 3347d2367afSJoe Perchessub deparenthesize { 3357d2367afSJoe Perches my ($string) = @_; 3367d2367afSJoe Perches return "" if (!defined($string)); 3377d2367afSJoe Perches $string =~ s@^\s*\(\s*@@g; 3387d2367afSJoe Perches $string =~ s@\s*\)\s*$@@g; 3397d2367afSJoe Perches $string =~ s@\s+@ @g; 3407d2367afSJoe Perches return $string; 3417d2367afSJoe Perches} 3427d2367afSJoe Perches 3436c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 3440a920b5bSAndy Whitcroft 3454a0df2efSAndy Whitcroftmy @dep_includes = (); 3464a0df2efSAndy Whitcroftmy @dep_functions = (); 3476c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 3486c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 34921caa13cSAndy Whitcroft open(my $REMOVE, '<', "$root/$removal") || 3506c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 35121caa13cSAndy Whitcroft while (<$REMOVE>) { 352f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 353f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 354f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 3554a0df2efSAndy Whitcroft push(@dep_includes, $1); 3564a0df2efSAndy Whitcroft 357f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 358f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 359f0a594c1SAndy Whitcroft } 3604a0df2efSAndy Whitcroft } 3610a920b5bSAndy Whitcroft } 3620a920b5bSAndy Whitcroft } 36321caa13cSAndy Whitcroft close($REMOVE); 3640a920b5bSAndy Whitcroft} 3650a920b5bSAndy Whitcroft 36600df344fSAndy Whitcroftmy @rawlines = (); 367c2fdda0dSAndy Whitcroftmy @lines = (); 368c2fdda0dSAndy Whitcroftmy $vname; 3696c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 37021caa13cSAndy Whitcroft my $FILE; 3716c72ffaaSAndy Whitcroft if ($file) { 37221caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 3736c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 37421caa13cSAndy Whitcroft } elsif ($filename eq '-') { 37521caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 3766c72ffaaSAndy Whitcroft } else { 37721caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 3786c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 3796c72ffaaSAndy Whitcroft } 380c2fdda0dSAndy Whitcroft if ($filename eq '-') { 381c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 382c2fdda0dSAndy Whitcroft } else { 383c2fdda0dSAndy Whitcroft $vname = $filename; 384c2fdda0dSAndy Whitcroft } 38521caa13cSAndy Whitcroft while (<$FILE>) { 3860a920b5bSAndy Whitcroft chomp; 38700df344fSAndy Whitcroft push(@rawlines, $_); 3886c72ffaaSAndy Whitcroft } 38921caa13cSAndy Whitcroft close($FILE); 390c2fdda0dSAndy Whitcroft if (!process($filename)) { 3910a920b5bSAndy Whitcroft $exit = 1; 3920a920b5bSAndy Whitcroft } 39300df344fSAndy Whitcroft @rawlines = (); 39413214adfSAndy Whitcroft @lines = (); 3950a920b5bSAndy Whitcroft} 3960a920b5bSAndy Whitcroft 3970a920b5bSAndy Whitcroftexit($exit); 3980a920b5bSAndy Whitcroft 3990a920b5bSAndy Whitcroftsub top_of_kernel_tree { 4006c72ffaaSAndy Whitcroft my ($root) = @_; 4016c72ffaaSAndy Whitcroft 4026c72ffaaSAndy Whitcroft my @tree_check = ( 4036c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 4046c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 4056c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 4066c72ffaaSAndy Whitcroft ); 4076c72ffaaSAndy Whitcroft 4086c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 4096c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 4100a920b5bSAndy Whitcroft return 0; 4110a920b5bSAndy Whitcroft } 4126c72ffaaSAndy Whitcroft } 4136c72ffaaSAndy Whitcroft return 1; 4146c72ffaaSAndy Whitcroft } 4150a920b5bSAndy Whitcroft 41620112475SJoe Perchessub parse_email { 41720112475SJoe Perches my ($formatted_email) = @_; 41820112475SJoe Perches 41920112475SJoe Perches my $name = ""; 42020112475SJoe Perches my $address = ""; 42120112475SJoe Perches my $comment = ""; 42220112475SJoe Perches 42320112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 42420112475SJoe Perches $name = $1; 42520112475SJoe Perches $address = $2; 42620112475SJoe Perches $comment = $3 if defined $3; 42720112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 42820112475SJoe Perches $address = $1; 42920112475SJoe Perches $comment = $2 if defined $2; 43020112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 43120112475SJoe Perches $address = $1; 43220112475SJoe Perches $comment = $2 if defined $2; 43320112475SJoe Perches $formatted_email =~ s/$address.*$//; 43420112475SJoe Perches $name = $formatted_email; 43520112475SJoe Perches $name =~ s/^\s+|\s+$//g; 43620112475SJoe Perches $name =~ s/^\"|\"$//g; 43720112475SJoe Perches # If there's a name left after stripping spaces and 43820112475SJoe Perches # leading quotes, and the address doesn't have both 43920112475SJoe Perches # leading and trailing angle brackets, the address 44020112475SJoe Perches # is invalid. ie: 44120112475SJoe Perches # "joe smith [email protected]" bad 44220112475SJoe Perches # "joe smith <[email protected]" bad 44320112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 44420112475SJoe Perches $name = ""; 44520112475SJoe Perches $address = ""; 44620112475SJoe Perches $comment = ""; 44720112475SJoe Perches } 44820112475SJoe Perches } 44920112475SJoe Perches 45020112475SJoe Perches $name =~ s/^\s+|\s+$//g; 45120112475SJoe Perches $name =~ s/^\"|\"$//g; 45220112475SJoe Perches $address =~ s/^\s+|\s+$//g; 45320112475SJoe Perches $address =~ s/^\<|\>$//g; 45420112475SJoe Perches 45520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 45620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 45720112475SJoe Perches $name = "\"$name\""; 45820112475SJoe Perches } 45920112475SJoe Perches 46020112475SJoe Perches return ($name, $address, $comment); 46120112475SJoe Perches} 46220112475SJoe Perches 46320112475SJoe Perchessub format_email { 46420112475SJoe Perches my ($name, $address) = @_; 46520112475SJoe Perches 46620112475SJoe Perches my $formatted_email; 46720112475SJoe Perches 46820112475SJoe Perches $name =~ s/^\s+|\s+$//g; 46920112475SJoe Perches $name =~ s/^\"|\"$//g; 47020112475SJoe Perches $address =~ s/^\s+|\s+$//g; 47120112475SJoe Perches 47220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 47320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 47420112475SJoe Perches $name = "\"$name\""; 47520112475SJoe Perches } 47620112475SJoe Perches 47720112475SJoe Perches if ("$name" eq "") { 47820112475SJoe Perches $formatted_email = "$address"; 47920112475SJoe Perches } else { 48020112475SJoe Perches $formatted_email = "$name <$address>"; 48120112475SJoe Perches } 48220112475SJoe Perches 48320112475SJoe Perches return $formatted_email; 48420112475SJoe Perches} 48520112475SJoe Perches 486000d1cc1SJoe Perchessub which_conf { 487000d1cc1SJoe Perches my ($conf) = @_; 488000d1cc1SJoe Perches 489000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 490000d1cc1SJoe Perches if (-e "$path/$conf") { 491000d1cc1SJoe Perches return "$path/$conf"; 492000d1cc1SJoe Perches } 493000d1cc1SJoe Perches } 494000d1cc1SJoe Perches 495000d1cc1SJoe Perches return ""; 496000d1cc1SJoe Perches} 497000d1cc1SJoe Perches 4980a920b5bSAndy Whitcroftsub expand_tabs { 4990a920b5bSAndy Whitcroft my ($str) = @_; 5000a920b5bSAndy Whitcroft 5010a920b5bSAndy Whitcroft my $res = ''; 5020a920b5bSAndy Whitcroft my $n = 0; 5030a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 5040a920b5bSAndy Whitcroft if ($c eq "\t") { 5050a920b5bSAndy Whitcroft $res .= ' '; 5060a920b5bSAndy Whitcroft $n++; 5070a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 5080a920b5bSAndy Whitcroft $res .= ' '; 5090a920b5bSAndy Whitcroft } 5100a920b5bSAndy Whitcroft next; 5110a920b5bSAndy Whitcroft } 5120a920b5bSAndy Whitcroft $res .= $c; 5130a920b5bSAndy Whitcroft $n++; 5140a920b5bSAndy Whitcroft } 5150a920b5bSAndy Whitcroft 5160a920b5bSAndy Whitcroft return $res; 5170a920b5bSAndy Whitcroft} 5186c72ffaaSAndy Whitcroftsub copy_spacing { 519773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 5206c72ffaaSAndy Whitcroft return $res; 5216c72ffaaSAndy Whitcroft} 5220a920b5bSAndy Whitcroft 5234a0df2efSAndy Whitcroftsub line_stats { 5244a0df2efSAndy Whitcroft my ($line) = @_; 5254a0df2efSAndy Whitcroft 5264a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 5274a0df2efSAndy Whitcroft $line =~ s/^.//; 5284a0df2efSAndy Whitcroft $line = expand_tabs($line); 5294a0df2efSAndy Whitcroft 5304a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 5314a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 5324a0df2efSAndy Whitcroft 5334a0df2efSAndy Whitcroft return (length($line), length($white)); 5344a0df2efSAndy Whitcroft} 5354a0df2efSAndy Whitcroft 536773647a0SAndy Whitcroftmy $sanitise_quote = ''; 537773647a0SAndy Whitcroft 538773647a0SAndy Whitcroftsub sanitise_line_reset { 539773647a0SAndy Whitcroft my ($in_comment) = @_; 540773647a0SAndy Whitcroft 541773647a0SAndy Whitcroft if ($in_comment) { 542773647a0SAndy Whitcroft $sanitise_quote = '*/'; 543773647a0SAndy Whitcroft } else { 544773647a0SAndy Whitcroft $sanitise_quote = ''; 545773647a0SAndy Whitcroft } 546773647a0SAndy Whitcroft} 54700df344fSAndy Whitcroftsub sanitise_line { 54800df344fSAndy Whitcroft my ($line) = @_; 54900df344fSAndy Whitcroft 55000df344fSAndy Whitcroft my $res = ''; 55100df344fSAndy Whitcroft my $l = ''; 55200df344fSAndy Whitcroft 553c2fdda0dSAndy Whitcroft my $qlen = 0; 554773647a0SAndy Whitcroft my $off = 0; 555773647a0SAndy Whitcroft my $c; 55600df344fSAndy Whitcroft 557773647a0SAndy Whitcroft # Always copy over the diff marker. 558773647a0SAndy Whitcroft $res = substr($line, 0, 1); 559773647a0SAndy Whitcroft 560773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 561773647a0SAndy Whitcroft $c = substr($line, $off, 1); 562773647a0SAndy Whitcroft 563773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 564773647a0SAndy Whitcroft # and end, all to $;. 565773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 566773647a0SAndy Whitcroft $sanitise_quote = '*/'; 567773647a0SAndy Whitcroft 568773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 569773647a0SAndy Whitcroft $off++; 57000df344fSAndy Whitcroft next; 571773647a0SAndy Whitcroft } 57281bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 573773647a0SAndy Whitcroft $sanitise_quote = ''; 574773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 575773647a0SAndy Whitcroft $off++; 576773647a0SAndy Whitcroft next; 577773647a0SAndy Whitcroft } 578113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 579113f04a8SDaniel Walker $sanitise_quote = '//'; 580113f04a8SDaniel Walker 581113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 582113f04a8SDaniel Walker $off++; 583113f04a8SDaniel Walker next; 584113f04a8SDaniel Walker } 585773647a0SAndy Whitcroft 586773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 587773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 588773647a0SAndy Whitcroft $c eq "\\") { 589773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 590773647a0SAndy Whitcroft $off++; 591773647a0SAndy Whitcroft next; 592773647a0SAndy Whitcroft } 593773647a0SAndy Whitcroft # Regular quotes. 594773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 595773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 596773647a0SAndy Whitcroft $sanitise_quote = $c; 597773647a0SAndy Whitcroft 598773647a0SAndy Whitcroft substr($res, $off, 1, $c); 599773647a0SAndy Whitcroft next; 600773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 601773647a0SAndy Whitcroft $sanitise_quote = ''; 60200df344fSAndy Whitcroft } 60300df344fSAndy Whitcroft } 604773647a0SAndy Whitcroft 605fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 606773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 607773647a0SAndy Whitcroft substr($res, $off, 1, $;); 608113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 609113f04a8SDaniel Walker substr($res, $off, 1, $;); 610773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 611773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 61200df344fSAndy Whitcroft } else { 613773647a0SAndy Whitcroft substr($res, $off, 1, $c); 61400df344fSAndy Whitcroft } 615c2fdda0dSAndy Whitcroft } 616c2fdda0dSAndy Whitcroft 617113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 618113f04a8SDaniel Walker $sanitise_quote = ''; 619113f04a8SDaniel Walker } 620113f04a8SDaniel Walker 621c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 622c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 623c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 624c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 625c2fdda0dSAndy Whitcroft 626c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 627c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 628c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 629c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 630c2fdda0dSAndy Whitcroft } 631c2fdda0dSAndy Whitcroft 63200df344fSAndy Whitcroft return $res; 63300df344fSAndy Whitcroft} 63400df344fSAndy Whitcroft 6358905a67cSAndy Whitcroftsub ctx_statement_block { 6368905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 6378905a67cSAndy Whitcroft my $line = $linenr - 1; 6388905a67cSAndy Whitcroft my $blk = ''; 6398905a67cSAndy Whitcroft my $soff = $off; 6408905a67cSAndy Whitcroft my $coff = $off - 1; 641773647a0SAndy Whitcroft my $coff_set = 0; 6428905a67cSAndy Whitcroft 64313214adfSAndy Whitcroft my $loff = 0; 64413214adfSAndy Whitcroft 6458905a67cSAndy Whitcroft my $type = ''; 6468905a67cSAndy Whitcroft my $level = 0; 647a2750645SAndy Whitcroft my @stack = (); 648cf655043SAndy Whitcroft my $p; 6498905a67cSAndy Whitcroft my $c; 6508905a67cSAndy Whitcroft my $len = 0; 65113214adfSAndy Whitcroft 65213214adfSAndy Whitcroft my $remainder; 6538905a67cSAndy Whitcroft while (1) { 654a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 655a2750645SAndy Whitcroft 656773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 6578905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 6588905a67cSAndy Whitcroft # context. 6598905a67cSAndy Whitcroft if ($off >= $len) { 6608905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 661dea33496SAndy Whitcroft last if (!defined $lines[$line]); 662c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 6638905a67cSAndy Whitcroft $remain--; 66413214adfSAndy Whitcroft $loff = $len; 665c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 6668905a67cSAndy Whitcroft $len = length($blk); 6678905a67cSAndy Whitcroft $line++; 6688905a67cSAndy Whitcroft last; 6698905a67cSAndy Whitcroft } 6708905a67cSAndy Whitcroft # Bail if there is no further context. 6718905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 67213214adfSAndy Whitcroft if ($off >= $len) { 6738905a67cSAndy Whitcroft last; 6748905a67cSAndy Whitcroft } 6758905a67cSAndy Whitcroft } 676cf655043SAndy Whitcroft $p = $c; 6778905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 67813214adfSAndy Whitcroft $remainder = substr($blk, $off); 6798905a67cSAndy Whitcroft 680773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 6814635f4fbSAndy Whitcroft 6824635f4fbSAndy Whitcroft # Handle nested #if/#else. 6834635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 6844635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 6854635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 6864635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 6874635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 6884635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 6894635f4fbSAndy Whitcroft } 6904635f4fbSAndy Whitcroft 6918905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 6928905a67cSAndy Whitcroft # outermost level. 6938905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 6948905a67cSAndy Whitcroft last; 6958905a67cSAndy Whitcroft } 6968905a67cSAndy Whitcroft 69713214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 698773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 699773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 700773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 701773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 702773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 703773647a0SAndy Whitcroft $coff_set = 1; 704773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 705773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 70613214adfSAndy Whitcroft } 70713214adfSAndy Whitcroft 7088905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 7098905a67cSAndy Whitcroft $level++; 7108905a67cSAndy Whitcroft $type = '('; 7118905a67cSAndy Whitcroft } 7128905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 7138905a67cSAndy Whitcroft $level--; 7148905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 7158905a67cSAndy Whitcroft 7168905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 7178905a67cSAndy Whitcroft $coff = $off; 718773647a0SAndy Whitcroft $coff_set = 1; 719773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 7208905a67cSAndy Whitcroft } 7218905a67cSAndy Whitcroft } 7228905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 7238905a67cSAndy Whitcroft $level++; 7248905a67cSAndy Whitcroft $type = '{'; 7258905a67cSAndy Whitcroft } 7268905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 7278905a67cSAndy Whitcroft $level--; 7288905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 7298905a67cSAndy Whitcroft 7308905a67cSAndy Whitcroft if ($level == 0) { 731b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 732b998e001SPatrick Pannuto $off++; 733b998e001SPatrick Pannuto } 7348905a67cSAndy Whitcroft last; 7358905a67cSAndy Whitcroft } 7368905a67cSAndy Whitcroft } 7378905a67cSAndy Whitcroft $off++; 7388905a67cSAndy Whitcroft } 739a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 74013214adfSAndy Whitcroft if ($off == $len) { 741a3bb97a7SAndy Whitcroft $loff = $len + 1; 74213214adfSAndy Whitcroft $line++; 74313214adfSAndy Whitcroft $remain--; 74413214adfSAndy Whitcroft } 7458905a67cSAndy Whitcroft 7468905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 7478905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 7488905a67cSAndy Whitcroft 7498905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 7508905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 7518905a67cSAndy Whitcroft 752773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 75313214adfSAndy Whitcroft 75413214adfSAndy Whitcroft return ($statement, $condition, 75513214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 75613214adfSAndy Whitcroft} 75713214adfSAndy Whitcroft 758cf655043SAndy Whitcroftsub statement_lines { 759cf655043SAndy Whitcroft my ($stmt) = @_; 760cf655043SAndy Whitcroft 761cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 762cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 763cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 764cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 765cf655043SAndy Whitcroft 766cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 767cf655043SAndy Whitcroft 768cf655043SAndy Whitcroft return $#stmt_lines + 2; 769cf655043SAndy Whitcroft} 770cf655043SAndy Whitcroft 771cf655043SAndy Whitcroftsub statement_rawlines { 772cf655043SAndy Whitcroft my ($stmt) = @_; 773cf655043SAndy Whitcroft 774cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 775cf655043SAndy Whitcroft 776cf655043SAndy Whitcroft return $#stmt_lines + 2; 777cf655043SAndy Whitcroft} 778cf655043SAndy Whitcroft 779cf655043SAndy Whitcroftsub statement_block_size { 780cf655043SAndy Whitcroft my ($stmt) = @_; 781cf655043SAndy Whitcroft 782cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 783cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 784cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 785cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 786cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 787cf655043SAndy Whitcroft 788cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 789cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 790cf655043SAndy Whitcroft 791cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 792cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 793cf655043SAndy Whitcroft 794cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 795cf655043SAndy Whitcroft return $stmt_lines; 796cf655043SAndy Whitcroft } else { 797cf655043SAndy Whitcroft return $stmt_statements; 798cf655043SAndy Whitcroft } 799cf655043SAndy Whitcroft} 800cf655043SAndy Whitcroft 80113214adfSAndy Whitcroftsub ctx_statement_full { 80213214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 80313214adfSAndy Whitcroft my ($statement, $condition, $level); 80413214adfSAndy Whitcroft 80513214adfSAndy Whitcroft my (@chunks); 80613214adfSAndy Whitcroft 807cf655043SAndy Whitcroft # Grab the first conditional/block pair. 80813214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 80913214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 810773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 81113214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 812cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 813cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 814cf655043SAndy Whitcroft } 815cf655043SAndy Whitcroft 816cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 817cf655043SAndy Whitcroft # could continue the statement. 818cf655043SAndy Whitcroft for (;;) { 81913214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 82013214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 821cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 822773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 823cf655043SAndy Whitcroft #print "C: push\n"; 824cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 82513214adfSAndy Whitcroft } 82613214adfSAndy Whitcroft 82713214adfSAndy Whitcroft return ($level, $linenr, @chunks); 8288905a67cSAndy Whitcroft} 8298905a67cSAndy Whitcroft 8304a0df2efSAndy Whitcroftsub ctx_block_get { 831f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 8324a0df2efSAndy Whitcroft my $line; 8334a0df2efSAndy Whitcroft my $start = $linenr - 1; 8344a0df2efSAndy Whitcroft my $blk = ''; 8354a0df2efSAndy Whitcroft my @o; 8364a0df2efSAndy Whitcroft my @c; 8374a0df2efSAndy Whitcroft my @res = (); 8384a0df2efSAndy Whitcroft 839f0a594c1SAndy Whitcroft my $level = 0; 8404635f4fbSAndy Whitcroft my @stack = ($level); 84100df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 84200df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 84300df344fSAndy Whitcroft $remain--; 84400df344fSAndy Whitcroft 84500df344fSAndy Whitcroft $blk .= $rawlines[$line]; 8464635f4fbSAndy Whitcroft 8474635f4fbSAndy Whitcroft # Handle nested #if/#else. 84801464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 8494635f4fbSAndy Whitcroft push(@stack, $level); 85001464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 8514635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 85201464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 8534635f4fbSAndy Whitcroft $level = pop(@stack); 8544635f4fbSAndy Whitcroft } 8554635f4fbSAndy Whitcroft 85601464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 857f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 858f0a594c1SAndy Whitcroft if ($off > 0) { 859f0a594c1SAndy Whitcroft $off--; 860f0a594c1SAndy Whitcroft next; 861f0a594c1SAndy Whitcroft } 8624a0df2efSAndy Whitcroft 863f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 864f0a594c1SAndy Whitcroft $level--; 865f0a594c1SAndy Whitcroft last if ($level == 0); 866f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 867f0a594c1SAndy Whitcroft $level++; 868f0a594c1SAndy Whitcroft } 869f0a594c1SAndy Whitcroft } 8704a0df2efSAndy Whitcroft 871f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 87200df344fSAndy Whitcroft push(@res, $rawlines[$line]); 8734a0df2efSAndy Whitcroft } 8744a0df2efSAndy Whitcroft 875f0a594c1SAndy Whitcroft last if ($level == 0); 8764a0df2efSAndy Whitcroft } 8774a0df2efSAndy Whitcroft 878f0a594c1SAndy Whitcroft return ($level, @res); 8794a0df2efSAndy Whitcroft} 8804a0df2efSAndy Whitcroftsub ctx_block_outer { 8814a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 8824a0df2efSAndy Whitcroft 883f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 884f0a594c1SAndy Whitcroft return @r; 8854a0df2efSAndy Whitcroft} 8864a0df2efSAndy Whitcroftsub ctx_block { 8874a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 8884a0df2efSAndy Whitcroft 889f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 890f0a594c1SAndy Whitcroft return @r; 891653d4876SAndy Whitcroft} 892653d4876SAndy Whitcroftsub ctx_statement { 893f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 894f0a594c1SAndy Whitcroft 895f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 896f0a594c1SAndy Whitcroft return @r; 897f0a594c1SAndy Whitcroft} 898f0a594c1SAndy Whitcroftsub ctx_block_level { 899653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 900653d4876SAndy Whitcroft 901f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 9024a0df2efSAndy Whitcroft} 9039c0ca6f9SAndy Whitcroftsub ctx_statement_level { 9049c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 9059c0ca6f9SAndy Whitcroft 9069c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 9079c0ca6f9SAndy Whitcroft} 9084a0df2efSAndy Whitcroft 9094a0df2efSAndy Whitcroftsub ctx_locate_comment { 9104a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 9114a0df2efSAndy Whitcroft 9124a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 913beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 9144a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 9154a0df2efSAndy Whitcroft 9164a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 9174a0df2efSAndy Whitcroft # comment. 9184a0df2efSAndy Whitcroft my $in_comment = 0; 9194a0df2efSAndy Whitcroft $current_comment = ''; 9204a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 92100df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 92200df344fSAndy Whitcroft #warn " $line\n"; 9234a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 9244a0df2efSAndy Whitcroft $in_comment = 1; 9254a0df2efSAndy Whitcroft } 9264a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 9274a0df2efSAndy Whitcroft $in_comment = 1; 9284a0df2efSAndy Whitcroft } 9294a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 9304a0df2efSAndy Whitcroft $current_comment = ''; 9314a0df2efSAndy Whitcroft } 9324a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 9334a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 9344a0df2efSAndy Whitcroft $in_comment = 0; 9354a0df2efSAndy Whitcroft } 9364a0df2efSAndy Whitcroft } 9374a0df2efSAndy Whitcroft 9384a0df2efSAndy Whitcroft chomp($current_comment); 9394a0df2efSAndy Whitcroft return($current_comment); 9404a0df2efSAndy Whitcroft} 9414a0df2efSAndy Whitcroftsub ctx_has_comment { 9424a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 9434a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 9444a0df2efSAndy Whitcroft 94500df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 9464a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 9474a0df2efSAndy Whitcroft 9484a0df2efSAndy Whitcroft return ($cmt ne ''); 9494a0df2efSAndy Whitcroft} 9504a0df2efSAndy Whitcroft 9514d001e4dSAndy Whitcroftsub raw_line { 9524d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 9534d001e4dSAndy Whitcroft 9544d001e4dSAndy Whitcroft my $offset = $linenr - 1; 9554d001e4dSAndy Whitcroft $cnt++; 9564d001e4dSAndy Whitcroft 9574d001e4dSAndy Whitcroft my $line; 9584d001e4dSAndy Whitcroft while ($cnt) { 9594d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 9604d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 9614d001e4dSAndy Whitcroft $cnt--; 9624d001e4dSAndy Whitcroft } 9634d001e4dSAndy Whitcroft 9644d001e4dSAndy Whitcroft return $line; 9654d001e4dSAndy Whitcroft} 9664d001e4dSAndy Whitcroft 9670a920b5bSAndy Whitcroftsub cat_vet { 9680a920b5bSAndy Whitcroft my ($vet) = @_; 9699c0ca6f9SAndy Whitcroft my ($res, $coded); 9700a920b5bSAndy Whitcroft 9719c0ca6f9SAndy Whitcroft $res = ''; 9726c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 9736c72ffaaSAndy Whitcroft $res .= $1; 9746c72ffaaSAndy Whitcroft if ($2 ne '') { 9759c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 9766c72ffaaSAndy Whitcroft $res .= $coded; 9776c72ffaaSAndy Whitcroft } 9789c0ca6f9SAndy Whitcroft } 9799c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 9800a920b5bSAndy Whitcroft 9819c0ca6f9SAndy Whitcroft return $res; 9820a920b5bSAndy Whitcroft} 9830a920b5bSAndy Whitcroft 984c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 985cf655043SAndy Whitcroftmy $av_pending; 986c2fdda0dSAndy Whitcroftmy @av_paren_type; 9871f65f947SAndy Whitcroftmy $av_pend_colon; 988c2fdda0dSAndy Whitcroft 989c2fdda0dSAndy Whitcroftsub annotate_reset { 990c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 991cf655043SAndy Whitcroft $av_pending = '_'; 992cf655043SAndy Whitcroft @av_paren_type = ('E'); 9931f65f947SAndy Whitcroft $av_pend_colon = 'O'; 994c2fdda0dSAndy Whitcroft} 995c2fdda0dSAndy Whitcroft 9966c72ffaaSAndy Whitcroftsub annotate_values { 9976c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 9986c72ffaaSAndy Whitcroft 9996c72ffaaSAndy Whitcroft my $res; 10001f65f947SAndy Whitcroft my $var = '_' x length($stream); 10016c72ffaaSAndy Whitcroft my $cur = $stream; 10026c72ffaaSAndy Whitcroft 1003c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 10046c72ffaaSAndy Whitcroft 10056c72ffaaSAndy Whitcroft while (length($cur)) { 1006773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1007cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1008171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 10096c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1010c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1011c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1012cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1013c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 10146c72ffaaSAndy Whitcroft } 10156c72ffaaSAndy Whitcroft 1016c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 10179446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 10189446ef56SAndy Whitcroft push(@av_paren_type, $type); 10199446ef56SAndy Whitcroft $type = 'C'; 10209446ef56SAndy Whitcroft 1021e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1022c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 10236c72ffaaSAndy Whitcroft $type = 'T'; 10246c72ffaaSAndy Whitcroft 1025389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1026389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1027389a2fe5SAndy Whitcroft $type = 'T'; 1028389a2fe5SAndy Whitcroft 1029c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1030171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1031c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1032171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1033171ae1a4SAndy Whitcroft if ($2 ne '') { 1034cf655043SAndy Whitcroft $av_pending = 'N'; 1035171ae1a4SAndy Whitcroft } 1036171ae1a4SAndy Whitcroft $type = 'E'; 1037171ae1a4SAndy Whitcroft 1038c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1039171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1040171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1041171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 10426c72ffaaSAndy Whitcroft 1043c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1044cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1045c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1046cf655043SAndy Whitcroft 1047cf655043SAndy Whitcroft push(@av_paren_type, $type); 1048cf655043SAndy Whitcroft push(@av_paren_type, $type); 1049171ae1a4SAndy Whitcroft $type = 'E'; 1050cf655043SAndy Whitcroft 1051c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1052cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1053cf655043SAndy Whitcroft $av_preprocessor = 1; 1054cf655043SAndy Whitcroft 1055cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1056cf655043SAndy Whitcroft 1057171ae1a4SAndy Whitcroft $type = 'E'; 1058cf655043SAndy Whitcroft 1059c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1060cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1061cf655043SAndy Whitcroft 1062cf655043SAndy Whitcroft $av_preprocessor = 1; 1063cf655043SAndy Whitcroft 1064cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1065cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1066cf655043SAndy Whitcroft pop(@av_paren_type); 1067cf655043SAndy Whitcroft push(@av_paren_type, $type); 1068171ae1a4SAndy Whitcroft $type = 'E'; 10696c72ffaaSAndy Whitcroft 10706c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1071c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 10726c72ffaaSAndy Whitcroft 1073171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1074171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1075171ae1a4SAndy Whitcroft $av_pending = $type; 1076171ae1a4SAndy Whitcroft $type = 'N'; 1077171ae1a4SAndy Whitcroft 10786c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1079c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 10806c72ffaaSAndy Whitcroft if (defined $2) { 1081cf655043SAndy Whitcroft $av_pending = 'V'; 10826c72ffaaSAndy Whitcroft } 10836c72ffaaSAndy Whitcroft $type = 'N'; 10846c72ffaaSAndy Whitcroft 108514b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1086c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 108714b111c1SAndy Whitcroft $av_pending = 'E'; 10886c72ffaaSAndy Whitcroft $type = 'N'; 10896c72ffaaSAndy Whitcroft 10901f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 10911f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 10921f65f947SAndy Whitcroft $av_pend_colon = 'C'; 10931f65f947SAndy Whitcroft $type = 'N'; 10941f65f947SAndy Whitcroft 109514b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1096c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 10976c72ffaaSAndy Whitcroft $type = 'N'; 10986c72ffaaSAndy Whitcroft 10996c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1100c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1101cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1102cf655043SAndy Whitcroft $av_pending = '_'; 11036c72ffaaSAndy Whitcroft $type = 'N'; 11046c72ffaaSAndy Whitcroft 11056c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1106cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1107cf655043SAndy Whitcroft if ($new_type ne '_') { 1108cf655043SAndy Whitcroft $type = $new_type; 1109c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1110c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 11116c72ffaaSAndy Whitcroft } else { 1112c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 11136c72ffaaSAndy Whitcroft } 11146c72ffaaSAndy Whitcroft 1115c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1116c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1117c8cb2ca3SAndy Whitcroft $type = 'V'; 1118cf655043SAndy Whitcroft $av_pending = 'V'; 11196c72ffaaSAndy Whitcroft 11208e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 11218e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 11221f65f947SAndy Whitcroft $av_pend_colon = 'B'; 11238e761b04SAndy Whitcroft } elsif ($type eq 'E') { 11248e761b04SAndy Whitcroft $av_pend_colon = 'L'; 11251f65f947SAndy Whitcroft } 11261f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 11271f65f947SAndy Whitcroft $type = 'V'; 11281f65f947SAndy Whitcroft 11296c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1130c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 11316c72ffaaSAndy Whitcroft $type = 'V'; 11326c72ffaaSAndy Whitcroft 11336c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1134c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 11356c72ffaaSAndy Whitcroft $type = 'N'; 11366c72ffaaSAndy Whitcroft 1137cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1138c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 113913214adfSAndy Whitcroft $type = 'E'; 11401f65f947SAndy Whitcroft $av_pend_colon = 'O'; 114113214adfSAndy Whitcroft 11428e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 11438e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 11448e761b04SAndy Whitcroft $type = 'C'; 11458e761b04SAndy Whitcroft 11461f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 11471f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 11481f65f947SAndy Whitcroft $type = 'N'; 11491f65f947SAndy Whitcroft 11501f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 11511f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 11521f65f947SAndy Whitcroft 11531f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 11541f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 11551f65f947SAndy Whitcroft $type = 'E'; 11561f65f947SAndy Whitcroft } else { 11571f65f947SAndy Whitcroft $type = 'N'; 11581f65f947SAndy Whitcroft } 11591f65f947SAndy Whitcroft $av_pend_colon = 'O'; 11601f65f947SAndy Whitcroft 11618e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 116213214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 11636c72ffaaSAndy Whitcroft $type = 'N'; 11646c72ffaaSAndy Whitcroft 11650d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 116674048ed8SAndy Whitcroft my $variant; 116774048ed8SAndy Whitcroft 116874048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 116974048ed8SAndy Whitcroft if ($type eq 'V') { 117074048ed8SAndy Whitcroft $variant = 'B'; 117174048ed8SAndy Whitcroft } else { 117274048ed8SAndy Whitcroft $variant = 'U'; 117374048ed8SAndy Whitcroft } 117474048ed8SAndy Whitcroft 117574048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 117674048ed8SAndy Whitcroft $type = 'N'; 117774048ed8SAndy Whitcroft 11786c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1179c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 11806c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 11816c72ffaaSAndy Whitcroft $type = 'N'; 11826c72ffaaSAndy Whitcroft } 11836c72ffaaSAndy Whitcroft 11846c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1185c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 11866c72ffaaSAndy Whitcroft } 11876c72ffaaSAndy Whitcroft if (defined $1) { 11886c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 11896c72ffaaSAndy Whitcroft $res .= $type x length($1); 11906c72ffaaSAndy Whitcroft } 11916c72ffaaSAndy Whitcroft } 11926c72ffaaSAndy Whitcroft 11931f65f947SAndy Whitcroft return ($res, $var); 11946c72ffaaSAndy Whitcroft} 11956c72ffaaSAndy Whitcroft 11968905a67cSAndy Whitcroftsub possible { 119713214adfSAndy Whitcroft my ($possible, $line) = @_; 11989a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 11990776e594SAndy Whitcroft ^(?: 12000776e594SAndy Whitcroft $Modifier| 12010776e594SAndy Whitcroft $Storage| 12020776e594SAndy Whitcroft $Type| 12039a974fdbSAndy Whitcroft DEFINE_\S+ 12049a974fdbSAndy Whitcroft )$| 12059a974fdbSAndy Whitcroft ^(?: 12060776e594SAndy Whitcroft goto| 12070776e594SAndy Whitcroft return| 12080776e594SAndy Whitcroft case| 12090776e594SAndy Whitcroft else| 12100776e594SAndy Whitcroft asm|__asm__| 12110776e594SAndy Whitcroft do 12129a974fdbSAndy Whitcroft )(?:\s|$)| 12130776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 12149a974fdbSAndy Whitcroft )}x; 12159a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 12169a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1217c45dcabdSAndy Whitcroft # Check for modifiers. 1218c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1219c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1220c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1221c45dcabdSAndy Whitcroft 1222c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1223c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1224d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 12259a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1226d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1227d2506586SAndy Whitcroft push(@modifierList, $modifier); 1228d2506586SAndy Whitcroft } 12299a974fdbSAndy Whitcroft } 1230c45dcabdSAndy Whitcroft 1231c45dcabdSAndy Whitcroft } else { 123213214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 12338905a67cSAndy Whitcroft push(@typeList, $possible); 1234c45dcabdSAndy Whitcroft } 12358905a67cSAndy Whitcroft build_types(); 12360776e594SAndy Whitcroft } else { 12370776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 12388905a67cSAndy Whitcroft } 12398905a67cSAndy Whitcroft} 12408905a67cSAndy Whitcroft 12416c72ffaaSAndy Whitcroftmy $prefix = ''; 12426c72ffaaSAndy Whitcroft 1243000d1cc1SJoe Perchessub show_type { 1244000d1cc1SJoe Perches return !defined $ignore_type{$_[0]}; 1245000d1cc1SJoe Perches} 1246000d1cc1SJoe Perches 1247f0a594c1SAndy Whitcroftsub report { 1248000d1cc1SJoe Perches if (!show_type($_[1]) || 1249000d1cc1SJoe Perches (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { 1250773647a0SAndy Whitcroft return 0; 1251773647a0SAndy Whitcroft } 1252000d1cc1SJoe Perches my $line; 1253000d1cc1SJoe Perches if ($show_types) { 1254000d1cc1SJoe Perches $line = "$prefix$_[0]:$_[1]: $_[2]\n"; 1255000d1cc1SJoe Perches } else { 1256000d1cc1SJoe Perches $line = "$prefix$_[0]: $_[2]\n"; 1257000d1cc1SJoe Perches } 12588905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 12598905a67cSAndy Whitcroft 126013214adfSAndy Whitcroft push(our @report, $line); 1261773647a0SAndy Whitcroft 1262773647a0SAndy Whitcroft return 1; 1263f0a594c1SAndy Whitcroft} 1264f0a594c1SAndy Whitcroftsub report_dump { 126513214adfSAndy Whitcroft our @report; 1266f0a594c1SAndy Whitcroft} 1267000d1cc1SJoe Perches 1268de7d4f0eSAndy Whitcroftsub ERROR { 1269000d1cc1SJoe Perches if (report("ERROR", $_[0], $_[1])) { 1270de7d4f0eSAndy Whitcroft our $clean = 0; 12716c72ffaaSAndy Whitcroft our $cnt_error++; 1272de7d4f0eSAndy Whitcroft } 1273773647a0SAndy Whitcroft} 1274de7d4f0eSAndy Whitcroftsub WARN { 1275000d1cc1SJoe Perches if (report("WARNING", $_[0], $_[1])) { 1276de7d4f0eSAndy Whitcroft our $clean = 0; 12776c72ffaaSAndy Whitcroft our $cnt_warn++; 1278de7d4f0eSAndy Whitcroft } 1279773647a0SAndy Whitcroft} 1280de7d4f0eSAndy Whitcroftsub CHK { 1281000d1cc1SJoe Perches if ($check && report("CHECK", $_[0], $_[1])) { 1282de7d4f0eSAndy Whitcroft our $clean = 0; 12836c72ffaaSAndy Whitcroft our $cnt_chk++; 12846c72ffaaSAndy Whitcroft } 1285de7d4f0eSAndy Whitcroft} 1286de7d4f0eSAndy Whitcroft 12876ecd9674SAndy Whitcroftsub check_absolute_file { 12886ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 12896ecd9674SAndy Whitcroft my $file = $absolute; 12906ecd9674SAndy Whitcroft 12916ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 12926ecd9674SAndy Whitcroft 12936ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 12946ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 12956ecd9674SAndy Whitcroft if (-f "$root/$file") { 12966ecd9674SAndy Whitcroft ##print "file<$file>\n"; 12976ecd9674SAndy Whitcroft last; 12986ecd9674SAndy Whitcroft } 12996ecd9674SAndy Whitcroft } 13006ecd9674SAndy Whitcroft if (! -f _) { 13016ecd9674SAndy Whitcroft return 0; 13026ecd9674SAndy Whitcroft } 13036ecd9674SAndy Whitcroft 13046ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 13056ecd9674SAndy Whitcroft my $prefix = $absolute; 13066ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 13076ecd9674SAndy Whitcroft 13086ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 13096ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1310000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1311000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 13126ecd9674SAndy Whitcroft } 13136ecd9674SAndy Whitcroft} 13146ecd9674SAndy Whitcroft 13150a920b5bSAndy Whitcroftsub process { 13160a920b5bSAndy Whitcroft my $filename = shift; 13170a920b5bSAndy Whitcroft 13180a920b5bSAndy Whitcroft my $linenr=0; 13190a920b5bSAndy Whitcroft my $prevline=""; 1320c2fdda0dSAndy Whitcroft my $prevrawline=""; 13210a920b5bSAndy Whitcroft my $stashline=""; 1322c2fdda0dSAndy Whitcroft my $stashrawline=""; 13230a920b5bSAndy Whitcroft 13244a0df2efSAndy Whitcroft my $length; 13250a920b5bSAndy Whitcroft my $indent; 13260a920b5bSAndy Whitcroft my $previndent=0; 13270a920b5bSAndy Whitcroft my $stashindent=0; 13280a920b5bSAndy Whitcroft 1329de7d4f0eSAndy Whitcroft our $clean = 1; 13300a920b5bSAndy Whitcroft my $signoff = 0; 13310a920b5bSAndy Whitcroft my $is_patch = 0; 13320a920b5bSAndy Whitcroft 133313214adfSAndy Whitcroft our @report = (); 13346c72ffaaSAndy Whitcroft our $cnt_lines = 0; 13356c72ffaaSAndy Whitcroft our $cnt_error = 0; 13366c72ffaaSAndy Whitcroft our $cnt_warn = 0; 13376c72ffaaSAndy Whitcroft our $cnt_chk = 0; 13386c72ffaaSAndy Whitcroft 13390a920b5bSAndy Whitcroft # Trace the real file/line as we go. 13400a920b5bSAndy Whitcroft my $realfile = ''; 13410a920b5bSAndy Whitcroft my $realline = 0; 13420a920b5bSAndy Whitcroft my $realcnt = 0; 13430a920b5bSAndy Whitcroft my $here = ''; 13440a920b5bSAndy Whitcroft my $in_comment = 0; 1345c2fdda0dSAndy Whitcroft my $comment_edge = 0; 13460a920b5bSAndy Whitcroft my $first_line = 0; 13471e855726SWolfram Sang my $p1_prefix = ''; 13480a920b5bSAndy Whitcroft 134913214adfSAndy Whitcroft my $prev_values = 'E'; 135013214adfSAndy Whitcroft 135113214adfSAndy Whitcroft # suppression flags 1352773647a0SAndy Whitcroft my %suppress_ifbraces; 1353170d3a22SAndy Whitcroft my %suppress_whiletrailers; 13542b474a1aSAndy Whitcroft my %suppress_export; 1355653d4876SAndy Whitcroft 1356c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1357de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1358c2fdda0dSAndy Whitcroft # 1359de7d4f0eSAndy Whitcroft my @setup_docs = (); 1360de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1361773647a0SAndy Whitcroft 1362773647a0SAndy Whitcroft sanitise_line_reset(); 1363c2fdda0dSAndy Whitcroft my $line; 1364c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1365773647a0SAndy Whitcroft $linenr++; 1366773647a0SAndy Whitcroft $line = $rawline; 1367c2fdda0dSAndy Whitcroft 1368773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1369de7d4f0eSAndy Whitcroft $setup_docs = 0; 1370de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1371de7d4f0eSAndy Whitcroft $setup_docs = 1; 1372de7d4f0eSAndy Whitcroft } 1373773647a0SAndy Whitcroft #next; 1374de7d4f0eSAndy Whitcroft } 1375773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1376773647a0SAndy Whitcroft $realline=$1-1; 1377773647a0SAndy Whitcroft if (defined $2) { 1378773647a0SAndy Whitcroft $realcnt=$3+1; 1379773647a0SAndy Whitcroft } else { 1380773647a0SAndy Whitcroft $realcnt=1+1; 1381773647a0SAndy Whitcroft } 1382c45dcabdSAndy Whitcroft $in_comment = 0; 1383773647a0SAndy Whitcroft 1384773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1385773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1386773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1387773647a0SAndy Whitcroft # at context start. 1388773647a0SAndy Whitcroft my $edge; 138901fa9147SAndy Whitcroft my $cnt = $realcnt; 139001fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 139101fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 139201fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 139301fa9147SAndy Whitcroft $cnt--; 139401fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1395721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1396fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1397fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1398fae17daeSAndy Whitcroft ($edge) = $1; 1399fae17daeSAndy Whitcroft last; 1400fae17daeSAndy Whitcroft } 1401773647a0SAndy Whitcroft } 1402773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1403773647a0SAndy Whitcroft $in_comment = 1; 1404773647a0SAndy Whitcroft } 1405773647a0SAndy Whitcroft 1406773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1407773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1408773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1409773647a0SAndy Whitcroft if (!defined $edge && 141083242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1411773647a0SAndy Whitcroft { 1412773647a0SAndy Whitcroft $in_comment = 1; 1413773647a0SAndy Whitcroft } 1414773647a0SAndy Whitcroft 1415773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1416773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1417773647a0SAndy Whitcroft 1418171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1419773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1420171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1421773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1422773647a0SAndy Whitcroft } 1423773647a0SAndy Whitcroft push(@lines, $line); 1424773647a0SAndy Whitcroft 1425773647a0SAndy Whitcroft if ($realcnt > 1) { 1426773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1427773647a0SAndy Whitcroft } else { 1428773647a0SAndy Whitcroft $realcnt = 0; 1429773647a0SAndy Whitcroft } 1430773647a0SAndy Whitcroft 1431773647a0SAndy Whitcroft #print "==>$rawline\n"; 1432773647a0SAndy Whitcroft #print "-->$line\n"; 1433de7d4f0eSAndy Whitcroft 1434de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1435de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1436de7d4f0eSAndy Whitcroft } 1437de7d4f0eSAndy Whitcroft } 1438de7d4f0eSAndy Whitcroft 14396c72ffaaSAndy Whitcroft $prefix = ''; 14406c72ffaaSAndy Whitcroft 1441773647a0SAndy Whitcroft $realcnt = 0; 1442773647a0SAndy Whitcroft $linenr = 0; 14430a920b5bSAndy Whitcroft foreach my $line (@lines) { 14440a920b5bSAndy Whitcroft $linenr++; 14450a920b5bSAndy Whitcroft 1446c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 14476c72ffaaSAndy Whitcroft 14480a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 14496c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 14500a920b5bSAndy Whitcroft $is_patch = 1; 14514a0df2efSAndy Whitcroft $first_line = $linenr + 1; 14520a920b5bSAndy Whitcroft $realline=$1-1; 14530a920b5bSAndy Whitcroft if (defined $2) { 14540a920b5bSAndy Whitcroft $realcnt=$3+1; 14550a920b5bSAndy Whitcroft } else { 14560a920b5bSAndy Whitcroft $realcnt=1+1; 14570a920b5bSAndy Whitcroft } 1458c2fdda0dSAndy Whitcroft annotate_reset(); 145913214adfSAndy Whitcroft $prev_values = 'E'; 146013214adfSAndy Whitcroft 1461773647a0SAndy Whitcroft %suppress_ifbraces = (); 1462170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 14632b474a1aSAndy Whitcroft %suppress_export = (); 14640a920b5bSAndy Whitcroft next; 14650a920b5bSAndy Whitcroft 14664a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 14674a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 14684a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1469773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 14700a920b5bSAndy Whitcroft $realline++; 1471d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 14720a920b5bSAndy Whitcroft 14734a0df2efSAndy Whitcroft # Measure the line length and indent. 1474c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 14750a920b5bSAndy Whitcroft 14760a920b5bSAndy Whitcroft # Track the previous line. 14770a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 14780a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1479c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1480c2fdda0dSAndy Whitcroft 1481773647a0SAndy Whitcroft #warn "line<$line>\n"; 14826c72ffaaSAndy Whitcroft 1483d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1484d8aaf121SAndy Whitcroft $realcnt--; 14850a920b5bSAndy Whitcroft } 14860a920b5bSAndy Whitcroft 1487cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1488cc77cdcaSAndy Whitcroft 14890a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1490773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1491773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1492773647a0SAndy Whitcroft 14936c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 14946c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1495773647a0SAndy Whitcroft 1496773647a0SAndy Whitcroft # extract the filename as it passes 14973bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 14983bf9a009SRabin Vincent $realfile = $1; 14993bf9a009SRabin Vincent $realfile =~ s@^([^/]*)/@@; 15003bf9a009SRabin Vincent 15013bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1502773647a0SAndy Whitcroft $realfile = $1; 15031e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 15041e855726SWolfram Sang 15051e855726SWolfram Sang $p1_prefix = $1; 1506e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1507e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 1508000d1cc1SJoe Perches WARN("PATCH_PREFIX", 1509000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 15101e855726SWolfram Sang } 1511773647a0SAndy Whitcroft 1512c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1513000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 1514000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1515773647a0SAndy Whitcroft } 1516773647a0SAndy Whitcroft next; 1517773647a0SAndy Whitcroft } 1518773647a0SAndy Whitcroft 1519389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 15200a920b5bSAndy Whitcroft 1521c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1522c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1523c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 15240a920b5bSAndy Whitcroft 15256c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 15266c72ffaaSAndy Whitcroft 15273bf9a009SRabin Vincent# Check for incorrect file permissions 15283bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 15293bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 15303bf9a009SRabin Vincent if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) { 1531000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 1532000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 15333bf9a009SRabin Vincent } 15343bf9a009SRabin Vincent } 15353bf9a009SRabin Vincent 153620112475SJoe Perches# Check the patch for a signoff: 1537d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 15384a0df2efSAndy Whitcroft $signoff++; 15390a920b5bSAndy Whitcroft } 154020112475SJoe Perches 154120112475SJoe Perches# Check signature styles 154220112475SJoe Perches if ($line =~ /^(\s*)($signature_tags)(\s*)(.*)/) { 154320112475SJoe Perches my $space_before = $1; 154420112475SJoe Perches my $sign_off = $2; 154520112475SJoe Perches my $space_after = $3; 154620112475SJoe Perches my $email = $4; 154720112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 154820112475SJoe Perches 154920112475SJoe Perches if (defined $space_before && $space_before ne "") { 1550000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1551000d1cc1SJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); 155220112475SJoe Perches } 155320112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 1554000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1555000d1cc1SJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); 155620112475SJoe Perches } 155720112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 1558000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1559000d1cc1SJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr); 156020112475SJoe Perches } 156120112475SJoe Perches 156220112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 156320112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 156420112475SJoe Perches if ($suggested_email eq "") { 1565000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 1566000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 156720112475SJoe Perches } else { 156820112475SJoe Perches my $dequoted = $suggested_email; 156920112475SJoe Perches $dequoted =~ s/^"//; 157020112475SJoe Perches $dequoted =~ s/" </ </; 157120112475SJoe Perches # Don't force email to have quotes 157220112475SJoe Perches # Allow just an angle bracketed address 157320112475SJoe Perches if ("$dequoted$comment" ne $email && 157420112475SJoe Perches "<$email_address>$comment" ne $email && 157520112475SJoe Perches "$suggested_email$comment" ne $email) { 1576000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1577000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 157820112475SJoe Perches } 15790a920b5bSAndy Whitcroft } 15800a920b5bSAndy Whitcroft } 15810a920b5bSAndy Whitcroft 158200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 15838905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1584000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 1585000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 15866c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1587de7d4f0eSAndy Whitcroft } 1588de7d4f0eSAndy Whitcroft 15896ecd9674SAndy Whitcroft# Check for absolute kernel paths. 15906ecd9674SAndy Whitcroft if ($tree) { 15916ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 15926ecd9674SAndy Whitcroft my $file = $1; 15936ecd9674SAndy Whitcroft 15946ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 15956ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 15966ecd9674SAndy Whitcroft # 15976ecd9674SAndy Whitcroft } else { 15986ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 15996ecd9674SAndy Whitcroft } 16006ecd9674SAndy Whitcroft } 16016ecd9674SAndy Whitcroft } 16026ecd9674SAndy Whitcroft 1603de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1604de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1605171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1606171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1607171ae1a4SAndy Whitcroft 1608171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1609171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1610171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1611171ae1a4SAndy Whitcroft 161234d99219SJoe Perches CHK("INVALID_UTF8", 1613000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 161400df344fSAndy Whitcroft } 16150a920b5bSAndy Whitcroft 161630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 161730670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 161800df344fSAndy Whitcroft 16190a920b5bSAndy Whitcroft#trailing whitespace 16209c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1621c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1622000d1cc1SJoe Perches ERROR("DOS_LINE_ENDINGS", 1623000d1cc1SJoe Perches "DOS line endings\n" . $herevet); 16249c0ca6f9SAndy Whitcroft 1625c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1626c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1627000d1cc1SJoe Perches ERROR("TRAILING_WHITESPACE", 1628000d1cc1SJoe Perches "trailing whitespace\n" . $herevet); 1629d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 16300a920b5bSAndy Whitcroft } 16315368df20SAndy Whitcroft 16323354957aSAndi Kleen# check for Kconfig help text having a real description 16339fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 16349fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 16353354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 16369fe287d7SAndy Whitcroft $line =~ /\+\s*(?:---)?help(?:---)?$/) { 16373354957aSAndi Kleen my $length = 0; 16389fe287d7SAndy Whitcroft my $cnt = $realcnt; 16399fe287d7SAndy Whitcroft my $ln = $linenr + 1; 16409fe287d7SAndy Whitcroft my $f; 16419fe287d7SAndy Whitcroft my $is_end = 0; 16429fe287d7SAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1]) { 16439fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 16449fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 16459fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 16469fe287d7SAndy Whitcroft $ln++; 16479fe287d7SAndy Whitcroft 16489fe287d7SAndy Whitcroft next if ($f =~ /^-/); 16499fe287d7SAndy Whitcroft $f =~ s/^.//; 16503354957aSAndi Kleen $f =~ s/#.*//; 16513354957aSAndi Kleen $f =~ s/^\s+//; 16523354957aSAndi Kleen next if ($f =~ /^$/); 16539fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 16549fe287d7SAndy Whitcroft $is_end = 1; 16559fe287d7SAndy Whitcroft last; 16569fe287d7SAndy Whitcroft } 16573354957aSAndi Kleen $length++; 16583354957aSAndi Kleen } 1659000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 1660000d1cc1SJoe Perches "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4); 16619fe287d7SAndy Whitcroft #print "is_end<$is_end> length<$length>\n"; 16623354957aSAndi Kleen } 16633354957aSAndi Kleen 16645368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 16655368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 16665368df20SAndy Whitcroft 16670a920b5bSAndy Whitcroft#80 column limit 1668c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1669f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 16700fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 16718bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1672f4c014c0SAndy Whitcroft $length > 80) 1673c45dcabdSAndy Whitcroft { 1674000d1cc1SJoe Perches WARN("LONG_LINE", 1675000d1cc1SJoe Perches "line over 80 characters\n" . $herecurr); 16760a920b5bSAndy Whitcroft } 16770a920b5bSAndy Whitcroft 16785e79d96eSJoe Perches# check for spaces before a quoted newline 16795e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 1680000d1cc1SJoe Perches WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 1681000d1cc1SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr); 16825e79d96eSJoe Perches } 16835e79d96eSJoe Perches 16848905a67cSAndy Whitcroft# check for adding lines without a newline. 16858905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 1686000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 1687000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 16888905a67cSAndy Whitcroft } 16898905a67cSAndy Whitcroft 169042e41c54SMike Frysinger# Blackfin: use hi/lo macros 169142e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 169242e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 169342e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1694000d1cc1SJoe Perches ERROR("LO_MACRO", 1695000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 169642e41c54SMike Frysinger } 169742e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 169842e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1699000d1cc1SJoe Perches ERROR("HI_MACRO", 1700000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 170142e41c54SMike Frysinger } 170242e41c54SMike Frysinger } 170342e41c54SMike Frysinger 1704b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 1705b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 17060a920b5bSAndy Whitcroft 17070a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 17080a920b5bSAndy Whitcroft# more than 8 must use tabs. 1709c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1710c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1711c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1712000d1cc1SJoe Perches ERROR("CODE_INDENT", 1713000d1cc1SJoe Perches "code indent should use tabs where possible\n" . $herevet); 1714d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 17150a920b5bSAndy Whitcroft } 17160a920b5bSAndy Whitcroft 171708e44365SAlberto Panizzo# check for space before tabs. 171808e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 171908e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1720000d1cc1SJoe Perches WARN("SPACE_BEFORE_TAB", 1721000d1cc1SJoe Perches "please, no space before tabs\n" . $herevet); 172208e44365SAlberto Panizzo } 172308e44365SAlberto Panizzo 17245f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 17256b4c5bebSAndy Whitcroft# Exceptions: 17266b4c5bebSAndy Whitcroft# 1) within comments 17276b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 17286b4c5bebSAndy Whitcroft# 3) hanging labels 17296b4c5bebSAndy Whitcroft if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { 17305f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1731000d1cc1SJoe Perches WARN("LEADING_SPACE", 1732000d1cc1SJoe Perches "please, no spaces at the start of a line\n" . $herevet); 17335f7ddae6SRaffaele Recalcati } 17345f7ddae6SRaffaele Recalcati 1735b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 1736b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 1737b9ea10d6SAndy Whitcroft 1738c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1739cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1740000d1cc1SJoe Perches WARN("CVS_KEYWORD", 1741000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1742c2fdda0dSAndy Whitcroft } 174322f2a2efSAndy Whitcroft 174442e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 174542e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 174642e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1747000d1cc1SJoe Perches ERROR("CSYNC", 1748000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 174942e41c54SMike Frysinger } 175042e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 175142e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1752000d1cc1SJoe Perches ERROR("SSYNC", 1753000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 175442e41c54SMike Frysinger } 175542e41c54SMike Frysinger 17569c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 17572b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 17582b474a1aSAndy Whitcroft $realline_next); 17599c9ba34eSAndy Whitcroft if ($realcnt && $line =~ /.\s*\S/) { 1760170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1761f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 1762171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1763171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1764171ae1a4SAndy Whitcroft 17652b474a1aSAndy Whitcroft # Find the real next line. 17662b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 17672b474a1aSAndy Whitcroft if (defined $realline_next && 17682b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 17692b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 17702b474a1aSAndy Whitcroft $realline_next++; 17712b474a1aSAndy Whitcroft } 17722b474a1aSAndy Whitcroft 1773171ae1a4SAndy Whitcroft my $s = $stat; 1774171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1775cf655043SAndy Whitcroft 1776c2fdda0dSAndy Whitcroft # Ignore goto labels. 1777171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1778c2fdda0dSAndy Whitcroft 1779c2fdda0dSAndy Whitcroft # Ignore functions being called 1780171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1781c2fdda0dSAndy Whitcroft 1782463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 1783463f2864SAndy Whitcroft 1784c45dcabdSAndy Whitcroft # declarations always start with types 1785d2506586SAndy 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) { 1786c45dcabdSAndy Whitcroft my $type = $1; 1787c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1788c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1789c45dcabdSAndy Whitcroft 17906c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1791a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1792c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1793c2fdda0dSAndy Whitcroft } 17948905a67cSAndy Whitcroft 17956c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 179665863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1797c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 17989c0ca6f9SAndy Whitcroft } 17998905a67cSAndy Whitcroft 18008905a67cSAndy Whitcroft # Check for any sort of function declaration. 18018905a67cSAndy Whitcroft # int foo(something bar, other baz); 18028905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1803171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 18048905a67cSAndy Whitcroft my ($name_len) = length($1); 18058905a67cSAndy Whitcroft 1806cf655043SAndy Whitcroft my $ctx = $s; 1807773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 18088905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1809cf655043SAndy Whitcroft 18108905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1811c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 18128905a67cSAndy Whitcroft 1813c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 18148905a67cSAndy Whitcroft } 18158905a67cSAndy Whitcroft } 18168905a67cSAndy Whitcroft } 18178905a67cSAndy Whitcroft 18189c0ca6f9SAndy Whitcroft } 18199c0ca6f9SAndy Whitcroft 182000df344fSAndy Whitcroft# 182100df344fSAndy Whitcroft# Checks which may be anchored in the context. 182200df344fSAndy Whitcroft# 182300df344fSAndy Whitcroft 182400df344fSAndy Whitcroft# Check for switch () and associated case and default 182500df344fSAndy Whitcroft# statements should be at the same indent. 182600df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 182700df344fSAndy Whitcroft my $err = ''; 182800df344fSAndy Whitcroft my $sep = ''; 182900df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 183000df344fSAndy Whitcroft shift(@ctx); 183100df344fSAndy Whitcroft for my $ctx (@ctx) { 183200df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 183300df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 183400df344fSAndy Whitcroft $indent != $cindent) { 183500df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 183600df344fSAndy Whitcroft $sep = ''; 183700df344fSAndy Whitcroft } else { 183800df344fSAndy Whitcroft $sep = "[...]\n"; 183900df344fSAndy Whitcroft } 184000df344fSAndy Whitcroft } 184100df344fSAndy Whitcroft if ($err ne '') { 1842000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 1843000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 1844de7d4f0eSAndy Whitcroft } 1845de7d4f0eSAndy Whitcroft } 1846de7d4f0eSAndy Whitcroft 1847de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 1848de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 1849c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 1850773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 1851773647a0SAndy Whitcroft 18529c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1853de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 1854de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 1855de7d4f0eSAndy Whitcroft 1856548596d5SAndy Whitcroft my $ctx_ln = $linenr; 1857548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 1858de7d4f0eSAndy Whitcroft 1859548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 1860548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 1861548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 1862548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 1863548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 1864773647a0SAndy Whitcroft $ctx_ln++; 1865773647a0SAndy Whitcroft } 1866548596d5SAndy Whitcroft 186753210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 186853210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 1869773647a0SAndy Whitcroft 1870773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1871000d1cc1SJoe Perches ERROR("OPEN_BRACE", 1872000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 187301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 187400df344fSAndy Whitcroft } 1875773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1876773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 1877773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 1878773647a0SAndy Whitcroft { 18799c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 18809c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 1881000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 1882000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 188301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 18849c0ca6f9SAndy Whitcroft } 18859c0ca6f9SAndy Whitcroft } 188600df344fSAndy Whitcroft } 188700df344fSAndy Whitcroft 18884d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 18894d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 18904d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 18914d001e4dSAndy Whitcroft 18924d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 18934d001e4dSAndy Whitcroft 18944d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 18954d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 18964d001e4dSAndy Whitcroft # where necessary. 18974d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 18984d001e4dSAndy Whitcroft 18994d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 19006f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 19016f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 19024d001e4dSAndy Whitcroft 19034d001e4dSAndy Whitcroft # We want to check the first line inside the block 19044d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 19054d001e4dSAndy Whitcroft # 1) any blank line termination 19064d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 19074d001e4dSAndy Whitcroft # 3) any do (...) { 19084d001e4dSAndy Whitcroft my $continuation = 0; 19094d001e4dSAndy Whitcroft my $check = 0; 19104d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 19114d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 19124d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 19134d001e4dSAndy Whitcroft $continuation = 1; 19144d001e4dSAndy Whitcroft } 19159bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 19164d001e4dSAndy Whitcroft $check = 1; 19174d001e4dSAndy Whitcroft $cond_lines++; 19184d001e4dSAndy Whitcroft } 19194d001e4dSAndy Whitcroft 19204d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 19214d001e4dSAndy Whitcroft # preprocessor statement. 19224d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 19234d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 19244d001e4dSAndy Whitcroft $check = 0; 19254d001e4dSAndy Whitcroft } 19264d001e4dSAndy Whitcroft 19279bd49efeSAndy Whitcroft my $cond_ptr = -1; 1928740504c6SAndy Whitcroft $continuation = 0; 19299bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 19309bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 19314d001e4dSAndy Whitcroft 1932f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 1933f16fa28fSAndy Whitcroft # is not linear. 1934f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 1935f16fa28fSAndy Whitcroft $check = 0; 1936f16fa28fSAndy Whitcroft } 1937f16fa28fSAndy Whitcroft 19389bd49efeSAndy Whitcroft # Ignore: 19399bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 19409bd49efeSAndy Whitcroft # 2) preprocessor lines, and 19419bd49efeSAndy Whitcroft # 3) labels. 1942740504c6SAndy Whitcroft if ($continuation || 1943740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 19449bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 19459bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 1946740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 194730dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 19489bd49efeSAndy Whitcroft $cond_lines++; 19499bd49efeSAndy Whitcroft } 19504d001e4dSAndy Whitcroft } 195130dad6ebSAndy Whitcroft } 19524d001e4dSAndy Whitcroft 19534d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 19544d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 19554d001e4dSAndy Whitcroft 19564d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 19574d001e4dSAndy Whitcroft # this is not this patch's fault. 19584d001e4dSAndy Whitcroft if (!defined($stat_real) || 19594d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 19604d001e4dSAndy Whitcroft $check = 0; 19614d001e4dSAndy Whitcroft } 19624d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 19634d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 19644d001e4dSAndy Whitcroft } 19654d001e4dSAndy Whitcroft 19669bd49efeSAndy 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"; 19674d001e4dSAndy Whitcroft 19684d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 19694d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 1970000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 1971000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 19724d001e4dSAndy Whitcroft } 19734d001e4dSAndy Whitcroft } 19744d001e4dSAndy Whitcroft 19756c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 19766c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 19771f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 19781f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 19796c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 1980c2fdda0dSAndy Whitcroft if ($dbg_values) { 1981c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 1982cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 1983cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 19841f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 1985c2fdda0dSAndy Whitcroft } 19866c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 19876c72ffaaSAndy Whitcroft 198800df344fSAndy Whitcroft#ignore lines not being added 198900df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 199000df344fSAndy Whitcroft 1991653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 19927429c690SAndy Whitcroft if ($dbg_type) { 19937429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 1994000d1cc1SJoe Perches ERROR("TEST_TYPE", 1995000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 19967429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 1997000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 1998000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 19997429c690SAndy Whitcroft } 2000653d4876SAndy Whitcroft next; 2001653d4876SAndy Whitcroft } 2002a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2003a1ef277eSAndy Whitcroft if ($dbg_attr) { 20049360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2005000d1cc1SJoe Perches ERROR("TEST_ATTR", 2006000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 20079360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2008000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2009000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2010a1ef277eSAndy Whitcroft } 2011a1ef277eSAndy Whitcroft next; 2012a1ef277eSAndy Whitcroft } 2013653d4876SAndy Whitcroft 2014f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 201599423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 201699423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2017000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2018000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . $hereprev); 2019f0a594c1SAndy Whitcroft } 2020f0a594c1SAndy Whitcroft 202100df344fSAndy Whitcroft# 202200df344fSAndy Whitcroft# Checks which are anchored on the added line. 202300df344fSAndy Whitcroft# 202400df344fSAndy Whitcroft 2025653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2026c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2027653d4876SAndy Whitcroft my $path = $1; 2028653d4876SAndy Whitcroft if ($path =~ m{//}) { 2029000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2030000d1cc1SJoe Perches "malformed #include filename\n" . 2031de7d4f0eSAndy Whitcroft $herecurr); 2032653d4876SAndy Whitcroft } 2033653d4876SAndy Whitcroft } 2034653d4876SAndy Whitcroft 203500df344fSAndy Whitcroft# no C99 // comments 203600df344fSAndy Whitcroft if ($line =~ m{//}) { 2037000d1cc1SJoe Perches ERROR("C99_COMMENTS", 2038000d1cc1SJoe Perches "do not use C99 // comments\n" . $herecurr); 203900df344fSAndy Whitcroft } 204000df344fSAndy Whitcroft # Remove C99 comments. 20410a920b5bSAndy Whitcroft $line =~ s@//.*@@; 20426c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 20430a920b5bSAndy Whitcroft 20442b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 20452b474a1aSAndy Whitcroft# the whole statement. 20462b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 20472b474a1aSAndy Whitcroft if (defined $realline_next && 20482b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 20492b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 20502b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 20512b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 20523cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 20533cbf62dfSAndy Whitcroft # a prefix: 20543cbf62dfSAndy Whitcroft # XXX(foo); 20553cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 2056653d4876SAndy Whitcroft my $name = $1; 20573cbf62dfSAndy Whitcroft if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ && 20583cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 20593cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 20603cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 20613cbf62dfSAndy Whitcroft 20623cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 20632b474a1aSAndy Whitcroft \n.}\s*$| 206448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 206548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 206648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 20672b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 20682b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 206948012058SAndy Whitcroft )/x) { 20702b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 20712b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 20722b474a1aSAndy Whitcroft } else { 20732b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 20740a920b5bSAndy Whitcroft } 20750a920b5bSAndy Whitcroft } 20762b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 20772b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 20782b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 20792b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 20802b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 20812b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 20822b474a1aSAndy Whitcroft } 20832b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 20842b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 2085000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 2086000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 20872b474a1aSAndy Whitcroft } 20880a920b5bSAndy Whitcroft 20895150bda4SJoe Eloff# check for global initialisers. 2090c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 2091000d1cc1SJoe Perches ERROR("GLOBAL_INITIALISERS", 2092000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 2093f0a594c1SAndy Whitcroft $herecurr); 2094f0a594c1SAndy Whitcroft } 20950a920b5bSAndy Whitcroft# check for static initialisers. 20962d1bafd7SAndy Whitcroft if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2097000d1cc1SJoe Perches ERROR("INITIALISED_STATIC", 2098000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 2099de7d4f0eSAndy Whitcroft $herecurr); 21000a920b5bSAndy Whitcroft } 21010a920b5bSAndy Whitcroft 2102cb710ecaSJoe Perches# check for static const char * arrays. 2103cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 2104000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2105000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 2106cb710ecaSJoe Perches $herecurr); 2107cb710ecaSJoe Perches } 2108cb710ecaSJoe Perches 2109cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 2110cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 2111000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2112000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 2113cb710ecaSJoe Perches $herecurr); 2114cb710ecaSJoe Perches } 2115cb710ecaSJoe Perches 211693ed0e2dSJoe Perches# check for declarations of struct pci_device_id 211793ed0e2dSJoe Perches if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { 2118000d1cc1SJoe Perches WARN("DEFINE_PCI_DEVICE_TABLE", 2119000d1cc1SJoe Perches "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); 212093ed0e2dSJoe Perches } 212193ed0e2dSJoe Perches 2122653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 2123653d4876SAndy Whitcroft# make sense. 2124653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 21258054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 2126c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 21278ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 2128653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 2129000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 2130000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 21310a920b5bSAndy Whitcroft } 21320a920b5bSAndy Whitcroft 21330a920b5bSAndy Whitcroft# * goes on variable not on type 213465863862SAndy Whitcroft # (char*[ const]) 213500ef4eceSAndy Whitcroft if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 213665863862SAndy Whitcroft my ($from, $to) = ($1, $1); 2137d8aaf121SAndy Whitcroft 213865863862SAndy Whitcroft # Should start with a space. 213965863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 214065863862SAndy Whitcroft # Should not end with a space. 214165863862SAndy Whitcroft $to =~ s/\s+$//; 214265863862SAndy Whitcroft # '*'s should not have spaces between. 2143f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 214465863862SAndy Whitcroft } 2145d8aaf121SAndy Whitcroft 214665863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 214765863862SAndy Whitcroft if ($from ne $to) { 2148000d1cc1SJoe Perches ERROR("POINTER_LOCATION", 2149000d1cc1SJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 215065863862SAndy Whitcroft } 215100ef4eceSAndy Whitcroft } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 215265863862SAndy Whitcroft my ($from, $to, $ident) = ($1, $1, $2); 2153d8aaf121SAndy Whitcroft 215465863862SAndy Whitcroft # Should start with a space. 215565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 215665863862SAndy Whitcroft # Should not end with a space. 215765863862SAndy Whitcroft $to =~ s/\s+$//; 215865863862SAndy Whitcroft # '*'s should not have spaces between. 2159f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 216065863862SAndy Whitcroft } 216165863862SAndy Whitcroft # Modifiers should have spaces. 216265863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 216365863862SAndy Whitcroft 2164667026e7SAndy Whitcroft #print "from<$from> to<$to> ident<$ident>\n"; 2165667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 2166000d1cc1SJoe Perches ERROR("POINTER_LOCATION", 2167000d1cc1SJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 216865863862SAndy Whitcroft } 21690a920b5bSAndy Whitcroft } 21700a920b5bSAndy Whitcroft 21710a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 21720a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 21730a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 21740a920b5bSAndy Whitcroft# print "$herecurr"; 21750a920b5bSAndy Whitcroft# $clean = 0; 21760a920b5bSAndy Whitcroft# } 21770a920b5bSAndy Whitcroft 21788905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 2179000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 2180000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 21818905a67cSAndy Whitcroft } 21828905a67cSAndy Whitcroft 218317441227SJoe Perches# check for uses of printk_ratelimit 218417441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 2185000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 2186000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 218717441227SJoe Perches } 218817441227SJoe Perches 218900df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 219000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 219100df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 219225985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 219300df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 2194f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 219500df344fSAndy Whitcroft my $ok = 0; 219600df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 219700df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 219825985edcSLucas De Marchi # we have a preceding printk if it ends 219900df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 220000df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 220100df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 220200df344fSAndy Whitcroft $ok = 1; 220300df344fSAndy Whitcroft } 220400df344fSAndy Whitcroft last; 220500df344fSAndy Whitcroft } 220600df344fSAndy Whitcroft } 220700df344fSAndy Whitcroft if ($ok == 0) { 2208000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 2209000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 22100a920b5bSAndy Whitcroft } 221100df344fSAndy Whitcroft } 22120a920b5bSAndy Whitcroft 2213653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 2214653d4876SAndy Whitcroft# or if closed on same line 2215c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2216c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 2217000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2218000d1cc1SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr); 22190a920b5bSAndy Whitcroft } 2220653d4876SAndy Whitcroft 22218905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 22228905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 22238905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2224000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2225000d1cc1SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev); 22268905a67cSAndy Whitcroft } 22278905a67cSAndy Whitcroft 22280c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 22290c73b4ebSAndy Whitcroft if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 2230000d1cc1SJoe Perches WARN("SPACING", 2231000d1cc1SJoe Perches "missing space after $1 definition\n" . $herecurr); 22320c73b4ebSAndy Whitcroft } 22330c73b4ebSAndy Whitcroft 22348d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 22358d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 2236fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2237fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 22388d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 22398d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 22408d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 2241fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 2242fe2a7dbcSAndy Whitcroft $prefix !~ /{\s+$/) { 2243000d1cc1SJoe Perches ERROR("BRACKET_SPACE", 2244000d1cc1SJoe Perches "space prohibited before open square bracket '['\n" . $herecurr); 22458d31cfceSAndy Whitcroft } 22468d31cfceSAndy Whitcroft } 22478d31cfceSAndy Whitcroft 2248f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 22496c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 2250c2fdda0dSAndy Whitcroft my $name = $1; 2251773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 2252773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 2253c2fdda0dSAndy Whitcroft 2254c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 2255773647a0SAndy Whitcroft if ($name =~ /^(?: 2256773647a0SAndy Whitcroft if|for|while|switch|return|case| 2257773647a0SAndy Whitcroft volatile|__volatile__| 2258773647a0SAndy Whitcroft __attribute__|format|__extension__| 2259773647a0SAndy Whitcroft asm|__asm__)$/x) 2260773647a0SAndy Whitcroft { 2261c2fdda0dSAndy Whitcroft 2262c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 2263c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 2264c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2265c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2266773647a0SAndy Whitcroft 2267773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2268c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2269c2fdda0dSAndy Whitcroft 2270c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2271c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2272773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2273c2fdda0dSAndy Whitcroft 2274c2fdda0dSAndy Whitcroft } else { 2275000d1cc1SJoe Perches WARN("SPACING", 2276000d1cc1SJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr); 2277f0a594c1SAndy Whitcroft } 22786c72ffaaSAndy Whitcroft } 2279653d4876SAndy Whitcroft# Check operator spacing. 22800a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 22819c0ca6f9SAndy Whitcroft my $ops = qr{ 22829c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 22839c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 22849c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 22851f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 22861f65f947SAndy Whitcroft \?|: 22879c0ca6f9SAndy Whitcroft }x; 2288cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 228900df344fSAndy Whitcroft my $off = 0; 22906c72ffaaSAndy Whitcroft 22916c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 22926c72ffaaSAndy Whitcroft 22930a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 22944a0df2efSAndy Whitcroft $off += length($elements[$n]); 22954a0df2efSAndy Whitcroft 229625985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 2297773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 2298773647a0SAndy Whitcroft my $cc = ''; 2299773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 2300773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 2301773647a0SAndy Whitcroft } 2302773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 2303773647a0SAndy Whitcroft 23044a0df2efSAndy Whitcroft my $a = ''; 23054a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 23064a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 2307cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 23084a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 23094a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 2310773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 23114a0df2efSAndy Whitcroft 23120a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 23134a0df2efSAndy Whitcroft 23144a0df2efSAndy Whitcroft my $c = ''; 23150a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 23164a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 23174a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2318cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 23194a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 23204a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 23218b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 23224a0df2efSAndy Whitcroft } else { 23234a0df2efSAndy Whitcroft $c = 'E'; 23240a920b5bSAndy Whitcroft } 23250a920b5bSAndy Whitcroft 23264a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 23274a0df2efSAndy Whitcroft 23284a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 23294a0df2efSAndy Whitcroft 23306c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 2331de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 23320a920b5bSAndy Whitcroft 233374048ed8SAndy Whitcroft # Pull out the value of this operator. 23346c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 23350a920b5bSAndy Whitcroft 23361f65f947SAndy Whitcroft # Get the full operator variant. 23371f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 23381f65f947SAndy Whitcroft 233913214adfSAndy Whitcroft # Ignore operators passed as parameters. 234013214adfSAndy Whitcroft if ($op_type ne 'V' && 234113214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 234213214adfSAndy Whitcroft 2343cf655043SAndy Whitcroft# # Ignore comments 2344cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 234513214adfSAndy Whitcroft 2346d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 234713214adfSAndy Whitcroft } elsif ($op eq ';') { 2348cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 2349cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 2350000d1cc1SJoe Perches ERROR("SPACING", 2351000d1cc1SJoe Perches "space required after that '$op' $at\n" . $hereptr); 2352d8aaf121SAndy Whitcroft } 2353d8aaf121SAndy Whitcroft 2354d8aaf121SAndy Whitcroft # // is a comment 2355d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 23560a920b5bSAndy Whitcroft 23571f65f947SAndy Whitcroft # No spaces for: 23581f65f947SAndy Whitcroft # -> 23591f65f947SAndy Whitcroft # : when part of a bitfield 23601f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 23614a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 2362000d1cc1SJoe Perches ERROR("SPACING", 2363000d1cc1SJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr); 23640a920b5bSAndy Whitcroft } 23650a920b5bSAndy Whitcroft 23660a920b5bSAndy Whitcroft # , must have a space on the right. 23670a920b5bSAndy Whitcroft } elsif ($op eq ',') { 2368cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2369000d1cc1SJoe Perches ERROR("SPACING", 2370000d1cc1SJoe Perches "space required after that '$op' $at\n" . $hereptr); 23710a920b5bSAndy Whitcroft } 23720a920b5bSAndy Whitcroft 23739c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 237474048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 23759c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 23769c0ca6f9SAndy Whitcroft 23779c0ca6f9SAndy Whitcroft # unary operators should have a space before and 23789c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 23799c0ca6f9SAndy Whitcroft # unary operator, or a cast 23809c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 238174048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 23820d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 2383cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2384000d1cc1SJoe Perches ERROR("SPACING", 2385000d1cc1SJoe Perches "space required before that '$op' $at\n" . $hereptr); 23860a920b5bSAndy Whitcroft } 2387a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2388171ae1a4SAndy Whitcroft # A unary '*' may be const 2389171ae1a4SAndy Whitcroft 2390171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 2391000d1cc1SJoe Perches ERROR("SPACING", 2392000d1cc1SJoe Perches "space prohibited after that '$op' $at\n" . $hereptr); 23930a920b5bSAndy Whitcroft } 23940a920b5bSAndy Whitcroft 23950a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 23960a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 2397773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2398000d1cc1SJoe Perches ERROR("SPACING", 2399000d1cc1SJoe Perches "space required one side of that '$op' $at\n" . $hereptr); 24000a920b5bSAndy Whitcroft } 2401773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 2402773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2403000d1cc1SJoe Perches ERROR("SPACING", 2404000d1cc1SJoe Perches "space prohibited before that '$op' $at\n" . $hereptr); 2405653d4876SAndy Whitcroft } 2406773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 2407000d1cc1SJoe Perches ERROR("SPACING", 2408000d1cc1SJoe Perches "space prohibited after that '$op' $at\n" . $hereptr); 2409773647a0SAndy Whitcroft } 2410773647a0SAndy Whitcroft 24110a920b5bSAndy Whitcroft 24120a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 24139c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 24149c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 24159c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 2416c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 2417c2fdda0dSAndy Whitcroft $op eq '%') 24180a920b5bSAndy Whitcroft { 2419773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 2420000d1cc1SJoe Perches ERROR("SPACING", 2421000d1cc1SJoe Perches "need consistent spacing around '$op' $at\n" . 2422de7d4f0eSAndy Whitcroft $hereptr); 24230a920b5bSAndy Whitcroft } 24240a920b5bSAndy Whitcroft 24251f65f947SAndy Whitcroft # A colon needs no spaces before when it is 24261f65f947SAndy Whitcroft # terminating a case value or a label. 24271f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 24281f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 2429000d1cc1SJoe Perches ERROR("SPACING", 2430000d1cc1SJoe Perches "space prohibited before that '$op' $at\n" . $hereptr); 24311f65f947SAndy Whitcroft } 24321f65f947SAndy Whitcroft 24330a920b5bSAndy Whitcroft # All the others need spaces both sides. 2434cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 24351f65f947SAndy Whitcroft my $ok = 0; 24361f65f947SAndy Whitcroft 243722f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 24381f65f947SAndy Whitcroft if (($op eq '<' && 24391f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 24401f65f947SAndy Whitcroft ($op eq '>' && 24411f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 24421f65f947SAndy Whitcroft { 24431f65f947SAndy Whitcroft $ok = 1; 24441f65f947SAndy Whitcroft } 24451f65f947SAndy Whitcroft 24461f65f947SAndy Whitcroft # Ignore ?: 24471f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 24481f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 24491f65f947SAndy Whitcroft $ok = 1; 24501f65f947SAndy Whitcroft } 24511f65f947SAndy Whitcroft 24521f65f947SAndy Whitcroft if ($ok == 0) { 2453000d1cc1SJoe Perches ERROR("SPACING", 2454000d1cc1SJoe Perches "spaces required around that '$op' $at\n" . $hereptr); 24550a920b5bSAndy Whitcroft } 245622f2a2efSAndy Whitcroft } 24574a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 24580a920b5bSAndy Whitcroft } 24590a920b5bSAndy Whitcroft } 24600a920b5bSAndy Whitcroft 2461f0a594c1SAndy Whitcroft# check for multiple assignments 2462f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 2463000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 2464000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 2465f0a594c1SAndy Whitcroft } 2466f0a594c1SAndy Whitcroft 246722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 246822f2a2efSAndy Whitcroft## # continuation. 246922f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 247022f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 247122f2a2efSAndy Whitcroft## 247222f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 247322f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 247422f2a2efSAndy Whitcroft## my $ln = $line; 247522f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 247622f2a2efSAndy Whitcroft## } 247722f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 2478000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 2479000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 248022f2a2efSAndy Whitcroft## } 248122f2a2efSAndy Whitcroft## } 2482f0a594c1SAndy Whitcroft 24830a920b5bSAndy Whitcroft#need space before brace following if, while, etc 248422f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 248522f2a2efSAndy Whitcroft $line =~ /do{/) { 2486000d1cc1SJoe Perches ERROR("SPACING", 2487000d1cc1SJoe Perches "space required before the open brace '{'\n" . $herecurr); 2488de7d4f0eSAndy Whitcroft } 2489de7d4f0eSAndy Whitcroft 2490de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 2491de7d4f0eSAndy Whitcroft# on the line 2492de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 2493000d1cc1SJoe Perches ERROR("SPACING", 2494000d1cc1SJoe Perches "space required after that close brace '}'\n" . $herecurr); 24950a920b5bSAndy Whitcroft } 24960a920b5bSAndy Whitcroft 249722f2a2efSAndy Whitcroft# check spacing on square brackets 249822f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2499000d1cc1SJoe Perches ERROR("SPACING", 2500000d1cc1SJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr); 250122f2a2efSAndy Whitcroft } 250222f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 2503000d1cc1SJoe Perches ERROR("SPACING", 2504000d1cc1SJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr); 250522f2a2efSAndy Whitcroft } 250622f2a2efSAndy Whitcroft 2507c45dcabdSAndy Whitcroft# check spacing on parentheses 25089c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 25099c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 2510000d1cc1SJoe Perches ERROR("SPACING", 2511000d1cc1SJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr); 251222f2a2efSAndy Whitcroft } 251313214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2514c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2515c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2516000d1cc1SJoe Perches ERROR("SPACING", 2517000d1cc1SJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr); 251822f2a2efSAndy Whitcroft } 251922f2a2efSAndy Whitcroft 25200a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 25214a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 25220a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2523000d1cc1SJoe Perches WARN("INDENTED_LABEL", 2524000d1cc1SJoe Perches "labels should not be indented\n" . $herecurr); 25250a920b5bSAndy Whitcroft } 25260a920b5bSAndy Whitcroft 2527c45dcabdSAndy Whitcroft# Return is not a function. 2528c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2529c45dcabdSAndy Whitcroft my $spacing = $1; 2530c45dcabdSAndy Whitcroft my $value = $2; 2531c45dcabdSAndy Whitcroft 253286f9d059SAndy Whitcroft # Flatten any parentheses 2533fb2d2c1bSAndy Whitcroft $value =~ s/\(/ \(/g; 2534fb2d2c1bSAndy Whitcroft $value =~ s/\)/\) /g; 253563f17f89SAndy Whitcroft while ($value =~ s/\[[^\{\}]*\]/1/ || 253663f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 253763f17f89SAndy Whitcroft $Compare\s* 253863f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 253963f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2540c45dcabdSAndy Whitcroft } 2541fb2d2c1bSAndy Whitcroft#print "value<$value>\n"; 2542fb2d2c1bSAndy Whitcroft if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 2543000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 2544000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 2545c45dcabdSAndy Whitcroft 2546c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2547000d1cc1SJoe Perches ERROR("SPACING", 2548000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 2549c45dcabdSAndy Whitcroft } 2550c45dcabdSAndy Whitcroft } 255153a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 255253a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 255353a3c448SAndy Whitcroft my $name = $1; 255453a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 2555000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 2556000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 255753a3c448SAndy Whitcroft } 255853a3c448SAndy Whitcroft } 2559c45dcabdSAndy Whitcroft 25607d2367afSJoe Perches# typecasts on min/max could be min_t/max_t 25617d2367afSJoe Perches if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) { 25627d2367afSJoe Perches if (defined $2 || defined $8) { 25637d2367afSJoe Perches my $call = $1; 25647d2367afSJoe Perches my $cast1 = deparenthesize($2); 25657d2367afSJoe Perches my $arg1 = $3; 25667d2367afSJoe Perches my $cast2 = deparenthesize($8); 25677d2367afSJoe Perches my $arg2 = $9; 25687d2367afSJoe Perches my $cast; 25697d2367afSJoe Perches 25707d2367afSJoe Perches if ($cast1 ne "" && $cast2 ne "") { 25717d2367afSJoe Perches $cast = "$cast1 or $cast2"; 25727d2367afSJoe Perches } elsif ($cast1 ne "") { 25737d2367afSJoe Perches $cast = $cast1; 25747d2367afSJoe Perches } else { 25757d2367afSJoe Perches $cast = $cast2; 25767d2367afSJoe Perches } 257730ecad51SHui Zhu WARN("MINMAX", 257830ecad51SHui Zhu "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr); 25797d2367afSJoe Perches } 25807d2367afSJoe Perches } 25817d2367afSJoe Perches 25820a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 25834a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2584000d1cc1SJoe Perches ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); 25850a920b5bSAndy Whitcroft } 25860a920b5bSAndy Whitcroft 2587f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2588f5fe35ddSAndy Whitcroft# statements after the conditional. 2589170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 2590170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2591170d3a22SAndy Whitcroft $remain_next, $off_next); 2592170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2593170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2594170d3a22SAndy Whitcroft 2595170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2596170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2597170d3a22SAndy Whitcroft # then count those as offsets. 2598170d3a22SAndy Whitcroft my ($whitespace) = 2599170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2600170d3a22SAndy Whitcroft my $offset = 2601170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2602170d3a22SAndy Whitcroft 2603170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2604170d3a22SAndy Whitcroft $offset} = 1; 2605170d3a22SAndy Whitcroft } 2606170d3a22SAndy Whitcroft } 2607170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2608170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2609171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 26108905a67cSAndy Whitcroft 2611b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2612000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 2613000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 26148905a67cSAndy Whitcroft } 26158905a67cSAndy Whitcroft 26168905a67cSAndy Whitcroft # Find out what is on the end of the line after the 26178905a67cSAndy Whitcroft # conditional. 2618773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 26198905a67cSAndy Whitcroft $s =~ s/\n.*//g; 262013214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 262153210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 262253210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2623773647a0SAndy Whitcroft { 2624bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2625bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2626bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 262742bdf74cSHidetoshi Seto my $stat_real = ''; 2628bb44ad39SAndy Whitcroft 262942bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 263042bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 2631bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2632bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2633bb44ad39SAndy Whitcroft } 2634bb44ad39SAndy Whitcroft 2635000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2636000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 26378905a67cSAndy Whitcroft } 26388905a67cSAndy Whitcroft } 26398905a67cSAndy Whitcroft 264013214adfSAndy Whitcroft# Check for bitwise tests written as boolean 264113214adfSAndy Whitcroft if ($line =~ / 264213214adfSAndy Whitcroft (?: 264313214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 264413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 264513214adfSAndy Whitcroft (?:\&\&|\|\|) 264613214adfSAndy Whitcroft | 264713214adfSAndy Whitcroft (?:\&\&|\|\|) 264813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 264913214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 265013214adfSAndy Whitcroft )/x) 265113214adfSAndy Whitcroft { 2652000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 2653000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 265413214adfSAndy Whitcroft } 265513214adfSAndy Whitcroft 26568905a67cSAndy Whitcroft# if and else should not have general statements after it 265713214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 265813214adfSAndy Whitcroft my $s = $1; 265913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 266013214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 2661000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2662000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 26630a920b5bSAndy Whitcroft } 266413214adfSAndy Whitcroft } 266539667782SAndy Whitcroft# if should not continue a brace 266639667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 2667000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2668000d1cc1SJoe Perches "trailing statements should be on next line\n" . 266939667782SAndy Whitcroft $herecurr); 267039667782SAndy Whitcroft } 2671a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2672a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2673a1080bf8SAndy Whitcroft $line !~ /\G(?: 26743fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2675a1080bf8SAndy Whitcroft \s*return\s+ 2676a1080bf8SAndy Whitcroft )/xg) 2677a1080bf8SAndy Whitcroft { 2678000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2679000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 2680a1080bf8SAndy Whitcroft } 26810a920b5bSAndy Whitcroft 26820a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 26830a920b5bSAndy Whitcroft # indent level to be relevant to each other. 26840a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 26850a920b5bSAndy Whitcroft $previndent == $indent) { 2686000d1cc1SJoe Perches ERROR("ELSE_AFTER_BRACE", 2687000d1cc1SJoe Perches "else should follow close brace '}'\n" . $hereprev); 26880a920b5bSAndy Whitcroft } 26890a920b5bSAndy Whitcroft 2690c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2691c2fdda0dSAndy Whitcroft $previndent == $indent) { 2692c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2693c2fdda0dSAndy Whitcroft 2694c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2695c2fdda0dSAndy Whitcroft # conditional. 2696773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2697c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2698c2fdda0dSAndy Whitcroft 2699c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2700000d1cc1SJoe Perches ERROR("WHILE_AFTER_BRACE", 2701000d1cc1SJoe Perches "while should follow close brace '}'\n" . $hereprev); 2702c2fdda0dSAndy Whitcroft } 2703c2fdda0dSAndy Whitcroft } 2704c2fdda0dSAndy Whitcroft 27050a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 27060a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 27070a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 27080a920b5bSAndy Whitcroft# print "$herecurr"; 27090a920b5bSAndy Whitcroft# $clean = 0; 27100a920b5bSAndy Whitcroft# } 27110a920b5bSAndy Whitcroft 27120a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2713c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2714000d1cc1SJoe Perches WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 2715000d1cc1SJoe Perches "Whitepspace after \\ makes next lines useless\n" . $herecurr); 27160a920b5bSAndy Whitcroft } 27170a920b5bSAndy Whitcroft 2718653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2719c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2720e09dec48SAndy Whitcroft my $file = "$1.h"; 2721e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2722e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2723e09dec48SAndy Whitcroft $realfile ne $checkfile && 27247840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 2725c45dcabdSAndy Whitcroft { 2726e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2727000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 2728000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2729e09dec48SAndy Whitcroft } else { 2730000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 2731000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2732e09dec48SAndy Whitcroft } 27330a920b5bSAndy Whitcroft } 27340a920b5bSAndy Whitcroft } 27350a920b5bSAndy Whitcroft 2736653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2737653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2738cf655043SAndy Whitcroft# in a known good container 2739b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2740b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2741d8aaf121SAndy Whitcroft my $ln = $linenr; 2742d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2743c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2744c45dcabdSAndy Whitcroft my $ctx = ''; 2745653d4876SAndy Whitcroft 2746c45dcabdSAndy Whitcroft my $args = defined($1); 2747de7d4f0eSAndy Whitcroft 2748c45dcabdSAndy Whitcroft # Find the end of the macro and limit our statement 2749c45dcabdSAndy Whitcroft # search to that. 2750c45dcabdSAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1] && 2751c45dcabdSAndy Whitcroft $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2752c45dcabdSAndy Whitcroft { 2753c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1] . "\n"; 2754a3bb97a7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 2755c45dcabdSAndy Whitcroft $ln++; 2756de7d4f0eSAndy Whitcroft } 2757c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1]; 2758d8aaf121SAndy Whitcroft 2759c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2760c45dcabdSAndy Whitcroft ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2761c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2762a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2763c45dcabdSAndy Whitcroft 2764c45dcabdSAndy Whitcroft # Extract the remainder of the define (if any) and 2765c45dcabdSAndy Whitcroft # rip off surrounding spaces, and trailing \'s. 2766c45dcabdSAndy Whitcroft $rest = ''; 2767636d140aSAndy Whitcroft while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2768636d140aSAndy Whitcroft #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2769a3bb97a7SAndy Whitcroft if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2770c45dcabdSAndy Whitcroft $rest .= substr($lines[$ln - 1], $off) . "\n"; 2771c45dcabdSAndy Whitcroft $cnt--; 2772a3bb97a7SAndy Whitcroft } 2773a3bb97a7SAndy Whitcroft $ln++; 2774c45dcabdSAndy Whitcroft $off = 0; 2775c45dcabdSAndy Whitcroft } 2776c45dcabdSAndy Whitcroft $rest =~ s/\\\n.//g; 2777c45dcabdSAndy Whitcroft $rest =~ s/^\s*//s; 2778c45dcabdSAndy Whitcroft $rest =~ s/\s*$//s; 2779c45dcabdSAndy Whitcroft 2780c45dcabdSAndy Whitcroft # Clean up the original statement. 2781c45dcabdSAndy Whitcroft if ($args) { 2782c45dcabdSAndy Whitcroft substr($dstat, 0, length($dcond), ''); 2783d8aaf121SAndy Whitcroft } else { 2784c45dcabdSAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2785c45dcabdSAndy Whitcroft } 2786292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2787c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2788c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2789c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2790c45dcabdSAndy Whitcroft 2791c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2792bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2793bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2794bf30d6edSAndy Whitcroft $dstat =~ s/\[[^\{\}]*\]/1/) 2795bf30d6edSAndy Whitcroft { 2796c45dcabdSAndy Whitcroft } 2797c45dcabdSAndy Whitcroft 2798c45dcabdSAndy Whitcroft my $exceptions = qr{ 2799c45dcabdSAndy Whitcroft $Declare| 2800c45dcabdSAndy Whitcroft module_param_named| 2801c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 2802c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2803c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2804383099fdSAndy Whitcroft __typeof__\(| 280522fd2d3eSStefani Seibold union| 280622fd2d3eSStefani Seibold struct| 2807ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 2808ea71a0a0SAndy Whitcroft ^\"|\"$ 2809c45dcabdSAndy Whitcroft }x; 28105eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 28115eaa20b9SAndy Whitcroft if ($rest ne '' && $rest ne ',') { 2812c45dcabdSAndy Whitcroft if ($rest !~ /while\s*\(/ && 2813c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/) 2814c45dcabdSAndy Whitcroft { 2815000d1cc1SJoe Perches ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 2816000d1cc1SJoe Perches "Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2817c45dcabdSAndy Whitcroft } 2818c45dcabdSAndy Whitcroft 2819c45dcabdSAndy Whitcroft } elsif ($ctx !~ /;/) { 2820c45dcabdSAndy Whitcroft if ($dstat ne '' && 2821c45dcabdSAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2822c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/ && 2823b132e5d5SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && 2824c45dcabdSAndy Whitcroft $dstat =~ /$Operators/) 2825c45dcabdSAndy Whitcroft { 2826000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 2827000d1cc1SJoe Perches "Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2828d8aaf121SAndy Whitcroft } 28290a920b5bSAndy Whitcroft } 2830653d4876SAndy Whitcroft } 28310a920b5bSAndy Whitcroft 2832080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 2833080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 2834080ba929SMike Frysinger# . 2835080ba929SMike Frysinger# ALIGN(...) 2836080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 2837080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 2838000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 2839000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 2840080ba929SMike Frysinger } 2841080ba929SMike Frysinger 2842f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 284313214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 284413214adfSAndy Whitcroft my ($level, $endln, @chunks) = 2845cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 284613214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2847cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2848cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 284913214adfSAndy Whitcroft my $allowed = 0; 285013214adfSAndy Whitcroft my $seen = 0; 2851773647a0SAndy Whitcroft my $herectx = $here . "\n"; 2852cf655043SAndy Whitcroft my $ln = $linenr - 1; 285313214adfSAndy Whitcroft for my $chunk (@chunks) { 285413214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 285513214adfSAndy Whitcroft 2856773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 2857773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2858773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 2859773647a0SAndy Whitcroft 2860773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2861773647a0SAndy Whitcroft 2862773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 2863773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 2864773647a0SAndy Whitcroft 2865773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2866cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 2867cf655043SAndy Whitcroft 2868773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 286913214adfSAndy Whitcroft 287013214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 287113214adfSAndy Whitcroft 2872cf655043SAndy Whitcroft #print "cond<$cond> block<$block> allowed<$allowed>\n"; 2873cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2874cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 287513214adfSAndy Whitcroft $allowed = 1; 287613214adfSAndy Whitcroft } 287713214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2878cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 287913214adfSAndy Whitcroft $allowed = 1; 288013214adfSAndy Whitcroft } 2881cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2882cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 288313214adfSAndy Whitcroft $allowed = 1; 288413214adfSAndy Whitcroft } 288513214adfSAndy Whitcroft } 288613214adfSAndy Whitcroft if ($seen && !$allowed) { 2887000d1cc1SJoe Perches WARN("BRACES", 2888000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 288913214adfSAndy Whitcroft } 289013214adfSAndy Whitcroft } 289113214adfSAndy Whitcroft } 2892773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 289313214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 2894cf655043SAndy Whitcroft my $allowed = 0; 2895f0a594c1SAndy Whitcroft 2896cf655043SAndy Whitcroft # Check the pre-context. 2897cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2898cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 2899cf655043SAndy Whitcroft $allowed = 1; 2900f0a594c1SAndy Whitcroft } 2901773647a0SAndy Whitcroft 2902773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 2903773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 2904773647a0SAndy Whitcroft 2905cf655043SAndy Whitcroft # Check the condition. 2906cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 2907773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 2908cf655043SAndy Whitcroft if (defined $cond) { 2909773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2910cf655043SAndy Whitcroft } 2911cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2912cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 2913cf655043SAndy Whitcroft $allowed = 1; 2914cf655043SAndy Whitcroft } 2915cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2916cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 2917cf655043SAndy Whitcroft $allowed = 1; 2918cf655043SAndy Whitcroft } 2919cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2920cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 2921cf655043SAndy Whitcroft $allowed = 1; 2922cf655043SAndy Whitcroft } 2923cf655043SAndy Whitcroft # Check the post-context. 2924cf655043SAndy Whitcroft if (defined $chunks[1]) { 2925cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 2926cf655043SAndy Whitcroft if (defined $cond) { 2927773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2928cf655043SAndy Whitcroft } 2929cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 2930cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 2931cf655043SAndy Whitcroft $allowed = 1; 2932cf655043SAndy Whitcroft } 2933cf655043SAndy Whitcroft } 2934cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 293569932487SJustin P. Mattock my $herectx = $here . "\n"; 2936f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 2937cf655043SAndy Whitcroft 2938f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 293969932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 2940cf655043SAndy Whitcroft } 2941cf655043SAndy Whitcroft 2942000d1cc1SJoe Perches WARN("BRACES", 2943000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 2944f0a594c1SAndy Whitcroft } 2945f0a594c1SAndy Whitcroft } 2946f0a594c1SAndy Whitcroft 2947653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 29484a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 2949c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 2950000d1cc1SJoe Perches ERROR("DEPRECATED_INCLUDE", 2951000d1cc1SJoe Perches "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 29520a920b5bSAndy Whitcroft } 29530a920b5bSAndy Whitcroft } 29540a920b5bSAndy Whitcroft 29554a0df2efSAndy Whitcroft# don't use deprecated functions 29564a0df2efSAndy Whitcroft for my $func (@dep_functions) { 295700df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 2958000d1cc1SJoe Perches ERROR("DEPRECATED_FUNCTION", 2959000d1cc1SJoe Perches "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 29604a0df2efSAndy Whitcroft } 29614a0df2efSAndy Whitcroft } 29624a0df2efSAndy Whitcroft 29634a0df2efSAndy Whitcroft# no volatiles please 29646c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 29656c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 2966000d1cc1SJoe Perches WARN("VOLATILE", 2967000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 29684a0df2efSAndy Whitcroft } 29694a0df2efSAndy Whitcroft 297000df344fSAndy Whitcroft# warn about #if 0 2971c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2972000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 2973000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 2974de7d4f0eSAndy Whitcroft $herecurr); 29754a0df2efSAndy Whitcroft } 29764a0df2efSAndy Whitcroft 2977f0a594c1SAndy Whitcroft# check for needless kfree() checks 2978f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2979f0a594c1SAndy Whitcroft my $expr = $1; 2980f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 2981000d1cc1SJoe Perches WARN("NEEDLESS_KFREE", 2982000d1cc1SJoe Perches "kfree(NULL) is safe this check is probably not required\n" . $hereprev); 2983f0a594c1SAndy Whitcroft } 2984f0a594c1SAndy Whitcroft } 29854c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks 29864c432a8fSGreg Kroah-Hartman if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 29874c432a8fSGreg Kroah-Hartman my $expr = $1; 29884c432a8fSGreg Kroah-Hartman if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { 2989000d1cc1SJoe Perches WARN("NEEDLESS_USB_FREE_URB", 2990000d1cc1SJoe Perches "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); 29914c432a8fSGreg Kroah-Hartman } 29924c432a8fSGreg Kroah-Hartman } 2993f0a594c1SAndy Whitcroft 29941a15a250SPatrick Pannuto# prefer usleep_range over udelay 29951a15a250SPatrick Pannuto if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { 29961a15a250SPatrick Pannuto # ignore udelay's < 10, however 29971a15a250SPatrick Pannuto if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { 2998000d1cc1SJoe Perches CHK("USLEEP_RANGE", 2999000d1cc1SJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); 30001a15a250SPatrick Pannuto } 30011a15a250SPatrick Pannuto } 30021a15a250SPatrick Pannuto 300309ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 300409ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 300509ef8725SPatrick Pannuto if ($1 < 20) { 3006000d1cc1SJoe Perches WARN("MSLEEP", 3007000d1cc1SJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); 300809ef8725SPatrick Pannuto } 300909ef8725SPatrick Pannuto } 301009ef8725SPatrick Pannuto 301100df344fSAndy Whitcroft# warn about #ifdefs in C files 3012c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 301300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 301400df344fSAndy Whitcroft# print "$herecurr"; 301500df344fSAndy Whitcroft# $clean = 0; 301600df344fSAndy Whitcroft# } 301700df344fSAndy Whitcroft 301822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 3019c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 3020000d1cc1SJoe Perches ERROR("SPACING", 3021000d1cc1SJoe Perches "exactly one space required after that #$1\n" . $herecurr); 302222f2a2efSAndy Whitcroft } 302322f2a2efSAndy Whitcroft 30244a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 3025171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 3026171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 30274a0df2efSAndy Whitcroft my $which = $1; 30284a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3029000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 3030000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 30314a0df2efSAndy Whitcroft } 30324a0df2efSAndy Whitcroft } 30334a0df2efSAndy Whitcroft# check for memory barriers without a comment. 30344a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 30354a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3036000d1cc1SJoe Perches CHK("MEMORY_BARRIER", 3037000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 30384a0df2efSAndy Whitcroft } 30394a0df2efSAndy Whitcroft } 30404a0df2efSAndy Whitcroft# check of hardware specific defines 3041c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 3042000d1cc1SJoe Perches CHK("ARCH_DEFINES", 3043000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 30440a920b5bSAndy Whitcroft } 3045653d4876SAndy Whitcroft 3046d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 3047d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 3048000d1cc1SJoe Perches WARN("STORAGE_CLASS", 3049000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 3050d4977c78STobias Klauser } 3051d4977c78STobias Klauser 3052de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 3053de7d4f0eSAndy Whitcroft# storage class and type. 30549c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 30559c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 3056000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 3057000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 3058de7d4f0eSAndy Whitcroft } 3059de7d4f0eSAndy Whitcroft 30608905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 30618905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 3062000d1cc1SJoe Perches WARN("INLINE", 3063000d1cc1SJoe Perches "plain inline is preferred over $1\n" . $herecurr); 30648905a67cSAndy Whitcroft } 30658905a67cSAndy Whitcroft 30663d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 30673d130fd0SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 3068000d1cc1SJoe Perches WARN("PREFER_PACKED", 3069000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 30703d130fd0SJoe Perches } 30713d130fd0SJoe Perches 307239b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 307339b7e287SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 3074000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 3075000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 307639b7e287SJoe Perches } 307739b7e287SJoe Perches 30788f53a9b8SJoe Perches# check for sizeof(&) 30798f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 3080000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 3081000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 30828f53a9b8SJoe Perches } 30838f53a9b8SJoe Perches 3084428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 3085428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 3086000d1cc1SJoe Perches WARN("LINE_CONTINUATIONS", 3087000d1cc1SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 3088428e2fdcSJoe Perches } 3089428e2fdcSJoe Perches 3090de7d4f0eSAndy Whitcroft# check for new externs in .c files. 3091171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 3092c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3093171ae1a4SAndy Whitcroft { 3094c45dcabdSAndy Whitcroft my $function_name = $1; 3095c45dcabdSAndy Whitcroft my $paren_space = $2; 3096171ae1a4SAndy Whitcroft 3097171ae1a4SAndy Whitcroft my $s = $stat; 3098171ae1a4SAndy Whitcroft if (defined $cond) { 3099171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 3100171ae1a4SAndy Whitcroft } 3101c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 3102c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 3103c45dcabdSAndy Whitcroft { 3104000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3105000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3106de7d4f0eSAndy Whitcroft } 3107de7d4f0eSAndy Whitcroft 3108171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 3109000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 3110000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 3111171ae1a4SAndy Whitcroft } 31129c9ba34eSAndy Whitcroft 31139c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 31149c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 31159c9ba34eSAndy Whitcroft { 3116000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3117000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3118171ae1a4SAndy Whitcroft } 3119171ae1a4SAndy Whitcroft 3120de7d4f0eSAndy Whitcroft# checks for new __setup's 3121de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 3122de7d4f0eSAndy Whitcroft my $name = $1; 3123de7d4f0eSAndy Whitcroft 3124de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 3125000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 3126000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 3127de7d4f0eSAndy Whitcroft } 3128653d4876SAndy Whitcroft } 31299c0ca6f9SAndy Whitcroft 31309c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 3131caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 3132000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 3133000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 31349c0ca6f9SAndy Whitcroft } 313513214adfSAndy Whitcroft 3136caf2a54fSJoe Perches# check for multiple semicolons 3137caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 3138000d1cc1SJoe Perches WARN("ONE_SEMICOLON", 3139000d1cc1SJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr); 3140caf2a54fSJoe Perches } 3141caf2a54fSJoe Perches 314213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 314313214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 3144000d1cc1SJoe Perches WARN("USE_FUNC", 3145000d1cc1SJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 314613214adfSAndy Whitcroft } 3147773647a0SAndy Whitcroft 31484882720bSThomas Gleixner# check for semaphores initialized locked 31494882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 3150000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 3151000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 31521704f47bSPeter Zijlstra 3153773647a0SAndy Whitcroft } 3154*67d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 3155*67d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 3156000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 3157*67d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 3158773647a0SAndy Whitcroft } 3159f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 3160f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 3161000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 3162000d1cc1SJoe Perches "please use device_initcall() instead of __initcall()\n" . $herecurr); 3163f3db6639SMichael Ellerman } 316479404849SEmese Revfy# check for various ops structs, ensure they are const. 316579404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 316679404849SEmese Revfy address_space_operations| 316779404849SEmese Revfy backlight_ops| 316879404849SEmese Revfy block_device_operations| 316979404849SEmese Revfy dentry_operations| 317079404849SEmese Revfy dev_pm_ops| 317179404849SEmese Revfy dma_map_ops| 317279404849SEmese Revfy extent_io_ops| 317379404849SEmese Revfy file_lock_operations| 317479404849SEmese Revfy file_operations| 317579404849SEmese Revfy hv_ops| 317679404849SEmese Revfy ide_dma_ops| 317779404849SEmese Revfy intel_dvo_dev_ops| 317879404849SEmese Revfy item_operations| 317979404849SEmese Revfy iwl_ops| 318079404849SEmese Revfy kgdb_arch| 318179404849SEmese Revfy kgdb_io| 318279404849SEmese Revfy kset_uevent_ops| 318379404849SEmese Revfy lock_manager_operations| 318479404849SEmese Revfy microcode_ops| 318579404849SEmese Revfy mtrr_ops| 318679404849SEmese Revfy neigh_ops| 318779404849SEmese Revfy nlmsvc_binding| 318879404849SEmese Revfy pci_raw_ops| 318979404849SEmese Revfy pipe_buf_operations| 319079404849SEmese Revfy platform_hibernation_ops| 319179404849SEmese Revfy platform_suspend_ops| 319279404849SEmese Revfy proto_ops| 319379404849SEmese Revfy rpc_pipe_ops| 319479404849SEmese Revfy seq_operations| 319579404849SEmese Revfy snd_ac97_build_ops| 319679404849SEmese Revfy soc_pcmcia_socket_ops| 319779404849SEmese Revfy stacktrace_ops| 319879404849SEmese Revfy sysfs_ops| 319979404849SEmese Revfy tty_operations| 320079404849SEmese Revfy usb_mon_operations| 320179404849SEmese Revfy wd_ops}x; 32026903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 320379404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 3204000d1cc1SJoe Perches WARN("CONST_STRUCT", 3205000d1cc1SJoe Perches "struct $1 should normally be const\n" . 32066903ffb2SAndy Whitcroft $herecurr); 32072b6db5cbSAndy Whitcroft } 3208773647a0SAndy Whitcroft 3209773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 3210773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 3211773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 3212c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 3213c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 3214171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 3215171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 3216171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 3217773647a0SAndy Whitcroft { 3218000d1cc1SJoe Perches WARN("NR_CPUS", 3219000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 3220773647a0SAndy Whitcroft } 32219c9ba34eSAndy Whitcroft 32229c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 32239c9ba34eSAndy Whitcroft my $string; 32249c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 32259c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 32262a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 32279c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 3228000d1cc1SJoe Perches WARN("PRINTF_L", 3229000d1cc1SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 32309c9ba34eSAndy Whitcroft last; 32319c9ba34eSAndy Whitcroft } 32329c9ba34eSAndy Whitcroft } 3233691d77b6SAndy Whitcroft 3234691d77b6SAndy Whitcroft# whine mightly about in_atomic 3235691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 3236691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 3237000d1cc1SJoe Perches ERROR("IN_ATOMIC", 3238000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 3239f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 3240000d1cc1SJoe Perches WARN("IN_ATOMIC", 3241000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 3242691d77b6SAndy Whitcroft } 3243691d77b6SAndy Whitcroft } 32441704f47bSPeter Zijlstra 32451704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 32461704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 32471704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 32481704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 32491704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 32501704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 3251000d1cc1SJoe Perches ERROR("LOCKDEP", 3252000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 32531704f47bSPeter Zijlstra } 32541704f47bSPeter Zijlstra } 325588f8831cSDave Jones 325688f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 325788f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 3258000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 3259000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 326088f8831cSDave Jones } 3261309c00c7SDave Jones 3262309c00c7SDave Jones # Check for memset with swapped arguments 3263309c00c7SDave Jones if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) { 3264000d1cc1SJoe Perches ERROR("MEMSET", 3265000d1cc1SJoe Perches "memset size is 3rd argument, not the second.\n" . $herecurr); 3266309c00c7SDave Jones } 326713214adfSAndy Whitcroft } 326813214adfSAndy Whitcroft 326913214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 327013214adfSAndy Whitcroft # so just keep quiet. 327113214adfSAndy Whitcroft if ($#rawlines == -1) { 327213214adfSAndy Whitcroft exit(0); 32730a920b5bSAndy Whitcroft } 32740a920b5bSAndy Whitcroft 32758905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 32768905a67cSAndy Whitcroft # things that appear to be patches. 32778905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 32788905a67cSAndy Whitcroft exit(0); 32798905a67cSAndy Whitcroft } 32808905a67cSAndy Whitcroft 32818905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 32828905a67cSAndy Whitcroft # just keep quiet. 32838905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 32848905a67cSAndy Whitcroft exit(0); 32858905a67cSAndy Whitcroft } 32868905a67cSAndy Whitcroft 32878905a67cSAndy Whitcroft if (!$is_patch) { 3288000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 3289000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 32900a920b5bSAndy Whitcroft } 32910a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 3292000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 3293000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 32940a920b5bSAndy Whitcroft } 32950a920b5bSAndy Whitcroft 3296f0a594c1SAndy Whitcroft print report_dump(); 329713214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 329813214adfSAndy Whitcroft print "$filename " if ($summary_file); 32996c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 33006c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 33016c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 33028905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 33036c72ffaaSAndy Whitcroft } 33048905a67cSAndy Whitcroft 3305d2c0a235SAndy Whitcroft if ($quiet == 0) { 3306d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 3307d2c0a235SAndy Whitcroft # then suggest that. 3308d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 3309d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 3310d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 3311b0781216SMike Frysinger $rpt_cleaners = 0; 3312d2c0a235SAndy Whitcroft } 3313d2c0a235SAndy Whitcroft } 3314d2c0a235SAndy Whitcroft 3315000d1cc1SJoe Perches if (keys %ignore_type) { 3316000d1cc1SJoe Perches print "NOTE: Ignored message types:"; 3317000d1cc1SJoe Perches foreach my $ignore (sort keys %ignore_type) { 3318000d1cc1SJoe Perches print " $ignore"; 3319000d1cc1SJoe Perches } 3320000d1cc1SJoe Perches print "\n"; 3321000d1cc1SJoe Perches print "\n" if ($quiet == 0); 3322000d1cc1SJoe Perches } 3323000d1cc1SJoe Perches 33240a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 3325c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 33260a920b5bSAndy Whitcroft } 33270a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 3328000d1cc1SJoe Perches print << "EOM"; 3329000d1cc1SJoe Perches$vname has style problems, please review. 3330000d1cc1SJoe Perches 3331000d1cc1SJoe PerchesIf any of these errors are false positives, please report 3332000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 3333000d1cc1SJoe PerchesEOM 33340a920b5bSAndy Whitcroft } 333513214adfSAndy Whitcroft 33360a920b5bSAndy Whitcroft return $clean; 33370a920b5bSAndy Whitcroft} 3338