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; 329624b8d6SJoe Perchesmy $fix_inplace = 0; 336c72ffaaSAndy Whitcroftmy $root; 34c2fdda0dSAndy Whitcroftmy %debug; 353445686aSJoe Perchesmy %camelcase = (); 3691bfe484SJoe Perchesmy %use_type = (); 3791bfe484SJoe Perchesmy @use = (); 3891bfe484SJoe Perchesmy %ignore_type = (); 39000d1cc1SJoe Perchesmy @ignore = (); 4077f5b10aSHannes Edermy $help = 0; 41000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 426cd7f386SJoe Perchesmy $max_line_length = 80; 43d62a201fSDave Hansenmy $ignore_perl_version = 0; 44d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 4577f5b10aSHannes Eder 4677f5b10aSHannes Edersub help { 4777f5b10aSHannes Eder my ($exitcode) = @_; 4877f5b10aSHannes Eder 4977f5b10aSHannes Eder print << "EOM"; 5077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 5177f5b10aSHannes EderVersion: $V 5277f5b10aSHannes Eder 5377f5b10aSHannes EderOptions: 5477f5b10aSHannes Eder -q, --quiet quiet 5577f5b10aSHannes Eder --no-tree run without a kernel tree 5677f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 5777f5b10aSHannes Eder --patch treat FILE as patchfile (default) 5877f5b10aSHannes Eder --emacs emacs compile window format 5977f5b10aSHannes Eder --terse one line per report 6077f5b10aSHannes Eder -f, --file treat FILE as regular source file 6177f5b10aSHannes Eder --subjective, --strict enable more subjective tests 6291bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 63000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 646cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 65000d1cc1SJoe Perches --show-types show the message "types" in the output 6677f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 6777f5b10aSHannes Eder --no-summary suppress the per-file summary 6877f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 6977f5b10aSHannes Eder --summary-file include the filename in summary 7077f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 7177f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 7277f5b10aSHannes Eder is all off) 7377f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 7477f5b10aSHannes Eder literally 753705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 763705ce5bSJoe Perches If correctable single-line errors exist, create 773705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 783705ce5bSJoe Perches with potential errors corrected to the preferred 793705ce5bSJoe Perches checkpatch style 809624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 819624b8d6SJoe Perches Is the same as --fix, but overwrites the input 829624b8d6SJoe Perches file. It's your fault if there's no backup or git 83d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 84d62a201fSDave Hansen runtime errors. 8577f5b10aSHannes Eder -h, --help, --version display this help and exit 8677f5b10aSHannes Eder 8777f5b10aSHannes EderWhen FILE is - read standard input. 8877f5b10aSHannes EderEOM 8977f5b10aSHannes Eder 9077f5b10aSHannes Eder exit($exitcode); 9177f5b10aSHannes Eder} 9277f5b10aSHannes Eder 93000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 94000d1cc1SJoe Perchesif (-f $conf) { 95000d1cc1SJoe Perches my @conf_args; 96000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 97000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 98000d1cc1SJoe Perches 99000d1cc1SJoe Perches while (<$conffile>) { 100000d1cc1SJoe Perches my $line = $_; 101000d1cc1SJoe Perches 102000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 103000d1cc1SJoe Perches $line =~ s/^\s*//g; 104000d1cc1SJoe Perches $line =~ s/\s+/ /g; 105000d1cc1SJoe Perches 106000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 107000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 108000d1cc1SJoe Perches 109000d1cc1SJoe Perches my @words = split(" ", $line); 110000d1cc1SJoe Perches foreach my $word (@words) { 111000d1cc1SJoe Perches last if ($word =~ m/^#/); 112000d1cc1SJoe Perches push (@conf_args, $word); 113000d1cc1SJoe Perches } 114000d1cc1SJoe Perches } 115000d1cc1SJoe Perches close($conffile); 116000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 117000d1cc1SJoe Perches} 118000d1cc1SJoe Perches 1190a920b5bSAndy WhitcroftGetOptions( 1206c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1210a920b5bSAndy Whitcroft 'tree!' => \$tree, 1220a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1230a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1246c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1258905a67cSAndy Whitcroft 'terse!' => \$terse, 12677f5b10aSHannes Eder 'f|file!' => \$file, 1276c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1286c72ffaaSAndy Whitcroft 'strict!' => \$check, 129000d1cc1SJoe Perches 'ignore=s' => \@ignore, 13091bfe484SJoe Perches 'types=s' => \@use, 131000d1cc1SJoe Perches 'show-types!' => \$show_types, 1326cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 1336c72ffaaSAndy Whitcroft 'root=s' => \$root, 1348905a67cSAndy Whitcroft 'summary!' => \$summary, 1358905a67cSAndy Whitcroft 'mailback!' => \$mailback, 13613214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 1373705ce5bSJoe Perches 'fix!' => \$fix, 1389624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 139d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 140c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 141773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 14277f5b10aSHannes Eder 'h|help' => \$help, 14377f5b10aSHannes Eder 'version' => \$help 14477f5b10aSHannes Eder) or help(1); 14577f5b10aSHannes Eder 14677f5b10aSHannes Ederhelp(0) if ($help); 1470a920b5bSAndy Whitcroft 1489624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 1499624b8d6SJoe Perches 1500a920b5bSAndy Whitcroftmy $exit = 0; 1510a920b5bSAndy Whitcroft 152d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 153d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 154d62a201fSDave Hansen if (!$ignore_perl_version) { 155d62a201fSDave Hansen exit(1); 156d62a201fSDave Hansen } 157d62a201fSDave Hansen} 158d62a201fSDave Hansen 1590a920b5bSAndy Whitcroftif ($#ARGV < 0) { 16077f5b10aSHannes Eder print "$P: no input files\n"; 1610a920b5bSAndy Whitcroft exit(1); 1620a920b5bSAndy Whitcroft} 1630a920b5bSAndy Whitcroft 16491bfe484SJoe Perchessub hash_save_array_words { 16591bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 16691bfe484SJoe Perches 16791bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 16891bfe484SJoe Perches foreach my $word (@array) { 169000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 170000d1cc1SJoe Perches $word =~ s/^\s*//g; 171000d1cc1SJoe Perches $word =~ s/\s+/ /g; 172000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 173000d1cc1SJoe Perches 174000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 175000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 176000d1cc1SJoe Perches 17791bfe484SJoe Perches $hashRef->{$word}++; 178000d1cc1SJoe Perches } 17991bfe484SJoe Perches} 18091bfe484SJoe Perches 18191bfe484SJoe Perchessub hash_show_words { 18291bfe484SJoe Perches my ($hashRef, $prefix) = @_; 18391bfe484SJoe Perches 18458cb3cf6SJoe Perches if ($quiet == 0 && keys %$hashRef) { 18591bfe484SJoe Perches print "NOTE: $prefix message types:"; 18658cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 18791bfe484SJoe Perches print " $word"; 18891bfe484SJoe Perches } 18991bfe484SJoe Perches print "\n\n"; 19091bfe484SJoe Perches } 19191bfe484SJoe Perches} 19291bfe484SJoe Perches 19391bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 19491bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 195000d1cc1SJoe Perches 196c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 197c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 1987429c690SAndy Whitcroftmy $dbg_type = 0; 199a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 200c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 20121caa13cSAndy Whitcroft ## no critic 20221caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 20321caa13cSAndy Whitcroft die "$@" if ($@); 204c2fdda0dSAndy Whitcroft} 205c2fdda0dSAndy Whitcroft 206d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 207d2c0a235SAndy Whitcroft 2088905a67cSAndy Whitcroftif ($terse) { 2098905a67cSAndy Whitcroft $emacs = 1; 2108905a67cSAndy Whitcroft $quiet++; 2118905a67cSAndy Whitcroft} 2128905a67cSAndy Whitcroft 2136c72ffaaSAndy Whitcroftif ($tree) { 2146c72ffaaSAndy Whitcroft if (defined $root) { 2156c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 2166c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 2176c72ffaaSAndy Whitcroft } 2186c72ffaaSAndy Whitcroft } else { 2196c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 2206c72ffaaSAndy Whitcroft $root = '.'; 2216c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 2226c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 2236c72ffaaSAndy Whitcroft $root = $1; 2246c72ffaaSAndy Whitcroft } 2256c72ffaaSAndy Whitcroft } 2266c72ffaaSAndy Whitcroft 2276c72ffaaSAndy Whitcroft if (!defined $root) { 2280a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 2290a920b5bSAndy Whitcroft exit(2); 2300a920b5bSAndy Whitcroft } 2316c72ffaaSAndy Whitcroft} 2326c72ffaaSAndy Whitcroft 2336c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 2346c72ffaaSAndy Whitcroft 2352ceb532bSAndy Whitcroftour $Ident = qr{ 2362ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 2372ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 2382ceb532bSAndy Whitcroft }x; 2396c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 2406c72ffaaSAndy Whitcroftour $Sparse = qr{ 2416c72ffaaSAndy Whitcroft __user| 2426c72ffaaSAndy Whitcroft __kernel| 2436c72ffaaSAndy Whitcroft __force| 2446c72ffaaSAndy Whitcroft __iomem| 2456c72ffaaSAndy Whitcroft __must_check| 2466c72ffaaSAndy Whitcroft __init_refok| 247417495edSAndy Whitcroft __kprobes| 248165e72a6SSven Eckelmann __ref| 249165e72a6SSven Eckelmann __rcu 2506c72ffaaSAndy Whitcroft }x; 251e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 252e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 253e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 254e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 255e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 2568716de38SJoe Perches 25752131292SWolfram Sang# Notes to $Attribute: 25852131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 2596c72ffaaSAndy Whitcroftour $Attribute = qr{ 2606c72ffaaSAndy Whitcroft const| 26103f1df7dSJoe Perches __percpu| 26203f1df7dSJoe Perches __nocast| 26303f1df7dSJoe Perches __safe| 26403f1df7dSJoe Perches __bitwise__| 26503f1df7dSJoe Perches __packed__| 26603f1df7dSJoe Perches __packed2__| 26703f1df7dSJoe Perches __naked| 26803f1df7dSJoe Perches __maybe_unused| 26903f1df7dSJoe Perches __always_unused| 27003f1df7dSJoe Perches __noreturn| 27103f1df7dSJoe Perches __used| 27203f1df7dSJoe Perches __cold| 27303f1df7dSJoe Perches __noclone| 27403f1df7dSJoe Perches __deprecated| 2756c72ffaaSAndy Whitcroft __read_mostly| 2766c72ffaaSAndy Whitcroft __kprobes| 2778716de38SJoe Perches $InitAttribute| 27824e1d81aSAndy Whitcroft ____cacheline_aligned| 27924e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 2805fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 2815fe3af11SAndy Whitcroft __weak 2826c72ffaaSAndy Whitcroft }x; 283c45dcabdSAndy Whitcroftour $Modifier; 2846c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 2856c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 2866c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 2876c72ffaaSAndy Whitcroft 28895e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 28995e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 29095e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 29195e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 2922435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 293326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 294326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 295326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 29674349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 2972435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 298326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 299447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 30023f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 3016c72ffaaSAndy Whitcroftour $Operators = qr{ 3026c72ffaaSAndy Whitcroft <=|>=|==|!=| 3036c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 30423f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 3056c72ffaaSAndy Whitcroft }x; 3066c72ffaaSAndy Whitcroft 3078905a67cSAndy Whitcroftour $NonptrType; 3088716de38SJoe Perchesour $NonptrTypeWithAttr; 3098905a67cSAndy Whitcroftour $Type; 3108905a67cSAndy Whitcroftour $Declare; 3118905a67cSAndy Whitcroft 31215662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 31315662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 314171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 315171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 316171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 317171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 318171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 319171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 320171ae1a4SAndy Whitcroft}x; 321171ae1a4SAndy Whitcroft 32215662b3eSJoe Perchesour $UTF8 = qr{ 32315662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 32415662b3eSJoe Perches | $NON_ASCII_UTF8 32515662b3eSJoe Perches}x; 32615662b3eSJoe Perches 3278ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 328fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 3298ed22cadSAndy Whitcroft atomic_t 3308ed22cadSAndy Whitcroft)}; 3318ed22cadSAndy Whitcroft 332691e669bSJoe Perchesour $logFunctions = qr{(?x: 3336e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 3347d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 3356e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 336b0531722SJoe Perches panic| 33706668727SJoe Perches MODULE_[A-Z_]+| 33806668727SJoe Perches seq_vprintf|seq_printf|seq_puts 339691e669bSJoe Perches)}; 340691e669bSJoe Perches 34120112475SJoe Perchesour $signature_tags = qr{(?xi: 34220112475SJoe Perches Signed-off-by:| 34320112475SJoe Perches Acked-by:| 34420112475SJoe Perches Tested-by:| 34520112475SJoe Perches Reviewed-by:| 34620112475SJoe Perches Reported-by:| 3478543ae12SMugunthan V N Suggested-by:| 34820112475SJoe Perches To:| 34920112475SJoe Perches Cc: 35020112475SJoe Perches)}; 35120112475SJoe Perches 3528905a67cSAndy Whitcroftour @typeList = ( 3538905a67cSAndy Whitcroft qr{void}, 354c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 355c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 356c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 357c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 358c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 359c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 360c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 3618905a67cSAndy Whitcroft qr{unsigned}, 3628905a67cSAndy Whitcroft qr{float}, 3638905a67cSAndy Whitcroft qr{double}, 3648905a67cSAndy Whitcroft qr{bool}, 3658905a67cSAndy Whitcroft qr{struct\s+$Ident}, 3668905a67cSAndy Whitcroft qr{union\s+$Ident}, 3678905a67cSAndy Whitcroft qr{enum\s+$Ident}, 3688905a67cSAndy Whitcroft qr{${Ident}_t}, 3698905a67cSAndy Whitcroft qr{${Ident}_handler}, 3708905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 3718905a67cSAndy Whitcroft); 3728716de38SJoe Perchesour @typeListWithAttr = ( 3738716de38SJoe Perches @typeList, 3748716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 3758716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 3768716de38SJoe Perches); 3778716de38SJoe Perches 378c45dcabdSAndy Whitcroftour @modifierList = ( 379c45dcabdSAndy Whitcroft qr{fastcall}, 380c45dcabdSAndy Whitcroft); 3818905a67cSAndy Whitcroft 3822435880fSJoe Perchesour @mode_permission_funcs = ( 3832435880fSJoe Perches ["module_param", 3], 3842435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 3852435880fSJoe Perches ["module_param_array_named", 5], 3862435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 3872435880fSJoe Perches ["proc_create(?:_data|)", 2], 3882435880fSJoe Perches ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], 3892435880fSJoe Perches); 3902435880fSJoe Perches 3917840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 3927840a94cSWolfram Sang irq| 3937840a94cSWolfram Sang memory 3947840a94cSWolfram Sang)}; 3957840a94cSWolfram Sang# memory.h: ARM has a custom one 3967840a94cSWolfram Sang 3978905a67cSAndy Whitcroftsub build_types { 398d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 399d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 4008716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 401c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 4028905a67cSAndy Whitcroft $NonptrType = qr{ 403d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 404cf655043SAndy Whitcroft (?: 4056b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 4068ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 407c45dcabdSAndy Whitcroft (?:${all}\b) 408cf655043SAndy Whitcroft ) 409c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 4108905a67cSAndy Whitcroft }x; 4118716de38SJoe Perches $NonptrTypeWithAttr = qr{ 4128716de38SJoe Perches (?:$Modifier\s+|const\s+)* 4138716de38SJoe Perches (?: 4148716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 4158716de38SJoe Perches (?:$typeTypedefs\b)| 4168716de38SJoe Perches (?:${allWithAttr}\b) 4178716de38SJoe Perches ) 4188716de38SJoe Perches (?:\s+$Modifier|\s+const)* 4198716de38SJoe Perches }x; 4208905a67cSAndy Whitcroft $Type = qr{ 421c45dcabdSAndy Whitcroft $NonptrType 422b337d8b8SAndy Whitcroft (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 423c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 4248905a67cSAndy Whitcroft }x; 4258905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 4268905a67cSAndy Whitcroft} 4278905a67cSAndy Whitcroftbuild_types(); 4286c72ffaaSAndy Whitcroft 4297d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 430d1fe9c09SJoe Perches 431d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 432d1fe9c09SJoe Perches# requires at least perl version v5.10.0 433d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 434d1fe9c09SJoe Perches 435d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 4362435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 437d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; 4387d2367afSJoe Perches 4397d2367afSJoe Perchessub deparenthesize { 4407d2367afSJoe Perches my ($string) = @_; 4417d2367afSJoe Perches return "" if (!defined($string)); 4427d2367afSJoe Perches $string =~ s@^\s*\(\s*@@g; 4437d2367afSJoe Perches $string =~ s@\s*\)\s*$@@g; 4447d2367afSJoe Perches $string =~ s@\s+@ @g; 4457d2367afSJoe Perches return $string; 4467d2367afSJoe Perches} 4477d2367afSJoe Perches 4483445686aSJoe Perchessub seed_camelcase_file { 4493445686aSJoe Perches my ($file) = @_; 4503445686aSJoe Perches 4513445686aSJoe Perches return if (!(-f $file)); 4523445686aSJoe Perches 4533445686aSJoe Perches local $/; 4543445686aSJoe Perches 4553445686aSJoe Perches open(my $include_file, '<', "$file") 4563445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 4573445686aSJoe Perches my $text = <$include_file>; 4583445686aSJoe Perches close($include_file); 4593445686aSJoe Perches 4603445686aSJoe Perches my @lines = split('\n', $text); 4613445686aSJoe Perches 4623445686aSJoe Perches foreach my $line (@lines) { 4633445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 4643445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 4653445686aSJoe Perches $camelcase{$1} = 1; 46611ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 46711ea516aSJoe Perches $camelcase{$1} = 1; 46811ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 4693445686aSJoe Perches $camelcase{$1} = 1; 4703445686aSJoe Perches } 4713445686aSJoe Perches } 4723445686aSJoe Perches} 4733445686aSJoe Perches 4743445686aSJoe Perchesmy $camelcase_seeded = 0; 4753445686aSJoe Perchessub seed_camelcase_includes { 4763445686aSJoe Perches return if ($camelcase_seeded); 4773445686aSJoe Perches 4783445686aSJoe Perches my $files; 479c707a81dSJoe Perches my $camelcase_cache = ""; 480c707a81dSJoe Perches my @include_files = (); 481c707a81dSJoe Perches 482c707a81dSJoe Perches $camelcase_seeded = 1; 483351b2a1fSJoe Perches 4843645e328SRichard Genoud if (-e ".git") { 485351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 486351b2a1fSJoe Perches chomp $git_last_include_commit; 487c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 488c707a81dSJoe Perches } else { 489c707a81dSJoe Perches my $last_mod_date = 0; 490c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 491c707a81dSJoe Perches @include_files = split('\n', $files); 492c707a81dSJoe Perches foreach my $file (@include_files) { 493c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 494c707a81dSJoe Perches localtime((stat $file)[9])); 495c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 496c707a81dSJoe Perches } 497c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 498c707a81dSJoe Perches } 499c707a81dSJoe Perches 500c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 501c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 502c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 503351b2a1fSJoe Perches while (<$camelcase_file>) { 504351b2a1fSJoe Perches chomp; 505351b2a1fSJoe Perches $camelcase{$_} = 1; 506351b2a1fSJoe Perches } 507351b2a1fSJoe Perches close($camelcase_file); 508351b2a1fSJoe Perches 509351b2a1fSJoe Perches return; 510351b2a1fSJoe Perches } 511c707a81dSJoe Perches 5123645e328SRichard Genoud if (-e ".git") { 513c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 514c707a81dSJoe Perches @include_files = split('\n', $files); 5153445686aSJoe Perches } 516c707a81dSJoe Perches 5173445686aSJoe Perches foreach my $file (@include_files) { 5183445686aSJoe Perches seed_camelcase_file($file); 5193445686aSJoe Perches } 520351b2a1fSJoe Perches 521c707a81dSJoe Perches if ($camelcase_cache ne "") { 522351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 523c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 524c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 525351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 526351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 527351b2a1fSJoe Perches } 528351b2a1fSJoe Perches close($camelcase_file); 529351b2a1fSJoe Perches } 5303445686aSJoe Perches} 5313445686aSJoe Perches 5326c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 5330a920b5bSAndy Whitcroft 53400df344fSAndy Whitcroftmy @rawlines = (); 535c2fdda0dSAndy Whitcroftmy @lines = (); 5363705ce5bSJoe Perchesmy @fixed = (); 537c2fdda0dSAndy Whitcroftmy $vname; 5386c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 53921caa13cSAndy Whitcroft my $FILE; 5406c72ffaaSAndy Whitcroft if ($file) { 54121caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 5426c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 54321caa13cSAndy Whitcroft } elsif ($filename eq '-') { 54421caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 5456c72ffaaSAndy Whitcroft } else { 54621caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 5476c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 5486c72ffaaSAndy Whitcroft } 549c2fdda0dSAndy Whitcroft if ($filename eq '-') { 550c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 551c2fdda0dSAndy Whitcroft } else { 552c2fdda0dSAndy Whitcroft $vname = $filename; 553c2fdda0dSAndy Whitcroft } 55421caa13cSAndy Whitcroft while (<$FILE>) { 5550a920b5bSAndy Whitcroft chomp; 55600df344fSAndy Whitcroft push(@rawlines, $_); 5576c72ffaaSAndy Whitcroft } 55821caa13cSAndy Whitcroft close($FILE); 559c2fdda0dSAndy Whitcroft if (!process($filename)) { 5600a920b5bSAndy Whitcroft $exit = 1; 5610a920b5bSAndy Whitcroft } 56200df344fSAndy Whitcroft @rawlines = (); 56313214adfSAndy Whitcroft @lines = (); 5643705ce5bSJoe Perches @fixed = (); 5650a920b5bSAndy Whitcroft} 5660a920b5bSAndy Whitcroft 5670a920b5bSAndy Whitcroftexit($exit); 5680a920b5bSAndy Whitcroft 5690a920b5bSAndy Whitcroftsub top_of_kernel_tree { 5706c72ffaaSAndy Whitcroft my ($root) = @_; 5716c72ffaaSAndy Whitcroft 5726c72ffaaSAndy Whitcroft my @tree_check = ( 5736c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 5746c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 5756c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 5766c72ffaaSAndy Whitcroft ); 5776c72ffaaSAndy Whitcroft 5786c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 5796c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 5800a920b5bSAndy Whitcroft return 0; 5810a920b5bSAndy Whitcroft } 5826c72ffaaSAndy Whitcroft } 5836c72ffaaSAndy Whitcroft return 1; 5846c72ffaaSAndy Whitcroft} 5850a920b5bSAndy Whitcroft 58620112475SJoe Perchessub parse_email { 58720112475SJoe Perches my ($formatted_email) = @_; 58820112475SJoe Perches 58920112475SJoe Perches my $name = ""; 59020112475SJoe Perches my $address = ""; 59120112475SJoe Perches my $comment = ""; 59220112475SJoe Perches 59320112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 59420112475SJoe Perches $name = $1; 59520112475SJoe Perches $address = $2; 59620112475SJoe Perches $comment = $3 if defined $3; 59720112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 59820112475SJoe Perches $address = $1; 59920112475SJoe Perches $comment = $2 if defined $2; 60020112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 60120112475SJoe Perches $address = $1; 60220112475SJoe Perches $comment = $2 if defined $2; 60320112475SJoe Perches $formatted_email =~ s/$address.*$//; 60420112475SJoe Perches $name = $formatted_email; 6053705ce5bSJoe Perches $name = trim($name); 60620112475SJoe Perches $name =~ s/^\"|\"$//g; 60720112475SJoe Perches # If there's a name left after stripping spaces and 60820112475SJoe Perches # leading quotes, and the address doesn't have both 60920112475SJoe Perches # leading and trailing angle brackets, the address 61020112475SJoe Perches # is invalid. ie: 61120112475SJoe Perches # "joe smith [email protected]" bad 61220112475SJoe Perches # "joe smith <[email protected]" bad 61320112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 61420112475SJoe Perches $name = ""; 61520112475SJoe Perches $address = ""; 61620112475SJoe Perches $comment = ""; 61720112475SJoe Perches } 61820112475SJoe Perches } 61920112475SJoe Perches 6203705ce5bSJoe Perches $name = trim($name); 62120112475SJoe Perches $name =~ s/^\"|\"$//g; 6223705ce5bSJoe Perches $address = trim($address); 62320112475SJoe Perches $address =~ s/^\<|\>$//g; 62420112475SJoe Perches 62520112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 62620112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 62720112475SJoe Perches $name = "\"$name\""; 62820112475SJoe Perches } 62920112475SJoe Perches 63020112475SJoe Perches return ($name, $address, $comment); 63120112475SJoe Perches} 63220112475SJoe Perches 63320112475SJoe Perchessub format_email { 63420112475SJoe Perches my ($name, $address) = @_; 63520112475SJoe Perches 63620112475SJoe Perches my $formatted_email; 63720112475SJoe Perches 6383705ce5bSJoe Perches $name = trim($name); 63920112475SJoe Perches $name =~ s/^\"|\"$//g; 6403705ce5bSJoe Perches $address = trim($address); 64120112475SJoe Perches 64220112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 64320112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 64420112475SJoe Perches $name = "\"$name\""; 64520112475SJoe Perches } 64620112475SJoe Perches 64720112475SJoe Perches if ("$name" eq "") { 64820112475SJoe Perches $formatted_email = "$address"; 64920112475SJoe Perches } else { 65020112475SJoe Perches $formatted_email = "$name <$address>"; 65120112475SJoe Perches } 65220112475SJoe Perches 65320112475SJoe Perches return $formatted_email; 65420112475SJoe Perches} 65520112475SJoe Perches 656000d1cc1SJoe Perchessub which_conf { 657000d1cc1SJoe Perches my ($conf) = @_; 658000d1cc1SJoe Perches 659000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 660000d1cc1SJoe Perches if (-e "$path/$conf") { 661000d1cc1SJoe Perches return "$path/$conf"; 662000d1cc1SJoe Perches } 663000d1cc1SJoe Perches } 664000d1cc1SJoe Perches 665000d1cc1SJoe Perches return ""; 666000d1cc1SJoe Perches} 667000d1cc1SJoe Perches 6680a920b5bSAndy Whitcroftsub expand_tabs { 6690a920b5bSAndy Whitcroft my ($str) = @_; 6700a920b5bSAndy Whitcroft 6710a920b5bSAndy Whitcroft my $res = ''; 6720a920b5bSAndy Whitcroft my $n = 0; 6730a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 6740a920b5bSAndy Whitcroft if ($c eq "\t") { 6750a920b5bSAndy Whitcroft $res .= ' '; 6760a920b5bSAndy Whitcroft $n++; 6770a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 6780a920b5bSAndy Whitcroft $res .= ' '; 6790a920b5bSAndy Whitcroft } 6800a920b5bSAndy Whitcroft next; 6810a920b5bSAndy Whitcroft } 6820a920b5bSAndy Whitcroft $res .= $c; 6830a920b5bSAndy Whitcroft $n++; 6840a920b5bSAndy Whitcroft } 6850a920b5bSAndy Whitcroft 6860a920b5bSAndy Whitcroft return $res; 6870a920b5bSAndy Whitcroft} 6886c72ffaaSAndy Whitcroftsub copy_spacing { 689773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 6906c72ffaaSAndy Whitcroft return $res; 6916c72ffaaSAndy Whitcroft} 6920a920b5bSAndy Whitcroft 6934a0df2efSAndy Whitcroftsub line_stats { 6944a0df2efSAndy Whitcroft my ($line) = @_; 6954a0df2efSAndy Whitcroft 6964a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 6974a0df2efSAndy Whitcroft $line =~ s/^.//; 6984a0df2efSAndy Whitcroft $line = expand_tabs($line); 6994a0df2efSAndy Whitcroft 7004a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 7014a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 7024a0df2efSAndy Whitcroft 7034a0df2efSAndy Whitcroft return (length($line), length($white)); 7044a0df2efSAndy Whitcroft} 7054a0df2efSAndy Whitcroft 706773647a0SAndy Whitcroftmy $sanitise_quote = ''; 707773647a0SAndy Whitcroft 708773647a0SAndy Whitcroftsub sanitise_line_reset { 709773647a0SAndy Whitcroft my ($in_comment) = @_; 710773647a0SAndy Whitcroft 711773647a0SAndy Whitcroft if ($in_comment) { 712773647a0SAndy Whitcroft $sanitise_quote = '*/'; 713773647a0SAndy Whitcroft } else { 714773647a0SAndy Whitcroft $sanitise_quote = ''; 715773647a0SAndy Whitcroft } 716773647a0SAndy Whitcroft} 71700df344fSAndy Whitcroftsub sanitise_line { 71800df344fSAndy Whitcroft my ($line) = @_; 71900df344fSAndy Whitcroft 72000df344fSAndy Whitcroft my $res = ''; 72100df344fSAndy Whitcroft my $l = ''; 72200df344fSAndy Whitcroft 723c2fdda0dSAndy Whitcroft my $qlen = 0; 724773647a0SAndy Whitcroft my $off = 0; 725773647a0SAndy Whitcroft my $c; 72600df344fSAndy Whitcroft 727773647a0SAndy Whitcroft # Always copy over the diff marker. 728773647a0SAndy Whitcroft $res = substr($line, 0, 1); 729773647a0SAndy Whitcroft 730773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 731773647a0SAndy Whitcroft $c = substr($line, $off, 1); 732773647a0SAndy Whitcroft 733773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 734773647a0SAndy Whitcroft # and end, all to $;. 735773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 736773647a0SAndy Whitcroft $sanitise_quote = '*/'; 737773647a0SAndy Whitcroft 738773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 739773647a0SAndy Whitcroft $off++; 74000df344fSAndy Whitcroft next; 741773647a0SAndy Whitcroft } 74281bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 743773647a0SAndy Whitcroft $sanitise_quote = ''; 744773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 745773647a0SAndy Whitcroft $off++; 746773647a0SAndy Whitcroft next; 747773647a0SAndy Whitcroft } 748113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 749113f04a8SDaniel Walker $sanitise_quote = '//'; 750113f04a8SDaniel Walker 751113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 752113f04a8SDaniel Walker $off++; 753113f04a8SDaniel Walker next; 754113f04a8SDaniel Walker } 755773647a0SAndy Whitcroft 756773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 757773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 758773647a0SAndy Whitcroft $c eq "\\") { 759773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 760773647a0SAndy Whitcroft $off++; 761773647a0SAndy Whitcroft next; 762773647a0SAndy Whitcroft } 763773647a0SAndy Whitcroft # Regular quotes. 764773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 765773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 766773647a0SAndy Whitcroft $sanitise_quote = $c; 767773647a0SAndy Whitcroft 768773647a0SAndy Whitcroft substr($res, $off, 1, $c); 769773647a0SAndy Whitcroft next; 770773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 771773647a0SAndy Whitcroft $sanitise_quote = ''; 77200df344fSAndy Whitcroft } 77300df344fSAndy Whitcroft } 774773647a0SAndy Whitcroft 775fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 776773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 777773647a0SAndy Whitcroft substr($res, $off, 1, $;); 778113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 779113f04a8SDaniel Walker substr($res, $off, 1, $;); 780773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 781773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 78200df344fSAndy Whitcroft } else { 783773647a0SAndy Whitcroft substr($res, $off, 1, $c); 78400df344fSAndy Whitcroft } 785c2fdda0dSAndy Whitcroft } 786c2fdda0dSAndy Whitcroft 787113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 788113f04a8SDaniel Walker $sanitise_quote = ''; 789113f04a8SDaniel Walker } 790113f04a8SDaniel Walker 791c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 792c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 793c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 794c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 795c2fdda0dSAndy Whitcroft 796c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 797c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 798c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 799c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 800c2fdda0dSAndy Whitcroft } 801c2fdda0dSAndy Whitcroft 80200df344fSAndy Whitcroft return $res; 80300df344fSAndy Whitcroft} 80400df344fSAndy Whitcroft 805a6962d72SJoe Perchessub get_quoted_string { 806a6962d72SJoe Perches my ($line, $rawline) = @_; 807a6962d72SJoe Perches 808a6962d72SJoe Perches return "" if ($line !~ m/(\"[X]+\")/g); 809a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 810a6962d72SJoe Perches} 811a6962d72SJoe Perches 8128905a67cSAndy Whitcroftsub ctx_statement_block { 8138905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 8148905a67cSAndy Whitcroft my $line = $linenr - 1; 8158905a67cSAndy Whitcroft my $blk = ''; 8168905a67cSAndy Whitcroft my $soff = $off; 8178905a67cSAndy Whitcroft my $coff = $off - 1; 818773647a0SAndy Whitcroft my $coff_set = 0; 8198905a67cSAndy Whitcroft 82013214adfSAndy Whitcroft my $loff = 0; 82113214adfSAndy Whitcroft 8228905a67cSAndy Whitcroft my $type = ''; 8238905a67cSAndy Whitcroft my $level = 0; 824a2750645SAndy Whitcroft my @stack = (); 825cf655043SAndy Whitcroft my $p; 8268905a67cSAndy Whitcroft my $c; 8278905a67cSAndy Whitcroft my $len = 0; 82813214adfSAndy Whitcroft 82913214adfSAndy Whitcroft my $remainder; 8308905a67cSAndy Whitcroft while (1) { 831a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 832a2750645SAndy Whitcroft 833773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 8348905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 8358905a67cSAndy Whitcroft # context. 8368905a67cSAndy Whitcroft if ($off >= $len) { 8378905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 838dea33496SAndy Whitcroft last if (!defined $lines[$line]); 839c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 8408905a67cSAndy Whitcroft $remain--; 84113214adfSAndy Whitcroft $loff = $len; 842c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 8438905a67cSAndy Whitcroft $len = length($blk); 8448905a67cSAndy Whitcroft $line++; 8458905a67cSAndy Whitcroft last; 8468905a67cSAndy Whitcroft } 8478905a67cSAndy Whitcroft # Bail if there is no further context. 8488905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 84913214adfSAndy Whitcroft if ($off >= $len) { 8508905a67cSAndy Whitcroft last; 8518905a67cSAndy Whitcroft } 852f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 853f74bd194SAndy Whitcroft $level++; 854f74bd194SAndy Whitcroft $type = '#'; 855f74bd194SAndy Whitcroft } 8568905a67cSAndy Whitcroft } 857cf655043SAndy Whitcroft $p = $c; 8588905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 85913214adfSAndy Whitcroft $remainder = substr($blk, $off); 8608905a67cSAndy Whitcroft 861773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 8624635f4fbSAndy Whitcroft 8634635f4fbSAndy Whitcroft # Handle nested #if/#else. 8644635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 8654635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 8664635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 8674635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 8684635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 8694635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 8704635f4fbSAndy Whitcroft } 8714635f4fbSAndy Whitcroft 8728905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 8738905a67cSAndy Whitcroft # outermost level. 8748905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 8758905a67cSAndy Whitcroft last; 8768905a67cSAndy Whitcroft } 8778905a67cSAndy Whitcroft 87813214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 879773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 880773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 881773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 882773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 883773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 884773647a0SAndy Whitcroft $coff_set = 1; 885773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 886773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 88713214adfSAndy Whitcroft } 88813214adfSAndy Whitcroft 8898905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 8908905a67cSAndy Whitcroft $level++; 8918905a67cSAndy Whitcroft $type = '('; 8928905a67cSAndy Whitcroft } 8938905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 8948905a67cSAndy Whitcroft $level--; 8958905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 8968905a67cSAndy Whitcroft 8978905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 8988905a67cSAndy Whitcroft $coff = $off; 899773647a0SAndy Whitcroft $coff_set = 1; 900773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 9018905a67cSAndy Whitcroft } 9028905a67cSAndy Whitcroft } 9038905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 9048905a67cSAndy Whitcroft $level++; 9058905a67cSAndy Whitcroft $type = '{'; 9068905a67cSAndy Whitcroft } 9078905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 9088905a67cSAndy Whitcroft $level--; 9098905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 9108905a67cSAndy Whitcroft 9118905a67cSAndy Whitcroft if ($level == 0) { 912b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 913b998e001SPatrick Pannuto $off++; 914b998e001SPatrick Pannuto } 9158905a67cSAndy Whitcroft last; 9168905a67cSAndy Whitcroft } 9178905a67cSAndy Whitcroft } 918f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 919f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 920f74bd194SAndy Whitcroft $level--; 921f74bd194SAndy Whitcroft $type = ''; 922f74bd194SAndy Whitcroft $off++; 923f74bd194SAndy Whitcroft last; 924f74bd194SAndy Whitcroft } 9258905a67cSAndy Whitcroft $off++; 9268905a67cSAndy Whitcroft } 927a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 92813214adfSAndy Whitcroft if ($off == $len) { 929a3bb97a7SAndy Whitcroft $loff = $len + 1; 93013214adfSAndy Whitcroft $line++; 93113214adfSAndy Whitcroft $remain--; 93213214adfSAndy Whitcroft } 9338905a67cSAndy Whitcroft 9348905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 9358905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 9368905a67cSAndy Whitcroft 9378905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 9388905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 9398905a67cSAndy Whitcroft 940773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 94113214adfSAndy Whitcroft 94213214adfSAndy Whitcroft return ($statement, $condition, 94313214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 94413214adfSAndy Whitcroft} 94513214adfSAndy Whitcroft 946cf655043SAndy Whitcroftsub statement_lines { 947cf655043SAndy Whitcroft my ($stmt) = @_; 948cf655043SAndy Whitcroft 949cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 950cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 951cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 952cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 953cf655043SAndy Whitcroft 954cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 955cf655043SAndy Whitcroft 956cf655043SAndy Whitcroft return $#stmt_lines + 2; 957cf655043SAndy Whitcroft} 958cf655043SAndy Whitcroft 959cf655043SAndy Whitcroftsub statement_rawlines { 960cf655043SAndy Whitcroft my ($stmt) = @_; 961cf655043SAndy Whitcroft 962cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 963cf655043SAndy Whitcroft 964cf655043SAndy Whitcroft return $#stmt_lines + 2; 965cf655043SAndy Whitcroft} 966cf655043SAndy Whitcroft 967cf655043SAndy Whitcroftsub statement_block_size { 968cf655043SAndy Whitcroft my ($stmt) = @_; 969cf655043SAndy Whitcroft 970cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 971cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 972cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 973cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 974cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 975cf655043SAndy Whitcroft 976cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 977cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 978cf655043SAndy Whitcroft 979cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 980cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 981cf655043SAndy Whitcroft 982cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 983cf655043SAndy Whitcroft return $stmt_lines; 984cf655043SAndy Whitcroft } else { 985cf655043SAndy Whitcroft return $stmt_statements; 986cf655043SAndy Whitcroft } 987cf655043SAndy Whitcroft} 988cf655043SAndy Whitcroft 98913214adfSAndy Whitcroftsub ctx_statement_full { 99013214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 99113214adfSAndy Whitcroft my ($statement, $condition, $level); 99213214adfSAndy Whitcroft 99313214adfSAndy Whitcroft my (@chunks); 99413214adfSAndy Whitcroft 995cf655043SAndy Whitcroft # Grab the first conditional/block pair. 99613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 99713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 998773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 99913214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1000cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1001cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1002cf655043SAndy Whitcroft } 1003cf655043SAndy Whitcroft 1004cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1005cf655043SAndy Whitcroft # could continue the statement. 1006cf655043SAndy Whitcroft for (;;) { 100713214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 100813214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1009cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1010773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1011cf655043SAndy Whitcroft #print "C: push\n"; 1012cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 101313214adfSAndy Whitcroft } 101413214adfSAndy Whitcroft 101513214adfSAndy Whitcroft return ($level, $linenr, @chunks); 10168905a67cSAndy Whitcroft} 10178905a67cSAndy Whitcroft 10184a0df2efSAndy Whitcroftsub ctx_block_get { 1019f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 10204a0df2efSAndy Whitcroft my $line; 10214a0df2efSAndy Whitcroft my $start = $linenr - 1; 10224a0df2efSAndy Whitcroft my $blk = ''; 10234a0df2efSAndy Whitcroft my @o; 10244a0df2efSAndy Whitcroft my @c; 10254a0df2efSAndy Whitcroft my @res = (); 10264a0df2efSAndy Whitcroft 1027f0a594c1SAndy Whitcroft my $level = 0; 10284635f4fbSAndy Whitcroft my @stack = ($level); 102900df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 103000df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 103100df344fSAndy Whitcroft $remain--; 103200df344fSAndy Whitcroft 103300df344fSAndy Whitcroft $blk .= $rawlines[$line]; 10344635f4fbSAndy Whitcroft 10354635f4fbSAndy Whitcroft # Handle nested #if/#else. 103601464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 10374635f4fbSAndy Whitcroft push(@stack, $level); 103801464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 10394635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 104001464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 10414635f4fbSAndy Whitcroft $level = pop(@stack); 10424635f4fbSAndy Whitcroft } 10434635f4fbSAndy Whitcroft 104401464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1045f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1046f0a594c1SAndy Whitcroft if ($off > 0) { 1047f0a594c1SAndy Whitcroft $off--; 1048f0a594c1SAndy Whitcroft next; 1049f0a594c1SAndy Whitcroft } 10504a0df2efSAndy Whitcroft 1051f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1052f0a594c1SAndy Whitcroft $level--; 1053f0a594c1SAndy Whitcroft last if ($level == 0); 1054f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1055f0a594c1SAndy Whitcroft $level++; 1056f0a594c1SAndy Whitcroft } 1057f0a594c1SAndy Whitcroft } 10584a0df2efSAndy Whitcroft 1059f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 106000df344fSAndy Whitcroft push(@res, $rawlines[$line]); 10614a0df2efSAndy Whitcroft } 10624a0df2efSAndy Whitcroft 1063f0a594c1SAndy Whitcroft last if ($level == 0); 10644a0df2efSAndy Whitcroft } 10654a0df2efSAndy Whitcroft 1066f0a594c1SAndy Whitcroft return ($level, @res); 10674a0df2efSAndy Whitcroft} 10684a0df2efSAndy Whitcroftsub ctx_block_outer { 10694a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10704a0df2efSAndy Whitcroft 1071f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1072f0a594c1SAndy Whitcroft return @r; 10734a0df2efSAndy Whitcroft} 10744a0df2efSAndy Whitcroftsub ctx_block { 10754a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10764a0df2efSAndy Whitcroft 1077f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1078f0a594c1SAndy Whitcroft return @r; 1079653d4876SAndy Whitcroft} 1080653d4876SAndy Whitcroftsub ctx_statement { 1081f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1082f0a594c1SAndy Whitcroft 1083f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1084f0a594c1SAndy Whitcroft return @r; 1085f0a594c1SAndy Whitcroft} 1086f0a594c1SAndy Whitcroftsub ctx_block_level { 1087653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1088653d4876SAndy Whitcroft 1089f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 10904a0df2efSAndy Whitcroft} 10919c0ca6f9SAndy Whitcroftsub ctx_statement_level { 10929c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 10939c0ca6f9SAndy Whitcroft 10949c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 10959c0ca6f9SAndy Whitcroft} 10964a0df2efSAndy Whitcroft 10974a0df2efSAndy Whitcroftsub ctx_locate_comment { 10984a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 10994a0df2efSAndy Whitcroft 11004a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1101beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 11024a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 11034a0df2efSAndy Whitcroft 11044a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 11054a0df2efSAndy Whitcroft # comment. 11064a0df2efSAndy Whitcroft my $in_comment = 0; 11074a0df2efSAndy Whitcroft $current_comment = ''; 11084a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 110900df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 111000df344fSAndy Whitcroft #warn " $line\n"; 11114a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 11124a0df2efSAndy Whitcroft $in_comment = 1; 11134a0df2efSAndy Whitcroft } 11144a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 11154a0df2efSAndy Whitcroft $in_comment = 1; 11164a0df2efSAndy Whitcroft } 11174a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 11184a0df2efSAndy Whitcroft $current_comment = ''; 11194a0df2efSAndy Whitcroft } 11204a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 11214a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 11224a0df2efSAndy Whitcroft $in_comment = 0; 11234a0df2efSAndy Whitcroft } 11244a0df2efSAndy Whitcroft } 11254a0df2efSAndy Whitcroft 11264a0df2efSAndy Whitcroft chomp($current_comment); 11274a0df2efSAndy Whitcroft return($current_comment); 11284a0df2efSAndy Whitcroft} 11294a0df2efSAndy Whitcroftsub ctx_has_comment { 11304a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 11314a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 11324a0df2efSAndy Whitcroft 113300df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 11344a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 11354a0df2efSAndy Whitcroft 11364a0df2efSAndy Whitcroft return ($cmt ne ''); 11374a0df2efSAndy Whitcroft} 11384a0df2efSAndy Whitcroft 11394d001e4dSAndy Whitcroftsub raw_line { 11404d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 11414d001e4dSAndy Whitcroft 11424d001e4dSAndy Whitcroft my $offset = $linenr - 1; 11434d001e4dSAndy Whitcroft $cnt++; 11444d001e4dSAndy Whitcroft 11454d001e4dSAndy Whitcroft my $line; 11464d001e4dSAndy Whitcroft while ($cnt) { 11474d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 11484d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 11494d001e4dSAndy Whitcroft $cnt--; 11504d001e4dSAndy Whitcroft } 11514d001e4dSAndy Whitcroft 11524d001e4dSAndy Whitcroft return $line; 11534d001e4dSAndy Whitcroft} 11544d001e4dSAndy Whitcroft 11550a920b5bSAndy Whitcroftsub cat_vet { 11560a920b5bSAndy Whitcroft my ($vet) = @_; 11579c0ca6f9SAndy Whitcroft my ($res, $coded); 11580a920b5bSAndy Whitcroft 11599c0ca6f9SAndy Whitcroft $res = ''; 11606c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 11616c72ffaaSAndy Whitcroft $res .= $1; 11626c72ffaaSAndy Whitcroft if ($2 ne '') { 11639c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 11646c72ffaaSAndy Whitcroft $res .= $coded; 11656c72ffaaSAndy Whitcroft } 11669c0ca6f9SAndy Whitcroft } 11679c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 11680a920b5bSAndy Whitcroft 11699c0ca6f9SAndy Whitcroft return $res; 11700a920b5bSAndy Whitcroft} 11710a920b5bSAndy Whitcroft 1172c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1173cf655043SAndy Whitcroftmy $av_pending; 1174c2fdda0dSAndy Whitcroftmy @av_paren_type; 11751f65f947SAndy Whitcroftmy $av_pend_colon; 1176c2fdda0dSAndy Whitcroft 1177c2fdda0dSAndy Whitcroftsub annotate_reset { 1178c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1179cf655043SAndy Whitcroft $av_pending = '_'; 1180cf655043SAndy Whitcroft @av_paren_type = ('E'); 11811f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1182c2fdda0dSAndy Whitcroft} 1183c2fdda0dSAndy Whitcroft 11846c72ffaaSAndy Whitcroftsub annotate_values { 11856c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 11866c72ffaaSAndy Whitcroft 11876c72ffaaSAndy Whitcroft my $res; 11881f65f947SAndy Whitcroft my $var = '_' x length($stream); 11896c72ffaaSAndy Whitcroft my $cur = $stream; 11906c72ffaaSAndy Whitcroft 1191c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 11926c72ffaaSAndy Whitcroft 11936c72ffaaSAndy Whitcroft while (length($cur)) { 1194773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1195cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1196171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 11976c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1198c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1199c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1200cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1201c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 12026c72ffaaSAndy Whitcroft } 12036c72ffaaSAndy Whitcroft 1204c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 12059446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 12069446ef56SAndy Whitcroft push(@av_paren_type, $type); 1207addcdceaSAndy Whitcroft $type = 'c'; 12089446ef56SAndy Whitcroft 1209e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1210c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 12116c72ffaaSAndy Whitcroft $type = 'T'; 12126c72ffaaSAndy Whitcroft 1213389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1214389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1215389a2fe5SAndy Whitcroft $type = 'T'; 1216389a2fe5SAndy Whitcroft 1217c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1218171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1219c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1220171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1221171ae1a4SAndy Whitcroft if ($2 ne '') { 1222cf655043SAndy Whitcroft $av_pending = 'N'; 1223171ae1a4SAndy Whitcroft } 1224171ae1a4SAndy Whitcroft $type = 'E'; 1225171ae1a4SAndy Whitcroft 1226c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1227171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1228171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1229171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 12306c72ffaaSAndy Whitcroft 1231c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1232cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1233c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1234cf655043SAndy Whitcroft 1235cf655043SAndy Whitcroft push(@av_paren_type, $type); 1236cf655043SAndy Whitcroft push(@av_paren_type, $type); 1237171ae1a4SAndy Whitcroft $type = 'E'; 1238cf655043SAndy Whitcroft 1239c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1240cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1241cf655043SAndy Whitcroft $av_preprocessor = 1; 1242cf655043SAndy Whitcroft 1243cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1244cf655043SAndy Whitcroft 1245171ae1a4SAndy Whitcroft $type = 'E'; 1246cf655043SAndy Whitcroft 1247c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1248cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1249cf655043SAndy Whitcroft 1250cf655043SAndy Whitcroft $av_preprocessor = 1; 1251cf655043SAndy Whitcroft 1252cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1253cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1254cf655043SAndy Whitcroft pop(@av_paren_type); 1255cf655043SAndy Whitcroft push(@av_paren_type, $type); 1256171ae1a4SAndy Whitcroft $type = 'E'; 12576c72ffaaSAndy Whitcroft 12586c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1259c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 12606c72ffaaSAndy Whitcroft 1261171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1262171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1263171ae1a4SAndy Whitcroft $av_pending = $type; 1264171ae1a4SAndy Whitcroft $type = 'N'; 1265171ae1a4SAndy Whitcroft 12666c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1267c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 12686c72ffaaSAndy Whitcroft if (defined $2) { 1269cf655043SAndy Whitcroft $av_pending = 'V'; 12706c72ffaaSAndy Whitcroft } 12716c72ffaaSAndy Whitcroft $type = 'N'; 12726c72ffaaSAndy Whitcroft 127314b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1274c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 127514b111c1SAndy Whitcroft $av_pending = 'E'; 12766c72ffaaSAndy Whitcroft $type = 'N'; 12776c72ffaaSAndy Whitcroft 12781f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 12791f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 12801f65f947SAndy Whitcroft $av_pend_colon = 'C'; 12811f65f947SAndy Whitcroft $type = 'N'; 12821f65f947SAndy Whitcroft 128314b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1284c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 12856c72ffaaSAndy Whitcroft $type = 'N'; 12866c72ffaaSAndy Whitcroft 12876c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1288c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1289cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1290cf655043SAndy Whitcroft $av_pending = '_'; 12916c72ffaaSAndy Whitcroft $type = 'N'; 12926c72ffaaSAndy Whitcroft 12936c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1294cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1295cf655043SAndy Whitcroft if ($new_type ne '_') { 1296cf655043SAndy Whitcroft $type = $new_type; 1297c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1298c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 12996c72ffaaSAndy Whitcroft } else { 1300c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 13016c72ffaaSAndy Whitcroft } 13026c72ffaaSAndy Whitcroft 1303c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1304c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1305c8cb2ca3SAndy Whitcroft $type = 'V'; 1306cf655043SAndy Whitcroft $av_pending = 'V'; 13076c72ffaaSAndy Whitcroft 13088e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 13098e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 13101f65f947SAndy Whitcroft $av_pend_colon = 'B'; 13118e761b04SAndy Whitcroft } elsif ($type eq 'E') { 13128e761b04SAndy Whitcroft $av_pend_colon = 'L'; 13131f65f947SAndy Whitcroft } 13141f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 13151f65f947SAndy Whitcroft $type = 'V'; 13161f65f947SAndy Whitcroft 13176c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1318c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 13196c72ffaaSAndy Whitcroft $type = 'V'; 13206c72ffaaSAndy Whitcroft 13216c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1322c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 13236c72ffaaSAndy Whitcroft $type = 'N'; 13246c72ffaaSAndy Whitcroft 1325cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1326c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 132713214adfSAndy Whitcroft $type = 'E'; 13281f65f947SAndy Whitcroft $av_pend_colon = 'O'; 132913214adfSAndy Whitcroft 13308e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 13318e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 13328e761b04SAndy Whitcroft $type = 'C'; 13338e761b04SAndy Whitcroft 13341f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 13351f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 13361f65f947SAndy Whitcroft $type = 'N'; 13371f65f947SAndy Whitcroft 13381f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 13391f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 13401f65f947SAndy Whitcroft 13411f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 13421f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 13431f65f947SAndy Whitcroft $type = 'E'; 13441f65f947SAndy Whitcroft } else { 13451f65f947SAndy Whitcroft $type = 'N'; 13461f65f947SAndy Whitcroft } 13471f65f947SAndy Whitcroft $av_pend_colon = 'O'; 13481f65f947SAndy Whitcroft 13498e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 135013214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 13516c72ffaaSAndy Whitcroft $type = 'N'; 13526c72ffaaSAndy Whitcroft 13530d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 135474048ed8SAndy Whitcroft my $variant; 135574048ed8SAndy Whitcroft 135674048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 135774048ed8SAndy Whitcroft if ($type eq 'V') { 135874048ed8SAndy Whitcroft $variant = 'B'; 135974048ed8SAndy Whitcroft } else { 136074048ed8SAndy Whitcroft $variant = 'U'; 136174048ed8SAndy Whitcroft } 136274048ed8SAndy Whitcroft 136374048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 136474048ed8SAndy Whitcroft $type = 'N'; 136574048ed8SAndy Whitcroft 13666c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1367c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 13686c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 13696c72ffaaSAndy Whitcroft $type = 'N'; 13706c72ffaaSAndy Whitcroft } 13716c72ffaaSAndy Whitcroft 13726c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1373c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 13746c72ffaaSAndy Whitcroft } 13756c72ffaaSAndy Whitcroft if (defined $1) { 13766c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 13776c72ffaaSAndy Whitcroft $res .= $type x length($1); 13786c72ffaaSAndy Whitcroft } 13796c72ffaaSAndy Whitcroft } 13806c72ffaaSAndy Whitcroft 13811f65f947SAndy Whitcroft return ($res, $var); 13826c72ffaaSAndy Whitcroft} 13836c72ffaaSAndy Whitcroft 13848905a67cSAndy Whitcroftsub possible { 138513214adfSAndy Whitcroft my ($possible, $line) = @_; 13869a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 13870776e594SAndy Whitcroft ^(?: 13880776e594SAndy Whitcroft $Modifier| 13890776e594SAndy Whitcroft $Storage| 13900776e594SAndy Whitcroft $Type| 13919a974fdbSAndy Whitcroft DEFINE_\S+ 13929a974fdbSAndy Whitcroft )$| 13939a974fdbSAndy Whitcroft ^(?: 13940776e594SAndy Whitcroft goto| 13950776e594SAndy Whitcroft return| 13960776e594SAndy Whitcroft case| 13970776e594SAndy Whitcroft else| 13980776e594SAndy Whitcroft asm|__asm__| 139989a88353SAndy Whitcroft do| 140089a88353SAndy Whitcroft \#| 140189a88353SAndy Whitcroft \#\#| 14029a974fdbSAndy Whitcroft )(?:\s|$)| 14030776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 14049a974fdbSAndy Whitcroft )}x; 14059a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 14069a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1407c45dcabdSAndy Whitcroft # Check for modifiers. 1408c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1409c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1410c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1411c45dcabdSAndy Whitcroft 1412c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1413c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1414d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 14159a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1416d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1417d2506586SAndy Whitcroft push(@modifierList, $modifier); 1418d2506586SAndy Whitcroft } 14199a974fdbSAndy Whitcroft } 1420c45dcabdSAndy Whitcroft 1421c45dcabdSAndy Whitcroft } else { 142213214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 14238905a67cSAndy Whitcroft push(@typeList, $possible); 1424c45dcabdSAndy Whitcroft } 14258905a67cSAndy Whitcroft build_types(); 14260776e594SAndy Whitcroft } else { 14270776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 14288905a67cSAndy Whitcroft } 14298905a67cSAndy Whitcroft} 14308905a67cSAndy Whitcroft 14316c72ffaaSAndy Whitcroftmy $prefix = ''; 14326c72ffaaSAndy Whitcroft 1433000d1cc1SJoe Perchessub show_type { 143491bfe484SJoe Perches return defined $use_type{$_[0]} if (scalar keys %use_type > 0); 143591bfe484SJoe Perches 1436000d1cc1SJoe Perches return !defined $ignore_type{$_[0]}; 1437000d1cc1SJoe Perches} 1438000d1cc1SJoe Perches 1439f0a594c1SAndy Whitcroftsub report { 1440000d1cc1SJoe Perches if (!show_type($_[1]) || 1441000d1cc1SJoe Perches (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { 1442773647a0SAndy Whitcroft return 0; 1443773647a0SAndy Whitcroft } 1444000d1cc1SJoe Perches my $line; 1445000d1cc1SJoe Perches if ($show_types) { 1446000d1cc1SJoe Perches $line = "$prefix$_[0]:$_[1]: $_[2]\n"; 1447000d1cc1SJoe Perches } else { 1448000d1cc1SJoe Perches $line = "$prefix$_[0]: $_[2]\n"; 1449000d1cc1SJoe Perches } 14508905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 14518905a67cSAndy Whitcroft 145213214adfSAndy Whitcroft push(our @report, $line); 1453773647a0SAndy Whitcroft 1454773647a0SAndy Whitcroft return 1; 1455f0a594c1SAndy Whitcroft} 1456f0a594c1SAndy Whitcroftsub report_dump { 145713214adfSAndy Whitcroft our @report; 1458f0a594c1SAndy Whitcroft} 1459000d1cc1SJoe Perches 1460de7d4f0eSAndy Whitcroftsub ERROR { 1461000d1cc1SJoe Perches if (report("ERROR", $_[0], $_[1])) { 1462de7d4f0eSAndy Whitcroft our $clean = 0; 14636c72ffaaSAndy Whitcroft our $cnt_error++; 14643705ce5bSJoe Perches return 1; 1465de7d4f0eSAndy Whitcroft } 14663705ce5bSJoe Perches return 0; 1467773647a0SAndy Whitcroft} 1468de7d4f0eSAndy Whitcroftsub WARN { 1469000d1cc1SJoe Perches if (report("WARNING", $_[0], $_[1])) { 1470de7d4f0eSAndy Whitcroft our $clean = 0; 14716c72ffaaSAndy Whitcroft our $cnt_warn++; 14723705ce5bSJoe Perches return 1; 1473de7d4f0eSAndy Whitcroft } 14743705ce5bSJoe Perches return 0; 1475773647a0SAndy Whitcroft} 1476de7d4f0eSAndy Whitcroftsub CHK { 1477000d1cc1SJoe Perches if ($check && report("CHECK", $_[0], $_[1])) { 1478de7d4f0eSAndy Whitcroft our $clean = 0; 14796c72ffaaSAndy Whitcroft our $cnt_chk++; 14803705ce5bSJoe Perches return 1; 14816c72ffaaSAndy Whitcroft } 14823705ce5bSJoe Perches return 0; 1483de7d4f0eSAndy Whitcroft} 1484de7d4f0eSAndy Whitcroft 14856ecd9674SAndy Whitcroftsub check_absolute_file { 14866ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 14876ecd9674SAndy Whitcroft my $file = $absolute; 14886ecd9674SAndy Whitcroft 14896ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 14906ecd9674SAndy Whitcroft 14916ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 14926ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 14936ecd9674SAndy Whitcroft if (-f "$root/$file") { 14946ecd9674SAndy Whitcroft ##print "file<$file>\n"; 14956ecd9674SAndy Whitcroft last; 14966ecd9674SAndy Whitcroft } 14976ecd9674SAndy Whitcroft } 14986ecd9674SAndy Whitcroft if (! -f _) { 14996ecd9674SAndy Whitcroft return 0; 15006ecd9674SAndy Whitcroft } 15016ecd9674SAndy Whitcroft 15026ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 15036ecd9674SAndy Whitcroft my $prefix = $absolute; 15046ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 15056ecd9674SAndy Whitcroft 15066ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 15076ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1508000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1509000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 15106ecd9674SAndy Whitcroft } 15116ecd9674SAndy Whitcroft} 15126ecd9674SAndy Whitcroft 15133705ce5bSJoe Perchessub trim { 15143705ce5bSJoe Perches my ($string) = @_; 15153705ce5bSJoe Perches 1516b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1517b34c648bSJoe Perches 1518b34c648bSJoe Perches return $string; 1519b34c648bSJoe Perches} 1520b34c648bSJoe Perches 1521b34c648bSJoe Perchessub ltrim { 1522b34c648bSJoe Perches my ($string) = @_; 1523b34c648bSJoe Perches 1524b34c648bSJoe Perches $string =~ s/^\s+//; 1525b34c648bSJoe Perches 1526b34c648bSJoe Perches return $string; 1527b34c648bSJoe Perches} 1528b34c648bSJoe Perches 1529b34c648bSJoe Perchessub rtrim { 1530b34c648bSJoe Perches my ($string) = @_; 1531b34c648bSJoe Perches 1532b34c648bSJoe Perches $string =~ s/\s+$//; 15333705ce5bSJoe Perches 15343705ce5bSJoe Perches return $string; 15353705ce5bSJoe Perches} 15363705ce5bSJoe Perches 153752ea8506SJoe Perchessub string_find_replace { 153852ea8506SJoe Perches my ($string, $find, $replace) = @_; 153952ea8506SJoe Perches 154052ea8506SJoe Perches $string =~ s/$find/$replace/g; 154152ea8506SJoe Perches 154252ea8506SJoe Perches return $string; 154352ea8506SJoe Perches} 154452ea8506SJoe Perches 15453705ce5bSJoe Perchessub tabify { 15463705ce5bSJoe Perches my ($leading) = @_; 15473705ce5bSJoe Perches 15483705ce5bSJoe Perches my $source_indent = 8; 15493705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 15503705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 15513705ce5bSJoe Perches 15523705ce5bSJoe Perches #convert leading spaces to tabs 15533705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 15543705ce5bSJoe Perches #Remove spaces before a tab 15553705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 15563705ce5bSJoe Perches 15573705ce5bSJoe Perches return "$leading"; 15583705ce5bSJoe Perches} 15593705ce5bSJoe Perches 1560d1fe9c09SJoe Perchessub pos_last_openparen { 1561d1fe9c09SJoe Perches my ($line) = @_; 1562d1fe9c09SJoe Perches 1563d1fe9c09SJoe Perches my $pos = 0; 1564d1fe9c09SJoe Perches 1565d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1566d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1567d1fe9c09SJoe Perches 1568d1fe9c09SJoe Perches my $last_openparen = 0; 1569d1fe9c09SJoe Perches 1570d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1571d1fe9c09SJoe Perches return -1; 1572d1fe9c09SJoe Perches } 1573d1fe9c09SJoe Perches 1574d1fe9c09SJoe Perches my $len = length($line); 1575d1fe9c09SJoe Perches 1576d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1577d1fe9c09SJoe Perches my $string = substr($line, $pos); 1578d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1579d1fe9c09SJoe Perches $pos += length($1) - 1; 1580d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1581d1fe9c09SJoe Perches $last_openparen = $pos; 1582d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1583d1fe9c09SJoe Perches last; 1584d1fe9c09SJoe Perches } 1585d1fe9c09SJoe Perches } 1586d1fe9c09SJoe Perches 1587d1fe9c09SJoe Perches return $last_openparen + 1; 1588d1fe9c09SJoe Perches} 1589d1fe9c09SJoe Perches 15900a920b5bSAndy Whitcroftsub process { 15910a920b5bSAndy Whitcroft my $filename = shift; 15920a920b5bSAndy Whitcroft 15930a920b5bSAndy Whitcroft my $linenr=0; 15940a920b5bSAndy Whitcroft my $prevline=""; 1595c2fdda0dSAndy Whitcroft my $prevrawline=""; 15960a920b5bSAndy Whitcroft my $stashline=""; 1597c2fdda0dSAndy Whitcroft my $stashrawline=""; 15980a920b5bSAndy Whitcroft 15994a0df2efSAndy Whitcroft my $length; 16000a920b5bSAndy Whitcroft my $indent; 16010a920b5bSAndy Whitcroft my $previndent=0; 16020a920b5bSAndy Whitcroft my $stashindent=0; 16030a920b5bSAndy Whitcroft 1604de7d4f0eSAndy Whitcroft our $clean = 1; 16050a920b5bSAndy Whitcroft my $signoff = 0; 16060a920b5bSAndy Whitcroft my $is_patch = 0; 16070a920b5bSAndy Whitcroft 160815662b3eSJoe Perches my $in_header_lines = 1; 160915662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 161015662b3eSJoe Perches 1611fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1612fa64205dSPasi Savanainen 161313214adfSAndy Whitcroft our @report = (); 16146c72ffaaSAndy Whitcroft our $cnt_lines = 0; 16156c72ffaaSAndy Whitcroft our $cnt_error = 0; 16166c72ffaaSAndy Whitcroft our $cnt_warn = 0; 16176c72ffaaSAndy Whitcroft our $cnt_chk = 0; 16186c72ffaaSAndy Whitcroft 16190a920b5bSAndy Whitcroft # Trace the real file/line as we go. 16200a920b5bSAndy Whitcroft my $realfile = ''; 16210a920b5bSAndy Whitcroft my $realline = 0; 16220a920b5bSAndy Whitcroft my $realcnt = 0; 16230a920b5bSAndy Whitcroft my $here = ''; 16240a920b5bSAndy Whitcroft my $in_comment = 0; 1625c2fdda0dSAndy Whitcroft my $comment_edge = 0; 16260a920b5bSAndy Whitcroft my $first_line = 0; 16271e855726SWolfram Sang my $p1_prefix = ''; 16280a920b5bSAndy Whitcroft 162913214adfSAndy Whitcroft my $prev_values = 'E'; 163013214adfSAndy Whitcroft 163113214adfSAndy Whitcroft # suppression flags 1632773647a0SAndy Whitcroft my %suppress_ifbraces; 1633170d3a22SAndy Whitcroft my %suppress_whiletrailers; 16342b474a1aSAndy Whitcroft my %suppress_export; 16353e469cdcSAndy Whitcroft my $suppress_statement = 0; 1636653d4876SAndy Whitcroft 16377e51f197SJoe Perches my %signatures = (); 1638323c1260SJoe Perches 1639c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1640de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1641c2fdda0dSAndy Whitcroft # 1642de7d4f0eSAndy Whitcroft my @setup_docs = (); 1643de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1644773647a0SAndy Whitcroft 1645d8b07710SJoe Perches my $camelcase_file_seeded = 0; 1646d8b07710SJoe Perches 1647773647a0SAndy Whitcroft sanitise_line_reset(); 1648c2fdda0dSAndy Whitcroft my $line; 1649c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1650773647a0SAndy Whitcroft $linenr++; 1651773647a0SAndy Whitcroft $line = $rawline; 1652c2fdda0dSAndy Whitcroft 16533705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 16543705ce5bSJoe Perches 1655773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1656de7d4f0eSAndy Whitcroft $setup_docs = 0; 1657de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1658de7d4f0eSAndy Whitcroft $setup_docs = 1; 1659de7d4f0eSAndy Whitcroft } 1660773647a0SAndy Whitcroft #next; 1661de7d4f0eSAndy Whitcroft } 1662773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1663773647a0SAndy Whitcroft $realline=$1-1; 1664773647a0SAndy Whitcroft if (defined $2) { 1665773647a0SAndy Whitcroft $realcnt=$3+1; 1666773647a0SAndy Whitcroft } else { 1667773647a0SAndy Whitcroft $realcnt=1+1; 1668773647a0SAndy Whitcroft } 1669c45dcabdSAndy Whitcroft $in_comment = 0; 1670773647a0SAndy Whitcroft 1671773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1672773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1673773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1674773647a0SAndy Whitcroft # at context start. 1675773647a0SAndy Whitcroft my $edge; 167601fa9147SAndy Whitcroft my $cnt = $realcnt; 167701fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 167801fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 167901fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 168001fa9147SAndy Whitcroft $cnt--; 168101fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1682721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1683fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1684fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1685fae17daeSAndy Whitcroft ($edge) = $1; 1686fae17daeSAndy Whitcroft last; 1687fae17daeSAndy Whitcroft } 1688773647a0SAndy Whitcroft } 1689773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1690773647a0SAndy Whitcroft $in_comment = 1; 1691773647a0SAndy Whitcroft } 1692773647a0SAndy Whitcroft 1693773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1694773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1695773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1696773647a0SAndy Whitcroft if (!defined $edge && 169783242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1698773647a0SAndy Whitcroft { 1699773647a0SAndy Whitcroft $in_comment = 1; 1700773647a0SAndy Whitcroft } 1701773647a0SAndy Whitcroft 1702773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1703773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1704773647a0SAndy Whitcroft 1705171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1706773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1707171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1708773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1709773647a0SAndy Whitcroft } 1710773647a0SAndy Whitcroft push(@lines, $line); 1711773647a0SAndy Whitcroft 1712773647a0SAndy Whitcroft if ($realcnt > 1) { 1713773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1714773647a0SAndy Whitcroft } else { 1715773647a0SAndy Whitcroft $realcnt = 0; 1716773647a0SAndy Whitcroft } 1717773647a0SAndy Whitcroft 1718773647a0SAndy Whitcroft #print "==>$rawline\n"; 1719773647a0SAndy Whitcroft #print "-->$line\n"; 1720de7d4f0eSAndy Whitcroft 1721de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1722de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1723de7d4f0eSAndy Whitcroft } 1724de7d4f0eSAndy Whitcroft } 1725de7d4f0eSAndy Whitcroft 17266c72ffaaSAndy Whitcroft $prefix = ''; 17276c72ffaaSAndy Whitcroft 1728773647a0SAndy Whitcroft $realcnt = 0; 1729773647a0SAndy Whitcroft $linenr = 0; 17300a920b5bSAndy Whitcroft foreach my $line (@lines) { 17310a920b5bSAndy Whitcroft $linenr++; 17321b5539b1SJoe Perches my $sline = $line; #copy of $line 17331b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 17340a920b5bSAndy Whitcroft 1735c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 17366c72ffaaSAndy Whitcroft 17370a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 17386c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 17390a920b5bSAndy Whitcroft $is_patch = 1; 17404a0df2efSAndy Whitcroft $first_line = $linenr + 1; 17410a920b5bSAndy Whitcroft $realline=$1-1; 17420a920b5bSAndy Whitcroft if (defined $2) { 17430a920b5bSAndy Whitcroft $realcnt=$3+1; 17440a920b5bSAndy Whitcroft } else { 17450a920b5bSAndy Whitcroft $realcnt=1+1; 17460a920b5bSAndy Whitcroft } 1747c2fdda0dSAndy Whitcroft annotate_reset(); 174813214adfSAndy Whitcroft $prev_values = 'E'; 174913214adfSAndy Whitcroft 1750773647a0SAndy Whitcroft %suppress_ifbraces = (); 1751170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 17522b474a1aSAndy Whitcroft %suppress_export = (); 17533e469cdcSAndy Whitcroft $suppress_statement = 0; 17540a920b5bSAndy Whitcroft next; 17550a920b5bSAndy Whitcroft 17564a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 17574a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 17584a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1759773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 17600a920b5bSAndy Whitcroft $realline++; 1761d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 17620a920b5bSAndy Whitcroft 17634a0df2efSAndy Whitcroft # Measure the line length and indent. 1764c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 17650a920b5bSAndy Whitcroft 17660a920b5bSAndy Whitcroft # Track the previous line. 17670a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 17680a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1769c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1770c2fdda0dSAndy Whitcroft 1771773647a0SAndy Whitcroft #warn "line<$line>\n"; 17726c72ffaaSAndy Whitcroft 1773d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1774d8aaf121SAndy Whitcroft $realcnt--; 17750a920b5bSAndy Whitcroft } 17760a920b5bSAndy Whitcroft 1777cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1778cc77cdcaSAndy Whitcroft 17790a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1780773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1781773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1782773647a0SAndy Whitcroft 17836c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 17846c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1785773647a0SAndy Whitcroft 1786773647a0SAndy Whitcroft # extract the filename as it passes 17873bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 17883bf9a009SRabin Vincent $realfile = $1; 17892b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 1790270c49a0SJoe Perches $in_commit_log = 0; 17913bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1792773647a0SAndy Whitcroft $realfile = $1; 17932b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 1794270c49a0SJoe Perches $in_commit_log = 0; 17951e855726SWolfram Sang 17961e855726SWolfram Sang $p1_prefix = $1; 1797e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1798e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 1799000d1cc1SJoe Perches WARN("PATCH_PREFIX", 1800000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 18011e855726SWolfram Sang } 1802773647a0SAndy Whitcroft 1803c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1804000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 1805000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1806773647a0SAndy Whitcroft } 1807773647a0SAndy Whitcroft next; 1808773647a0SAndy Whitcroft } 1809773647a0SAndy Whitcroft 1810389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 18110a920b5bSAndy Whitcroft 1812c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1813c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1814c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 18150a920b5bSAndy Whitcroft 18166c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 18176c72ffaaSAndy Whitcroft 18183bf9a009SRabin Vincent# Check for incorrect file permissions 18193bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 18203bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 182104db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 182204db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 1823000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 1824000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 18253bf9a009SRabin Vincent } 18263bf9a009SRabin Vincent } 18273bf9a009SRabin Vincent 182820112475SJoe Perches# Check the patch for a signoff: 1829d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 18304a0df2efSAndy Whitcroft $signoff++; 183115662b3eSJoe Perches $in_commit_log = 0; 18320a920b5bSAndy Whitcroft } 183320112475SJoe Perches 183420112475SJoe Perches# Check signature styles 1835270c49a0SJoe Perches if (!$in_header_lines && 1836ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 183720112475SJoe Perches my $space_before = $1; 183820112475SJoe Perches my $sign_off = $2; 183920112475SJoe Perches my $space_after = $3; 184020112475SJoe Perches my $email = $4; 184120112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 184220112475SJoe Perches 1843ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 1844ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 1845ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 1846ce0338dfSJoe Perches } 184720112475SJoe Perches if (defined $space_before && $space_before ne "") { 18483705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18493705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 18503705ce5bSJoe Perches $fix) { 18513705ce5bSJoe Perches $fixed[$linenr - 1] = 18523705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18533705ce5bSJoe Perches } 185420112475SJoe Perches } 185520112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 18563705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18573705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 18583705ce5bSJoe Perches $fix) { 18593705ce5bSJoe Perches $fixed[$linenr - 1] = 18603705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18613705ce5bSJoe Perches } 18623705ce5bSJoe Perches 186320112475SJoe Perches } 186420112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 18653705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18663705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 18673705ce5bSJoe Perches $fix) { 18683705ce5bSJoe Perches $fixed[$linenr - 1] = 18693705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18703705ce5bSJoe Perches } 187120112475SJoe Perches } 187220112475SJoe Perches 187320112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 187420112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 187520112475SJoe Perches if ($suggested_email eq "") { 1876000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 1877000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 187820112475SJoe Perches } else { 187920112475SJoe Perches my $dequoted = $suggested_email; 188020112475SJoe Perches $dequoted =~ s/^"//; 188120112475SJoe Perches $dequoted =~ s/" </ </; 188220112475SJoe Perches # Don't force email to have quotes 188320112475SJoe Perches # Allow just an angle bracketed address 188420112475SJoe Perches if ("$dequoted$comment" ne $email && 188520112475SJoe Perches "<$email_address>$comment" ne $email && 188620112475SJoe Perches "$suggested_email$comment" ne $email) { 1887000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1888000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 188920112475SJoe Perches } 18900a920b5bSAndy Whitcroft } 18917e51f197SJoe Perches 18927e51f197SJoe Perches# Check for duplicate signatures 18937e51f197SJoe Perches my $sig_nospace = $line; 18947e51f197SJoe Perches $sig_nospace =~ s/\s//g; 18957e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 18967e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 18977e51f197SJoe Perches WARN("BAD_SIGN_OFF", 18987e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 18997e51f197SJoe Perches } else { 19007e51f197SJoe Perches $signatures{$sig_nospace} = 1; 19017e51f197SJoe Perches } 19020a920b5bSAndy Whitcroft } 19030a920b5bSAndy Whitcroft 190400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 19058905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1906000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 1907000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 19086c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1909de7d4f0eSAndy Whitcroft } 1910de7d4f0eSAndy Whitcroft 19116ecd9674SAndy Whitcroft# Check for absolute kernel paths. 19126ecd9674SAndy Whitcroft if ($tree) { 19136ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 19146ecd9674SAndy Whitcroft my $file = $1; 19156ecd9674SAndy Whitcroft 19166ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 19176ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 19186ecd9674SAndy Whitcroft # 19196ecd9674SAndy Whitcroft } else { 19206ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 19216ecd9674SAndy Whitcroft } 19226ecd9674SAndy Whitcroft } 19236ecd9674SAndy Whitcroft } 19246ecd9674SAndy Whitcroft 1925de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1926de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1927171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1928171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1929171ae1a4SAndy Whitcroft 1930171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1931171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1932171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1933171ae1a4SAndy Whitcroft 193434d99219SJoe Perches CHK("INVALID_UTF8", 1935000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 193600df344fSAndy Whitcroft } 19370a920b5bSAndy Whitcroft 193815662b3eSJoe Perches# Check if it's the start of a commit log 193915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 194015662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 1941270c49a0SJoe Perches $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { 194215662b3eSJoe Perches $in_header_lines = 0; 194315662b3eSJoe Perches $in_commit_log = 1; 194415662b3eSJoe Perches } 194515662b3eSJoe Perches 1946fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 1947fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 1948fa64205dSPasi Savanainen if ($in_header_lines && 1949fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 1950fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 1951fa64205dSPasi Savanainen $non_utf8_charset = 1; 1952fa64205dSPasi Savanainen } 1953fa64205dSPasi Savanainen 1954fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 195515662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 1956fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 195715662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 195815662b3eSJoe Perches } 195915662b3eSJoe Perches 196030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 196130670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 196200df344fSAndy Whitcroft 19630a920b5bSAndy Whitcroft#trailing whitespace 19649c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1965c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1966d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 1967d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 1968d5e616fcSJoe Perches $fix) { 1969d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/[\s\015]+$//; 1970d5e616fcSJoe Perches } 1971c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1972c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 19733705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 19743705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 19753705ce5bSJoe Perches $fix) { 1976d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 19773705ce5bSJoe Perches } 19783705ce5bSJoe Perches 1979d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 19800a920b5bSAndy Whitcroft } 19815368df20SAndy Whitcroft 19824783f894SJosh Triplett# Check for FSF mailing addresses. 1983109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 19843e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 19853e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 19864783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 19874783f894SJosh Triplett my $msg_type = \&ERROR; 19884783f894SJosh Triplett $msg_type = \&CHK if ($file); 19894783f894SJosh Triplett &{$msg_type}("FSF_MAILING_ADDRESS", 19904783f894SJosh Triplett "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet) 19914783f894SJosh Triplett } 19924783f894SJosh Triplett 19933354957aSAndi Kleen# check for Kconfig help text having a real description 19949fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 19959fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 19963354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 1997a1385803SAndy Whitcroft $line =~ /.\s*config\s+/) { 19983354957aSAndi Kleen my $length = 0; 19999fe287d7SAndy Whitcroft my $cnt = $realcnt; 20009fe287d7SAndy Whitcroft my $ln = $linenr + 1; 20019fe287d7SAndy Whitcroft my $f; 2002a1385803SAndy Whitcroft my $is_start = 0; 20039fe287d7SAndy Whitcroft my $is_end = 0; 2004a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 20059fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 20069fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 20079fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 20089fe287d7SAndy Whitcroft 20099fe287d7SAndy Whitcroft next if ($f =~ /^-/); 2010a1385803SAndy Whitcroft 2011a1385803SAndy Whitcroft if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { 2012a1385803SAndy Whitcroft $is_start = 1; 2013a1385803SAndy Whitcroft } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { 2014a1385803SAndy Whitcroft $length = -1; 2015a1385803SAndy Whitcroft } 2016a1385803SAndy Whitcroft 20179fe287d7SAndy Whitcroft $f =~ s/^.//; 20183354957aSAndi Kleen $f =~ s/#.*//; 20193354957aSAndi Kleen $f =~ s/^\s+//; 20203354957aSAndi Kleen next if ($f =~ /^$/); 20219fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 20229fe287d7SAndy Whitcroft $is_end = 1; 20239fe287d7SAndy Whitcroft last; 20249fe287d7SAndy Whitcroft } 20253354957aSAndi Kleen $length++; 20263354957aSAndi Kleen } 2027000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 2028a1385803SAndy Whitcroft "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 2029a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 20303354957aSAndi Kleen } 20313354957aSAndi Kleen 20321ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 20331ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 20341ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 20351ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 20361ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 20371ba8dfd1SKees Cook } 20381ba8dfd1SKees Cook 2039c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2040c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2041c68e5878SArnaud Lacombe my $flag = $1; 2042c68e5878SArnaud Lacombe my $replacement = { 2043c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2044c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2045c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2046c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2047c68e5878SArnaud Lacombe }; 2048c68e5878SArnaud Lacombe 2049c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2050c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2051c68e5878SArnaud Lacombe } 2052c68e5878SArnaud Lacombe 2053bff5da43SRob Herring# check for DT compatible documentation 2054bff5da43SRob Herring if (defined $root && $realfile =~ /\.dts/ && 2055bff5da43SRob Herring $rawline =~ /^\+\s*compatible\s*=/) { 2056bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2057bff5da43SRob Herring 2058bff5da43SRob Herring foreach my $compat (@compats) { 2059bff5da43SRob Herring my $compat2 = $compat; 2060bff5da43SRob Herring my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2061bff5da43SRob Herring $compat2 =~ s/\,[a-z]*\-/\,<\.\*>\-/; 2062bff5da43SRob Herring `grep -Erq "$compat|$compat2" $dt_path`; 2063bff5da43SRob Herring if ( $? >> 8 ) { 2064bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2065bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2066bff5da43SRob Herring } 2067bff5da43SRob Herring 2068bff5da43SRob Herring my $vendor = $compat; 2069bff5da43SRob Herring my $vendor_path = $dt_path . "vendor-prefixes.txt"; 2070bff5da43SRob Herring next if (! -f $vendor_path); 2071bff5da43SRob Herring $vendor =~ s/^([a-zA-Z0-9]+)\,.*/$1/; 2072bff5da43SRob Herring `grep -Eq "$vendor" $vendor_path`; 2073bff5da43SRob Herring if ( $? >> 8 ) { 2074bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2075bff5da43SRob Herring "DT compatible string vendor \"$vendor\" appears un-documented -- check $vendor_path\n" . $herecurr); 2076bff5da43SRob Herring } 2077bff5da43SRob Herring } 2078bff5da43SRob Herring } 2079bff5da43SRob Herring 20805368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 20815368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 20825368df20SAndy Whitcroft 20836cd7f386SJoe Perches#line length limit 2084c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2085f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 20860fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 20878bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 20886cd7f386SJoe Perches $length > $max_line_length) 2089c45dcabdSAndy Whitcroft { 2090000d1cc1SJoe Perches WARN("LONG_LINE", 20916cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 20920a920b5bSAndy Whitcroft } 20930a920b5bSAndy Whitcroft 2094ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability 20958c5fcd24SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 20968c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 20978c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 2098ca56dc09SJosh Triplett if ($line =~ /^\+\s*"/ && 2099ca56dc09SJosh Triplett $prevline =~ /"\s*$/ && 21008c5fcd24SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 2101ca56dc09SJosh Triplett WARN("SPLIT_STRING", 2102ca56dc09SJosh Triplett "quoted string split across lines\n" . $hereprev); 2103ca56dc09SJosh Triplett } 2104ca56dc09SJosh Triplett 21055e79d96eSJoe Perches# check for spaces before a quoted newline 21065e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 21073705ce5bSJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 21083705ce5bSJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 21093705ce5bSJoe Perches $fix) { 21103705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 21113705ce5bSJoe Perches } 21123705ce5bSJoe Perches 21135e79d96eSJoe Perches } 21145e79d96eSJoe Perches 21158905a67cSAndy Whitcroft# check for adding lines without a newline. 21168905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2117000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2118000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 21198905a67cSAndy Whitcroft } 21208905a67cSAndy Whitcroft 212142e41c54SMike Frysinger# Blackfin: use hi/lo macros 212242e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 212342e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 212442e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2125000d1cc1SJoe Perches ERROR("LO_MACRO", 2126000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 212742e41c54SMike Frysinger } 212842e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 212942e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2130000d1cc1SJoe Perches ERROR("HI_MACRO", 2131000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 213242e41c54SMike Frysinger } 213342e41c54SMike Frysinger } 213442e41c54SMike Frysinger 2135b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2136b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 21370a920b5bSAndy Whitcroft 21380a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 21390a920b5bSAndy Whitcroft# more than 8 must use tabs. 2140c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2141c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2142c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2143d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 21443705ce5bSJoe Perches if (ERROR("CODE_INDENT", 21453705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 21463705ce5bSJoe Perches $fix) { 21473705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 21483705ce5bSJoe Perches } 21490a920b5bSAndy Whitcroft } 21500a920b5bSAndy Whitcroft 215108e44365SAlberto Panizzo# check for space before tabs. 215208e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 215308e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 21543705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 21553705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 21563705ce5bSJoe Perches $fix) { 2157c76f4cb3SJoe Perches while ($fixed[$linenr - 1] =~ 2158c76f4cb3SJoe Perches s/(^\+.*) {8,8}+\t/$1\t\t/) {} 2159c76f4cb3SJoe Perches while ($fixed[$linenr - 1] =~ 2160c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 21613705ce5bSJoe Perches } 216208e44365SAlberto Panizzo } 216308e44365SAlberto Panizzo 2164d1fe9c09SJoe Perches# check for && or || at the start of a line 2165d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2166d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2167d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2168d1fe9c09SJoe Perches } 2169d1fe9c09SJoe Perches 2170d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2171d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 2172d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { 2173d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2174d1fe9c09SJoe Perches my $oldindent = $1; 2175d1fe9c09SJoe Perches my $rest = $2; 2176d1fe9c09SJoe Perches 2177d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2178d1fe9c09SJoe Perches if ($pos >= 0) { 2179b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2180b34a26f3SJoe Perches my $newindent = $2; 2181d1fe9c09SJoe Perches 2182d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2183d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2184d1fe9c09SJoe Perches " " x ($pos % 8); 2185d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2186d1fe9c09SJoe Perches 2187d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2188d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 21893705ce5bSJoe Perches 21903705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 21913705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 21923705ce5bSJoe Perches $fix && $line =~ /^\+/) { 21933705ce5bSJoe Perches $fixed[$linenr - 1] =~ 21943705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 21953705ce5bSJoe Perches } 2196d1fe9c09SJoe Perches } 2197d1fe9c09SJoe Perches } 2198d1fe9c09SJoe Perches } 2199d1fe9c09SJoe Perches 220023f780c9SJoe Perches if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { 22013705ce5bSJoe Perches if (CHK("SPACING", 22023705ce5bSJoe Perches "No space is necessary after a cast\n" . $hereprev) && 22033705ce5bSJoe Perches $fix) { 22043705ce5bSJoe Perches $fixed[$linenr - 1] =~ 22053705ce5bSJoe Perches s/^(\+.*\*[ \t]*\))[ \t]+/$1/; 22063705ce5bSJoe Perches } 2207aad4f614SJoe Perches } 2208aad4f614SJoe Perches 220905880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2210fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 2211fdb4bcd6SJoe Perches $rawline =~ /^\+[ \t]*\*/) { 221205880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 221305880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 221405880600SJoe Perches } 221505880600SJoe Perches 221605880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2217a605e32eSJoe Perches $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* 2218a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 221961135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2220a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 2221a605e32eSJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2222a605e32eSJoe Perches "networking block comments start with * on subsequent lines\n" . $hereprev); 2223a605e32eSJoe Perches } 2224a605e32eSJoe Perches 2225a605e32eSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2226c24f9f19SJoe Perches $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2227c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2228c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2229c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 223005880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 223105880600SJoe Perches "networking block comments put the trailing */ on a separate line\n" . $herecurr); 223205880600SJoe Perches } 223305880600SJoe Perches 22345f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 22356b4c5bebSAndy Whitcroft# Exceptions: 22366b4c5bebSAndy Whitcroft# 1) within comments 22376b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 22386b4c5bebSAndy Whitcroft# 3) hanging labels 22393705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 22405f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 22413705ce5bSJoe Perches if (WARN("LEADING_SPACE", 22423705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 22433705ce5bSJoe Perches $fix) { 22443705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 22453705ce5bSJoe Perches } 22465f7ddae6SRaffaele Recalcati } 22475f7ddae6SRaffaele Recalcati 2248b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 2249b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 2250b9ea10d6SAndy Whitcroft 22511ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 22521ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 22531ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 22541ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 22551ba8dfd1SKees Cook } 22561ba8dfd1SKees Cook 2257c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 2258cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 2259000d1cc1SJoe Perches WARN("CVS_KEYWORD", 2260000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 2261c2fdda0dSAndy Whitcroft } 226222f2a2efSAndy Whitcroft 226342e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 226442e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 226542e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2266000d1cc1SJoe Perches ERROR("CSYNC", 2267000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 226842e41c54SMike Frysinger } 226942e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 227042e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2271000d1cc1SJoe Perches ERROR("SSYNC", 2272000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 227342e41c54SMike Frysinger } 227442e41c54SMike Frysinger 227556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 227656e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 227756e77d70SJoe Perches WARN("HOTPLUG_SECTION", 227856e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 227956e77d70SJoe Perches } 228056e77d70SJoe Perches 22819c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 22822b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 22832b474a1aSAndy Whitcroft $realline_next); 22843e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 22853e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 22861b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 2287170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2288f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2289171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 2290171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 2291171ae1a4SAndy Whitcroft 22923e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 22933e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 22943e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 22953e469cdcSAndy Whitcroft # until we hit end of it. 22963e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 22973e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 22983e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 22993e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 23003e469cdcSAndy Whitcroft } 2301f74bd194SAndy Whitcroft 23022b474a1aSAndy Whitcroft # Find the real next line. 23032b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 23042b474a1aSAndy Whitcroft if (defined $realline_next && 23052b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 23062b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 23072b474a1aSAndy Whitcroft $realline_next++; 23082b474a1aSAndy Whitcroft } 23092b474a1aSAndy Whitcroft 2310171ae1a4SAndy Whitcroft my $s = $stat; 2311171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 2312cf655043SAndy Whitcroft 2313c2fdda0dSAndy Whitcroft # Ignore goto labels. 2314171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 2315c2fdda0dSAndy Whitcroft 2316c2fdda0dSAndy Whitcroft # Ignore functions being called 2317171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 2318c2fdda0dSAndy Whitcroft 2319463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 2320463f2864SAndy Whitcroft 2321c45dcabdSAndy Whitcroft # declarations always start with types 2322d2506586SAndy 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) { 2323c45dcabdSAndy Whitcroft my $type = $1; 2324c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 2325c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 2326c45dcabdSAndy Whitcroft 23276c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 2328a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 2329c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 2330c2fdda0dSAndy Whitcroft } 23318905a67cSAndy Whitcroft 23326c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 233365863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 2334c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 23359c0ca6f9SAndy Whitcroft } 23368905a67cSAndy Whitcroft 23378905a67cSAndy Whitcroft # Check for any sort of function declaration. 23388905a67cSAndy Whitcroft # int foo(something bar, other baz); 23398905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 2340171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 23418905a67cSAndy Whitcroft my ($name_len) = length($1); 23428905a67cSAndy Whitcroft 2343cf655043SAndy Whitcroft my $ctx = $s; 2344773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 23458905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 2346cf655043SAndy Whitcroft 23478905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 2348c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 23498905a67cSAndy Whitcroft 2350c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 23518905a67cSAndy Whitcroft } 23528905a67cSAndy Whitcroft } 23538905a67cSAndy Whitcroft } 23548905a67cSAndy Whitcroft 23559c0ca6f9SAndy Whitcroft } 23569c0ca6f9SAndy Whitcroft 235700df344fSAndy Whitcroft# 235800df344fSAndy Whitcroft# Checks which may be anchored in the context. 235900df344fSAndy Whitcroft# 236000df344fSAndy Whitcroft 236100df344fSAndy Whitcroft# Check for switch () and associated case and default 236200df344fSAndy Whitcroft# statements should be at the same indent. 236300df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 236400df344fSAndy Whitcroft my $err = ''; 236500df344fSAndy Whitcroft my $sep = ''; 236600df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 236700df344fSAndy Whitcroft shift(@ctx); 236800df344fSAndy Whitcroft for my $ctx (@ctx) { 236900df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 237000df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 237100df344fSAndy Whitcroft $indent != $cindent) { 237200df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 237300df344fSAndy Whitcroft $sep = ''; 237400df344fSAndy Whitcroft } else { 237500df344fSAndy Whitcroft $sep = "[...]\n"; 237600df344fSAndy Whitcroft } 237700df344fSAndy Whitcroft } 237800df344fSAndy Whitcroft if ($err ne '') { 2379000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2380000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2381de7d4f0eSAndy Whitcroft } 2382de7d4f0eSAndy Whitcroft } 2383de7d4f0eSAndy Whitcroft 2384de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2385de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 2386c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2387773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2388773647a0SAndy Whitcroft 23899c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 23908eef05ddSJoe Perches 23918eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 23928eef05ddSJoe Perches WARN("DEEP_INDENTATION", 23938eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 23948eef05ddSJoe Perches } 23958eef05ddSJoe Perches 2396de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2397de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 2398de7d4f0eSAndy Whitcroft 2399548596d5SAndy Whitcroft my $ctx_ln = $linenr; 2400548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 2401de7d4f0eSAndy Whitcroft 2402548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2403548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 2404548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 2405548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2406548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2407773647a0SAndy Whitcroft $ctx_ln++; 2408773647a0SAndy Whitcroft } 2409548596d5SAndy Whitcroft 241053210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 241153210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2412773647a0SAndy Whitcroft 2413773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 2414000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2415000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 241601464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 241700df344fSAndy Whitcroft } 2418773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2419773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 2420773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 2421773647a0SAndy Whitcroft { 24229c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 24239c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 2424000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 2425000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 242601464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 24279c0ca6f9SAndy Whitcroft } 24289c0ca6f9SAndy Whitcroft } 242900df344fSAndy Whitcroft } 243000df344fSAndy Whitcroft 24314d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 24324d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 24333e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 24343e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 24353e469cdcSAndy Whitcroft if (!defined $stat); 24364d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 24374d001e4dSAndy Whitcroft 24384d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 24394d001e4dSAndy Whitcroft 24404d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 24414d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 24424d001e4dSAndy Whitcroft # where necessary. 24434d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 24444d001e4dSAndy Whitcroft 24454d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 24466f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 24476f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 24484d001e4dSAndy Whitcroft 24494d001e4dSAndy Whitcroft # We want to check the first line inside the block 24504d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 24514d001e4dSAndy Whitcroft # 1) any blank line termination 24524d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 24534d001e4dSAndy Whitcroft # 3) any do (...) { 24544d001e4dSAndy Whitcroft my $continuation = 0; 24554d001e4dSAndy Whitcroft my $check = 0; 24564d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 24574d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 24584d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 24594d001e4dSAndy Whitcroft $continuation = 1; 24604d001e4dSAndy Whitcroft } 24619bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 24624d001e4dSAndy Whitcroft $check = 1; 24634d001e4dSAndy Whitcroft $cond_lines++; 24644d001e4dSAndy Whitcroft } 24654d001e4dSAndy Whitcroft 24664d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 24674d001e4dSAndy Whitcroft # preprocessor statement. 24684d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 24694d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 24704d001e4dSAndy Whitcroft $check = 0; 24714d001e4dSAndy Whitcroft } 24724d001e4dSAndy Whitcroft 24739bd49efeSAndy Whitcroft my $cond_ptr = -1; 2474740504c6SAndy Whitcroft $continuation = 0; 24759bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 24769bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 24774d001e4dSAndy Whitcroft 2478f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 2479f16fa28fSAndy Whitcroft # is not linear. 2480f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2481f16fa28fSAndy Whitcroft $check = 0; 2482f16fa28fSAndy Whitcroft } 2483f16fa28fSAndy Whitcroft 24849bd49efeSAndy Whitcroft # Ignore: 24859bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 24869bd49efeSAndy Whitcroft # 2) preprocessor lines, and 24879bd49efeSAndy Whitcroft # 3) labels. 2488740504c6SAndy Whitcroft if ($continuation || 2489740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 24909bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 24919bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 2492740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 249330dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 24949bd49efeSAndy Whitcroft $cond_lines++; 24959bd49efeSAndy Whitcroft } 24964d001e4dSAndy Whitcroft } 249730dad6ebSAndy Whitcroft } 24984d001e4dSAndy Whitcroft 24994d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 25004d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 25014d001e4dSAndy Whitcroft 25024d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 25034d001e4dSAndy Whitcroft # this is not this patch's fault. 25044d001e4dSAndy Whitcroft if (!defined($stat_real) || 25054d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 25064d001e4dSAndy Whitcroft $check = 0; 25074d001e4dSAndy Whitcroft } 25084d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 25094d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 25104d001e4dSAndy Whitcroft } 25114d001e4dSAndy Whitcroft 25129bd49efeSAndy 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"; 25134d001e4dSAndy Whitcroft 25144d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 25154d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 2516000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 2517000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 25184d001e4dSAndy Whitcroft } 25194d001e4dSAndy Whitcroft } 25204d001e4dSAndy Whitcroft 25216c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 25226c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 25231f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 25241f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 25256c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 2526c2fdda0dSAndy Whitcroft if ($dbg_values) { 2527c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 2528cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 2529cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 25301f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 2531c2fdda0dSAndy Whitcroft } 25326c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 25336c72ffaaSAndy Whitcroft 253400df344fSAndy Whitcroft#ignore lines not being added 25353705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 253600df344fSAndy Whitcroft 2537653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 25387429c690SAndy Whitcroft if ($dbg_type) { 25397429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 2540000d1cc1SJoe Perches ERROR("TEST_TYPE", 2541000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 25427429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2543000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 2544000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 25457429c690SAndy Whitcroft } 2546653d4876SAndy Whitcroft next; 2547653d4876SAndy Whitcroft } 2548a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2549a1ef277eSAndy Whitcroft if ($dbg_attr) { 25509360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2551000d1cc1SJoe Perches ERROR("TEST_ATTR", 2552000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 25539360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2554000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2555000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2556a1ef277eSAndy Whitcroft } 2557a1ef277eSAndy Whitcroft next; 2558a1ef277eSAndy Whitcroft } 2559653d4876SAndy Whitcroft 2560f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 256199423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 256299423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2563000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2564000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . $hereprev); 2565f0a594c1SAndy Whitcroft } 2566f0a594c1SAndy Whitcroft 256700df344fSAndy Whitcroft# 256800df344fSAndy Whitcroft# Checks which are anchored on the added line. 256900df344fSAndy Whitcroft# 257000df344fSAndy Whitcroft 2571653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2572c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2573653d4876SAndy Whitcroft my $path = $1; 2574653d4876SAndy Whitcroft if ($path =~ m{//}) { 2575000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2576495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 2577495e9d84SJoe Perches } 2578495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 2579495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 2580495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 2581653d4876SAndy Whitcroft } 2582653d4876SAndy Whitcroft } 2583653d4876SAndy Whitcroft 258400df344fSAndy Whitcroft# no C99 // comments 258500df344fSAndy Whitcroft if ($line =~ m{//}) { 25863705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 25873705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 25883705ce5bSJoe Perches $fix) { 25893705ce5bSJoe Perches my $line = $fixed[$linenr - 1]; 25903705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 25913705ce5bSJoe Perches my $comment = trim($1); 25923705ce5bSJoe Perches $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; 25933705ce5bSJoe Perches } 25943705ce5bSJoe Perches } 259500df344fSAndy Whitcroft } 259600df344fSAndy Whitcroft # Remove C99 comments. 25970a920b5bSAndy Whitcroft $line =~ s@//.*@@; 25986c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 25990a920b5bSAndy Whitcroft 26002b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 26012b474a1aSAndy Whitcroft# the whole statement. 26022b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 26032b474a1aSAndy Whitcroft if (defined $realline_next && 26042b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 26052b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 26062b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 26072b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 26083cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 26093cbf62dfSAndy Whitcroft # a prefix: 26103cbf62dfSAndy Whitcroft # XXX(foo); 26113cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 2612653d4876SAndy Whitcroft my $name = $1; 261387a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 26143cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 26153cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 26163cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 26173cbf62dfSAndy Whitcroft 26183cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 26192b474a1aSAndy Whitcroft \n.}\s*$| 262048012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 262148012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 262248012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 26232b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 26242b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 262548012058SAndy Whitcroft )/x) { 26262b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 26272b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 26282b474a1aSAndy Whitcroft } else { 26292b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 26300a920b5bSAndy Whitcroft } 26310a920b5bSAndy Whitcroft } 26322b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 26332b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 26342b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 26352b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 26362b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 26372b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 26382b474a1aSAndy Whitcroft } 26392b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 26402b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 2641000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 2642000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 26432b474a1aSAndy Whitcroft } 26440a920b5bSAndy Whitcroft 26455150bda4SJoe Eloff# check for global initialisers. 2646d5e616fcSJoe Perches if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { 2647d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 2648000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 2649d5e616fcSJoe Perches $herecurr) && 2650d5e616fcSJoe Perches $fix) { 2651d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; 2652d5e616fcSJoe Perches } 2653f0a594c1SAndy Whitcroft } 26540a920b5bSAndy Whitcroft# check for static initialisers. 2655d5e616fcSJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2656d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 2657000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 2658d5e616fcSJoe Perches $herecurr) && 2659d5e616fcSJoe Perches $fix) { 2660d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; 2661d5e616fcSJoe Perches } 26620a920b5bSAndy Whitcroft } 26630a920b5bSAndy Whitcroft 2664cb710ecaSJoe Perches# check for static const char * arrays. 2665cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 2666000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2667000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 2668cb710ecaSJoe Perches $herecurr); 2669cb710ecaSJoe Perches } 2670cb710ecaSJoe Perches 2671cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 2672cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 2673000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2674000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 2675cb710ecaSJoe Perches $herecurr); 2676cb710ecaSJoe Perches } 2677cb710ecaSJoe Perches 2678*9b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 2679*9b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 2680*9b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2681*9b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 2682*9b0fa60dSJoe Perches $herecurr); 2683*9b0fa60dSJoe Perches } 2684*9b0fa60dSJoe Perches 2685b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 2686b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 2687b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 2688b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 2689b36190c5SJoe Perches $fix) { 2690b36190c5SJoe Perches $fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 2691b36190c5SJoe Perches } 2692b36190c5SJoe Perches } 2693b36190c5SJoe Perches 269492e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE 269592e112fdSJoe Perches if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { 269692e112fdSJoe Perches if (WARN("DEFINE_PCI_DEVICE_TABLE", 269792e112fdSJoe Perches "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && 269892e112fdSJoe Perches $fix) { 269992e112fdSJoe Perches $fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; 270092e112fdSJoe Perches } 270193ed0e2dSJoe Perches } 270293ed0e2dSJoe Perches 2703653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 2704653d4876SAndy Whitcroft# make sense. 2705653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 27068054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 2707c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 27088ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 2709653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 2710000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 2711000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 27120a920b5bSAndy Whitcroft } 27130a920b5bSAndy Whitcroft 27140a920b5bSAndy Whitcroft# * goes on variable not on type 271565863862SAndy Whitcroft # (char*[ const]) 2716bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2717bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 27183705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 2719d8aaf121SAndy Whitcroft 272065863862SAndy Whitcroft # Should start with a space. 272165863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 272265863862SAndy Whitcroft # Should not end with a space. 272365863862SAndy Whitcroft $to =~ s/\s+$//; 272465863862SAndy Whitcroft # '*'s should not have spaces between. 2725f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 272665863862SAndy Whitcroft } 2727d8aaf121SAndy Whitcroft 27283705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 272965863862SAndy Whitcroft if ($from ne $to) { 27303705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 27313705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 27323705ce5bSJoe Perches $fix) { 27333705ce5bSJoe Perches my $sub_from = $ident; 27343705ce5bSJoe Perches my $sub_to = $ident; 27353705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 27363705ce5bSJoe Perches $fixed[$linenr - 1] =~ 27373705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 27383705ce5bSJoe Perches } 273965863862SAndy Whitcroft } 2740bfcb2cc7SAndy Whitcroft } 2741bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2742bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 27433705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 2744d8aaf121SAndy Whitcroft 274565863862SAndy Whitcroft # Should start with a space. 274665863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 274765863862SAndy Whitcroft # Should not end with a space. 274865863862SAndy Whitcroft $to =~ s/\s+$//; 274965863862SAndy Whitcroft # '*'s should not have spaces between. 2750f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 275165863862SAndy Whitcroft } 275265863862SAndy Whitcroft # Modifiers should have spaces. 275365863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 275465863862SAndy Whitcroft 27553705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 2756667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 27573705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 27583705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 27593705ce5bSJoe Perches $fix) { 27603705ce5bSJoe Perches 27613705ce5bSJoe Perches my $sub_from = $match; 27623705ce5bSJoe Perches my $sub_to = $match; 27633705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 27643705ce5bSJoe Perches $fixed[$linenr - 1] =~ 27653705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 27663705ce5bSJoe Perches } 276765863862SAndy Whitcroft } 27680a920b5bSAndy Whitcroft } 27690a920b5bSAndy Whitcroft 27700a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 27710a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 27720a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 27730a920b5bSAndy Whitcroft# print "$herecurr"; 27740a920b5bSAndy Whitcroft# $clean = 0; 27750a920b5bSAndy Whitcroft# } 27760a920b5bSAndy Whitcroft 27778905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 2778000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 2779000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 27808905a67cSAndy Whitcroft } 27818905a67cSAndy Whitcroft 278217441227SJoe Perches# check for uses of printk_ratelimit 278317441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 2784000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 2785000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 278617441227SJoe Perches } 278717441227SJoe Perches 278800df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 278900df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 279000df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 279125985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 279200df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 2793f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 279400df344fSAndy Whitcroft my $ok = 0; 279500df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 279600df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 279725985edcSLucas De Marchi # we have a preceding printk if it ends 279800df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 279900df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 280000df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 280100df344fSAndy Whitcroft $ok = 1; 280200df344fSAndy Whitcroft } 280300df344fSAndy Whitcroft last; 280400df344fSAndy Whitcroft } 280500df344fSAndy Whitcroft } 280600df344fSAndy Whitcroft if ($ok == 0) { 2807000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 2808000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 28090a920b5bSAndy Whitcroft } 281000df344fSAndy Whitcroft } 28110a920b5bSAndy Whitcroft 2812243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 2813243f3803SJoe Perches my $orig = $1; 2814243f3803SJoe Perches my $level = lc($orig); 2815243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 28168f26b837SJoe Perches my $level2 = $level; 28178f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 2818243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 28198f26b837SJoe Perches "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 2820243f3803SJoe Perches } 2821243f3803SJoe Perches 2822243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 2823d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 2824d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 2825d5e616fcSJoe Perches $fix) { 2826d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 2827d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 2828d5e616fcSJoe Perches } 2829243f3803SJoe Perches } 2830243f3803SJoe Perches 2831dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 2832dc139313SJoe Perches my $orig = $1; 2833dc139313SJoe Perches my $level = lc($orig); 2834dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 2835dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 2836dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 2837dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 2838dc139313SJoe Perches } 2839dc139313SJoe Perches 2840653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 2841653d4876SAndy Whitcroft# or if closed on same line 2842c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2843c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 2844000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2845000d1cc1SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr); 28460a920b5bSAndy Whitcroft } 2847653d4876SAndy Whitcroft 28488905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 28498905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 28508905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2851000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2852000d1cc1SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev); 28538905a67cSAndy Whitcroft } 28548905a67cSAndy Whitcroft 28550c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 28563705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 28573705ce5bSJoe Perches if (WARN("SPACING", 28583705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 28593705ce5bSJoe Perches $fix) { 28603705ce5bSJoe Perches $fixed[$linenr - 1] =~ 28613705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 28623705ce5bSJoe Perches } 28630c73b4ebSAndy Whitcroft } 28640c73b4ebSAndy Whitcroft 286531070b5dSJoe Perches# Function pointer declarations 286631070b5dSJoe Perches# check spacing between type, funcptr, and args 286731070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 286891f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 286931070b5dSJoe Perches my $declare = $1; 287031070b5dSJoe Perches my $pre_pointer_space = $2; 287131070b5dSJoe Perches my $post_pointer_space = $3; 287231070b5dSJoe Perches my $funcname = $4; 287331070b5dSJoe Perches my $post_funcname_space = $5; 287431070b5dSJoe Perches my $pre_args_space = $6; 287531070b5dSJoe Perches 287691f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 287791f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 287891f72e9cSJoe Perches# don't need a space so don't warn for those. 287991f72e9cSJoe Perches my $post_declare_space = ""; 288091f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 288191f72e9cSJoe Perches $post_declare_space = $1; 288291f72e9cSJoe Perches $declare = rtrim($declare); 288391f72e9cSJoe Perches } 288491f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 288531070b5dSJoe Perches WARN("SPACING", 288631070b5dSJoe Perches "missing space after return type\n" . $herecurr); 288791f72e9cSJoe Perches $post_declare_space = " "; 288831070b5dSJoe Perches } 288931070b5dSJoe Perches 289031070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 289191f72e9cSJoe Perches# This test is not currently implemented because these declarations are 289291f72e9cSJoe Perches# equivalent to 289391f72e9cSJoe Perches# int foo(int bar, ...) 289491f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 289591f72e9cSJoe Perches# 289691f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 289791f72e9cSJoe Perches# WARN("SPACING", 289891f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 289991f72e9cSJoe Perches# } 290031070b5dSJoe Perches 290131070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 290231070b5dSJoe Perches if (defined $pre_pointer_space && 290331070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 290431070b5dSJoe Perches WARN("SPACING", 290531070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 290631070b5dSJoe Perches } 290731070b5dSJoe Perches 290831070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 290931070b5dSJoe Perches if (defined $post_pointer_space && 291031070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 291131070b5dSJoe Perches WARN("SPACING", 291231070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 291331070b5dSJoe Perches } 291431070b5dSJoe Perches 291531070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 291631070b5dSJoe Perches if (defined $post_funcname_space && 291731070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 291831070b5dSJoe Perches WARN("SPACING", 291931070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 292031070b5dSJoe Perches } 292131070b5dSJoe Perches 292231070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 292331070b5dSJoe Perches if (defined $pre_args_space && 292431070b5dSJoe Perches $pre_args_space =~ /^\s/) { 292531070b5dSJoe Perches WARN("SPACING", 292631070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 292731070b5dSJoe Perches } 292831070b5dSJoe Perches 292931070b5dSJoe Perches if (show_type("SPACING") && $fix) { 293031070b5dSJoe Perches $fixed[$linenr - 1] =~ 293191f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 293231070b5dSJoe Perches } 293331070b5dSJoe Perches } 293431070b5dSJoe Perches 29358d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 29368d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 2937fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2938fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 29398d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 29408d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 29418d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 2942fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 2943daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 29443705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 29453705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 29463705ce5bSJoe Perches $fix) { 29473705ce5bSJoe Perches $fixed[$linenr - 1] =~ 29483705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 29493705ce5bSJoe Perches } 29508d31cfceSAndy Whitcroft } 29518d31cfceSAndy Whitcroft } 29528d31cfceSAndy Whitcroft 2953f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 29546c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 2955c2fdda0dSAndy Whitcroft my $name = $1; 2956773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 2957773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 2958c2fdda0dSAndy Whitcroft 2959c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 2960773647a0SAndy Whitcroft if ($name =~ /^(?: 2961773647a0SAndy Whitcroft if|for|while|switch|return|case| 2962773647a0SAndy Whitcroft volatile|__volatile__| 2963773647a0SAndy Whitcroft __attribute__|format|__extension__| 2964773647a0SAndy Whitcroft asm|__asm__)$/x) 2965773647a0SAndy Whitcroft { 2966c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 2967c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 2968c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2969c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2970773647a0SAndy Whitcroft 2971773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2972c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2973c2fdda0dSAndy Whitcroft 2974c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2975c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2976773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2977c2fdda0dSAndy Whitcroft 2978c2fdda0dSAndy Whitcroft } else { 29793705ce5bSJoe Perches if (WARN("SPACING", 29803705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 29813705ce5bSJoe Perches $fix) { 29823705ce5bSJoe Perches $fixed[$linenr - 1] =~ 29833705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 29843705ce5bSJoe Perches } 2985f0a594c1SAndy Whitcroft } 29866c72ffaaSAndy Whitcroft } 29879a4cad4eSEric Nelson 2988653d4876SAndy Whitcroft# Check operator spacing. 29890a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 29903705ce5bSJoe Perches my $fixed_line = ""; 29913705ce5bSJoe Perches my $line_fixed = 0; 29923705ce5bSJoe Perches 29939c0ca6f9SAndy Whitcroft my $ops = qr{ 29949c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 29959c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 29969c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 29971f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 299884731623SJoe Perches \?:|\?|: 29999c0ca6f9SAndy Whitcroft }x; 3000cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 30013705ce5bSJoe Perches 30023705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 30033705ce5bSJoe Perches## foreach my $el (@elements) { 30043705ce5bSJoe Perches## print("el: <$el>\n"); 30053705ce5bSJoe Perches## } 30063705ce5bSJoe Perches 30073705ce5bSJoe Perches my @fix_elements = (); 300800df344fSAndy Whitcroft my $off = 0; 30096c72ffaaSAndy Whitcroft 30103705ce5bSJoe Perches foreach my $el (@elements) { 30113705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 30123705ce5bSJoe Perches $off += length($el); 30133705ce5bSJoe Perches } 30143705ce5bSJoe Perches 30153705ce5bSJoe Perches $off = 0; 30163705ce5bSJoe Perches 30176c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 3018b34c648bSJoe Perches my $last_after = -1; 30196c72ffaaSAndy Whitcroft 30200a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 30213705ce5bSJoe Perches 30223705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 30233705ce5bSJoe Perches 30243705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 30253705ce5bSJoe Perches 30264a0df2efSAndy Whitcroft $off += length($elements[$n]); 30274a0df2efSAndy Whitcroft 302825985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 3029773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 3030773647a0SAndy Whitcroft my $cc = ''; 3031773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 3032773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 3033773647a0SAndy Whitcroft } 3034773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 3035773647a0SAndy Whitcroft 30364a0df2efSAndy Whitcroft my $a = ''; 30374a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 30384a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 3039cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 30404a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 30414a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 3042773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 30434a0df2efSAndy Whitcroft 30440a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 30454a0df2efSAndy Whitcroft 30464a0df2efSAndy Whitcroft my $c = ''; 30470a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 30484a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 30494a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 3050cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 30514a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 30524a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 30538b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 30544a0df2efSAndy Whitcroft } else { 30554a0df2efSAndy Whitcroft $c = 'E'; 30560a920b5bSAndy Whitcroft } 30570a920b5bSAndy Whitcroft 30584a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 30594a0df2efSAndy Whitcroft 30604a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 30614a0df2efSAndy Whitcroft 30626c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 3063de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 30640a920b5bSAndy Whitcroft 306574048ed8SAndy Whitcroft # Pull out the value of this operator. 30666c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 30670a920b5bSAndy Whitcroft 30681f65f947SAndy Whitcroft # Get the full operator variant. 30691f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 30701f65f947SAndy Whitcroft 307113214adfSAndy Whitcroft # Ignore operators passed as parameters. 307213214adfSAndy Whitcroft if ($op_type ne 'V' && 307313214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 307413214adfSAndy Whitcroft 3075cf655043SAndy Whitcroft# # Ignore comments 3076cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 307713214adfSAndy Whitcroft 3078d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 307913214adfSAndy Whitcroft } elsif ($op eq ';') { 3080cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 3081cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 30823705ce5bSJoe Perches if (ERROR("SPACING", 30833705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3084b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 30853705ce5bSJoe Perches $line_fixed = 1; 30863705ce5bSJoe Perches } 3087d8aaf121SAndy Whitcroft } 3088d8aaf121SAndy Whitcroft 3089d8aaf121SAndy Whitcroft # // is a comment 3090d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 30910a920b5bSAndy Whitcroft 30921f65f947SAndy Whitcroft # No spaces for: 30931f65f947SAndy Whitcroft # -> 30941f65f947SAndy Whitcroft # : when part of a bitfield 30951f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 30964a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 30973705ce5bSJoe Perches if (ERROR("SPACING", 30983705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 3099b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 31003705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 31013705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 31023705ce5bSJoe Perches } 3103b34c648bSJoe Perches $line_fixed = 1; 31043705ce5bSJoe Perches } 31050a920b5bSAndy Whitcroft } 31060a920b5bSAndy Whitcroft 31070a920b5bSAndy Whitcroft # , must have a space on the right. 31080a920b5bSAndy Whitcroft } elsif ($op eq ',') { 3109cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 31103705ce5bSJoe Perches if (ERROR("SPACING", 31113705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3112b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 31133705ce5bSJoe Perches $line_fixed = 1; 3114b34c648bSJoe Perches $last_after = $n; 31153705ce5bSJoe Perches } 31160a920b5bSAndy Whitcroft } 31170a920b5bSAndy Whitcroft 31189c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 311974048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 31209c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 31219c0ca6f9SAndy Whitcroft 31229c0ca6f9SAndy Whitcroft # unary operators should have a space before and 31239c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 31249c0ca6f9SAndy Whitcroft # unary operator, or a cast 31259c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 312674048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 31270d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 3128cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 31293705ce5bSJoe Perches if (ERROR("SPACING", 31303705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 3131b34c648bSJoe Perches if ($n != $last_after + 2) { 3132b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 31333705ce5bSJoe Perches $line_fixed = 1; 31343705ce5bSJoe Perches } 31350a920b5bSAndy Whitcroft } 3136b34c648bSJoe Perches } 3137a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 3138171ae1a4SAndy Whitcroft # A unary '*' may be const 3139171ae1a4SAndy Whitcroft 3140171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 31413705ce5bSJoe Perches if (ERROR("SPACING", 31423705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3143b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 31443705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 31453705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 31463705ce5bSJoe Perches } 3147b34c648bSJoe Perches $line_fixed = 1; 31483705ce5bSJoe Perches } 31490a920b5bSAndy Whitcroft } 31500a920b5bSAndy Whitcroft 31510a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 31520a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 3153773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 31543705ce5bSJoe Perches if (ERROR("SPACING", 31553705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 3156b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 31573705ce5bSJoe Perches $line_fixed = 1; 31583705ce5bSJoe Perches } 31590a920b5bSAndy Whitcroft } 3160773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 3161773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 31623705ce5bSJoe Perches if (ERROR("SPACING", 31633705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3164b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 31653705ce5bSJoe Perches $line_fixed = 1; 31663705ce5bSJoe Perches } 3167653d4876SAndy Whitcroft } 3168773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 31693705ce5bSJoe Perches if (ERROR("SPACING", 31703705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3171b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 31723705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 31733705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3174773647a0SAndy Whitcroft } 3175b34c648bSJoe Perches $line_fixed = 1; 31763705ce5bSJoe Perches } 31773705ce5bSJoe Perches } 31780a920b5bSAndy Whitcroft 31790a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 31809c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 31819c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 31829c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 3183c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 3184c2fdda0dSAndy Whitcroft $op eq '%') 31850a920b5bSAndy Whitcroft { 3186773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 31873705ce5bSJoe Perches if (ERROR("SPACING", 31883705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 3189b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3190b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3191b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3192b34c648bSJoe Perches } 31933705ce5bSJoe Perches $line_fixed = 1; 31943705ce5bSJoe Perches } 31950a920b5bSAndy Whitcroft } 31960a920b5bSAndy Whitcroft 31971f65f947SAndy Whitcroft # A colon needs no spaces before when it is 31981f65f947SAndy Whitcroft # terminating a case value or a label. 31991f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 32001f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 32013705ce5bSJoe Perches if (ERROR("SPACING", 32023705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3203b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 32043705ce5bSJoe Perches $line_fixed = 1; 32053705ce5bSJoe Perches } 32061f65f947SAndy Whitcroft } 32071f65f947SAndy Whitcroft 32080a920b5bSAndy Whitcroft # All the others need spaces both sides. 3209cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 32101f65f947SAndy Whitcroft my $ok = 0; 32111f65f947SAndy Whitcroft 321222f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 32131f65f947SAndy Whitcroft if (($op eq '<' && 32141f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 32151f65f947SAndy Whitcroft ($op eq '>' && 32161f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 32171f65f947SAndy Whitcroft { 32181f65f947SAndy Whitcroft $ok = 1; 32191f65f947SAndy Whitcroft } 32201f65f947SAndy Whitcroft 322184731623SJoe Perches # messages are ERROR, but ?: are CHK 32221f65f947SAndy Whitcroft if ($ok == 0) { 322384731623SJoe Perches my $msg_type = \&ERROR; 322484731623SJoe Perches $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 322584731623SJoe Perches 322684731623SJoe Perches if (&{$msg_type}("SPACING", 32273705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 3228b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3229b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3230b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3231b34c648bSJoe Perches } 32323705ce5bSJoe Perches $line_fixed = 1; 32333705ce5bSJoe Perches } 32340a920b5bSAndy Whitcroft } 323522f2a2efSAndy Whitcroft } 32364a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 32373705ce5bSJoe Perches 32383705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 32393705ce5bSJoe Perches 32403705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 32410a920b5bSAndy Whitcroft } 32423705ce5bSJoe Perches 32433705ce5bSJoe Perches if (($#elements % 2) == 0) { 32443705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 32453705ce5bSJoe Perches } 32463705ce5bSJoe Perches 32473705ce5bSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { 32483705ce5bSJoe Perches $fixed[$linenr - 1] = $fixed_line; 32493705ce5bSJoe Perches } 32503705ce5bSJoe Perches 32513705ce5bSJoe Perches 32520a920b5bSAndy Whitcroft } 32530a920b5bSAndy Whitcroft 3254786b6326SJoe Perches# check for whitespace before a non-naked semicolon 3255d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 3256786b6326SJoe Perches if (WARN("SPACING", 3257786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 3258786b6326SJoe Perches $fix) { 3259786b6326SJoe Perches 1 while $fixed[$linenr - 1] =~ 3260786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 3261786b6326SJoe Perches } 3262786b6326SJoe Perches } 3263786b6326SJoe Perches 3264f0a594c1SAndy Whitcroft# check for multiple assignments 3265f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 3266000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 3267000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 3268f0a594c1SAndy Whitcroft } 3269f0a594c1SAndy Whitcroft 327022f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 327122f2a2efSAndy Whitcroft## # continuation. 327222f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 327322f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 327422f2a2efSAndy Whitcroft## 327522f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 327622f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 327722f2a2efSAndy Whitcroft## my $ln = $line; 327822f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 327922f2a2efSAndy Whitcroft## } 328022f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 3281000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 3282000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 328322f2a2efSAndy Whitcroft## } 328422f2a2efSAndy Whitcroft## } 3285f0a594c1SAndy Whitcroft 32860a920b5bSAndy Whitcroft#need space before brace following if, while, etc 328722f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 328822f2a2efSAndy Whitcroft $line =~ /do{/) { 32893705ce5bSJoe Perches if (ERROR("SPACING", 32903705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 32913705ce5bSJoe Perches $fix) { 3292d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/; 32933705ce5bSJoe Perches } 3294de7d4f0eSAndy Whitcroft } 3295de7d4f0eSAndy Whitcroft 3296c4a62ef9SJoe Perches## # check for blank lines before declarations 3297c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 3298c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 3299c4a62ef9SJoe Perches## WARN("SPACING", 3300c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 3301c4a62ef9SJoe Perches## } 3302c4a62ef9SJoe Perches## 3303c4a62ef9SJoe Perches 3304de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 3305de7d4f0eSAndy Whitcroft# on the line 3306de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 3307d5e616fcSJoe Perches if (ERROR("SPACING", 3308d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 3309d5e616fcSJoe Perches $fix) { 3310d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3311d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 3312d5e616fcSJoe Perches } 33130a920b5bSAndy Whitcroft } 33140a920b5bSAndy Whitcroft 331522f2a2efSAndy Whitcroft# check spacing on square brackets 331622f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 33173705ce5bSJoe Perches if (ERROR("SPACING", 33183705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 33193705ce5bSJoe Perches $fix) { 33203705ce5bSJoe Perches $fixed[$linenr - 1] =~ 33213705ce5bSJoe Perches s/\[\s+/\[/; 33223705ce5bSJoe Perches } 332322f2a2efSAndy Whitcroft } 332422f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 33253705ce5bSJoe Perches if (ERROR("SPACING", 33263705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 33273705ce5bSJoe Perches $fix) { 33283705ce5bSJoe Perches $fixed[$linenr - 1] =~ 33293705ce5bSJoe Perches s/\s+\]/\]/; 33303705ce5bSJoe Perches } 333122f2a2efSAndy Whitcroft } 333222f2a2efSAndy Whitcroft 3333c45dcabdSAndy Whitcroft# check spacing on parentheses 33349c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 33359c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 33363705ce5bSJoe Perches if (ERROR("SPACING", 33373705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 33383705ce5bSJoe Perches $fix) { 33393705ce5bSJoe Perches $fixed[$linenr - 1] =~ 33403705ce5bSJoe Perches s/\(\s+/\(/; 33413705ce5bSJoe Perches } 334222f2a2efSAndy Whitcroft } 334313214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 3344c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 3345c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 33463705ce5bSJoe Perches if (ERROR("SPACING", 33473705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 33483705ce5bSJoe Perches $fix) { 33493705ce5bSJoe Perches $fixed[$linenr - 1] =~ 33503705ce5bSJoe Perches s/\s+\)/\)/; 33513705ce5bSJoe Perches } 335222f2a2efSAndy Whitcroft } 335322f2a2efSAndy Whitcroft 33540a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 33554a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 33560a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 33573705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 33583705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 33593705ce5bSJoe Perches $fix) { 33603705ce5bSJoe Perches $fixed[$linenr - 1] =~ 33613705ce5bSJoe Perches s/^(.)\s+/$1/; 33623705ce5bSJoe Perches } 33630a920b5bSAndy Whitcroft } 33640a920b5bSAndy Whitcroft 3365c45dcabdSAndy Whitcroft# Return is not a function. 3366507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 3367c45dcabdSAndy Whitcroft my $spacing = $1; 3368507e5141SJoe Perches if ($^V && $^V ge 5.10.0 && 3369507e5141SJoe Perches $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) { 3370000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 3371000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 3372c45dcabdSAndy Whitcroft 3373c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 3374000d1cc1SJoe Perches ERROR("SPACING", 3375000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 3376c45dcabdSAndy Whitcroft } 3377c45dcabdSAndy Whitcroft } 3378507e5141SJoe Perches 3379189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 3380189248d8SJoe Perches if ($^V && $^V ge 5.10.0 && 3381189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 3382189248d8SJoe Perches my $openparens = $1; 3383189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 3384189248d8SJoe Perches my $msg = ""; 3385189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 3386189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 3387189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 3388189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 3389189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 3390189248d8SJoe Perches } 3391189248d8SJoe Perches } 3392189248d8SJoe Perches 339353a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 339453a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 339553a3c448SAndy Whitcroft my $name = $1; 339653a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 3397000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 3398000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 339953a3c448SAndy Whitcroft } 340053a3c448SAndy Whitcroft } 3401c45dcabdSAndy Whitcroft 34020a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 34034a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 34043705ce5bSJoe Perches if (ERROR("SPACING", 34053705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 34063705ce5bSJoe Perches $fix) { 34073705ce5bSJoe Perches $fixed[$linenr - 1] =~ 34083705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 34093705ce5bSJoe Perches } 34100a920b5bSAndy Whitcroft } 34110a920b5bSAndy Whitcroft 3412f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 3413f5fe35ddSAndy Whitcroft# statements after the conditional. 3414170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 34153e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 34163e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 34173e469cdcSAndy Whitcroft if (!defined $stat); 3418170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 3419170d3a22SAndy Whitcroft $remain_next, $off_next); 3420170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 3421170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 3422170d3a22SAndy Whitcroft 3423170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 3424170d3a22SAndy Whitcroft # If the statement carries leading newlines, 3425170d3a22SAndy Whitcroft # then count those as offsets. 3426170d3a22SAndy Whitcroft my ($whitespace) = 3427170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 3428170d3a22SAndy Whitcroft my $offset = 3429170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 3430170d3a22SAndy Whitcroft 3431170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 3432170d3a22SAndy Whitcroft $offset} = 1; 3433170d3a22SAndy Whitcroft } 3434170d3a22SAndy Whitcroft } 3435170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 3436c11230f4SJoe Perches defined($stat) && defined($cond) && 3437170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 3438171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 34398905a67cSAndy Whitcroft 3440b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 3441000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 3442000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 34438905a67cSAndy Whitcroft } 34448905a67cSAndy Whitcroft 34458905a67cSAndy Whitcroft # Find out what is on the end of the line after the 34468905a67cSAndy Whitcroft # conditional. 3447773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 34488905a67cSAndy Whitcroft $s =~ s/\n.*//g; 344913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 345053210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 345153210168SAndy Whitcroft $c !~ /}\s*while\s*/) 3452773647a0SAndy Whitcroft { 3453bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 3454bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 3455bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 345642bdf74cSHidetoshi Seto my $stat_real = ''; 3457bb44ad39SAndy Whitcroft 345842bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 345942bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 3460bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 3461bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 3462bb44ad39SAndy Whitcroft } 3463bb44ad39SAndy Whitcroft 3464000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3465000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 34668905a67cSAndy Whitcroft } 34678905a67cSAndy Whitcroft } 34688905a67cSAndy Whitcroft 346913214adfSAndy Whitcroft# Check for bitwise tests written as boolean 347013214adfSAndy Whitcroft if ($line =~ / 347113214adfSAndy Whitcroft (?: 347213214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 347313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 347413214adfSAndy Whitcroft (?:\&\&|\|\|) 347513214adfSAndy Whitcroft | 347613214adfSAndy Whitcroft (?:\&\&|\|\|) 347713214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 347813214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 347913214adfSAndy Whitcroft )/x) 348013214adfSAndy Whitcroft { 3481000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 3482000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 348313214adfSAndy Whitcroft } 348413214adfSAndy Whitcroft 34858905a67cSAndy Whitcroft# if and else should not have general statements after it 348613214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 348713214adfSAndy Whitcroft my $s = $1; 348813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 348913214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 3490000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3491000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 34920a920b5bSAndy Whitcroft } 349313214adfSAndy Whitcroft } 349439667782SAndy Whitcroft# if should not continue a brace 349539667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 3496000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3497000d1cc1SJoe Perches "trailing statements should be on next line\n" . 349839667782SAndy Whitcroft $herecurr); 349939667782SAndy Whitcroft } 3500a1080bf8SAndy Whitcroft# case and default should not have general statements after them 3501a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 3502a1080bf8SAndy Whitcroft $line !~ /\G(?: 35033fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 3504a1080bf8SAndy Whitcroft \s*return\s+ 3505a1080bf8SAndy Whitcroft )/xg) 3506a1080bf8SAndy Whitcroft { 3507000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3508000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 3509a1080bf8SAndy Whitcroft } 35100a920b5bSAndy Whitcroft 35110a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 35120a920b5bSAndy Whitcroft # indent level to be relevant to each other. 35130a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 35140a920b5bSAndy Whitcroft $previndent == $indent) { 3515000d1cc1SJoe Perches ERROR("ELSE_AFTER_BRACE", 3516000d1cc1SJoe Perches "else should follow close brace '}'\n" . $hereprev); 35170a920b5bSAndy Whitcroft } 35180a920b5bSAndy Whitcroft 3519c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 3520c2fdda0dSAndy Whitcroft $previndent == $indent) { 3521c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 3522c2fdda0dSAndy Whitcroft 3523c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 3524c2fdda0dSAndy Whitcroft # conditional. 3525773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 3526c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 3527c2fdda0dSAndy Whitcroft 3528c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 3529000d1cc1SJoe Perches ERROR("WHILE_AFTER_BRACE", 3530000d1cc1SJoe Perches "while should follow close brace '}'\n" . $hereprev); 3531c2fdda0dSAndy Whitcroft } 3532c2fdda0dSAndy Whitcroft } 3533c2fdda0dSAndy Whitcroft 353495e2c602SJoe Perches#Specific variable tests 3535323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 3536323c1260SJoe Perches my $var = $1; 353795e2c602SJoe Perches 353895e2c602SJoe Perches#gcc binary extension 353995e2c602SJoe Perches if ($var =~ /^$Binary$/) { 3540d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 3541d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 3542d5e616fcSJoe Perches $fix) { 3543d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 3544d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3545d5e616fcSJoe Perches s/\b$var\b/$hexval/; 3546d5e616fcSJoe Perches } 354795e2c602SJoe Perches } 354895e2c602SJoe Perches 354995e2c602SJoe Perches#CamelCase 3550807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 3551be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 355222735ce8SJoe Perches#Ignore Page<foo> variants 3553807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 355422735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 35553445686aSJoe Perches $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { 35567e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 35577e781f67SJoe Perches my $word = $1; 35587e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 3559d8b07710SJoe Perches if ($check) { 3560d8b07710SJoe Perches seed_camelcase_includes(); 3561d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 3562d8b07710SJoe Perches seed_camelcase_file($realfile); 3563d8b07710SJoe Perches $camelcase_file_seeded = 1; 3564d8b07710SJoe Perches } 3565d8b07710SJoe Perches } 35667e781f67SJoe Perches if (!defined $camelcase{$word}) { 35677e781f67SJoe Perches $camelcase{$word} = 1; 3568be79794bSJoe Perches CHK("CAMELCASE", 35697e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 35707e781f67SJoe Perches } 3571323c1260SJoe Perches } 3572323c1260SJoe Perches } 35733445686aSJoe Perches } 35740a920b5bSAndy Whitcroft 35750a920b5bSAndy Whitcroft#no spaces allowed after \ in define 3576d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 3577d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 3578d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 3579d5e616fcSJoe Perches $fix) { 3580d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 3581d5e616fcSJoe Perches } 35820a920b5bSAndy Whitcroft } 35830a920b5bSAndy Whitcroft 3584653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 3585c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 3586e09dec48SAndy Whitcroft my $file = "$1.h"; 3587e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 3588e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 3589e09dec48SAndy Whitcroft $realfile ne $checkfile && 35907840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 3591c45dcabdSAndy Whitcroft { 3592e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 3593000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 3594000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3595e09dec48SAndy Whitcroft } else { 3596000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 3597000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3598e09dec48SAndy Whitcroft } 35990a920b5bSAndy Whitcroft } 36000a920b5bSAndy Whitcroft } 36010a920b5bSAndy Whitcroft 3602653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 3603653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 3604cf655043SAndy Whitcroft# in a known good container 3605b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 3606b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 3607d8aaf121SAndy Whitcroft my $ln = $linenr; 3608d8aaf121SAndy Whitcroft my $cnt = $realcnt; 3609c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 3610c45dcabdSAndy Whitcroft my $ctx = ''; 3611c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 3612f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3613f74bd194SAndy Whitcroft $ctx = $dstat; 3614c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 3615a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 3616c45dcabdSAndy Whitcroft 3617f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 3618292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 3619c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 3620c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 3621c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 3622c45dcabdSAndy Whitcroft 3623c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 3624bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 3625bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 3626c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 3627bf30d6edSAndy Whitcroft { 3628c45dcabdSAndy Whitcroft } 3629c45dcabdSAndy Whitcroft 3630e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 3631e45bab8eSAndy Whitcroft while ($dstat =~ s/("X*")\s*$Ident/$1/ || 3632e45bab8eSAndy Whitcroft $dstat =~ s/$Ident\s*("X*")/$1/) 3633e45bab8eSAndy Whitcroft { 3634e45bab8eSAndy Whitcroft } 3635e45bab8eSAndy Whitcroft 3636c45dcabdSAndy Whitcroft my $exceptions = qr{ 3637c45dcabdSAndy Whitcroft $Declare| 3638c45dcabdSAndy Whitcroft module_param_named| 3639a0a0a7a9SKees Cook MODULE_PARM_DESC| 3640c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 3641c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 3642383099fdSAndy Whitcroft __typeof__\(| 364322fd2d3eSStefani Seibold union| 364422fd2d3eSStefani Seibold struct| 3645ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 3646ea71a0a0SAndy Whitcroft ^\"|\"$ 3647c45dcabdSAndy Whitcroft }x; 36485eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 3649f74bd194SAndy Whitcroft if ($dstat ne '' && 3650f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 3651f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 36523cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 3653b9df76acSAndy Whitcroft $dstat !~ /^'X'$/ && # character constants 3654f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 3655f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 3656e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 365772f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 3658f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 3659f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 3660f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 3661f95a7e6aSJoe Perches $dstat !~ /^\({/ && # ({... 3662f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 3663c45dcabdSAndy Whitcroft { 3664f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 3665f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 3666f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 3667f74bd194SAndy Whitcroft 3668f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 3669f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 3670c45dcabdSAndy Whitcroft } 3671c45dcabdSAndy Whitcroft 3672f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 3673f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 3674f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 3675f74bd194SAndy Whitcroft } else { 3676000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 3677f74bd194SAndy Whitcroft "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 3678d8aaf121SAndy Whitcroft } 36790a920b5bSAndy Whitcroft } 36805023d347SJoe Perches 3681481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 36825023d347SJoe Perches 36835023d347SJoe Perches } else { 36845023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 3685481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 3686481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 36875023d347SJoe Perches $line =~ /^\+.*\\$/) { 36885023d347SJoe Perches WARN("LINE_CONTINUATIONS", 36895023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 36905023d347SJoe Perches } 3691653d4876SAndy Whitcroft } 36920a920b5bSAndy Whitcroft 3693b13edf7fSJoe Perches# do {} while (0) macro tests: 3694b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 3695b13edf7fSJoe Perches# macro should not end with a semicolon 3696b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 3697b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 3698b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 3699b13edf7fSJoe Perches my $ln = $linenr; 3700b13edf7fSJoe Perches my $cnt = $realcnt; 3701b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 3702b13edf7fSJoe Perches my $ctx = ''; 3703b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 3704b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 3705b13edf7fSJoe Perches $ctx = $dstat; 3706b13edf7fSJoe Perches 3707b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 3708b13edf7fSJoe Perches 3709b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 3710b13edf7fSJoe Perches my $stmts = $2; 3711b13edf7fSJoe Perches my $semis = $3; 3712b13edf7fSJoe Perches 3713b13edf7fSJoe Perches $ctx =~ s/\n*$//; 3714b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 3715b13edf7fSJoe Perches my $herectx = $here . "\n"; 3716b13edf7fSJoe Perches 3717b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 3718b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 3719b13edf7fSJoe Perches } 3720b13edf7fSJoe Perches 3721ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 3722ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 3723b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 3724b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 3725b13edf7fSJoe Perches } 3726b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 3727b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 3728b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 3729b13edf7fSJoe Perches } 3730b13edf7fSJoe Perches } 3731b13edf7fSJoe Perches } 3732b13edf7fSJoe Perches 3733080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 3734080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 3735080ba929SMike Frysinger# . 3736080ba929SMike Frysinger# ALIGN(...) 3737080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 3738080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 3739000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 3740000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 3741080ba929SMike Frysinger } 3742080ba929SMike Frysinger 3743f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 374413214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 374513214adfSAndy Whitcroft my ($level, $endln, @chunks) = 3746cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 374713214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 3748cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 3749cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 3750aad4f614SJoe Perches my @allowed = (); 3751aad4f614SJoe Perches my $allow = 0; 375213214adfSAndy Whitcroft my $seen = 0; 3753773647a0SAndy Whitcroft my $herectx = $here . "\n"; 3754cf655043SAndy Whitcroft my $ln = $linenr - 1; 375513214adfSAndy Whitcroft for my $chunk (@chunks) { 375613214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 375713214adfSAndy Whitcroft 3758773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 3759773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 3760773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 3761773647a0SAndy Whitcroft 3762aad4f614SJoe Perches $allowed[$allow] = 0; 3763773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 3764773647a0SAndy Whitcroft 3765773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 3766773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 3767773647a0SAndy Whitcroft 3768773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 3769cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 3770cf655043SAndy Whitcroft 3771773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 377213214adfSAndy Whitcroft 377313214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 377413214adfSAndy Whitcroft 3775aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 3776cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3777cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3778aad4f614SJoe Perches $allowed[$allow] = 1; 377913214adfSAndy Whitcroft } 378013214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3781cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3782aad4f614SJoe Perches $allowed[$allow] = 1; 378313214adfSAndy Whitcroft } 3784cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3785cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3786aad4f614SJoe Perches $allowed[$allow] = 1; 378713214adfSAndy Whitcroft } 3788aad4f614SJoe Perches $allow++; 378913214adfSAndy Whitcroft } 3790aad4f614SJoe Perches if ($seen) { 3791aad4f614SJoe Perches my $sum_allowed = 0; 3792aad4f614SJoe Perches foreach (@allowed) { 3793aad4f614SJoe Perches $sum_allowed += $_; 3794aad4f614SJoe Perches } 3795aad4f614SJoe Perches if ($sum_allowed == 0) { 3796000d1cc1SJoe Perches WARN("BRACES", 3797000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 3798aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 3799aad4f614SJoe Perches $seen != $allow) { 3800aad4f614SJoe Perches CHK("BRACES", 3801aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 3802aad4f614SJoe Perches } 380313214adfSAndy Whitcroft } 380413214adfSAndy Whitcroft } 380513214adfSAndy Whitcroft } 3806773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 380713214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 3808cf655043SAndy Whitcroft my $allowed = 0; 3809f0a594c1SAndy Whitcroft 3810cf655043SAndy Whitcroft # Check the pre-context. 3811cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 3812cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 3813cf655043SAndy Whitcroft $allowed = 1; 3814f0a594c1SAndy Whitcroft } 3815773647a0SAndy Whitcroft 3816773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 3817773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 3818773647a0SAndy Whitcroft 3819cf655043SAndy Whitcroft # Check the condition. 3820cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 3821773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 3822cf655043SAndy Whitcroft if (defined $cond) { 3823773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3824cf655043SAndy Whitcroft } 3825cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3826cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3827cf655043SAndy Whitcroft $allowed = 1; 3828cf655043SAndy Whitcroft } 3829cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3830cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3831cf655043SAndy Whitcroft $allowed = 1; 3832cf655043SAndy Whitcroft } 3833cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3834cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3835cf655043SAndy Whitcroft $allowed = 1; 3836cf655043SAndy Whitcroft } 3837cf655043SAndy Whitcroft # Check the post-context. 3838cf655043SAndy Whitcroft if (defined $chunks[1]) { 3839cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 3840cf655043SAndy Whitcroft if (defined $cond) { 3841773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3842cf655043SAndy Whitcroft } 3843cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 3844cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 3845cf655043SAndy Whitcroft $allowed = 1; 3846cf655043SAndy Whitcroft } 3847cf655043SAndy Whitcroft } 3848cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 384969932487SJustin P. Mattock my $herectx = $here . "\n"; 3850f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 3851cf655043SAndy Whitcroft 3852f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 385369932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 3854cf655043SAndy Whitcroft } 3855cf655043SAndy Whitcroft 3856000d1cc1SJoe Perches WARN("BRACES", 3857000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 3858f0a594c1SAndy Whitcroft } 3859f0a594c1SAndy Whitcroft } 3860f0a594c1SAndy Whitcroft 38610979ae66SJoe Perches# check for unnecessary blank lines around braces 386277b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 38630979ae66SJoe Perches CHK("BRACES", 38640979ae66SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 38650979ae66SJoe Perches } 386677b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 38670979ae66SJoe Perches CHK("BRACES", 38680979ae66SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 38690979ae66SJoe Perches } 38700979ae66SJoe Perches 38714a0df2efSAndy Whitcroft# no volatiles please 38726c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 38736c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 3874000d1cc1SJoe Perches WARN("VOLATILE", 3875000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 38764a0df2efSAndy Whitcroft } 38774a0df2efSAndy Whitcroft 387800df344fSAndy Whitcroft# warn about #if 0 3879c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 3880000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 3881000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 3882de7d4f0eSAndy Whitcroft $herecurr); 38834a0df2efSAndy Whitcroft } 38844a0df2efSAndy Whitcroft 388503df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 388603df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 388703df4b51SAndy Whitcroft my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 388803df4b51SAndy Whitcroft if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 388903df4b51SAndy Whitcroft WARN('NEEDLESS_IF', 389003df4b51SAndy Whitcroft "$1(NULL) is safe this check is probably not required\n" . $hereprev); 38914c432a8fSGreg Kroah-Hartman } 38924c432a8fSGreg Kroah-Hartman } 3893f0a594c1SAndy Whitcroft 38948716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 38958716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 38968716de38SJoe Perches my $attr = $1; 38978716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 38988716de38SJoe Perches my $ptr = $1; 38998716de38SJoe Perches my $var = $2; 39008716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 39018716de38SJoe Perches ERROR("MISPLACED_INIT", 39028716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 39038716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 39048716de38SJoe Perches WARN("MISPLACED_INIT", 39058716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 39068716de38SJoe Perches $fix) { 39078716de38SJoe 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; 39088716de38SJoe Perches } 39098716de38SJoe Perches } 39108716de38SJoe Perches } 39118716de38SJoe Perches 3912e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 3913e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 3914e970b884SJoe Perches my $attr = $1; 3915e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 3916e970b884SJoe Perches my $attr_prefix = $1; 3917e970b884SJoe Perches my $attr_type = $2; 3918e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 3919e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 3920e970b884SJoe Perches $fix) { 3921e970b884SJoe Perches $fixed[$linenr - 1] =~ 3922e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 3923e970b884SJoe Perches } 3924e970b884SJoe Perches } 3925e970b884SJoe Perches 3926e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 3927e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 3928e970b884SJoe Perches my $attr = $1; 3929e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 3930e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 3931e970b884SJoe Perches $fix) { 3932e970b884SJoe Perches my $lead = $fixed[$linenr - 1] =~ 3933e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 3934e970b884SJoe Perches $lead = rtrim($1); 3935e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 3936e970b884SJoe Perches $lead = "${lead}const "; 3937e970b884SJoe Perches $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/; 3938e970b884SJoe Perches } 3939e970b884SJoe Perches } 3940e970b884SJoe Perches 3941fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 3942fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 3943fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 3944fbdb8138SJoe Perches my $constant_func = $1; 3945fbdb8138SJoe Perches my $func = $constant_func; 3946fbdb8138SJoe Perches $func =~ s/^__constant_//; 3947fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 3948fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 3949fbdb8138SJoe Perches $fix) { 3950fbdb8138SJoe Perches $fixed[$linenr - 1] =~ s/\b$constant_func\b/$func/g; 3951fbdb8138SJoe Perches } 3952fbdb8138SJoe Perches } 3953fbdb8138SJoe Perches 39541a15a250SPatrick Pannuto# prefer usleep_range over udelay 395537581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 395643c1d77cSJoe Perches my $delay = $1; 39571a15a250SPatrick Pannuto # ignore udelay's < 10, however 395843c1d77cSJoe Perches if (! ($delay < 10) ) { 3959000d1cc1SJoe Perches CHK("USLEEP_RANGE", 396043c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 396143c1d77cSJoe Perches } 396243c1d77cSJoe Perches if ($delay > 2000) { 396343c1d77cSJoe Perches WARN("LONG_UDELAY", 396443c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 39651a15a250SPatrick Pannuto } 39661a15a250SPatrick Pannuto } 39671a15a250SPatrick Pannuto 396809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 396909ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 397009ef8725SPatrick Pannuto if ($1 < 20) { 3971000d1cc1SJoe Perches WARN("MSLEEP", 397243c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 397309ef8725SPatrick Pannuto } 397409ef8725SPatrick Pannuto } 397509ef8725SPatrick Pannuto 397636ec1939SJoe Perches# check for comparisons of jiffies 397736ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 397836ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 397936ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 398036ec1939SJoe Perches } 398136ec1939SJoe Perches 39829d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 39839d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 39849d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 39859d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 39869d7a34a5SJoe Perches } 39879d7a34a5SJoe Perches 398800df344fSAndy Whitcroft# warn about #ifdefs in C files 3989c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 399000df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 399100df344fSAndy Whitcroft# print "$herecurr"; 399200df344fSAndy Whitcroft# $clean = 0; 399300df344fSAndy Whitcroft# } 399400df344fSAndy Whitcroft 399522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 3996c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 39973705ce5bSJoe Perches if (ERROR("SPACING", 39983705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 39993705ce5bSJoe Perches $fix) { 40003705ce5bSJoe Perches $fixed[$linenr - 1] =~ 40013705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 40023705ce5bSJoe Perches } 40033705ce5bSJoe Perches 400422f2a2efSAndy Whitcroft } 400522f2a2efSAndy Whitcroft 40064a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 4007171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 4008171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 40094a0df2efSAndy Whitcroft my $which = $1; 40104a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4011000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 4012000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 40134a0df2efSAndy Whitcroft } 40144a0df2efSAndy Whitcroft } 40154a0df2efSAndy Whitcroft# check for memory barriers without a comment. 40164a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 40174a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4018c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 4019000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 40204a0df2efSAndy Whitcroft } 40214a0df2efSAndy Whitcroft } 40224a0df2efSAndy Whitcroft# check of hardware specific defines 4023c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 4024000d1cc1SJoe Perches CHK("ARCH_DEFINES", 4025000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 40260a920b5bSAndy Whitcroft } 4027653d4876SAndy Whitcroft 4028d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 4029d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 4030000d1cc1SJoe Perches WARN("STORAGE_CLASS", 4031000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 4032d4977c78STobias Klauser } 4033d4977c78STobias Klauser 4034de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 4035de7d4f0eSAndy Whitcroft# storage class and type. 40369c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 40379c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 4038000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 4039000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 4040de7d4f0eSAndy Whitcroft } 4041de7d4f0eSAndy Whitcroft 40428905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 40432b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 40442b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 4045d5e616fcSJoe Perches if (WARN("INLINE", 4046d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 4047d5e616fcSJoe Perches $fix) { 4048d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/; 4049d5e616fcSJoe Perches 4050d5e616fcSJoe Perches } 40518905a67cSAndy Whitcroft } 40528905a67cSAndy Whitcroft 40533d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 40542b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 40552b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 4056000d1cc1SJoe Perches WARN("PREFER_PACKED", 4057000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 40583d130fd0SJoe Perches } 40593d130fd0SJoe Perches 406039b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 40612b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 40622b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 4063000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 4064000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 406539b7e287SJoe Perches } 406639b7e287SJoe Perches 40675f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 40682b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 40692b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 4070d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 4071d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 4072d5e616fcSJoe Perches $fix) { 4073d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 4074d5e616fcSJoe Perches 4075d5e616fcSJoe Perches } 40765f14d3bdSJoe Perches } 40775f14d3bdSJoe Perches 40786061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 40792b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 40802b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 4081d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 4082d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 4083d5e616fcSJoe Perches $fix) { 4084d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 4085d5e616fcSJoe Perches } 40866061d949SJoe Perches } 40876061d949SJoe Perches 40888f53a9b8SJoe Perches# check for sizeof(&) 40898f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 4090000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 4091000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 40928f53a9b8SJoe Perches } 40938f53a9b8SJoe Perches 409466c80b60SJoe Perches# check for sizeof without parenthesis 409566c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 4096d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 4097d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 4098d5e616fcSJoe Perches $fix) { 4099d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 4100d5e616fcSJoe Perches } 410166c80b60SJoe Perches } 410266c80b60SJoe Perches 4103428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 4104428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 4105000d1cc1SJoe Perches WARN("LINE_CONTINUATIONS", 4106000d1cc1SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 4107428e2fdcSJoe Perches } 4108428e2fdcSJoe Perches 410988982feaSJoe Perches# check for struct spinlock declarations 411088982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 411188982feaSJoe Perches WARN("USE_SPINLOCK_T", 411288982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 411388982feaSJoe Perches } 411488982feaSJoe Perches 4115a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 411606668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 4117a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 411806668727SJoe Perches if ($fmt ne "" && $fmt !~ /[^\\]\%/) { 4119d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 4120d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 4121d5e616fcSJoe Perches $fix) { 4122d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/; 4123d5e616fcSJoe Perches } 4124a6962d72SJoe Perches } 4125a6962d72SJoe Perches } 4126a6962d72SJoe Perches 4127554e165cSAndy Whitcroft# Check for misused memsets 4128d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 4129d1fe9c09SJoe Perches defined $stat && 4130d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 4131554e165cSAndy Whitcroft 4132d7c76ba7SJoe Perches my $ms_addr = $2; 4133d1fe9c09SJoe Perches my $ms_val = $7; 4134d1fe9c09SJoe Perches my $ms_size = $12; 4135d7c76ba7SJoe Perches 4136554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 4137554e165cSAndy Whitcroft ERROR("MEMSET", 4138d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 4139554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 4140554e165cSAndy Whitcroft WARN("MEMSET", 4141d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 4142d7c76ba7SJoe Perches } 4143d7c76ba7SJoe Perches } 4144d7c76ba7SJoe Perches 414598a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 414698a9bba5SJoe Perches if ($^V && $^V ge 5.10.0 && 414798a9bba5SJoe Perches $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { 414898a9bba5SJoe Perches if (WARN("PREFER_ETHER_ADDR_COPY", 414998a9bba5SJoe Perches "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && 415098a9bba5SJoe Perches $fix) { 415198a9bba5SJoe Perches $fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 415298a9bba5SJoe Perches } 415398a9bba5SJoe Perches } 415498a9bba5SJoe Perches 4155d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 4156d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 4157d1fe9c09SJoe Perches defined $stat && 4158d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 4159d1fe9c09SJoe Perches if (defined $2 || defined $7) { 4160d7c76ba7SJoe Perches my $call = $1; 4161d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 4162d7c76ba7SJoe Perches my $arg1 = $3; 4163d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 4164d1fe9c09SJoe Perches my $arg2 = $8; 4165d7c76ba7SJoe Perches my $cast; 4166d7c76ba7SJoe Perches 4167d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 4168d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 4169d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 4170d7c76ba7SJoe Perches $cast = $cast1; 4171d7c76ba7SJoe Perches } else { 4172d7c76ba7SJoe Perches $cast = $cast2; 4173d7c76ba7SJoe Perches } 4174d7c76ba7SJoe Perches WARN("MINMAX", 4175d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 4176554e165cSAndy Whitcroft } 4177554e165cSAndy Whitcroft } 4178554e165cSAndy Whitcroft 41794a273195SJoe Perches# check usleep_range arguments 41804a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 41814a273195SJoe Perches defined $stat && 41824a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 41834a273195SJoe Perches my $min = $1; 41844a273195SJoe Perches my $max = $7; 41854a273195SJoe Perches if ($min eq $max) { 41864a273195SJoe Perches WARN("USLEEP_RANGE", 41874a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 41884a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 41894a273195SJoe Perches $min > $max) { 41904a273195SJoe Perches WARN("USLEEP_RANGE", 41914a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 41924a273195SJoe Perches } 41934a273195SJoe Perches } 41944a273195SJoe Perches 4195823b794cSJoe Perches# check for naked sscanf 4196823b794cSJoe Perches if ($^V && $^V ge 5.10.0 && 4197823b794cSJoe Perches defined $stat && 41986c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 4199823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 4200823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 4201823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 4202823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 4203823b794cSJoe Perches $lc = $lc + $linenr; 4204823b794cSJoe Perches my $stat_real = raw_line($linenr, 0); 4205823b794cSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 4206823b794cSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 4207823b794cSJoe Perches } 4208823b794cSJoe Perches WARN("NAKED_SSCANF", 4209823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 4210823b794cSJoe Perches } 4211823b794cSJoe Perches 421270dc8a48SJoe Perches# check for new externs in .h files. 421370dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 421470dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 4215d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 421670dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 421770dc8a48SJoe Perches $fix) { 421870dc8a48SJoe Perches $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 421970dc8a48SJoe Perches } 422070dc8a48SJoe Perches } 422170dc8a48SJoe Perches 4222de7d4f0eSAndy Whitcroft# check for new externs in .c files. 4223171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 4224c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 4225171ae1a4SAndy Whitcroft { 4226c45dcabdSAndy Whitcroft my $function_name = $1; 4227c45dcabdSAndy Whitcroft my $paren_space = $2; 4228171ae1a4SAndy Whitcroft 4229171ae1a4SAndy Whitcroft my $s = $stat; 4230171ae1a4SAndy Whitcroft if (defined $cond) { 4231171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 4232171ae1a4SAndy Whitcroft } 4233c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 4234c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 4235c45dcabdSAndy Whitcroft { 4236000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4237000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4238de7d4f0eSAndy Whitcroft } 4239de7d4f0eSAndy Whitcroft 4240171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 4241000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 4242000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 4243171ae1a4SAndy Whitcroft } 42449c9ba34eSAndy Whitcroft 42459c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 42469c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 42479c9ba34eSAndy Whitcroft { 4248000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4249000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4250171ae1a4SAndy Whitcroft } 4251171ae1a4SAndy Whitcroft 4252de7d4f0eSAndy Whitcroft# checks for new __setup's 4253de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 4254de7d4f0eSAndy Whitcroft my $name = $1; 4255de7d4f0eSAndy Whitcroft 4256de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 4257000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 4258000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 4259de7d4f0eSAndy Whitcroft } 4260653d4876SAndy Whitcroft } 42619c0ca6f9SAndy Whitcroft 42629c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 4263caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 4264000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 4265000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 42669c0ca6f9SAndy Whitcroft } 426713214adfSAndy Whitcroft 4268a640d25cSJoe Perches# alloc style 4269a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 4270a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 4271a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 4272a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 4273a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 4274a640d25cSJoe Perches } 4275a640d25cSJoe Perches 4276972fdea2SJoe Perches# check for krealloc arg reuse 4277972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 4278972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 4279972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 4280972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 4281972fdea2SJoe Perches } 4282972fdea2SJoe Perches 42835ce59ae0SJoe Perches# check for alloc argument mismatch 42845ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 42855ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 42865ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 42875ce59ae0SJoe Perches } 42885ce59ae0SJoe Perches 42897e4915e7SDavid Rientjes# check for GFP_NOWAIT use 42907e4915e7SDavid Rientjes if ($line =~ /\b__GFP_NOFAIL\b/) { 42917e4915e7SDavid Rientjes WARN("__GFP_NOFAIL", 42927e4915e7SDavid Rientjes "Use of __GFP_NOFAIL is deprecated, no new users should be added\n" . $herecurr); 42937e4915e7SDavid Rientjes } 42947e4915e7SDavid Rientjes 4295caf2a54fSJoe Perches# check for multiple semicolons 4296caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 4297d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 4298d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 4299d5e616fcSJoe Perches $fix) { 4300d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g; 4301d5e616fcSJoe Perches } 4302d1e2ad07SJoe Perches } 4303d1e2ad07SJoe Perches 4304c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch 4305c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 4306c34c09a8SJoe Perches my $has_break = 0; 4307c34c09a8SJoe Perches my $has_statement = 0; 4308c34c09a8SJoe Perches my $count = 0; 4309c34c09a8SJoe Perches my $prevline = $linenr; 4310c34c09a8SJoe Perches while ($prevline > 1 && $count < 3 && !$has_break) { 4311c34c09a8SJoe Perches $prevline--; 4312c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 4313c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 4314c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 4315c34c09a8SJoe Perches next if ($fline =~ /^\-/); 4316c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 4317c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 4318c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 4319c34c09a8SJoe Perches $has_statement = 1; 4320c34c09a8SJoe Perches $count++; 4321c34c09a8SJoe Perches $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); 4322c34c09a8SJoe Perches } 4323c34c09a8SJoe Perches if (!$has_break && $has_statement) { 4324c34c09a8SJoe Perches WARN("MISSING_BREAK", 4325c34c09a8SJoe Perches "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); 4326c34c09a8SJoe Perches } 4327c34c09a8SJoe Perches } 4328c34c09a8SJoe Perches 4329d1e2ad07SJoe Perches# check for switch/default statements without a break; 4330d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 4331d1e2ad07SJoe Perches defined $stat && 4332d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 4333d1e2ad07SJoe Perches my $ctx = ''; 4334d1e2ad07SJoe Perches my $herectx = $here . "\n"; 4335d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 4336d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4337d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4338d1e2ad07SJoe Perches } 4339d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 4340d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 4341caf2a54fSJoe Perches } 4342caf2a54fSJoe Perches 434313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 4344d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 4345d5e616fcSJoe Perches if (WARN("USE_FUNC", 4346d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 4347d5e616fcSJoe Perches $fix) { 4348d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g; 4349d5e616fcSJoe Perches } 435013214adfSAndy Whitcroft } 4351773647a0SAndy Whitcroft 43522c92488aSJoe Perches# check for use of yield() 43532c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 43542c92488aSJoe Perches WARN("YIELD", 43552c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 43562c92488aSJoe Perches } 43572c92488aSJoe Perches 4358179f8f40SJoe Perches# check for comparisons against true and false 4359179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 4360179f8f40SJoe Perches my $lead = $1; 4361179f8f40SJoe Perches my $arg = $2; 4362179f8f40SJoe Perches my $test = $3; 4363179f8f40SJoe Perches my $otype = $4; 4364179f8f40SJoe Perches my $trail = $5; 4365179f8f40SJoe Perches my $op = "!"; 4366179f8f40SJoe Perches 4367179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 4368179f8f40SJoe Perches 4369179f8f40SJoe Perches my $type = lc($otype); 4370179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 4371179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 4372179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 4373179f8f40SJoe Perches $op = ""; 4374179f8f40SJoe Perches } 4375179f8f40SJoe Perches 4376179f8f40SJoe Perches CHK("BOOL_COMPARISON", 4377179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 4378179f8f40SJoe Perches 4379179f8f40SJoe Perches## maybe suggesting a correct construct would better 4380179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 4381179f8f40SJoe Perches 4382179f8f40SJoe Perches } 4383179f8f40SJoe Perches } 4384179f8f40SJoe Perches 43854882720bSThomas Gleixner# check for semaphores initialized locked 43864882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 4387000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 4388000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 4389773647a0SAndy Whitcroft } 43906712d858SJoe Perches 439167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 439267d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 4393000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 439467d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 4395773647a0SAndy Whitcroft } 43966712d858SJoe Perches 4397f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 4398f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 4399000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 4400000d1cc1SJoe Perches "please use device_initcall() instead of __initcall()\n" . $herecurr); 4401f3db6639SMichael Ellerman } 44026712d858SJoe Perches 440379404849SEmese Revfy# check for various ops structs, ensure they are const. 440479404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 440579404849SEmese Revfy address_space_operations| 440679404849SEmese Revfy backlight_ops| 440779404849SEmese Revfy block_device_operations| 440879404849SEmese Revfy dentry_operations| 440979404849SEmese Revfy dev_pm_ops| 441079404849SEmese Revfy dma_map_ops| 441179404849SEmese Revfy extent_io_ops| 441279404849SEmese Revfy file_lock_operations| 441379404849SEmese Revfy file_operations| 441479404849SEmese Revfy hv_ops| 441579404849SEmese Revfy ide_dma_ops| 441679404849SEmese Revfy intel_dvo_dev_ops| 441779404849SEmese Revfy item_operations| 441879404849SEmese Revfy iwl_ops| 441979404849SEmese Revfy kgdb_arch| 442079404849SEmese Revfy kgdb_io| 442179404849SEmese Revfy kset_uevent_ops| 442279404849SEmese Revfy lock_manager_operations| 442379404849SEmese Revfy microcode_ops| 442479404849SEmese Revfy mtrr_ops| 442579404849SEmese Revfy neigh_ops| 442679404849SEmese Revfy nlmsvc_binding| 442779404849SEmese Revfy pci_raw_ops| 442879404849SEmese Revfy pipe_buf_operations| 442979404849SEmese Revfy platform_hibernation_ops| 443079404849SEmese Revfy platform_suspend_ops| 443179404849SEmese Revfy proto_ops| 443279404849SEmese Revfy rpc_pipe_ops| 443379404849SEmese Revfy seq_operations| 443479404849SEmese Revfy snd_ac97_build_ops| 443579404849SEmese Revfy soc_pcmcia_socket_ops| 443679404849SEmese Revfy stacktrace_ops| 443779404849SEmese Revfy sysfs_ops| 443879404849SEmese Revfy tty_operations| 443979404849SEmese Revfy usb_mon_operations| 444079404849SEmese Revfy wd_ops}x; 44416903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 444279404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 4443000d1cc1SJoe Perches WARN("CONST_STRUCT", 4444000d1cc1SJoe Perches "struct $1 should normally be const\n" . 44456903ffb2SAndy Whitcroft $herecurr); 44462b6db5cbSAndy Whitcroft } 4447773647a0SAndy Whitcroft 4448773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 4449773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 4450773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 4451c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 4452c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 4453171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 4454171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 4455171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 4456773647a0SAndy Whitcroft { 4457000d1cc1SJoe Perches WARN("NR_CPUS", 4458000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 4459773647a0SAndy Whitcroft } 44609c9ba34eSAndy Whitcroft 446152ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 446252ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 446352ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 446452ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 446552ea8506SJoe Perches } 446652ea8506SJoe Perches 44679c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 44689c9ba34eSAndy Whitcroft my $string; 44699c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 44709c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 44712a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 44729c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 4473000d1cc1SJoe Perches WARN("PRINTF_L", 4474000d1cc1SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 44759c9ba34eSAndy Whitcroft last; 44769c9ba34eSAndy Whitcroft } 44779c9ba34eSAndy Whitcroft } 4478691d77b6SAndy Whitcroft 4479691d77b6SAndy Whitcroft# whine mightly about in_atomic 4480691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 4481691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 4482000d1cc1SJoe Perches ERROR("IN_ATOMIC", 4483000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 4484f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 4485000d1cc1SJoe Perches WARN("IN_ATOMIC", 4486000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 4487691d77b6SAndy Whitcroft } 4488691d77b6SAndy Whitcroft } 44891704f47bSPeter Zijlstra 44901704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 44911704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 44921704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 44931704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 44941704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 44951704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 4496000d1cc1SJoe Perches ERROR("LOCKDEP", 4497000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 44981704f47bSPeter Zijlstra } 44991704f47bSPeter Zijlstra } 450088f8831cSDave Jones 450188f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 450288f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 4503000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 4504000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 450588f8831cSDave Jones } 45062435880fSJoe Perches 45072435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 45082435880fSJoe Perches my $func = $entry->[0]; 45092435880fSJoe Perches my $arg_pos = $entry->[1]; 45102435880fSJoe Perches 45112435880fSJoe Perches my $skip_args = ""; 45122435880fSJoe Perches if ($arg_pos > 1) { 45132435880fSJoe Perches $arg_pos--; 45142435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 45152435880fSJoe Perches } 45162435880fSJoe Perches my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; 45172435880fSJoe Perches if ($^V && $^V ge 5.10.0 && 45182435880fSJoe Perches $line =~ /$test/) { 45192435880fSJoe Perches my $val = $1; 45202435880fSJoe Perches $val = $6 if ($skip_args ne ""); 45212435880fSJoe Perches 45221727cc70SJoe Perches if ($val !~ /^0$/ && 45231727cc70SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 45241727cc70SJoe Perches length($val) ne 4)) { 45252435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 45261727cc70SJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 45272435880fSJoe Perches } 45282435880fSJoe Perches } 45292435880fSJoe Perches } 453013214adfSAndy Whitcroft } 453113214adfSAndy Whitcroft 453213214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 453313214adfSAndy Whitcroft # so just keep quiet. 453413214adfSAndy Whitcroft if ($#rawlines == -1) { 453513214adfSAndy Whitcroft exit(0); 45360a920b5bSAndy Whitcroft } 45370a920b5bSAndy Whitcroft 45388905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 45398905a67cSAndy Whitcroft # things that appear to be patches. 45408905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 45418905a67cSAndy Whitcroft exit(0); 45428905a67cSAndy Whitcroft } 45438905a67cSAndy Whitcroft 45448905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 45458905a67cSAndy Whitcroft # just keep quiet. 45468905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 45478905a67cSAndy Whitcroft exit(0); 45488905a67cSAndy Whitcroft } 45498905a67cSAndy Whitcroft 45508905a67cSAndy Whitcroft if (!$is_patch) { 4551000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 4552000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 45530a920b5bSAndy Whitcroft } 45540a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 4555000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 4556000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 45570a920b5bSAndy Whitcroft } 45580a920b5bSAndy Whitcroft 4559f0a594c1SAndy Whitcroft print report_dump(); 456013214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 456113214adfSAndy Whitcroft print "$filename " if ($summary_file); 45626c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 45636c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 45646c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 45658905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 45666c72ffaaSAndy Whitcroft } 45678905a67cSAndy Whitcroft 4568d2c0a235SAndy Whitcroft if ($quiet == 0) { 4569d1fe9c09SJoe Perches 4570d1fe9c09SJoe Perches if ($^V lt 5.10.0) { 4571d1fe9c09SJoe Perches print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); 4572d1fe9c09SJoe Perches print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); 4573d1fe9c09SJoe Perches } 4574d1fe9c09SJoe Perches 4575d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 4576d2c0a235SAndy Whitcroft # then suggest that. 4577d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 4578d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 4579d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 4580b0781216SMike Frysinger $rpt_cleaners = 0; 4581d2c0a235SAndy Whitcroft } 4582d2c0a235SAndy Whitcroft } 4583d2c0a235SAndy Whitcroft 458491bfe484SJoe Perches hash_show_words(\%use_type, "Used"); 458591bfe484SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 4586000d1cc1SJoe Perches 45873705ce5bSJoe Perches if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { 45889624b8d6SJoe Perches my $newfile = $filename; 45899624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 45903705ce5bSJoe Perches my $linecount = 0; 45913705ce5bSJoe Perches my $f; 45923705ce5bSJoe Perches 45933705ce5bSJoe Perches open($f, '>', $newfile) 45943705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 45953705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 45963705ce5bSJoe Perches $linecount++; 45973705ce5bSJoe Perches if ($file) { 45983705ce5bSJoe Perches if ($linecount > 3) { 45993705ce5bSJoe Perches $fixed_line =~ s/^\+//; 46003705ce5bSJoe Perches print $f $fixed_line. "\n"; 46013705ce5bSJoe Perches } 46023705ce5bSJoe Perches } else { 46033705ce5bSJoe Perches print $f $fixed_line . "\n"; 46043705ce5bSJoe Perches } 46053705ce5bSJoe Perches } 46063705ce5bSJoe Perches close($f); 46073705ce5bSJoe Perches 46083705ce5bSJoe Perches if (!$quiet) { 46093705ce5bSJoe Perches print << "EOM"; 46103705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 46113705ce5bSJoe Perches 46123705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 46133705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 46143705ce5bSJoe Perches 46153705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 46163705ce5bSJoe PerchesNo warranties, expressed or implied... 46173705ce5bSJoe Perches 46183705ce5bSJoe PerchesEOM 46193705ce5bSJoe Perches } 46203705ce5bSJoe Perches } 46213705ce5bSJoe Perches 46220a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 4623c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 46240a920b5bSAndy Whitcroft } 46250a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 4626000d1cc1SJoe Perches print << "EOM"; 4627000d1cc1SJoe Perches$vname has style problems, please review. 4628000d1cc1SJoe Perches 4629000d1cc1SJoe PerchesIf any of these errors are false positives, please report 4630000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 4631000d1cc1SJoe PerchesEOM 46320a920b5bSAndy Whitcroft } 463313214adfSAndy Whitcroft 46340a920b5bSAndy Whitcroft return $clean; 46350a920b5bSAndy Whitcroft} 4636