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 230d7c76ba7SJoe Perchesour $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-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 24315662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 24415662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 245171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 246171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 247171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 248171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 249171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 250171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 251171ae1a4SAndy Whitcroft}x; 252171ae1a4SAndy Whitcroft 25315662b3eSJoe Perchesour $UTF8 = qr{ 25415662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 25515662b3eSJoe Perches | $NON_ASCII_UTF8 25615662b3eSJoe Perches}x; 25715662b3eSJoe Perches 2588ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 259fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 2608ed22cadSAndy Whitcroft atomic_t 2618ed22cadSAndy Whitcroft)}; 2628ed22cadSAndy Whitcroft 263691e669bSJoe Perchesour $logFunctions = qr{(?x: 2646e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 2656e60c02eSJoe Perches [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 2666e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 267b0531722SJoe Perches panic| 268b0531722SJoe Perches MODULE_[A-Z_]+ 269691e669bSJoe Perches)}; 270691e669bSJoe Perches 27120112475SJoe Perchesour $signature_tags = qr{(?xi: 27220112475SJoe Perches Signed-off-by:| 27320112475SJoe Perches Acked-by:| 27420112475SJoe Perches Tested-by:| 27520112475SJoe Perches Reviewed-by:| 27620112475SJoe Perches Reported-by:| 27720112475SJoe Perches To:| 27820112475SJoe Perches Cc: 27920112475SJoe Perches)}; 28020112475SJoe Perches 2818905a67cSAndy Whitcroftour @typeList = ( 2828905a67cSAndy Whitcroft qr{void}, 283c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 284c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 285c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 286c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 287c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 288c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 289c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 2908905a67cSAndy Whitcroft qr{unsigned}, 2918905a67cSAndy Whitcroft qr{float}, 2928905a67cSAndy Whitcroft qr{double}, 2938905a67cSAndy Whitcroft qr{bool}, 2948905a67cSAndy Whitcroft qr{struct\s+$Ident}, 2958905a67cSAndy Whitcroft qr{union\s+$Ident}, 2968905a67cSAndy Whitcroft qr{enum\s+$Ident}, 2978905a67cSAndy Whitcroft qr{${Ident}_t}, 2988905a67cSAndy Whitcroft qr{${Ident}_handler}, 2998905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 3008905a67cSAndy Whitcroft); 301c45dcabdSAndy Whitcroftour @modifierList = ( 302c45dcabdSAndy Whitcroft qr{fastcall}, 303c45dcabdSAndy Whitcroft); 3048905a67cSAndy Whitcroft 3057840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 3067840a94cSWolfram Sang irq| 3077840a94cSWolfram Sang memory 3087840a94cSWolfram Sang)}; 3097840a94cSWolfram Sang# memory.h: ARM has a custom one 3107840a94cSWolfram Sang 3118905a67cSAndy Whitcroftsub build_types { 312d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 313d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 314c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 3158905a67cSAndy Whitcroft $NonptrType = qr{ 316d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 317cf655043SAndy Whitcroft (?: 3186b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 3198ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 320c45dcabdSAndy Whitcroft (?:${all}\b) 321cf655043SAndy Whitcroft ) 322c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 3238905a67cSAndy Whitcroft }x; 3248905a67cSAndy Whitcroft $Type = qr{ 325c45dcabdSAndy Whitcroft $NonptrType 326b337d8b8SAndy Whitcroft (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 327c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 3288905a67cSAndy Whitcroft }x; 3298905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 3308905a67cSAndy Whitcroft} 3318905a67cSAndy Whitcroftbuild_types(); 3326c72ffaaSAndy Whitcroft 3337d2367afSJoe Perches 3347d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 335d1fe9c09SJoe Perches 336d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 337d1fe9c09SJoe Perches# requires at least perl version v5.10.0 338d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 339d1fe9c09SJoe Perches 340d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 341d1fe9c09SJoe Perchesour $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; 342d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; 3437d2367afSJoe Perches 3447d2367afSJoe Perchessub deparenthesize { 3457d2367afSJoe Perches my ($string) = @_; 3467d2367afSJoe Perches return "" if (!defined($string)); 3477d2367afSJoe Perches $string =~ s@^\s*\(\s*@@g; 3487d2367afSJoe Perches $string =~ s@\s*\)\s*$@@g; 3497d2367afSJoe Perches $string =~ s@\s+@ @g; 3507d2367afSJoe Perches return $string; 3517d2367afSJoe Perches} 3527d2367afSJoe Perches 3536c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 3540a920b5bSAndy Whitcroft 35500df344fSAndy Whitcroftmy @rawlines = (); 356c2fdda0dSAndy Whitcroftmy @lines = (); 357c2fdda0dSAndy Whitcroftmy $vname; 3586c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 35921caa13cSAndy Whitcroft my $FILE; 3606c72ffaaSAndy Whitcroft if ($file) { 36121caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 3626c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 36321caa13cSAndy Whitcroft } elsif ($filename eq '-') { 36421caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 3656c72ffaaSAndy Whitcroft } else { 36621caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 3676c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 3686c72ffaaSAndy Whitcroft } 369c2fdda0dSAndy Whitcroft if ($filename eq '-') { 370c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 371c2fdda0dSAndy Whitcroft } else { 372c2fdda0dSAndy Whitcroft $vname = $filename; 373c2fdda0dSAndy Whitcroft } 37421caa13cSAndy Whitcroft while (<$FILE>) { 3750a920b5bSAndy Whitcroft chomp; 37600df344fSAndy Whitcroft push(@rawlines, $_); 3776c72ffaaSAndy Whitcroft } 37821caa13cSAndy Whitcroft close($FILE); 379c2fdda0dSAndy Whitcroft if (!process($filename)) { 3800a920b5bSAndy Whitcroft $exit = 1; 3810a920b5bSAndy Whitcroft } 38200df344fSAndy Whitcroft @rawlines = (); 38313214adfSAndy Whitcroft @lines = (); 3840a920b5bSAndy Whitcroft} 3850a920b5bSAndy Whitcroft 3860a920b5bSAndy Whitcroftexit($exit); 3870a920b5bSAndy Whitcroft 3880a920b5bSAndy Whitcroftsub top_of_kernel_tree { 3896c72ffaaSAndy Whitcroft my ($root) = @_; 3906c72ffaaSAndy Whitcroft 3916c72ffaaSAndy Whitcroft my @tree_check = ( 3926c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 3936c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 3946c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 3956c72ffaaSAndy Whitcroft ); 3966c72ffaaSAndy Whitcroft 3976c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 3986c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 3990a920b5bSAndy Whitcroft return 0; 4000a920b5bSAndy Whitcroft } 4016c72ffaaSAndy Whitcroft } 4026c72ffaaSAndy Whitcroft return 1; 4036c72ffaaSAndy Whitcroft} 4040a920b5bSAndy Whitcroft 40520112475SJoe Perchessub parse_email { 40620112475SJoe Perches my ($formatted_email) = @_; 40720112475SJoe Perches 40820112475SJoe Perches my $name = ""; 40920112475SJoe Perches my $address = ""; 41020112475SJoe Perches my $comment = ""; 41120112475SJoe Perches 41220112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 41320112475SJoe Perches $name = $1; 41420112475SJoe Perches $address = $2; 41520112475SJoe Perches $comment = $3 if defined $3; 41620112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 41720112475SJoe Perches $address = $1; 41820112475SJoe Perches $comment = $2 if defined $2; 41920112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 42020112475SJoe Perches $address = $1; 42120112475SJoe Perches $comment = $2 if defined $2; 42220112475SJoe Perches $formatted_email =~ s/$address.*$//; 42320112475SJoe Perches $name = $formatted_email; 42420112475SJoe Perches $name =~ s/^\s+|\s+$//g; 42520112475SJoe Perches $name =~ s/^\"|\"$//g; 42620112475SJoe Perches # If there's a name left after stripping spaces and 42720112475SJoe Perches # leading quotes, and the address doesn't have both 42820112475SJoe Perches # leading and trailing angle brackets, the address 42920112475SJoe Perches # is invalid. ie: 43020112475SJoe Perches # "joe smith [email protected]" bad 43120112475SJoe Perches # "joe smith <[email protected]" bad 43220112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 43320112475SJoe Perches $name = ""; 43420112475SJoe Perches $address = ""; 43520112475SJoe Perches $comment = ""; 43620112475SJoe Perches } 43720112475SJoe Perches } 43820112475SJoe Perches 43920112475SJoe Perches $name =~ s/^\s+|\s+$//g; 44020112475SJoe Perches $name =~ s/^\"|\"$//g; 44120112475SJoe Perches $address =~ s/^\s+|\s+$//g; 44220112475SJoe Perches $address =~ s/^\<|\>$//g; 44320112475SJoe Perches 44420112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 44520112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 44620112475SJoe Perches $name = "\"$name\""; 44720112475SJoe Perches } 44820112475SJoe Perches 44920112475SJoe Perches return ($name, $address, $comment); 45020112475SJoe Perches} 45120112475SJoe Perches 45220112475SJoe Perchessub format_email { 45320112475SJoe Perches my ($name, $address) = @_; 45420112475SJoe Perches 45520112475SJoe Perches my $formatted_email; 45620112475SJoe Perches 45720112475SJoe Perches $name =~ s/^\s+|\s+$//g; 45820112475SJoe Perches $name =~ s/^\"|\"$//g; 45920112475SJoe Perches $address =~ s/^\s+|\s+$//g; 46020112475SJoe Perches 46120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 46220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 46320112475SJoe Perches $name = "\"$name\""; 46420112475SJoe Perches } 46520112475SJoe Perches 46620112475SJoe Perches if ("$name" eq "") { 46720112475SJoe Perches $formatted_email = "$address"; 46820112475SJoe Perches } else { 46920112475SJoe Perches $formatted_email = "$name <$address>"; 47020112475SJoe Perches } 47120112475SJoe Perches 47220112475SJoe Perches return $formatted_email; 47320112475SJoe Perches} 47420112475SJoe Perches 475000d1cc1SJoe Perchessub which_conf { 476000d1cc1SJoe Perches my ($conf) = @_; 477000d1cc1SJoe Perches 478000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 479000d1cc1SJoe Perches if (-e "$path/$conf") { 480000d1cc1SJoe Perches return "$path/$conf"; 481000d1cc1SJoe Perches } 482000d1cc1SJoe Perches } 483000d1cc1SJoe Perches 484000d1cc1SJoe Perches return ""; 485000d1cc1SJoe Perches} 486000d1cc1SJoe Perches 4870a920b5bSAndy Whitcroftsub expand_tabs { 4880a920b5bSAndy Whitcroft my ($str) = @_; 4890a920b5bSAndy Whitcroft 4900a920b5bSAndy Whitcroft my $res = ''; 4910a920b5bSAndy Whitcroft my $n = 0; 4920a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 4930a920b5bSAndy Whitcroft if ($c eq "\t") { 4940a920b5bSAndy Whitcroft $res .= ' '; 4950a920b5bSAndy Whitcroft $n++; 4960a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 4970a920b5bSAndy Whitcroft $res .= ' '; 4980a920b5bSAndy Whitcroft } 4990a920b5bSAndy Whitcroft next; 5000a920b5bSAndy Whitcroft } 5010a920b5bSAndy Whitcroft $res .= $c; 5020a920b5bSAndy Whitcroft $n++; 5030a920b5bSAndy Whitcroft } 5040a920b5bSAndy Whitcroft 5050a920b5bSAndy Whitcroft return $res; 5060a920b5bSAndy Whitcroft} 5076c72ffaaSAndy Whitcroftsub copy_spacing { 508773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 5096c72ffaaSAndy Whitcroft return $res; 5106c72ffaaSAndy Whitcroft} 5110a920b5bSAndy Whitcroft 5124a0df2efSAndy Whitcroftsub line_stats { 5134a0df2efSAndy Whitcroft my ($line) = @_; 5144a0df2efSAndy Whitcroft 5154a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 5164a0df2efSAndy Whitcroft $line =~ s/^.//; 5174a0df2efSAndy Whitcroft $line = expand_tabs($line); 5184a0df2efSAndy Whitcroft 5194a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 5204a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 5214a0df2efSAndy Whitcroft 5224a0df2efSAndy Whitcroft return (length($line), length($white)); 5234a0df2efSAndy Whitcroft} 5244a0df2efSAndy Whitcroft 525773647a0SAndy Whitcroftmy $sanitise_quote = ''; 526773647a0SAndy Whitcroft 527773647a0SAndy Whitcroftsub sanitise_line_reset { 528773647a0SAndy Whitcroft my ($in_comment) = @_; 529773647a0SAndy Whitcroft 530773647a0SAndy Whitcroft if ($in_comment) { 531773647a0SAndy Whitcroft $sanitise_quote = '*/'; 532773647a0SAndy Whitcroft } else { 533773647a0SAndy Whitcroft $sanitise_quote = ''; 534773647a0SAndy Whitcroft } 535773647a0SAndy Whitcroft} 53600df344fSAndy Whitcroftsub sanitise_line { 53700df344fSAndy Whitcroft my ($line) = @_; 53800df344fSAndy Whitcroft 53900df344fSAndy Whitcroft my $res = ''; 54000df344fSAndy Whitcroft my $l = ''; 54100df344fSAndy Whitcroft 542c2fdda0dSAndy Whitcroft my $qlen = 0; 543773647a0SAndy Whitcroft my $off = 0; 544773647a0SAndy Whitcroft my $c; 54500df344fSAndy Whitcroft 546773647a0SAndy Whitcroft # Always copy over the diff marker. 547773647a0SAndy Whitcroft $res = substr($line, 0, 1); 548773647a0SAndy Whitcroft 549773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 550773647a0SAndy Whitcroft $c = substr($line, $off, 1); 551773647a0SAndy Whitcroft 552773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 553773647a0SAndy Whitcroft # and end, all to $;. 554773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 555773647a0SAndy Whitcroft $sanitise_quote = '*/'; 556773647a0SAndy Whitcroft 557773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 558773647a0SAndy Whitcroft $off++; 55900df344fSAndy Whitcroft next; 560773647a0SAndy Whitcroft } 56181bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 562773647a0SAndy Whitcroft $sanitise_quote = ''; 563773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 564773647a0SAndy Whitcroft $off++; 565773647a0SAndy Whitcroft next; 566773647a0SAndy Whitcroft } 567113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 568113f04a8SDaniel Walker $sanitise_quote = '//'; 569113f04a8SDaniel Walker 570113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 571113f04a8SDaniel Walker $off++; 572113f04a8SDaniel Walker next; 573113f04a8SDaniel Walker } 574773647a0SAndy Whitcroft 575773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 576773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 577773647a0SAndy Whitcroft $c eq "\\") { 578773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 579773647a0SAndy Whitcroft $off++; 580773647a0SAndy Whitcroft next; 581773647a0SAndy Whitcroft } 582773647a0SAndy Whitcroft # Regular quotes. 583773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 584773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 585773647a0SAndy Whitcroft $sanitise_quote = $c; 586773647a0SAndy Whitcroft 587773647a0SAndy Whitcroft substr($res, $off, 1, $c); 588773647a0SAndy Whitcroft next; 589773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 590773647a0SAndy Whitcroft $sanitise_quote = ''; 59100df344fSAndy Whitcroft } 59200df344fSAndy Whitcroft } 593773647a0SAndy Whitcroft 594fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 595773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 596773647a0SAndy Whitcroft substr($res, $off, 1, $;); 597113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 598113f04a8SDaniel Walker substr($res, $off, 1, $;); 599773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 600773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 60100df344fSAndy Whitcroft } else { 602773647a0SAndy Whitcroft substr($res, $off, 1, $c); 60300df344fSAndy Whitcroft } 604c2fdda0dSAndy Whitcroft } 605c2fdda0dSAndy Whitcroft 606113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 607113f04a8SDaniel Walker $sanitise_quote = ''; 608113f04a8SDaniel Walker } 609113f04a8SDaniel Walker 610c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 611c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 612c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 613c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 614c2fdda0dSAndy Whitcroft 615c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 616c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 617c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 618c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 619c2fdda0dSAndy Whitcroft } 620c2fdda0dSAndy Whitcroft 62100df344fSAndy Whitcroft return $res; 62200df344fSAndy Whitcroft} 62300df344fSAndy Whitcroft 6248905a67cSAndy Whitcroftsub ctx_statement_block { 6258905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 6268905a67cSAndy Whitcroft my $line = $linenr - 1; 6278905a67cSAndy Whitcroft my $blk = ''; 6288905a67cSAndy Whitcroft my $soff = $off; 6298905a67cSAndy Whitcroft my $coff = $off - 1; 630773647a0SAndy Whitcroft my $coff_set = 0; 6318905a67cSAndy Whitcroft 63213214adfSAndy Whitcroft my $loff = 0; 63313214adfSAndy Whitcroft 6348905a67cSAndy Whitcroft my $type = ''; 6358905a67cSAndy Whitcroft my $level = 0; 636a2750645SAndy Whitcroft my @stack = (); 637cf655043SAndy Whitcroft my $p; 6388905a67cSAndy Whitcroft my $c; 6398905a67cSAndy Whitcroft my $len = 0; 64013214adfSAndy Whitcroft 64113214adfSAndy Whitcroft my $remainder; 6428905a67cSAndy Whitcroft while (1) { 643a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 644a2750645SAndy Whitcroft 645773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 6468905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 6478905a67cSAndy Whitcroft # context. 6488905a67cSAndy Whitcroft if ($off >= $len) { 6498905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 650dea33496SAndy Whitcroft last if (!defined $lines[$line]); 651c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 6528905a67cSAndy Whitcroft $remain--; 65313214adfSAndy Whitcroft $loff = $len; 654c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 6558905a67cSAndy Whitcroft $len = length($blk); 6568905a67cSAndy Whitcroft $line++; 6578905a67cSAndy Whitcroft last; 6588905a67cSAndy Whitcroft } 6598905a67cSAndy Whitcroft # Bail if there is no further context. 6608905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 66113214adfSAndy Whitcroft if ($off >= $len) { 6628905a67cSAndy Whitcroft last; 6638905a67cSAndy Whitcroft } 664f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 665f74bd194SAndy Whitcroft $level++; 666f74bd194SAndy Whitcroft $type = '#'; 667f74bd194SAndy Whitcroft } 6688905a67cSAndy Whitcroft } 669cf655043SAndy Whitcroft $p = $c; 6708905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 67113214adfSAndy Whitcroft $remainder = substr($blk, $off); 6728905a67cSAndy Whitcroft 673773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 6744635f4fbSAndy Whitcroft 6754635f4fbSAndy Whitcroft # Handle nested #if/#else. 6764635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 6774635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 6784635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 6794635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 6804635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 6814635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 6824635f4fbSAndy Whitcroft } 6834635f4fbSAndy Whitcroft 6848905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 6858905a67cSAndy Whitcroft # outermost level. 6868905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 6878905a67cSAndy Whitcroft last; 6888905a67cSAndy Whitcroft } 6898905a67cSAndy Whitcroft 69013214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 691773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 692773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 693773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 694773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 695773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 696773647a0SAndy Whitcroft $coff_set = 1; 697773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 698773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 69913214adfSAndy Whitcroft } 70013214adfSAndy Whitcroft 7018905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 7028905a67cSAndy Whitcroft $level++; 7038905a67cSAndy Whitcroft $type = '('; 7048905a67cSAndy Whitcroft } 7058905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 7068905a67cSAndy Whitcroft $level--; 7078905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 7088905a67cSAndy Whitcroft 7098905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 7108905a67cSAndy Whitcroft $coff = $off; 711773647a0SAndy Whitcroft $coff_set = 1; 712773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 7138905a67cSAndy Whitcroft } 7148905a67cSAndy Whitcroft } 7158905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 7168905a67cSAndy Whitcroft $level++; 7178905a67cSAndy Whitcroft $type = '{'; 7188905a67cSAndy Whitcroft } 7198905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 7208905a67cSAndy Whitcroft $level--; 7218905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 7228905a67cSAndy Whitcroft 7238905a67cSAndy Whitcroft if ($level == 0) { 724b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 725b998e001SPatrick Pannuto $off++; 726b998e001SPatrick Pannuto } 7278905a67cSAndy Whitcroft last; 7288905a67cSAndy Whitcroft } 7298905a67cSAndy Whitcroft } 730f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 731f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 732f74bd194SAndy Whitcroft $level--; 733f74bd194SAndy Whitcroft $type = ''; 734f74bd194SAndy Whitcroft $off++; 735f74bd194SAndy Whitcroft last; 736f74bd194SAndy 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); 1019addcdceaSAndy 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__| 121189a88353SAndy Whitcroft do| 121289a88353SAndy Whitcroft \#| 121389a88353SAndy Whitcroft \#\#| 12149a974fdbSAndy Whitcroft )(?:\s|$)| 12150776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 12169a974fdbSAndy Whitcroft )}x; 12179a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 12189a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1219c45dcabdSAndy Whitcroft # Check for modifiers. 1220c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1221c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1222c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1223c45dcabdSAndy Whitcroft 1224c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1225c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1226d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 12279a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1228d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1229d2506586SAndy Whitcroft push(@modifierList, $modifier); 1230d2506586SAndy Whitcroft } 12319a974fdbSAndy Whitcroft } 1232c45dcabdSAndy Whitcroft 1233c45dcabdSAndy Whitcroft } else { 123413214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 12358905a67cSAndy Whitcroft push(@typeList, $possible); 1236c45dcabdSAndy Whitcroft } 12378905a67cSAndy Whitcroft build_types(); 12380776e594SAndy Whitcroft } else { 12390776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 12408905a67cSAndy Whitcroft } 12418905a67cSAndy Whitcroft} 12428905a67cSAndy Whitcroft 12436c72ffaaSAndy Whitcroftmy $prefix = ''; 12446c72ffaaSAndy Whitcroft 1245000d1cc1SJoe Perchessub show_type { 1246000d1cc1SJoe Perches return !defined $ignore_type{$_[0]}; 1247000d1cc1SJoe Perches} 1248000d1cc1SJoe Perches 1249f0a594c1SAndy Whitcroftsub report { 1250000d1cc1SJoe Perches if (!show_type($_[1]) || 1251000d1cc1SJoe Perches (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { 1252773647a0SAndy Whitcroft return 0; 1253773647a0SAndy Whitcroft } 1254000d1cc1SJoe Perches my $line; 1255000d1cc1SJoe Perches if ($show_types) { 1256000d1cc1SJoe Perches $line = "$prefix$_[0]:$_[1]: $_[2]\n"; 1257000d1cc1SJoe Perches } else { 1258000d1cc1SJoe Perches $line = "$prefix$_[0]: $_[2]\n"; 1259000d1cc1SJoe Perches } 12608905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 12618905a67cSAndy Whitcroft 126213214adfSAndy Whitcroft push(our @report, $line); 1263773647a0SAndy Whitcroft 1264773647a0SAndy Whitcroft return 1; 1265f0a594c1SAndy Whitcroft} 1266f0a594c1SAndy Whitcroftsub report_dump { 126713214adfSAndy Whitcroft our @report; 1268f0a594c1SAndy Whitcroft} 1269000d1cc1SJoe Perches 1270de7d4f0eSAndy Whitcroftsub ERROR { 1271000d1cc1SJoe Perches if (report("ERROR", $_[0], $_[1])) { 1272de7d4f0eSAndy Whitcroft our $clean = 0; 12736c72ffaaSAndy Whitcroft our $cnt_error++; 1274de7d4f0eSAndy Whitcroft } 1275773647a0SAndy Whitcroft} 1276de7d4f0eSAndy Whitcroftsub WARN { 1277000d1cc1SJoe Perches if (report("WARNING", $_[0], $_[1])) { 1278de7d4f0eSAndy Whitcroft our $clean = 0; 12796c72ffaaSAndy Whitcroft our $cnt_warn++; 1280de7d4f0eSAndy Whitcroft } 1281773647a0SAndy Whitcroft} 1282de7d4f0eSAndy Whitcroftsub CHK { 1283000d1cc1SJoe Perches if ($check && report("CHECK", $_[0], $_[1])) { 1284de7d4f0eSAndy Whitcroft our $clean = 0; 12856c72ffaaSAndy Whitcroft our $cnt_chk++; 12866c72ffaaSAndy Whitcroft } 1287de7d4f0eSAndy Whitcroft} 1288de7d4f0eSAndy Whitcroft 12896ecd9674SAndy Whitcroftsub check_absolute_file { 12906ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 12916ecd9674SAndy Whitcroft my $file = $absolute; 12926ecd9674SAndy Whitcroft 12936ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 12946ecd9674SAndy Whitcroft 12956ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 12966ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 12976ecd9674SAndy Whitcroft if (-f "$root/$file") { 12986ecd9674SAndy Whitcroft ##print "file<$file>\n"; 12996ecd9674SAndy Whitcroft last; 13006ecd9674SAndy Whitcroft } 13016ecd9674SAndy Whitcroft } 13026ecd9674SAndy Whitcroft if (! -f _) { 13036ecd9674SAndy Whitcroft return 0; 13046ecd9674SAndy Whitcroft } 13056ecd9674SAndy Whitcroft 13066ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 13076ecd9674SAndy Whitcroft my $prefix = $absolute; 13086ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 13096ecd9674SAndy Whitcroft 13106ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 13116ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1312000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1313000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 13146ecd9674SAndy Whitcroft } 13156ecd9674SAndy Whitcroft} 13166ecd9674SAndy Whitcroft 1317d1fe9c09SJoe Perchessub pos_last_openparen { 1318d1fe9c09SJoe Perches my ($line) = @_; 1319d1fe9c09SJoe Perches 1320d1fe9c09SJoe Perches my $pos = 0; 1321d1fe9c09SJoe Perches 1322d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1323d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1324d1fe9c09SJoe Perches 1325d1fe9c09SJoe Perches my $last_openparen = 0; 1326d1fe9c09SJoe Perches 1327d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1328d1fe9c09SJoe Perches return -1; 1329d1fe9c09SJoe Perches } 1330d1fe9c09SJoe Perches 1331d1fe9c09SJoe Perches my $len = length($line); 1332d1fe9c09SJoe Perches 1333d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1334d1fe9c09SJoe Perches my $string = substr($line, $pos); 1335d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1336d1fe9c09SJoe Perches $pos += length($1) - 1; 1337d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1338d1fe9c09SJoe Perches $last_openparen = $pos; 1339d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1340d1fe9c09SJoe Perches last; 1341d1fe9c09SJoe Perches } 1342d1fe9c09SJoe Perches } 1343d1fe9c09SJoe Perches 1344d1fe9c09SJoe Perches return $last_openparen + 1; 1345d1fe9c09SJoe Perches} 1346d1fe9c09SJoe Perches 13470a920b5bSAndy Whitcroftsub process { 13480a920b5bSAndy Whitcroft my $filename = shift; 13490a920b5bSAndy Whitcroft 13500a920b5bSAndy Whitcroft my $linenr=0; 13510a920b5bSAndy Whitcroft my $prevline=""; 1352c2fdda0dSAndy Whitcroft my $prevrawline=""; 13530a920b5bSAndy Whitcroft my $stashline=""; 1354c2fdda0dSAndy Whitcroft my $stashrawline=""; 13550a920b5bSAndy Whitcroft 13564a0df2efSAndy Whitcroft my $length; 13570a920b5bSAndy Whitcroft my $indent; 13580a920b5bSAndy Whitcroft my $previndent=0; 13590a920b5bSAndy Whitcroft my $stashindent=0; 13600a920b5bSAndy Whitcroft 1361de7d4f0eSAndy Whitcroft our $clean = 1; 13620a920b5bSAndy Whitcroft my $signoff = 0; 13630a920b5bSAndy Whitcroft my $is_patch = 0; 13640a920b5bSAndy Whitcroft 136515662b3eSJoe Perches my $in_header_lines = 1; 136615662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 136715662b3eSJoe Perches 1368fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1369fa64205dSPasi Savanainen 137013214adfSAndy Whitcroft our @report = (); 13716c72ffaaSAndy Whitcroft our $cnt_lines = 0; 13726c72ffaaSAndy Whitcroft our $cnt_error = 0; 13736c72ffaaSAndy Whitcroft our $cnt_warn = 0; 13746c72ffaaSAndy Whitcroft our $cnt_chk = 0; 13756c72ffaaSAndy Whitcroft 13760a920b5bSAndy Whitcroft # Trace the real file/line as we go. 13770a920b5bSAndy Whitcroft my $realfile = ''; 13780a920b5bSAndy Whitcroft my $realline = 0; 13790a920b5bSAndy Whitcroft my $realcnt = 0; 13800a920b5bSAndy Whitcroft my $here = ''; 13810a920b5bSAndy Whitcroft my $in_comment = 0; 1382c2fdda0dSAndy Whitcroft my $comment_edge = 0; 13830a920b5bSAndy Whitcroft my $first_line = 0; 13841e855726SWolfram Sang my $p1_prefix = ''; 13850a920b5bSAndy Whitcroft 138613214adfSAndy Whitcroft my $prev_values = 'E'; 138713214adfSAndy Whitcroft 138813214adfSAndy Whitcroft # suppression flags 1389773647a0SAndy Whitcroft my %suppress_ifbraces; 1390170d3a22SAndy Whitcroft my %suppress_whiletrailers; 13912b474a1aSAndy Whitcroft my %suppress_export; 13923e469cdcSAndy Whitcroft my $suppress_statement = 0; 1393653d4876SAndy Whitcroft 1394c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1395de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1396c2fdda0dSAndy Whitcroft # 1397de7d4f0eSAndy Whitcroft my @setup_docs = (); 1398de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1399773647a0SAndy Whitcroft 1400773647a0SAndy Whitcroft sanitise_line_reset(); 1401c2fdda0dSAndy Whitcroft my $line; 1402c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1403773647a0SAndy Whitcroft $linenr++; 1404773647a0SAndy Whitcroft $line = $rawline; 1405c2fdda0dSAndy Whitcroft 1406773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1407de7d4f0eSAndy Whitcroft $setup_docs = 0; 1408de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1409de7d4f0eSAndy Whitcroft $setup_docs = 1; 1410de7d4f0eSAndy Whitcroft } 1411773647a0SAndy Whitcroft #next; 1412de7d4f0eSAndy Whitcroft } 1413773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1414773647a0SAndy Whitcroft $realline=$1-1; 1415773647a0SAndy Whitcroft if (defined $2) { 1416773647a0SAndy Whitcroft $realcnt=$3+1; 1417773647a0SAndy Whitcroft } else { 1418773647a0SAndy Whitcroft $realcnt=1+1; 1419773647a0SAndy Whitcroft } 1420c45dcabdSAndy Whitcroft $in_comment = 0; 1421773647a0SAndy Whitcroft 1422773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1423773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1424773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1425773647a0SAndy Whitcroft # at context start. 1426773647a0SAndy Whitcroft my $edge; 142701fa9147SAndy Whitcroft my $cnt = $realcnt; 142801fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 142901fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 143001fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 143101fa9147SAndy Whitcroft $cnt--; 143201fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1433721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1434fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1435fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1436fae17daeSAndy Whitcroft ($edge) = $1; 1437fae17daeSAndy Whitcroft last; 1438fae17daeSAndy Whitcroft } 1439773647a0SAndy Whitcroft } 1440773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1441773647a0SAndy Whitcroft $in_comment = 1; 1442773647a0SAndy Whitcroft } 1443773647a0SAndy Whitcroft 1444773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1445773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1446773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1447773647a0SAndy Whitcroft if (!defined $edge && 144883242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1449773647a0SAndy Whitcroft { 1450773647a0SAndy Whitcroft $in_comment = 1; 1451773647a0SAndy Whitcroft } 1452773647a0SAndy Whitcroft 1453773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1454773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1455773647a0SAndy Whitcroft 1456171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1457773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1458171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1459773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1460773647a0SAndy Whitcroft } 1461773647a0SAndy Whitcroft push(@lines, $line); 1462773647a0SAndy Whitcroft 1463773647a0SAndy Whitcroft if ($realcnt > 1) { 1464773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1465773647a0SAndy Whitcroft } else { 1466773647a0SAndy Whitcroft $realcnt = 0; 1467773647a0SAndy Whitcroft } 1468773647a0SAndy Whitcroft 1469773647a0SAndy Whitcroft #print "==>$rawline\n"; 1470773647a0SAndy Whitcroft #print "-->$line\n"; 1471de7d4f0eSAndy Whitcroft 1472de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1473de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1474de7d4f0eSAndy Whitcroft } 1475de7d4f0eSAndy Whitcroft } 1476de7d4f0eSAndy Whitcroft 14776c72ffaaSAndy Whitcroft $prefix = ''; 14786c72ffaaSAndy Whitcroft 1479773647a0SAndy Whitcroft $realcnt = 0; 1480773647a0SAndy Whitcroft $linenr = 0; 14810a920b5bSAndy Whitcroft foreach my $line (@lines) { 14820a920b5bSAndy Whitcroft $linenr++; 14830a920b5bSAndy Whitcroft 1484c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 14856c72ffaaSAndy Whitcroft 14860a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 14876c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 14880a920b5bSAndy Whitcroft $is_patch = 1; 14894a0df2efSAndy Whitcroft $first_line = $linenr + 1; 14900a920b5bSAndy Whitcroft $realline=$1-1; 14910a920b5bSAndy Whitcroft if (defined $2) { 14920a920b5bSAndy Whitcroft $realcnt=$3+1; 14930a920b5bSAndy Whitcroft } else { 14940a920b5bSAndy Whitcroft $realcnt=1+1; 14950a920b5bSAndy Whitcroft } 1496c2fdda0dSAndy Whitcroft annotate_reset(); 149713214adfSAndy Whitcroft $prev_values = 'E'; 149813214adfSAndy Whitcroft 1499773647a0SAndy Whitcroft %suppress_ifbraces = (); 1500170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 15012b474a1aSAndy Whitcroft %suppress_export = (); 15023e469cdcSAndy Whitcroft $suppress_statement = 0; 15030a920b5bSAndy Whitcroft next; 15040a920b5bSAndy Whitcroft 15054a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 15064a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 15074a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1508773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 15090a920b5bSAndy Whitcroft $realline++; 1510d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 15110a920b5bSAndy Whitcroft 15124a0df2efSAndy Whitcroft # Measure the line length and indent. 1513c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 15140a920b5bSAndy Whitcroft 15150a920b5bSAndy Whitcroft # Track the previous line. 15160a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 15170a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1518c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1519c2fdda0dSAndy Whitcroft 1520773647a0SAndy Whitcroft #warn "line<$line>\n"; 15216c72ffaaSAndy Whitcroft 1522d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1523d8aaf121SAndy Whitcroft $realcnt--; 15240a920b5bSAndy Whitcroft } 15250a920b5bSAndy Whitcroft 1526cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1527cc77cdcaSAndy Whitcroft 15280a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1529773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1530773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1531773647a0SAndy Whitcroft 15326c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 15336c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1534773647a0SAndy Whitcroft 1535773647a0SAndy Whitcroft # extract the filename as it passes 15363bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 15373bf9a009SRabin Vincent $realfile = $1; 15383bf9a009SRabin Vincent $realfile =~ s@^([^/]*)/@@; 1539270c49a0SJoe Perches $in_commit_log = 0; 15403bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1541773647a0SAndy Whitcroft $realfile = $1; 15421e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 1543270c49a0SJoe Perches $in_commit_log = 0; 15441e855726SWolfram Sang 15451e855726SWolfram Sang $p1_prefix = $1; 1546e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1547e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 1548000d1cc1SJoe Perches WARN("PATCH_PREFIX", 1549000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 15501e855726SWolfram Sang } 1551773647a0SAndy Whitcroft 1552c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1553000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 1554000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1555773647a0SAndy Whitcroft } 1556773647a0SAndy Whitcroft next; 1557773647a0SAndy Whitcroft } 1558773647a0SAndy Whitcroft 1559389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 15600a920b5bSAndy Whitcroft 1561c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1562c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1563c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 15640a920b5bSAndy Whitcroft 15656c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 15666c72ffaaSAndy Whitcroft 15673bf9a009SRabin Vincent# Check for incorrect file permissions 15683bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 15693bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 15703bf9a009SRabin Vincent if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) { 1571000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 1572000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 15733bf9a009SRabin Vincent } 15743bf9a009SRabin Vincent } 15753bf9a009SRabin Vincent 157620112475SJoe Perches# Check the patch for a signoff: 1577d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 15784a0df2efSAndy Whitcroft $signoff++; 157915662b3eSJoe Perches $in_commit_log = 0; 15800a920b5bSAndy Whitcroft } 158120112475SJoe Perches 158220112475SJoe Perches# Check signature styles 1583270c49a0SJoe Perches if (!$in_header_lines && 1584ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 158520112475SJoe Perches my $space_before = $1; 158620112475SJoe Perches my $sign_off = $2; 158720112475SJoe Perches my $space_after = $3; 158820112475SJoe Perches my $email = $4; 158920112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 159020112475SJoe Perches 1591ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 1592ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 1593ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 1594ce0338dfSJoe Perches } 159520112475SJoe Perches if (defined $space_before && $space_before ne "") { 1596000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1597000d1cc1SJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); 159820112475SJoe Perches } 159920112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 1600000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1601000d1cc1SJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); 160220112475SJoe Perches } 160320112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 1604000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1605000d1cc1SJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr); 160620112475SJoe Perches } 160720112475SJoe Perches 160820112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 160920112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 161020112475SJoe Perches if ($suggested_email eq "") { 1611000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 1612000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 161320112475SJoe Perches } else { 161420112475SJoe Perches my $dequoted = $suggested_email; 161520112475SJoe Perches $dequoted =~ s/^"//; 161620112475SJoe Perches $dequoted =~ s/" </ </; 161720112475SJoe Perches # Don't force email to have quotes 161820112475SJoe Perches # Allow just an angle bracketed address 161920112475SJoe Perches if ("$dequoted$comment" ne $email && 162020112475SJoe Perches "<$email_address>$comment" ne $email && 162120112475SJoe Perches "$suggested_email$comment" ne $email) { 1622000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1623000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 162420112475SJoe Perches } 16250a920b5bSAndy Whitcroft } 16260a920b5bSAndy Whitcroft } 16270a920b5bSAndy Whitcroft 162800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 16298905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1630000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 1631000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 16326c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1633de7d4f0eSAndy Whitcroft } 1634de7d4f0eSAndy Whitcroft 16356ecd9674SAndy Whitcroft# Check for absolute kernel paths. 16366ecd9674SAndy Whitcroft if ($tree) { 16376ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 16386ecd9674SAndy Whitcroft my $file = $1; 16396ecd9674SAndy Whitcroft 16406ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 16416ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 16426ecd9674SAndy Whitcroft # 16436ecd9674SAndy Whitcroft } else { 16446ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 16456ecd9674SAndy Whitcroft } 16466ecd9674SAndy Whitcroft } 16476ecd9674SAndy Whitcroft } 16486ecd9674SAndy Whitcroft 1649de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1650de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1651171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1652171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1653171ae1a4SAndy Whitcroft 1654171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1655171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1656171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1657171ae1a4SAndy Whitcroft 165834d99219SJoe Perches CHK("INVALID_UTF8", 1659000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 166000df344fSAndy Whitcroft } 16610a920b5bSAndy Whitcroft 166215662b3eSJoe Perches# Check if it's the start of a commit log 166315662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 166415662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 1665270c49a0SJoe Perches $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { 166615662b3eSJoe Perches $in_header_lines = 0; 166715662b3eSJoe Perches $in_commit_log = 1; 166815662b3eSJoe Perches } 166915662b3eSJoe Perches 1670fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 1671fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 1672fa64205dSPasi Savanainen if ($in_header_lines && 1673fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 1674fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 1675fa64205dSPasi Savanainen $non_utf8_charset = 1; 1676fa64205dSPasi Savanainen } 1677fa64205dSPasi Savanainen 1678fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 167915662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 1680fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 168115662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 168215662b3eSJoe Perches } 168315662b3eSJoe Perches 168430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 168530670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 168600df344fSAndy Whitcroft 16870a920b5bSAndy Whitcroft#trailing whitespace 16889c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1689c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1690000d1cc1SJoe Perches ERROR("DOS_LINE_ENDINGS", 1691000d1cc1SJoe Perches "DOS line endings\n" . $herevet); 16929c0ca6f9SAndy Whitcroft 1693c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1694c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1695000d1cc1SJoe Perches ERROR("TRAILING_WHITESPACE", 1696000d1cc1SJoe Perches "trailing whitespace\n" . $herevet); 1697d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 16980a920b5bSAndy Whitcroft } 16995368df20SAndy Whitcroft 17003354957aSAndi Kleen# check for Kconfig help text having a real description 17019fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 17029fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 17033354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 1704a1385803SAndy Whitcroft $line =~ /.\s*config\s+/) { 17053354957aSAndi Kleen my $length = 0; 17069fe287d7SAndy Whitcroft my $cnt = $realcnt; 17079fe287d7SAndy Whitcroft my $ln = $linenr + 1; 17089fe287d7SAndy Whitcroft my $f; 1709a1385803SAndy Whitcroft my $is_start = 0; 17109fe287d7SAndy Whitcroft my $is_end = 0; 1711a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 17129fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 17139fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 17149fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 17159fe287d7SAndy Whitcroft 17169fe287d7SAndy Whitcroft next if ($f =~ /^-/); 1717a1385803SAndy Whitcroft 1718a1385803SAndy Whitcroft if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { 1719a1385803SAndy Whitcroft $is_start = 1; 1720a1385803SAndy Whitcroft } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { 1721a1385803SAndy Whitcroft $length = -1; 1722a1385803SAndy Whitcroft } 1723a1385803SAndy Whitcroft 17249fe287d7SAndy Whitcroft $f =~ s/^.//; 17253354957aSAndi Kleen $f =~ s/#.*//; 17263354957aSAndi Kleen $f =~ s/^\s+//; 17273354957aSAndi Kleen next if ($f =~ /^$/); 17289fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 17299fe287d7SAndy Whitcroft $is_end = 1; 17309fe287d7SAndy Whitcroft last; 17319fe287d7SAndy Whitcroft } 17323354957aSAndi Kleen $length++; 17333354957aSAndi Kleen } 1734000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 1735a1385803SAndy Whitcroft "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 1736a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 17373354957aSAndi Kleen } 17383354957aSAndi Kleen 17391ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 17401ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 17411ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 17421ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 17431ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 17441ba8dfd1SKees Cook } 17451ba8dfd1SKees Cook 1746c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 1747c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 1748c68e5878SArnaud Lacombe my $flag = $1; 1749c68e5878SArnaud Lacombe my $replacement = { 1750c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 1751c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 1752c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 1753c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 1754c68e5878SArnaud Lacombe }; 1755c68e5878SArnaud Lacombe 1756c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 1757c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 1758c68e5878SArnaud Lacombe } 1759c68e5878SArnaud Lacombe 17605368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 17615368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 17625368df20SAndy Whitcroft 17630a920b5bSAndy Whitcroft#80 column limit 1764c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1765f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 17660fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 17678bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1768f4c014c0SAndy Whitcroft $length > 80) 1769c45dcabdSAndy Whitcroft { 1770000d1cc1SJoe Perches WARN("LONG_LINE", 1771000d1cc1SJoe Perches "line over 80 characters\n" . $herecurr); 17720a920b5bSAndy Whitcroft } 17730a920b5bSAndy Whitcroft 1774ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability 1775ca56dc09SJosh Triplett# to grep for the string. Limited to strings used as parameters (those 1776ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false 1777ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per 1778ca56dc09SJosh Triplett# line of the string. Make an exception when the previous string ends in a 1779ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline 1780ca56dc09SJosh Triplett# assembly to indent the instruction on the following line). 1781ca56dc09SJosh Triplett if ($line =~ /^\+\s*"/ && 1782ca56dc09SJosh Triplett $prevline =~ /"\s*$/ && 1783ca56dc09SJosh Triplett $prevline =~ /\(/ && 1784ca56dc09SJosh Triplett $prevrawline !~ /\\n(?:\\t)*"\s*$/) { 1785ca56dc09SJosh Triplett WARN("SPLIT_STRING", 1786ca56dc09SJosh Triplett "quoted string split across lines\n" . $hereprev); 1787ca56dc09SJosh Triplett } 1788ca56dc09SJosh Triplett 17895e79d96eSJoe Perches# check for spaces before a quoted newline 17905e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 1791000d1cc1SJoe Perches WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 1792000d1cc1SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr); 17935e79d96eSJoe Perches } 17945e79d96eSJoe Perches 17958905a67cSAndy Whitcroft# check for adding lines without a newline. 17968905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 1797000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 1798000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 17998905a67cSAndy Whitcroft } 18008905a67cSAndy Whitcroft 180142e41c54SMike Frysinger# Blackfin: use hi/lo macros 180242e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 180342e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 180442e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1805000d1cc1SJoe Perches ERROR("LO_MACRO", 1806000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 180742e41c54SMike Frysinger } 180842e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 180942e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1810000d1cc1SJoe Perches ERROR("HI_MACRO", 1811000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 181242e41c54SMike Frysinger } 181342e41c54SMike Frysinger } 181442e41c54SMike Frysinger 1815b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 1816b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 18170a920b5bSAndy Whitcroft 18180a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 18190a920b5bSAndy Whitcroft# more than 8 must use tabs. 1820c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1821c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1822c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1823000d1cc1SJoe Perches ERROR("CODE_INDENT", 1824000d1cc1SJoe Perches "code indent should use tabs where possible\n" . $herevet); 1825d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 18260a920b5bSAndy Whitcroft } 18270a920b5bSAndy Whitcroft 182808e44365SAlberto Panizzo# check for space before tabs. 182908e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 183008e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1831000d1cc1SJoe Perches WARN("SPACE_BEFORE_TAB", 1832000d1cc1SJoe Perches "please, no space before tabs\n" . $herevet); 183308e44365SAlberto Panizzo } 183408e44365SAlberto Panizzo 1835d1fe9c09SJoe Perches# check for && or || at the start of a line 1836d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 1837d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 1838d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 1839d1fe9c09SJoe Perches } 1840d1fe9c09SJoe Perches 1841d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 1842d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 1843d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { 1844d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 1845d1fe9c09SJoe Perches my $oldindent = $1; 1846d1fe9c09SJoe Perches my $rest = $2; 1847d1fe9c09SJoe Perches 1848d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 1849d1fe9c09SJoe Perches if ($pos >= 0) { 1850b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 1851b34a26f3SJoe Perches my $newindent = $2; 1852d1fe9c09SJoe Perches 1853d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 1854d1fe9c09SJoe Perches "\t" x ($pos / 8) . 1855d1fe9c09SJoe Perches " " x ($pos % 8); 1856d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 1857d1fe9c09SJoe Perches 1858d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 1859d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 1860d1fe9c09SJoe Perches CHK("PARENTHESIS_ALIGNMENT", 1861d1fe9c09SJoe Perches "Alignment should match open parenthesis\n" . $hereprev); 1862d1fe9c09SJoe Perches } 1863d1fe9c09SJoe Perches } 1864d1fe9c09SJoe Perches } 1865d1fe9c09SJoe Perches 1866aad4f614SJoe Perches if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) { 1867aad4f614SJoe Perches CHK("SPACING", 1868aad4f614SJoe Perches "No space is necessary after a cast\n" . $hereprev); 1869aad4f614SJoe Perches } 1870aad4f614SJoe Perches 187105880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 187205880600SJoe Perches $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 187305880600SJoe Perches $prevrawline =~ /^\+[ \t]*$/) { 187405880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 187505880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 187605880600SJoe Perches } 187705880600SJoe Perches 187805880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 1879c24f9f19SJoe Perches $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 1880c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 1881c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 1882c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 188305880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 188405880600SJoe Perches "networking block comments put the trailing */ on a separate line\n" . $herecurr); 188505880600SJoe Perches } 188605880600SJoe Perches 18875f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 18886b4c5bebSAndy Whitcroft# Exceptions: 18896b4c5bebSAndy Whitcroft# 1) within comments 18906b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 18916b4c5bebSAndy Whitcroft# 3) hanging labels 18926b4c5bebSAndy Whitcroft if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { 18935f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1894000d1cc1SJoe Perches WARN("LEADING_SPACE", 1895000d1cc1SJoe Perches "please, no spaces at the start of a line\n" . $herevet); 18965f7ddae6SRaffaele Recalcati } 18975f7ddae6SRaffaele Recalcati 1898b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 1899b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 1900b9ea10d6SAndy Whitcroft 19011ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 19021ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 19031ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 19041ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 19051ba8dfd1SKees Cook } 19061ba8dfd1SKees Cook 1907c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1908cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1909000d1cc1SJoe Perches WARN("CVS_KEYWORD", 1910000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1911c2fdda0dSAndy Whitcroft } 191222f2a2efSAndy Whitcroft 191342e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 191442e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 191542e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1916000d1cc1SJoe Perches ERROR("CSYNC", 1917000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 191842e41c54SMike Frysinger } 191942e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 192042e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1921000d1cc1SJoe Perches ERROR("SSYNC", 1922000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 192342e41c54SMike Frysinger } 192442e41c54SMike Frysinger 19259c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 19262b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 19272b474a1aSAndy Whitcroft $realline_next); 19283e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 19293e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 19303e469cdcSAndy Whitcroft $realcnt && $line =~ /.\s*\S/) { 1931170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1932f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 1933171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1934171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1935171ae1a4SAndy Whitcroft 19363e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 19373e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 19383e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 19393e469cdcSAndy Whitcroft # until we hit end of it. 19403e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 19413e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 19423e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 19433e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 19443e469cdcSAndy Whitcroft } 1945f74bd194SAndy Whitcroft 19462b474a1aSAndy Whitcroft # Find the real next line. 19472b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 19482b474a1aSAndy Whitcroft if (defined $realline_next && 19492b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 19502b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 19512b474a1aSAndy Whitcroft $realline_next++; 19522b474a1aSAndy Whitcroft } 19532b474a1aSAndy Whitcroft 1954171ae1a4SAndy Whitcroft my $s = $stat; 1955171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1956cf655043SAndy Whitcroft 1957c2fdda0dSAndy Whitcroft # Ignore goto labels. 1958171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1959c2fdda0dSAndy Whitcroft 1960c2fdda0dSAndy Whitcroft # Ignore functions being called 1961171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1962c2fdda0dSAndy Whitcroft 1963463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 1964463f2864SAndy Whitcroft 1965c45dcabdSAndy Whitcroft # declarations always start with types 1966d2506586SAndy 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) { 1967c45dcabdSAndy Whitcroft my $type = $1; 1968c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1969c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1970c45dcabdSAndy Whitcroft 19716c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1972a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1973c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1974c2fdda0dSAndy Whitcroft } 19758905a67cSAndy Whitcroft 19766c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 197765863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1978c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 19799c0ca6f9SAndy Whitcroft } 19808905a67cSAndy Whitcroft 19818905a67cSAndy Whitcroft # Check for any sort of function declaration. 19828905a67cSAndy Whitcroft # int foo(something bar, other baz); 19838905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1984171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 19858905a67cSAndy Whitcroft my ($name_len) = length($1); 19868905a67cSAndy Whitcroft 1987cf655043SAndy Whitcroft my $ctx = $s; 1988773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 19898905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1990cf655043SAndy Whitcroft 19918905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1992c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 19938905a67cSAndy Whitcroft 1994c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 19958905a67cSAndy Whitcroft } 19968905a67cSAndy Whitcroft } 19978905a67cSAndy Whitcroft } 19988905a67cSAndy Whitcroft 19999c0ca6f9SAndy Whitcroft } 20009c0ca6f9SAndy Whitcroft 200100df344fSAndy Whitcroft# 200200df344fSAndy Whitcroft# Checks which may be anchored in the context. 200300df344fSAndy Whitcroft# 200400df344fSAndy Whitcroft 200500df344fSAndy Whitcroft# Check for switch () and associated case and default 200600df344fSAndy Whitcroft# statements should be at the same indent. 200700df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 200800df344fSAndy Whitcroft my $err = ''; 200900df344fSAndy Whitcroft my $sep = ''; 201000df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 201100df344fSAndy Whitcroft shift(@ctx); 201200df344fSAndy Whitcroft for my $ctx (@ctx) { 201300df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 201400df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 201500df344fSAndy Whitcroft $indent != $cindent) { 201600df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 201700df344fSAndy Whitcroft $sep = ''; 201800df344fSAndy Whitcroft } else { 201900df344fSAndy Whitcroft $sep = "[...]\n"; 202000df344fSAndy Whitcroft } 202100df344fSAndy Whitcroft } 202200df344fSAndy Whitcroft if ($err ne '') { 2023000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2024000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2025de7d4f0eSAndy Whitcroft } 2026de7d4f0eSAndy Whitcroft } 2027de7d4f0eSAndy Whitcroft 2028de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2029de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 2030c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2031773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2032773647a0SAndy Whitcroft 20339c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 20348eef05ddSJoe Perches 20358eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 20368eef05ddSJoe Perches WARN("DEEP_INDENTATION", 20378eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 20388eef05ddSJoe Perches } 20398eef05ddSJoe Perches 2040de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2041de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 2042de7d4f0eSAndy Whitcroft 2043548596d5SAndy Whitcroft my $ctx_ln = $linenr; 2044548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 2045de7d4f0eSAndy Whitcroft 2046548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2047548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 2048548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 2049548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2050548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2051773647a0SAndy Whitcroft $ctx_ln++; 2052773647a0SAndy Whitcroft } 2053548596d5SAndy Whitcroft 205453210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 205553210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2056773647a0SAndy Whitcroft 2057773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 2058000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2059000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 206001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 206100df344fSAndy Whitcroft } 2062773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2063773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 2064773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 2065773647a0SAndy Whitcroft { 20669c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 20679c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 2068000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 2069000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 207001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 20719c0ca6f9SAndy Whitcroft } 20729c0ca6f9SAndy Whitcroft } 207300df344fSAndy Whitcroft } 207400df344fSAndy Whitcroft 20754d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 20764d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 20773e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 20783e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 20793e469cdcSAndy Whitcroft if (!defined $stat); 20804d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 20814d001e4dSAndy Whitcroft 20824d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 20834d001e4dSAndy Whitcroft 20844d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 20854d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 20864d001e4dSAndy Whitcroft # where necessary. 20874d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 20884d001e4dSAndy Whitcroft 20894d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 20906f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 20916f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 20924d001e4dSAndy Whitcroft 20934d001e4dSAndy Whitcroft # We want to check the first line inside the block 20944d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 20954d001e4dSAndy Whitcroft # 1) any blank line termination 20964d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 20974d001e4dSAndy Whitcroft # 3) any do (...) { 20984d001e4dSAndy Whitcroft my $continuation = 0; 20994d001e4dSAndy Whitcroft my $check = 0; 21004d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 21014d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 21024d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 21034d001e4dSAndy Whitcroft $continuation = 1; 21044d001e4dSAndy Whitcroft } 21059bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 21064d001e4dSAndy Whitcroft $check = 1; 21074d001e4dSAndy Whitcroft $cond_lines++; 21084d001e4dSAndy Whitcroft } 21094d001e4dSAndy Whitcroft 21104d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 21114d001e4dSAndy Whitcroft # preprocessor statement. 21124d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 21134d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 21144d001e4dSAndy Whitcroft $check = 0; 21154d001e4dSAndy Whitcroft } 21164d001e4dSAndy Whitcroft 21179bd49efeSAndy Whitcroft my $cond_ptr = -1; 2118740504c6SAndy Whitcroft $continuation = 0; 21199bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 21209bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 21214d001e4dSAndy Whitcroft 2122f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 2123f16fa28fSAndy Whitcroft # is not linear. 2124f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2125f16fa28fSAndy Whitcroft $check = 0; 2126f16fa28fSAndy Whitcroft } 2127f16fa28fSAndy Whitcroft 21289bd49efeSAndy Whitcroft # Ignore: 21299bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 21309bd49efeSAndy Whitcroft # 2) preprocessor lines, and 21319bd49efeSAndy Whitcroft # 3) labels. 2132740504c6SAndy Whitcroft if ($continuation || 2133740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 21349bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 21359bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 2136740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 213730dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 21389bd49efeSAndy Whitcroft $cond_lines++; 21399bd49efeSAndy Whitcroft } 21404d001e4dSAndy Whitcroft } 214130dad6ebSAndy Whitcroft } 21424d001e4dSAndy Whitcroft 21434d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 21444d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 21454d001e4dSAndy Whitcroft 21464d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 21474d001e4dSAndy Whitcroft # this is not this patch's fault. 21484d001e4dSAndy Whitcroft if (!defined($stat_real) || 21494d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 21504d001e4dSAndy Whitcroft $check = 0; 21514d001e4dSAndy Whitcroft } 21524d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 21534d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 21544d001e4dSAndy Whitcroft } 21554d001e4dSAndy Whitcroft 21569bd49efeSAndy 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"; 21574d001e4dSAndy Whitcroft 21584d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 21594d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 2160000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 2161000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 21624d001e4dSAndy Whitcroft } 21634d001e4dSAndy Whitcroft } 21644d001e4dSAndy Whitcroft 21656c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 21666c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 21671f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 21681f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 21696c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 2170c2fdda0dSAndy Whitcroft if ($dbg_values) { 2171c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 2172cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 2173cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 21741f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 2175c2fdda0dSAndy Whitcroft } 21766c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 21776c72ffaaSAndy Whitcroft 217800df344fSAndy Whitcroft#ignore lines not being added 217900df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 218000df344fSAndy Whitcroft 2181653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 21827429c690SAndy Whitcroft if ($dbg_type) { 21837429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 2184000d1cc1SJoe Perches ERROR("TEST_TYPE", 2185000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 21867429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2187000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 2188000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 21897429c690SAndy Whitcroft } 2190653d4876SAndy Whitcroft next; 2191653d4876SAndy Whitcroft } 2192a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2193a1ef277eSAndy Whitcroft if ($dbg_attr) { 21949360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2195000d1cc1SJoe Perches ERROR("TEST_ATTR", 2196000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 21979360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2198000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2199000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2200a1ef277eSAndy Whitcroft } 2201a1ef277eSAndy Whitcroft next; 2202a1ef277eSAndy Whitcroft } 2203653d4876SAndy Whitcroft 2204f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 220599423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 220699423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2207000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2208000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . $hereprev); 2209f0a594c1SAndy Whitcroft } 2210f0a594c1SAndy Whitcroft 221100df344fSAndy Whitcroft# 221200df344fSAndy Whitcroft# Checks which are anchored on the added line. 221300df344fSAndy Whitcroft# 221400df344fSAndy Whitcroft 2215653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2216c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2217653d4876SAndy Whitcroft my $path = $1; 2218653d4876SAndy Whitcroft if ($path =~ m{//}) { 2219000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2220000d1cc1SJoe Perches "malformed #include filename\n" . 2221de7d4f0eSAndy Whitcroft $herecurr); 2222653d4876SAndy Whitcroft } 2223653d4876SAndy Whitcroft } 2224653d4876SAndy Whitcroft 222500df344fSAndy Whitcroft# no C99 // comments 222600df344fSAndy Whitcroft if ($line =~ m{//}) { 2227000d1cc1SJoe Perches ERROR("C99_COMMENTS", 2228000d1cc1SJoe Perches "do not use C99 // comments\n" . $herecurr); 222900df344fSAndy Whitcroft } 223000df344fSAndy Whitcroft # Remove C99 comments. 22310a920b5bSAndy Whitcroft $line =~ s@//.*@@; 22326c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 22330a920b5bSAndy Whitcroft 22342b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 22352b474a1aSAndy Whitcroft# the whole statement. 22362b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 22372b474a1aSAndy Whitcroft if (defined $realline_next && 22382b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 22392b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 22402b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 22412b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 22423cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 22433cbf62dfSAndy Whitcroft # a prefix: 22443cbf62dfSAndy Whitcroft # XXX(foo); 22453cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 2246653d4876SAndy Whitcroft my $name = $1; 224787a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 22483cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 22493cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 22503cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 22513cbf62dfSAndy Whitcroft 22523cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 22532b474a1aSAndy Whitcroft \n.}\s*$| 225448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 225548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 225648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 22572b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 22582b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 225948012058SAndy Whitcroft )/x) { 22602b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 22612b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 22622b474a1aSAndy Whitcroft } else { 22632b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 22640a920b5bSAndy Whitcroft } 22650a920b5bSAndy Whitcroft } 22662b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 22672b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 22682b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 22692b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 22702b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 22712b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 22722b474a1aSAndy Whitcroft } 22732b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 22742b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 2275000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 2276000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 22772b474a1aSAndy Whitcroft } 22780a920b5bSAndy Whitcroft 22795150bda4SJoe Eloff# check for global initialisers. 2280c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 2281000d1cc1SJoe Perches ERROR("GLOBAL_INITIALISERS", 2282000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 2283f0a594c1SAndy Whitcroft $herecurr); 2284f0a594c1SAndy Whitcroft } 22850a920b5bSAndy Whitcroft# check for static initialisers. 22862d1bafd7SAndy Whitcroft if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2287000d1cc1SJoe Perches ERROR("INITIALISED_STATIC", 2288000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 2289de7d4f0eSAndy Whitcroft $herecurr); 22900a920b5bSAndy Whitcroft } 22910a920b5bSAndy Whitcroft 2292cb710ecaSJoe Perches# check for static const char * arrays. 2293cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 2294000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2295000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 2296cb710ecaSJoe Perches $herecurr); 2297cb710ecaSJoe Perches } 2298cb710ecaSJoe Perches 2299cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 2300cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 2301000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2302000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 2303cb710ecaSJoe Perches $herecurr); 2304cb710ecaSJoe Perches } 2305cb710ecaSJoe Perches 230693ed0e2dSJoe Perches# check for declarations of struct pci_device_id 230793ed0e2dSJoe Perches if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { 2308000d1cc1SJoe Perches WARN("DEFINE_PCI_DEVICE_TABLE", 2309000d1cc1SJoe Perches "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); 231093ed0e2dSJoe Perches } 231193ed0e2dSJoe Perches 2312653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 2313653d4876SAndy Whitcroft# make sense. 2314653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 23158054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 2316c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 23178ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 2318653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 2319000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 2320000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 23210a920b5bSAndy Whitcroft } 23220a920b5bSAndy Whitcroft 23230a920b5bSAndy Whitcroft# * goes on variable not on type 232465863862SAndy Whitcroft # (char*[ const]) 2325bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2326bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 2327bfcb2cc7SAndy Whitcroft my ($from, $to) = ($2, $2); 2328d8aaf121SAndy Whitcroft 232965863862SAndy Whitcroft # Should start with a space. 233065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 233165863862SAndy Whitcroft # Should not end with a space. 233265863862SAndy Whitcroft $to =~ s/\s+$//; 233365863862SAndy Whitcroft # '*'s should not have spaces between. 2334f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 233565863862SAndy Whitcroft } 2336d8aaf121SAndy Whitcroft 233765863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 233865863862SAndy Whitcroft if ($from ne $to) { 2339000d1cc1SJoe Perches ERROR("POINTER_LOCATION", 2340000d1cc1SJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 234165863862SAndy Whitcroft } 2342bfcb2cc7SAndy Whitcroft } 2343bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2344bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 2345bfcb2cc7SAndy Whitcroft my ($from, $to, $ident) = ($2, $2, $3); 2346d8aaf121SAndy Whitcroft 234765863862SAndy Whitcroft # Should start with a space. 234865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 234965863862SAndy Whitcroft # Should not end with a space. 235065863862SAndy Whitcroft $to =~ s/\s+$//; 235165863862SAndy Whitcroft # '*'s should not have spaces between. 2352f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 235365863862SAndy Whitcroft } 235465863862SAndy Whitcroft # Modifiers should have spaces. 235565863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 235665863862SAndy Whitcroft 2357667026e7SAndy Whitcroft #print "from<$from> to<$to> ident<$ident>\n"; 2358667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 2359000d1cc1SJoe Perches ERROR("POINTER_LOCATION", 2360000d1cc1SJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 236165863862SAndy Whitcroft } 23620a920b5bSAndy Whitcroft } 23630a920b5bSAndy Whitcroft 23640a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 23650a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 23660a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 23670a920b5bSAndy Whitcroft# print "$herecurr"; 23680a920b5bSAndy Whitcroft# $clean = 0; 23690a920b5bSAndy Whitcroft# } 23700a920b5bSAndy Whitcroft 23718905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 2372000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 2373000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 23748905a67cSAndy Whitcroft } 23758905a67cSAndy Whitcroft 237617441227SJoe Perches# check for uses of printk_ratelimit 237717441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 2378000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 2379000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 238017441227SJoe Perches } 238117441227SJoe Perches 238200df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 238300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 238400df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 238525985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 238600df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 2387f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 238800df344fSAndy Whitcroft my $ok = 0; 238900df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 239000df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 239125985edcSLucas De Marchi # we have a preceding printk if it ends 239200df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 239300df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 239400df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 239500df344fSAndy Whitcroft $ok = 1; 239600df344fSAndy Whitcroft } 239700df344fSAndy Whitcroft last; 239800df344fSAndy Whitcroft } 239900df344fSAndy Whitcroft } 240000df344fSAndy Whitcroft if ($ok == 0) { 2401000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 2402000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 24030a920b5bSAndy Whitcroft } 240400df344fSAndy Whitcroft } 24050a920b5bSAndy Whitcroft 2406243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 2407243f3803SJoe Perches my $orig = $1; 2408243f3803SJoe Perches my $level = lc($orig); 2409243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 24108f26b837SJoe Perches my $level2 = $level; 24118f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 2412243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 24138f26b837SJoe Perches "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 2414243f3803SJoe Perches } 2415243f3803SJoe Perches 2416243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 2417243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 2418243f3803SJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr); 2419243f3803SJoe Perches } 2420243f3803SJoe Perches 2421653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 2422653d4876SAndy Whitcroft# or if closed on same line 2423c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2424c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 2425000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2426000d1cc1SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr); 24270a920b5bSAndy Whitcroft } 2428653d4876SAndy Whitcroft 24298905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 24308905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 24318905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2432000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2433000d1cc1SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev); 24348905a67cSAndy Whitcroft } 24358905a67cSAndy Whitcroft 24360c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 24370c73b4ebSAndy Whitcroft if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 2438000d1cc1SJoe Perches WARN("SPACING", 2439000d1cc1SJoe Perches "missing space after $1 definition\n" . $herecurr); 24400c73b4ebSAndy Whitcroft } 24410c73b4ebSAndy Whitcroft 24428d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 24438d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 2444fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2445fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 24468d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 24478d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 24488d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 2449fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 2450daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 2451000d1cc1SJoe Perches ERROR("BRACKET_SPACE", 2452000d1cc1SJoe Perches "space prohibited before open square bracket '['\n" . $herecurr); 24538d31cfceSAndy Whitcroft } 24548d31cfceSAndy Whitcroft } 24558d31cfceSAndy Whitcroft 2456f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 24576c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 2458c2fdda0dSAndy Whitcroft my $name = $1; 2459773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 2460773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 2461c2fdda0dSAndy Whitcroft 2462c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 2463773647a0SAndy Whitcroft if ($name =~ /^(?: 2464773647a0SAndy Whitcroft if|for|while|switch|return|case| 2465773647a0SAndy Whitcroft volatile|__volatile__| 2466773647a0SAndy Whitcroft __attribute__|format|__extension__| 2467773647a0SAndy Whitcroft asm|__asm__)$/x) 2468773647a0SAndy Whitcroft { 2469c2fdda0dSAndy Whitcroft 2470c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 2471c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 2472c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2473c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2474773647a0SAndy Whitcroft 2475773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2476c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2477c2fdda0dSAndy Whitcroft 2478c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2479c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2480773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2481c2fdda0dSAndy Whitcroft 2482c2fdda0dSAndy Whitcroft } else { 2483000d1cc1SJoe Perches WARN("SPACING", 2484000d1cc1SJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr); 2485f0a594c1SAndy Whitcroft } 24866c72ffaaSAndy Whitcroft } 24879a4cad4eSEric Nelson 24889a4cad4eSEric Nelson# check for whitespace before a non-naked semicolon 24899a4cad4eSEric Nelson if ($line =~ /^\+.*\S\s+;/) { 24909a4cad4eSEric Nelson CHK("SPACING", 24919a4cad4eSEric Nelson "space prohibited before semicolon\n" . $herecurr); 24929a4cad4eSEric Nelson } 24939a4cad4eSEric Nelson 2494653d4876SAndy Whitcroft# Check operator spacing. 24950a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 24969c0ca6f9SAndy Whitcroft my $ops = qr{ 24979c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 24989c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 24999c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 25001f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 25011f65f947SAndy Whitcroft \?|: 25029c0ca6f9SAndy Whitcroft }x; 2503cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 250400df344fSAndy Whitcroft my $off = 0; 25056c72ffaaSAndy Whitcroft 25066c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 25076c72ffaaSAndy Whitcroft 25080a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 25094a0df2efSAndy Whitcroft $off += length($elements[$n]); 25104a0df2efSAndy Whitcroft 251125985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 2512773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 2513773647a0SAndy Whitcroft my $cc = ''; 2514773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 2515773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 2516773647a0SAndy Whitcroft } 2517773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 2518773647a0SAndy Whitcroft 25194a0df2efSAndy Whitcroft my $a = ''; 25204a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 25214a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 2522cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 25234a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 25244a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 2525773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 25264a0df2efSAndy Whitcroft 25270a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 25284a0df2efSAndy Whitcroft 25294a0df2efSAndy Whitcroft my $c = ''; 25300a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 25314a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 25324a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2533cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 25344a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 25354a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 25368b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 25374a0df2efSAndy Whitcroft } else { 25384a0df2efSAndy Whitcroft $c = 'E'; 25390a920b5bSAndy Whitcroft } 25400a920b5bSAndy Whitcroft 25414a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 25424a0df2efSAndy Whitcroft 25434a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 25444a0df2efSAndy Whitcroft 25456c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 2546de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 25470a920b5bSAndy Whitcroft 254874048ed8SAndy Whitcroft # Pull out the value of this operator. 25496c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 25500a920b5bSAndy Whitcroft 25511f65f947SAndy Whitcroft # Get the full operator variant. 25521f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 25531f65f947SAndy Whitcroft 255413214adfSAndy Whitcroft # Ignore operators passed as parameters. 255513214adfSAndy Whitcroft if ($op_type ne 'V' && 255613214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 255713214adfSAndy Whitcroft 2558cf655043SAndy Whitcroft# # Ignore comments 2559cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 256013214adfSAndy Whitcroft 2561d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 256213214adfSAndy Whitcroft } elsif ($op eq ';') { 2563cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 2564cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 2565000d1cc1SJoe Perches ERROR("SPACING", 2566000d1cc1SJoe Perches "space required after that '$op' $at\n" . $hereptr); 2567d8aaf121SAndy Whitcroft } 2568d8aaf121SAndy Whitcroft 2569d8aaf121SAndy Whitcroft # // is a comment 2570d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 25710a920b5bSAndy Whitcroft 25721f65f947SAndy Whitcroft # No spaces for: 25731f65f947SAndy Whitcroft # -> 25741f65f947SAndy Whitcroft # : when part of a bitfield 25751f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 25764a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 2577000d1cc1SJoe Perches ERROR("SPACING", 2578000d1cc1SJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr); 25790a920b5bSAndy Whitcroft } 25800a920b5bSAndy Whitcroft 25810a920b5bSAndy Whitcroft # , must have a space on the right. 25820a920b5bSAndy Whitcroft } elsif ($op eq ',') { 2583cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2584000d1cc1SJoe Perches ERROR("SPACING", 2585000d1cc1SJoe Perches "space required after that '$op' $at\n" . $hereptr); 25860a920b5bSAndy Whitcroft } 25870a920b5bSAndy Whitcroft 25889c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 258974048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 25909c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 25919c0ca6f9SAndy Whitcroft 25929c0ca6f9SAndy Whitcroft # unary operators should have a space before and 25939c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 25949c0ca6f9SAndy Whitcroft # unary operator, or a cast 25959c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 259674048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 25970d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 2598cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2599000d1cc1SJoe Perches ERROR("SPACING", 2600000d1cc1SJoe Perches "space required before that '$op' $at\n" . $hereptr); 26010a920b5bSAndy Whitcroft } 2602a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2603171ae1a4SAndy Whitcroft # A unary '*' may be const 2604171ae1a4SAndy Whitcroft 2605171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 2606000d1cc1SJoe Perches ERROR("SPACING", 2607000d1cc1SJoe Perches "space prohibited after that '$op' $at\n" . $hereptr); 26080a920b5bSAndy Whitcroft } 26090a920b5bSAndy Whitcroft 26100a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 26110a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 2612773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2613000d1cc1SJoe Perches ERROR("SPACING", 2614000d1cc1SJoe Perches "space required one side of that '$op' $at\n" . $hereptr); 26150a920b5bSAndy Whitcroft } 2616773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 2617773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2618000d1cc1SJoe Perches ERROR("SPACING", 2619000d1cc1SJoe Perches "space prohibited before that '$op' $at\n" . $hereptr); 2620653d4876SAndy Whitcroft } 2621773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 2622000d1cc1SJoe Perches ERROR("SPACING", 2623000d1cc1SJoe Perches "space prohibited after that '$op' $at\n" . $hereptr); 2624773647a0SAndy Whitcroft } 2625773647a0SAndy Whitcroft 26260a920b5bSAndy Whitcroft 26270a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 26289c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 26299c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 26309c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 2631c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 2632c2fdda0dSAndy Whitcroft $op eq '%') 26330a920b5bSAndy Whitcroft { 2634773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 2635000d1cc1SJoe Perches ERROR("SPACING", 2636000d1cc1SJoe Perches "need consistent spacing around '$op' $at\n" . 2637de7d4f0eSAndy Whitcroft $hereptr); 26380a920b5bSAndy Whitcroft } 26390a920b5bSAndy Whitcroft 26401f65f947SAndy Whitcroft # A colon needs no spaces before when it is 26411f65f947SAndy Whitcroft # terminating a case value or a label. 26421f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 26431f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 2644000d1cc1SJoe Perches ERROR("SPACING", 2645000d1cc1SJoe Perches "space prohibited before that '$op' $at\n" . $hereptr); 26461f65f947SAndy Whitcroft } 26471f65f947SAndy Whitcroft 26480a920b5bSAndy Whitcroft # All the others need spaces both sides. 2649cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 26501f65f947SAndy Whitcroft my $ok = 0; 26511f65f947SAndy Whitcroft 265222f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 26531f65f947SAndy Whitcroft if (($op eq '<' && 26541f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 26551f65f947SAndy Whitcroft ($op eq '>' && 26561f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 26571f65f947SAndy Whitcroft { 26581f65f947SAndy Whitcroft $ok = 1; 26591f65f947SAndy Whitcroft } 26601f65f947SAndy Whitcroft 26611f65f947SAndy Whitcroft # Ignore ?: 26621f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 26631f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 26641f65f947SAndy Whitcroft $ok = 1; 26651f65f947SAndy Whitcroft } 26661f65f947SAndy Whitcroft 26671f65f947SAndy Whitcroft if ($ok == 0) { 2668000d1cc1SJoe Perches ERROR("SPACING", 2669000d1cc1SJoe Perches "spaces required around that '$op' $at\n" . $hereptr); 26700a920b5bSAndy Whitcroft } 267122f2a2efSAndy Whitcroft } 26724a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 26730a920b5bSAndy Whitcroft } 26740a920b5bSAndy Whitcroft } 26750a920b5bSAndy Whitcroft 2676f0a594c1SAndy Whitcroft# check for multiple assignments 2677f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 2678000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 2679000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 2680f0a594c1SAndy Whitcroft } 2681f0a594c1SAndy Whitcroft 268222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 268322f2a2efSAndy Whitcroft## # continuation. 268422f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 268522f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 268622f2a2efSAndy Whitcroft## 268722f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 268822f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 268922f2a2efSAndy Whitcroft## my $ln = $line; 269022f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 269122f2a2efSAndy Whitcroft## } 269222f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 2693000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 2694000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 269522f2a2efSAndy Whitcroft## } 269622f2a2efSAndy Whitcroft## } 2697f0a594c1SAndy Whitcroft 26980a920b5bSAndy Whitcroft#need space before brace following if, while, etc 269922f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 270022f2a2efSAndy Whitcroft $line =~ /do{/) { 2701000d1cc1SJoe Perches ERROR("SPACING", 2702000d1cc1SJoe Perches "space required before the open brace '{'\n" . $herecurr); 2703de7d4f0eSAndy Whitcroft } 2704de7d4f0eSAndy Whitcroft 2705de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 2706de7d4f0eSAndy Whitcroft# on the line 2707de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 2708000d1cc1SJoe Perches ERROR("SPACING", 2709000d1cc1SJoe Perches "space required after that close brace '}'\n" . $herecurr); 27100a920b5bSAndy Whitcroft } 27110a920b5bSAndy Whitcroft 271222f2a2efSAndy Whitcroft# check spacing on square brackets 271322f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2714000d1cc1SJoe Perches ERROR("SPACING", 2715000d1cc1SJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr); 271622f2a2efSAndy Whitcroft } 271722f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 2718000d1cc1SJoe Perches ERROR("SPACING", 2719000d1cc1SJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr); 272022f2a2efSAndy Whitcroft } 272122f2a2efSAndy Whitcroft 2722c45dcabdSAndy Whitcroft# check spacing on parentheses 27239c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 27249c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 2725000d1cc1SJoe Perches ERROR("SPACING", 2726000d1cc1SJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr); 272722f2a2efSAndy Whitcroft } 272813214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2729c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2730c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2731000d1cc1SJoe Perches ERROR("SPACING", 2732000d1cc1SJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr); 273322f2a2efSAndy Whitcroft } 273422f2a2efSAndy Whitcroft 27350a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 27364a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 27370a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2738000d1cc1SJoe Perches WARN("INDENTED_LABEL", 2739000d1cc1SJoe Perches "labels should not be indented\n" . $herecurr); 27400a920b5bSAndy Whitcroft } 27410a920b5bSAndy Whitcroft 2742c45dcabdSAndy Whitcroft# Return is not a function. 2743c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2744c45dcabdSAndy Whitcroft my $spacing = $1; 2745c45dcabdSAndy Whitcroft my $value = $2; 2746c45dcabdSAndy Whitcroft 274786f9d059SAndy Whitcroft # Flatten any parentheses 2748fb2d2c1bSAndy Whitcroft $value =~ s/\(/ \(/g; 2749fb2d2c1bSAndy Whitcroft $value =~ s/\)/\) /g; 2750e01886adSAndy Whitcroft while ($value =~ s/\[[^\[\]]*\]/1/ || 275163f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 275263f17f89SAndy Whitcroft $Compare\s* 275363f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 275463f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2755c45dcabdSAndy Whitcroft } 2756fb2d2c1bSAndy Whitcroft#print "value<$value>\n"; 2757fb2d2c1bSAndy Whitcroft if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 2758000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 2759000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 2760c45dcabdSAndy Whitcroft 2761c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2762000d1cc1SJoe Perches ERROR("SPACING", 2763000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 2764c45dcabdSAndy Whitcroft } 2765c45dcabdSAndy Whitcroft } 276653a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 276753a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 276853a3c448SAndy Whitcroft my $name = $1; 276953a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 2770000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 2771000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 277253a3c448SAndy Whitcroft } 277353a3c448SAndy Whitcroft } 2774c45dcabdSAndy Whitcroft 27750a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 27764a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2777000d1cc1SJoe Perches ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); 27780a920b5bSAndy Whitcroft } 27790a920b5bSAndy Whitcroft 2780f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2781f5fe35ddSAndy Whitcroft# statements after the conditional. 2782170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 27833e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 27843e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 27853e469cdcSAndy Whitcroft if (!defined $stat); 2786170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2787170d3a22SAndy Whitcroft $remain_next, $off_next); 2788170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2789170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2790170d3a22SAndy Whitcroft 2791170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2792170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2793170d3a22SAndy Whitcroft # then count those as offsets. 2794170d3a22SAndy Whitcroft my ($whitespace) = 2795170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2796170d3a22SAndy Whitcroft my $offset = 2797170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2798170d3a22SAndy Whitcroft 2799170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2800170d3a22SAndy Whitcroft $offset} = 1; 2801170d3a22SAndy Whitcroft } 2802170d3a22SAndy Whitcroft } 2803170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2804170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2805171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 28068905a67cSAndy Whitcroft 2807b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2808000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 2809000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 28108905a67cSAndy Whitcroft } 28118905a67cSAndy Whitcroft 28128905a67cSAndy Whitcroft # Find out what is on the end of the line after the 28138905a67cSAndy Whitcroft # conditional. 2814773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 28158905a67cSAndy Whitcroft $s =~ s/\n.*//g; 281613214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 281753210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 281853210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2819773647a0SAndy Whitcroft { 2820bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2821bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2822bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 282342bdf74cSHidetoshi Seto my $stat_real = ''; 2824bb44ad39SAndy Whitcroft 282542bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 282642bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 2827bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2828bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2829bb44ad39SAndy Whitcroft } 2830bb44ad39SAndy Whitcroft 2831000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2832000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 28338905a67cSAndy Whitcroft } 28348905a67cSAndy Whitcroft } 28358905a67cSAndy Whitcroft 283613214adfSAndy Whitcroft# Check for bitwise tests written as boolean 283713214adfSAndy Whitcroft if ($line =~ / 283813214adfSAndy Whitcroft (?: 283913214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 284013214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 284113214adfSAndy Whitcroft (?:\&\&|\|\|) 284213214adfSAndy Whitcroft | 284313214adfSAndy Whitcroft (?:\&\&|\|\|) 284413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 284513214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 284613214adfSAndy Whitcroft )/x) 284713214adfSAndy Whitcroft { 2848000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 2849000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 285013214adfSAndy Whitcroft } 285113214adfSAndy Whitcroft 28528905a67cSAndy Whitcroft# if and else should not have general statements after it 285313214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 285413214adfSAndy Whitcroft my $s = $1; 285513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 285613214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 2857000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2858000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 28590a920b5bSAndy Whitcroft } 286013214adfSAndy Whitcroft } 286139667782SAndy Whitcroft# if should not continue a brace 286239667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 2863000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2864000d1cc1SJoe Perches "trailing statements should be on next line\n" . 286539667782SAndy Whitcroft $herecurr); 286639667782SAndy Whitcroft } 2867a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2868a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2869a1080bf8SAndy Whitcroft $line !~ /\G(?: 28703fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2871a1080bf8SAndy Whitcroft \s*return\s+ 2872a1080bf8SAndy Whitcroft )/xg) 2873a1080bf8SAndy Whitcroft { 2874000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2875000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 2876a1080bf8SAndy Whitcroft } 28770a920b5bSAndy Whitcroft 28780a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 28790a920b5bSAndy Whitcroft # indent level to be relevant to each other. 28800a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 28810a920b5bSAndy Whitcroft $previndent == $indent) { 2882000d1cc1SJoe Perches ERROR("ELSE_AFTER_BRACE", 2883000d1cc1SJoe Perches "else should follow close brace '}'\n" . $hereprev); 28840a920b5bSAndy Whitcroft } 28850a920b5bSAndy Whitcroft 2886c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2887c2fdda0dSAndy Whitcroft $previndent == $indent) { 2888c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2889c2fdda0dSAndy Whitcroft 2890c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2891c2fdda0dSAndy Whitcroft # conditional. 2892773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2893c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2894c2fdda0dSAndy Whitcroft 2895c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2896000d1cc1SJoe Perches ERROR("WHILE_AFTER_BRACE", 2897000d1cc1SJoe Perches "while should follow close brace '}'\n" . $hereprev); 2898c2fdda0dSAndy Whitcroft } 2899c2fdda0dSAndy Whitcroft } 2900c2fdda0dSAndy Whitcroft 29010a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 29020a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 29030a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 29040a920b5bSAndy Whitcroft# print "$herecurr"; 29050a920b5bSAndy Whitcroft# $clean = 0; 29060a920b5bSAndy Whitcroft# } 29070a920b5bSAndy Whitcroft 29080a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2909c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2910000d1cc1SJoe Perches WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 2911000d1cc1SJoe Perches "Whitepspace after \\ makes next lines useless\n" . $herecurr); 29120a920b5bSAndy Whitcroft } 29130a920b5bSAndy Whitcroft 2914653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2915c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2916e09dec48SAndy Whitcroft my $file = "$1.h"; 2917e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2918e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2919e09dec48SAndy Whitcroft $realfile ne $checkfile && 29207840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 2921c45dcabdSAndy Whitcroft { 2922e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2923000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 2924000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2925e09dec48SAndy Whitcroft } else { 2926000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 2927000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2928e09dec48SAndy Whitcroft } 29290a920b5bSAndy Whitcroft } 29300a920b5bSAndy Whitcroft } 29310a920b5bSAndy Whitcroft 2932653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2933653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2934cf655043SAndy Whitcroft# in a known good container 2935b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2936b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2937d8aaf121SAndy Whitcroft my $ln = $linenr; 2938d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2939c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2940c45dcabdSAndy Whitcroft my $ctx = ''; 2941c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2942f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2943f74bd194SAndy Whitcroft $ctx = $dstat; 2944c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2945a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2946c45dcabdSAndy Whitcroft 2947f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 2948292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2949c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2950c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2951c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2952c45dcabdSAndy Whitcroft 2953c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2954bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2955bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2956c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 2957bf30d6edSAndy Whitcroft { 2958c45dcabdSAndy Whitcroft } 2959c45dcabdSAndy Whitcroft 2960e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 2961e45bab8eSAndy Whitcroft while ($dstat =~ s/("X*")\s*$Ident/$1/ || 2962e45bab8eSAndy Whitcroft $dstat =~ s/$Ident\s*("X*")/$1/) 2963e45bab8eSAndy Whitcroft { 2964e45bab8eSAndy Whitcroft } 2965e45bab8eSAndy Whitcroft 2966c45dcabdSAndy Whitcroft my $exceptions = qr{ 2967c45dcabdSAndy Whitcroft $Declare| 2968c45dcabdSAndy Whitcroft module_param_named| 2969a0a0a7a9SKees Cook MODULE_PARM_DESC| 2970c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2971c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2972383099fdSAndy Whitcroft __typeof__\(| 297322fd2d3eSStefani Seibold union| 297422fd2d3eSStefani Seibold struct| 2975ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 2976ea71a0a0SAndy Whitcroft ^\"|\"$ 2977c45dcabdSAndy Whitcroft }x; 29785eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 2979f74bd194SAndy Whitcroft if ($dstat ne '' && 2980f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 2981f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 2982fd1b57acSAndy Whitcroft $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo 2983b9df76acSAndy Whitcroft $dstat !~ /^'X'$/ && # character constants 2984f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 2985f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 298672f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 2987f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 2988f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 2989f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 2990f74bd194SAndy Whitcroft $dstat !~ /^\({/) # ({... 2991c45dcabdSAndy Whitcroft { 2992f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 2993f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 2994f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 2995f74bd194SAndy Whitcroft 2996f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 2997f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 2998c45dcabdSAndy Whitcroft } 2999c45dcabdSAndy Whitcroft 3000f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 3001f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 3002f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 3003f74bd194SAndy Whitcroft } else { 3004000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 3005f74bd194SAndy Whitcroft "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 3006d8aaf121SAndy Whitcroft } 30070a920b5bSAndy Whitcroft } 30085023d347SJoe Perches 30095023d347SJoe Perches# check for line continuations outside of #defines 30105023d347SJoe Perches 30115023d347SJoe Perches } else { 30125023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 30135023d347SJoe Perches $line =~ /^\+.*\\$/) { 30145023d347SJoe Perches WARN("LINE_CONTINUATIONS", 30155023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 30165023d347SJoe Perches } 3017653d4876SAndy Whitcroft } 30180a920b5bSAndy Whitcroft 3019b13edf7fSJoe Perches# do {} while (0) macro tests: 3020b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 3021b13edf7fSJoe Perches# macro should not end with a semicolon 3022b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 3023b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 3024b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 3025b13edf7fSJoe Perches my $ln = $linenr; 3026b13edf7fSJoe Perches my $cnt = $realcnt; 3027b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 3028b13edf7fSJoe Perches my $ctx = ''; 3029b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 3030b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 3031b13edf7fSJoe Perches $ctx = $dstat; 3032b13edf7fSJoe Perches 3033b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 3034b13edf7fSJoe Perches 3035b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 3036b13edf7fSJoe Perches my $stmts = $2; 3037b13edf7fSJoe Perches my $semis = $3; 3038b13edf7fSJoe Perches 3039b13edf7fSJoe Perches $ctx =~ s/\n*$//; 3040b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 3041b13edf7fSJoe Perches my $herectx = $here . "\n"; 3042b13edf7fSJoe Perches 3043b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 3044b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 3045b13edf7fSJoe Perches } 3046b13edf7fSJoe Perches 3047ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 3048ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 3049b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 3050b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 3051b13edf7fSJoe Perches } 3052b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 3053b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 3054b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 3055b13edf7fSJoe Perches } 3056b13edf7fSJoe Perches } 3057b13edf7fSJoe Perches } 3058b13edf7fSJoe Perches 3059080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 3060080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 3061080ba929SMike Frysinger# . 3062080ba929SMike Frysinger# ALIGN(...) 3063080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 3064080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 3065000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 3066000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 3067080ba929SMike Frysinger } 3068080ba929SMike Frysinger 3069f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 307013214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 307113214adfSAndy Whitcroft my ($level, $endln, @chunks) = 3072cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 307313214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 3074cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 3075cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 3076aad4f614SJoe Perches my @allowed = (); 3077aad4f614SJoe Perches my $allow = 0; 307813214adfSAndy Whitcroft my $seen = 0; 3079773647a0SAndy Whitcroft my $herectx = $here . "\n"; 3080cf655043SAndy Whitcroft my $ln = $linenr - 1; 308113214adfSAndy Whitcroft for my $chunk (@chunks) { 308213214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 308313214adfSAndy Whitcroft 3084773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 3085773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 3086773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 3087773647a0SAndy Whitcroft 3088aad4f614SJoe Perches $allowed[$allow] = 0; 3089773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 3090773647a0SAndy Whitcroft 3091773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 3092773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 3093773647a0SAndy Whitcroft 3094773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 3095cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 3096cf655043SAndy Whitcroft 3097773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 309813214adfSAndy Whitcroft 309913214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 310013214adfSAndy Whitcroft 3101aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 3102cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3103cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3104aad4f614SJoe Perches $allowed[$allow] = 1; 310513214adfSAndy Whitcroft } 310613214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3107cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3108aad4f614SJoe Perches $allowed[$allow] = 1; 310913214adfSAndy Whitcroft } 3110cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3111cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3112aad4f614SJoe Perches $allowed[$allow] = 1; 311313214adfSAndy Whitcroft } 3114aad4f614SJoe Perches $allow++; 311513214adfSAndy Whitcroft } 3116aad4f614SJoe Perches if ($seen) { 3117aad4f614SJoe Perches my $sum_allowed = 0; 3118aad4f614SJoe Perches foreach (@allowed) { 3119aad4f614SJoe Perches $sum_allowed += $_; 3120aad4f614SJoe Perches } 3121aad4f614SJoe Perches if ($sum_allowed == 0) { 3122000d1cc1SJoe Perches WARN("BRACES", 3123000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 3124aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 3125aad4f614SJoe Perches $seen != $allow) { 3126aad4f614SJoe Perches CHK("BRACES", 3127aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 3128aad4f614SJoe Perches } 312913214adfSAndy Whitcroft } 313013214adfSAndy Whitcroft } 313113214adfSAndy Whitcroft } 3132773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 313313214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 3134cf655043SAndy Whitcroft my $allowed = 0; 3135f0a594c1SAndy Whitcroft 3136cf655043SAndy Whitcroft # Check the pre-context. 3137cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 3138cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 3139cf655043SAndy Whitcroft $allowed = 1; 3140f0a594c1SAndy Whitcroft } 3141773647a0SAndy Whitcroft 3142773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 3143773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 3144773647a0SAndy Whitcroft 3145cf655043SAndy Whitcroft # Check the condition. 3146cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 3147773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 3148cf655043SAndy Whitcroft if (defined $cond) { 3149773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3150cf655043SAndy Whitcroft } 3151cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3152cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3153cf655043SAndy Whitcroft $allowed = 1; 3154cf655043SAndy Whitcroft } 3155cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3156cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3157cf655043SAndy Whitcroft $allowed = 1; 3158cf655043SAndy Whitcroft } 3159cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3160cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3161cf655043SAndy Whitcroft $allowed = 1; 3162cf655043SAndy Whitcroft } 3163cf655043SAndy Whitcroft # Check the post-context. 3164cf655043SAndy Whitcroft if (defined $chunks[1]) { 3165cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 3166cf655043SAndy Whitcroft if (defined $cond) { 3167773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3168cf655043SAndy Whitcroft } 3169cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 3170cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 3171cf655043SAndy Whitcroft $allowed = 1; 3172cf655043SAndy Whitcroft } 3173cf655043SAndy Whitcroft } 3174cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 317569932487SJustin P. Mattock my $herectx = $here . "\n"; 3176f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 3177cf655043SAndy Whitcroft 3178f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 317969932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 3180cf655043SAndy Whitcroft } 3181cf655043SAndy Whitcroft 3182000d1cc1SJoe Perches WARN("BRACES", 3183000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 3184f0a594c1SAndy Whitcroft } 3185f0a594c1SAndy Whitcroft } 3186f0a594c1SAndy Whitcroft 31874a0df2efSAndy Whitcroft# no volatiles please 31886c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 31896c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 3190000d1cc1SJoe Perches WARN("VOLATILE", 3191000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 31924a0df2efSAndy Whitcroft } 31934a0df2efSAndy Whitcroft 319400df344fSAndy Whitcroft# warn about #if 0 3195c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 3196000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 3197000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 3198de7d4f0eSAndy Whitcroft $herecurr); 31994a0df2efSAndy Whitcroft } 32004a0df2efSAndy Whitcroft 3201*03df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 3202*03df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 3203*03df4b51SAndy Whitcroft my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 3204*03df4b51SAndy Whitcroft if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 3205*03df4b51SAndy Whitcroft WARN('NEEDLESS_IF', 3206*03df4b51SAndy Whitcroft "$1(NULL) is safe this check is probably not required\n" . $hereprev); 32074c432a8fSGreg Kroah-Hartman } 32084c432a8fSGreg Kroah-Hartman } 3209f0a594c1SAndy Whitcroft 32101a15a250SPatrick Pannuto# prefer usleep_range over udelay 32111a15a250SPatrick Pannuto if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { 32121a15a250SPatrick Pannuto # ignore udelay's < 10, however 32131a15a250SPatrick Pannuto if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { 3214000d1cc1SJoe Perches CHK("USLEEP_RANGE", 3215000d1cc1SJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); 32161a15a250SPatrick Pannuto } 32171a15a250SPatrick Pannuto } 32181a15a250SPatrick Pannuto 321909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 322009ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 322109ef8725SPatrick Pannuto if ($1 < 20) { 3222000d1cc1SJoe Perches WARN("MSLEEP", 3223000d1cc1SJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); 322409ef8725SPatrick Pannuto } 322509ef8725SPatrick Pannuto } 322609ef8725SPatrick Pannuto 322700df344fSAndy Whitcroft# warn about #ifdefs in C files 3228c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 322900df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 323000df344fSAndy Whitcroft# print "$herecurr"; 323100df344fSAndy Whitcroft# $clean = 0; 323200df344fSAndy Whitcroft# } 323300df344fSAndy Whitcroft 323422f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 3235c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 3236000d1cc1SJoe Perches ERROR("SPACING", 3237000d1cc1SJoe Perches "exactly one space required after that #$1\n" . $herecurr); 323822f2a2efSAndy Whitcroft } 323922f2a2efSAndy Whitcroft 32404a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 3241171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 3242171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 32434a0df2efSAndy Whitcroft my $which = $1; 32444a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3245000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 3246000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 32474a0df2efSAndy Whitcroft } 32484a0df2efSAndy Whitcroft } 32494a0df2efSAndy Whitcroft# check for memory barriers without a comment. 32504a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 32514a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3252000d1cc1SJoe Perches CHK("MEMORY_BARRIER", 3253000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 32544a0df2efSAndy Whitcroft } 32554a0df2efSAndy Whitcroft } 32564a0df2efSAndy Whitcroft# check of hardware specific defines 3257c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 3258000d1cc1SJoe Perches CHK("ARCH_DEFINES", 3259000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 32600a920b5bSAndy Whitcroft } 3261653d4876SAndy Whitcroft 3262d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 3263d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 3264000d1cc1SJoe Perches WARN("STORAGE_CLASS", 3265000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 3266d4977c78STobias Klauser } 3267d4977c78STobias Klauser 3268de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 3269de7d4f0eSAndy Whitcroft# storage class and type. 32709c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 32719c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 3272000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 3273000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 3274de7d4f0eSAndy Whitcroft } 3275de7d4f0eSAndy Whitcroft 32768905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 32778905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 3278000d1cc1SJoe Perches WARN("INLINE", 3279000d1cc1SJoe Perches "plain inline is preferred over $1\n" . $herecurr); 32808905a67cSAndy Whitcroft } 32818905a67cSAndy Whitcroft 32823d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 32833d130fd0SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 3284000d1cc1SJoe Perches WARN("PREFER_PACKED", 3285000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 32863d130fd0SJoe Perches } 32873d130fd0SJoe Perches 328839b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 328939b7e287SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 3290000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 3291000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 329239b7e287SJoe Perches } 329339b7e287SJoe Perches 32945f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 32955f14d3bdSJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 32965f14d3bdSJoe Perches WARN("PREFER_PRINTF", 32975f14d3bdSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); 32985f14d3bdSJoe Perches } 32995f14d3bdSJoe Perches 33006061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 33016061d949SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 33026061d949SJoe Perches WARN("PREFER_SCANF", 33036061d949SJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr); 33046061d949SJoe Perches } 33056061d949SJoe Perches 33068f53a9b8SJoe Perches# check for sizeof(&) 33078f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 3308000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 3309000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 33108f53a9b8SJoe Perches } 33118f53a9b8SJoe Perches 331266c80b60SJoe Perches# check for sizeof without parenthesis 331366c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 331466c80b60SJoe Perches WARN("SIZEOF_PARENTHESIS", 331566c80b60SJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr); 331666c80b60SJoe Perches } 331766c80b60SJoe Perches 3318428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 3319428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 3320000d1cc1SJoe Perches WARN("LINE_CONTINUATIONS", 3321000d1cc1SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 3322428e2fdcSJoe Perches } 3323428e2fdcSJoe Perches 3324554e165cSAndy Whitcroft# Check for misused memsets 3325d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3326d1fe9c09SJoe Perches defined $stat && 3327d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 3328554e165cSAndy Whitcroft 3329d7c76ba7SJoe Perches my $ms_addr = $2; 3330d1fe9c09SJoe Perches my $ms_val = $7; 3331d1fe9c09SJoe Perches my $ms_size = $12; 3332d7c76ba7SJoe Perches 3333554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 3334554e165cSAndy Whitcroft ERROR("MEMSET", 3335d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 3336554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 3337554e165cSAndy Whitcroft WARN("MEMSET", 3338d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 3339d7c76ba7SJoe Perches } 3340d7c76ba7SJoe Perches } 3341d7c76ba7SJoe Perches 3342d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 3343d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3344d1fe9c09SJoe Perches defined $stat && 3345d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 3346d1fe9c09SJoe Perches if (defined $2 || defined $7) { 3347d7c76ba7SJoe Perches my $call = $1; 3348d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 3349d7c76ba7SJoe Perches my $arg1 = $3; 3350d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 3351d1fe9c09SJoe Perches my $arg2 = $8; 3352d7c76ba7SJoe Perches my $cast; 3353d7c76ba7SJoe Perches 3354d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 3355d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 3356d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 3357d7c76ba7SJoe Perches $cast = $cast1; 3358d7c76ba7SJoe Perches } else { 3359d7c76ba7SJoe Perches $cast = $cast2; 3360d7c76ba7SJoe Perches } 3361d7c76ba7SJoe Perches WARN("MINMAX", 3362d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 3363554e165cSAndy Whitcroft } 3364554e165cSAndy Whitcroft } 3365554e165cSAndy Whitcroft 33664a273195SJoe Perches# check usleep_range arguments 33674a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 33684a273195SJoe Perches defined $stat && 33694a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 33704a273195SJoe Perches my $min = $1; 33714a273195SJoe Perches my $max = $7; 33724a273195SJoe Perches if ($min eq $max) { 33734a273195SJoe Perches WARN("USLEEP_RANGE", 33744a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 33754a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 33764a273195SJoe Perches $min > $max) { 33774a273195SJoe Perches WARN("USLEEP_RANGE", 33784a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 33794a273195SJoe Perches } 33804a273195SJoe Perches } 33814a273195SJoe Perches 3382de7d4f0eSAndy Whitcroft# check for new externs in .c files. 3383171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 3384c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3385171ae1a4SAndy Whitcroft { 3386c45dcabdSAndy Whitcroft my $function_name = $1; 3387c45dcabdSAndy Whitcroft my $paren_space = $2; 3388171ae1a4SAndy Whitcroft 3389171ae1a4SAndy Whitcroft my $s = $stat; 3390171ae1a4SAndy Whitcroft if (defined $cond) { 3391171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 3392171ae1a4SAndy Whitcroft } 3393c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 3394c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 3395c45dcabdSAndy Whitcroft { 3396000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3397000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3398de7d4f0eSAndy Whitcroft } 3399de7d4f0eSAndy Whitcroft 3400171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 3401000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 3402000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 3403171ae1a4SAndy Whitcroft } 34049c9ba34eSAndy Whitcroft 34059c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 34069c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 34079c9ba34eSAndy Whitcroft { 3408000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3409000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3410171ae1a4SAndy Whitcroft } 3411171ae1a4SAndy Whitcroft 3412de7d4f0eSAndy Whitcroft# checks for new __setup's 3413de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 3414de7d4f0eSAndy Whitcroft my $name = $1; 3415de7d4f0eSAndy Whitcroft 3416de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 3417000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 3418000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 3419de7d4f0eSAndy Whitcroft } 3420653d4876SAndy Whitcroft } 34219c0ca6f9SAndy Whitcroft 34229c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 3423caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 3424000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 3425000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 34269c0ca6f9SAndy Whitcroft } 342713214adfSAndy Whitcroft 3428caf2a54fSJoe Perches# check for multiple semicolons 3429caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 3430000d1cc1SJoe Perches WARN("ONE_SEMICOLON", 3431000d1cc1SJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr); 3432caf2a54fSJoe Perches } 3433caf2a54fSJoe Perches 343413214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 343513214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 3436000d1cc1SJoe Perches WARN("USE_FUNC", 3437000d1cc1SJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 343813214adfSAndy Whitcroft } 3439773647a0SAndy Whitcroft 34402c92488aSJoe Perches# check for use of yield() 34412c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 34422c92488aSJoe Perches WARN("YIELD", 34432c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 34442c92488aSJoe Perches } 34452c92488aSJoe Perches 34464882720bSThomas Gleixner# check for semaphores initialized locked 34474882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 3448000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 3449000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 3450773647a0SAndy Whitcroft } 34516712d858SJoe Perches 345267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 345367d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 3454000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 345567d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 3456773647a0SAndy Whitcroft } 34576712d858SJoe Perches 3458f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 3459f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 3460000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 3461000d1cc1SJoe Perches "please use device_initcall() instead of __initcall()\n" . $herecurr); 3462f3db6639SMichael Ellerman } 34636712d858SJoe Perches 346479404849SEmese Revfy# check for various ops structs, ensure they are const. 346579404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 346679404849SEmese Revfy address_space_operations| 346779404849SEmese Revfy backlight_ops| 346879404849SEmese Revfy block_device_operations| 346979404849SEmese Revfy dentry_operations| 347079404849SEmese Revfy dev_pm_ops| 347179404849SEmese Revfy dma_map_ops| 347279404849SEmese Revfy extent_io_ops| 347379404849SEmese Revfy file_lock_operations| 347479404849SEmese Revfy file_operations| 347579404849SEmese Revfy hv_ops| 347679404849SEmese Revfy ide_dma_ops| 347779404849SEmese Revfy intel_dvo_dev_ops| 347879404849SEmese Revfy item_operations| 347979404849SEmese Revfy iwl_ops| 348079404849SEmese Revfy kgdb_arch| 348179404849SEmese Revfy kgdb_io| 348279404849SEmese Revfy kset_uevent_ops| 348379404849SEmese Revfy lock_manager_operations| 348479404849SEmese Revfy microcode_ops| 348579404849SEmese Revfy mtrr_ops| 348679404849SEmese Revfy neigh_ops| 348779404849SEmese Revfy nlmsvc_binding| 348879404849SEmese Revfy pci_raw_ops| 348979404849SEmese Revfy pipe_buf_operations| 349079404849SEmese Revfy platform_hibernation_ops| 349179404849SEmese Revfy platform_suspend_ops| 349279404849SEmese Revfy proto_ops| 349379404849SEmese Revfy rpc_pipe_ops| 349479404849SEmese Revfy seq_operations| 349579404849SEmese Revfy snd_ac97_build_ops| 349679404849SEmese Revfy soc_pcmcia_socket_ops| 349779404849SEmese Revfy stacktrace_ops| 349879404849SEmese Revfy sysfs_ops| 349979404849SEmese Revfy tty_operations| 350079404849SEmese Revfy usb_mon_operations| 350179404849SEmese Revfy wd_ops}x; 35026903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 350379404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 3504000d1cc1SJoe Perches WARN("CONST_STRUCT", 3505000d1cc1SJoe Perches "struct $1 should normally be const\n" . 35066903ffb2SAndy Whitcroft $herecurr); 35072b6db5cbSAndy Whitcroft } 3508773647a0SAndy Whitcroft 3509773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 3510773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 3511773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 3512c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 3513c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 3514171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 3515171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 3516171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 3517773647a0SAndy Whitcroft { 3518000d1cc1SJoe Perches WARN("NR_CPUS", 3519000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 3520773647a0SAndy Whitcroft } 35219c9ba34eSAndy Whitcroft 35229c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 35239c9ba34eSAndy Whitcroft my $string; 35249c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 35259c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 35262a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 35279c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 3528000d1cc1SJoe Perches WARN("PRINTF_L", 3529000d1cc1SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 35309c9ba34eSAndy Whitcroft last; 35319c9ba34eSAndy Whitcroft } 35329c9ba34eSAndy Whitcroft } 3533691d77b6SAndy Whitcroft 3534691d77b6SAndy Whitcroft# whine mightly about in_atomic 3535691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 3536691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 3537000d1cc1SJoe Perches ERROR("IN_ATOMIC", 3538000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 3539f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 3540000d1cc1SJoe Perches WARN("IN_ATOMIC", 3541000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 3542691d77b6SAndy Whitcroft } 3543691d77b6SAndy Whitcroft } 35441704f47bSPeter Zijlstra 35451704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 35461704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 35471704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 35481704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 35491704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 35501704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 3551000d1cc1SJoe Perches ERROR("LOCKDEP", 3552000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 35531704f47bSPeter Zijlstra } 35541704f47bSPeter Zijlstra } 355588f8831cSDave Jones 355688f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 355788f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 3558000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 3559000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 356088f8831cSDave Jones } 356113214adfSAndy Whitcroft } 356213214adfSAndy Whitcroft 356313214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 356413214adfSAndy Whitcroft # so just keep quiet. 356513214adfSAndy Whitcroft if ($#rawlines == -1) { 356613214adfSAndy Whitcroft exit(0); 35670a920b5bSAndy Whitcroft } 35680a920b5bSAndy Whitcroft 35698905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 35708905a67cSAndy Whitcroft # things that appear to be patches. 35718905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 35728905a67cSAndy Whitcroft exit(0); 35738905a67cSAndy Whitcroft } 35748905a67cSAndy Whitcroft 35758905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 35768905a67cSAndy Whitcroft # just keep quiet. 35778905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 35788905a67cSAndy Whitcroft exit(0); 35798905a67cSAndy Whitcroft } 35808905a67cSAndy Whitcroft 35818905a67cSAndy Whitcroft if (!$is_patch) { 3582000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 3583000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 35840a920b5bSAndy Whitcroft } 35850a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 3586000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 3587000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 35880a920b5bSAndy Whitcroft } 35890a920b5bSAndy Whitcroft 3590f0a594c1SAndy Whitcroft print report_dump(); 359113214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 359213214adfSAndy Whitcroft print "$filename " if ($summary_file); 35936c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 35946c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 35956c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 35968905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 35976c72ffaaSAndy Whitcroft } 35988905a67cSAndy Whitcroft 3599d2c0a235SAndy Whitcroft if ($quiet == 0) { 3600d1fe9c09SJoe Perches 3601d1fe9c09SJoe Perches if ($^V lt 5.10.0) { 3602d1fe9c09SJoe Perches print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); 3603d1fe9c09SJoe Perches print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); 3604d1fe9c09SJoe Perches } 3605d1fe9c09SJoe Perches 3606d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 3607d2c0a235SAndy Whitcroft # then suggest that. 3608d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 3609d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 3610d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 3611b0781216SMike Frysinger $rpt_cleaners = 0; 3612d2c0a235SAndy Whitcroft } 3613d2c0a235SAndy Whitcroft } 3614d2c0a235SAndy Whitcroft 361511232688SArtem Bityutskiy if ($quiet == 0 && keys %ignore_type) { 3616000d1cc1SJoe Perches print "NOTE: Ignored message types:"; 3617000d1cc1SJoe Perches foreach my $ignore (sort keys %ignore_type) { 3618000d1cc1SJoe Perches print " $ignore"; 3619000d1cc1SJoe Perches } 362011232688SArtem Bityutskiy print "\n\n"; 3621000d1cc1SJoe Perches } 3622000d1cc1SJoe Perches 36230a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 3624c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 36250a920b5bSAndy Whitcroft } 36260a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 3627000d1cc1SJoe Perches print << "EOM"; 3628000d1cc1SJoe Perches$vname has style problems, please review. 3629000d1cc1SJoe Perches 3630000d1cc1SJoe PerchesIf any of these errors are false positives, please report 3631000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 3632000d1cc1SJoe PerchesEOM 36330a920b5bSAndy Whitcroft } 363413214adfSAndy Whitcroft 36350a920b5bSAndy Whitcroft return $clean; 36360a920b5bSAndy Whitcroft} 3637