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 3554a0df2efSAndy Whitcroftmy @dep_includes = (); 3564a0df2efSAndy Whitcroftmy @dep_functions = (); 3576c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 3586c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 35921caa13cSAndy Whitcroft open(my $REMOVE, '<', "$root/$removal") || 3606c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 36121caa13cSAndy Whitcroft while (<$REMOVE>) { 362f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 363f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 364f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 3654a0df2efSAndy Whitcroft push(@dep_includes, $1); 3664a0df2efSAndy Whitcroft 367f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 368f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 369f0a594c1SAndy Whitcroft } 3704a0df2efSAndy Whitcroft } 3710a920b5bSAndy Whitcroft } 3720a920b5bSAndy Whitcroft } 37321caa13cSAndy Whitcroft close($REMOVE); 3740a920b5bSAndy Whitcroft} 3750a920b5bSAndy Whitcroft 37600df344fSAndy Whitcroftmy @rawlines = (); 377c2fdda0dSAndy Whitcroftmy @lines = (); 378c2fdda0dSAndy Whitcroftmy $vname; 3796c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 38021caa13cSAndy Whitcroft my $FILE; 3816c72ffaaSAndy Whitcroft if ($file) { 38221caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 3836c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 38421caa13cSAndy Whitcroft } elsif ($filename eq '-') { 38521caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 3866c72ffaaSAndy Whitcroft } else { 38721caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 3886c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 3896c72ffaaSAndy Whitcroft } 390c2fdda0dSAndy Whitcroft if ($filename eq '-') { 391c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 392c2fdda0dSAndy Whitcroft } else { 393c2fdda0dSAndy Whitcroft $vname = $filename; 394c2fdda0dSAndy Whitcroft } 39521caa13cSAndy Whitcroft while (<$FILE>) { 3960a920b5bSAndy Whitcroft chomp; 39700df344fSAndy Whitcroft push(@rawlines, $_); 3986c72ffaaSAndy Whitcroft } 39921caa13cSAndy Whitcroft close($FILE); 400c2fdda0dSAndy Whitcroft if (!process($filename)) { 4010a920b5bSAndy Whitcroft $exit = 1; 4020a920b5bSAndy Whitcroft } 40300df344fSAndy Whitcroft @rawlines = (); 40413214adfSAndy Whitcroft @lines = (); 4050a920b5bSAndy Whitcroft} 4060a920b5bSAndy Whitcroft 4070a920b5bSAndy Whitcroftexit($exit); 4080a920b5bSAndy Whitcroft 4090a920b5bSAndy Whitcroftsub top_of_kernel_tree { 4106c72ffaaSAndy Whitcroft my ($root) = @_; 4116c72ffaaSAndy Whitcroft 4126c72ffaaSAndy Whitcroft my @tree_check = ( 4136c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 4146c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 4156c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 4166c72ffaaSAndy Whitcroft ); 4176c72ffaaSAndy Whitcroft 4186c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 4196c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 4200a920b5bSAndy Whitcroft return 0; 4210a920b5bSAndy Whitcroft } 4226c72ffaaSAndy Whitcroft } 4236c72ffaaSAndy Whitcroft return 1; 4246c72ffaaSAndy Whitcroft } 4250a920b5bSAndy Whitcroft 42620112475SJoe Perchessub parse_email { 42720112475SJoe Perches my ($formatted_email) = @_; 42820112475SJoe Perches 42920112475SJoe Perches my $name = ""; 43020112475SJoe Perches my $address = ""; 43120112475SJoe Perches my $comment = ""; 43220112475SJoe Perches 43320112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 43420112475SJoe Perches $name = $1; 43520112475SJoe Perches $address = $2; 43620112475SJoe Perches $comment = $3 if defined $3; 43720112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 43820112475SJoe Perches $address = $1; 43920112475SJoe Perches $comment = $2 if defined $2; 44020112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 44120112475SJoe Perches $address = $1; 44220112475SJoe Perches $comment = $2 if defined $2; 44320112475SJoe Perches $formatted_email =~ s/$address.*$//; 44420112475SJoe Perches $name = $formatted_email; 44520112475SJoe Perches $name =~ s/^\s+|\s+$//g; 44620112475SJoe Perches $name =~ s/^\"|\"$//g; 44720112475SJoe Perches # If there's a name left after stripping spaces and 44820112475SJoe Perches # leading quotes, and the address doesn't have both 44920112475SJoe Perches # leading and trailing angle brackets, the address 45020112475SJoe Perches # is invalid. ie: 45120112475SJoe Perches # "joe smith [email protected]" bad 45220112475SJoe Perches # "joe smith <[email protected]" bad 45320112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 45420112475SJoe Perches $name = ""; 45520112475SJoe Perches $address = ""; 45620112475SJoe Perches $comment = ""; 45720112475SJoe Perches } 45820112475SJoe Perches } 45920112475SJoe Perches 46020112475SJoe Perches $name =~ s/^\s+|\s+$//g; 46120112475SJoe Perches $name =~ s/^\"|\"$//g; 46220112475SJoe Perches $address =~ s/^\s+|\s+$//g; 46320112475SJoe Perches $address =~ s/^\<|\>$//g; 46420112475SJoe Perches 46520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 46620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 46720112475SJoe Perches $name = "\"$name\""; 46820112475SJoe Perches } 46920112475SJoe Perches 47020112475SJoe Perches return ($name, $address, $comment); 47120112475SJoe Perches} 47220112475SJoe Perches 47320112475SJoe Perchessub format_email { 47420112475SJoe Perches my ($name, $address) = @_; 47520112475SJoe Perches 47620112475SJoe Perches my $formatted_email; 47720112475SJoe Perches 47820112475SJoe Perches $name =~ s/^\s+|\s+$//g; 47920112475SJoe Perches $name =~ s/^\"|\"$//g; 48020112475SJoe Perches $address =~ s/^\s+|\s+$//g; 48120112475SJoe Perches 48220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 48320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 48420112475SJoe Perches $name = "\"$name\""; 48520112475SJoe Perches } 48620112475SJoe Perches 48720112475SJoe Perches if ("$name" eq "") { 48820112475SJoe Perches $formatted_email = "$address"; 48920112475SJoe Perches } else { 49020112475SJoe Perches $formatted_email = "$name <$address>"; 49120112475SJoe Perches } 49220112475SJoe Perches 49320112475SJoe Perches return $formatted_email; 49420112475SJoe Perches} 49520112475SJoe Perches 496000d1cc1SJoe Perchessub which_conf { 497000d1cc1SJoe Perches my ($conf) = @_; 498000d1cc1SJoe Perches 499000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 500000d1cc1SJoe Perches if (-e "$path/$conf") { 501000d1cc1SJoe Perches return "$path/$conf"; 502000d1cc1SJoe Perches } 503000d1cc1SJoe Perches } 504000d1cc1SJoe Perches 505000d1cc1SJoe Perches return ""; 506000d1cc1SJoe Perches} 507000d1cc1SJoe Perches 5080a920b5bSAndy Whitcroftsub expand_tabs { 5090a920b5bSAndy Whitcroft my ($str) = @_; 5100a920b5bSAndy Whitcroft 5110a920b5bSAndy Whitcroft my $res = ''; 5120a920b5bSAndy Whitcroft my $n = 0; 5130a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 5140a920b5bSAndy Whitcroft if ($c eq "\t") { 5150a920b5bSAndy Whitcroft $res .= ' '; 5160a920b5bSAndy Whitcroft $n++; 5170a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 5180a920b5bSAndy Whitcroft $res .= ' '; 5190a920b5bSAndy Whitcroft } 5200a920b5bSAndy Whitcroft next; 5210a920b5bSAndy Whitcroft } 5220a920b5bSAndy Whitcroft $res .= $c; 5230a920b5bSAndy Whitcroft $n++; 5240a920b5bSAndy Whitcroft } 5250a920b5bSAndy Whitcroft 5260a920b5bSAndy Whitcroft return $res; 5270a920b5bSAndy Whitcroft} 5286c72ffaaSAndy Whitcroftsub copy_spacing { 529773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 5306c72ffaaSAndy Whitcroft return $res; 5316c72ffaaSAndy Whitcroft} 5320a920b5bSAndy Whitcroft 5334a0df2efSAndy Whitcroftsub line_stats { 5344a0df2efSAndy Whitcroft my ($line) = @_; 5354a0df2efSAndy Whitcroft 5364a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 5374a0df2efSAndy Whitcroft $line =~ s/^.//; 5384a0df2efSAndy Whitcroft $line = expand_tabs($line); 5394a0df2efSAndy Whitcroft 5404a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 5414a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 5424a0df2efSAndy Whitcroft 5434a0df2efSAndy Whitcroft return (length($line), length($white)); 5444a0df2efSAndy Whitcroft} 5454a0df2efSAndy Whitcroft 546773647a0SAndy Whitcroftmy $sanitise_quote = ''; 547773647a0SAndy Whitcroft 548773647a0SAndy Whitcroftsub sanitise_line_reset { 549773647a0SAndy Whitcroft my ($in_comment) = @_; 550773647a0SAndy Whitcroft 551773647a0SAndy Whitcroft if ($in_comment) { 552773647a0SAndy Whitcroft $sanitise_quote = '*/'; 553773647a0SAndy Whitcroft } else { 554773647a0SAndy Whitcroft $sanitise_quote = ''; 555773647a0SAndy Whitcroft } 556773647a0SAndy Whitcroft} 55700df344fSAndy Whitcroftsub sanitise_line { 55800df344fSAndy Whitcroft my ($line) = @_; 55900df344fSAndy Whitcroft 56000df344fSAndy Whitcroft my $res = ''; 56100df344fSAndy Whitcroft my $l = ''; 56200df344fSAndy Whitcroft 563c2fdda0dSAndy Whitcroft my $qlen = 0; 564773647a0SAndy Whitcroft my $off = 0; 565773647a0SAndy Whitcroft my $c; 56600df344fSAndy Whitcroft 567773647a0SAndy Whitcroft # Always copy over the diff marker. 568773647a0SAndy Whitcroft $res = substr($line, 0, 1); 569773647a0SAndy Whitcroft 570773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 571773647a0SAndy Whitcroft $c = substr($line, $off, 1); 572773647a0SAndy Whitcroft 573773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 574773647a0SAndy Whitcroft # and end, all to $;. 575773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 576773647a0SAndy Whitcroft $sanitise_quote = '*/'; 577773647a0SAndy Whitcroft 578773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 579773647a0SAndy Whitcroft $off++; 58000df344fSAndy Whitcroft next; 581773647a0SAndy Whitcroft } 58281bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 583773647a0SAndy Whitcroft $sanitise_quote = ''; 584773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 585773647a0SAndy Whitcroft $off++; 586773647a0SAndy Whitcroft next; 587773647a0SAndy Whitcroft } 588113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 589113f04a8SDaniel Walker $sanitise_quote = '//'; 590113f04a8SDaniel Walker 591113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 592113f04a8SDaniel Walker $off++; 593113f04a8SDaniel Walker next; 594113f04a8SDaniel Walker } 595773647a0SAndy Whitcroft 596773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 597773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 598773647a0SAndy Whitcroft $c eq "\\") { 599773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 600773647a0SAndy Whitcroft $off++; 601773647a0SAndy Whitcroft next; 602773647a0SAndy Whitcroft } 603773647a0SAndy Whitcroft # Regular quotes. 604773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 605773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 606773647a0SAndy Whitcroft $sanitise_quote = $c; 607773647a0SAndy Whitcroft 608773647a0SAndy Whitcroft substr($res, $off, 1, $c); 609773647a0SAndy Whitcroft next; 610773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 611773647a0SAndy Whitcroft $sanitise_quote = ''; 61200df344fSAndy Whitcroft } 61300df344fSAndy Whitcroft } 614773647a0SAndy Whitcroft 615fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 616773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 617773647a0SAndy Whitcroft substr($res, $off, 1, $;); 618113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 619113f04a8SDaniel Walker substr($res, $off, 1, $;); 620773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 621773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 62200df344fSAndy Whitcroft } else { 623773647a0SAndy Whitcroft substr($res, $off, 1, $c); 62400df344fSAndy Whitcroft } 625c2fdda0dSAndy Whitcroft } 626c2fdda0dSAndy Whitcroft 627113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 628113f04a8SDaniel Walker $sanitise_quote = ''; 629113f04a8SDaniel Walker } 630113f04a8SDaniel Walker 631c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 632c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 633c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 634c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 635c2fdda0dSAndy Whitcroft 636c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 637c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 638c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 639c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 640c2fdda0dSAndy Whitcroft } 641c2fdda0dSAndy Whitcroft 64200df344fSAndy Whitcroft return $res; 64300df344fSAndy Whitcroft} 64400df344fSAndy Whitcroft 6458905a67cSAndy Whitcroftsub ctx_statement_block { 6468905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 6478905a67cSAndy Whitcroft my $line = $linenr - 1; 6488905a67cSAndy Whitcroft my $blk = ''; 6498905a67cSAndy Whitcroft my $soff = $off; 6508905a67cSAndy Whitcroft my $coff = $off - 1; 651773647a0SAndy Whitcroft my $coff_set = 0; 6528905a67cSAndy Whitcroft 65313214adfSAndy Whitcroft my $loff = 0; 65413214adfSAndy Whitcroft 6558905a67cSAndy Whitcroft my $type = ''; 6568905a67cSAndy Whitcroft my $level = 0; 657a2750645SAndy Whitcroft my @stack = (); 658cf655043SAndy Whitcroft my $p; 6598905a67cSAndy Whitcroft my $c; 6608905a67cSAndy Whitcroft my $len = 0; 66113214adfSAndy Whitcroft 66213214adfSAndy Whitcroft my $remainder; 6638905a67cSAndy Whitcroft while (1) { 664a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 665a2750645SAndy Whitcroft 666773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 6678905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 6688905a67cSAndy Whitcroft # context. 6698905a67cSAndy Whitcroft if ($off >= $len) { 6708905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 671dea33496SAndy Whitcroft last if (!defined $lines[$line]); 672c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 6738905a67cSAndy Whitcroft $remain--; 67413214adfSAndy Whitcroft $loff = $len; 675c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 6768905a67cSAndy Whitcroft $len = length($blk); 6778905a67cSAndy Whitcroft $line++; 6788905a67cSAndy Whitcroft last; 6798905a67cSAndy Whitcroft } 6808905a67cSAndy Whitcroft # Bail if there is no further context. 6818905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 68213214adfSAndy Whitcroft if ($off >= $len) { 6838905a67cSAndy Whitcroft last; 6848905a67cSAndy Whitcroft } 685f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 686f74bd194SAndy Whitcroft $level++; 687f74bd194SAndy Whitcroft $type = '#'; 688f74bd194SAndy Whitcroft } 6898905a67cSAndy Whitcroft } 690cf655043SAndy Whitcroft $p = $c; 6918905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 69213214adfSAndy Whitcroft $remainder = substr($blk, $off); 6938905a67cSAndy Whitcroft 694773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 6954635f4fbSAndy Whitcroft 6964635f4fbSAndy Whitcroft # Handle nested #if/#else. 6974635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 6984635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 6994635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 7004635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 7014635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 7024635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 7034635f4fbSAndy Whitcroft } 7044635f4fbSAndy Whitcroft 7058905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 7068905a67cSAndy Whitcroft # outermost level. 7078905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 7088905a67cSAndy Whitcroft last; 7098905a67cSAndy Whitcroft } 7108905a67cSAndy Whitcroft 71113214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 712773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 713773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 714773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 715773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 716773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 717773647a0SAndy Whitcroft $coff_set = 1; 718773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 719773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 72013214adfSAndy Whitcroft } 72113214adfSAndy Whitcroft 7228905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 7238905a67cSAndy Whitcroft $level++; 7248905a67cSAndy Whitcroft $type = '('; 7258905a67cSAndy Whitcroft } 7268905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 7278905a67cSAndy Whitcroft $level--; 7288905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 7298905a67cSAndy Whitcroft 7308905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 7318905a67cSAndy Whitcroft $coff = $off; 732773647a0SAndy Whitcroft $coff_set = 1; 733773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 7348905a67cSAndy Whitcroft } 7358905a67cSAndy Whitcroft } 7368905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 7378905a67cSAndy Whitcroft $level++; 7388905a67cSAndy Whitcroft $type = '{'; 7398905a67cSAndy Whitcroft } 7408905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 7418905a67cSAndy Whitcroft $level--; 7428905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 7438905a67cSAndy Whitcroft 7448905a67cSAndy Whitcroft if ($level == 0) { 745b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 746b998e001SPatrick Pannuto $off++; 747b998e001SPatrick Pannuto } 7488905a67cSAndy Whitcroft last; 7498905a67cSAndy Whitcroft } 7508905a67cSAndy Whitcroft } 751f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 752f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 753f74bd194SAndy Whitcroft $level--; 754f74bd194SAndy Whitcroft $type = ''; 755f74bd194SAndy Whitcroft $off++; 756f74bd194SAndy Whitcroft last; 757f74bd194SAndy Whitcroft } 7588905a67cSAndy Whitcroft $off++; 7598905a67cSAndy Whitcroft } 760a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 76113214adfSAndy Whitcroft if ($off == $len) { 762a3bb97a7SAndy Whitcroft $loff = $len + 1; 76313214adfSAndy Whitcroft $line++; 76413214adfSAndy Whitcroft $remain--; 76513214adfSAndy Whitcroft } 7668905a67cSAndy Whitcroft 7678905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 7688905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 7698905a67cSAndy Whitcroft 7708905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 7718905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 7728905a67cSAndy Whitcroft 773773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 77413214adfSAndy Whitcroft 77513214adfSAndy Whitcroft return ($statement, $condition, 77613214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 77713214adfSAndy Whitcroft} 77813214adfSAndy Whitcroft 779cf655043SAndy Whitcroftsub statement_lines { 780cf655043SAndy Whitcroft my ($stmt) = @_; 781cf655043SAndy Whitcroft 782cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 783cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 784cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 785cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 786cf655043SAndy Whitcroft 787cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 788cf655043SAndy Whitcroft 789cf655043SAndy Whitcroft return $#stmt_lines + 2; 790cf655043SAndy Whitcroft} 791cf655043SAndy Whitcroft 792cf655043SAndy Whitcroftsub statement_rawlines { 793cf655043SAndy Whitcroft my ($stmt) = @_; 794cf655043SAndy Whitcroft 795cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 796cf655043SAndy Whitcroft 797cf655043SAndy Whitcroft return $#stmt_lines + 2; 798cf655043SAndy Whitcroft} 799cf655043SAndy Whitcroft 800cf655043SAndy Whitcroftsub statement_block_size { 801cf655043SAndy Whitcroft my ($stmt) = @_; 802cf655043SAndy Whitcroft 803cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 804cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 805cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 806cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 807cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 808cf655043SAndy Whitcroft 809cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 810cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 811cf655043SAndy Whitcroft 812cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 813cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 814cf655043SAndy Whitcroft 815cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 816cf655043SAndy Whitcroft return $stmt_lines; 817cf655043SAndy Whitcroft } else { 818cf655043SAndy Whitcroft return $stmt_statements; 819cf655043SAndy Whitcroft } 820cf655043SAndy Whitcroft} 821cf655043SAndy Whitcroft 82213214adfSAndy Whitcroftsub ctx_statement_full { 82313214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 82413214adfSAndy Whitcroft my ($statement, $condition, $level); 82513214adfSAndy Whitcroft 82613214adfSAndy Whitcroft my (@chunks); 82713214adfSAndy Whitcroft 828cf655043SAndy Whitcroft # Grab the first conditional/block pair. 82913214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 83013214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 831773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 83213214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 833cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 834cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 835cf655043SAndy Whitcroft } 836cf655043SAndy Whitcroft 837cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 838cf655043SAndy Whitcroft # could continue the statement. 839cf655043SAndy Whitcroft for (;;) { 84013214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 84113214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 842cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 843773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 844cf655043SAndy Whitcroft #print "C: push\n"; 845cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 84613214adfSAndy Whitcroft } 84713214adfSAndy Whitcroft 84813214adfSAndy Whitcroft return ($level, $linenr, @chunks); 8498905a67cSAndy Whitcroft} 8508905a67cSAndy Whitcroft 8514a0df2efSAndy Whitcroftsub ctx_block_get { 852f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 8534a0df2efSAndy Whitcroft my $line; 8544a0df2efSAndy Whitcroft my $start = $linenr - 1; 8554a0df2efSAndy Whitcroft my $blk = ''; 8564a0df2efSAndy Whitcroft my @o; 8574a0df2efSAndy Whitcroft my @c; 8584a0df2efSAndy Whitcroft my @res = (); 8594a0df2efSAndy Whitcroft 860f0a594c1SAndy Whitcroft my $level = 0; 8614635f4fbSAndy Whitcroft my @stack = ($level); 86200df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 86300df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 86400df344fSAndy Whitcroft $remain--; 86500df344fSAndy Whitcroft 86600df344fSAndy Whitcroft $blk .= $rawlines[$line]; 8674635f4fbSAndy Whitcroft 8684635f4fbSAndy Whitcroft # Handle nested #if/#else. 86901464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 8704635f4fbSAndy Whitcroft push(@stack, $level); 87101464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 8724635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 87301464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 8744635f4fbSAndy Whitcroft $level = pop(@stack); 8754635f4fbSAndy Whitcroft } 8764635f4fbSAndy Whitcroft 87701464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 878f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 879f0a594c1SAndy Whitcroft if ($off > 0) { 880f0a594c1SAndy Whitcroft $off--; 881f0a594c1SAndy Whitcroft next; 882f0a594c1SAndy Whitcroft } 8834a0df2efSAndy Whitcroft 884f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 885f0a594c1SAndy Whitcroft $level--; 886f0a594c1SAndy Whitcroft last if ($level == 0); 887f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 888f0a594c1SAndy Whitcroft $level++; 889f0a594c1SAndy Whitcroft } 890f0a594c1SAndy Whitcroft } 8914a0df2efSAndy Whitcroft 892f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 89300df344fSAndy Whitcroft push(@res, $rawlines[$line]); 8944a0df2efSAndy Whitcroft } 8954a0df2efSAndy Whitcroft 896f0a594c1SAndy Whitcroft last if ($level == 0); 8974a0df2efSAndy Whitcroft } 8984a0df2efSAndy Whitcroft 899f0a594c1SAndy Whitcroft return ($level, @res); 9004a0df2efSAndy Whitcroft} 9014a0df2efSAndy Whitcroftsub ctx_block_outer { 9024a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 9034a0df2efSAndy Whitcroft 904f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 905f0a594c1SAndy Whitcroft return @r; 9064a0df2efSAndy Whitcroft} 9074a0df2efSAndy Whitcroftsub ctx_block { 9084a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 9094a0df2efSAndy Whitcroft 910f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 911f0a594c1SAndy Whitcroft return @r; 912653d4876SAndy Whitcroft} 913653d4876SAndy Whitcroftsub ctx_statement { 914f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 915f0a594c1SAndy Whitcroft 916f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 917f0a594c1SAndy Whitcroft return @r; 918f0a594c1SAndy Whitcroft} 919f0a594c1SAndy Whitcroftsub ctx_block_level { 920653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 921653d4876SAndy Whitcroft 922f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 9234a0df2efSAndy Whitcroft} 9249c0ca6f9SAndy Whitcroftsub ctx_statement_level { 9259c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 9269c0ca6f9SAndy Whitcroft 9279c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 9289c0ca6f9SAndy Whitcroft} 9294a0df2efSAndy Whitcroft 9304a0df2efSAndy Whitcroftsub ctx_locate_comment { 9314a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 9324a0df2efSAndy Whitcroft 9334a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 934beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 9354a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 9364a0df2efSAndy Whitcroft 9374a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 9384a0df2efSAndy Whitcroft # comment. 9394a0df2efSAndy Whitcroft my $in_comment = 0; 9404a0df2efSAndy Whitcroft $current_comment = ''; 9414a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 94200df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 94300df344fSAndy Whitcroft #warn " $line\n"; 9444a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 9454a0df2efSAndy Whitcroft $in_comment = 1; 9464a0df2efSAndy Whitcroft } 9474a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 9484a0df2efSAndy Whitcroft $in_comment = 1; 9494a0df2efSAndy Whitcroft } 9504a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 9514a0df2efSAndy Whitcroft $current_comment = ''; 9524a0df2efSAndy Whitcroft } 9534a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 9544a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 9554a0df2efSAndy Whitcroft $in_comment = 0; 9564a0df2efSAndy Whitcroft } 9574a0df2efSAndy Whitcroft } 9584a0df2efSAndy Whitcroft 9594a0df2efSAndy Whitcroft chomp($current_comment); 9604a0df2efSAndy Whitcroft return($current_comment); 9614a0df2efSAndy Whitcroft} 9624a0df2efSAndy Whitcroftsub ctx_has_comment { 9634a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 9644a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 9654a0df2efSAndy Whitcroft 96600df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 9674a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 9684a0df2efSAndy Whitcroft 9694a0df2efSAndy Whitcroft return ($cmt ne ''); 9704a0df2efSAndy Whitcroft} 9714a0df2efSAndy Whitcroft 9724d001e4dSAndy Whitcroftsub raw_line { 9734d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 9744d001e4dSAndy Whitcroft 9754d001e4dSAndy Whitcroft my $offset = $linenr - 1; 9764d001e4dSAndy Whitcroft $cnt++; 9774d001e4dSAndy Whitcroft 9784d001e4dSAndy Whitcroft my $line; 9794d001e4dSAndy Whitcroft while ($cnt) { 9804d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 9814d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 9824d001e4dSAndy Whitcroft $cnt--; 9834d001e4dSAndy Whitcroft } 9844d001e4dSAndy Whitcroft 9854d001e4dSAndy Whitcroft return $line; 9864d001e4dSAndy Whitcroft} 9874d001e4dSAndy Whitcroft 9880a920b5bSAndy Whitcroftsub cat_vet { 9890a920b5bSAndy Whitcroft my ($vet) = @_; 9909c0ca6f9SAndy Whitcroft my ($res, $coded); 9910a920b5bSAndy Whitcroft 9929c0ca6f9SAndy Whitcroft $res = ''; 9936c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 9946c72ffaaSAndy Whitcroft $res .= $1; 9956c72ffaaSAndy Whitcroft if ($2 ne '') { 9969c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 9976c72ffaaSAndy Whitcroft $res .= $coded; 9986c72ffaaSAndy Whitcroft } 9999c0ca6f9SAndy Whitcroft } 10009c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 10010a920b5bSAndy Whitcroft 10029c0ca6f9SAndy Whitcroft return $res; 10030a920b5bSAndy Whitcroft} 10040a920b5bSAndy Whitcroft 1005c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1006cf655043SAndy Whitcroftmy $av_pending; 1007c2fdda0dSAndy Whitcroftmy @av_paren_type; 10081f65f947SAndy Whitcroftmy $av_pend_colon; 1009c2fdda0dSAndy Whitcroft 1010c2fdda0dSAndy Whitcroftsub annotate_reset { 1011c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1012cf655043SAndy Whitcroft $av_pending = '_'; 1013cf655043SAndy Whitcroft @av_paren_type = ('E'); 10141f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1015c2fdda0dSAndy Whitcroft} 1016c2fdda0dSAndy Whitcroft 10176c72ffaaSAndy Whitcroftsub annotate_values { 10186c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 10196c72ffaaSAndy Whitcroft 10206c72ffaaSAndy Whitcroft my $res; 10211f65f947SAndy Whitcroft my $var = '_' x length($stream); 10226c72ffaaSAndy Whitcroft my $cur = $stream; 10236c72ffaaSAndy Whitcroft 1024c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 10256c72ffaaSAndy Whitcroft 10266c72ffaaSAndy Whitcroft while (length($cur)) { 1027773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1028cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1029171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 10306c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1031c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1032c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1033cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1034c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 10356c72ffaaSAndy Whitcroft } 10366c72ffaaSAndy Whitcroft 1037c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 10389446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 10399446ef56SAndy Whitcroft push(@av_paren_type, $type); 1040addcdceaSAndy Whitcroft $type = 'c'; 10419446ef56SAndy Whitcroft 1042e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1043c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 10446c72ffaaSAndy Whitcroft $type = 'T'; 10456c72ffaaSAndy Whitcroft 1046389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1047389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1048389a2fe5SAndy Whitcroft $type = 'T'; 1049389a2fe5SAndy Whitcroft 1050c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1051171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1052c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1053171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1054171ae1a4SAndy Whitcroft if ($2 ne '') { 1055cf655043SAndy Whitcroft $av_pending = 'N'; 1056171ae1a4SAndy Whitcroft } 1057171ae1a4SAndy Whitcroft $type = 'E'; 1058171ae1a4SAndy Whitcroft 1059c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1060171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1061171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1062171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 10636c72ffaaSAndy Whitcroft 1064c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1065cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1066c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1067cf655043SAndy Whitcroft 1068cf655043SAndy Whitcroft push(@av_paren_type, $type); 1069cf655043SAndy Whitcroft push(@av_paren_type, $type); 1070171ae1a4SAndy Whitcroft $type = 'E'; 1071cf655043SAndy Whitcroft 1072c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1073cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1074cf655043SAndy Whitcroft $av_preprocessor = 1; 1075cf655043SAndy Whitcroft 1076cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1077cf655043SAndy Whitcroft 1078171ae1a4SAndy Whitcroft $type = 'E'; 1079cf655043SAndy Whitcroft 1080c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1081cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1082cf655043SAndy Whitcroft 1083cf655043SAndy Whitcroft $av_preprocessor = 1; 1084cf655043SAndy Whitcroft 1085cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1086cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1087cf655043SAndy Whitcroft pop(@av_paren_type); 1088cf655043SAndy Whitcroft push(@av_paren_type, $type); 1089171ae1a4SAndy Whitcroft $type = 'E'; 10906c72ffaaSAndy Whitcroft 10916c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1092c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 10936c72ffaaSAndy Whitcroft 1094171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1095171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1096171ae1a4SAndy Whitcroft $av_pending = $type; 1097171ae1a4SAndy Whitcroft $type = 'N'; 1098171ae1a4SAndy Whitcroft 10996c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1100c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 11016c72ffaaSAndy Whitcroft if (defined $2) { 1102cf655043SAndy Whitcroft $av_pending = 'V'; 11036c72ffaaSAndy Whitcroft } 11046c72ffaaSAndy Whitcroft $type = 'N'; 11056c72ffaaSAndy Whitcroft 110614b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1107c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 110814b111c1SAndy Whitcroft $av_pending = 'E'; 11096c72ffaaSAndy Whitcroft $type = 'N'; 11106c72ffaaSAndy Whitcroft 11111f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 11121f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 11131f65f947SAndy Whitcroft $av_pend_colon = 'C'; 11141f65f947SAndy Whitcroft $type = 'N'; 11151f65f947SAndy Whitcroft 111614b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1117c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 11186c72ffaaSAndy Whitcroft $type = 'N'; 11196c72ffaaSAndy Whitcroft 11206c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1121c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1122cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1123cf655043SAndy Whitcroft $av_pending = '_'; 11246c72ffaaSAndy Whitcroft $type = 'N'; 11256c72ffaaSAndy Whitcroft 11266c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1127cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1128cf655043SAndy Whitcroft if ($new_type ne '_') { 1129cf655043SAndy Whitcroft $type = $new_type; 1130c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1131c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 11326c72ffaaSAndy Whitcroft } else { 1133c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 11346c72ffaaSAndy Whitcroft } 11356c72ffaaSAndy Whitcroft 1136c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1137c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1138c8cb2ca3SAndy Whitcroft $type = 'V'; 1139cf655043SAndy Whitcroft $av_pending = 'V'; 11406c72ffaaSAndy Whitcroft 11418e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 11428e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 11431f65f947SAndy Whitcroft $av_pend_colon = 'B'; 11448e761b04SAndy Whitcroft } elsif ($type eq 'E') { 11458e761b04SAndy Whitcroft $av_pend_colon = 'L'; 11461f65f947SAndy Whitcroft } 11471f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 11481f65f947SAndy Whitcroft $type = 'V'; 11491f65f947SAndy Whitcroft 11506c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1151c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 11526c72ffaaSAndy Whitcroft $type = 'V'; 11536c72ffaaSAndy Whitcroft 11546c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1155c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 11566c72ffaaSAndy Whitcroft $type = 'N'; 11576c72ffaaSAndy Whitcroft 1158cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1159c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 116013214adfSAndy Whitcroft $type = 'E'; 11611f65f947SAndy Whitcroft $av_pend_colon = 'O'; 116213214adfSAndy Whitcroft 11638e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 11648e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 11658e761b04SAndy Whitcroft $type = 'C'; 11668e761b04SAndy Whitcroft 11671f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 11681f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 11691f65f947SAndy Whitcroft $type = 'N'; 11701f65f947SAndy Whitcroft 11711f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 11721f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 11731f65f947SAndy Whitcroft 11741f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 11751f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 11761f65f947SAndy Whitcroft $type = 'E'; 11771f65f947SAndy Whitcroft } else { 11781f65f947SAndy Whitcroft $type = 'N'; 11791f65f947SAndy Whitcroft } 11801f65f947SAndy Whitcroft $av_pend_colon = 'O'; 11811f65f947SAndy Whitcroft 11828e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 118313214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 11846c72ffaaSAndy Whitcroft $type = 'N'; 11856c72ffaaSAndy Whitcroft 11860d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 118774048ed8SAndy Whitcroft my $variant; 118874048ed8SAndy Whitcroft 118974048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 119074048ed8SAndy Whitcroft if ($type eq 'V') { 119174048ed8SAndy Whitcroft $variant = 'B'; 119274048ed8SAndy Whitcroft } else { 119374048ed8SAndy Whitcroft $variant = 'U'; 119474048ed8SAndy Whitcroft } 119574048ed8SAndy Whitcroft 119674048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 119774048ed8SAndy Whitcroft $type = 'N'; 119874048ed8SAndy Whitcroft 11996c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1200c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 12016c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 12026c72ffaaSAndy Whitcroft $type = 'N'; 12036c72ffaaSAndy Whitcroft } 12046c72ffaaSAndy Whitcroft 12056c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1206c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 12076c72ffaaSAndy Whitcroft } 12086c72ffaaSAndy Whitcroft if (defined $1) { 12096c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 12106c72ffaaSAndy Whitcroft $res .= $type x length($1); 12116c72ffaaSAndy Whitcroft } 12126c72ffaaSAndy Whitcroft } 12136c72ffaaSAndy Whitcroft 12141f65f947SAndy Whitcroft return ($res, $var); 12156c72ffaaSAndy Whitcroft} 12166c72ffaaSAndy Whitcroft 12178905a67cSAndy Whitcroftsub possible { 121813214adfSAndy Whitcroft my ($possible, $line) = @_; 12199a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 12200776e594SAndy Whitcroft ^(?: 12210776e594SAndy Whitcroft $Modifier| 12220776e594SAndy Whitcroft $Storage| 12230776e594SAndy Whitcroft $Type| 12249a974fdbSAndy Whitcroft DEFINE_\S+ 12259a974fdbSAndy Whitcroft )$| 12269a974fdbSAndy Whitcroft ^(?: 12270776e594SAndy Whitcroft goto| 12280776e594SAndy Whitcroft return| 12290776e594SAndy Whitcroft case| 12300776e594SAndy Whitcroft else| 12310776e594SAndy Whitcroft asm|__asm__| 123289a88353SAndy Whitcroft do| 123389a88353SAndy Whitcroft \#| 123489a88353SAndy Whitcroft \#\#| 12359a974fdbSAndy Whitcroft )(?:\s|$)| 12360776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 12379a974fdbSAndy Whitcroft )}x; 12389a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 12399a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1240c45dcabdSAndy Whitcroft # Check for modifiers. 1241c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1242c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1243c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1244c45dcabdSAndy Whitcroft 1245c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1246c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1247d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 12489a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1249d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1250d2506586SAndy Whitcroft push(@modifierList, $modifier); 1251d2506586SAndy Whitcroft } 12529a974fdbSAndy Whitcroft } 1253c45dcabdSAndy Whitcroft 1254c45dcabdSAndy Whitcroft } else { 125513214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 12568905a67cSAndy Whitcroft push(@typeList, $possible); 1257c45dcabdSAndy Whitcroft } 12588905a67cSAndy Whitcroft build_types(); 12590776e594SAndy Whitcroft } else { 12600776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 12618905a67cSAndy Whitcroft } 12628905a67cSAndy Whitcroft} 12638905a67cSAndy Whitcroft 12646c72ffaaSAndy Whitcroftmy $prefix = ''; 12656c72ffaaSAndy Whitcroft 1266000d1cc1SJoe Perchessub show_type { 1267000d1cc1SJoe Perches return !defined $ignore_type{$_[0]}; 1268000d1cc1SJoe Perches} 1269000d1cc1SJoe Perches 1270f0a594c1SAndy Whitcroftsub report { 1271000d1cc1SJoe Perches if (!show_type($_[1]) || 1272000d1cc1SJoe Perches (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { 1273773647a0SAndy Whitcroft return 0; 1274773647a0SAndy Whitcroft } 1275000d1cc1SJoe Perches my $line; 1276000d1cc1SJoe Perches if ($show_types) { 1277000d1cc1SJoe Perches $line = "$prefix$_[0]:$_[1]: $_[2]\n"; 1278000d1cc1SJoe Perches } else { 1279000d1cc1SJoe Perches $line = "$prefix$_[0]: $_[2]\n"; 1280000d1cc1SJoe Perches } 12818905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 12828905a67cSAndy Whitcroft 128313214adfSAndy Whitcroft push(our @report, $line); 1284773647a0SAndy Whitcroft 1285773647a0SAndy Whitcroft return 1; 1286f0a594c1SAndy Whitcroft} 1287f0a594c1SAndy Whitcroftsub report_dump { 128813214adfSAndy Whitcroft our @report; 1289f0a594c1SAndy Whitcroft} 1290000d1cc1SJoe Perches 1291de7d4f0eSAndy Whitcroftsub ERROR { 1292000d1cc1SJoe Perches if (report("ERROR", $_[0], $_[1])) { 1293de7d4f0eSAndy Whitcroft our $clean = 0; 12946c72ffaaSAndy Whitcroft our $cnt_error++; 1295de7d4f0eSAndy Whitcroft } 1296773647a0SAndy Whitcroft} 1297de7d4f0eSAndy Whitcroftsub WARN { 1298000d1cc1SJoe Perches if (report("WARNING", $_[0], $_[1])) { 1299de7d4f0eSAndy Whitcroft our $clean = 0; 13006c72ffaaSAndy Whitcroft our $cnt_warn++; 1301de7d4f0eSAndy Whitcroft } 1302773647a0SAndy Whitcroft} 1303de7d4f0eSAndy Whitcroftsub CHK { 1304000d1cc1SJoe Perches if ($check && report("CHECK", $_[0], $_[1])) { 1305de7d4f0eSAndy Whitcroft our $clean = 0; 13066c72ffaaSAndy Whitcroft our $cnt_chk++; 13076c72ffaaSAndy Whitcroft } 1308de7d4f0eSAndy Whitcroft} 1309de7d4f0eSAndy Whitcroft 13106ecd9674SAndy Whitcroftsub check_absolute_file { 13116ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 13126ecd9674SAndy Whitcroft my $file = $absolute; 13136ecd9674SAndy Whitcroft 13146ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 13156ecd9674SAndy Whitcroft 13166ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 13176ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 13186ecd9674SAndy Whitcroft if (-f "$root/$file") { 13196ecd9674SAndy Whitcroft ##print "file<$file>\n"; 13206ecd9674SAndy Whitcroft last; 13216ecd9674SAndy Whitcroft } 13226ecd9674SAndy Whitcroft } 13236ecd9674SAndy Whitcroft if (! -f _) { 13246ecd9674SAndy Whitcroft return 0; 13256ecd9674SAndy Whitcroft } 13266ecd9674SAndy Whitcroft 13276ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 13286ecd9674SAndy Whitcroft my $prefix = $absolute; 13296ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 13306ecd9674SAndy Whitcroft 13316ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 13326ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1333000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1334000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 13356ecd9674SAndy Whitcroft } 13366ecd9674SAndy Whitcroft} 13376ecd9674SAndy Whitcroft 1338d1fe9c09SJoe Perchessub pos_last_openparen { 1339d1fe9c09SJoe Perches my ($line) = @_; 1340d1fe9c09SJoe Perches 1341d1fe9c09SJoe Perches my $pos = 0; 1342d1fe9c09SJoe Perches 1343d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1344d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1345d1fe9c09SJoe Perches 1346d1fe9c09SJoe Perches my $last_openparen = 0; 1347d1fe9c09SJoe Perches 1348d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1349d1fe9c09SJoe Perches return -1; 1350d1fe9c09SJoe Perches } 1351d1fe9c09SJoe Perches 1352d1fe9c09SJoe Perches my $len = length($line); 1353d1fe9c09SJoe Perches 1354d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1355d1fe9c09SJoe Perches my $string = substr($line, $pos); 1356d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1357d1fe9c09SJoe Perches $pos += length($1) - 1; 1358d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1359d1fe9c09SJoe Perches $last_openparen = $pos; 1360d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1361d1fe9c09SJoe Perches last; 1362d1fe9c09SJoe Perches } 1363d1fe9c09SJoe Perches } 1364d1fe9c09SJoe Perches 1365d1fe9c09SJoe Perches return $last_openparen + 1; 1366d1fe9c09SJoe Perches} 1367d1fe9c09SJoe Perches 13680a920b5bSAndy Whitcroftsub process { 13690a920b5bSAndy Whitcroft my $filename = shift; 13700a920b5bSAndy Whitcroft 13710a920b5bSAndy Whitcroft my $linenr=0; 13720a920b5bSAndy Whitcroft my $prevline=""; 1373c2fdda0dSAndy Whitcroft my $prevrawline=""; 13740a920b5bSAndy Whitcroft my $stashline=""; 1375c2fdda0dSAndy Whitcroft my $stashrawline=""; 13760a920b5bSAndy Whitcroft 13774a0df2efSAndy Whitcroft my $length; 13780a920b5bSAndy Whitcroft my $indent; 13790a920b5bSAndy Whitcroft my $previndent=0; 13800a920b5bSAndy Whitcroft my $stashindent=0; 13810a920b5bSAndy Whitcroft 1382de7d4f0eSAndy Whitcroft our $clean = 1; 13830a920b5bSAndy Whitcroft my $signoff = 0; 13840a920b5bSAndy Whitcroft my $is_patch = 0; 13850a920b5bSAndy Whitcroft 138615662b3eSJoe Perches my $in_header_lines = 1; 138715662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 138815662b3eSJoe Perches 138913214adfSAndy Whitcroft our @report = (); 13906c72ffaaSAndy Whitcroft our $cnt_lines = 0; 13916c72ffaaSAndy Whitcroft our $cnt_error = 0; 13926c72ffaaSAndy Whitcroft our $cnt_warn = 0; 13936c72ffaaSAndy Whitcroft our $cnt_chk = 0; 13946c72ffaaSAndy Whitcroft 13950a920b5bSAndy Whitcroft # Trace the real file/line as we go. 13960a920b5bSAndy Whitcroft my $realfile = ''; 13970a920b5bSAndy Whitcroft my $realline = 0; 13980a920b5bSAndy Whitcroft my $realcnt = 0; 13990a920b5bSAndy Whitcroft my $here = ''; 14000a920b5bSAndy Whitcroft my $in_comment = 0; 1401c2fdda0dSAndy Whitcroft my $comment_edge = 0; 14020a920b5bSAndy Whitcroft my $first_line = 0; 14031e855726SWolfram Sang my $p1_prefix = ''; 14040a920b5bSAndy Whitcroft 140513214adfSAndy Whitcroft my $prev_values = 'E'; 140613214adfSAndy Whitcroft 140713214adfSAndy Whitcroft # suppression flags 1408773647a0SAndy Whitcroft my %suppress_ifbraces; 1409170d3a22SAndy Whitcroft my %suppress_whiletrailers; 14102b474a1aSAndy Whitcroft my %suppress_export; 14113e469cdcSAndy Whitcroft my $suppress_statement = 0; 1412653d4876SAndy Whitcroft 1413c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1414de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1415c2fdda0dSAndy Whitcroft # 1416de7d4f0eSAndy Whitcroft my @setup_docs = (); 1417de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1418773647a0SAndy Whitcroft 1419773647a0SAndy Whitcroft sanitise_line_reset(); 1420c2fdda0dSAndy Whitcroft my $line; 1421c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1422773647a0SAndy Whitcroft $linenr++; 1423773647a0SAndy Whitcroft $line = $rawline; 1424c2fdda0dSAndy Whitcroft 1425773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1426de7d4f0eSAndy Whitcroft $setup_docs = 0; 1427de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1428de7d4f0eSAndy Whitcroft $setup_docs = 1; 1429de7d4f0eSAndy Whitcroft } 1430773647a0SAndy Whitcroft #next; 1431de7d4f0eSAndy Whitcroft } 1432773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1433773647a0SAndy Whitcroft $realline=$1-1; 1434773647a0SAndy Whitcroft if (defined $2) { 1435773647a0SAndy Whitcroft $realcnt=$3+1; 1436773647a0SAndy Whitcroft } else { 1437773647a0SAndy Whitcroft $realcnt=1+1; 1438773647a0SAndy Whitcroft } 1439c45dcabdSAndy Whitcroft $in_comment = 0; 1440773647a0SAndy Whitcroft 1441773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1442773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1443773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1444773647a0SAndy Whitcroft # at context start. 1445773647a0SAndy Whitcroft my $edge; 144601fa9147SAndy Whitcroft my $cnt = $realcnt; 144701fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 144801fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 144901fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 145001fa9147SAndy Whitcroft $cnt--; 145101fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1452721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1453fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1454fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1455fae17daeSAndy Whitcroft ($edge) = $1; 1456fae17daeSAndy Whitcroft last; 1457fae17daeSAndy Whitcroft } 1458773647a0SAndy Whitcroft } 1459773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1460773647a0SAndy Whitcroft $in_comment = 1; 1461773647a0SAndy Whitcroft } 1462773647a0SAndy Whitcroft 1463773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1464773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1465773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1466773647a0SAndy Whitcroft if (!defined $edge && 146783242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1468773647a0SAndy Whitcroft { 1469773647a0SAndy Whitcroft $in_comment = 1; 1470773647a0SAndy Whitcroft } 1471773647a0SAndy Whitcroft 1472773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1473773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1474773647a0SAndy Whitcroft 1475171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1476773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1477171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1478773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1479773647a0SAndy Whitcroft } 1480773647a0SAndy Whitcroft push(@lines, $line); 1481773647a0SAndy Whitcroft 1482773647a0SAndy Whitcroft if ($realcnt > 1) { 1483773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1484773647a0SAndy Whitcroft } else { 1485773647a0SAndy Whitcroft $realcnt = 0; 1486773647a0SAndy Whitcroft } 1487773647a0SAndy Whitcroft 1488773647a0SAndy Whitcroft #print "==>$rawline\n"; 1489773647a0SAndy Whitcroft #print "-->$line\n"; 1490de7d4f0eSAndy Whitcroft 1491de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1492de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1493de7d4f0eSAndy Whitcroft } 1494de7d4f0eSAndy Whitcroft } 1495de7d4f0eSAndy Whitcroft 14966c72ffaaSAndy Whitcroft $prefix = ''; 14976c72ffaaSAndy Whitcroft 1498773647a0SAndy Whitcroft $realcnt = 0; 1499773647a0SAndy Whitcroft $linenr = 0; 15000a920b5bSAndy Whitcroft foreach my $line (@lines) { 15010a920b5bSAndy Whitcroft $linenr++; 15020a920b5bSAndy Whitcroft 1503c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 15046c72ffaaSAndy Whitcroft 15050a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 15066c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 15070a920b5bSAndy Whitcroft $is_patch = 1; 15084a0df2efSAndy Whitcroft $first_line = $linenr + 1; 15090a920b5bSAndy Whitcroft $realline=$1-1; 15100a920b5bSAndy Whitcroft if (defined $2) { 15110a920b5bSAndy Whitcroft $realcnt=$3+1; 15120a920b5bSAndy Whitcroft } else { 15130a920b5bSAndy Whitcroft $realcnt=1+1; 15140a920b5bSAndy Whitcroft } 1515c2fdda0dSAndy Whitcroft annotate_reset(); 151613214adfSAndy Whitcroft $prev_values = 'E'; 151713214adfSAndy Whitcroft 1518773647a0SAndy Whitcroft %suppress_ifbraces = (); 1519170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 15202b474a1aSAndy Whitcroft %suppress_export = (); 15213e469cdcSAndy Whitcroft $suppress_statement = 0; 15220a920b5bSAndy Whitcroft next; 15230a920b5bSAndy Whitcroft 15244a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 15254a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 15264a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1527773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 15280a920b5bSAndy Whitcroft $realline++; 1529d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 15300a920b5bSAndy Whitcroft 15314a0df2efSAndy Whitcroft # Measure the line length and indent. 1532c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 15330a920b5bSAndy Whitcroft 15340a920b5bSAndy Whitcroft # Track the previous line. 15350a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 15360a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1537c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1538c2fdda0dSAndy Whitcroft 1539773647a0SAndy Whitcroft #warn "line<$line>\n"; 15406c72ffaaSAndy Whitcroft 1541d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1542d8aaf121SAndy Whitcroft $realcnt--; 15430a920b5bSAndy Whitcroft } 15440a920b5bSAndy Whitcroft 1545cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1546cc77cdcaSAndy Whitcroft 15470a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1548773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1549773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1550773647a0SAndy Whitcroft 15516c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 15526c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1553773647a0SAndy Whitcroft 1554773647a0SAndy Whitcroft # extract the filename as it passes 15553bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 15563bf9a009SRabin Vincent $realfile = $1; 15573bf9a009SRabin Vincent $realfile =~ s@^([^/]*)/@@; 1558270c49a0SJoe Perches $in_commit_log = 0; 15593bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1560773647a0SAndy Whitcroft $realfile = $1; 15611e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 1562270c49a0SJoe Perches $in_commit_log = 0; 15631e855726SWolfram Sang 15641e855726SWolfram Sang $p1_prefix = $1; 1565e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1566e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 1567000d1cc1SJoe Perches WARN("PATCH_PREFIX", 1568000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 15691e855726SWolfram Sang } 1570773647a0SAndy Whitcroft 1571c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1572000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 1573000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1574773647a0SAndy Whitcroft } 1575773647a0SAndy Whitcroft next; 1576773647a0SAndy Whitcroft } 1577773647a0SAndy Whitcroft 1578389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 15790a920b5bSAndy Whitcroft 1580c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1581c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1582c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 15830a920b5bSAndy Whitcroft 15846c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 15856c72ffaaSAndy Whitcroft 15863bf9a009SRabin Vincent# Check for incorrect file permissions 15873bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 15883bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 15893bf9a009SRabin Vincent if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) { 1590000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 1591000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 15923bf9a009SRabin Vincent } 15933bf9a009SRabin Vincent } 15943bf9a009SRabin Vincent 159520112475SJoe Perches# Check the patch for a signoff: 1596d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 15974a0df2efSAndy Whitcroft $signoff++; 159815662b3eSJoe Perches $in_commit_log = 0; 15990a920b5bSAndy Whitcroft } 160020112475SJoe Perches 160120112475SJoe Perches# Check signature styles 1602270c49a0SJoe Perches if (!$in_header_lines && 1603ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 160420112475SJoe Perches my $space_before = $1; 160520112475SJoe Perches my $sign_off = $2; 160620112475SJoe Perches my $space_after = $3; 160720112475SJoe Perches my $email = $4; 160820112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 160920112475SJoe Perches 1610ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 1611ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 1612ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 1613ce0338dfSJoe Perches } 161420112475SJoe Perches if (defined $space_before && $space_before ne "") { 1615000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1616000d1cc1SJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); 161720112475SJoe Perches } 161820112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 1619000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1620000d1cc1SJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); 162120112475SJoe Perches } 162220112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 1623000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1624000d1cc1SJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr); 162520112475SJoe Perches } 162620112475SJoe Perches 162720112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 162820112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 162920112475SJoe Perches if ($suggested_email eq "") { 1630000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 1631000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 163220112475SJoe Perches } else { 163320112475SJoe Perches my $dequoted = $suggested_email; 163420112475SJoe Perches $dequoted =~ s/^"//; 163520112475SJoe Perches $dequoted =~ s/" </ </; 163620112475SJoe Perches # Don't force email to have quotes 163720112475SJoe Perches # Allow just an angle bracketed address 163820112475SJoe Perches if ("$dequoted$comment" ne $email && 163920112475SJoe Perches "<$email_address>$comment" ne $email && 164020112475SJoe Perches "$suggested_email$comment" ne $email) { 1641000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1642000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 164320112475SJoe Perches } 16440a920b5bSAndy Whitcroft } 16450a920b5bSAndy Whitcroft } 16460a920b5bSAndy Whitcroft 164700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 16488905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1649000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 1650000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 16516c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1652de7d4f0eSAndy Whitcroft } 1653de7d4f0eSAndy Whitcroft 16546ecd9674SAndy Whitcroft# Check for absolute kernel paths. 16556ecd9674SAndy Whitcroft if ($tree) { 16566ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 16576ecd9674SAndy Whitcroft my $file = $1; 16586ecd9674SAndy Whitcroft 16596ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 16606ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 16616ecd9674SAndy Whitcroft # 16626ecd9674SAndy Whitcroft } else { 16636ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 16646ecd9674SAndy Whitcroft } 16656ecd9674SAndy Whitcroft } 16666ecd9674SAndy Whitcroft } 16676ecd9674SAndy Whitcroft 1668de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1669de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1670171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1671171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1672171ae1a4SAndy Whitcroft 1673171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1674171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1675171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1676171ae1a4SAndy Whitcroft 167734d99219SJoe Perches CHK("INVALID_UTF8", 1678000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 167900df344fSAndy Whitcroft } 16800a920b5bSAndy Whitcroft 168115662b3eSJoe Perches# Check if it's the start of a commit log 168215662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 168315662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 1684270c49a0SJoe Perches $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { 168515662b3eSJoe Perches $in_header_lines = 0; 168615662b3eSJoe Perches $in_commit_log = 1; 168715662b3eSJoe Perches } 168815662b3eSJoe Perches 168915662b3eSJoe Perches# Still not yet in a patch, check for any UTF-8 169015662b3eSJoe Perches if ($in_commit_log && $realfile =~ /^$/ && 169115662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 169215662b3eSJoe Perches CHK("UTF8_BEFORE_PATCH", 169315662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 169415662b3eSJoe Perches } 169515662b3eSJoe Perches 169630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 169730670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 169800df344fSAndy Whitcroft 16990a920b5bSAndy Whitcroft#trailing whitespace 17009c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1701c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1702000d1cc1SJoe Perches ERROR("DOS_LINE_ENDINGS", 1703000d1cc1SJoe Perches "DOS line endings\n" . $herevet); 17049c0ca6f9SAndy Whitcroft 1705c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1706c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1707000d1cc1SJoe Perches ERROR("TRAILING_WHITESPACE", 1708000d1cc1SJoe Perches "trailing whitespace\n" . $herevet); 1709d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 17100a920b5bSAndy Whitcroft } 17115368df20SAndy Whitcroft 17123354957aSAndi Kleen# check for Kconfig help text having a real description 17139fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 17149fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 17153354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 1716a1385803SAndy Whitcroft $line =~ /.\s*config\s+/) { 17173354957aSAndi Kleen my $length = 0; 17189fe287d7SAndy Whitcroft my $cnt = $realcnt; 17199fe287d7SAndy Whitcroft my $ln = $linenr + 1; 17209fe287d7SAndy Whitcroft my $f; 1721a1385803SAndy Whitcroft my $is_start = 0; 17229fe287d7SAndy Whitcroft my $is_end = 0; 1723a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 17249fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 17259fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 17269fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 17279fe287d7SAndy Whitcroft 17289fe287d7SAndy Whitcroft next if ($f =~ /^-/); 1729a1385803SAndy Whitcroft 1730a1385803SAndy Whitcroft if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { 1731a1385803SAndy Whitcroft $is_start = 1; 1732a1385803SAndy Whitcroft } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { 1733a1385803SAndy Whitcroft $length = -1; 1734a1385803SAndy Whitcroft } 1735a1385803SAndy Whitcroft 17369fe287d7SAndy Whitcroft $f =~ s/^.//; 17373354957aSAndi Kleen $f =~ s/#.*//; 17383354957aSAndi Kleen $f =~ s/^\s+//; 17393354957aSAndi Kleen next if ($f =~ /^$/); 17409fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 17419fe287d7SAndy Whitcroft $is_end = 1; 17429fe287d7SAndy Whitcroft last; 17439fe287d7SAndy Whitcroft } 17443354957aSAndi Kleen $length++; 17453354957aSAndi Kleen } 1746000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 1747a1385803SAndy Whitcroft "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 1748a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 17493354957aSAndi Kleen } 17503354957aSAndi Kleen 1751c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 1752c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 1753c68e5878SArnaud Lacombe my $flag = $1; 1754c68e5878SArnaud Lacombe my $replacement = { 1755c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 1756c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 1757c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 1758c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 1759c68e5878SArnaud Lacombe }; 1760c68e5878SArnaud Lacombe 1761c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 1762c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 1763c68e5878SArnaud Lacombe } 1764c68e5878SArnaud Lacombe 17655368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 17665368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 17675368df20SAndy Whitcroft 17680a920b5bSAndy Whitcroft#80 column limit 1769c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1770f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 17710fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 17728bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1773f4c014c0SAndy Whitcroft $length > 80) 1774c45dcabdSAndy Whitcroft { 1775000d1cc1SJoe Perches WARN("LONG_LINE", 1776000d1cc1SJoe Perches "line over 80 characters\n" . $herecurr); 17770a920b5bSAndy Whitcroft } 17780a920b5bSAndy Whitcroft 1779ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability 1780ca56dc09SJosh Triplett# to grep for the string. Limited to strings used as parameters (those 1781ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false 1782ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per 1783ca56dc09SJosh Triplett# line of the string. Make an exception when the previous string ends in a 1784ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline 1785ca56dc09SJosh Triplett# assembly to indent the instruction on the following line). 1786ca56dc09SJosh Triplett if ($line =~ /^\+\s*"/ && 1787ca56dc09SJosh Triplett $prevline =~ /"\s*$/ && 1788ca56dc09SJosh Triplett $prevline =~ /\(/ && 1789ca56dc09SJosh Triplett $prevrawline !~ /\\n(?:\\t)*"\s*$/) { 1790ca56dc09SJosh Triplett WARN("SPLIT_STRING", 1791ca56dc09SJosh Triplett "quoted string split across lines\n" . $hereprev); 1792ca56dc09SJosh Triplett } 1793ca56dc09SJosh Triplett 17945e79d96eSJoe Perches# check for spaces before a quoted newline 17955e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 1796000d1cc1SJoe Perches WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 1797000d1cc1SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr); 17985e79d96eSJoe Perches } 17995e79d96eSJoe Perches 18008905a67cSAndy Whitcroft# check for adding lines without a newline. 18018905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 1802000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 1803000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 18048905a67cSAndy Whitcroft } 18058905a67cSAndy Whitcroft 180642e41c54SMike Frysinger# Blackfin: use hi/lo macros 180742e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 180842e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 180942e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1810000d1cc1SJoe Perches ERROR("LO_MACRO", 1811000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 181242e41c54SMike Frysinger } 181342e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 181442e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1815000d1cc1SJoe Perches ERROR("HI_MACRO", 1816000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 181742e41c54SMike Frysinger } 181842e41c54SMike Frysinger } 181942e41c54SMike Frysinger 1820b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 1821b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 18220a920b5bSAndy Whitcroft 18230a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 18240a920b5bSAndy Whitcroft# more than 8 must use tabs. 1825c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1826c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1827c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1828000d1cc1SJoe Perches ERROR("CODE_INDENT", 1829000d1cc1SJoe Perches "code indent should use tabs where possible\n" . $herevet); 1830d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 18310a920b5bSAndy Whitcroft } 18320a920b5bSAndy Whitcroft 183308e44365SAlberto Panizzo# check for space before tabs. 183408e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 183508e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1836000d1cc1SJoe Perches WARN("SPACE_BEFORE_TAB", 1837000d1cc1SJoe Perches "please, no space before tabs\n" . $herevet); 183808e44365SAlberto Panizzo } 183908e44365SAlberto Panizzo 1840d1fe9c09SJoe Perches# check for && or || at the start of a line 1841d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 1842d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 1843d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 1844d1fe9c09SJoe Perches } 1845d1fe9c09SJoe Perches 1846d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 1847d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 1848d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { 1849d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 1850d1fe9c09SJoe Perches my $oldindent = $1; 1851d1fe9c09SJoe Perches my $rest = $2; 1852d1fe9c09SJoe Perches 1853d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 1854d1fe9c09SJoe Perches if ($pos >= 0) { 1855b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 1856b34a26f3SJoe Perches my $newindent = $2; 1857d1fe9c09SJoe Perches 1858d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 1859d1fe9c09SJoe Perches "\t" x ($pos / 8) . 1860d1fe9c09SJoe Perches " " x ($pos % 8); 1861d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 1862d1fe9c09SJoe Perches 1863d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 1864d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 1865d1fe9c09SJoe Perches CHK("PARENTHESIS_ALIGNMENT", 1866d1fe9c09SJoe Perches "Alignment should match open parenthesis\n" . $hereprev); 1867d1fe9c09SJoe Perches } 1868d1fe9c09SJoe Perches } 1869d1fe9c09SJoe Perches } 1870d1fe9c09SJoe Perches 1871aad4f614SJoe Perches if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) { 1872aad4f614SJoe Perches CHK("SPACING", 1873aad4f614SJoe Perches "No space is necessary after a cast\n" . $hereprev); 1874aad4f614SJoe Perches } 1875aad4f614SJoe Perches 18765f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 18776b4c5bebSAndy Whitcroft# Exceptions: 18786b4c5bebSAndy Whitcroft# 1) within comments 18796b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 18806b4c5bebSAndy Whitcroft# 3) hanging labels 18816b4c5bebSAndy Whitcroft if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { 18825f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1883000d1cc1SJoe Perches WARN("LEADING_SPACE", 1884000d1cc1SJoe Perches "please, no spaces at the start of a line\n" . $herevet); 18855f7ddae6SRaffaele Recalcati } 18865f7ddae6SRaffaele Recalcati 1887b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 1888b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 1889b9ea10d6SAndy Whitcroft 1890c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1891cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1892000d1cc1SJoe Perches WARN("CVS_KEYWORD", 1893000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1894c2fdda0dSAndy Whitcroft } 189522f2a2efSAndy Whitcroft 189642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 189742e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 189842e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1899000d1cc1SJoe Perches ERROR("CSYNC", 1900000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 190142e41c54SMike Frysinger } 190242e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 190342e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 1904000d1cc1SJoe Perches ERROR("SSYNC", 1905000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 190642e41c54SMike Frysinger } 190742e41c54SMike Frysinger 19089c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 19092b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 19102b474a1aSAndy Whitcroft $realline_next); 19113e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 19123e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 19133e469cdcSAndy Whitcroft $realcnt && $line =~ /.\s*\S/) { 1914170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1915f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 1916171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1917171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1918171ae1a4SAndy Whitcroft 19193e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 19203e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 19213e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 19223e469cdcSAndy Whitcroft # until we hit end of it. 19233e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 19243e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 19253e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 19263e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 19273e469cdcSAndy Whitcroft } 1928f74bd194SAndy Whitcroft 19292b474a1aSAndy Whitcroft # Find the real next line. 19302b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 19312b474a1aSAndy Whitcroft if (defined $realline_next && 19322b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 19332b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 19342b474a1aSAndy Whitcroft $realline_next++; 19352b474a1aSAndy Whitcroft } 19362b474a1aSAndy Whitcroft 1937171ae1a4SAndy Whitcroft my $s = $stat; 1938171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1939cf655043SAndy Whitcroft 1940c2fdda0dSAndy Whitcroft # Ignore goto labels. 1941171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1942c2fdda0dSAndy Whitcroft 1943c2fdda0dSAndy Whitcroft # Ignore functions being called 1944171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1945c2fdda0dSAndy Whitcroft 1946463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 1947463f2864SAndy Whitcroft 1948c45dcabdSAndy Whitcroft # declarations always start with types 1949d2506586SAndy 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) { 1950c45dcabdSAndy Whitcroft my $type = $1; 1951c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1952c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1953c45dcabdSAndy Whitcroft 19546c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1955a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1956c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1957c2fdda0dSAndy Whitcroft } 19588905a67cSAndy Whitcroft 19596c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 196065863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1961c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 19629c0ca6f9SAndy Whitcroft } 19638905a67cSAndy Whitcroft 19648905a67cSAndy Whitcroft # Check for any sort of function declaration. 19658905a67cSAndy Whitcroft # int foo(something bar, other baz); 19668905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1967171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 19688905a67cSAndy Whitcroft my ($name_len) = length($1); 19698905a67cSAndy Whitcroft 1970cf655043SAndy Whitcroft my $ctx = $s; 1971773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 19728905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1973cf655043SAndy Whitcroft 19748905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1975c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 19768905a67cSAndy Whitcroft 1977c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 19788905a67cSAndy Whitcroft } 19798905a67cSAndy Whitcroft } 19808905a67cSAndy Whitcroft } 19818905a67cSAndy Whitcroft 19829c0ca6f9SAndy Whitcroft } 19839c0ca6f9SAndy Whitcroft 198400df344fSAndy Whitcroft# 198500df344fSAndy Whitcroft# Checks which may be anchored in the context. 198600df344fSAndy Whitcroft# 198700df344fSAndy Whitcroft 198800df344fSAndy Whitcroft# Check for switch () and associated case and default 198900df344fSAndy Whitcroft# statements should be at the same indent. 199000df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 199100df344fSAndy Whitcroft my $err = ''; 199200df344fSAndy Whitcroft my $sep = ''; 199300df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 199400df344fSAndy Whitcroft shift(@ctx); 199500df344fSAndy Whitcroft for my $ctx (@ctx) { 199600df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 199700df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 199800df344fSAndy Whitcroft $indent != $cindent) { 199900df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 200000df344fSAndy Whitcroft $sep = ''; 200100df344fSAndy Whitcroft } else { 200200df344fSAndy Whitcroft $sep = "[...]\n"; 200300df344fSAndy Whitcroft } 200400df344fSAndy Whitcroft } 200500df344fSAndy Whitcroft if ($err ne '') { 2006000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2007000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2008de7d4f0eSAndy Whitcroft } 2009de7d4f0eSAndy Whitcroft } 2010de7d4f0eSAndy Whitcroft 2011de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2012de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 2013c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2014773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2015773647a0SAndy Whitcroft 20169c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 20178eef05ddSJoe Perches 20188eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 20198eef05ddSJoe Perches WARN("DEEP_INDENTATION", 20208eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 20218eef05ddSJoe Perches } 20228eef05ddSJoe Perches 2023de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2024de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 2025de7d4f0eSAndy Whitcroft 2026548596d5SAndy Whitcroft my $ctx_ln = $linenr; 2027548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 2028de7d4f0eSAndy Whitcroft 2029548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2030548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 2031548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 2032548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2033548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2034773647a0SAndy Whitcroft $ctx_ln++; 2035773647a0SAndy Whitcroft } 2036548596d5SAndy Whitcroft 203753210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 203853210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2039773647a0SAndy Whitcroft 2040773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 2041000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2042000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 204301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 204400df344fSAndy Whitcroft } 2045773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2046773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 2047773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 2048773647a0SAndy Whitcroft { 20499c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 20509c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 2051000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 2052000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 205301464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 20549c0ca6f9SAndy Whitcroft } 20559c0ca6f9SAndy Whitcroft } 205600df344fSAndy Whitcroft } 205700df344fSAndy Whitcroft 20584d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 20594d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 20603e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 20613e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 20623e469cdcSAndy Whitcroft if (!defined $stat); 20634d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 20644d001e4dSAndy Whitcroft 20654d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 20664d001e4dSAndy Whitcroft 20674d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 20684d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 20694d001e4dSAndy Whitcroft # where necessary. 20704d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 20714d001e4dSAndy Whitcroft 20724d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 20736f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 20746f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 20754d001e4dSAndy Whitcroft 20764d001e4dSAndy Whitcroft # We want to check the first line inside the block 20774d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 20784d001e4dSAndy Whitcroft # 1) any blank line termination 20794d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 20804d001e4dSAndy Whitcroft # 3) any do (...) { 20814d001e4dSAndy Whitcroft my $continuation = 0; 20824d001e4dSAndy Whitcroft my $check = 0; 20834d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 20844d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 20854d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 20864d001e4dSAndy Whitcroft $continuation = 1; 20874d001e4dSAndy Whitcroft } 20889bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 20894d001e4dSAndy Whitcroft $check = 1; 20904d001e4dSAndy Whitcroft $cond_lines++; 20914d001e4dSAndy Whitcroft } 20924d001e4dSAndy Whitcroft 20934d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 20944d001e4dSAndy Whitcroft # preprocessor statement. 20954d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 20964d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 20974d001e4dSAndy Whitcroft $check = 0; 20984d001e4dSAndy Whitcroft } 20994d001e4dSAndy Whitcroft 21009bd49efeSAndy Whitcroft my $cond_ptr = -1; 2101740504c6SAndy Whitcroft $continuation = 0; 21029bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 21039bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 21044d001e4dSAndy Whitcroft 2105f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 2106f16fa28fSAndy Whitcroft # is not linear. 2107f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2108f16fa28fSAndy Whitcroft $check = 0; 2109f16fa28fSAndy Whitcroft } 2110f16fa28fSAndy Whitcroft 21119bd49efeSAndy Whitcroft # Ignore: 21129bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 21139bd49efeSAndy Whitcroft # 2) preprocessor lines, and 21149bd49efeSAndy Whitcroft # 3) labels. 2115740504c6SAndy Whitcroft if ($continuation || 2116740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 21179bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 21189bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 2119740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 212030dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 21219bd49efeSAndy Whitcroft $cond_lines++; 21229bd49efeSAndy Whitcroft } 21234d001e4dSAndy Whitcroft } 212430dad6ebSAndy Whitcroft } 21254d001e4dSAndy Whitcroft 21264d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 21274d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 21284d001e4dSAndy Whitcroft 21294d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 21304d001e4dSAndy Whitcroft # this is not this patch's fault. 21314d001e4dSAndy Whitcroft if (!defined($stat_real) || 21324d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 21334d001e4dSAndy Whitcroft $check = 0; 21344d001e4dSAndy Whitcroft } 21354d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 21364d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 21374d001e4dSAndy Whitcroft } 21384d001e4dSAndy Whitcroft 21399bd49efeSAndy 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"; 21404d001e4dSAndy Whitcroft 21414d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 21424d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 2143000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 2144000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 21454d001e4dSAndy Whitcroft } 21464d001e4dSAndy Whitcroft } 21474d001e4dSAndy Whitcroft 21486c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 21496c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 21501f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 21511f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 21526c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 2153c2fdda0dSAndy Whitcroft if ($dbg_values) { 2154c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 2155cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 2156cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 21571f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 2158c2fdda0dSAndy Whitcroft } 21596c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 21606c72ffaaSAndy Whitcroft 216100df344fSAndy Whitcroft#ignore lines not being added 216200df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 216300df344fSAndy Whitcroft 2164653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 21657429c690SAndy Whitcroft if ($dbg_type) { 21667429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 2167000d1cc1SJoe Perches ERROR("TEST_TYPE", 2168000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 21697429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2170000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 2171000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 21727429c690SAndy Whitcroft } 2173653d4876SAndy Whitcroft next; 2174653d4876SAndy Whitcroft } 2175a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2176a1ef277eSAndy Whitcroft if ($dbg_attr) { 21779360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2178000d1cc1SJoe Perches ERROR("TEST_ATTR", 2179000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 21809360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2181000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2182000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2183a1ef277eSAndy Whitcroft } 2184a1ef277eSAndy Whitcroft next; 2185a1ef277eSAndy Whitcroft } 2186653d4876SAndy Whitcroft 2187f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 218899423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 218999423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2190000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2191000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . $hereprev); 2192f0a594c1SAndy Whitcroft } 2193f0a594c1SAndy Whitcroft 219400df344fSAndy Whitcroft# 219500df344fSAndy Whitcroft# Checks which are anchored on the added line. 219600df344fSAndy Whitcroft# 219700df344fSAndy Whitcroft 2198653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2199c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2200653d4876SAndy Whitcroft my $path = $1; 2201653d4876SAndy Whitcroft if ($path =~ m{//}) { 2202000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2203000d1cc1SJoe Perches "malformed #include filename\n" . 2204de7d4f0eSAndy Whitcroft $herecurr); 2205653d4876SAndy Whitcroft } 2206653d4876SAndy Whitcroft } 2207653d4876SAndy Whitcroft 220800df344fSAndy Whitcroft# no C99 // comments 220900df344fSAndy Whitcroft if ($line =~ m{//}) { 2210000d1cc1SJoe Perches ERROR("C99_COMMENTS", 2211000d1cc1SJoe Perches "do not use C99 // comments\n" . $herecurr); 221200df344fSAndy Whitcroft } 221300df344fSAndy Whitcroft # Remove C99 comments. 22140a920b5bSAndy Whitcroft $line =~ s@//.*@@; 22156c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 22160a920b5bSAndy Whitcroft 22172b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 22182b474a1aSAndy Whitcroft# the whole statement. 22192b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 22202b474a1aSAndy Whitcroft if (defined $realline_next && 22212b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 22222b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 22232b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 22242b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 22253cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 22263cbf62dfSAndy Whitcroft # a prefix: 22273cbf62dfSAndy Whitcroft # XXX(foo); 22283cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 2229653d4876SAndy Whitcroft my $name = $1; 223087a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 22313cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 22323cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 22333cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 22343cbf62dfSAndy Whitcroft 22353cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 22362b474a1aSAndy Whitcroft \n.}\s*$| 223748012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 223848012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 223948012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 22402b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 22412b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 224248012058SAndy Whitcroft )/x) { 22432b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 22442b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 22452b474a1aSAndy Whitcroft } else { 22462b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 22470a920b5bSAndy Whitcroft } 22480a920b5bSAndy Whitcroft } 22492b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 22502b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 22512b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 22522b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 22532b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 22542b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 22552b474a1aSAndy Whitcroft } 22562b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 22572b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 2258000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 2259000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 22602b474a1aSAndy Whitcroft } 22610a920b5bSAndy Whitcroft 22625150bda4SJoe Eloff# check for global initialisers. 2263c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 2264000d1cc1SJoe Perches ERROR("GLOBAL_INITIALISERS", 2265000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 2266f0a594c1SAndy Whitcroft $herecurr); 2267f0a594c1SAndy Whitcroft } 22680a920b5bSAndy Whitcroft# check for static initialisers. 22692d1bafd7SAndy Whitcroft if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2270000d1cc1SJoe Perches ERROR("INITIALISED_STATIC", 2271000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 2272de7d4f0eSAndy Whitcroft $herecurr); 22730a920b5bSAndy Whitcroft } 22740a920b5bSAndy Whitcroft 2275cb710ecaSJoe Perches# check for static const char * arrays. 2276cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 2277000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2278000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 2279cb710ecaSJoe Perches $herecurr); 2280cb710ecaSJoe Perches } 2281cb710ecaSJoe Perches 2282cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 2283cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 2284000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2285000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 2286cb710ecaSJoe Perches $herecurr); 2287cb710ecaSJoe Perches } 2288cb710ecaSJoe Perches 228993ed0e2dSJoe Perches# check for declarations of struct pci_device_id 229093ed0e2dSJoe Perches if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { 2291000d1cc1SJoe Perches WARN("DEFINE_PCI_DEVICE_TABLE", 2292000d1cc1SJoe Perches "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); 229393ed0e2dSJoe Perches } 229493ed0e2dSJoe Perches 2295653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 2296653d4876SAndy Whitcroft# make sense. 2297653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 22988054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 2299c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 23008ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 2301653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 2302000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 2303000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 23040a920b5bSAndy Whitcroft } 23050a920b5bSAndy Whitcroft 23060a920b5bSAndy Whitcroft# * goes on variable not on type 230765863862SAndy Whitcroft # (char*[ const]) 2308bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2309bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 2310bfcb2cc7SAndy Whitcroft my ($from, $to) = ($2, $2); 2311d8aaf121SAndy Whitcroft 231265863862SAndy Whitcroft # Should start with a space. 231365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 231465863862SAndy Whitcroft # Should not end with a space. 231565863862SAndy Whitcroft $to =~ s/\s+$//; 231665863862SAndy Whitcroft # '*'s should not have spaces between. 2317f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 231865863862SAndy Whitcroft } 2319d8aaf121SAndy Whitcroft 232065863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 232165863862SAndy Whitcroft if ($from ne $to) { 2322000d1cc1SJoe Perches ERROR("POINTER_LOCATION", 2323000d1cc1SJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 232465863862SAndy Whitcroft } 2325bfcb2cc7SAndy Whitcroft } 2326bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2327bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 2328bfcb2cc7SAndy Whitcroft my ($from, $to, $ident) = ($2, $2, $3); 2329d8aaf121SAndy Whitcroft 233065863862SAndy Whitcroft # Should start with a space. 233165863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 233265863862SAndy Whitcroft # Should not end with a space. 233365863862SAndy Whitcroft $to =~ s/\s+$//; 233465863862SAndy Whitcroft # '*'s should not have spaces between. 2335f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 233665863862SAndy Whitcroft } 233765863862SAndy Whitcroft # Modifiers should have spaces. 233865863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 233965863862SAndy Whitcroft 2340667026e7SAndy Whitcroft #print "from<$from> to<$to> ident<$ident>\n"; 2341667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 2342000d1cc1SJoe Perches ERROR("POINTER_LOCATION", 2343000d1cc1SJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 234465863862SAndy Whitcroft } 23450a920b5bSAndy Whitcroft } 23460a920b5bSAndy Whitcroft 23470a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 23480a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 23490a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 23500a920b5bSAndy Whitcroft# print "$herecurr"; 23510a920b5bSAndy Whitcroft# $clean = 0; 23520a920b5bSAndy Whitcroft# } 23530a920b5bSAndy Whitcroft 23548905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 2355000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 2356000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 23578905a67cSAndy Whitcroft } 23588905a67cSAndy Whitcroft 235917441227SJoe Perches# check for uses of printk_ratelimit 236017441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 2361000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 2362000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 236317441227SJoe Perches } 236417441227SJoe Perches 236500df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 236600df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 236700df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 236825985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 236900df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 2370f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 237100df344fSAndy Whitcroft my $ok = 0; 237200df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 237300df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 237425985edcSLucas De Marchi # we have a preceding printk if it ends 237500df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 237600df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 237700df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 237800df344fSAndy Whitcroft $ok = 1; 237900df344fSAndy Whitcroft } 238000df344fSAndy Whitcroft last; 238100df344fSAndy Whitcroft } 238200df344fSAndy Whitcroft } 238300df344fSAndy Whitcroft if ($ok == 0) { 2384000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 2385000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 23860a920b5bSAndy Whitcroft } 238700df344fSAndy Whitcroft } 23880a920b5bSAndy Whitcroft 2389243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 2390243f3803SJoe Perches my $orig = $1; 2391243f3803SJoe Perches my $level = lc($orig); 2392243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 2393243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 2394243f3803SJoe Perches "Prefer pr_$level(... to printk(KERN_$1, ...\n" . $herecurr); 2395243f3803SJoe Perches } 2396243f3803SJoe Perches 2397243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 2398243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 2399243f3803SJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr); 2400243f3803SJoe Perches } 2401243f3803SJoe Perches 2402653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 2403653d4876SAndy Whitcroft# or if closed on same line 2404c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2405c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 2406000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2407000d1cc1SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr); 24080a920b5bSAndy Whitcroft } 2409653d4876SAndy Whitcroft 24108905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 24118905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 24128905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2413000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2414000d1cc1SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev); 24158905a67cSAndy Whitcroft } 24168905a67cSAndy Whitcroft 24170c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 24180c73b4ebSAndy Whitcroft if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 2419000d1cc1SJoe Perches WARN("SPACING", 2420000d1cc1SJoe Perches "missing space after $1 definition\n" . $herecurr); 24210c73b4ebSAndy Whitcroft } 24220c73b4ebSAndy Whitcroft 24238d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 24248d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 2425fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2426fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 24278d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 24288d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 24298d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 2430fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 2431daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 2432000d1cc1SJoe Perches ERROR("BRACKET_SPACE", 2433000d1cc1SJoe Perches "space prohibited before open square bracket '['\n" . $herecurr); 24348d31cfceSAndy Whitcroft } 24358d31cfceSAndy Whitcroft } 24368d31cfceSAndy Whitcroft 2437f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 24386c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 2439c2fdda0dSAndy Whitcroft my $name = $1; 2440773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 2441773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 2442c2fdda0dSAndy Whitcroft 2443c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 2444773647a0SAndy Whitcroft if ($name =~ /^(?: 2445773647a0SAndy Whitcroft if|for|while|switch|return|case| 2446773647a0SAndy Whitcroft volatile|__volatile__| 2447773647a0SAndy Whitcroft __attribute__|format|__extension__| 2448773647a0SAndy Whitcroft asm|__asm__)$/x) 2449773647a0SAndy Whitcroft { 2450c2fdda0dSAndy Whitcroft 2451c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 2452c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 2453c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2454c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2455773647a0SAndy Whitcroft 2456773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2457c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2458c2fdda0dSAndy Whitcroft 2459c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2460c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2461773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2462c2fdda0dSAndy Whitcroft 2463c2fdda0dSAndy Whitcroft } else { 2464000d1cc1SJoe Perches WARN("SPACING", 2465000d1cc1SJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr); 2466f0a594c1SAndy Whitcroft } 24676c72ffaaSAndy Whitcroft } 24689a4cad4eSEric Nelson 24699a4cad4eSEric Nelson# check for whitespace before a non-naked semicolon 24709a4cad4eSEric Nelson if ($line =~ /^\+.*\S\s+;/) { 24719a4cad4eSEric Nelson CHK("SPACING", 24729a4cad4eSEric Nelson "space prohibited before semicolon\n" . $herecurr); 24739a4cad4eSEric Nelson } 24749a4cad4eSEric Nelson 2475653d4876SAndy Whitcroft# Check operator spacing. 24760a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 24779c0ca6f9SAndy Whitcroft my $ops = qr{ 24789c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 24799c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 24809c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 24811f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 24821f65f947SAndy Whitcroft \?|: 24839c0ca6f9SAndy Whitcroft }x; 2484cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 248500df344fSAndy Whitcroft my $off = 0; 24866c72ffaaSAndy Whitcroft 24876c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 24886c72ffaaSAndy Whitcroft 24890a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 24904a0df2efSAndy Whitcroft $off += length($elements[$n]); 24914a0df2efSAndy Whitcroft 249225985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 2493773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 2494773647a0SAndy Whitcroft my $cc = ''; 2495773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 2496773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 2497773647a0SAndy Whitcroft } 2498773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 2499773647a0SAndy Whitcroft 25004a0df2efSAndy Whitcroft my $a = ''; 25014a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 25024a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 2503cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 25044a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 25054a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 2506773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 25074a0df2efSAndy Whitcroft 25080a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 25094a0df2efSAndy Whitcroft 25104a0df2efSAndy Whitcroft my $c = ''; 25110a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 25124a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 25134a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2514cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 25154a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 25164a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 25178b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 25184a0df2efSAndy Whitcroft } else { 25194a0df2efSAndy Whitcroft $c = 'E'; 25200a920b5bSAndy Whitcroft } 25210a920b5bSAndy Whitcroft 25224a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 25234a0df2efSAndy Whitcroft 25244a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 25254a0df2efSAndy Whitcroft 25266c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 2527de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 25280a920b5bSAndy Whitcroft 252974048ed8SAndy Whitcroft # Pull out the value of this operator. 25306c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 25310a920b5bSAndy Whitcroft 25321f65f947SAndy Whitcroft # Get the full operator variant. 25331f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 25341f65f947SAndy Whitcroft 253513214adfSAndy Whitcroft # Ignore operators passed as parameters. 253613214adfSAndy Whitcroft if ($op_type ne 'V' && 253713214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 253813214adfSAndy Whitcroft 2539cf655043SAndy Whitcroft# # Ignore comments 2540cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 254113214adfSAndy Whitcroft 2542d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 254313214adfSAndy Whitcroft } elsif ($op eq ';') { 2544cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 2545cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 2546000d1cc1SJoe Perches ERROR("SPACING", 2547000d1cc1SJoe Perches "space required after that '$op' $at\n" . $hereptr); 2548d8aaf121SAndy Whitcroft } 2549d8aaf121SAndy Whitcroft 2550d8aaf121SAndy Whitcroft # // is a comment 2551d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 25520a920b5bSAndy Whitcroft 25531f65f947SAndy Whitcroft # No spaces for: 25541f65f947SAndy Whitcroft # -> 25551f65f947SAndy Whitcroft # : when part of a bitfield 25561f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 25574a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 2558000d1cc1SJoe Perches ERROR("SPACING", 2559000d1cc1SJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr); 25600a920b5bSAndy Whitcroft } 25610a920b5bSAndy Whitcroft 25620a920b5bSAndy Whitcroft # , must have a space on the right. 25630a920b5bSAndy Whitcroft } elsif ($op eq ',') { 2564cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2565000d1cc1SJoe Perches ERROR("SPACING", 2566000d1cc1SJoe Perches "space required after that '$op' $at\n" . $hereptr); 25670a920b5bSAndy Whitcroft } 25680a920b5bSAndy Whitcroft 25699c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 257074048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 25719c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 25729c0ca6f9SAndy Whitcroft 25739c0ca6f9SAndy Whitcroft # unary operators should have a space before and 25749c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 25759c0ca6f9SAndy Whitcroft # unary operator, or a cast 25769c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 257774048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 25780d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 2579cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2580000d1cc1SJoe Perches ERROR("SPACING", 2581000d1cc1SJoe Perches "space required before that '$op' $at\n" . $hereptr); 25820a920b5bSAndy Whitcroft } 2583a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2584171ae1a4SAndy Whitcroft # A unary '*' may be const 2585171ae1a4SAndy Whitcroft 2586171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 2587000d1cc1SJoe Perches ERROR("SPACING", 2588000d1cc1SJoe Perches "space prohibited after that '$op' $at\n" . $hereptr); 25890a920b5bSAndy Whitcroft } 25900a920b5bSAndy Whitcroft 25910a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 25920a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 2593773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2594000d1cc1SJoe Perches ERROR("SPACING", 2595000d1cc1SJoe Perches "space required one side of that '$op' $at\n" . $hereptr); 25960a920b5bSAndy Whitcroft } 2597773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 2598773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2599000d1cc1SJoe Perches ERROR("SPACING", 2600000d1cc1SJoe Perches "space prohibited before that '$op' $at\n" . $hereptr); 2601653d4876SAndy Whitcroft } 2602773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 2603000d1cc1SJoe Perches ERROR("SPACING", 2604000d1cc1SJoe Perches "space prohibited after that '$op' $at\n" . $hereptr); 2605773647a0SAndy Whitcroft } 2606773647a0SAndy Whitcroft 26070a920b5bSAndy Whitcroft 26080a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 26099c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 26109c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 26119c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 2612c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 2613c2fdda0dSAndy Whitcroft $op eq '%') 26140a920b5bSAndy Whitcroft { 2615773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 2616000d1cc1SJoe Perches ERROR("SPACING", 2617000d1cc1SJoe Perches "need consistent spacing around '$op' $at\n" . 2618de7d4f0eSAndy Whitcroft $hereptr); 26190a920b5bSAndy Whitcroft } 26200a920b5bSAndy Whitcroft 26211f65f947SAndy Whitcroft # A colon needs no spaces before when it is 26221f65f947SAndy Whitcroft # terminating a case value or a label. 26231f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 26241f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 2625000d1cc1SJoe Perches ERROR("SPACING", 2626000d1cc1SJoe Perches "space prohibited before that '$op' $at\n" . $hereptr); 26271f65f947SAndy Whitcroft } 26281f65f947SAndy Whitcroft 26290a920b5bSAndy Whitcroft # All the others need spaces both sides. 2630cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 26311f65f947SAndy Whitcroft my $ok = 0; 26321f65f947SAndy Whitcroft 263322f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 26341f65f947SAndy Whitcroft if (($op eq '<' && 26351f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 26361f65f947SAndy Whitcroft ($op eq '>' && 26371f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 26381f65f947SAndy Whitcroft { 26391f65f947SAndy Whitcroft $ok = 1; 26401f65f947SAndy Whitcroft } 26411f65f947SAndy Whitcroft 26421f65f947SAndy Whitcroft # Ignore ?: 26431f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 26441f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 26451f65f947SAndy Whitcroft $ok = 1; 26461f65f947SAndy Whitcroft } 26471f65f947SAndy Whitcroft 26481f65f947SAndy Whitcroft if ($ok == 0) { 2649000d1cc1SJoe Perches ERROR("SPACING", 2650000d1cc1SJoe Perches "spaces required around that '$op' $at\n" . $hereptr); 26510a920b5bSAndy Whitcroft } 265222f2a2efSAndy Whitcroft } 26534a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 26540a920b5bSAndy Whitcroft } 26550a920b5bSAndy Whitcroft } 26560a920b5bSAndy Whitcroft 2657f0a594c1SAndy Whitcroft# check for multiple assignments 2658f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 2659000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 2660000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 2661f0a594c1SAndy Whitcroft } 2662f0a594c1SAndy Whitcroft 266322f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 266422f2a2efSAndy Whitcroft## # continuation. 266522f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 266622f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 266722f2a2efSAndy Whitcroft## 266822f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 266922f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 267022f2a2efSAndy Whitcroft## my $ln = $line; 267122f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 267222f2a2efSAndy Whitcroft## } 267322f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 2674000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 2675000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 267622f2a2efSAndy Whitcroft## } 267722f2a2efSAndy Whitcroft## } 2678f0a594c1SAndy Whitcroft 26790a920b5bSAndy Whitcroft#need space before brace following if, while, etc 268022f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 268122f2a2efSAndy Whitcroft $line =~ /do{/) { 2682000d1cc1SJoe Perches ERROR("SPACING", 2683000d1cc1SJoe Perches "space required before the open brace '{'\n" . $herecurr); 2684de7d4f0eSAndy Whitcroft } 2685de7d4f0eSAndy Whitcroft 2686de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 2687de7d4f0eSAndy Whitcroft# on the line 2688de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 2689000d1cc1SJoe Perches ERROR("SPACING", 2690000d1cc1SJoe Perches "space required after that close brace '}'\n" . $herecurr); 26910a920b5bSAndy Whitcroft } 26920a920b5bSAndy Whitcroft 269322f2a2efSAndy Whitcroft# check spacing on square brackets 269422f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2695000d1cc1SJoe Perches ERROR("SPACING", 2696000d1cc1SJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr); 269722f2a2efSAndy Whitcroft } 269822f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 2699000d1cc1SJoe Perches ERROR("SPACING", 2700000d1cc1SJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr); 270122f2a2efSAndy Whitcroft } 270222f2a2efSAndy Whitcroft 2703c45dcabdSAndy Whitcroft# check spacing on parentheses 27049c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 27059c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 2706000d1cc1SJoe Perches ERROR("SPACING", 2707000d1cc1SJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr); 270822f2a2efSAndy Whitcroft } 270913214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2710c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2711c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2712000d1cc1SJoe Perches ERROR("SPACING", 2713000d1cc1SJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr); 271422f2a2efSAndy Whitcroft } 271522f2a2efSAndy Whitcroft 27160a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 27174a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 27180a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2719000d1cc1SJoe Perches WARN("INDENTED_LABEL", 2720000d1cc1SJoe Perches "labels should not be indented\n" . $herecurr); 27210a920b5bSAndy Whitcroft } 27220a920b5bSAndy Whitcroft 2723c45dcabdSAndy Whitcroft# Return is not a function. 2724c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2725c45dcabdSAndy Whitcroft my $spacing = $1; 2726c45dcabdSAndy Whitcroft my $value = $2; 2727c45dcabdSAndy Whitcroft 272886f9d059SAndy Whitcroft # Flatten any parentheses 2729fb2d2c1bSAndy Whitcroft $value =~ s/\(/ \(/g; 2730fb2d2c1bSAndy Whitcroft $value =~ s/\)/\) /g; 2731e01886adSAndy Whitcroft while ($value =~ s/\[[^\[\]]*\]/1/ || 273263f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 273363f17f89SAndy Whitcroft $Compare\s* 273463f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 273563f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2736c45dcabdSAndy Whitcroft } 2737fb2d2c1bSAndy Whitcroft#print "value<$value>\n"; 2738fb2d2c1bSAndy Whitcroft if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 2739000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 2740000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 2741c45dcabdSAndy Whitcroft 2742c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2743000d1cc1SJoe Perches ERROR("SPACING", 2744000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 2745c45dcabdSAndy Whitcroft } 2746c45dcabdSAndy Whitcroft } 274753a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 274853a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 274953a3c448SAndy Whitcroft my $name = $1; 275053a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 2751000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 2752000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 275353a3c448SAndy Whitcroft } 275453a3c448SAndy Whitcroft } 2755c45dcabdSAndy Whitcroft 27560a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 27574a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2758000d1cc1SJoe Perches ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); 27590a920b5bSAndy Whitcroft } 27600a920b5bSAndy Whitcroft 2761f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2762f5fe35ddSAndy Whitcroft# statements after the conditional. 2763170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 27643e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 27653e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 27663e469cdcSAndy Whitcroft if (!defined $stat); 2767170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2768170d3a22SAndy Whitcroft $remain_next, $off_next); 2769170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2770170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2771170d3a22SAndy Whitcroft 2772170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2773170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2774170d3a22SAndy Whitcroft # then count those as offsets. 2775170d3a22SAndy Whitcroft my ($whitespace) = 2776170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2777170d3a22SAndy Whitcroft my $offset = 2778170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2779170d3a22SAndy Whitcroft 2780170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2781170d3a22SAndy Whitcroft $offset} = 1; 2782170d3a22SAndy Whitcroft } 2783170d3a22SAndy Whitcroft } 2784170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2785170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2786171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 27878905a67cSAndy Whitcroft 2788b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2789000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 2790000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 27918905a67cSAndy Whitcroft } 27928905a67cSAndy Whitcroft 27938905a67cSAndy Whitcroft # Find out what is on the end of the line after the 27948905a67cSAndy Whitcroft # conditional. 2795773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 27968905a67cSAndy Whitcroft $s =~ s/\n.*//g; 279713214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 279853210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 279953210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2800773647a0SAndy Whitcroft { 2801bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2802bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2803bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 280442bdf74cSHidetoshi Seto my $stat_real = ''; 2805bb44ad39SAndy Whitcroft 280642bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 280742bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 2808bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2809bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2810bb44ad39SAndy Whitcroft } 2811bb44ad39SAndy Whitcroft 2812000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2813000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 28148905a67cSAndy Whitcroft } 28158905a67cSAndy Whitcroft } 28168905a67cSAndy Whitcroft 281713214adfSAndy Whitcroft# Check for bitwise tests written as boolean 281813214adfSAndy Whitcroft if ($line =~ / 281913214adfSAndy Whitcroft (?: 282013214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 282113214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 282213214adfSAndy Whitcroft (?:\&\&|\|\|) 282313214adfSAndy Whitcroft | 282413214adfSAndy Whitcroft (?:\&\&|\|\|) 282513214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 282613214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 282713214adfSAndy Whitcroft )/x) 282813214adfSAndy Whitcroft { 2829000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 2830000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 283113214adfSAndy Whitcroft } 283213214adfSAndy Whitcroft 28338905a67cSAndy Whitcroft# if and else should not have general statements after it 283413214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 283513214adfSAndy Whitcroft my $s = $1; 283613214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 283713214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 2838000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2839000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 28400a920b5bSAndy Whitcroft } 284113214adfSAndy Whitcroft } 284239667782SAndy Whitcroft# if should not continue a brace 284339667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 2844000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2845000d1cc1SJoe Perches "trailing statements should be on next line\n" . 284639667782SAndy Whitcroft $herecurr); 284739667782SAndy Whitcroft } 2848a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2849a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2850a1080bf8SAndy Whitcroft $line !~ /\G(?: 28513fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2852a1080bf8SAndy Whitcroft \s*return\s+ 2853a1080bf8SAndy Whitcroft )/xg) 2854a1080bf8SAndy Whitcroft { 2855000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 2856000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 2857a1080bf8SAndy Whitcroft } 28580a920b5bSAndy Whitcroft 28590a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 28600a920b5bSAndy Whitcroft # indent level to be relevant to each other. 28610a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 28620a920b5bSAndy Whitcroft $previndent == $indent) { 2863000d1cc1SJoe Perches ERROR("ELSE_AFTER_BRACE", 2864000d1cc1SJoe Perches "else should follow close brace '}'\n" . $hereprev); 28650a920b5bSAndy Whitcroft } 28660a920b5bSAndy Whitcroft 2867c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2868c2fdda0dSAndy Whitcroft $previndent == $indent) { 2869c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2870c2fdda0dSAndy Whitcroft 2871c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2872c2fdda0dSAndy Whitcroft # conditional. 2873773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2874c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2875c2fdda0dSAndy Whitcroft 2876c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2877000d1cc1SJoe Perches ERROR("WHILE_AFTER_BRACE", 2878000d1cc1SJoe Perches "while should follow close brace '}'\n" . $hereprev); 2879c2fdda0dSAndy Whitcroft } 2880c2fdda0dSAndy Whitcroft } 2881c2fdda0dSAndy Whitcroft 28820a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 28830a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 28840a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 28850a920b5bSAndy Whitcroft# print "$herecurr"; 28860a920b5bSAndy Whitcroft# $clean = 0; 28870a920b5bSAndy Whitcroft# } 28880a920b5bSAndy Whitcroft 28890a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2890c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2891000d1cc1SJoe Perches WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 2892000d1cc1SJoe Perches "Whitepspace after \\ makes next lines useless\n" . $herecurr); 28930a920b5bSAndy Whitcroft } 28940a920b5bSAndy Whitcroft 2895653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2896c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2897e09dec48SAndy Whitcroft my $file = "$1.h"; 2898e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2899e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2900e09dec48SAndy Whitcroft $realfile ne $checkfile && 29017840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 2902c45dcabdSAndy Whitcroft { 2903e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2904000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 2905000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2906e09dec48SAndy Whitcroft } else { 2907000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 2908000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2909e09dec48SAndy Whitcroft } 29100a920b5bSAndy Whitcroft } 29110a920b5bSAndy Whitcroft } 29120a920b5bSAndy Whitcroft 2913653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2914653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2915cf655043SAndy Whitcroft# in a known good container 2916b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2917b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2918d8aaf121SAndy Whitcroft my $ln = $linenr; 2919d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2920c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2921c45dcabdSAndy Whitcroft my $ctx = ''; 2922c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2923f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2924f74bd194SAndy Whitcroft $ctx = $dstat; 2925c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2926a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2927c45dcabdSAndy Whitcroft 2928f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 2929292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2930c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2931c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2932c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2933c45dcabdSAndy Whitcroft 2934c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2935bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2936bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2937c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 2938bf30d6edSAndy Whitcroft { 2939c45dcabdSAndy Whitcroft } 2940c45dcabdSAndy Whitcroft 2941e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 2942e45bab8eSAndy Whitcroft while ($dstat =~ s/("X*")\s*$Ident/$1/ || 2943e45bab8eSAndy Whitcroft $dstat =~ s/$Ident\s*("X*")/$1/) 2944e45bab8eSAndy Whitcroft { 2945e45bab8eSAndy Whitcroft } 2946e45bab8eSAndy Whitcroft 2947c45dcabdSAndy Whitcroft my $exceptions = qr{ 2948c45dcabdSAndy Whitcroft $Declare| 2949c45dcabdSAndy Whitcroft module_param_named| 2950c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 2951c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2952c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2953383099fdSAndy Whitcroft __typeof__\(| 295422fd2d3eSStefani Seibold union| 295522fd2d3eSStefani Seibold struct| 2956ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 2957ea71a0a0SAndy Whitcroft ^\"|\"$ 2958c45dcabdSAndy Whitcroft }x; 29595eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 2960f74bd194SAndy Whitcroft if ($dstat ne '' && 2961f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 2962f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 2963fd1b57acSAndy Whitcroft $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo 2964b9df76acSAndy Whitcroft $dstat !~ /^'X'$/ && # character constants 2965f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 2966f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 296772f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 2968f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 2969f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 2970f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 2971f74bd194SAndy Whitcroft $dstat !~ /^\({/) # ({... 2972c45dcabdSAndy Whitcroft { 2973f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 2974f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 2975f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 2976f74bd194SAndy Whitcroft 2977f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 2978f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 2979c45dcabdSAndy Whitcroft } 2980c45dcabdSAndy Whitcroft 2981f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 2982f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 2983f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 2984f74bd194SAndy Whitcroft } else { 2985000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 2986f74bd194SAndy Whitcroft "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 2987d8aaf121SAndy Whitcroft } 29880a920b5bSAndy Whitcroft } 2989653d4876SAndy Whitcroft } 29900a920b5bSAndy Whitcroft 2991080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 2992080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 2993080ba929SMike Frysinger# . 2994080ba929SMike Frysinger# ALIGN(...) 2995080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 2996080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 2997000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 2998000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 2999080ba929SMike Frysinger } 3000080ba929SMike Frysinger 3001f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 300213214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 300313214adfSAndy Whitcroft my ($level, $endln, @chunks) = 3004cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 300513214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 3006cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 3007cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 3008aad4f614SJoe Perches my @allowed = (); 3009aad4f614SJoe Perches my $allow = 0; 301013214adfSAndy Whitcroft my $seen = 0; 3011773647a0SAndy Whitcroft my $herectx = $here . "\n"; 3012cf655043SAndy Whitcroft my $ln = $linenr - 1; 301313214adfSAndy Whitcroft for my $chunk (@chunks) { 301413214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 301513214adfSAndy Whitcroft 3016773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 3017773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 3018773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 3019773647a0SAndy Whitcroft 3020aad4f614SJoe Perches $allowed[$allow] = 0; 3021773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 3022773647a0SAndy Whitcroft 3023773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 3024773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 3025773647a0SAndy Whitcroft 3026773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 3027cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 3028cf655043SAndy Whitcroft 3029773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 303013214adfSAndy Whitcroft 303113214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 303213214adfSAndy Whitcroft 3033aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 3034cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3035cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3036aad4f614SJoe Perches $allowed[$allow] = 1; 303713214adfSAndy Whitcroft } 303813214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3039cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3040aad4f614SJoe Perches $allowed[$allow] = 1; 304113214adfSAndy Whitcroft } 3042cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3043cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3044aad4f614SJoe Perches $allowed[$allow] = 1; 304513214adfSAndy Whitcroft } 3046aad4f614SJoe Perches $allow++; 304713214adfSAndy Whitcroft } 3048aad4f614SJoe Perches if ($seen) { 3049aad4f614SJoe Perches my $sum_allowed = 0; 3050aad4f614SJoe Perches foreach (@allowed) { 3051aad4f614SJoe Perches $sum_allowed += $_; 3052aad4f614SJoe Perches } 3053aad4f614SJoe Perches if ($sum_allowed == 0) { 3054000d1cc1SJoe Perches WARN("BRACES", 3055000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 3056aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 3057aad4f614SJoe Perches $seen != $allow) { 3058aad4f614SJoe Perches CHK("BRACES", 3059aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 3060aad4f614SJoe Perches } 306113214adfSAndy Whitcroft } 306213214adfSAndy Whitcroft } 306313214adfSAndy Whitcroft } 3064773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 306513214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 3066cf655043SAndy Whitcroft my $allowed = 0; 3067f0a594c1SAndy Whitcroft 3068cf655043SAndy Whitcroft # Check the pre-context. 3069cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 3070cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 3071cf655043SAndy Whitcroft $allowed = 1; 3072f0a594c1SAndy Whitcroft } 3073773647a0SAndy Whitcroft 3074773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 3075773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 3076773647a0SAndy Whitcroft 3077cf655043SAndy Whitcroft # Check the condition. 3078cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 3079773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 3080cf655043SAndy Whitcroft if (defined $cond) { 3081773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3082cf655043SAndy Whitcroft } 3083cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3084cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3085cf655043SAndy Whitcroft $allowed = 1; 3086cf655043SAndy Whitcroft } 3087cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3088cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3089cf655043SAndy Whitcroft $allowed = 1; 3090cf655043SAndy Whitcroft } 3091cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3092cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3093cf655043SAndy Whitcroft $allowed = 1; 3094cf655043SAndy Whitcroft } 3095cf655043SAndy Whitcroft # Check the post-context. 3096cf655043SAndy Whitcroft if (defined $chunks[1]) { 3097cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 3098cf655043SAndy Whitcroft if (defined $cond) { 3099773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3100cf655043SAndy Whitcroft } 3101cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 3102cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 3103cf655043SAndy Whitcroft $allowed = 1; 3104cf655043SAndy Whitcroft } 3105cf655043SAndy Whitcroft } 3106cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 310769932487SJustin P. Mattock my $herectx = $here . "\n"; 3108f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 3109cf655043SAndy Whitcroft 3110f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 311169932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 3112cf655043SAndy Whitcroft } 3113cf655043SAndy Whitcroft 3114000d1cc1SJoe Perches WARN("BRACES", 3115000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 3116f0a594c1SAndy Whitcroft } 3117f0a594c1SAndy Whitcroft } 3118f0a594c1SAndy Whitcroft 3119653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 31204a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 3121c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 3122000d1cc1SJoe Perches ERROR("DEPRECATED_INCLUDE", 3123000d1cc1SJoe Perches "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 31240a920b5bSAndy Whitcroft } 31250a920b5bSAndy Whitcroft } 31260a920b5bSAndy Whitcroft 31274a0df2efSAndy Whitcroft# don't use deprecated functions 31284a0df2efSAndy Whitcroft for my $func (@dep_functions) { 312900df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 3130000d1cc1SJoe Perches ERROR("DEPRECATED_FUNCTION", 3131000d1cc1SJoe Perches "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 31324a0df2efSAndy Whitcroft } 31334a0df2efSAndy Whitcroft } 31344a0df2efSAndy Whitcroft 31354a0df2efSAndy Whitcroft# no volatiles please 31366c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 31376c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 3138000d1cc1SJoe Perches WARN("VOLATILE", 3139000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 31404a0df2efSAndy Whitcroft } 31414a0df2efSAndy Whitcroft 314200df344fSAndy Whitcroft# warn about #if 0 3143c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 3144000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 3145000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 3146de7d4f0eSAndy Whitcroft $herecurr); 31474a0df2efSAndy Whitcroft } 31484a0df2efSAndy Whitcroft 3149f0a594c1SAndy Whitcroft# check for needless kfree() checks 3150f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 3151f0a594c1SAndy Whitcroft my $expr = $1; 3152f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 3153000d1cc1SJoe Perches WARN("NEEDLESS_KFREE", 3154000d1cc1SJoe Perches "kfree(NULL) is safe this check is probably not required\n" . $hereprev); 3155f0a594c1SAndy Whitcroft } 3156f0a594c1SAndy Whitcroft } 31574c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks 31584c432a8fSGreg Kroah-Hartman if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 31594c432a8fSGreg Kroah-Hartman my $expr = $1; 31604c432a8fSGreg Kroah-Hartman if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { 3161000d1cc1SJoe Perches WARN("NEEDLESS_USB_FREE_URB", 3162000d1cc1SJoe Perches "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); 31634c432a8fSGreg Kroah-Hartman } 31644c432a8fSGreg Kroah-Hartman } 3165f0a594c1SAndy Whitcroft 31661a15a250SPatrick Pannuto# prefer usleep_range over udelay 31671a15a250SPatrick Pannuto if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { 31681a15a250SPatrick Pannuto # ignore udelay's < 10, however 31691a15a250SPatrick Pannuto if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { 3170000d1cc1SJoe Perches CHK("USLEEP_RANGE", 3171000d1cc1SJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); 31721a15a250SPatrick Pannuto } 31731a15a250SPatrick Pannuto } 31741a15a250SPatrick Pannuto 317509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 317609ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 317709ef8725SPatrick Pannuto if ($1 < 20) { 3178000d1cc1SJoe Perches WARN("MSLEEP", 3179000d1cc1SJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); 318009ef8725SPatrick Pannuto } 318109ef8725SPatrick Pannuto } 318209ef8725SPatrick Pannuto 318300df344fSAndy Whitcroft# warn about #ifdefs in C files 3184c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 318500df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 318600df344fSAndy Whitcroft# print "$herecurr"; 318700df344fSAndy Whitcroft# $clean = 0; 318800df344fSAndy Whitcroft# } 318900df344fSAndy Whitcroft 319022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 3191c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 3192000d1cc1SJoe Perches ERROR("SPACING", 3193000d1cc1SJoe Perches "exactly one space required after that #$1\n" . $herecurr); 319422f2a2efSAndy Whitcroft } 319522f2a2efSAndy Whitcroft 31964a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 3197171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 3198171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 31994a0df2efSAndy Whitcroft my $which = $1; 32004a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3201000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 3202000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 32034a0df2efSAndy Whitcroft } 32044a0df2efSAndy Whitcroft } 32054a0df2efSAndy Whitcroft# check for memory barriers without a comment. 32064a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 32074a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3208000d1cc1SJoe Perches CHK("MEMORY_BARRIER", 3209000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 32104a0df2efSAndy Whitcroft } 32114a0df2efSAndy Whitcroft } 32124a0df2efSAndy Whitcroft# check of hardware specific defines 3213c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 3214000d1cc1SJoe Perches CHK("ARCH_DEFINES", 3215000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 32160a920b5bSAndy Whitcroft } 3217653d4876SAndy Whitcroft 3218d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 3219d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 3220000d1cc1SJoe Perches WARN("STORAGE_CLASS", 3221000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 3222d4977c78STobias Klauser } 3223d4977c78STobias Klauser 3224de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 3225de7d4f0eSAndy Whitcroft# storage class and type. 32269c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 32279c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 3228000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 3229000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 3230de7d4f0eSAndy Whitcroft } 3231de7d4f0eSAndy Whitcroft 32328905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 32338905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 3234000d1cc1SJoe Perches WARN("INLINE", 3235000d1cc1SJoe Perches "plain inline is preferred over $1\n" . $herecurr); 32368905a67cSAndy Whitcroft } 32378905a67cSAndy Whitcroft 32383d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 32393d130fd0SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 3240000d1cc1SJoe Perches WARN("PREFER_PACKED", 3241000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 32423d130fd0SJoe Perches } 32433d130fd0SJoe Perches 324439b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 324539b7e287SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 3246000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 3247000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 324839b7e287SJoe Perches } 324939b7e287SJoe Perches 32505f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 32515f14d3bdSJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 32525f14d3bdSJoe Perches WARN("PREFER_PRINTF", 32535f14d3bdSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); 32545f14d3bdSJoe Perches } 32555f14d3bdSJoe Perches 32566061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 32576061d949SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 32586061d949SJoe Perches WARN("PREFER_SCANF", 32596061d949SJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr); 32606061d949SJoe Perches } 32616061d949SJoe Perches 32628f53a9b8SJoe Perches# check for sizeof(&) 32638f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 3264000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 3265000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 32668f53a9b8SJoe Perches } 32678f53a9b8SJoe Perches 3268*66c80b60SJoe Perches# check for sizeof without parenthesis 3269*66c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 3270*66c80b60SJoe Perches WARN("SIZEOF_PARENTHESIS", 3271*66c80b60SJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr); 3272*66c80b60SJoe Perches } 3273*66c80b60SJoe Perches 3274428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 3275428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 3276000d1cc1SJoe Perches WARN("LINE_CONTINUATIONS", 3277000d1cc1SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 3278428e2fdcSJoe Perches } 3279428e2fdcSJoe Perches 3280554e165cSAndy Whitcroft# Check for misused memsets 3281d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3282d1fe9c09SJoe Perches defined $stat && 3283d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 3284554e165cSAndy Whitcroft 3285d7c76ba7SJoe Perches my $ms_addr = $2; 3286d1fe9c09SJoe Perches my $ms_val = $7; 3287d1fe9c09SJoe Perches my $ms_size = $12; 3288d7c76ba7SJoe Perches 3289554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 3290554e165cSAndy Whitcroft ERROR("MEMSET", 3291d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 3292554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 3293554e165cSAndy Whitcroft WARN("MEMSET", 3294d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 3295d7c76ba7SJoe Perches } 3296d7c76ba7SJoe Perches } 3297d7c76ba7SJoe Perches 3298d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 3299d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3300d1fe9c09SJoe Perches defined $stat && 3301d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 3302d1fe9c09SJoe Perches if (defined $2 || defined $7) { 3303d7c76ba7SJoe Perches my $call = $1; 3304d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 3305d7c76ba7SJoe Perches my $arg1 = $3; 3306d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 3307d1fe9c09SJoe Perches my $arg2 = $8; 3308d7c76ba7SJoe Perches my $cast; 3309d7c76ba7SJoe Perches 3310d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 3311d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 3312d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 3313d7c76ba7SJoe Perches $cast = $cast1; 3314d7c76ba7SJoe Perches } else { 3315d7c76ba7SJoe Perches $cast = $cast2; 3316d7c76ba7SJoe Perches } 3317d7c76ba7SJoe Perches WARN("MINMAX", 3318d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 3319554e165cSAndy Whitcroft } 3320554e165cSAndy Whitcroft } 3321554e165cSAndy Whitcroft 33224a273195SJoe Perches# check usleep_range arguments 33234a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 33244a273195SJoe Perches defined $stat && 33254a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 33264a273195SJoe Perches my $min = $1; 33274a273195SJoe Perches my $max = $7; 33284a273195SJoe Perches if ($min eq $max) { 33294a273195SJoe Perches WARN("USLEEP_RANGE", 33304a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 33314a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 33324a273195SJoe Perches $min > $max) { 33334a273195SJoe Perches WARN("USLEEP_RANGE", 33344a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 33354a273195SJoe Perches } 33364a273195SJoe Perches } 33374a273195SJoe Perches 3338de7d4f0eSAndy Whitcroft# check for new externs in .c files. 3339171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 3340c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3341171ae1a4SAndy Whitcroft { 3342c45dcabdSAndy Whitcroft my $function_name = $1; 3343c45dcabdSAndy Whitcroft my $paren_space = $2; 3344171ae1a4SAndy Whitcroft 3345171ae1a4SAndy Whitcroft my $s = $stat; 3346171ae1a4SAndy Whitcroft if (defined $cond) { 3347171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 3348171ae1a4SAndy Whitcroft } 3349c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 3350c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 3351c45dcabdSAndy Whitcroft { 3352000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3353000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3354de7d4f0eSAndy Whitcroft } 3355de7d4f0eSAndy Whitcroft 3356171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 3357000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 3358000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 3359171ae1a4SAndy Whitcroft } 33609c9ba34eSAndy Whitcroft 33619c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 33629c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 33639c9ba34eSAndy Whitcroft { 3364000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 3365000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 3366171ae1a4SAndy Whitcroft } 3367171ae1a4SAndy Whitcroft 3368de7d4f0eSAndy Whitcroft# checks for new __setup's 3369de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 3370de7d4f0eSAndy Whitcroft my $name = $1; 3371de7d4f0eSAndy Whitcroft 3372de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 3373000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 3374000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 3375de7d4f0eSAndy Whitcroft } 3376653d4876SAndy Whitcroft } 33779c0ca6f9SAndy Whitcroft 33789c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 3379caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 3380000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 3381000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 33829c0ca6f9SAndy Whitcroft } 338313214adfSAndy Whitcroft 3384caf2a54fSJoe Perches# check for multiple semicolons 3385caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 3386000d1cc1SJoe Perches WARN("ONE_SEMICOLON", 3387000d1cc1SJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr); 3388caf2a54fSJoe Perches } 3389caf2a54fSJoe Perches 339013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 339113214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 3392000d1cc1SJoe Perches WARN("USE_FUNC", 3393000d1cc1SJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 339413214adfSAndy Whitcroft } 3395773647a0SAndy Whitcroft 33962c92488aSJoe Perches# check for use of yield() 33972c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 33982c92488aSJoe Perches WARN("YIELD", 33992c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 34002c92488aSJoe Perches } 34012c92488aSJoe Perches 34024882720bSThomas Gleixner# check for semaphores initialized locked 34034882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 3404000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 3405000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 3406773647a0SAndy Whitcroft } 34076712d858SJoe Perches 340867d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 340967d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 3410000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 341167d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 3412773647a0SAndy Whitcroft } 34136712d858SJoe Perches 3414f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 3415f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 3416000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 3417000d1cc1SJoe Perches "please use device_initcall() instead of __initcall()\n" . $herecurr); 3418f3db6639SMichael Ellerman } 34196712d858SJoe Perches 342079404849SEmese Revfy# check for various ops structs, ensure they are const. 342179404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 342279404849SEmese Revfy address_space_operations| 342379404849SEmese Revfy backlight_ops| 342479404849SEmese Revfy block_device_operations| 342579404849SEmese Revfy dentry_operations| 342679404849SEmese Revfy dev_pm_ops| 342779404849SEmese Revfy dma_map_ops| 342879404849SEmese Revfy extent_io_ops| 342979404849SEmese Revfy file_lock_operations| 343079404849SEmese Revfy file_operations| 343179404849SEmese Revfy hv_ops| 343279404849SEmese Revfy ide_dma_ops| 343379404849SEmese Revfy intel_dvo_dev_ops| 343479404849SEmese Revfy item_operations| 343579404849SEmese Revfy iwl_ops| 343679404849SEmese Revfy kgdb_arch| 343779404849SEmese Revfy kgdb_io| 343879404849SEmese Revfy kset_uevent_ops| 343979404849SEmese Revfy lock_manager_operations| 344079404849SEmese Revfy microcode_ops| 344179404849SEmese Revfy mtrr_ops| 344279404849SEmese Revfy neigh_ops| 344379404849SEmese Revfy nlmsvc_binding| 344479404849SEmese Revfy pci_raw_ops| 344579404849SEmese Revfy pipe_buf_operations| 344679404849SEmese Revfy platform_hibernation_ops| 344779404849SEmese Revfy platform_suspend_ops| 344879404849SEmese Revfy proto_ops| 344979404849SEmese Revfy rpc_pipe_ops| 345079404849SEmese Revfy seq_operations| 345179404849SEmese Revfy snd_ac97_build_ops| 345279404849SEmese Revfy soc_pcmcia_socket_ops| 345379404849SEmese Revfy stacktrace_ops| 345479404849SEmese Revfy sysfs_ops| 345579404849SEmese Revfy tty_operations| 345679404849SEmese Revfy usb_mon_operations| 345779404849SEmese Revfy wd_ops}x; 34586903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 345979404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 3460000d1cc1SJoe Perches WARN("CONST_STRUCT", 3461000d1cc1SJoe Perches "struct $1 should normally be const\n" . 34626903ffb2SAndy Whitcroft $herecurr); 34632b6db5cbSAndy Whitcroft } 3464773647a0SAndy Whitcroft 3465773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 3466773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 3467773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 3468c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 3469c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 3470171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 3471171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 3472171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 3473773647a0SAndy Whitcroft { 3474000d1cc1SJoe Perches WARN("NR_CPUS", 3475000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 3476773647a0SAndy Whitcroft } 34779c9ba34eSAndy Whitcroft 34789c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 34799c9ba34eSAndy Whitcroft my $string; 34809c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 34819c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 34822a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 34839c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 3484000d1cc1SJoe Perches WARN("PRINTF_L", 3485000d1cc1SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 34869c9ba34eSAndy Whitcroft last; 34879c9ba34eSAndy Whitcroft } 34889c9ba34eSAndy Whitcroft } 3489691d77b6SAndy Whitcroft 3490691d77b6SAndy Whitcroft# whine mightly about in_atomic 3491691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 3492691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 3493000d1cc1SJoe Perches ERROR("IN_ATOMIC", 3494000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 3495f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 3496000d1cc1SJoe Perches WARN("IN_ATOMIC", 3497000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 3498691d77b6SAndy Whitcroft } 3499691d77b6SAndy Whitcroft } 35001704f47bSPeter Zijlstra 35011704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 35021704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 35031704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 35041704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 35051704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 35061704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 3507000d1cc1SJoe Perches ERROR("LOCKDEP", 3508000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 35091704f47bSPeter Zijlstra } 35101704f47bSPeter Zijlstra } 351188f8831cSDave Jones 351288f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 351388f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 3514000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 3515000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 351688f8831cSDave Jones } 351713214adfSAndy Whitcroft } 351813214adfSAndy Whitcroft 351913214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 352013214adfSAndy Whitcroft # so just keep quiet. 352113214adfSAndy Whitcroft if ($#rawlines == -1) { 352213214adfSAndy Whitcroft exit(0); 35230a920b5bSAndy Whitcroft } 35240a920b5bSAndy Whitcroft 35258905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 35268905a67cSAndy Whitcroft # things that appear to be patches. 35278905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 35288905a67cSAndy Whitcroft exit(0); 35298905a67cSAndy Whitcroft } 35308905a67cSAndy Whitcroft 35318905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 35328905a67cSAndy Whitcroft # just keep quiet. 35338905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 35348905a67cSAndy Whitcroft exit(0); 35358905a67cSAndy Whitcroft } 35368905a67cSAndy Whitcroft 35378905a67cSAndy Whitcroft if (!$is_patch) { 3538000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 3539000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 35400a920b5bSAndy Whitcroft } 35410a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 3542000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 3543000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 35440a920b5bSAndy Whitcroft } 35450a920b5bSAndy Whitcroft 3546f0a594c1SAndy Whitcroft print report_dump(); 354713214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 354813214adfSAndy Whitcroft print "$filename " if ($summary_file); 35496c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 35506c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 35516c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 35528905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 35536c72ffaaSAndy Whitcroft } 35548905a67cSAndy Whitcroft 3555d2c0a235SAndy Whitcroft if ($quiet == 0) { 3556d1fe9c09SJoe Perches 3557d1fe9c09SJoe Perches if ($^V lt 5.10.0) { 3558d1fe9c09SJoe Perches print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); 3559d1fe9c09SJoe Perches print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); 3560d1fe9c09SJoe Perches } 3561d1fe9c09SJoe Perches 3562d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 3563d2c0a235SAndy Whitcroft # then suggest that. 3564d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 3565d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 3566d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 3567b0781216SMike Frysinger $rpt_cleaners = 0; 3568d2c0a235SAndy Whitcroft } 3569d2c0a235SAndy Whitcroft } 3570d2c0a235SAndy Whitcroft 357111232688SArtem Bityutskiy if ($quiet == 0 && keys %ignore_type) { 3572000d1cc1SJoe Perches print "NOTE: Ignored message types:"; 3573000d1cc1SJoe Perches foreach my $ignore (sort keys %ignore_type) { 3574000d1cc1SJoe Perches print " $ignore"; 3575000d1cc1SJoe Perches } 357611232688SArtem Bityutskiy print "\n\n"; 3577000d1cc1SJoe Perches } 3578000d1cc1SJoe Perches 35790a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 3580c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 35810a920b5bSAndy Whitcroft } 35820a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 3583000d1cc1SJoe Perches print << "EOM"; 3584000d1cc1SJoe Perches$vname has style problems, please review. 3585000d1cc1SJoe Perches 3586000d1cc1SJoe PerchesIf any of these errors are false positives, please report 3587000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 3588000d1cc1SJoe PerchesEOM 35890a920b5bSAndy Whitcroft } 359013214adfSAndy Whitcroft 35910a920b5bSAndy Whitcroft return $clean; 35920a920b5bSAndy Whitcroft} 3593