10a920b5bSAndy Whitcroft#!/usr/bin/perl -w 2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit) 300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit) 42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) 5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]> 60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2 70a920b5bSAndy Whitcroft 80a920b5bSAndy Whitcroftuse strict; 9c707a81dSJoe Perchesuse POSIX; 100a920b5bSAndy Whitcroft 110a920b5bSAndy Whitcroftmy $P = $0; 1200df344fSAndy Whitcroft$P =~ s@.*/@@g; 130a920b5bSAndy Whitcroft 14000d1cc1SJoe Perchesmy $V = '0.32'; 150a920b5bSAndy Whitcroft 160a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 170a920b5bSAndy Whitcroft 180a920b5bSAndy Whitcroftmy $quiet = 0; 190a920b5bSAndy Whitcroftmy $tree = 1; 200a920b5bSAndy Whitcroftmy $chk_signoff = 1; 210a920b5bSAndy Whitcroftmy $chk_patch = 1; 22773647a0SAndy Whitcroftmy $tst_only; 236c72ffaaSAndy Whitcroftmy $emacs = 0; 248905a67cSAndy Whitcroftmy $terse = 0; 256c72ffaaSAndy Whitcroftmy $file = 0; 266c72ffaaSAndy Whitcroftmy $check = 0; 278905a67cSAndy Whitcroftmy $summary = 1; 288905a67cSAndy Whitcroftmy $mailback = 0; 2913214adfSAndy Whitcroftmy $summary_file = 0; 30000d1cc1SJoe Perchesmy $show_types = 0; 313705ce5bSJoe Perchesmy $fix = 0; 326c72ffaaSAndy Whitcroftmy $root; 33c2fdda0dSAndy Whitcroftmy %debug; 343445686aSJoe Perchesmy %camelcase = (); 3591bfe484SJoe Perchesmy %use_type = (); 3691bfe484SJoe Perchesmy @use = (); 3791bfe484SJoe Perchesmy %ignore_type = (); 38000d1cc1SJoe Perchesmy @ignore = (); 3977f5b10aSHannes Edermy $help = 0; 40000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 416cd7f386SJoe Perchesmy $max_line_length = 80; 42d62a201fSDave Hansenmy $ignore_perl_version = 0; 43d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 4477f5b10aSHannes Eder 4577f5b10aSHannes Edersub help { 4677f5b10aSHannes Eder my ($exitcode) = @_; 4777f5b10aSHannes Eder 4877f5b10aSHannes Eder print << "EOM"; 4977f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 5077f5b10aSHannes EderVersion: $V 5177f5b10aSHannes Eder 5277f5b10aSHannes EderOptions: 5377f5b10aSHannes Eder -q, --quiet quiet 5477f5b10aSHannes Eder --no-tree run without a kernel tree 5577f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 5677f5b10aSHannes Eder --patch treat FILE as patchfile (default) 5777f5b10aSHannes Eder --emacs emacs compile window format 5877f5b10aSHannes Eder --terse one line per report 5977f5b10aSHannes Eder -f, --file treat FILE as regular source file 6077f5b10aSHannes Eder --subjective, --strict enable more subjective tests 6191bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 62000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 636cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 64000d1cc1SJoe Perches --show-types show the message "types" in the output 6577f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 6677f5b10aSHannes Eder --no-summary suppress the per-file summary 6777f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 6877f5b10aSHannes Eder --summary-file include the filename in summary 6977f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 7077f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 7177f5b10aSHannes Eder is all off) 7277f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 7377f5b10aSHannes Eder literally 743705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 753705ce5bSJoe Perches If correctable single-line errors exist, create 763705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 773705ce5bSJoe Perches with potential errors corrected to the preferred 783705ce5bSJoe Perches checkpatch style 79d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 80d62a201fSDave Hansen runtime errors. 8177f5b10aSHannes Eder -h, --help, --version display this help and exit 8277f5b10aSHannes Eder 8377f5b10aSHannes EderWhen FILE is - read standard input. 8477f5b10aSHannes EderEOM 8577f5b10aSHannes Eder 8677f5b10aSHannes Eder exit($exitcode); 8777f5b10aSHannes Eder} 8877f5b10aSHannes Eder 89000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 90000d1cc1SJoe Perchesif (-f $conf) { 91000d1cc1SJoe Perches my @conf_args; 92000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 93000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 94000d1cc1SJoe Perches 95000d1cc1SJoe Perches while (<$conffile>) { 96000d1cc1SJoe Perches my $line = $_; 97000d1cc1SJoe Perches 98000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 99000d1cc1SJoe Perches $line =~ s/^\s*//g; 100000d1cc1SJoe Perches $line =~ s/\s+/ /g; 101000d1cc1SJoe Perches 102000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 103000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 104000d1cc1SJoe Perches 105000d1cc1SJoe Perches my @words = split(" ", $line); 106000d1cc1SJoe Perches foreach my $word (@words) { 107000d1cc1SJoe Perches last if ($word =~ m/^#/); 108000d1cc1SJoe Perches push (@conf_args, $word); 109000d1cc1SJoe Perches } 110000d1cc1SJoe Perches } 111000d1cc1SJoe Perches close($conffile); 112000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 113000d1cc1SJoe Perches} 114000d1cc1SJoe Perches 1150a920b5bSAndy WhitcroftGetOptions( 1166c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1170a920b5bSAndy Whitcroft 'tree!' => \$tree, 1180a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1190a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1206c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1218905a67cSAndy Whitcroft 'terse!' => \$terse, 12277f5b10aSHannes Eder 'f|file!' => \$file, 1236c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1246c72ffaaSAndy Whitcroft 'strict!' => \$check, 125000d1cc1SJoe Perches 'ignore=s' => \@ignore, 12691bfe484SJoe Perches 'types=s' => \@use, 127000d1cc1SJoe Perches 'show-types!' => \$show_types, 1286cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 1296c72ffaaSAndy Whitcroft 'root=s' => \$root, 1308905a67cSAndy Whitcroft 'summary!' => \$summary, 1318905a67cSAndy Whitcroft 'mailback!' => \$mailback, 13213214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 1333705ce5bSJoe Perches 'fix!' => \$fix, 134d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 135c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 136773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 13777f5b10aSHannes Eder 'h|help' => \$help, 13877f5b10aSHannes Eder 'version' => \$help 13977f5b10aSHannes Eder) or help(1); 14077f5b10aSHannes Eder 14177f5b10aSHannes Ederhelp(0) if ($help); 1420a920b5bSAndy Whitcroft 1430a920b5bSAndy Whitcroftmy $exit = 0; 1440a920b5bSAndy Whitcroft 145d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 146d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 147d62a201fSDave Hansen if (!$ignore_perl_version) { 148d62a201fSDave Hansen exit(1); 149d62a201fSDave Hansen } 150d62a201fSDave Hansen} 151d62a201fSDave Hansen 1520a920b5bSAndy Whitcroftif ($#ARGV < 0) { 15377f5b10aSHannes Eder print "$P: no input files\n"; 1540a920b5bSAndy Whitcroft exit(1); 1550a920b5bSAndy Whitcroft} 1560a920b5bSAndy Whitcroft 15791bfe484SJoe Perchessub hash_save_array_words { 15891bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 15991bfe484SJoe Perches 16091bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 16191bfe484SJoe Perches foreach my $word (@array) { 162000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 163000d1cc1SJoe Perches $word =~ s/^\s*//g; 164000d1cc1SJoe Perches $word =~ s/\s+/ /g; 165000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 166000d1cc1SJoe Perches 167000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 168000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 169000d1cc1SJoe Perches 17091bfe484SJoe Perches $hashRef->{$word}++; 171000d1cc1SJoe Perches } 17291bfe484SJoe Perches} 17391bfe484SJoe Perches 17491bfe484SJoe Perchessub hash_show_words { 17591bfe484SJoe Perches my ($hashRef, $prefix) = @_; 17691bfe484SJoe Perches 17758cb3cf6SJoe Perches if ($quiet == 0 && keys %$hashRef) { 17891bfe484SJoe Perches print "NOTE: $prefix message types:"; 17958cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 18091bfe484SJoe Perches print " $word"; 18191bfe484SJoe Perches } 18291bfe484SJoe Perches print "\n\n"; 18391bfe484SJoe Perches } 18491bfe484SJoe Perches} 18591bfe484SJoe Perches 18691bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 18791bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 188000d1cc1SJoe Perches 189c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 190c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 1917429c690SAndy Whitcroftmy $dbg_type = 0; 192a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 193c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 19421caa13cSAndy Whitcroft ## no critic 19521caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 19621caa13cSAndy Whitcroft die "$@" if ($@); 197c2fdda0dSAndy Whitcroft} 198c2fdda0dSAndy Whitcroft 199d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 200d2c0a235SAndy Whitcroft 2018905a67cSAndy Whitcroftif ($terse) { 2028905a67cSAndy Whitcroft $emacs = 1; 2038905a67cSAndy Whitcroft $quiet++; 2048905a67cSAndy Whitcroft} 2058905a67cSAndy Whitcroft 2066c72ffaaSAndy Whitcroftif ($tree) { 2076c72ffaaSAndy Whitcroft if (defined $root) { 2086c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 2096c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 2106c72ffaaSAndy Whitcroft } 2116c72ffaaSAndy Whitcroft } else { 2126c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 2136c72ffaaSAndy Whitcroft $root = '.'; 2146c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 2156c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 2166c72ffaaSAndy Whitcroft $root = $1; 2176c72ffaaSAndy Whitcroft } 2186c72ffaaSAndy Whitcroft } 2196c72ffaaSAndy Whitcroft 2206c72ffaaSAndy Whitcroft if (!defined $root) { 2210a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 2220a920b5bSAndy Whitcroft exit(2); 2230a920b5bSAndy Whitcroft } 2246c72ffaaSAndy Whitcroft} 2256c72ffaaSAndy Whitcroft 2266c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 2276c72ffaaSAndy Whitcroft 2282ceb532bSAndy Whitcroftour $Ident = qr{ 2292ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 2302ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 2312ceb532bSAndy Whitcroft }x; 2326c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 2336c72ffaaSAndy Whitcroftour $Sparse = qr{ 2346c72ffaaSAndy Whitcroft __user| 2356c72ffaaSAndy Whitcroft __kernel| 2366c72ffaaSAndy Whitcroft __force| 2376c72ffaaSAndy Whitcroft __iomem| 2386c72ffaaSAndy Whitcroft __must_check| 2396c72ffaaSAndy Whitcroft __init_refok| 240417495edSAndy Whitcroft __kprobes| 241165e72a6SSven Eckelmann __ref| 242165e72a6SSven Eckelmann __rcu 2436c72ffaaSAndy Whitcroft }x; 24452131292SWolfram Sang 2458716de38SJoe Perchesour $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)}; 2468716de38SJoe Perches 24752131292SWolfram Sang# Notes to $Attribute: 24852131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 2496c72ffaaSAndy Whitcroftour $Attribute = qr{ 2506c72ffaaSAndy Whitcroft const| 25103f1df7dSJoe Perches __percpu| 25203f1df7dSJoe Perches __nocast| 25303f1df7dSJoe Perches __safe| 25403f1df7dSJoe Perches __bitwise__| 25503f1df7dSJoe Perches __packed__| 25603f1df7dSJoe Perches __packed2__| 25703f1df7dSJoe Perches __naked| 25803f1df7dSJoe Perches __maybe_unused| 25903f1df7dSJoe Perches __always_unused| 26003f1df7dSJoe Perches __noreturn| 26103f1df7dSJoe Perches __used| 26203f1df7dSJoe Perches __cold| 26303f1df7dSJoe Perches __noclone| 26403f1df7dSJoe Perches __deprecated| 2656c72ffaaSAndy Whitcroft __read_mostly| 2666c72ffaaSAndy Whitcroft __kprobes| 2678716de38SJoe Perches $InitAttribute| 26824e1d81aSAndy Whitcroft ____cacheline_aligned| 26924e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 2705fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 2715fe3af11SAndy Whitcroft __weak 2726c72ffaaSAndy Whitcroft }x; 273c45dcabdSAndy Whitcroftour $Modifier; 2746c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 2756c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 2766c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 2776c72ffaaSAndy Whitcroft 27895e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 27995e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 28095e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 28195e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 282326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 283326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 284326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 28574349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 28695e2c602SJoe Perchesour $Constant = qr{$Float|$Binary|$Hex|$Int}; 287326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 28886f9d059SAndy Whitcroftour $Compare = qr{<=|>=|==|!=|<|>}; 28923f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 2906c72ffaaSAndy Whitcroftour $Operators = qr{ 2916c72ffaaSAndy Whitcroft <=|>=|==|!=| 2926c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 29323f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 2946c72ffaaSAndy Whitcroft }x; 2956c72ffaaSAndy Whitcroft 2968905a67cSAndy Whitcroftour $NonptrType; 2978716de38SJoe Perchesour $NonptrTypeWithAttr; 2988905a67cSAndy Whitcroftour $Type; 2998905a67cSAndy Whitcroftour $Declare; 3008905a67cSAndy Whitcroft 30115662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 30215662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 303171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 304171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 305171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 306171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 307171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 308171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 309171ae1a4SAndy Whitcroft}x; 310171ae1a4SAndy Whitcroft 31115662b3eSJoe Perchesour $UTF8 = qr{ 31215662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 31315662b3eSJoe Perches | $NON_ASCII_UTF8 31415662b3eSJoe Perches}x; 31515662b3eSJoe Perches 3168ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 317fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 3188ed22cadSAndy Whitcroft atomic_t 3198ed22cadSAndy Whitcroft)}; 3208ed22cadSAndy Whitcroft 321691e669bSJoe Perchesour $logFunctions = qr{(?x: 3226e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 3237d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 3246e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 325b0531722SJoe Perches panic| 32606668727SJoe Perches MODULE_[A-Z_]+| 32706668727SJoe Perches seq_vprintf|seq_printf|seq_puts 328691e669bSJoe Perches)}; 329691e669bSJoe Perches 33020112475SJoe Perchesour $signature_tags = qr{(?xi: 33120112475SJoe Perches Signed-off-by:| 33220112475SJoe Perches Acked-by:| 33320112475SJoe Perches Tested-by:| 33420112475SJoe Perches Reviewed-by:| 33520112475SJoe Perches Reported-by:| 3368543ae12SMugunthan V N Suggested-by:| 33720112475SJoe Perches To:| 33820112475SJoe Perches Cc: 33920112475SJoe Perches)}; 34020112475SJoe Perches 3418905a67cSAndy Whitcroftour @typeList = ( 3428905a67cSAndy Whitcroft qr{void}, 343c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 344c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 345c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 346c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 347c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 348c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 349c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 3508905a67cSAndy Whitcroft qr{unsigned}, 3518905a67cSAndy Whitcroft qr{float}, 3528905a67cSAndy Whitcroft qr{double}, 3538905a67cSAndy Whitcroft qr{bool}, 3548905a67cSAndy Whitcroft qr{struct\s+$Ident}, 3558905a67cSAndy Whitcroft qr{union\s+$Ident}, 3568905a67cSAndy Whitcroft qr{enum\s+$Ident}, 3578905a67cSAndy Whitcroft qr{${Ident}_t}, 3588905a67cSAndy Whitcroft qr{${Ident}_handler}, 3598905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 3608905a67cSAndy Whitcroft); 3618716de38SJoe Perchesour @typeListWithAttr = ( 3628716de38SJoe Perches @typeList, 3638716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 3648716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 3658716de38SJoe Perches); 3668716de38SJoe Perches 367c45dcabdSAndy Whitcroftour @modifierList = ( 368c45dcabdSAndy Whitcroft qr{fastcall}, 369c45dcabdSAndy Whitcroft); 3708905a67cSAndy Whitcroft 3717840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 3727840a94cSWolfram Sang irq| 3737840a94cSWolfram Sang memory 3747840a94cSWolfram Sang)}; 3757840a94cSWolfram Sang# memory.h: ARM has a custom one 3767840a94cSWolfram Sang 3778905a67cSAndy Whitcroftsub build_types { 378d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 379d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 3808716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 381c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 3828905a67cSAndy Whitcroft $NonptrType = qr{ 383d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 384cf655043SAndy Whitcroft (?: 3856b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 3868ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 387c45dcabdSAndy Whitcroft (?:${all}\b) 388cf655043SAndy Whitcroft ) 389c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 3908905a67cSAndy Whitcroft }x; 3918716de38SJoe Perches $NonptrTypeWithAttr = qr{ 3928716de38SJoe Perches (?:$Modifier\s+|const\s+)* 3938716de38SJoe Perches (?: 3948716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 3958716de38SJoe Perches (?:$typeTypedefs\b)| 3968716de38SJoe Perches (?:${allWithAttr}\b) 3978716de38SJoe Perches ) 3988716de38SJoe Perches (?:\s+$Modifier|\s+const)* 3998716de38SJoe Perches }x; 4008905a67cSAndy Whitcroft $Type = qr{ 401c45dcabdSAndy Whitcroft $NonptrType 402b337d8b8SAndy Whitcroft (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 403c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 4048905a67cSAndy Whitcroft }x; 4058905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 4068905a67cSAndy Whitcroft} 4078905a67cSAndy Whitcroftbuild_types(); 4086c72ffaaSAndy Whitcroft 4097d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 410d1fe9c09SJoe Perches 411d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 412d1fe9c09SJoe Perches# requires at least perl version v5.10.0 413d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 414d1fe9c09SJoe Perches 415d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 416d1fe9c09SJoe Perchesour $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; 417d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; 4187d2367afSJoe Perches 4197d2367afSJoe Perchessub deparenthesize { 4207d2367afSJoe Perches my ($string) = @_; 4217d2367afSJoe Perches return "" if (!defined($string)); 4227d2367afSJoe Perches $string =~ s@^\s*\(\s*@@g; 4237d2367afSJoe Perches $string =~ s@\s*\)\s*$@@g; 4247d2367afSJoe Perches $string =~ s@\s+@ @g; 4257d2367afSJoe Perches return $string; 4267d2367afSJoe Perches} 4277d2367afSJoe Perches 4283445686aSJoe Perchessub seed_camelcase_file { 4293445686aSJoe Perches my ($file) = @_; 4303445686aSJoe Perches 4313445686aSJoe Perches return if (!(-f $file)); 4323445686aSJoe Perches 4333445686aSJoe Perches local $/; 4343445686aSJoe Perches 4353445686aSJoe Perches open(my $include_file, '<', "$file") 4363445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 4373445686aSJoe Perches my $text = <$include_file>; 4383445686aSJoe Perches close($include_file); 4393445686aSJoe Perches 4403445686aSJoe Perches my @lines = split('\n', $text); 4413445686aSJoe Perches 4423445686aSJoe Perches foreach my $line (@lines) { 4433445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 4443445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 4453445686aSJoe Perches $camelcase{$1} = 1; 44611ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 44711ea516aSJoe Perches $camelcase{$1} = 1; 44811ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 4493445686aSJoe Perches $camelcase{$1} = 1; 4503445686aSJoe Perches } 4513445686aSJoe Perches } 4523445686aSJoe Perches} 4533445686aSJoe Perches 4543445686aSJoe Perchesmy $camelcase_seeded = 0; 4553445686aSJoe Perchessub seed_camelcase_includes { 4563445686aSJoe Perches return if ($camelcase_seeded); 4573445686aSJoe Perches 4583445686aSJoe Perches my $files; 459c707a81dSJoe Perches my $camelcase_cache = ""; 460c707a81dSJoe Perches my @include_files = (); 461c707a81dSJoe Perches 462c707a81dSJoe Perches $camelcase_seeded = 1; 463351b2a1fSJoe Perches 4643445686aSJoe Perches if (-d ".git") { 465351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 466351b2a1fSJoe Perches chomp $git_last_include_commit; 467c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 468c707a81dSJoe Perches } else { 469c707a81dSJoe Perches my $last_mod_date = 0; 470c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 471c707a81dSJoe Perches @include_files = split('\n', $files); 472c707a81dSJoe Perches foreach my $file (@include_files) { 473c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 474c707a81dSJoe Perches localtime((stat $file)[9])); 475c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 476c707a81dSJoe Perches } 477c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 478c707a81dSJoe Perches } 479c707a81dSJoe Perches 480c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 481c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 482c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 483351b2a1fSJoe Perches while (<$camelcase_file>) { 484351b2a1fSJoe Perches chomp; 485351b2a1fSJoe Perches $camelcase{$_} = 1; 486351b2a1fSJoe Perches } 487351b2a1fSJoe Perches close($camelcase_file); 488351b2a1fSJoe Perches 489351b2a1fSJoe Perches return; 490351b2a1fSJoe Perches } 491c707a81dSJoe Perches 492c707a81dSJoe Perches if (-d ".git") { 493c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 494c707a81dSJoe Perches @include_files = split('\n', $files); 4953445686aSJoe Perches } 496c707a81dSJoe Perches 4973445686aSJoe Perches foreach my $file (@include_files) { 4983445686aSJoe Perches seed_camelcase_file($file); 4993445686aSJoe Perches } 500351b2a1fSJoe Perches 501c707a81dSJoe Perches if ($camelcase_cache ne "") { 502351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 503c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 504c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 505351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 506351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 507351b2a1fSJoe Perches } 508351b2a1fSJoe Perches close($camelcase_file); 509351b2a1fSJoe Perches } 5103445686aSJoe Perches} 5113445686aSJoe Perches 5126c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 5130a920b5bSAndy Whitcroft 51400df344fSAndy Whitcroftmy @rawlines = (); 515c2fdda0dSAndy Whitcroftmy @lines = (); 5163705ce5bSJoe Perchesmy @fixed = (); 517c2fdda0dSAndy Whitcroftmy $vname; 5186c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 51921caa13cSAndy Whitcroft my $FILE; 5206c72ffaaSAndy Whitcroft if ($file) { 52121caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 5226c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 52321caa13cSAndy Whitcroft } elsif ($filename eq '-') { 52421caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 5256c72ffaaSAndy Whitcroft } else { 52621caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 5276c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 5286c72ffaaSAndy Whitcroft } 529c2fdda0dSAndy Whitcroft if ($filename eq '-') { 530c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 531c2fdda0dSAndy Whitcroft } else { 532c2fdda0dSAndy Whitcroft $vname = $filename; 533c2fdda0dSAndy Whitcroft } 53421caa13cSAndy Whitcroft while (<$FILE>) { 5350a920b5bSAndy Whitcroft chomp; 53600df344fSAndy Whitcroft push(@rawlines, $_); 5376c72ffaaSAndy Whitcroft } 53821caa13cSAndy Whitcroft close($FILE); 539c2fdda0dSAndy Whitcroft if (!process($filename)) { 5400a920b5bSAndy Whitcroft $exit = 1; 5410a920b5bSAndy Whitcroft } 54200df344fSAndy Whitcroft @rawlines = (); 54313214adfSAndy Whitcroft @lines = (); 5443705ce5bSJoe Perches @fixed = (); 5450a920b5bSAndy Whitcroft} 5460a920b5bSAndy Whitcroft 5470a920b5bSAndy Whitcroftexit($exit); 5480a920b5bSAndy Whitcroft 5490a920b5bSAndy Whitcroftsub top_of_kernel_tree { 5506c72ffaaSAndy Whitcroft my ($root) = @_; 5516c72ffaaSAndy Whitcroft 5526c72ffaaSAndy Whitcroft my @tree_check = ( 5536c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 5546c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 5556c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 5566c72ffaaSAndy Whitcroft ); 5576c72ffaaSAndy Whitcroft 5586c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 5596c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 5600a920b5bSAndy Whitcroft return 0; 5610a920b5bSAndy Whitcroft } 5626c72ffaaSAndy Whitcroft } 5636c72ffaaSAndy Whitcroft return 1; 5646c72ffaaSAndy Whitcroft} 5650a920b5bSAndy Whitcroft 56620112475SJoe Perchessub parse_email { 56720112475SJoe Perches my ($formatted_email) = @_; 56820112475SJoe Perches 56920112475SJoe Perches my $name = ""; 57020112475SJoe Perches my $address = ""; 57120112475SJoe Perches my $comment = ""; 57220112475SJoe Perches 57320112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 57420112475SJoe Perches $name = $1; 57520112475SJoe Perches $address = $2; 57620112475SJoe Perches $comment = $3 if defined $3; 57720112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 57820112475SJoe Perches $address = $1; 57920112475SJoe Perches $comment = $2 if defined $2; 58020112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 58120112475SJoe Perches $address = $1; 58220112475SJoe Perches $comment = $2 if defined $2; 58320112475SJoe Perches $formatted_email =~ s/$address.*$//; 58420112475SJoe Perches $name = $formatted_email; 5853705ce5bSJoe Perches $name = trim($name); 58620112475SJoe Perches $name =~ s/^\"|\"$//g; 58720112475SJoe Perches # If there's a name left after stripping spaces and 58820112475SJoe Perches # leading quotes, and the address doesn't have both 58920112475SJoe Perches # leading and trailing angle brackets, the address 59020112475SJoe Perches # is invalid. ie: 59120112475SJoe Perches # "joe smith [email protected]" bad 59220112475SJoe Perches # "joe smith <[email protected]" bad 59320112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 59420112475SJoe Perches $name = ""; 59520112475SJoe Perches $address = ""; 59620112475SJoe Perches $comment = ""; 59720112475SJoe Perches } 59820112475SJoe Perches } 59920112475SJoe Perches 6003705ce5bSJoe Perches $name = trim($name); 60120112475SJoe Perches $name =~ s/^\"|\"$//g; 6023705ce5bSJoe Perches $address = trim($address); 60320112475SJoe Perches $address =~ s/^\<|\>$//g; 60420112475SJoe Perches 60520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 60620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 60720112475SJoe Perches $name = "\"$name\""; 60820112475SJoe Perches } 60920112475SJoe Perches 61020112475SJoe Perches return ($name, $address, $comment); 61120112475SJoe Perches} 61220112475SJoe Perches 61320112475SJoe Perchessub format_email { 61420112475SJoe Perches my ($name, $address) = @_; 61520112475SJoe Perches 61620112475SJoe Perches my $formatted_email; 61720112475SJoe Perches 6183705ce5bSJoe Perches $name = trim($name); 61920112475SJoe Perches $name =~ s/^\"|\"$//g; 6203705ce5bSJoe Perches $address = trim($address); 62120112475SJoe Perches 62220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 62320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 62420112475SJoe Perches $name = "\"$name\""; 62520112475SJoe Perches } 62620112475SJoe Perches 62720112475SJoe Perches if ("$name" eq "") { 62820112475SJoe Perches $formatted_email = "$address"; 62920112475SJoe Perches } else { 63020112475SJoe Perches $formatted_email = "$name <$address>"; 63120112475SJoe Perches } 63220112475SJoe Perches 63320112475SJoe Perches return $formatted_email; 63420112475SJoe Perches} 63520112475SJoe Perches 636000d1cc1SJoe Perchessub which_conf { 637000d1cc1SJoe Perches my ($conf) = @_; 638000d1cc1SJoe Perches 639000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 640000d1cc1SJoe Perches if (-e "$path/$conf") { 641000d1cc1SJoe Perches return "$path/$conf"; 642000d1cc1SJoe Perches } 643000d1cc1SJoe Perches } 644000d1cc1SJoe Perches 645000d1cc1SJoe Perches return ""; 646000d1cc1SJoe Perches} 647000d1cc1SJoe Perches 6480a920b5bSAndy Whitcroftsub expand_tabs { 6490a920b5bSAndy Whitcroft my ($str) = @_; 6500a920b5bSAndy Whitcroft 6510a920b5bSAndy Whitcroft my $res = ''; 6520a920b5bSAndy Whitcroft my $n = 0; 6530a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 6540a920b5bSAndy Whitcroft if ($c eq "\t") { 6550a920b5bSAndy Whitcroft $res .= ' '; 6560a920b5bSAndy Whitcroft $n++; 6570a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 6580a920b5bSAndy Whitcroft $res .= ' '; 6590a920b5bSAndy Whitcroft } 6600a920b5bSAndy Whitcroft next; 6610a920b5bSAndy Whitcroft } 6620a920b5bSAndy Whitcroft $res .= $c; 6630a920b5bSAndy Whitcroft $n++; 6640a920b5bSAndy Whitcroft } 6650a920b5bSAndy Whitcroft 6660a920b5bSAndy Whitcroft return $res; 6670a920b5bSAndy Whitcroft} 6686c72ffaaSAndy Whitcroftsub copy_spacing { 669773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 6706c72ffaaSAndy Whitcroft return $res; 6716c72ffaaSAndy Whitcroft} 6720a920b5bSAndy Whitcroft 6734a0df2efSAndy Whitcroftsub line_stats { 6744a0df2efSAndy Whitcroft my ($line) = @_; 6754a0df2efSAndy Whitcroft 6764a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 6774a0df2efSAndy Whitcroft $line =~ s/^.//; 6784a0df2efSAndy Whitcroft $line = expand_tabs($line); 6794a0df2efSAndy Whitcroft 6804a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 6814a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 6824a0df2efSAndy Whitcroft 6834a0df2efSAndy Whitcroft return (length($line), length($white)); 6844a0df2efSAndy Whitcroft} 6854a0df2efSAndy Whitcroft 686773647a0SAndy Whitcroftmy $sanitise_quote = ''; 687773647a0SAndy Whitcroft 688773647a0SAndy Whitcroftsub sanitise_line_reset { 689773647a0SAndy Whitcroft my ($in_comment) = @_; 690773647a0SAndy Whitcroft 691773647a0SAndy Whitcroft if ($in_comment) { 692773647a0SAndy Whitcroft $sanitise_quote = '*/'; 693773647a0SAndy Whitcroft } else { 694773647a0SAndy Whitcroft $sanitise_quote = ''; 695773647a0SAndy Whitcroft } 696773647a0SAndy Whitcroft} 69700df344fSAndy Whitcroftsub sanitise_line { 69800df344fSAndy Whitcroft my ($line) = @_; 69900df344fSAndy Whitcroft 70000df344fSAndy Whitcroft my $res = ''; 70100df344fSAndy Whitcroft my $l = ''; 70200df344fSAndy Whitcroft 703c2fdda0dSAndy Whitcroft my $qlen = 0; 704773647a0SAndy Whitcroft my $off = 0; 705773647a0SAndy Whitcroft my $c; 70600df344fSAndy Whitcroft 707773647a0SAndy Whitcroft # Always copy over the diff marker. 708773647a0SAndy Whitcroft $res = substr($line, 0, 1); 709773647a0SAndy Whitcroft 710773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 711773647a0SAndy Whitcroft $c = substr($line, $off, 1); 712773647a0SAndy Whitcroft 713773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 714773647a0SAndy Whitcroft # and end, all to $;. 715773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 716773647a0SAndy Whitcroft $sanitise_quote = '*/'; 717773647a0SAndy Whitcroft 718773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 719773647a0SAndy Whitcroft $off++; 72000df344fSAndy Whitcroft next; 721773647a0SAndy Whitcroft } 72281bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 723773647a0SAndy Whitcroft $sanitise_quote = ''; 724773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 725773647a0SAndy Whitcroft $off++; 726773647a0SAndy Whitcroft next; 727773647a0SAndy Whitcroft } 728113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 729113f04a8SDaniel Walker $sanitise_quote = '//'; 730113f04a8SDaniel Walker 731113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 732113f04a8SDaniel Walker $off++; 733113f04a8SDaniel Walker next; 734113f04a8SDaniel Walker } 735773647a0SAndy Whitcroft 736773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 737773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 738773647a0SAndy Whitcroft $c eq "\\") { 739773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 740773647a0SAndy Whitcroft $off++; 741773647a0SAndy Whitcroft next; 742773647a0SAndy Whitcroft } 743773647a0SAndy Whitcroft # Regular quotes. 744773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 745773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 746773647a0SAndy Whitcroft $sanitise_quote = $c; 747773647a0SAndy Whitcroft 748773647a0SAndy Whitcroft substr($res, $off, 1, $c); 749773647a0SAndy Whitcroft next; 750773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 751773647a0SAndy Whitcroft $sanitise_quote = ''; 75200df344fSAndy Whitcroft } 75300df344fSAndy Whitcroft } 754773647a0SAndy Whitcroft 755fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 756773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 757773647a0SAndy Whitcroft substr($res, $off, 1, $;); 758113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 759113f04a8SDaniel Walker substr($res, $off, 1, $;); 760773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 761773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 76200df344fSAndy Whitcroft } else { 763773647a0SAndy Whitcroft substr($res, $off, 1, $c); 76400df344fSAndy Whitcroft } 765c2fdda0dSAndy Whitcroft } 766c2fdda0dSAndy Whitcroft 767113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 768113f04a8SDaniel Walker $sanitise_quote = ''; 769113f04a8SDaniel Walker } 770113f04a8SDaniel Walker 771c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 772c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 773c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 774c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 775c2fdda0dSAndy Whitcroft 776c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 777c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 778c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 779c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 780c2fdda0dSAndy Whitcroft } 781c2fdda0dSAndy Whitcroft 78200df344fSAndy Whitcroft return $res; 78300df344fSAndy Whitcroft} 78400df344fSAndy Whitcroft 785a6962d72SJoe Perchessub get_quoted_string { 786a6962d72SJoe Perches my ($line, $rawline) = @_; 787a6962d72SJoe Perches 788a6962d72SJoe Perches return "" if ($line !~ m/(\"[X]+\")/g); 789a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 790a6962d72SJoe Perches} 791a6962d72SJoe Perches 7928905a67cSAndy Whitcroftsub ctx_statement_block { 7938905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 7948905a67cSAndy Whitcroft my $line = $linenr - 1; 7958905a67cSAndy Whitcroft my $blk = ''; 7968905a67cSAndy Whitcroft my $soff = $off; 7978905a67cSAndy Whitcroft my $coff = $off - 1; 798773647a0SAndy Whitcroft my $coff_set = 0; 7998905a67cSAndy Whitcroft 80013214adfSAndy Whitcroft my $loff = 0; 80113214adfSAndy Whitcroft 8028905a67cSAndy Whitcroft my $type = ''; 8038905a67cSAndy Whitcroft my $level = 0; 804a2750645SAndy Whitcroft my @stack = (); 805cf655043SAndy Whitcroft my $p; 8068905a67cSAndy Whitcroft my $c; 8078905a67cSAndy Whitcroft my $len = 0; 80813214adfSAndy Whitcroft 80913214adfSAndy Whitcroft my $remainder; 8108905a67cSAndy Whitcroft while (1) { 811a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 812a2750645SAndy Whitcroft 813773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 8148905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 8158905a67cSAndy Whitcroft # context. 8168905a67cSAndy Whitcroft if ($off >= $len) { 8178905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 818dea33496SAndy Whitcroft last if (!defined $lines[$line]); 819c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 8208905a67cSAndy Whitcroft $remain--; 82113214adfSAndy Whitcroft $loff = $len; 822c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 8238905a67cSAndy Whitcroft $len = length($blk); 8248905a67cSAndy Whitcroft $line++; 8258905a67cSAndy Whitcroft last; 8268905a67cSAndy Whitcroft } 8278905a67cSAndy Whitcroft # Bail if there is no further context. 8288905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 82913214adfSAndy Whitcroft if ($off >= $len) { 8308905a67cSAndy Whitcroft last; 8318905a67cSAndy Whitcroft } 832f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 833f74bd194SAndy Whitcroft $level++; 834f74bd194SAndy Whitcroft $type = '#'; 835f74bd194SAndy Whitcroft } 8368905a67cSAndy Whitcroft } 837cf655043SAndy Whitcroft $p = $c; 8388905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 83913214adfSAndy Whitcroft $remainder = substr($blk, $off); 8408905a67cSAndy Whitcroft 841773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 8424635f4fbSAndy Whitcroft 8434635f4fbSAndy Whitcroft # Handle nested #if/#else. 8444635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 8454635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 8464635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 8474635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 8484635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 8494635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 8504635f4fbSAndy Whitcroft } 8514635f4fbSAndy Whitcroft 8528905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 8538905a67cSAndy Whitcroft # outermost level. 8548905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 8558905a67cSAndy Whitcroft last; 8568905a67cSAndy Whitcroft } 8578905a67cSAndy Whitcroft 85813214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 859773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 860773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 861773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 862773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 863773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 864773647a0SAndy Whitcroft $coff_set = 1; 865773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 866773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 86713214adfSAndy Whitcroft } 86813214adfSAndy Whitcroft 8698905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 8708905a67cSAndy Whitcroft $level++; 8718905a67cSAndy Whitcroft $type = '('; 8728905a67cSAndy Whitcroft } 8738905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 8748905a67cSAndy Whitcroft $level--; 8758905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 8768905a67cSAndy Whitcroft 8778905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 8788905a67cSAndy Whitcroft $coff = $off; 879773647a0SAndy Whitcroft $coff_set = 1; 880773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 8818905a67cSAndy Whitcroft } 8828905a67cSAndy Whitcroft } 8838905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 8848905a67cSAndy Whitcroft $level++; 8858905a67cSAndy Whitcroft $type = '{'; 8868905a67cSAndy Whitcroft } 8878905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 8888905a67cSAndy Whitcroft $level--; 8898905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 8908905a67cSAndy Whitcroft 8918905a67cSAndy Whitcroft if ($level == 0) { 892b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 893b998e001SPatrick Pannuto $off++; 894b998e001SPatrick Pannuto } 8958905a67cSAndy Whitcroft last; 8968905a67cSAndy Whitcroft } 8978905a67cSAndy Whitcroft } 898f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 899f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 900f74bd194SAndy Whitcroft $level--; 901f74bd194SAndy Whitcroft $type = ''; 902f74bd194SAndy Whitcroft $off++; 903f74bd194SAndy Whitcroft last; 904f74bd194SAndy Whitcroft } 9058905a67cSAndy Whitcroft $off++; 9068905a67cSAndy Whitcroft } 907a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 90813214adfSAndy Whitcroft if ($off == $len) { 909a3bb97a7SAndy Whitcroft $loff = $len + 1; 91013214adfSAndy Whitcroft $line++; 91113214adfSAndy Whitcroft $remain--; 91213214adfSAndy Whitcroft } 9138905a67cSAndy Whitcroft 9148905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 9158905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 9168905a67cSAndy Whitcroft 9178905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 9188905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 9198905a67cSAndy Whitcroft 920773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 92113214adfSAndy Whitcroft 92213214adfSAndy Whitcroft return ($statement, $condition, 92313214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 92413214adfSAndy Whitcroft} 92513214adfSAndy Whitcroft 926cf655043SAndy Whitcroftsub statement_lines { 927cf655043SAndy Whitcroft my ($stmt) = @_; 928cf655043SAndy Whitcroft 929cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 930cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 931cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 932cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 933cf655043SAndy Whitcroft 934cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 935cf655043SAndy Whitcroft 936cf655043SAndy Whitcroft return $#stmt_lines + 2; 937cf655043SAndy Whitcroft} 938cf655043SAndy Whitcroft 939cf655043SAndy Whitcroftsub statement_rawlines { 940cf655043SAndy Whitcroft my ($stmt) = @_; 941cf655043SAndy Whitcroft 942cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 943cf655043SAndy Whitcroft 944cf655043SAndy Whitcroft return $#stmt_lines + 2; 945cf655043SAndy Whitcroft} 946cf655043SAndy Whitcroft 947cf655043SAndy Whitcroftsub statement_block_size { 948cf655043SAndy Whitcroft my ($stmt) = @_; 949cf655043SAndy Whitcroft 950cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 951cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 952cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 953cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 954cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 955cf655043SAndy Whitcroft 956cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 957cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 958cf655043SAndy Whitcroft 959cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 960cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 961cf655043SAndy Whitcroft 962cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 963cf655043SAndy Whitcroft return $stmt_lines; 964cf655043SAndy Whitcroft } else { 965cf655043SAndy Whitcroft return $stmt_statements; 966cf655043SAndy Whitcroft } 967cf655043SAndy Whitcroft} 968cf655043SAndy Whitcroft 96913214adfSAndy Whitcroftsub ctx_statement_full { 97013214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 97113214adfSAndy Whitcroft my ($statement, $condition, $level); 97213214adfSAndy Whitcroft 97313214adfSAndy Whitcroft my (@chunks); 97413214adfSAndy Whitcroft 975cf655043SAndy Whitcroft # Grab the first conditional/block pair. 97613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 97713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 978773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 97913214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 980cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 981cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 982cf655043SAndy Whitcroft } 983cf655043SAndy Whitcroft 984cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 985cf655043SAndy Whitcroft # could continue the statement. 986cf655043SAndy Whitcroft for (;;) { 98713214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 98813214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 989cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 990773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 991cf655043SAndy Whitcroft #print "C: push\n"; 992cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 99313214adfSAndy Whitcroft } 99413214adfSAndy Whitcroft 99513214adfSAndy Whitcroft return ($level, $linenr, @chunks); 9968905a67cSAndy Whitcroft} 9978905a67cSAndy Whitcroft 9984a0df2efSAndy Whitcroftsub ctx_block_get { 999f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 10004a0df2efSAndy Whitcroft my $line; 10014a0df2efSAndy Whitcroft my $start = $linenr - 1; 10024a0df2efSAndy Whitcroft my $blk = ''; 10034a0df2efSAndy Whitcroft my @o; 10044a0df2efSAndy Whitcroft my @c; 10054a0df2efSAndy Whitcroft my @res = (); 10064a0df2efSAndy Whitcroft 1007f0a594c1SAndy Whitcroft my $level = 0; 10084635f4fbSAndy Whitcroft my @stack = ($level); 100900df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 101000df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 101100df344fSAndy Whitcroft $remain--; 101200df344fSAndy Whitcroft 101300df344fSAndy Whitcroft $blk .= $rawlines[$line]; 10144635f4fbSAndy Whitcroft 10154635f4fbSAndy Whitcroft # Handle nested #if/#else. 101601464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 10174635f4fbSAndy Whitcroft push(@stack, $level); 101801464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 10194635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 102001464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 10214635f4fbSAndy Whitcroft $level = pop(@stack); 10224635f4fbSAndy Whitcroft } 10234635f4fbSAndy Whitcroft 102401464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1025f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1026f0a594c1SAndy Whitcroft if ($off > 0) { 1027f0a594c1SAndy Whitcroft $off--; 1028f0a594c1SAndy Whitcroft next; 1029f0a594c1SAndy Whitcroft } 10304a0df2efSAndy Whitcroft 1031f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1032f0a594c1SAndy Whitcroft $level--; 1033f0a594c1SAndy Whitcroft last if ($level == 0); 1034f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1035f0a594c1SAndy Whitcroft $level++; 1036f0a594c1SAndy Whitcroft } 1037f0a594c1SAndy Whitcroft } 10384a0df2efSAndy Whitcroft 1039f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 104000df344fSAndy Whitcroft push(@res, $rawlines[$line]); 10414a0df2efSAndy Whitcroft } 10424a0df2efSAndy Whitcroft 1043f0a594c1SAndy Whitcroft last if ($level == 0); 10444a0df2efSAndy Whitcroft } 10454a0df2efSAndy Whitcroft 1046f0a594c1SAndy Whitcroft return ($level, @res); 10474a0df2efSAndy Whitcroft} 10484a0df2efSAndy Whitcroftsub ctx_block_outer { 10494a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10504a0df2efSAndy Whitcroft 1051f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1052f0a594c1SAndy Whitcroft return @r; 10534a0df2efSAndy Whitcroft} 10544a0df2efSAndy Whitcroftsub ctx_block { 10554a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10564a0df2efSAndy Whitcroft 1057f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1058f0a594c1SAndy Whitcroft return @r; 1059653d4876SAndy Whitcroft} 1060653d4876SAndy Whitcroftsub ctx_statement { 1061f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1062f0a594c1SAndy Whitcroft 1063f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1064f0a594c1SAndy Whitcroft return @r; 1065f0a594c1SAndy Whitcroft} 1066f0a594c1SAndy Whitcroftsub ctx_block_level { 1067653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1068653d4876SAndy Whitcroft 1069f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 10704a0df2efSAndy Whitcroft} 10719c0ca6f9SAndy Whitcroftsub ctx_statement_level { 10729c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 10739c0ca6f9SAndy Whitcroft 10749c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 10759c0ca6f9SAndy Whitcroft} 10764a0df2efSAndy Whitcroft 10774a0df2efSAndy Whitcroftsub ctx_locate_comment { 10784a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 10794a0df2efSAndy Whitcroft 10804a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1081beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 10824a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 10834a0df2efSAndy Whitcroft 10844a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 10854a0df2efSAndy Whitcroft # comment. 10864a0df2efSAndy Whitcroft my $in_comment = 0; 10874a0df2efSAndy Whitcroft $current_comment = ''; 10884a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 108900df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 109000df344fSAndy Whitcroft #warn " $line\n"; 10914a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 10924a0df2efSAndy Whitcroft $in_comment = 1; 10934a0df2efSAndy Whitcroft } 10944a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 10954a0df2efSAndy Whitcroft $in_comment = 1; 10964a0df2efSAndy Whitcroft } 10974a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 10984a0df2efSAndy Whitcroft $current_comment = ''; 10994a0df2efSAndy Whitcroft } 11004a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 11014a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 11024a0df2efSAndy Whitcroft $in_comment = 0; 11034a0df2efSAndy Whitcroft } 11044a0df2efSAndy Whitcroft } 11054a0df2efSAndy Whitcroft 11064a0df2efSAndy Whitcroft chomp($current_comment); 11074a0df2efSAndy Whitcroft return($current_comment); 11084a0df2efSAndy Whitcroft} 11094a0df2efSAndy Whitcroftsub ctx_has_comment { 11104a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 11114a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 11124a0df2efSAndy Whitcroft 111300df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 11144a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 11154a0df2efSAndy Whitcroft 11164a0df2efSAndy Whitcroft return ($cmt ne ''); 11174a0df2efSAndy Whitcroft} 11184a0df2efSAndy Whitcroft 11194d001e4dSAndy Whitcroftsub raw_line { 11204d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 11214d001e4dSAndy Whitcroft 11224d001e4dSAndy Whitcroft my $offset = $linenr - 1; 11234d001e4dSAndy Whitcroft $cnt++; 11244d001e4dSAndy Whitcroft 11254d001e4dSAndy Whitcroft my $line; 11264d001e4dSAndy Whitcroft while ($cnt) { 11274d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 11284d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 11294d001e4dSAndy Whitcroft $cnt--; 11304d001e4dSAndy Whitcroft } 11314d001e4dSAndy Whitcroft 11324d001e4dSAndy Whitcroft return $line; 11334d001e4dSAndy Whitcroft} 11344d001e4dSAndy Whitcroft 11350a920b5bSAndy Whitcroftsub cat_vet { 11360a920b5bSAndy Whitcroft my ($vet) = @_; 11379c0ca6f9SAndy Whitcroft my ($res, $coded); 11380a920b5bSAndy Whitcroft 11399c0ca6f9SAndy Whitcroft $res = ''; 11406c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 11416c72ffaaSAndy Whitcroft $res .= $1; 11426c72ffaaSAndy Whitcroft if ($2 ne '') { 11439c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 11446c72ffaaSAndy Whitcroft $res .= $coded; 11456c72ffaaSAndy Whitcroft } 11469c0ca6f9SAndy Whitcroft } 11479c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 11480a920b5bSAndy Whitcroft 11499c0ca6f9SAndy Whitcroft return $res; 11500a920b5bSAndy Whitcroft} 11510a920b5bSAndy Whitcroft 1152c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1153cf655043SAndy Whitcroftmy $av_pending; 1154c2fdda0dSAndy Whitcroftmy @av_paren_type; 11551f65f947SAndy Whitcroftmy $av_pend_colon; 1156c2fdda0dSAndy Whitcroft 1157c2fdda0dSAndy Whitcroftsub annotate_reset { 1158c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1159cf655043SAndy Whitcroft $av_pending = '_'; 1160cf655043SAndy Whitcroft @av_paren_type = ('E'); 11611f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1162c2fdda0dSAndy Whitcroft} 1163c2fdda0dSAndy Whitcroft 11646c72ffaaSAndy Whitcroftsub annotate_values { 11656c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 11666c72ffaaSAndy Whitcroft 11676c72ffaaSAndy Whitcroft my $res; 11681f65f947SAndy Whitcroft my $var = '_' x length($stream); 11696c72ffaaSAndy Whitcroft my $cur = $stream; 11706c72ffaaSAndy Whitcroft 1171c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 11726c72ffaaSAndy Whitcroft 11736c72ffaaSAndy Whitcroft while (length($cur)) { 1174773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1175cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1176171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 11776c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1178c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1179c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1180cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1181c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 11826c72ffaaSAndy Whitcroft } 11836c72ffaaSAndy Whitcroft 1184c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 11859446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 11869446ef56SAndy Whitcroft push(@av_paren_type, $type); 1187addcdceaSAndy Whitcroft $type = 'c'; 11889446ef56SAndy Whitcroft 1189e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1190c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 11916c72ffaaSAndy Whitcroft $type = 'T'; 11926c72ffaaSAndy Whitcroft 1193389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1194389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1195389a2fe5SAndy Whitcroft $type = 'T'; 1196389a2fe5SAndy Whitcroft 1197c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1198171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1199c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1200171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1201171ae1a4SAndy Whitcroft if ($2 ne '') { 1202cf655043SAndy Whitcroft $av_pending = 'N'; 1203171ae1a4SAndy Whitcroft } 1204171ae1a4SAndy Whitcroft $type = 'E'; 1205171ae1a4SAndy Whitcroft 1206c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1207171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1208171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1209171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 12106c72ffaaSAndy Whitcroft 1211c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1212cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1213c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1214cf655043SAndy Whitcroft 1215cf655043SAndy Whitcroft push(@av_paren_type, $type); 1216cf655043SAndy Whitcroft push(@av_paren_type, $type); 1217171ae1a4SAndy Whitcroft $type = 'E'; 1218cf655043SAndy Whitcroft 1219c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1220cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1221cf655043SAndy Whitcroft $av_preprocessor = 1; 1222cf655043SAndy Whitcroft 1223cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1224cf655043SAndy Whitcroft 1225171ae1a4SAndy Whitcroft $type = 'E'; 1226cf655043SAndy Whitcroft 1227c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1228cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1229cf655043SAndy Whitcroft 1230cf655043SAndy Whitcroft $av_preprocessor = 1; 1231cf655043SAndy Whitcroft 1232cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1233cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1234cf655043SAndy Whitcroft pop(@av_paren_type); 1235cf655043SAndy Whitcroft push(@av_paren_type, $type); 1236171ae1a4SAndy Whitcroft $type = 'E'; 12376c72ffaaSAndy Whitcroft 12386c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1239c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 12406c72ffaaSAndy Whitcroft 1241171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1242171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1243171ae1a4SAndy Whitcroft $av_pending = $type; 1244171ae1a4SAndy Whitcroft $type = 'N'; 1245171ae1a4SAndy Whitcroft 12466c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1247c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 12486c72ffaaSAndy Whitcroft if (defined $2) { 1249cf655043SAndy Whitcroft $av_pending = 'V'; 12506c72ffaaSAndy Whitcroft } 12516c72ffaaSAndy Whitcroft $type = 'N'; 12526c72ffaaSAndy Whitcroft 125314b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1254c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 125514b111c1SAndy Whitcroft $av_pending = 'E'; 12566c72ffaaSAndy Whitcroft $type = 'N'; 12576c72ffaaSAndy Whitcroft 12581f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 12591f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 12601f65f947SAndy Whitcroft $av_pend_colon = 'C'; 12611f65f947SAndy Whitcroft $type = 'N'; 12621f65f947SAndy Whitcroft 126314b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1264c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 12656c72ffaaSAndy Whitcroft $type = 'N'; 12666c72ffaaSAndy Whitcroft 12676c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1268c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1269cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1270cf655043SAndy Whitcroft $av_pending = '_'; 12716c72ffaaSAndy Whitcroft $type = 'N'; 12726c72ffaaSAndy Whitcroft 12736c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1274cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1275cf655043SAndy Whitcroft if ($new_type ne '_') { 1276cf655043SAndy Whitcroft $type = $new_type; 1277c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1278c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 12796c72ffaaSAndy Whitcroft } else { 1280c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 12816c72ffaaSAndy Whitcroft } 12826c72ffaaSAndy Whitcroft 1283c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1284c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1285c8cb2ca3SAndy Whitcroft $type = 'V'; 1286cf655043SAndy Whitcroft $av_pending = 'V'; 12876c72ffaaSAndy Whitcroft 12888e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 12898e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 12901f65f947SAndy Whitcroft $av_pend_colon = 'B'; 12918e761b04SAndy Whitcroft } elsif ($type eq 'E') { 12928e761b04SAndy Whitcroft $av_pend_colon = 'L'; 12931f65f947SAndy Whitcroft } 12941f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 12951f65f947SAndy Whitcroft $type = 'V'; 12961f65f947SAndy Whitcroft 12976c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1298c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 12996c72ffaaSAndy Whitcroft $type = 'V'; 13006c72ffaaSAndy Whitcroft 13016c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1302c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 13036c72ffaaSAndy Whitcroft $type = 'N'; 13046c72ffaaSAndy Whitcroft 1305cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1306c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 130713214adfSAndy Whitcroft $type = 'E'; 13081f65f947SAndy Whitcroft $av_pend_colon = 'O'; 130913214adfSAndy Whitcroft 13108e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 13118e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 13128e761b04SAndy Whitcroft $type = 'C'; 13138e761b04SAndy Whitcroft 13141f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 13151f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 13161f65f947SAndy Whitcroft $type = 'N'; 13171f65f947SAndy Whitcroft 13181f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 13191f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 13201f65f947SAndy Whitcroft 13211f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 13221f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 13231f65f947SAndy Whitcroft $type = 'E'; 13241f65f947SAndy Whitcroft } else { 13251f65f947SAndy Whitcroft $type = 'N'; 13261f65f947SAndy Whitcroft } 13271f65f947SAndy Whitcroft $av_pend_colon = 'O'; 13281f65f947SAndy Whitcroft 13298e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 133013214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 13316c72ffaaSAndy Whitcroft $type = 'N'; 13326c72ffaaSAndy Whitcroft 13330d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 133474048ed8SAndy Whitcroft my $variant; 133574048ed8SAndy Whitcroft 133674048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 133774048ed8SAndy Whitcroft if ($type eq 'V') { 133874048ed8SAndy Whitcroft $variant = 'B'; 133974048ed8SAndy Whitcroft } else { 134074048ed8SAndy Whitcroft $variant = 'U'; 134174048ed8SAndy Whitcroft } 134274048ed8SAndy Whitcroft 134374048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 134474048ed8SAndy Whitcroft $type = 'N'; 134574048ed8SAndy Whitcroft 13466c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1347c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 13486c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 13496c72ffaaSAndy Whitcroft $type = 'N'; 13506c72ffaaSAndy Whitcroft } 13516c72ffaaSAndy Whitcroft 13526c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1353c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 13546c72ffaaSAndy Whitcroft } 13556c72ffaaSAndy Whitcroft if (defined $1) { 13566c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 13576c72ffaaSAndy Whitcroft $res .= $type x length($1); 13586c72ffaaSAndy Whitcroft } 13596c72ffaaSAndy Whitcroft } 13606c72ffaaSAndy Whitcroft 13611f65f947SAndy Whitcroft return ($res, $var); 13626c72ffaaSAndy Whitcroft} 13636c72ffaaSAndy Whitcroft 13648905a67cSAndy Whitcroftsub possible { 136513214adfSAndy Whitcroft my ($possible, $line) = @_; 13669a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 13670776e594SAndy Whitcroft ^(?: 13680776e594SAndy Whitcroft $Modifier| 13690776e594SAndy Whitcroft $Storage| 13700776e594SAndy Whitcroft $Type| 13719a974fdbSAndy Whitcroft DEFINE_\S+ 13729a974fdbSAndy Whitcroft )$| 13739a974fdbSAndy Whitcroft ^(?: 13740776e594SAndy Whitcroft goto| 13750776e594SAndy Whitcroft return| 13760776e594SAndy Whitcroft case| 13770776e594SAndy Whitcroft else| 13780776e594SAndy Whitcroft asm|__asm__| 137989a88353SAndy Whitcroft do| 138089a88353SAndy Whitcroft \#| 138189a88353SAndy Whitcroft \#\#| 13829a974fdbSAndy Whitcroft )(?:\s|$)| 13830776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 13849a974fdbSAndy Whitcroft )}x; 13859a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 13869a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1387c45dcabdSAndy Whitcroft # Check for modifiers. 1388c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1389c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1390c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1391c45dcabdSAndy Whitcroft 1392c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1393c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1394d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 13959a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1396d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1397d2506586SAndy Whitcroft push(@modifierList, $modifier); 1398d2506586SAndy Whitcroft } 13999a974fdbSAndy Whitcroft } 1400c45dcabdSAndy Whitcroft 1401c45dcabdSAndy Whitcroft } else { 140213214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 14038905a67cSAndy Whitcroft push(@typeList, $possible); 1404c45dcabdSAndy Whitcroft } 14058905a67cSAndy Whitcroft build_types(); 14060776e594SAndy Whitcroft } else { 14070776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 14088905a67cSAndy Whitcroft } 14098905a67cSAndy Whitcroft} 14108905a67cSAndy Whitcroft 14116c72ffaaSAndy Whitcroftmy $prefix = ''; 14126c72ffaaSAndy Whitcroft 1413000d1cc1SJoe Perchessub show_type { 141491bfe484SJoe Perches return defined $use_type{$_[0]} if (scalar keys %use_type > 0); 141591bfe484SJoe Perches 1416000d1cc1SJoe Perches return !defined $ignore_type{$_[0]}; 1417000d1cc1SJoe Perches} 1418000d1cc1SJoe Perches 1419f0a594c1SAndy Whitcroftsub report { 1420000d1cc1SJoe Perches if (!show_type($_[1]) || 1421000d1cc1SJoe Perches (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { 1422773647a0SAndy Whitcroft return 0; 1423773647a0SAndy Whitcroft } 1424000d1cc1SJoe Perches my $line; 1425000d1cc1SJoe Perches if ($show_types) { 1426000d1cc1SJoe Perches $line = "$prefix$_[0]:$_[1]: $_[2]\n"; 1427000d1cc1SJoe Perches } else { 1428000d1cc1SJoe Perches $line = "$prefix$_[0]: $_[2]\n"; 1429000d1cc1SJoe Perches } 14308905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 14318905a67cSAndy Whitcroft 143213214adfSAndy Whitcroft push(our @report, $line); 1433773647a0SAndy Whitcroft 1434773647a0SAndy Whitcroft return 1; 1435f0a594c1SAndy Whitcroft} 1436f0a594c1SAndy Whitcroftsub report_dump { 143713214adfSAndy Whitcroft our @report; 1438f0a594c1SAndy Whitcroft} 1439000d1cc1SJoe Perches 1440de7d4f0eSAndy Whitcroftsub ERROR { 1441000d1cc1SJoe Perches if (report("ERROR", $_[0], $_[1])) { 1442de7d4f0eSAndy Whitcroft our $clean = 0; 14436c72ffaaSAndy Whitcroft our $cnt_error++; 14443705ce5bSJoe Perches return 1; 1445de7d4f0eSAndy Whitcroft } 14463705ce5bSJoe Perches return 0; 1447773647a0SAndy Whitcroft} 1448de7d4f0eSAndy Whitcroftsub WARN { 1449000d1cc1SJoe Perches if (report("WARNING", $_[0], $_[1])) { 1450de7d4f0eSAndy Whitcroft our $clean = 0; 14516c72ffaaSAndy Whitcroft our $cnt_warn++; 14523705ce5bSJoe Perches return 1; 1453de7d4f0eSAndy Whitcroft } 14543705ce5bSJoe Perches return 0; 1455773647a0SAndy Whitcroft} 1456de7d4f0eSAndy Whitcroftsub CHK { 1457000d1cc1SJoe Perches if ($check && report("CHECK", $_[0], $_[1])) { 1458de7d4f0eSAndy Whitcroft our $clean = 0; 14596c72ffaaSAndy Whitcroft our $cnt_chk++; 14603705ce5bSJoe Perches return 1; 14616c72ffaaSAndy Whitcroft } 14623705ce5bSJoe Perches return 0; 1463de7d4f0eSAndy Whitcroft} 1464de7d4f0eSAndy Whitcroft 14656ecd9674SAndy Whitcroftsub check_absolute_file { 14666ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 14676ecd9674SAndy Whitcroft my $file = $absolute; 14686ecd9674SAndy Whitcroft 14696ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 14706ecd9674SAndy Whitcroft 14716ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 14726ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 14736ecd9674SAndy Whitcroft if (-f "$root/$file") { 14746ecd9674SAndy Whitcroft ##print "file<$file>\n"; 14756ecd9674SAndy Whitcroft last; 14766ecd9674SAndy Whitcroft } 14776ecd9674SAndy Whitcroft } 14786ecd9674SAndy Whitcroft if (! -f _) { 14796ecd9674SAndy Whitcroft return 0; 14806ecd9674SAndy Whitcroft } 14816ecd9674SAndy Whitcroft 14826ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 14836ecd9674SAndy Whitcroft my $prefix = $absolute; 14846ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 14856ecd9674SAndy Whitcroft 14866ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 14876ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1488000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1489000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 14906ecd9674SAndy Whitcroft } 14916ecd9674SAndy Whitcroft} 14926ecd9674SAndy Whitcroft 14933705ce5bSJoe Perchessub trim { 14943705ce5bSJoe Perches my ($string) = @_; 14953705ce5bSJoe Perches 1496b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1497b34c648bSJoe Perches 1498b34c648bSJoe Perches return $string; 1499b34c648bSJoe Perches} 1500b34c648bSJoe Perches 1501b34c648bSJoe Perchessub ltrim { 1502b34c648bSJoe Perches my ($string) = @_; 1503b34c648bSJoe Perches 1504b34c648bSJoe Perches $string =~ s/^\s+//; 1505b34c648bSJoe Perches 1506b34c648bSJoe Perches return $string; 1507b34c648bSJoe Perches} 1508b34c648bSJoe Perches 1509b34c648bSJoe Perchessub rtrim { 1510b34c648bSJoe Perches my ($string) = @_; 1511b34c648bSJoe Perches 1512b34c648bSJoe Perches $string =~ s/\s+$//; 15133705ce5bSJoe Perches 15143705ce5bSJoe Perches return $string; 15153705ce5bSJoe Perches} 15163705ce5bSJoe Perches 1517*52ea8506SJoe Perchessub string_find_replace { 1518*52ea8506SJoe Perches my ($string, $find, $replace) = @_; 1519*52ea8506SJoe Perches 1520*52ea8506SJoe Perches $string =~ s/$find/$replace/g; 1521*52ea8506SJoe Perches 1522*52ea8506SJoe Perches return $string; 1523*52ea8506SJoe Perches} 1524*52ea8506SJoe Perches 15253705ce5bSJoe Perchessub tabify { 15263705ce5bSJoe Perches my ($leading) = @_; 15273705ce5bSJoe Perches 15283705ce5bSJoe Perches my $source_indent = 8; 15293705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 15303705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 15313705ce5bSJoe Perches 15323705ce5bSJoe Perches #convert leading spaces to tabs 15333705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 15343705ce5bSJoe Perches #Remove spaces before a tab 15353705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 15363705ce5bSJoe Perches 15373705ce5bSJoe Perches return "$leading"; 15383705ce5bSJoe Perches} 15393705ce5bSJoe Perches 1540d1fe9c09SJoe Perchessub pos_last_openparen { 1541d1fe9c09SJoe Perches my ($line) = @_; 1542d1fe9c09SJoe Perches 1543d1fe9c09SJoe Perches my $pos = 0; 1544d1fe9c09SJoe Perches 1545d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1546d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1547d1fe9c09SJoe Perches 1548d1fe9c09SJoe Perches my $last_openparen = 0; 1549d1fe9c09SJoe Perches 1550d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1551d1fe9c09SJoe Perches return -1; 1552d1fe9c09SJoe Perches } 1553d1fe9c09SJoe Perches 1554d1fe9c09SJoe Perches my $len = length($line); 1555d1fe9c09SJoe Perches 1556d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1557d1fe9c09SJoe Perches my $string = substr($line, $pos); 1558d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1559d1fe9c09SJoe Perches $pos += length($1) - 1; 1560d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1561d1fe9c09SJoe Perches $last_openparen = $pos; 1562d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1563d1fe9c09SJoe Perches last; 1564d1fe9c09SJoe Perches } 1565d1fe9c09SJoe Perches } 1566d1fe9c09SJoe Perches 1567d1fe9c09SJoe Perches return $last_openparen + 1; 1568d1fe9c09SJoe Perches} 1569d1fe9c09SJoe Perches 15700a920b5bSAndy Whitcroftsub process { 15710a920b5bSAndy Whitcroft my $filename = shift; 15720a920b5bSAndy Whitcroft 15730a920b5bSAndy Whitcroft my $linenr=0; 15740a920b5bSAndy Whitcroft my $prevline=""; 1575c2fdda0dSAndy Whitcroft my $prevrawline=""; 15760a920b5bSAndy Whitcroft my $stashline=""; 1577c2fdda0dSAndy Whitcroft my $stashrawline=""; 15780a920b5bSAndy Whitcroft 15794a0df2efSAndy Whitcroft my $length; 15800a920b5bSAndy Whitcroft my $indent; 15810a920b5bSAndy Whitcroft my $previndent=0; 15820a920b5bSAndy Whitcroft my $stashindent=0; 15830a920b5bSAndy Whitcroft 1584de7d4f0eSAndy Whitcroft our $clean = 1; 15850a920b5bSAndy Whitcroft my $signoff = 0; 15860a920b5bSAndy Whitcroft my $is_patch = 0; 15870a920b5bSAndy Whitcroft 158815662b3eSJoe Perches my $in_header_lines = 1; 158915662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 159015662b3eSJoe Perches 1591fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1592fa64205dSPasi Savanainen 159313214adfSAndy Whitcroft our @report = (); 15946c72ffaaSAndy Whitcroft our $cnt_lines = 0; 15956c72ffaaSAndy Whitcroft our $cnt_error = 0; 15966c72ffaaSAndy Whitcroft our $cnt_warn = 0; 15976c72ffaaSAndy Whitcroft our $cnt_chk = 0; 15986c72ffaaSAndy Whitcroft 15990a920b5bSAndy Whitcroft # Trace the real file/line as we go. 16000a920b5bSAndy Whitcroft my $realfile = ''; 16010a920b5bSAndy Whitcroft my $realline = 0; 16020a920b5bSAndy Whitcroft my $realcnt = 0; 16030a920b5bSAndy Whitcroft my $here = ''; 16040a920b5bSAndy Whitcroft my $in_comment = 0; 1605c2fdda0dSAndy Whitcroft my $comment_edge = 0; 16060a920b5bSAndy Whitcroft my $first_line = 0; 16071e855726SWolfram Sang my $p1_prefix = ''; 16080a920b5bSAndy Whitcroft 160913214adfSAndy Whitcroft my $prev_values = 'E'; 161013214adfSAndy Whitcroft 161113214adfSAndy Whitcroft # suppression flags 1612773647a0SAndy Whitcroft my %suppress_ifbraces; 1613170d3a22SAndy Whitcroft my %suppress_whiletrailers; 16142b474a1aSAndy Whitcroft my %suppress_export; 16153e469cdcSAndy Whitcroft my $suppress_statement = 0; 1616653d4876SAndy Whitcroft 16177e51f197SJoe Perches my %signatures = (); 1618323c1260SJoe Perches 1619c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1620de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1621c2fdda0dSAndy Whitcroft # 1622de7d4f0eSAndy Whitcroft my @setup_docs = (); 1623de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1624773647a0SAndy Whitcroft 1625d8b07710SJoe Perches my $camelcase_file_seeded = 0; 1626d8b07710SJoe Perches 1627773647a0SAndy Whitcroft sanitise_line_reset(); 1628c2fdda0dSAndy Whitcroft my $line; 1629c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1630773647a0SAndy Whitcroft $linenr++; 1631773647a0SAndy Whitcroft $line = $rawline; 1632c2fdda0dSAndy Whitcroft 16333705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 16343705ce5bSJoe Perches 1635773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1636de7d4f0eSAndy Whitcroft $setup_docs = 0; 1637de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1638de7d4f0eSAndy Whitcroft $setup_docs = 1; 1639de7d4f0eSAndy Whitcroft } 1640773647a0SAndy Whitcroft #next; 1641de7d4f0eSAndy Whitcroft } 1642773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1643773647a0SAndy Whitcroft $realline=$1-1; 1644773647a0SAndy Whitcroft if (defined $2) { 1645773647a0SAndy Whitcroft $realcnt=$3+1; 1646773647a0SAndy Whitcroft } else { 1647773647a0SAndy Whitcroft $realcnt=1+1; 1648773647a0SAndy Whitcroft } 1649c45dcabdSAndy Whitcroft $in_comment = 0; 1650773647a0SAndy Whitcroft 1651773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1652773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1653773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1654773647a0SAndy Whitcroft # at context start. 1655773647a0SAndy Whitcroft my $edge; 165601fa9147SAndy Whitcroft my $cnt = $realcnt; 165701fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 165801fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 165901fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 166001fa9147SAndy Whitcroft $cnt--; 166101fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1662721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1663fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1664fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1665fae17daeSAndy Whitcroft ($edge) = $1; 1666fae17daeSAndy Whitcroft last; 1667fae17daeSAndy Whitcroft } 1668773647a0SAndy Whitcroft } 1669773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1670773647a0SAndy Whitcroft $in_comment = 1; 1671773647a0SAndy Whitcroft } 1672773647a0SAndy Whitcroft 1673773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1674773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1675773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1676773647a0SAndy Whitcroft if (!defined $edge && 167783242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1678773647a0SAndy Whitcroft { 1679773647a0SAndy Whitcroft $in_comment = 1; 1680773647a0SAndy Whitcroft } 1681773647a0SAndy Whitcroft 1682773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1683773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1684773647a0SAndy Whitcroft 1685171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1686773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1687171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1688773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1689773647a0SAndy Whitcroft } 1690773647a0SAndy Whitcroft push(@lines, $line); 1691773647a0SAndy Whitcroft 1692773647a0SAndy Whitcroft if ($realcnt > 1) { 1693773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1694773647a0SAndy Whitcroft } else { 1695773647a0SAndy Whitcroft $realcnt = 0; 1696773647a0SAndy Whitcroft } 1697773647a0SAndy Whitcroft 1698773647a0SAndy Whitcroft #print "==>$rawline\n"; 1699773647a0SAndy Whitcroft #print "-->$line\n"; 1700de7d4f0eSAndy Whitcroft 1701de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1702de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1703de7d4f0eSAndy Whitcroft } 1704de7d4f0eSAndy Whitcroft } 1705de7d4f0eSAndy Whitcroft 17066c72ffaaSAndy Whitcroft $prefix = ''; 17076c72ffaaSAndy Whitcroft 1708773647a0SAndy Whitcroft $realcnt = 0; 1709773647a0SAndy Whitcroft $linenr = 0; 17100a920b5bSAndy Whitcroft foreach my $line (@lines) { 17110a920b5bSAndy Whitcroft $linenr++; 17121b5539b1SJoe Perches my $sline = $line; #copy of $line 17131b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 17140a920b5bSAndy Whitcroft 1715c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 17166c72ffaaSAndy Whitcroft 17170a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 17186c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 17190a920b5bSAndy Whitcroft $is_patch = 1; 17204a0df2efSAndy Whitcroft $first_line = $linenr + 1; 17210a920b5bSAndy Whitcroft $realline=$1-1; 17220a920b5bSAndy Whitcroft if (defined $2) { 17230a920b5bSAndy Whitcroft $realcnt=$3+1; 17240a920b5bSAndy Whitcroft } else { 17250a920b5bSAndy Whitcroft $realcnt=1+1; 17260a920b5bSAndy Whitcroft } 1727c2fdda0dSAndy Whitcroft annotate_reset(); 172813214adfSAndy Whitcroft $prev_values = 'E'; 172913214adfSAndy Whitcroft 1730773647a0SAndy Whitcroft %suppress_ifbraces = (); 1731170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 17322b474a1aSAndy Whitcroft %suppress_export = (); 17333e469cdcSAndy Whitcroft $suppress_statement = 0; 17340a920b5bSAndy Whitcroft next; 17350a920b5bSAndy Whitcroft 17364a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 17374a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 17384a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1739773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 17400a920b5bSAndy Whitcroft $realline++; 1741d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 17420a920b5bSAndy Whitcroft 17434a0df2efSAndy Whitcroft # Measure the line length and indent. 1744c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 17450a920b5bSAndy Whitcroft 17460a920b5bSAndy Whitcroft # Track the previous line. 17470a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 17480a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1749c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1750c2fdda0dSAndy Whitcroft 1751773647a0SAndy Whitcroft #warn "line<$line>\n"; 17526c72ffaaSAndy Whitcroft 1753d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1754d8aaf121SAndy Whitcroft $realcnt--; 17550a920b5bSAndy Whitcroft } 17560a920b5bSAndy Whitcroft 1757cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1758cc77cdcaSAndy Whitcroft 17590a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1760773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1761773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1762773647a0SAndy Whitcroft 17636c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 17646c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1765773647a0SAndy Whitcroft 1766773647a0SAndy Whitcroft # extract the filename as it passes 17673bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 17683bf9a009SRabin Vincent $realfile = $1; 17693bf9a009SRabin Vincent $realfile =~ s@^([^/]*)/@@; 1770270c49a0SJoe Perches $in_commit_log = 0; 17713bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1772773647a0SAndy Whitcroft $realfile = $1; 17731e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 1774270c49a0SJoe Perches $in_commit_log = 0; 17751e855726SWolfram Sang 17761e855726SWolfram Sang $p1_prefix = $1; 1777e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1778e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 1779000d1cc1SJoe Perches WARN("PATCH_PREFIX", 1780000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 17811e855726SWolfram Sang } 1782773647a0SAndy Whitcroft 1783c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1784000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 1785000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1786773647a0SAndy Whitcroft } 1787773647a0SAndy Whitcroft next; 1788773647a0SAndy Whitcroft } 1789773647a0SAndy Whitcroft 1790389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 17910a920b5bSAndy Whitcroft 1792c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1793c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1794c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 17950a920b5bSAndy Whitcroft 17966c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 17976c72ffaaSAndy Whitcroft 17983bf9a009SRabin Vincent# Check for incorrect file permissions 17993bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 18003bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 180104db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 180204db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 1803000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 1804000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 18053bf9a009SRabin Vincent } 18063bf9a009SRabin Vincent } 18073bf9a009SRabin Vincent 180820112475SJoe Perches# Check the patch for a signoff: 1809d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 18104a0df2efSAndy Whitcroft $signoff++; 181115662b3eSJoe Perches $in_commit_log = 0; 18120a920b5bSAndy Whitcroft } 181320112475SJoe Perches 181420112475SJoe Perches# Check signature styles 1815270c49a0SJoe Perches if (!$in_header_lines && 1816ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 181720112475SJoe Perches my $space_before = $1; 181820112475SJoe Perches my $sign_off = $2; 181920112475SJoe Perches my $space_after = $3; 182020112475SJoe Perches my $email = $4; 182120112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 182220112475SJoe Perches 1823ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 1824ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 1825ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 1826ce0338dfSJoe Perches } 182720112475SJoe Perches if (defined $space_before && $space_before ne "") { 18283705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18293705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 18303705ce5bSJoe Perches $fix) { 18313705ce5bSJoe Perches $fixed[$linenr - 1] = 18323705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18333705ce5bSJoe Perches } 183420112475SJoe Perches } 183520112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 18363705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18373705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 18383705ce5bSJoe Perches $fix) { 18393705ce5bSJoe Perches $fixed[$linenr - 1] = 18403705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18413705ce5bSJoe Perches } 18423705ce5bSJoe Perches 184320112475SJoe Perches } 184420112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 18453705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18463705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 18473705ce5bSJoe Perches $fix) { 18483705ce5bSJoe Perches $fixed[$linenr - 1] = 18493705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18503705ce5bSJoe Perches } 185120112475SJoe Perches } 185220112475SJoe Perches 185320112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 185420112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 185520112475SJoe Perches if ($suggested_email eq "") { 1856000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 1857000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 185820112475SJoe Perches } else { 185920112475SJoe Perches my $dequoted = $suggested_email; 186020112475SJoe Perches $dequoted =~ s/^"//; 186120112475SJoe Perches $dequoted =~ s/" </ </; 186220112475SJoe Perches # Don't force email to have quotes 186320112475SJoe Perches # Allow just an angle bracketed address 186420112475SJoe Perches if ("$dequoted$comment" ne $email && 186520112475SJoe Perches "<$email_address>$comment" ne $email && 186620112475SJoe Perches "$suggested_email$comment" ne $email) { 1867000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1868000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 186920112475SJoe Perches } 18700a920b5bSAndy Whitcroft } 18717e51f197SJoe Perches 18727e51f197SJoe Perches# Check for duplicate signatures 18737e51f197SJoe Perches my $sig_nospace = $line; 18747e51f197SJoe Perches $sig_nospace =~ s/\s//g; 18757e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 18767e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 18777e51f197SJoe Perches WARN("BAD_SIGN_OFF", 18787e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 18797e51f197SJoe Perches } else { 18807e51f197SJoe Perches $signatures{$sig_nospace} = 1; 18817e51f197SJoe Perches } 18820a920b5bSAndy Whitcroft } 18830a920b5bSAndy Whitcroft 188400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 18858905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1886000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 1887000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 18886c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1889de7d4f0eSAndy Whitcroft } 1890de7d4f0eSAndy Whitcroft 18916ecd9674SAndy Whitcroft# Check for absolute kernel paths. 18926ecd9674SAndy Whitcroft if ($tree) { 18936ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 18946ecd9674SAndy Whitcroft my $file = $1; 18956ecd9674SAndy Whitcroft 18966ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 18976ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 18986ecd9674SAndy Whitcroft # 18996ecd9674SAndy Whitcroft } else { 19006ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 19016ecd9674SAndy Whitcroft } 19026ecd9674SAndy Whitcroft } 19036ecd9674SAndy Whitcroft } 19046ecd9674SAndy Whitcroft 1905de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1906de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1907171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1908171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1909171ae1a4SAndy Whitcroft 1910171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1911171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1912171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1913171ae1a4SAndy Whitcroft 191434d99219SJoe Perches CHK("INVALID_UTF8", 1915000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 191600df344fSAndy Whitcroft } 19170a920b5bSAndy Whitcroft 191815662b3eSJoe Perches# Check if it's the start of a commit log 191915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 192015662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 1921270c49a0SJoe Perches $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { 192215662b3eSJoe Perches $in_header_lines = 0; 192315662b3eSJoe Perches $in_commit_log = 1; 192415662b3eSJoe Perches } 192515662b3eSJoe Perches 1926fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 1927fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 1928fa64205dSPasi Savanainen if ($in_header_lines && 1929fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 1930fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 1931fa64205dSPasi Savanainen $non_utf8_charset = 1; 1932fa64205dSPasi Savanainen } 1933fa64205dSPasi Savanainen 1934fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 193515662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 1936fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 193715662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 193815662b3eSJoe Perches } 193915662b3eSJoe Perches 194030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 194130670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 194200df344fSAndy Whitcroft 19430a920b5bSAndy Whitcroft#trailing whitespace 19449c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1945c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1946d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 1947d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 1948d5e616fcSJoe Perches $fix) { 1949d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/[\s\015]+$//; 1950d5e616fcSJoe Perches } 1951c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1952c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 19533705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 19543705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 19553705ce5bSJoe Perches $fix) { 1956d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 19573705ce5bSJoe Perches } 19583705ce5bSJoe Perches 1959d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 19600a920b5bSAndy Whitcroft } 19615368df20SAndy Whitcroft 19623354957aSAndi Kleen# check for Kconfig help text having a real description 19639fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 19649fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 19653354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 1966a1385803SAndy Whitcroft $line =~ /.\s*config\s+/) { 19673354957aSAndi Kleen my $length = 0; 19689fe287d7SAndy Whitcroft my $cnt = $realcnt; 19699fe287d7SAndy Whitcroft my $ln = $linenr + 1; 19709fe287d7SAndy Whitcroft my $f; 1971a1385803SAndy Whitcroft my $is_start = 0; 19729fe287d7SAndy Whitcroft my $is_end = 0; 1973a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 19749fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 19759fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 19769fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 19779fe287d7SAndy Whitcroft 19789fe287d7SAndy Whitcroft next if ($f =~ /^-/); 1979a1385803SAndy Whitcroft 1980a1385803SAndy Whitcroft if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { 1981a1385803SAndy Whitcroft $is_start = 1; 1982a1385803SAndy Whitcroft } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { 1983a1385803SAndy Whitcroft $length = -1; 1984a1385803SAndy Whitcroft } 1985a1385803SAndy Whitcroft 19869fe287d7SAndy Whitcroft $f =~ s/^.//; 19873354957aSAndi Kleen $f =~ s/#.*//; 19883354957aSAndi Kleen $f =~ s/^\s+//; 19893354957aSAndi Kleen next if ($f =~ /^$/); 19909fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 19919fe287d7SAndy Whitcroft $is_end = 1; 19929fe287d7SAndy Whitcroft last; 19939fe287d7SAndy Whitcroft } 19943354957aSAndi Kleen $length++; 19953354957aSAndi Kleen } 1996000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 1997a1385803SAndy Whitcroft "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 1998a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 19993354957aSAndi Kleen } 20003354957aSAndi Kleen 20011ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 20021ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 20031ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 20041ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 20051ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 20061ba8dfd1SKees Cook } 20071ba8dfd1SKees Cook 2008c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2009c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2010c68e5878SArnaud Lacombe my $flag = $1; 2011c68e5878SArnaud Lacombe my $replacement = { 2012c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2013c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2014c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2015c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2016c68e5878SArnaud Lacombe }; 2017c68e5878SArnaud Lacombe 2018c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2019c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2020c68e5878SArnaud Lacombe } 2021c68e5878SArnaud Lacombe 20225368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 20235368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 20245368df20SAndy Whitcroft 20256cd7f386SJoe Perches#line length limit 2026c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2027f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 20280fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 20298bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 20306cd7f386SJoe Perches $length > $max_line_length) 2031c45dcabdSAndy Whitcroft { 2032000d1cc1SJoe Perches WARN("LONG_LINE", 20336cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 20340a920b5bSAndy Whitcroft } 20350a920b5bSAndy Whitcroft 2036ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability 2037ca56dc09SJosh Triplett# to grep for the string. Limited to strings used as parameters (those 2038ca56dc09SJosh Triplett# following an open parenthesis), which almost completely eliminates false 2039ca56dc09SJosh Triplett# positives, as well as warning only once per parameter rather than once per 2040ca56dc09SJosh Triplett# line of the string. Make an exception when the previous string ends in a 2041ca56dc09SJosh Triplett# newline (multiple lines in one string constant) or \n\t (common in inline 2042ca56dc09SJosh Triplett# assembly to indent the instruction on the following line). 2043ca56dc09SJosh Triplett if ($line =~ /^\+\s*"/ && 2044ca56dc09SJosh Triplett $prevline =~ /"\s*$/ && 2045ca56dc09SJosh Triplett $prevline =~ /\(/ && 2046ca56dc09SJosh Triplett $prevrawline !~ /\\n(?:\\t)*"\s*$/) { 2047ca56dc09SJosh Triplett WARN("SPLIT_STRING", 2048ca56dc09SJosh Triplett "quoted string split across lines\n" . $hereprev); 2049ca56dc09SJosh Triplett } 2050ca56dc09SJosh Triplett 20515e79d96eSJoe Perches# check for spaces before a quoted newline 20525e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 20533705ce5bSJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 20543705ce5bSJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 20553705ce5bSJoe Perches $fix) { 20563705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 20573705ce5bSJoe Perches } 20583705ce5bSJoe Perches 20595e79d96eSJoe Perches } 20605e79d96eSJoe Perches 20618905a67cSAndy Whitcroft# check for adding lines without a newline. 20628905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2063000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2064000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 20658905a67cSAndy Whitcroft } 20668905a67cSAndy Whitcroft 206742e41c54SMike Frysinger# Blackfin: use hi/lo macros 206842e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 206942e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 207042e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2071000d1cc1SJoe Perches ERROR("LO_MACRO", 2072000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 207342e41c54SMike Frysinger } 207442e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 207542e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2076000d1cc1SJoe Perches ERROR("HI_MACRO", 2077000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 207842e41c54SMike Frysinger } 207942e41c54SMike Frysinger } 208042e41c54SMike Frysinger 2081b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2082b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 20830a920b5bSAndy Whitcroft 20840a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 20850a920b5bSAndy Whitcroft# more than 8 must use tabs. 2086c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2087c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2088c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2089d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 20903705ce5bSJoe Perches if (ERROR("CODE_INDENT", 20913705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 20923705ce5bSJoe Perches $fix) { 20933705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 20943705ce5bSJoe Perches } 20950a920b5bSAndy Whitcroft } 20960a920b5bSAndy Whitcroft 209708e44365SAlberto Panizzo# check for space before tabs. 209808e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 209908e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 21003705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 21013705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 21023705ce5bSJoe Perches $fix) { 21033705ce5bSJoe Perches $fixed[$linenr - 1] =~ 21043705ce5bSJoe Perches s/(^\+.*) +\t/$1\t/; 21053705ce5bSJoe Perches } 210608e44365SAlberto Panizzo } 210708e44365SAlberto Panizzo 2108d1fe9c09SJoe Perches# check for && or || at the start of a line 2109d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2110d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2111d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2112d1fe9c09SJoe Perches } 2113d1fe9c09SJoe Perches 2114d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2115d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 2116d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { 2117d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2118d1fe9c09SJoe Perches my $oldindent = $1; 2119d1fe9c09SJoe Perches my $rest = $2; 2120d1fe9c09SJoe Perches 2121d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2122d1fe9c09SJoe Perches if ($pos >= 0) { 2123b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2124b34a26f3SJoe Perches my $newindent = $2; 2125d1fe9c09SJoe Perches 2126d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2127d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2128d1fe9c09SJoe Perches " " x ($pos % 8); 2129d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2130d1fe9c09SJoe Perches 2131d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2132d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 21333705ce5bSJoe Perches 21343705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 21353705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 21363705ce5bSJoe Perches $fix && $line =~ /^\+/) { 21373705ce5bSJoe Perches $fixed[$linenr - 1] =~ 21383705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 21393705ce5bSJoe Perches } 2140d1fe9c09SJoe Perches } 2141d1fe9c09SJoe Perches } 2142d1fe9c09SJoe Perches } 2143d1fe9c09SJoe Perches 214423f780c9SJoe Perches if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { 21453705ce5bSJoe Perches if (CHK("SPACING", 21463705ce5bSJoe Perches "No space is necessary after a cast\n" . $hereprev) && 21473705ce5bSJoe Perches $fix) { 21483705ce5bSJoe Perches $fixed[$linenr - 1] =~ 21493705ce5bSJoe Perches s/^(\+.*\*[ \t]*\))[ \t]+/$1/; 21503705ce5bSJoe Perches } 2151aad4f614SJoe Perches } 2152aad4f614SJoe Perches 215305880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2154fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 2155fdb4bcd6SJoe Perches $rawline =~ /^\+[ \t]*\*/) { 215605880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 215705880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 215805880600SJoe Perches } 215905880600SJoe Perches 216005880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2161a605e32eSJoe Perches $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* 2162a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 216361135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2164a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 2165a605e32eSJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2166a605e32eSJoe Perches "networking block comments start with * on subsequent lines\n" . $hereprev); 2167a605e32eSJoe Perches } 2168a605e32eSJoe Perches 2169a605e32eSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2170c24f9f19SJoe Perches $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2171c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2172c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2173c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 217405880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 217505880600SJoe Perches "networking block comments put the trailing */ on a separate line\n" . $herecurr); 217605880600SJoe Perches } 217705880600SJoe Perches 21785f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 21796b4c5bebSAndy Whitcroft# Exceptions: 21806b4c5bebSAndy Whitcroft# 1) within comments 21816b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 21826b4c5bebSAndy Whitcroft# 3) hanging labels 21833705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 21845f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 21853705ce5bSJoe Perches if (WARN("LEADING_SPACE", 21863705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 21873705ce5bSJoe Perches $fix) { 21883705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 21893705ce5bSJoe Perches } 21905f7ddae6SRaffaele Recalcati } 21915f7ddae6SRaffaele Recalcati 2192b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 2193b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 2194b9ea10d6SAndy Whitcroft 21951ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 21961ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 21971ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 21981ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 21991ba8dfd1SKees Cook } 22001ba8dfd1SKees Cook 2201c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 2202cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 2203000d1cc1SJoe Perches WARN("CVS_KEYWORD", 2204000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 2205c2fdda0dSAndy Whitcroft } 220622f2a2efSAndy Whitcroft 220742e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 220842e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 220942e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2210000d1cc1SJoe Perches ERROR("CSYNC", 2211000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 221242e41c54SMike Frysinger } 221342e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 221442e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2215000d1cc1SJoe Perches ERROR("SSYNC", 2216000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 221742e41c54SMike Frysinger } 221842e41c54SMike Frysinger 221956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 222056e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 222156e77d70SJoe Perches WARN("HOTPLUG_SECTION", 222256e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 222356e77d70SJoe Perches } 222456e77d70SJoe Perches 22259c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 22262b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 22272b474a1aSAndy Whitcroft $realline_next); 22283e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 22293e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 22301b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 2231170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2232f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2233171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 2234171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 2235171ae1a4SAndy Whitcroft 22363e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 22373e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 22383e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 22393e469cdcSAndy Whitcroft # until we hit end of it. 22403e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 22413e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 22423e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 22433e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 22443e469cdcSAndy Whitcroft } 2245f74bd194SAndy Whitcroft 22462b474a1aSAndy Whitcroft # Find the real next line. 22472b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 22482b474a1aSAndy Whitcroft if (defined $realline_next && 22492b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 22502b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 22512b474a1aSAndy Whitcroft $realline_next++; 22522b474a1aSAndy Whitcroft } 22532b474a1aSAndy Whitcroft 2254171ae1a4SAndy Whitcroft my $s = $stat; 2255171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 2256cf655043SAndy Whitcroft 2257c2fdda0dSAndy Whitcroft # Ignore goto labels. 2258171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 2259c2fdda0dSAndy Whitcroft 2260c2fdda0dSAndy Whitcroft # Ignore functions being called 2261171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 2262c2fdda0dSAndy Whitcroft 2263463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 2264463f2864SAndy Whitcroft 2265c45dcabdSAndy Whitcroft # declarations always start with types 2266d2506586SAndy 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) { 2267c45dcabdSAndy Whitcroft my $type = $1; 2268c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 2269c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 2270c45dcabdSAndy Whitcroft 22716c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 2272a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 2273c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 2274c2fdda0dSAndy Whitcroft } 22758905a67cSAndy Whitcroft 22766c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 227765863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 2278c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 22799c0ca6f9SAndy Whitcroft } 22808905a67cSAndy Whitcroft 22818905a67cSAndy Whitcroft # Check for any sort of function declaration. 22828905a67cSAndy Whitcroft # int foo(something bar, other baz); 22838905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 2284171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 22858905a67cSAndy Whitcroft my ($name_len) = length($1); 22868905a67cSAndy Whitcroft 2287cf655043SAndy Whitcroft my $ctx = $s; 2288773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 22898905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 2290cf655043SAndy Whitcroft 22918905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 2292c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 22938905a67cSAndy Whitcroft 2294c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 22958905a67cSAndy Whitcroft } 22968905a67cSAndy Whitcroft } 22978905a67cSAndy Whitcroft } 22988905a67cSAndy Whitcroft 22999c0ca6f9SAndy Whitcroft } 23009c0ca6f9SAndy Whitcroft 230100df344fSAndy Whitcroft# 230200df344fSAndy Whitcroft# Checks which may be anchored in the context. 230300df344fSAndy Whitcroft# 230400df344fSAndy Whitcroft 230500df344fSAndy Whitcroft# Check for switch () and associated case and default 230600df344fSAndy Whitcroft# statements should be at the same indent. 230700df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 230800df344fSAndy Whitcroft my $err = ''; 230900df344fSAndy Whitcroft my $sep = ''; 231000df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 231100df344fSAndy Whitcroft shift(@ctx); 231200df344fSAndy Whitcroft for my $ctx (@ctx) { 231300df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 231400df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 231500df344fSAndy Whitcroft $indent != $cindent) { 231600df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 231700df344fSAndy Whitcroft $sep = ''; 231800df344fSAndy Whitcroft } else { 231900df344fSAndy Whitcroft $sep = "[...]\n"; 232000df344fSAndy Whitcroft } 232100df344fSAndy Whitcroft } 232200df344fSAndy Whitcroft if ($err ne '') { 2323000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2324000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2325de7d4f0eSAndy Whitcroft } 2326de7d4f0eSAndy Whitcroft } 2327de7d4f0eSAndy Whitcroft 2328de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2329de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 2330c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2331773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2332773647a0SAndy Whitcroft 23339c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 23348eef05ddSJoe Perches 23358eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 23368eef05ddSJoe Perches WARN("DEEP_INDENTATION", 23378eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 23388eef05ddSJoe Perches } 23398eef05ddSJoe Perches 2340de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2341de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 2342de7d4f0eSAndy Whitcroft 2343548596d5SAndy Whitcroft my $ctx_ln = $linenr; 2344548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 2345de7d4f0eSAndy Whitcroft 2346548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2347548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 2348548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 2349548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2350548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2351773647a0SAndy Whitcroft $ctx_ln++; 2352773647a0SAndy Whitcroft } 2353548596d5SAndy Whitcroft 235453210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 235553210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2356773647a0SAndy Whitcroft 2357773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 2358000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2359000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 236001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 236100df344fSAndy Whitcroft } 2362773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2363773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 2364773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 2365773647a0SAndy Whitcroft { 23669c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 23679c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 2368000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 2369000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 237001464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 23719c0ca6f9SAndy Whitcroft } 23729c0ca6f9SAndy Whitcroft } 237300df344fSAndy Whitcroft } 237400df344fSAndy Whitcroft 23754d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 23764d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 23773e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 23783e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 23793e469cdcSAndy Whitcroft if (!defined $stat); 23804d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 23814d001e4dSAndy Whitcroft 23824d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 23834d001e4dSAndy Whitcroft 23844d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 23854d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 23864d001e4dSAndy Whitcroft # where necessary. 23874d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 23884d001e4dSAndy Whitcroft 23894d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 23906f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 23916f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 23924d001e4dSAndy Whitcroft 23934d001e4dSAndy Whitcroft # We want to check the first line inside the block 23944d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 23954d001e4dSAndy Whitcroft # 1) any blank line termination 23964d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 23974d001e4dSAndy Whitcroft # 3) any do (...) { 23984d001e4dSAndy Whitcroft my $continuation = 0; 23994d001e4dSAndy Whitcroft my $check = 0; 24004d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 24014d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 24024d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 24034d001e4dSAndy Whitcroft $continuation = 1; 24044d001e4dSAndy Whitcroft } 24059bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 24064d001e4dSAndy Whitcroft $check = 1; 24074d001e4dSAndy Whitcroft $cond_lines++; 24084d001e4dSAndy Whitcroft } 24094d001e4dSAndy Whitcroft 24104d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 24114d001e4dSAndy Whitcroft # preprocessor statement. 24124d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 24134d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 24144d001e4dSAndy Whitcroft $check = 0; 24154d001e4dSAndy Whitcroft } 24164d001e4dSAndy Whitcroft 24179bd49efeSAndy Whitcroft my $cond_ptr = -1; 2418740504c6SAndy Whitcroft $continuation = 0; 24199bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 24209bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 24214d001e4dSAndy Whitcroft 2422f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 2423f16fa28fSAndy Whitcroft # is not linear. 2424f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2425f16fa28fSAndy Whitcroft $check = 0; 2426f16fa28fSAndy Whitcroft } 2427f16fa28fSAndy Whitcroft 24289bd49efeSAndy Whitcroft # Ignore: 24299bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 24309bd49efeSAndy Whitcroft # 2) preprocessor lines, and 24319bd49efeSAndy Whitcroft # 3) labels. 2432740504c6SAndy Whitcroft if ($continuation || 2433740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 24349bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 24359bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 2436740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 243730dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 24389bd49efeSAndy Whitcroft $cond_lines++; 24399bd49efeSAndy Whitcroft } 24404d001e4dSAndy Whitcroft } 244130dad6ebSAndy Whitcroft } 24424d001e4dSAndy Whitcroft 24434d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 24444d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 24454d001e4dSAndy Whitcroft 24464d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 24474d001e4dSAndy Whitcroft # this is not this patch's fault. 24484d001e4dSAndy Whitcroft if (!defined($stat_real) || 24494d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 24504d001e4dSAndy Whitcroft $check = 0; 24514d001e4dSAndy Whitcroft } 24524d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 24534d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 24544d001e4dSAndy Whitcroft } 24554d001e4dSAndy Whitcroft 24569bd49efeSAndy 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"; 24574d001e4dSAndy Whitcroft 24584d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 24594d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 2460000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 2461000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 24624d001e4dSAndy Whitcroft } 24634d001e4dSAndy Whitcroft } 24644d001e4dSAndy Whitcroft 24656c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 24666c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 24671f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 24681f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 24696c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 2470c2fdda0dSAndy Whitcroft if ($dbg_values) { 2471c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 2472cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 2473cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 24741f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 2475c2fdda0dSAndy Whitcroft } 24766c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 24776c72ffaaSAndy Whitcroft 247800df344fSAndy Whitcroft#ignore lines not being added 24793705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 248000df344fSAndy Whitcroft 2481653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 24827429c690SAndy Whitcroft if ($dbg_type) { 24837429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 2484000d1cc1SJoe Perches ERROR("TEST_TYPE", 2485000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 24867429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2487000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 2488000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 24897429c690SAndy Whitcroft } 2490653d4876SAndy Whitcroft next; 2491653d4876SAndy Whitcroft } 2492a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2493a1ef277eSAndy Whitcroft if ($dbg_attr) { 24949360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2495000d1cc1SJoe Perches ERROR("TEST_ATTR", 2496000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 24979360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2498000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2499000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2500a1ef277eSAndy Whitcroft } 2501a1ef277eSAndy Whitcroft next; 2502a1ef277eSAndy Whitcroft } 2503653d4876SAndy Whitcroft 2504f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 250599423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 250699423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2507000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2508000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . $hereprev); 2509f0a594c1SAndy Whitcroft } 2510f0a594c1SAndy Whitcroft 251100df344fSAndy Whitcroft# 251200df344fSAndy Whitcroft# Checks which are anchored on the added line. 251300df344fSAndy Whitcroft# 251400df344fSAndy Whitcroft 2515653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2516c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2517653d4876SAndy Whitcroft my $path = $1; 2518653d4876SAndy Whitcroft if ($path =~ m{//}) { 2519000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2520495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 2521495e9d84SJoe Perches } 2522495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 2523495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 2524495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 2525653d4876SAndy Whitcroft } 2526653d4876SAndy Whitcroft } 2527653d4876SAndy Whitcroft 252800df344fSAndy Whitcroft# no C99 // comments 252900df344fSAndy Whitcroft if ($line =~ m{//}) { 25303705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 25313705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 25323705ce5bSJoe Perches $fix) { 25333705ce5bSJoe Perches my $line = $fixed[$linenr - 1]; 25343705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 25353705ce5bSJoe Perches my $comment = trim($1); 25363705ce5bSJoe Perches $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; 25373705ce5bSJoe Perches } 25383705ce5bSJoe Perches } 253900df344fSAndy Whitcroft } 254000df344fSAndy Whitcroft # Remove C99 comments. 25410a920b5bSAndy Whitcroft $line =~ s@//.*@@; 25426c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 25430a920b5bSAndy Whitcroft 25442b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 25452b474a1aSAndy Whitcroft# the whole statement. 25462b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 25472b474a1aSAndy Whitcroft if (defined $realline_next && 25482b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 25492b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 25502b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 25512b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 25523cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 25533cbf62dfSAndy Whitcroft # a prefix: 25543cbf62dfSAndy Whitcroft # XXX(foo); 25553cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 2556653d4876SAndy Whitcroft my $name = $1; 255787a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 25583cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 25593cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 25603cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 25613cbf62dfSAndy Whitcroft 25623cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 25632b474a1aSAndy Whitcroft \n.}\s*$| 256448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 256548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 256648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 25672b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 25682b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 256948012058SAndy Whitcroft )/x) { 25702b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 25712b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 25722b474a1aSAndy Whitcroft } else { 25732b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 25740a920b5bSAndy Whitcroft } 25750a920b5bSAndy Whitcroft } 25762b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 25772b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 25782b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 25792b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 25802b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 25812b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 25822b474a1aSAndy Whitcroft } 25832b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 25842b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 2585000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 2586000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 25872b474a1aSAndy Whitcroft } 25880a920b5bSAndy Whitcroft 25895150bda4SJoe Eloff# check for global initialisers. 2590d5e616fcSJoe Perches if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { 2591d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 2592000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 2593d5e616fcSJoe Perches $herecurr) && 2594d5e616fcSJoe Perches $fix) { 2595d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; 2596d5e616fcSJoe Perches } 2597f0a594c1SAndy Whitcroft } 25980a920b5bSAndy Whitcroft# check for static initialisers. 2599d5e616fcSJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2600d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 2601000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 2602d5e616fcSJoe Perches $herecurr) && 2603d5e616fcSJoe Perches $fix) { 2604d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; 2605d5e616fcSJoe Perches } 26060a920b5bSAndy Whitcroft } 26070a920b5bSAndy Whitcroft 2608cb710ecaSJoe Perches# check for static const char * arrays. 2609cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 2610000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2611000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 2612cb710ecaSJoe Perches $herecurr); 2613cb710ecaSJoe Perches } 2614cb710ecaSJoe Perches 2615cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 2616cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 2617000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2618000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 2619cb710ecaSJoe Perches $herecurr); 2620cb710ecaSJoe Perches } 2621cb710ecaSJoe Perches 262293ed0e2dSJoe Perches# check for declarations of struct pci_device_id 262393ed0e2dSJoe Perches if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { 2624000d1cc1SJoe Perches WARN("DEFINE_PCI_DEVICE_TABLE", 2625000d1cc1SJoe Perches "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); 262693ed0e2dSJoe Perches } 262793ed0e2dSJoe Perches 2628653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 2629653d4876SAndy Whitcroft# make sense. 2630653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 26318054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 2632c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 26338ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 2634653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 2635000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 2636000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 26370a920b5bSAndy Whitcroft } 26380a920b5bSAndy Whitcroft 26390a920b5bSAndy Whitcroft# * goes on variable not on type 264065863862SAndy Whitcroft # (char*[ const]) 2641bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2642bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 26433705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 2644d8aaf121SAndy Whitcroft 264565863862SAndy Whitcroft # Should start with a space. 264665863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 264765863862SAndy Whitcroft # Should not end with a space. 264865863862SAndy Whitcroft $to =~ s/\s+$//; 264965863862SAndy Whitcroft # '*'s should not have spaces between. 2650f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 265165863862SAndy Whitcroft } 2652d8aaf121SAndy Whitcroft 26533705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 265465863862SAndy Whitcroft if ($from ne $to) { 26553705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 26563705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 26573705ce5bSJoe Perches $fix) { 26583705ce5bSJoe Perches my $sub_from = $ident; 26593705ce5bSJoe Perches my $sub_to = $ident; 26603705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 26613705ce5bSJoe Perches $fixed[$linenr - 1] =~ 26623705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 26633705ce5bSJoe Perches } 266465863862SAndy Whitcroft } 2665bfcb2cc7SAndy Whitcroft } 2666bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2667bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 26683705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 2669d8aaf121SAndy Whitcroft 267065863862SAndy Whitcroft # Should start with a space. 267165863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 267265863862SAndy Whitcroft # Should not end with a space. 267365863862SAndy Whitcroft $to =~ s/\s+$//; 267465863862SAndy Whitcroft # '*'s should not have spaces between. 2675f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 267665863862SAndy Whitcroft } 267765863862SAndy Whitcroft # Modifiers should have spaces. 267865863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 267965863862SAndy Whitcroft 26803705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 2681667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 26823705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 26833705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 26843705ce5bSJoe Perches $fix) { 26853705ce5bSJoe Perches 26863705ce5bSJoe Perches my $sub_from = $match; 26873705ce5bSJoe Perches my $sub_to = $match; 26883705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 26893705ce5bSJoe Perches $fixed[$linenr - 1] =~ 26903705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 26913705ce5bSJoe Perches } 269265863862SAndy Whitcroft } 26930a920b5bSAndy Whitcroft } 26940a920b5bSAndy Whitcroft 26950a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 26960a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 26970a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 26980a920b5bSAndy Whitcroft# print "$herecurr"; 26990a920b5bSAndy Whitcroft# $clean = 0; 27000a920b5bSAndy Whitcroft# } 27010a920b5bSAndy Whitcroft 27028905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 2703000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 2704000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 27058905a67cSAndy Whitcroft } 27068905a67cSAndy Whitcroft 270717441227SJoe Perches# check for uses of printk_ratelimit 270817441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 2709000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 2710000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 271117441227SJoe Perches } 271217441227SJoe Perches 271300df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 271400df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 271500df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 271625985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 271700df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 2718f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 271900df344fSAndy Whitcroft my $ok = 0; 272000df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 272100df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 272225985edcSLucas De Marchi # we have a preceding printk if it ends 272300df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 272400df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 272500df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 272600df344fSAndy Whitcroft $ok = 1; 272700df344fSAndy Whitcroft } 272800df344fSAndy Whitcroft last; 272900df344fSAndy Whitcroft } 273000df344fSAndy Whitcroft } 273100df344fSAndy Whitcroft if ($ok == 0) { 2732000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 2733000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 27340a920b5bSAndy Whitcroft } 273500df344fSAndy Whitcroft } 27360a920b5bSAndy Whitcroft 2737243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 2738243f3803SJoe Perches my $orig = $1; 2739243f3803SJoe Perches my $level = lc($orig); 2740243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 27418f26b837SJoe Perches my $level2 = $level; 27428f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 2743243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 27448f26b837SJoe Perches "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 2745243f3803SJoe Perches } 2746243f3803SJoe Perches 2747243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 2748d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 2749d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 2750d5e616fcSJoe Perches $fix) { 2751d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 2752d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 2753d5e616fcSJoe Perches } 2754243f3803SJoe Perches } 2755243f3803SJoe Perches 2756dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 2757dc139313SJoe Perches my $orig = $1; 2758dc139313SJoe Perches my $level = lc($orig); 2759dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 2760dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 2761dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 2762dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 2763dc139313SJoe Perches } 2764dc139313SJoe Perches 2765653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 2766653d4876SAndy Whitcroft# or if closed on same line 2767c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2768c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 2769000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2770000d1cc1SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr); 27710a920b5bSAndy Whitcroft } 2772653d4876SAndy Whitcroft 27738905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 27748905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 27758905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2776000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2777000d1cc1SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev); 27788905a67cSAndy Whitcroft } 27798905a67cSAndy Whitcroft 27800c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 27813705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 27823705ce5bSJoe Perches if (WARN("SPACING", 27833705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 27843705ce5bSJoe Perches $fix) { 27853705ce5bSJoe Perches $fixed[$linenr - 1] =~ 27863705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 27873705ce5bSJoe Perches } 27880c73b4ebSAndy Whitcroft } 27890c73b4ebSAndy Whitcroft 27908d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 27918d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 2792fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2793fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 27948d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 27958d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 27968d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 2797fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 2798daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 27993705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 28003705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 28013705ce5bSJoe Perches $fix) { 28023705ce5bSJoe Perches $fixed[$linenr - 1] =~ 28033705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 28043705ce5bSJoe Perches } 28058d31cfceSAndy Whitcroft } 28068d31cfceSAndy Whitcroft } 28078d31cfceSAndy Whitcroft 2808f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 28096c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 2810c2fdda0dSAndy Whitcroft my $name = $1; 2811773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 2812773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 2813c2fdda0dSAndy Whitcroft 2814c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 2815773647a0SAndy Whitcroft if ($name =~ /^(?: 2816773647a0SAndy Whitcroft if|for|while|switch|return|case| 2817773647a0SAndy Whitcroft volatile|__volatile__| 2818773647a0SAndy Whitcroft __attribute__|format|__extension__| 2819773647a0SAndy Whitcroft asm|__asm__)$/x) 2820773647a0SAndy Whitcroft { 2821c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 2822c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 2823c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2824c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2825773647a0SAndy Whitcroft 2826773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2827c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2828c2fdda0dSAndy Whitcroft 2829c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2830c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2831773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2832c2fdda0dSAndy Whitcroft 2833c2fdda0dSAndy Whitcroft } else { 28343705ce5bSJoe Perches if (WARN("SPACING", 28353705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 28363705ce5bSJoe Perches $fix) { 28373705ce5bSJoe Perches $fixed[$linenr - 1] =~ 28383705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 28393705ce5bSJoe Perches } 2840f0a594c1SAndy Whitcroft } 28416c72ffaaSAndy Whitcroft } 28429a4cad4eSEric Nelson 2843653d4876SAndy Whitcroft# Check operator spacing. 28440a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 28453705ce5bSJoe Perches my $fixed_line = ""; 28463705ce5bSJoe Perches my $line_fixed = 0; 28473705ce5bSJoe Perches 28489c0ca6f9SAndy Whitcroft my $ops = qr{ 28499c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 28509c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 28519c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 28521f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 285384731623SJoe Perches \?:|\?|: 28549c0ca6f9SAndy Whitcroft }x; 2855cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 28563705ce5bSJoe Perches 28573705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 28583705ce5bSJoe Perches## foreach my $el (@elements) { 28593705ce5bSJoe Perches## print("el: <$el>\n"); 28603705ce5bSJoe Perches## } 28613705ce5bSJoe Perches 28623705ce5bSJoe Perches my @fix_elements = (); 286300df344fSAndy Whitcroft my $off = 0; 28646c72ffaaSAndy Whitcroft 28653705ce5bSJoe Perches foreach my $el (@elements) { 28663705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 28673705ce5bSJoe Perches $off += length($el); 28683705ce5bSJoe Perches } 28693705ce5bSJoe Perches 28703705ce5bSJoe Perches $off = 0; 28713705ce5bSJoe Perches 28726c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 2873b34c648bSJoe Perches my $last_after = -1; 28746c72ffaaSAndy Whitcroft 28750a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 28763705ce5bSJoe Perches 28773705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 28783705ce5bSJoe Perches 28793705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 28803705ce5bSJoe Perches 28814a0df2efSAndy Whitcroft $off += length($elements[$n]); 28824a0df2efSAndy Whitcroft 288325985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 2884773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 2885773647a0SAndy Whitcroft my $cc = ''; 2886773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 2887773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 2888773647a0SAndy Whitcroft } 2889773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 2890773647a0SAndy Whitcroft 28914a0df2efSAndy Whitcroft my $a = ''; 28924a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 28934a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 2894cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 28954a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 28964a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 2897773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 28984a0df2efSAndy Whitcroft 28990a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 29004a0df2efSAndy Whitcroft 29014a0df2efSAndy Whitcroft my $c = ''; 29020a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 29034a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 29044a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2905cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 29064a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 29074a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 29088b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 29094a0df2efSAndy Whitcroft } else { 29104a0df2efSAndy Whitcroft $c = 'E'; 29110a920b5bSAndy Whitcroft } 29120a920b5bSAndy Whitcroft 29134a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 29144a0df2efSAndy Whitcroft 29154a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 29164a0df2efSAndy Whitcroft 29176c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 2918de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 29190a920b5bSAndy Whitcroft 292074048ed8SAndy Whitcroft # Pull out the value of this operator. 29216c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 29220a920b5bSAndy Whitcroft 29231f65f947SAndy Whitcroft # Get the full operator variant. 29241f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 29251f65f947SAndy Whitcroft 292613214adfSAndy Whitcroft # Ignore operators passed as parameters. 292713214adfSAndy Whitcroft if ($op_type ne 'V' && 292813214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 292913214adfSAndy Whitcroft 2930cf655043SAndy Whitcroft# # Ignore comments 2931cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 293213214adfSAndy Whitcroft 2933d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 293413214adfSAndy Whitcroft } elsif ($op eq ';') { 2935cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 2936cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 29373705ce5bSJoe Perches if (ERROR("SPACING", 29383705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 2939b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 29403705ce5bSJoe Perches $line_fixed = 1; 29413705ce5bSJoe Perches } 2942d8aaf121SAndy Whitcroft } 2943d8aaf121SAndy Whitcroft 2944d8aaf121SAndy Whitcroft # // is a comment 2945d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 29460a920b5bSAndy Whitcroft 29471f65f947SAndy Whitcroft # No spaces for: 29481f65f947SAndy Whitcroft # -> 29491f65f947SAndy Whitcroft # : when part of a bitfield 29501f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 29514a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 29523705ce5bSJoe Perches if (ERROR("SPACING", 29533705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 2954b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 29553705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 29563705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 29573705ce5bSJoe Perches } 2958b34c648bSJoe Perches $line_fixed = 1; 29593705ce5bSJoe Perches } 29600a920b5bSAndy Whitcroft } 29610a920b5bSAndy Whitcroft 29620a920b5bSAndy Whitcroft # , must have a space on the right. 29630a920b5bSAndy Whitcroft } elsif ($op eq ',') { 2964cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 29653705ce5bSJoe Perches if (ERROR("SPACING", 29663705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 2967b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 29683705ce5bSJoe Perches $line_fixed = 1; 2969b34c648bSJoe Perches $last_after = $n; 29703705ce5bSJoe Perches } 29710a920b5bSAndy Whitcroft } 29720a920b5bSAndy Whitcroft 29739c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 297474048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 29759c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 29769c0ca6f9SAndy Whitcroft 29779c0ca6f9SAndy Whitcroft # unary operators should have a space before and 29789c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 29799c0ca6f9SAndy Whitcroft # unary operator, or a cast 29809c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 298174048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 29820d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 2983cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 29843705ce5bSJoe Perches if (ERROR("SPACING", 29853705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 2986b34c648bSJoe Perches if ($n != $last_after + 2) { 2987b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 29883705ce5bSJoe Perches $line_fixed = 1; 29893705ce5bSJoe Perches } 29900a920b5bSAndy Whitcroft } 2991b34c648bSJoe Perches } 2992a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2993171ae1a4SAndy Whitcroft # A unary '*' may be const 2994171ae1a4SAndy Whitcroft 2995171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 29963705ce5bSJoe Perches if (ERROR("SPACING", 29973705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 2998b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 29993705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 30003705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 30013705ce5bSJoe Perches } 3002b34c648bSJoe Perches $line_fixed = 1; 30033705ce5bSJoe Perches } 30040a920b5bSAndy Whitcroft } 30050a920b5bSAndy Whitcroft 30060a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 30070a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 3008773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 30093705ce5bSJoe Perches if (ERROR("SPACING", 30103705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 3011b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 30123705ce5bSJoe Perches $line_fixed = 1; 30133705ce5bSJoe Perches } 30140a920b5bSAndy Whitcroft } 3015773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 3016773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 30173705ce5bSJoe Perches if (ERROR("SPACING", 30183705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3019b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 30203705ce5bSJoe Perches $line_fixed = 1; 30213705ce5bSJoe Perches } 3022653d4876SAndy Whitcroft } 3023773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 30243705ce5bSJoe Perches if (ERROR("SPACING", 30253705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3026b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 30273705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 30283705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3029773647a0SAndy Whitcroft } 3030b34c648bSJoe Perches $line_fixed = 1; 30313705ce5bSJoe Perches } 30323705ce5bSJoe Perches } 30330a920b5bSAndy Whitcroft 30340a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 30359c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 30369c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 30379c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 3038c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 3039c2fdda0dSAndy Whitcroft $op eq '%') 30400a920b5bSAndy Whitcroft { 3041773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 30423705ce5bSJoe Perches if (ERROR("SPACING", 30433705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 3044b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3045b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3046b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3047b34c648bSJoe Perches } 30483705ce5bSJoe Perches $line_fixed = 1; 30493705ce5bSJoe Perches } 30500a920b5bSAndy Whitcroft } 30510a920b5bSAndy Whitcroft 30521f65f947SAndy Whitcroft # A colon needs no spaces before when it is 30531f65f947SAndy Whitcroft # terminating a case value or a label. 30541f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 30551f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 30563705ce5bSJoe Perches if (ERROR("SPACING", 30573705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3058b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 30593705ce5bSJoe Perches $line_fixed = 1; 30603705ce5bSJoe Perches } 30611f65f947SAndy Whitcroft } 30621f65f947SAndy Whitcroft 30630a920b5bSAndy Whitcroft # All the others need spaces both sides. 3064cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 30651f65f947SAndy Whitcroft my $ok = 0; 30661f65f947SAndy Whitcroft 306722f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 30681f65f947SAndy Whitcroft if (($op eq '<' && 30691f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 30701f65f947SAndy Whitcroft ($op eq '>' && 30711f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 30721f65f947SAndy Whitcroft { 30731f65f947SAndy Whitcroft $ok = 1; 30741f65f947SAndy Whitcroft } 30751f65f947SAndy Whitcroft 307684731623SJoe Perches # messages are ERROR, but ?: are CHK 30771f65f947SAndy Whitcroft if ($ok == 0) { 307884731623SJoe Perches my $msg_type = \&ERROR; 307984731623SJoe Perches $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 308084731623SJoe Perches 308184731623SJoe Perches if (&{$msg_type}("SPACING", 30823705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 3083b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3084b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3085b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3086b34c648bSJoe Perches } 30873705ce5bSJoe Perches $line_fixed = 1; 30883705ce5bSJoe Perches } 30890a920b5bSAndy Whitcroft } 309022f2a2efSAndy Whitcroft } 30914a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 30923705ce5bSJoe Perches 30933705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 30943705ce5bSJoe Perches 30953705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 30960a920b5bSAndy Whitcroft } 30973705ce5bSJoe Perches 30983705ce5bSJoe Perches if (($#elements % 2) == 0) { 30993705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 31003705ce5bSJoe Perches } 31013705ce5bSJoe Perches 31023705ce5bSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { 31033705ce5bSJoe Perches $fixed[$linenr - 1] = $fixed_line; 31043705ce5bSJoe Perches } 31053705ce5bSJoe Perches 31063705ce5bSJoe Perches 31070a920b5bSAndy Whitcroft } 31080a920b5bSAndy Whitcroft 3109786b6326SJoe Perches# check for whitespace before a non-naked semicolon 3110786b6326SJoe Perches if ($line =~ /^\+.*\S\s+;/) { 3111786b6326SJoe Perches if (WARN("SPACING", 3112786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 3113786b6326SJoe Perches $fix) { 3114786b6326SJoe Perches 1 while $fixed[$linenr - 1] =~ 3115786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 3116786b6326SJoe Perches } 3117786b6326SJoe Perches } 3118786b6326SJoe Perches 3119f0a594c1SAndy Whitcroft# check for multiple assignments 3120f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 3121000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 3122000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 3123f0a594c1SAndy Whitcroft } 3124f0a594c1SAndy Whitcroft 312522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 312622f2a2efSAndy Whitcroft## # continuation. 312722f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 312822f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 312922f2a2efSAndy Whitcroft## 313022f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 313122f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 313222f2a2efSAndy Whitcroft## my $ln = $line; 313322f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 313422f2a2efSAndy Whitcroft## } 313522f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 3136000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 3137000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 313822f2a2efSAndy Whitcroft## } 313922f2a2efSAndy Whitcroft## } 3140f0a594c1SAndy Whitcroft 31410a920b5bSAndy Whitcroft#need space before brace following if, while, etc 314222f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 314322f2a2efSAndy Whitcroft $line =~ /do{/) { 31443705ce5bSJoe Perches if (ERROR("SPACING", 31453705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 31463705ce5bSJoe Perches $fix) { 3147d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/; 31483705ce5bSJoe Perches } 3149de7d4f0eSAndy Whitcroft } 3150de7d4f0eSAndy Whitcroft 3151c4a62ef9SJoe Perches## # check for blank lines before declarations 3152c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 3153c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 3154c4a62ef9SJoe Perches## WARN("SPACING", 3155c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 3156c4a62ef9SJoe Perches## } 3157c4a62ef9SJoe Perches## 3158c4a62ef9SJoe Perches 3159de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 3160de7d4f0eSAndy Whitcroft# on the line 3161de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 3162d5e616fcSJoe Perches if (ERROR("SPACING", 3163d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 3164d5e616fcSJoe Perches $fix) { 3165d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3166d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 3167d5e616fcSJoe Perches } 31680a920b5bSAndy Whitcroft } 31690a920b5bSAndy Whitcroft 317022f2a2efSAndy Whitcroft# check spacing on square brackets 317122f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 31723705ce5bSJoe Perches if (ERROR("SPACING", 31733705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 31743705ce5bSJoe Perches $fix) { 31753705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31763705ce5bSJoe Perches s/\[\s+/\[/; 31773705ce5bSJoe Perches } 317822f2a2efSAndy Whitcroft } 317922f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 31803705ce5bSJoe Perches if (ERROR("SPACING", 31813705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 31823705ce5bSJoe Perches $fix) { 31833705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31843705ce5bSJoe Perches s/\s+\]/\]/; 31853705ce5bSJoe Perches } 318622f2a2efSAndy Whitcroft } 318722f2a2efSAndy Whitcroft 3188c45dcabdSAndy Whitcroft# check spacing on parentheses 31899c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 31909c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 31913705ce5bSJoe Perches if (ERROR("SPACING", 31923705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 31933705ce5bSJoe Perches $fix) { 31943705ce5bSJoe Perches $fixed[$linenr - 1] =~ 31953705ce5bSJoe Perches s/\(\s+/\(/; 31963705ce5bSJoe Perches } 319722f2a2efSAndy Whitcroft } 319813214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 3199c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 3200c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 32013705ce5bSJoe Perches if (ERROR("SPACING", 32023705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 32033705ce5bSJoe Perches $fix) { 32043705ce5bSJoe Perches $fixed[$linenr - 1] =~ 32053705ce5bSJoe Perches s/\s+\)/\)/; 32063705ce5bSJoe Perches } 320722f2a2efSAndy Whitcroft } 320822f2a2efSAndy Whitcroft 32090a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 32104a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 32110a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 32123705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 32133705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 32143705ce5bSJoe Perches $fix) { 32153705ce5bSJoe Perches $fixed[$linenr - 1] =~ 32163705ce5bSJoe Perches s/^(.)\s+/$1/; 32173705ce5bSJoe Perches } 32180a920b5bSAndy Whitcroft } 32190a920b5bSAndy Whitcroft 3220c45dcabdSAndy Whitcroft# Return is not a function. 3221c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 3222c45dcabdSAndy Whitcroft my $spacing = $1; 3223c45dcabdSAndy Whitcroft my $value = $2; 3224c45dcabdSAndy Whitcroft 322586f9d059SAndy Whitcroft # Flatten any parentheses 3226fb2d2c1bSAndy Whitcroft $value =~ s/\(/ \(/g; 3227fb2d2c1bSAndy Whitcroft $value =~ s/\)/\) /g; 3228e01886adSAndy Whitcroft while ($value =~ s/\[[^\[\]]*\]/1/ || 322963f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 323063f17f89SAndy Whitcroft $Compare\s* 323163f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 323263f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 3233c45dcabdSAndy Whitcroft } 3234fb2d2c1bSAndy Whitcroft#print "value<$value>\n"; 3235fb2d2c1bSAndy Whitcroft if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 3236000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 3237000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 3238c45dcabdSAndy Whitcroft 3239c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 3240000d1cc1SJoe Perches ERROR("SPACING", 3241000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 3242c45dcabdSAndy Whitcroft } 3243c45dcabdSAndy Whitcroft } 324453a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 324553a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 324653a3c448SAndy Whitcroft my $name = $1; 324753a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 3248000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 3249000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 325053a3c448SAndy Whitcroft } 325153a3c448SAndy Whitcroft } 3252c45dcabdSAndy Whitcroft 32530a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 32544a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 32553705ce5bSJoe Perches if (ERROR("SPACING", 32563705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 32573705ce5bSJoe Perches $fix) { 32583705ce5bSJoe Perches $fixed[$linenr - 1] =~ 32593705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 32603705ce5bSJoe Perches } 32610a920b5bSAndy Whitcroft } 32620a920b5bSAndy Whitcroft 3263f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 3264f5fe35ddSAndy Whitcroft# statements after the conditional. 3265170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 32663e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 32673e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 32683e469cdcSAndy Whitcroft if (!defined $stat); 3269170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 3270170d3a22SAndy Whitcroft $remain_next, $off_next); 3271170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 3272170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 3273170d3a22SAndy Whitcroft 3274170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 3275170d3a22SAndy Whitcroft # If the statement carries leading newlines, 3276170d3a22SAndy Whitcroft # then count those as offsets. 3277170d3a22SAndy Whitcroft my ($whitespace) = 3278170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 3279170d3a22SAndy Whitcroft my $offset = 3280170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 3281170d3a22SAndy Whitcroft 3282170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 3283170d3a22SAndy Whitcroft $offset} = 1; 3284170d3a22SAndy Whitcroft } 3285170d3a22SAndy Whitcroft } 3286170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 3287170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 3288171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 32898905a67cSAndy Whitcroft 3290b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 3291000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 3292000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 32938905a67cSAndy Whitcroft } 32948905a67cSAndy Whitcroft 32958905a67cSAndy Whitcroft # Find out what is on the end of the line after the 32968905a67cSAndy Whitcroft # conditional. 3297773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 32988905a67cSAndy Whitcroft $s =~ s/\n.*//g; 329913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 330053210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 330153210168SAndy Whitcroft $c !~ /}\s*while\s*/) 3302773647a0SAndy Whitcroft { 3303bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 3304bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 3305bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 330642bdf74cSHidetoshi Seto my $stat_real = ''; 3307bb44ad39SAndy Whitcroft 330842bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 330942bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 3310bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 3311bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 3312bb44ad39SAndy Whitcroft } 3313bb44ad39SAndy Whitcroft 3314000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3315000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 33168905a67cSAndy Whitcroft } 33178905a67cSAndy Whitcroft } 33188905a67cSAndy Whitcroft 331913214adfSAndy Whitcroft# Check for bitwise tests written as boolean 332013214adfSAndy Whitcroft if ($line =~ / 332113214adfSAndy Whitcroft (?: 332213214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 332313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 332413214adfSAndy Whitcroft (?:\&\&|\|\|) 332513214adfSAndy Whitcroft | 332613214adfSAndy Whitcroft (?:\&\&|\|\|) 332713214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 332813214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 332913214adfSAndy Whitcroft )/x) 333013214adfSAndy Whitcroft { 3331000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 3332000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 333313214adfSAndy Whitcroft } 333413214adfSAndy Whitcroft 33358905a67cSAndy Whitcroft# if and else should not have general statements after it 333613214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 333713214adfSAndy Whitcroft my $s = $1; 333813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 333913214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 3340000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3341000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 33420a920b5bSAndy Whitcroft } 334313214adfSAndy Whitcroft } 334439667782SAndy Whitcroft# if should not continue a brace 334539667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 3346000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3347000d1cc1SJoe Perches "trailing statements should be on next line\n" . 334839667782SAndy Whitcroft $herecurr); 334939667782SAndy Whitcroft } 3350a1080bf8SAndy Whitcroft# case and default should not have general statements after them 3351a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 3352a1080bf8SAndy Whitcroft $line !~ /\G(?: 33533fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 3354a1080bf8SAndy Whitcroft \s*return\s+ 3355a1080bf8SAndy Whitcroft )/xg) 3356a1080bf8SAndy Whitcroft { 3357000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3358000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 3359a1080bf8SAndy Whitcroft } 33600a920b5bSAndy Whitcroft 33610a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 33620a920b5bSAndy Whitcroft # indent level to be relevant to each other. 33630a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 33640a920b5bSAndy Whitcroft $previndent == $indent) { 3365000d1cc1SJoe Perches ERROR("ELSE_AFTER_BRACE", 3366000d1cc1SJoe Perches "else should follow close brace '}'\n" . $hereprev); 33670a920b5bSAndy Whitcroft } 33680a920b5bSAndy Whitcroft 3369c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 3370c2fdda0dSAndy Whitcroft $previndent == $indent) { 3371c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 3372c2fdda0dSAndy Whitcroft 3373c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 3374c2fdda0dSAndy Whitcroft # conditional. 3375773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 3376c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 3377c2fdda0dSAndy Whitcroft 3378c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 3379000d1cc1SJoe Perches ERROR("WHILE_AFTER_BRACE", 3380000d1cc1SJoe Perches "while should follow close brace '}'\n" . $hereprev); 3381c2fdda0dSAndy Whitcroft } 3382c2fdda0dSAndy Whitcroft } 3383c2fdda0dSAndy Whitcroft 338495e2c602SJoe Perches#Specific variable tests 3385323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 3386323c1260SJoe Perches my $var = $1; 338795e2c602SJoe Perches 338895e2c602SJoe Perches#gcc binary extension 338995e2c602SJoe Perches if ($var =~ /^$Binary$/) { 3390d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 3391d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 3392d5e616fcSJoe Perches $fix) { 3393d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 3394d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3395d5e616fcSJoe Perches s/\b$var\b/$hexval/; 3396d5e616fcSJoe Perches } 339795e2c602SJoe Perches } 339895e2c602SJoe Perches 339995e2c602SJoe Perches#CamelCase 3400807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 3401be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 340222735ce8SJoe Perches#Ignore Page<foo> variants 3403807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 340422735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 34053445686aSJoe Perches $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { 34067e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 34077e781f67SJoe Perches my $word = $1; 34087e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 3409d8b07710SJoe Perches if ($check) { 3410d8b07710SJoe Perches seed_camelcase_includes(); 3411d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 3412d8b07710SJoe Perches seed_camelcase_file($realfile); 3413d8b07710SJoe Perches $camelcase_file_seeded = 1; 3414d8b07710SJoe Perches } 3415d8b07710SJoe Perches } 34167e781f67SJoe Perches if (!defined $camelcase{$word}) { 34177e781f67SJoe Perches $camelcase{$word} = 1; 3418be79794bSJoe Perches CHK("CAMELCASE", 34197e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 34207e781f67SJoe Perches } 3421323c1260SJoe Perches } 3422323c1260SJoe Perches } 34233445686aSJoe Perches } 34240a920b5bSAndy Whitcroft 34250a920b5bSAndy Whitcroft#no spaces allowed after \ in define 3426d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 3427d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 3428d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 3429d5e616fcSJoe Perches $fix) { 3430d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 3431d5e616fcSJoe Perches } 34320a920b5bSAndy Whitcroft } 34330a920b5bSAndy Whitcroft 3434653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 3435c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 3436e09dec48SAndy Whitcroft my $file = "$1.h"; 3437e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 3438e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 3439e09dec48SAndy Whitcroft $realfile ne $checkfile && 34407840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 3441c45dcabdSAndy Whitcroft { 3442e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 3443000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 3444000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3445e09dec48SAndy Whitcroft } else { 3446000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 3447000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3448e09dec48SAndy Whitcroft } 34490a920b5bSAndy Whitcroft } 34500a920b5bSAndy Whitcroft } 34510a920b5bSAndy Whitcroft 3452653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 3453653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 3454cf655043SAndy Whitcroft# in a known good container 3455b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 3456b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 3457d8aaf121SAndy Whitcroft my $ln = $linenr; 3458d8aaf121SAndy Whitcroft my $cnt = $realcnt; 3459c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 3460c45dcabdSAndy Whitcroft my $ctx = ''; 3461c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 3462f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3463f74bd194SAndy Whitcroft $ctx = $dstat; 3464c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 3465a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 3466c45dcabdSAndy Whitcroft 3467f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 3468292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 3469c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 3470c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 3471c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 3472c45dcabdSAndy Whitcroft 3473c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 3474bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 3475bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 3476c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 3477bf30d6edSAndy Whitcroft { 3478c45dcabdSAndy Whitcroft } 3479c45dcabdSAndy Whitcroft 3480e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 3481e45bab8eSAndy Whitcroft while ($dstat =~ s/("X*")\s*$Ident/$1/ || 3482e45bab8eSAndy Whitcroft $dstat =~ s/$Ident\s*("X*")/$1/) 3483e45bab8eSAndy Whitcroft { 3484e45bab8eSAndy Whitcroft } 3485e45bab8eSAndy Whitcroft 3486c45dcabdSAndy Whitcroft my $exceptions = qr{ 3487c45dcabdSAndy Whitcroft $Declare| 3488c45dcabdSAndy Whitcroft module_param_named| 3489a0a0a7a9SKees Cook MODULE_PARM_DESC| 3490c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 3491c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 3492383099fdSAndy Whitcroft __typeof__\(| 349322fd2d3eSStefani Seibold union| 349422fd2d3eSStefani Seibold struct| 3495ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 3496ea71a0a0SAndy Whitcroft ^\"|\"$ 3497c45dcabdSAndy Whitcroft }x; 34985eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 3499f74bd194SAndy Whitcroft if ($dstat ne '' && 3500f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 3501f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 35023cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 3503b9df76acSAndy Whitcroft $dstat !~ /^'X'$/ && # character constants 3504f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 3505f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 3506e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 350772f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 3508f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 3509f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 3510f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 3511f95a7e6aSJoe Perches $dstat !~ /^\({/ && # ({... 3512f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 3513c45dcabdSAndy Whitcroft { 3514f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 3515f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 3516f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 3517f74bd194SAndy Whitcroft 3518f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 3519f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 3520c45dcabdSAndy Whitcroft } 3521c45dcabdSAndy Whitcroft 3522f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 3523f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 3524f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 3525f74bd194SAndy Whitcroft } else { 3526000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 3527f74bd194SAndy Whitcroft "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 3528d8aaf121SAndy Whitcroft } 35290a920b5bSAndy Whitcroft } 35305023d347SJoe Perches 3531481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 35325023d347SJoe Perches 35335023d347SJoe Perches } else { 35345023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 3535481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 3536481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 35375023d347SJoe Perches $line =~ /^\+.*\\$/) { 35385023d347SJoe Perches WARN("LINE_CONTINUATIONS", 35395023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 35405023d347SJoe Perches } 3541653d4876SAndy Whitcroft } 35420a920b5bSAndy Whitcroft 3543b13edf7fSJoe Perches# do {} while (0) macro tests: 3544b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 3545b13edf7fSJoe Perches# macro should not end with a semicolon 3546b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 3547b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 3548b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 3549b13edf7fSJoe Perches my $ln = $linenr; 3550b13edf7fSJoe Perches my $cnt = $realcnt; 3551b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 3552b13edf7fSJoe Perches my $ctx = ''; 3553b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 3554b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 3555b13edf7fSJoe Perches $ctx = $dstat; 3556b13edf7fSJoe Perches 3557b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 3558b13edf7fSJoe Perches 3559b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 3560b13edf7fSJoe Perches my $stmts = $2; 3561b13edf7fSJoe Perches my $semis = $3; 3562b13edf7fSJoe Perches 3563b13edf7fSJoe Perches $ctx =~ s/\n*$//; 3564b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 3565b13edf7fSJoe Perches my $herectx = $here . "\n"; 3566b13edf7fSJoe Perches 3567b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 3568b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 3569b13edf7fSJoe Perches } 3570b13edf7fSJoe Perches 3571ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 3572ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 3573b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 3574b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 3575b13edf7fSJoe Perches } 3576b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 3577b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 3578b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 3579b13edf7fSJoe Perches } 3580b13edf7fSJoe Perches } 3581b13edf7fSJoe Perches } 3582b13edf7fSJoe Perches 3583080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 3584080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 3585080ba929SMike Frysinger# . 3586080ba929SMike Frysinger# ALIGN(...) 3587080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 3588080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 3589000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 3590000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 3591080ba929SMike Frysinger } 3592080ba929SMike Frysinger 3593f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 359413214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 359513214adfSAndy Whitcroft my ($level, $endln, @chunks) = 3596cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 359713214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 3598cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 3599cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 3600aad4f614SJoe Perches my @allowed = (); 3601aad4f614SJoe Perches my $allow = 0; 360213214adfSAndy Whitcroft my $seen = 0; 3603773647a0SAndy Whitcroft my $herectx = $here . "\n"; 3604cf655043SAndy Whitcroft my $ln = $linenr - 1; 360513214adfSAndy Whitcroft for my $chunk (@chunks) { 360613214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 360713214adfSAndy Whitcroft 3608773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 3609773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 3610773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 3611773647a0SAndy Whitcroft 3612aad4f614SJoe Perches $allowed[$allow] = 0; 3613773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 3614773647a0SAndy Whitcroft 3615773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 3616773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 3617773647a0SAndy Whitcroft 3618773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 3619cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 3620cf655043SAndy Whitcroft 3621773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 362213214adfSAndy Whitcroft 362313214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 362413214adfSAndy Whitcroft 3625aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 3626cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3627cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3628aad4f614SJoe Perches $allowed[$allow] = 1; 362913214adfSAndy Whitcroft } 363013214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3631cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3632aad4f614SJoe Perches $allowed[$allow] = 1; 363313214adfSAndy Whitcroft } 3634cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3635cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3636aad4f614SJoe Perches $allowed[$allow] = 1; 363713214adfSAndy Whitcroft } 3638aad4f614SJoe Perches $allow++; 363913214adfSAndy Whitcroft } 3640aad4f614SJoe Perches if ($seen) { 3641aad4f614SJoe Perches my $sum_allowed = 0; 3642aad4f614SJoe Perches foreach (@allowed) { 3643aad4f614SJoe Perches $sum_allowed += $_; 3644aad4f614SJoe Perches } 3645aad4f614SJoe Perches if ($sum_allowed == 0) { 3646000d1cc1SJoe Perches WARN("BRACES", 3647000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 3648aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 3649aad4f614SJoe Perches $seen != $allow) { 3650aad4f614SJoe Perches CHK("BRACES", 3651aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 3652aad4f614SJoe Perches } 365313214adfSAndy Whitcroft } 365413214adfSAndy Whitcroft } 365513214adfSAndy Whitcroft } 3656773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 365713214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 3658cf655043SAndy Whitcroft my $allowed = 0; 3659f0a594c1SAndy Whitcroft 3660cf655043SAndy Whitcroft # Check the pre-context. 3661cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 3662cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 3663cf655043SAndy Whitcroft $allowed = 1; 3664f0a594c1SAndy Whitcroft } 3665773647a0SAndy Whitcroft 3666773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 3667773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 3668773647a0SAndy Whitcroft 3669cf655043SAndy Whitcroft # Check the condition. 3670cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 3671773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 3672cf655043SAndy Whitcroft if (defined $cond) { 3673773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3674cf655043SAndy Whitcroft } 3675cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3676cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3677cf655043SAndy Whitcroft $allowed = 1; 3678cf655043SAndy Whitcroft } 3679cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3680cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3681cf655043SAndy Whitcroft $allowed = 1; 3682cf655043SAndy Whitcroft } 3683cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3684cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3685cf655043SAndy Whitcroft $allowed = 1; 3686cf655043SAndy Whitcroft } 3687cf655043SAndy Whitcroft # Check the post-context. 3688cf655043SAndy Whitcroft if (defined $chunks[1]) { 3689cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 3690cf655043SAndy Whitcroft if (defined $cond) { 3691773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3692cf655043SAndy Whitcroft } 3693cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 3694cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 3695cf655043SAndy Whitcroft $allowed = 1; 3696cf655043SAndy Whitcroft } 3697cf655043SAndy Whitcroft } 3698cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 369969932487SJustin P. Mattock my $herectx = $here . "\n"; 3700f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 3701cf655043SAndy Whitcroft 3702f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 370369932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 3704cf655043SAndy Whitcroft } 3705cf655043SAndy Whitcroft 3706000d1cc1SJoe Perches WARN("BRACES", 3707000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 3708f0a594c1SAndy Whitcroft } 3709f0a594c1SAndy Whitcroft } 3710f0a594c1SAndy Whitcroft 37110979ae66SJoe Perches# check for unnecessary blank lines around braces 371277b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 37130979ae66SJoe Perches CHK("BRACES", 37140979ae66SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 37150979ae66SJoe Perches } 371677b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 37170979ae66SJoe Perches CHK("BRACES", 37180979ae66SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 37190979ae66SJoe Perches } 37200979ae66SJoe Perches 37214a0df2efSAndy Whitcroft# no volatiles please 37226c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 37236c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 3724000d1cc1SJoe Perches WARN("VOLATILE", 3725000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 37264a0df2efSAndy Whitcroft } 37274a0df2efSAndy Whitcroft 372800df344fSAndy Whitcroft# warn about #if 0 3729c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 3730000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 3731000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 3732de7d4f0eSAndy Whitcroft $herecurr); 37334a0df2efSAndy Whitcroft } 37344a0df2efSAndy Whitcroft 373503df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 373603df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 373703df4b51SAndy Whitcroft my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 373803df4b51SAndy Whitcroft if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 373903df4b51SAndy Whitcroft WARN('NEEDLESS_IF', 374003df4b51SAndy Whitcroft "$1(NULL) is safe this check is probably not required\n" . $hereprev); 37414c432a8fSGreg Kroah-Hartman } 37424c432a8fSGreg Kroah-Hartman } 3743f0a594c1SAndy Whitcroft 37448716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 37458716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 37468716de38SJoe Perches my $attr = $1; 37478716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 37488716de38SJoe Perches my $ptr = $1; 37498716de38SJoe Perches my $var = $2; 37508716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 37518716de38SJoe Perches ERROR("MISPLACED_INIT", 37528716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 37538716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 37548716de38SJoe Perches WARN("MISPLACED_INIT", 37558716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 37568716de38SJoe Perches $fix) { 37578716de38SJoe Perches $fixed[$linenr - 1] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e; 37588716de38SJoe Perches } 37598716de38SJoe Perches } 37608716de38SJoe Perches } 37618716de38SJoe Perches 37621a15a250SPatrick Pannuto# prefer usleep_range over udelay 376337581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 37641a15a250SPatrick Pannuto # ignore udelay's < 10, however 376537581c28SBruce Allan if (! ($1 < 10) ) { 3766000d1cc1SJoe Perches CHK("USLEEP_RANGE", 3767000d1cc1SJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); 37681a15a250SPatrick Pannuto } 37691a15a250SPatrick Pannuto } 37701a15a250SPatrick Pannuto 377109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 377209ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 377309ef8725SPatrick Pannuto if ($1 < 20) { 3774000d1cc1SJoe Perches WARN("MSLEEP", 3775000d1cc1SJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); 377609ef8725SPatrick Pannuto } 377709ef8725SPatrick Pannuto } 377809ef8725SPatrick Pannuto 377936ec1939SJoe Perches# check for comparisons of jiffies 378036ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 378136ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 378236ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 378336ec1939SJoe Perches } 378436ec1939SJoe Perches 37859d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 37869d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 37879d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 37889d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 37899d7a34a5SJoe Perches } 37909d7a34a5SJoe Perches 379100df344fSAndy Whitcroft# warn about #ifdefs in C files 3792c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 379300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 379400df344fSAndy Whitcroft# print "$herecurr"; 379500df344fSAndy Whitcroft# $clean = 0; 379600df344fSAndy Whitcroft# } 379700df344fSAndy Whitcroft 379822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 3799c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 38003705ce5bSJoe Perches if (ERROR("SPACING", 38013705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 38023705ce5bSJoe Perches $fix) { 38033705ce5bSJoe Perches $fixed[$linenr - 1] =~ 38043705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 38053705ce5bSJoe Perches } 38063705ce5bSJoe Perches 380722f2a2efSAndy Whitcroft } 380822f2a2efSAndy Whitcroft 38094a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 3810171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 3811171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 38124a0df2efSAndy Whitcroft my $which = $1; 38134a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3814000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 3815000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 38164a0df2efSAndy Whitcroft } 38174a0df2efSAndy Whitcroft } 38184a0df2efSAndy Whitcroft# check for memory barriers without a comment. 38194a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 38204a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 3821000d1cc1SJoe Perches CHK("MEMORY_BARRIER", 3822000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 38234a0df2efSAndy Whitcroft } 38244a0df2efSAndy Whitcroft } 38254a0df2efSAndy Whitcroft# check of hardware specific defines 3826c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 3827000d1cc1SJoe Perches CHK("ARCH_DEFINES", 3828000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 38290a920b5bSAndy Whitcroft } 3830653d4876SAndy Whitcroft 3831d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 3832d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 3833000d1cc1SJoe Perches WARN("STORAGE_CLASS", 3834000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 3835d4977c78STobias Klauser } 3836d4977c78STobias Klauser 3837de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 3838de7d4f0eSAndy Whitcroft# storage class and type. 38399c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 38409c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 3841000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 3842000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 3843de7d4f0eSAndy Whitcroft } 3844de7d4f0eSAndy Whitcroft 38458905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 38468905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 3847d5e616fcSJoe Perches if (WARN("INLINE", 3848d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 3849d5e616fcSJoe Perches $fix) { 3850d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/; 3851d5e616fcSJoe Perches 3852d5e616fcSJoe Perches } 38538905a67cSAndy Whitcroft } 38548905a67cSAndy Whitcroft 38553d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 38563d130fd0SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 3857000d1cc1SJoe Perches WARN("PREFER_PACKED", 3858000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 38593d130fd0SJoe Perches } 38603d130fd0SJoe Perches 386139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 386239b7e287SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 3863000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 3864000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 386539b7e287SJoe Perches } 386639b7e287SJoe Perches 38675f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 38685f14d3bdSJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 3869d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 3870d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 3871d5e616fcSJoe Perches $fix) { 3872d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 3873d5e616fcSJoe Perches 3874d5e616fcSJoe Perches } 38755f14d3bdSJoe Perches } 38765f14d3bdSJoe Perches 38776061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 38786061d949SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 3879d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 3880d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 3881d5e616fcSJoe Perches $fix) { 3882d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 3883d5e616fcSJoe Perches } 38846061d949SJoe Perches } 38856061d949SJoe Perches 38868f53a9b8SJoe Perches# check for sizeof(&) 38878f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 3888000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 3889000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 38908f53a9b8SJoe Perches } 38918f53a9b8SJoe Perches 389266c80b60SJoe Perches# check for sizeof without parenthesis 389366c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 3894d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 3895d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 3896d5e616fcSJoe Perches $fix) { 3897d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 3898d5e616fcSJoe Perches } 389966c80b60SJoe Perches } 390066c80b60SJoe Perches 3901428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 3902428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 3903000d1cc1SJoe Perches WARN("LINE_CONTINUATIONS", 3904000d1cc1SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 3905428e2fdcSJoe Perches } 3906428e2fdcSJoe Perches 390788982feaSJoe Perches# check for struct spinlock declarations 390888982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 390988982feaSJoe Perches WARN("USE_SPINLOCK_T", 391088982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 391188982feaSJoe Perches } 391288982feaSJoe Perches 3913a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 391406668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 3915a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 391606668727SJoe Perches if ($fmt ne "" && $fmt !~ /[^\\]\%/) { 3917d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 3918d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 3919d5e616fcSJoe Perches $fix) { 3920d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/; 3921d5e616fcSJoe Perches } 3922a6962d72SJoe Perches } 3923a6962d72SJoe Perches } 3924a6962d72SJoe Perches 3925554e165cSAndy Whitcroft# Check for misused memsets 3926d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3927d1fe9c09SJoe Perches defined $stat && 3928d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 3929554e165cSAndy Whitcroft 3930d7c76ba7SJoe Perches my $ms_addr = $2; 3931d1fe9c09SJoe Perches my $ms_val = $7; 3932d1fe9c09SJoe Perches my $ms_size = $12; 3933d7c76ba7SJoe Perches 3934554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 3935554e165cSAndy Whitcroft ERROR("MEMSET", 3936d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 3937554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 3938554e165cSAndy Whitcroft WARN("MEMSET", 3939d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 3940d7c76ba7SJoe Perches } 3941d7c76ba7SJoe Perches } 3942d7c76ba7SJoe Perches 3943d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 3944d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 3945d1fe9c09SJoe Perches defined $stat && 3946d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 3947d1fe9c09SJoe Perches if (defined $2 || defined $7) { 3948d7c76ba7SJoe Perches my $call = $1; 3949d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 3950d7c76ba7SJoe Perches my $arg1 = $3; 3951d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 3952d1fe9c09SJoe Perches my $arg2 = $8; 3953d7c76ba7SJoe Perches my $cast; 3954d7c76ba7SJoe Perches 3955d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 3956d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 3957d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 3958d7c76ba7SJoe Perches $cast = $cast1; 3959d7c76ba7SJoe Perches } else { 3960d7c76ba7SJoe Perches $cast = $cast2; 3961d7c76ba7SJoe Perches } 3962d7c76ba7SJoe Perches WARN("MINMAX", 3963d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 3964554e165cSAndy Whitcroft } 3965554e165cSAndy Whitcroft } 3966554e165cSAndy Whitcroft 39674a273195SJoe Perches# check usleep_range arguments 39684a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 39694a273195SJoe Perches defined $stat && 39704a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 39714a273195SJoe Perches my $min = $1; 39724a273195SJoe Perches my $max = $7; 39734a273195SJoe Perches if ($min eq $max) { 39744a273195SJoe Perches WARN("USLEEP_RANGE", 39754a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 39764a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 39774a273195SJoe Perches $min > $max) { 39784a273195SJoe Perches WARN("USLEEP_RANGE", 39794a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 39804a273195SJoe Perches } 39814a273195SJoe Perches } 39824a273195SJoe Perches 398370dc8a48SJoe Perches# check for new externs in .h files. 398470dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 398570dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 3986d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 398770dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 398870dc8a48SJoe Perches $fix) { 398970dc8a48SJoe Perches $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 399070dc8a48SJoe Perches } 399170dc8a48SJoe Perches } 399270dc8a48SJoe Perches 3993de7d4f0eSAndy Whitcroft# check for new externs in .c files. 3994171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 3995c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3996171ae1a4SAndy Whitcroft { 3997c45dcabdSAndy Whitcroft my $function_name = $1; 3998c45dcabdSAndy Whitcroft my $paren_space = $2; 3999171ae1a4SAndy Whitcroft 4000171ae1a4SAndy Whitcroft my $s = $stat; 4001171ae1a4SAndy Whitcroft if (defined $cond) { 4002171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 4003171ae1a4SAndy Whitcroft } 4004c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 4005c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 4006c45dcabdSAndy Whitcroft { 4007000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4008000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4009de7d4f0eSAndy Whitcroft } 4010de7d4f0eSAndy Whitcroft 4011171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 4012000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 4013000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 4014171ae1a4SAndy Whitcroft } 40159c9ba34eSAndy Whitcroft 40169c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 40179c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 40189c9ba34eSAndy Whitcroft { 4019000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4020000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4021171ae1a4SAndy Whitcroft } 4022171ae1a4SAndy Whitcroft 4023de7d4f0eSAndy Whitcroft# checks for new __setup's 4024de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 4025de7d4f0eSAndy Whitcroft my $name = $1; 4026de7d4f0eSAndy Whitcroft 4027de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 4028000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 4029000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 4030de7d4f0eSAndy Whitcroft } 4031653d4876SAndy Whitcroft } 40329c0ca6f9SAndy Whitcroft 40339c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 4034caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 4035000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 4036000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 40379c0ca6f9SAndy Whitcroft } 403813214adfSAndy Whitcroft 4039a640d25cSJoe Perches# alloc style 4040a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 4041a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 4042a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 4043a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 4044a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 4045a640d25cSJoe Perches } 4046a640d25cSJoe Perches 4047972fdea2SJoe Perches# check for krealloc arg reuse 4048972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 4049972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 4050972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 4051972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 4052972fdea2SJoe Perches } 4053972fdea2SJoe Perches 40545ce59ae0SJoe Perches# check for alloc argument mismatch 40555ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 40565ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 40575ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 40585ce59ae0SJoe Perches } 40595ce59ae0SJoe Perches 4060caf2a54fSJoe Perches# check for multiple semicolons 4061caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 4062d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 4063d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 4064d5e616fcSJoe Perches $fix) { 4065d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g; 4066d5e616fcSJoe Perches } 4067d1e2ad07SJoe Perches } 4068d1e2ad07SJoe Perches 4069d1e2ad07SJoe Perches# check for switch/default statements without a break; 4070d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 4071d1e2ad07SJoe Perches defined $stat && 4072d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 4073d1e2ad07SJoe Perches my $ctx = ''; 4074d1e2ad07SJoe Perches my $herectx = $here . "\n"; 4075d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 4076d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4077d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4078d1e2ad07SJoe Perches } 4079d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 4080d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 4081caf2a54fSJoe Perches } 4082caf2a54fSJoe Perches 408313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 4084d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 4085d5e616fcSJoe Perches if (WARN("USE_FUNC", 4086d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 4087d5e616fcSJoe Perches $fix) { 4088d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g; 4089d5e616fcSJoe Perches } 409013214adfSAndy Whitcroft } 4091773647a0SAndy Whitcroft 40922c92488aSJoe Perches# check for use of yield() 40932c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 40942c92488aSJoe Perches WARN("YIELD", 40952c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 40962c92488aSJoe Perches } 40972c92488aSJoe Perches 4098179f8f40SJoe Perches# check for comparisons against true and false 4099179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 4100179f8f40SJoe Perches my $lead = $1; 4101179f8f40SJoe Perches my $arg = $2; 4102179f8f40SJoe Perches my $test = $3; 4103179f8f40SJoe Perches my $otype = $4; 4104179f8f40SJoe Perches my $trail = $5; 4105179f8f40SJoe Perches my $op = "!"; 4106179f8f40SJoe Perches 4107179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 4108179f8f40SJoe Perches 4109179f8f40SJoe Perches my $type = lc($otype); 4110179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 4111179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 4112179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 4113179f8f40SJoe Perches $op = ""; 4114179f8f40SJoe Perches } 4115179f8f40SJoe Perches 4116179f8f40SJoe Perches CHK("BOOL_COMPARISON", 4117179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 4118179f8f40SJoe Perches 4119179f8f40SJoe Perches## maybe suggesting a correct construct would better 4120179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 4121179f8f40SJoe Perches 4122179f8f40SJoe Perches } 4123179f8f40SJoe Perches } 4124179f8f40SJoe Perches 41254882720bSThomas Gleixner# check for semaphores initialized locked 41264882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 4127000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 4128000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 4129773647a0SAndy Whitcroft } 41306712d858SJoe Perches 413167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 413267d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 4133000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 413467d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 4135773647a0SAndy Whitcroft } 41366712d858SJoe Perches 4137f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 4138f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 4139000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 4140000d1cc1SJoe Perches "please use device_initcall() instead of __initcall()\n" . $herecurr); 4141f3db6639SMichael Ellerman } 41426712d858SJoe Perches 414379404849SEmese Revfy# check for various ops structs, ensure they are const. 414479404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 414579404849SEmese Revfy address_space_operations| 414679404849SEmese Revfy backlight_ops| 414779404849SEmese Revfy block_device_operations| 414879404849SEmese Revfy dentry_operations| 414979404849SEmese Revfy dev_pm_ops| 415079404849SEmese Revfy dma_map_ops| 415179404849SEmese Revfy extent_io_ops| 415279404849SEmese Revfy file_lock_operations| 415379404849SEmese Revfy file_operations| 415479404849SEmese Revfy hv_ops| 415579404849SEmese Revfy ide_dma_ops| 415679404849SEmese Revfy intel_dvo_dev_ops| 415779404849SEmese Revfy item_operations| 415879404849SEmese Revfy iwl_ops| 415979404849SEmese Revfy kgdb_arch| 416079404849SEmese Revfy kgdb_io| 416179404849SEmese Revfy kset_uevent_ops| 416279404849SEmese Revfy lock_manager_operations| 416379404849SEmese Revfy microcode_ops| 416479404849SEmese Revfy mtrr_ops| 416579404849SEmese Revfy neigh_ops| 416679404849SEmese Revfy nlmsvc_binding| 416779404849SEmese Revfy pci_raw_ops| 416879404849SEmese Revfy pipe_buf_operations| 416979404849SEmese Revfy platform_hibernation_ops| 417079404849SEmese Revfy platform_suspend_ops| 417179404849SEmese Revfy proto_ops| 417279404849SEmese Revfy rpc_pipe_ops| 417379404849SEmese Revfy seq_operations| 417479404849SEmese Revfy snd_ac97_build_ops| 417579404849SEmese Revfy soc_pcmcia_socket_ops| 417679404849SEmese Revfy stacktrace_ops| 417779404849SEmese Revfy sysfs_ops| 417879404849SEmese Revfy tty_operations| 417979404849SEmese Revfy usb_mon_operations| 418079404849SEmese Revfy wd_ops}x; 41816903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 418279404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 4183000d1cc1SJoe Perches WARN("CONST_STRUCT", 4184000d1cc1SJoe Perches "struct $1 should normally be const\n" . 41856903ffb2SAndy Whitcroft $herecurr); 41862b6db5cbSAndy Whitcroft } 4187773647a0SAndy Whitcroft 4188773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 4189773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 4190773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 4191c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 4192c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 4193171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 4194171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 4195171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 4196773647a0SAndy Whitcroft { 4197000d1cc1SJoe Perches WARN("NR_CPUS", 4198000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 4199773647a0SAndy Whitcroft } 42009c9ba34eSAndy Whitcroft 4201*52ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 4202*52ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 4203*52ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 4204*52ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 4205*52ea8506SJoe Perches } 4206*52ea8506SJoe Perches 42079c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 42089c9ba34eSAndy Whitcroft my $string; 42099c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 42109c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 42112a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 42129c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 4213000d1cc1SJoe Perches WARN("PRINTF_L", 4214000d1cc1SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 42159c9ba34eSAndy Whitcroft last; 42169c9ba34eSAndy Whitcroft } 42179c9ba34eSAndy Whitcroft } 4218691d77b6SAndy Whitcroft 4219691d77b6SAndy Whitcroft# whine mightly about in_atomic 4220691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 4221691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 4222000d1cc1SJoe Perches ERROR("IN_ATOMIC", 4223000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 4224f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 4225000d1cc1SJoe Perches WARN("IN_ATOMIC", 4226000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 4227691d77b6SAndy Whitcroft } 4228691d77b6SAndy Whitcroft } 42291704f47bSPeter Zijlstra 42301704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 42311704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 42321704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 42331704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 42341704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 42351704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 4236000d1cc1SJoe Perches ERROR("LOCKDEP", 4237000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 42381704f47bSPeter Zijlstra } 42391704f47bSPeter Zijlstra } 424088f8831cSDave Jones 424188f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 424288f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 4243000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 4244000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 424588f8831cSDave Jones } 424613214adfSAndy Whitcroft } 424713214adfSAndy Whitcroft 424813214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 424913214adfSAndy Whitcroft # so just keep quiet. 425013214adfSAndy Whitcroft if ($#rawlines == -1) { 425113214adfSAndy Whitcroft exit(0); 42520a920b5bSAndy Whitcroft } 42530a920b5bSAndy Whitcroft 42548905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 42558905a67cSAndy Whitcroft # things that appear to be patches. 42568905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 42578905a67cSAndy Whitcroft exit(0); 42588905a67cSAndy Whitcroft } 42598905a67cSAndy Whitcroft 42608905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 42618905a67cSAndy Whitcroft # just keep quiet. 42628905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 42638905a67cSAndy Whitcroft exit(0); 42648905a67cSAndy Whitcroft } 42658905a67cSAndy Whitcroft 42668905a67cSAndy Whitcroft if (!$is_patch) { 4267000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 4268000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 42690a920b5bSAndy Whitcroft } 42700a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 4271000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 4272000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 42730a920b5bSAndy Whitcroft } 42740a920b5bSAndy Whitcroft 4275f0a594c1SAndy Whitcroft print report_dump(); 427613214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 427713214adfSAndy Whitcroft print "$filename " if ($summary_file); 42786c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 42796c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 42806c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 42818905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 42826c72ffaaSAndy Whitcroft } 42838905a67cSAndy Whitcroft 4284d2c0a235SAndy Whitcroft if ($quiet == 0) { 4285d1fe9c09SJoe Perches 4286d1fe9c09SJoe Perches if ($^V lt 5.10.0) { 4287d1fe9c09SJoe Perches print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); 4288d1fe9c09SJoe Perches print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); 4289d1fe9c09SJoe Perches } 4290d1fe9c09SJoe Perches 4291d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 4292d2c0a235SAndy Whitcroft # then suggest that. 4293d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 4294d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 4295d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 4296b0781216SMike Frysinger $rpt_cleaners = 0; 4297d2c0a235SAndy Whitcroft } 4298d2c0a235SAndy Whitcroft } 4299d2c0a235SAndy Whitcroft 430091bfe484SJoe Perches hash_show_words(\%use_type, "Used"); 430191bfe484SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 4302000d1cc1SJoe Perches 43033705ce5bSJoe Perches if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { 43043705ce5bSJoe Perches my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; 43053705ce5bSJoe Perches my $linecount = 0; 43063705ce5bSJoe Perches my $f; 43073705ce5bSJoe Perches 43083705ce5bSJoe Perches open($f, '>', $newfile) 43093705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 43103705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 43113705ce5bSJoe Perches $linecount++; 43123705ce5bSJoe Perches if ($file) { 43133705ce5bSJoe Perches if ($linecount > 3) { 43143705ce5bSJoe Perches $fixed_line =~ s/^\+//; 43153705ce5bSJoe Perches print $f $fixed_line. "\n"; 43163705ce5bSJoe Perches } 43173705ce5bSJoe Perches } else { 43183705ce5bSJoe Perches print $f $fixed_line . "\n"; 43193705ce5bSJoe Perches } 43203705ce5bSJoe Perches } 43213705ce5bSJoe Perches close($f); 43223705ce5bSJoe Perches 43233705ce5bSJoe Perches if (!$quiet) { 43243705ce5bSJoe Perches print << "EOM"; 43253705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 43263705ce5bSJoe Perches 43273705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 43283705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 43293705ce5bSJoe Perches 43303705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 43313705ce5bSJoe PerchesNo warranties, expressed or implied... 43323705ce5bSJoe Perches 43333705ce5bSJoe PerchesEOM 43343705ce5bSJoe Perches } 43353705ce5bSJoe Perches } 43363705ce5bSJoe Perches 43370a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 4338c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 43390a920b5bSAndy Whitcroft } 43400a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 4341000d1cc1SJoe Perches print << "EOM"; 4342000d1cc1SJoe Perches$vname has style problems, please review. 4343000d1cc1SJoe Perches 4344000d1cc1SJoe PerchesIf any of these errors are false positives, please report 4345000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 4346000d1cc1SJoe PerchesEOM 43470a920b5bSAndy Whitcroft } 434813214adfSAndy Whitcroft 43490a920b5bSAndy Whitcroft return $clean; 43500a920b5bSAndy Whitcroft} 4351