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; 28491cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 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 30791cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 30891cb5195SJoe Perches 3098905a67cSAndy Whitcroftour $NonptrType; 3108716de38SJoe Perchesour $NonptrTypeWithAttr; 3118905a67cSAndy Whitcroftour $Type; 3128905a67cSAndy Whitcroftour $Declare; 3138905a67cSAndy Whitcroft 31415662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 31515662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 316171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 317171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 318171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 319171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 320171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 321171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 322171ae1a4SAndy Whitcroft}x; 323171ae1a4SAndy Whitcroft 32415662b3eSJoe Perchesour $UTF8 = qr{ 32515662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 32615662b3eSJoe Perches | $NON_ASCII_UTF8 32715662b3eSJoe Perches}x; 32815662b3eSJoe Perches 3298ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 330fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 3318ed22cadSAndy Whitcroft atomic_t 3328ed22cadSAndy Whitcroft)}; 3338ed22cadSAndy Whitcroft 334691e669bSJoe Perchesour $logFunctions = qr{(?x: 3356e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 3367d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 3376e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 338b0531722SJoe Perches panic| 33906668727SJoe Perches MODULE_[A-Z_]+| 34006668727SJoe Perches seq_vprintf|seq_printf|seq_puts 341691e669bSJoe Perches)}; 342691e669bSJoe Perches 34320112475SJoe Perchesour $signature_tags = qr{(?xi: 34420112475SJoe Perches Signed-off-by:| 34520112475SJoe Perches Acked-by:| 34620112475SJoe Perches Tested-by:| 34720112475SJoe Perches Reviewed-by:| 34820112475SJoe Perches Reported-by:| 3498543ae12SMugunthan V N Suggested-by:| 35020112475SJoe Perches To:| 35120112475SJoe Perches Cc: 35220112475SJoe Perches)}; 35320112475SJoe Perches 3548905a67cSAndy Whitcroftour @typeList = ( 3558905a67cSAndy Whitcroft qr{void}, 356c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 357c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 358c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 359c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 360c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 361c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 362c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 3638905a67cSAndy Whitcroft qr{unsigned}, 3648905a67cSAndy Whitcroft qr{float}, 3658905a67cSAndy Whitcroft qr{double}, 3668905a67cSAndy Whitcroft qr{bool}, 3678905a67cSAndy Whitcroft qr{struct\s+$Ident}, 3688905a67cSAndy Whitcroft qr{union\s+$Ident}, 3698905a67cSAndy Whitcroft qr{enum\s+$Ident}, 3708905a67cSAndy Whitcroft qr{${Ident}_t}, 3718905a67cSAndy Whitcroft qr{${Ident}_handler}, 3728905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 3738905a67cSAndy Whitcroft); 3748716de38SJoe Perchesour @typeListWithAttr = ( 3758716de38SJoe Perches @typeList, 3768716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 3778716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 3788716de38SJoe Perches); 3798716de38SJoe Perches 380c45dcabdSAndy Whitcroftour @modifierList = ( 381c45dcabdSAndy Whitcroft qr{fastcall}, 382c45dcabdSAndy Whitcroft); 3838905a67cSAndy Whitcroft 3842435880fSJoe Perchesour @mode_permission_funcs = ( 3852435880fSJoe Perches ["module_param", 3], 3862435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 3872435880fSJoe Perches ["module_param_array_named", 5], 3882435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 3892435880fSJoe Perches ["proc_create(?:_data|)", 2], 3902435880fSJoe Perches ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], 3912435880fSJoe Perches); 3922435880fSJoe Perches 393515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 394515a235eSJoe Perchesour $mode_perms_search = ""; 395515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 396515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 397515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 398515a235eSJoe Perches} 399515a235eSJoe Perches 400*3f7bac03SJoe Perchesour $declaration_macros = qr{(?x: 401*3f7bac03SJoe Perches (?:$Storage\s+)?(?:DECLARE|DEFINE)_[A-Z]+\s*\(| 402*3f7bac03SJoe Perches (?:$Storage\s+)?LIST_HEAD\s*\( 403*3f7bac03SJoe Perches)}; 404*3f7bac03SJoe Perches 4057840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 4067840a94cSWolfram Sang irq| 4077840a94cSWolfram Sang memory 4087840a94cSWolfram Sang)}; 4097840a94cSWolfram Sang# memory.h: ARM has a custom one 4107840a94cSWolfram Sang 4118905a67cSAndy Whitcroftsub build_types { 412d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 413d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 4148716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 415c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 4168905a67cSAndy Whitcroft $NonptrType = qr{ 417d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 418cf655043SAndy Whitcroft (?: 4196b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 4208ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 421c45dcabdSAndy Whitcroft (?:${all}\b) 422cf655043SAndy Whitcroft ) 423c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 4248905a67cSAndy Whitcroft }x; 4258716de38SJoe Perches $NonptrTypeWithAttr = qr{ 4268716de38SJoe Perches (?:$Modifier\s+|const\s+)* 4278716de38SJoe Perches (?: 4288716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 4298716de38SJoe Perches (?:$typeTypedefs\b)| 4308716de38SJoe Perches (?:${allWithAttr}\b) 4318716de38SJoe Perches ) 4328716de38SJoe Perches (?:\s+$Modifier|\s+const)* 4338716de38SJoe Perches }x; 4348905a67cSAndy Whitcroft $Type = qr{ 435c45dcabdSAndy Whitcroft $NonptrType 436b337d8b8SAndy Whitcroft (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 437c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 4388905a67cSAndy Whitcroft }x; 43991cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 4408905a67cSAndy Whitcroft} 4418905a67cSAndy Whitcroftbuild_types(); 4426c72ffaaSAndy Whitcroft 4437d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 444d1fe9c09SJoe Perches 445d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 446d1fe9c09SJoe Perches# requires at least perl version v5.10.0 447d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 448d1fe9c09SJoe Perches 449d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 4502435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 451d7c76ba7SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; 4527d2367afSJoe Perches 4537d2367afSJoe Perchessub deparenthesize { 4547d2367afSJoe Perches my ($string) = @_; 4557d2367afSJoe Perches return "" if (!defined($string)); 4565b9553abSJoe Perches 4575b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 4585b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 4595b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 4605b9553abSJoe Perches } 4615b9553abSJoe Perches 4627d2367afSJoe Perches $string =~ s@\s+@ @g; 4635b9553abSJoe Perches 4647d2367afSJoe Perches return $string; 4657d2367afSJoe Perches} 4667d2367afSJoe Perches 4673445686aSJoe Perchessub seed_camelcase_file { 4683445686aSJoe Perches my ($file) = @_; 4693445686aSJoe Perches 4703445686aSJoe Perches return if (!(-f $file)); 4713445686aSJoe Perches 4723445686aSJoe Perches local $/; 4733445686aSJoe Perches 4743445686aSJoe Perches open(my $include_file, '<', "$file") 4753445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 4763445686aSJoe Perches my $text = <$include_file>; 4773445686aSJoe Perches close($include_file); 4783445686aSJoe Perches 4793445686aSJoe Perches my @lines = split('\n', $text); 4803445686aSJoe Perches 4813445686aSJoe Perches foreach my $line (@lines) { 4823445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 4833445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 4843445686aSJoe Perches $camelcase{$1} = 1; 48511ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 48611ea516aSJoe Perches $camelcase{$1} = 1; 48711ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 4883445686aSJoe Perches $camelcase{$1} = 1; 4893445686aSJoe Perches } 4903445686aSJoe Perches } 4913445686aSJoe Perches} 4923445686aSJoe Perches 4933445686aSJoe Perchesmy $camelcase_seeded = 0; 4943445686aSJoe Perchessub seed_camelcase_includes { 4953445686aSJoe Perches return if ($camelcase_seeded); 4963445686aSJoe Perches 4973445686aSJoe Perches my $files; 498c707a81dSJoe Perches my $camelcase_cache = ""; 499c707a81dSJoe Perches my @include_files = (); 500c707a81dSJoe Perches 501c707a81dSJoe Perches $camelcase_seeded = 1; 502351b2a1fSJoe Perches 5033645e328SRichard Genoud if (-e ".git") { 504351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 505351b2a1fSJoe Perches chomp $git_last_include_commit; 506c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 507c707a81dSJoe Perches } else { 508c707a81dSJoe Perches my $last_mod_date = 0; 509c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 510c707a81dSJoe Perches @include_files = split('\n', $files); 511c707a81dSJoe Perches foreach my $file (@include_files) { 512c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 513c707a81dSJoe Perches localtime((stat $file)[9])); 514c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 515c707a81dSJoe Perches } 516c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 517c707a81dSJoe Perches } 518c707a81dSJoe Perches 519c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 520c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 521c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 522351b2a1fSJoe Perches while (<$camelcase_file>) { 523351b2a1fSJoe Perches chomp; 524351b2a1fSJoe Perches $camelcase{$_} = 1; 525351b2a1fSJoe Perches } 526351b2a1fSJoe Perches close($camelcase_file); 527351b2a1fSJoe Perches 528351b2a1fSJoe Perches return; 529351b2a1fSJoe Perches } 530c707a81dSJoe Perches 5313645e328SRichard Genoud if (-e ".git") { 532c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 533c707a81dSJoe Perches @include_files = split('\n', $files); 5343445686aSJoe Perches } 535c707a81dSJoe Perches 5363445686aSJoe Perches foreach my $file (@include_files) { 5373445686aSJoe Perches seed_camelcase_file($file); 5383445686aSJoe Perches } 539351b2a1fSJoe Perches 540c707a81dSJoe Perches if ($camelcase_cache ne "") { 541351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 542c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 543c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 544351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 545351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 546351b2a1fSJoe Perches } 547351b2a1fSJoe Perches close($camelcase_file); 548351b2a1fSJoe Perches } 5493445686aSJoe Perches} 5503445686aSJoe Perches 5516c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 5520a920b5bSAndy Whitcroft 55300df344fSAndy Whitcroftmy @rawlines = (); 554c2fdda0dSAndy Whitcroftmy @lines = (); 5553705ce5bSJoe Perchesmy @fixed = (); 556c2fdda0dSAndy Whitcroftmy $vname; 5576c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 55821caa13cSAndy Whitcroft my $FILE; 5596c72ffaaSAndy Whitcroft if ($file) { 56021caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 5616c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 56221caa13cSAndy Whitcroft } elsif ($filename eq '-') { 56321caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 5646c72ffaaSAndy Whitcroft } else { 56521caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 5666c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 5676c72ffaaSAndy Whitcroft } 568c2fdda0dSAndy Whitcroft if ($filename eq '-') { 569c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 570c2fdda0dSAndy Whitcroft } else { 571c2fdda0dSAndy Whitcroft $vname = $filename; 572c2fdda0dSAndy Whitcroft } 57321caa13cSAndy Whitcroft while (<$FILE>) { 5740a920b5bSAndy Whitcroft chomp; 57500df344fSAndy Whitcroft push(@rawlines, $_); 5766c72ffaaSAndy Whitcroft } 57721caa13cSAndy Whitcroft close($FILE); 578c2fdda0dSAndy Whitcroft if (!process($filename)) { 5790a920b5bSAndy Whitcroft $exit = 1; 5800a920b5bSAndy Whitcroft } 58100df344fSAndy Whitcroft @rawlines = (); 58213214adfSAndy Whitcroft @lines = (); 5833705ce5bSJoe Perches @fixed = (); 5840a920b5bSAndy Whitcroft} 5850a920b5bSAndy Whitcroft 5860a920b5bSAndy Whitcroftexit($exit); 5870a920b5bSAndy Whitcroft 5880a920b5bSAndy Whitcroftsub top_of_kernel_tree { 5896c72ffaaSAndy Whitcroft my ($root) = @_; 5906c72ffaaSAndy Whitcroft 5916c72ffaaSAndy Whitcroft my @tree_check = ( 5926c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 5936c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 5946c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 5956c72ffaaSAndy Whitcroft ); 5966c72ffaaSAndy Whitcroft 5976c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 5986c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 5990a920b5bSAndy Whitcroft return 0; 6000a920b5bSAndy Whitcroft } 6016c72ffaaSAndy Whitcroft } 6026c72ffaaSAndy Whitcroft return 1; 6036c72ffaaSAndy Whitcroft} 6040a920b5bSAndy Whitcroft 60520112475SJoe Perchessub parse_email { 60620112475SJoe Perches my ($formatted_email) = @_; 60720112475SJoe Perches 60820112475SJoe Perches my $name = ""; 60920112475SJoe Perches my $address = ""; 61020112475SJoe Perches my $comment = ""; 61120112475SJoe Perches 61220112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 61320112475SJoe Perches $name = $1; 61420112475SJoe Perches $address = $2; 61520112475SJoe Perches $comment = $3 if defined $3; 61620112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 61720112475SJoe Perches $address = $1; 61820112475SJoe Perches $comment = $2 if defined $2; 61920112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 62020112475SJoe Perches $address = $1; 62120112475SJoe Perches $comment = $2 if defined $2; 62220112475SJoe Perches $formatted_email =~ s/$address.*$//; 62320112475SJoe Perches $name = $formatted_email; 6243705ce5bSJoe Perches $name = trim($name); 62520112475SJoe Perches $name =~ s/^\"|\"$//g; 62620112475SJoe Perches # If there's a name left after stripping spaces and 62720112475SJoe Perches # leading quotes, and the address doesn't have both 62820112475SJoe Perches # leading and trailing angle brackets, the address 62920112475SJoe Perches # is invalid. ie: 63020112475SJoe Perches # "joe smith [email protected]" bad 63120112475SJoe Perches # "joe smith <[email protected]" bad 63220112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 63320112475SJoe Perches $name = ""; 63420112475SJoe Perches $address = ""; 63520112475SJoe Perches $comment = ""; 63620112475SJoe Perches } 63720112475SJoe Perches } 63820112475SJoe Perches 6393705ce5bSJoe Perches $name = trim($name); 64020112475SJoe Perches $name =~ s/^\"|\"$//g; 6413705ce5bSJoe Perches $address = trim($address); 64220112475SJoe Perches $address =~ s/^\<|\>$//g; 64320112475SJoe Perches 64420112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 64520112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 64620112475SJoe Perches $name = "\"$name\""; 64720112475SJoe Perches } 64820112475SJoe Perches 64920112475SJoe Perches return ($name, $address, $comment); 65020112475SJoe Perches} 65120112475SJoe Perches 65220112475SJoe Perchessub format_email { 65320112475SJoe Perches my ($name, $address) = @_; 65420112475SJoe Perches 65520112475SJoe Perches my $formatted_email; 65620112475SJoe Perches 6573705ce5bSJoe Perches $name = trim($name); 65820112475SJoe Perches $name =~ s/^\"|\"$//g; 6593705ce5bSJoe Perches $address = trim($address); 66020112475SJoe Perches 66120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 66220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 66320112475SJoe Perches $name = "\"$name\""; 66420112475SJoe Perches } 66520112475SJoe Perches 66620112475SJoe Perches if ("$name" eq "") { 66720112475SJoe Perches $formatted_email = "$address"; 66820112475SJoe Perches } else { 66920112475SJoe Perches $formatted_email = "$name <$address>"; 67020112475SJoe Perches } 67120112475SJoe Perches 67220112475SJoe Perches return $formatted_email; 67320112475SJoe Perches} 67420112475SJoe Perches 675000d1cc1SJoe Perchessub which_conf { 676000d1cc1SJoe Perches my ($conf) = @_; 677000d1cc1SJoe Perches 678000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 679000d1cc1SJoe Perches if (-e "$path/$conf") { 680000d1cc1SJoe Perches return "$path/$conf"; 681000d1cc1SJoe Perches } 682000d1cc1SJoe Perches } 683000d1cc1SJoe Perches 684000d1cc1SJoe Perches return ""; 685000d1cc1SJoe Perches} 686000d1cc1SJoe Perches 6870a920b5bSAndy Whitcroftsub expand_tabs { 6880a920b5bSAndy Whitcroft my ($str) = @_; 6890a920b5bSAndy Whitcroft 6900a920b5bSAndy Whitcroft my $res = ''; 6910a920b5bSAndy Whitcroft my $n = 0; 6920a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 6930a920b5bSAndy Whitcroft if ($c eq "\t") { 6940a920b5bSAndy Whitcroft $res .= ' '; 6950a920b5bSAndy Whitcroft $n++; 6960a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 6970a920b5bSAndy Whitcroft $res .= ' '; 6980a920b5bSAndy Whitcroft } 6990a920b5bSAndy Whitcroft next; 7000a920b5bSAndy Whitcroft } 7010a920b5bSAndy Whitcroft $res .= $c; 7020a920b5bSAndy Whitcroft $n++; 7030a920b5bSAndy Whitcroft } 7040a920b5bSAndy Whitcroft 7050a920b5bSAndy Whitcroft return $res; 7060a920b5bSAndy Whitcroft} 7076c72ffaaSAndy Whitcroftsub copy_spacing { 708773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 7096c72ffaaSAndy Whitcroft return $res; 7106c72ffaaSAndy Whitcroft} 7110a920b5bSAndy Whitcroft 7124a0df2efSAndy Whitcroftsub line_stats { 7134a0df2efSAndy Whitcroft my ($line) = @_; 7144a0df2efSAndy Whitcroft 7154a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 7164a0df2efSAndy Whitcroft $line =~ s/^.//; 7174a0df2efSAndy Whitcroft $line = expand_tabs($line); 7184a0df2efSAndy Whitcroft 7194a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 7204a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 7214a0df2efSAndy Whitcroft 7224a0df2efSAndy Whitcroft return (length($line), length($white)); 7234a0df2efSAndy Whitcroft} 7244a0df2efSAndy Whitcroft 725773647a0SAndy Whitcroftmy $sanitise_quote = ''; 726773647a0SAndy Whitcroft 727773647a0SAndy Whitcroftsub sanitise_line_reset { 728773647a0SAndy Whitcroft my ($in_comment) = @_; 729773647a0SAndy Whitcroft 730773647a0SAndy Whitcroft if ($in_comment) { 731773647a0SAndy Whitcroft $sanitise_quote = '*/'; 732773647a0SAndy Whitcroft } else { 733773647a0SAndy Whitcroft $sanitise_quote = ''; 734773647a0SAndy Whitcroft } 735773647a0SAndy Whitcroft} 73600df344fSAndy Whitcroftsub sanitise_line { 73700df344fSAndy Whitcroft my ($line) = @_; 73800df344fSAndy Whitcroft 73900df344fSAndy Whitcroft my $res = ''; 74000df344fSAndy Whitcroft my $l = ''; 74100df344fSAndy Whitcroft 742c2fdda0dSAndy Whitcroft my $qlen = 0; 743773647a0SAndy Whitcroft my $off = 0; 744773647a0SAndy Whitcroft my $c; 74500df344fSAndy Whitcroft 746773647a0SAndy Whitcroft # Always copy over the diff marker. 747773647a0SAndy Whitcroft $res = substr($line, 0, 1); 748773647a0SAndy Whitcroft 749773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 750773647a0SAndy Whitcroft $c = substr($line, $off, 1); 751773647a0SAndy Whitcroft 752773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 753773647a0SAndy Whitcroft # and end, all to $;. 754773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 755773647a0SAndy Whitcroft $sanitise_quote = '*/'; 756773647a0SAndy Whitcroft 757773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 758773647a0SAndy Whitcroft $off++; 75900df344fSAndy Whitcroft next; 760773647a0SAndy Whitcroft } 76181bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 762773647a0SAndy Whitcroft $sanitise_quote = ''; 763773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 764773647a0SAndy Whitcroft $off++; 765773647a0SAndy Whitcroft next; 766773647a0SAndy Whitcroft } 767113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 768113f04a8SDaniel Walker $sanitise_quote = '//'; 769113f04a8SDaniel Walker 770113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 771113f04a8SDaniel Walker $off++; 772113f04a8SDaniel Walker next; 773113f04a8SDaniel Walker } 774773647a0SAndy Whitcroft 775773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 776773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 777773647a0SAndy Whitcroft $c eq "\\") { 778773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 779773647a0SAndy Whitcroft $off++; 780773647a0SAndy Whitcroft next; 781773647a0SAndy Whitcroft } 782773647a0SAndy Whitcroft # Regular quotes. 783773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 784773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 785773647a0SAndy Whitcroft $sanitise_quote = $c; 786773647a0SAndy Whitcroft 787773647a0SAndy Whitcroft substr($res, $off, 1, $c); 788773647a0SAndy Whitcroft next; 789773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 790773647a0SAndy Whitcroft $sanitise_quote = ''; 79100df344fSAndy Whitcroft } 79200df344fSAndy Whitcroft } 793773647a0SAndy Whitcroft 794fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 795773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 796773647a0SAndy Whitcroft substr($res, $off, 1, $;); 797113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 798113f04a8SDaniel Walker substr($res, $off, 1, $;); 799773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 800773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 80100df344fSAndy Whitcroft } else { 802773647a0SAndy Whitcroft substr($res, $off, 1, $c); 80300df344fSAndy Whitcroft } 804c2fdda0dSAndy Whitcroft } 805c2fdda0dSAndy Whitcroft 806113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 807113f04a8SDaniel Walker $sanitise_quote = ''; 808113f04a8SDaniel Walker } 809113f04a8SDaniel Walker 810c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 811c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 812c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 813c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 814c2fdda0dSAndy Whitcroft 815c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 816c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 817c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 818c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 819c2fdda0dSAndy Whitcroft } 820c2fdda0dSAndy Whitcroft 82100df344fSAndy Whitcroft return $res; 82200df344fSAndy Whitcroft} 82300df344fSAndy Whitcroft 824a6962d72SJoe Perchessub get_quoted_string { 825a6962d72SJoe Perches my ($line, $rawline) = @_; 826a6962d72SJoe Perches 827a6962d72SJoe Perches return "" if ($line !~ m/(\"[X]+\")/g); 828a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 829a6962d72SJoe Perches} 830a6962d72SJoe Perches 8318905a67cSAndy Whitcroftsub ctx_statement_block { 8328905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 8338905a67cSAndy Whitcroft my $line = $linenr - 1; 8348905a67cSAndy Whitcroft my $blk = ''; 8358905a67cSAndy Whitcroft my $soff = $off; 8368905a67cSAndy Whitcroft my $coff = $off - 1; 837773647a0SAndy Whitcroft my $coff_set = 0; 8388905a67cSAndy Whitcroft 83913214adfSAndy Whitcroft my $loff = 0; 84013214adfSAndy Whitcroft 8418905a67cSAndy Whitcroft my $type = ''; 8428905a67cSAndy Whitcroft my $level = 0; 843a2750645SAndy Whitcroft my @stack = (); 844cf655043SAndy Whitcroft my $p; 8458905a67cSAndy Whitcroft my $c; 8468905a67cSAndy Whitcroft my $len = 0; 84713214adfSAndy Whitcroft 84813214adfSAndy Whitcroft my $remainder; 8498905a67cSAndy Whitcroft while (1) { 850a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 851a2750645SAndy Whitcroft 852773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 8538905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 8548905a67cSAndy Whitcroft # context. 8558905a67cSAndy Whitcroft if ($off >= $len) { 8568905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 857dea33496SAndy Whitcroft last if (!defined $lines[$line]); 858c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 8598905a67cSAndy Whitcroft $remain--; 86013214adfSAndy Whitcroft $loff = $len; 861c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 8628905a67cSAndy Whitcroft $len = length($blk); 8638905a67cSAndy Whitcroft $line++; 8648905a67cSAndy Whitcroft last; 8658905a67cSAndy Whitcroft } 8668905a67cSAndy Whitcroft # Bail if there is no further context. 8678905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 86813214adfSAndy Whitcroft if ($off >= $len) { 8698905a67cSAndy Whitcroft last; 8708905a67cSAndy Whitcroft } 871f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 872f74bd194SAndy Whitcroft $level++; 873f74bd194SAndy Whitcroft $type = '#'; 874f74bd194SAndy Whitcroft } 8758905a67cSAndy Whitcroft } 876cf655043SAndy Whitcroft $p = $c; 8778905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 87813214adfSAndy Whitcroft $remainder = substr($blk, $off); 8798905a67cSAndy Whitcroft 880773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 8814635f4fbSAndy Whitcroft 8824635f4fbSAndy Whitcroft # Handle nested #if/#else. 8834635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 8844635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 8854635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 8864635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 8874635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 8884635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 8894635f4fbSAndy Whitcroft } 8904635f4fbSAndy Whitcroft 8918905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 8928905a67cSAndy Whitcroft # outermost level. 8938905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 8948905a67cSAndy Whitcroft last; 8958905a67cSAndy Whitcroft } 8968905a67cSAndy Whitcroft 89713214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 898773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 899773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 900773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 901773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 902773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 903773647a0SAndy Whitcroft $coff_set = 1; 904773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 905773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 90613214adfSAndy Whitcroft } 90713214adfSAndy Whitcroft 9088905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 9098905a67cSAndy Whitcroft $level++; 9108905a67cSAndy Whitcroft $type = '('; 9118905a67cSAndy Whitcroft } 9128905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 9138905a67cSAndy Whitcroft $level--; 9148905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 9158905a67cSAndy Whitcroft 9168905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 9178905a67cSAndy Whitcroft $coff = $off; 918773647a0SAndy Whitcroft $coff_set = 1; 919773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 9208905a67cSAndy Whitcroft } 9218905a67cSAndy Whitcroft } 9228905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 9238905a67cSAndy Whitcroft $level++; 9248905a67cSAndy Whitcroft $type = '{'; 9258905a67cSAndy Whitcroft } 9268905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 9278905a67cSAndy Whitcroft $level--; 9288905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 9298905a67cSAndy Whitcroft 9308905a67cSAndy Whitcroft if ($level == 0) { 931b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 932b998e001SPatrick Pannuto $off++; 933b998e001SPatrick Pannuto } 9348905a67cSAndy Whitcroft last; 9358905a67cSAndy Whitcroft } 9368905a67cSAndy Whitcroft } 937f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 938f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 939f74bd194SAndy Whitcroft $level--; 940f74bd194SAndy Whitcroft $type = ''; 941f74bd194SAndy Whitcroft $off++; 942f74bd194SAndy Whitcroft last; 943f74bd194SAndy Whitcroft } 9448905a67cSAndy Whitcroft $off++; 9458905a67cSAndy Whitcroft } 946a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 94713214adfSAndy Whitcroft if ($off == $len) { 948a3bb97a7SAndy Whitcroft $loff = $len + 1; 94913214adfSAndy Whitcroft $line++; 95013214adfSAndy Whitcroft $remain--; 95113214adfSAndy Whitcroft } 9528905a67cSAndy Whitcroft 9538905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 9548905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 9558905a67cSAndy Whitcroft 9568905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 9578905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 9588905a67cSAndy Whitcroft 959773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 96013214adfSAndy Whitcroft 96113214adfSAndy Whitcroft return ($statement, $condition, 96213214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 96313214adfSAndy Whitcroft} 96413214adfSAndy Whitcroft 965cf655043SAndy Whitcroftsub statement_lines { 966cf655043SAndy Whitcroft my ($stmt) = @_; 967cf655043SAndy Whitcroft 968cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 969cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 970cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 971cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 972cf655043SAndy Whitcroft 973cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 974cf655043SAndy Whitcroft 975cf655043SAndy Whitcroft return $#stmt_lines + 2; 976cf655043SAndy Whitcroft} 977cf655043SAndy Whitcroft 978cf655043SAndy Whitcroftsub statement_rawlines { 979cf655043SAndy Whitcroft my ($stmt) = @_; 980cf655043SAndy Whitcroft 981cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 982cf655043SAndy Whitcroft 983cf655043SAndy Whitcroft return $#stmt_lines + 2; 984cf655043SAndy Whitcroft} 985cf655043SAndy Whitcroft 986cf655043SAndy Whitcroftsub statement_block_size { 987cf655043SAndy Whitcroft my ($stmt) = @_; 988cf655043SAndy Whitcroft 989cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 990cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 991cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 992cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 993cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 994cf655043SAndy Whitcroft 995cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 996cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 997cf655043SAndy Whitcroft 998cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 999cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1000cf655043SAndy Whitcroft 1001cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1002cf655043SAndy Whitcroft return $stmt_lines; 1003cf655043SAndy Whitcroft } else { 1004cf655043SAndy Whitcroft return $stmt_statements; 1005cf655043SAndy Whitcroft } 1006cf655043SAndy Whitcroft} 1007cf655043SAndy Whitcroft 100813214adfSAndy Whitcroftsub ctx_statement_full { 100913214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 101013214adfSAndy Whitcroft my ($statement, $condition, $level); 101113214adfSAndy Whitcroft 101213214adfSAndy Whitcroft my (@chunks); 101313214adfSAndy Whitcroft 1014cf655043SAndy Whitcroft # Grab the first conditional/block pair. 101513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 101613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1017773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 101813214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1019cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1020cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1021cf655043SAndy Whitcroft } 1022cf655043SAndy Whitcroft 1023cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1024cf655043SAndy Whitcroft # could continue the statement. 1025cf655043SAndy Whitcroft for (;;) { 102613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 102713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1028cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1029773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1030cf655043SAndy Whitcroft #print "C: push\n"; 1031cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 103213214adfSAndy Whitcroft } 103313214adfSAndy Whitcroft 103413214adfSAndy Whitcroft return ($level, $linenr, @chunks); 10358905a67cSAndy Whitcroft} 10368905a67cSAndy Whitcroft 10374a0df2efSAndy Whitcroftsub ctx_block_get { 1038f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 10394a0df2efSAndy Whitcroft my $line; 10404a0df2efSAndy Whitcroft my $start = $linenr - 1; 10414a0df2efSAndy Whitcroft my $blk = ''; 10424a0df2efSAndy Whitcroft my @o; 10434a0df2efSAndy Whitcroft my @c; 10444a0df2efSAndy Whitcroft my @res = (); 10454a0df2efSAndy Whitcroft 1046f0a594c1SAndy Whitcroft my $level = 0; 10474635f4fbSAndy Whitcroft my @stack = ($level); 104800df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 104900df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 105000df344fSAndy Whitcroft $remain--; 105100df344fSAndy Whitcroft 105200df344fSAndy Whitcroft $blk .= $rawlines[$line]; 10534635f4fbSAndy Whitcroft 10544635f4fbSAndy Whitcroft # Handle nested #if/#else. 105501464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 10564635f4fbSAndy Whitcroft push(@stack, $level); 105701464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 10584635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 105901464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 10604635f4fbSAndy Whitcroft $level = pop(@stack); 10614635f4fbSAndy Whitcroft } 10624635f4fbSAndy Whitcroft 106301464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1064f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1065f0a594c1SAndy Whitcroft if ($off > 0) { 1066f0a594c1SAndy Whitcroft $off--; 1067f0a594c1SAndy Whitcroft next; 1068f0a594c1SAndy Whitcroft } 10694a0df2efSAndy Whitcroft 1070f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1071f0a594c1SAndy Whitcroft $level--; 1072f0a594c1SAndy Whitcroft last if ($level == 0); 1073f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1074f0a594c1SAndy Whitcroft $level++; 1075f0a594c1SAndy Whitcroft } 1076f0a594c1SAndy Whitcroft } 10774a0df2efSAndy Whitcroft 1078f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 107900df344fSAndy Whitcroft push(@res, $rawlines[$line]); 10804a0df2efSAndy Whitcroft } 10814a0df2efSAndy Whitcroft 1082f0a594c1SAndy Whitcroft last if ($level == 0); 10834a0df2efSAndy Whitcroft } 10844a0df2efSAndy Whitcroft 1085f0a594c1SAndy Whitcroft return ($level, @res); 10864a0df2efSAndy Whitcroft} 10874a0df2efSAndy Whitcroftsub ctx_block_outer { 10884a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10894a0df2efSAndy Whitcroft 1090f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1091f0a594c1SAndy Whitcroft return @r; 10924a0df2efSAndy Whitcroft} 10934a0df2efSAndy Whitcroftsub ctx_block { 10944a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 10954a0df2efSAndy Whitcroft 1096f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1097f0a594c1SAndy Whitcroft return @r; 1098653d4876SAndy Whitcroft} 1099653d4876SAndy Whitcroftsub ctx_statement { 1100f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1101f0a594c1SAndy Whitcroft 1102f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1103f0a594c1SAndy Whitcroft return @r; 1104f0a594c1SAndy Whitcroft} 1105f0a594c1SAndy Whitcroftsub ctx_block_level { 1106653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1107653d4876SAndy Whitcroft 1108f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 11094a0df2efSAndy Whitcroft} 11109c0ca6f9SAndy Whitcroftsub ctx_statement_level { 11119c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 11129c0ca6f9SAndy Whitcroft 11139c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 11149c0ca6f9SAndy Whitcroft} 11154a0df2efSAndy Whitcroft 11164a0df2efSAndy Whitcroftsub ctx_locate_comment { 11174a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 11184a0df2efSAndy Whitcroft 11194a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1120beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 11214a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 11224a0df2efSAndy Whitcroft 11234a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 11244a0df2efSAndy Whitcroft # comment. 11254a0df2efSAndy Whitcroft my $in_comment = 0; 11264a0df2efSAndy Whitcroft $current_comment = ''; 11274a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 112800df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 112900df344fSAndy Whitcroft #warn " $line\n"; 11304a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 11314a0df2efSAndy Whitcroft $in_comment = 1; 11324a0df2efSAndy Whitcroft } 11334a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 11344a0df2efSAndy Whitcroft $in_comment = 1; 11354a0df2efSAndy Whitcroft } 11364a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 11374a0df2efSAndy Whitcroft $current_comment = ''; 11384a0df2efSAndy Whitcroft } 11394a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 11404a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 11414a0df2efSAndy Whitcroft $in_comment = 0; 11424a0df2efSAndy Whitcroft } 11434a0df2efSAndy Whitcroft } 11444a0df2efSAndy Whitcroft 11454a0df2efSAndy Whitcroft chomp($current_comment); 11464a0df2efSAndy Whitcroft return($current_comment); 11474a0df2efSAndy Whitcroft} 11484a0df2efSAndy Whitcroftsub ctx_has_comment { 11494a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 11504a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 11514a0df2efSAndy Whitcroft 115200df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 11534a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 11544a0df2efSAndy Whitcroft 11554a0df2efSAndy Whitcroft return ($cmt ne ''); 11564a0df2efSAndy Whitcroft} 11574a0df2efSAndy Whitcroft 11584d001e4dSAndy Whitcroftsub raw_line { 11594d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 11604d001e4dSAndy Whitcroft 11614d001e4dSAndy Whitcroft my $offset = $linenr - 1; 11624d001e4dSAndy Whitcroft $cnt++; 11634d001e4dSAndy Whitcroft 11644d001e4dSAndy Whitcroft my $line; 11654d001e4dSAndy Whitcroft while ($cnt) { 11664d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 11674d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 11684d001e4dSAndy Whitcroft $cnt--; 11694d001e4dSAndy Whitcroft } 11704d001e4dSAndy Whitcroft 11714d001e4dSAndy Whitcroft return $line; 11724d001e4dSAndy Whitcroft} 11734d001e4dSAndy Whitcroft 11740a920b5bSAndy Whitcroftsub cat_vet { 11750a920b5bSAndy Whitcroft my ($vet) = @_; 11769c0ca6f9SAndy Whitcroft my ($res, $coded); 11770a920b5bSAndy Whitcroft 11789c0ca6f9SAndy Whitcroft $res = ''; 11796c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 11806c72ffaaSAndy Whitcroft $res .= $1; 11816c72ffaaSAndy Whitcroft if ($2 ne '') { 11829c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 11836c72ffaaSAndy Whitcroft $res .= $coded; 11846c72ffaaSAndy Whitcroft } 11859c0ca6f9SAndy Whitcroft } 11869c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 11870a920b5bSAndy Whitcroft 11889c0ca6f9SAndy Whitcroft return $res; 11890a920b5bSAndy Whitcroft} 11900a920b5bSAndy Whitcroft 1191c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1192cf655043SAndy Whitcroftmy $av_pending; 1193c2fdda0dSAndy Whitcroftmy @av_paren_type; 11941f65f947SAndy Whitcroftmy $av_pend_colon; 1195c2fdda0dSAndy Whitcroft 1196c2fdda0dSAndy Whitcroftsub annotate_reset { 1197c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1198cf655043SAndy Whitcroft $av_pending = '_'; 1199cf655043SAndy Whitcroft @av_paren_type = ('E'); 12001f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1201c2fdda0dSAndy Whitcroft} 1202c2fdda0dSAndy Whitcroft 12036c72ffaaSAndy Whitcroftsub annotate_values { 12046c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 12056c72ffaaSAndy Whitcroft 12066c72ffaaSAndy Whitcroft my $res; 12071f65f947SAndy Whitcroft my $var = '_' x length($stream); 12086c72ffaaSAndy Whitcroft my $cur = $stream; 12096c72ffaaSAndy Whitcroft 1210c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 12116c72ffaaSAndy Whitcroft 12126c72ffaaSAndy Whitcroft while (length($cur)) { 1213773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1214cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1215171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 12166c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1217c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1218c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1219cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1220c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 12216c72ffaaSAndy Whitcroft } 12226c72ffaaSAndy Whitcroft 1223c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 12249446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 12259446ef56SAndy Whitcroft push(@av_paren_type, $type); 1226addcdceaSAndy Whitcroft $type = 'c'; 12279446ef56SAndy Whitcroft 1228e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1229c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 12306c72ffaaSAndy Whitcroft $type = 'T'; 12316c72ffaaSAndy Whitcroft 1232389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1233389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1234389a2fe5SAndy Whitcroft $type = 'T'; 1235389a2fe5SAndy Whitcroft 1236c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1237171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1238c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1239171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1240171ae1a4SAndy Whitcroft if ($2 ne '') { 1241cf655043SAndy Whitcroft $av_pending = 'N'; 1242171ae1a4SAndy Whitcroft } 1243171ae1a4SAndy Whitcroft $type = 'E'; 1244171ae1a4SAndy Whitcroft 1245c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1246171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1247171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1248171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 12496c72ffaaSAndy Whitcroft 1250c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1251cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1252c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1253cf655043SAndy Whitcroft 1254cf655043SAndy Whitcroft push(@av_paren_type, $type); 1255cf655043SAndy Whitcroft push(@av_paren_type, $type); 1256171ae1a4SAndy Whitcroft $type = 'E'; 1257cf655043SAndy Whitcroft 1258c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1259cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1260cf655043SAndy Whitcroft $av_preprocessor = 1; 1261cf655043SAndy Whitcroft 1262cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1263cf655043SAndy Whitcroft 1264171ae1a4SAndy Whitcroft $type = 'E'; 1265cf655043SAndy Whitcroft 1266c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1267cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1268cf655043SAndy Whitcroft 1269cf655043SAndy Whitcroft $av_preprocessor = 1; 1270cf655043SAndy Whitcroft 1271cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1272cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1273cf655043SAndy Whitcroft pop(@av_paren_type); 1274cf655043SAndy Whitcroft push(@av_paren_type, $type); 1275171ae1a4SAndy Whitcroft $type = 'E'; 12766c72ffaaSAndy Whitcroft 12776c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1278c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 12796c72ffaaSAndy Whitcroft 1280171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1281171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1282171ae1a4SAndy Whitcroft $av_pending = $type; 1283171ae1a4SAndy Whitcroft $type = 'N'; 1284171ae1a4SAndy Whitcroft 12856c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1286c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 12876c72ffaaSAndy Whitcroft if (defined $2) { 1288cf655043SAndy Whitcroft $av_pending = 'V'; 12896c72ffaaSAndy Whitcroft } 12906c72ffaaSAndy Whitcroft $type = 'N'; 12916c72ffaaSAndy Whitcroft 129214b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1293c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 129414b111c1SAndy Whitcroft $av_pending = 'E'; 12956c72ffaaSAndy Whitcroft $type = 'N'; 12966c72ffaaSAndy Whitcroft 12971f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 12981f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 12991f65f947SAndy Whitcroft $av_pend_colon = 'C'; 13001f65f947SAndy Whitcroft $type = 'N'; 13011f65f947SAndy Whitcroft 130214b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1303c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 13046c72ffaaSAndy Whitcroft $type = 'N'; 13056c72ffaaSAndy Whitcroft 13066c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1307c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1308cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1309cf655043SAndy Whitcroft $av_pending = '_'; 13106c72ffaaSAndy Whitcroft $type = 'N'; 13116c72ffaaSAndy Whitcroft 13126c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1313cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1314cf655043SAndy Whitcroft if ($new_type ne '_') { 1315cf655043SAndy Whitcroft $type = $new_type; 1316c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1317c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 13186c72ffaaSAndy Whitcroft } else { 1319c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 13206c72ffaaSAndy Whitcroft } 13216c72ffaaSAndy Whitcroft 1322c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1323c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1324c8cb2ca3SAndy Whitcroft $type = 'V'; 1325cf655043SAndy Whitcroft $av_pending = 'V'; 13266c72ffaaSAndy Whitcroft 13278e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 13288e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 13291f65f947SAndy Whitcroft $av_pend_colon = 'B'; 13308e761b04SAndy Whitcroft } elsif ($type eq 'E') { 13318e761b04SAndy Whitcroft $av_pend_colon = 'L'; 13321f65f947SAndy Whitcroft } 13331f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 13341f65f947SAndy Whitcroft $type = 'V'; 13351f65f947SAndy Whitcroft 13366c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1337c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 13386c72ffaaSAndy Whitcroft $type = 'V'; 13396c72ffaaSAndy Whitcroft 13406c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1341c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 13426c72ffaaSAndy Whitcroft $type = 'N'; 13436c72ffaaSAndy Whitcroft 1344cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1345c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 134613214adfSAndy Whitcroft $type = 'E'; 13471f65f947SAndy Whitcroft $av_pend_colon = 'O'; 134813214adfSAndy Whitcroft 13498e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 13508e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 13518e761b04SAndy Whitcroft $type = 'C'; 13528e761b04SAndy Whitcroft 13531f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 13541f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 13551f65f947SAndy Whitcroft $type = 'N'; 13561f65f947SAndy Whitcroft 13571f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 13581f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 13591f65f947SAndy Whitcroft 13601f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 13611f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 13621f65f947SAndy Whitcroft $type = 'E'; 13631f65f947SAndy Whitcroft } else { 13641f65f947SAndy Whitcroft $type = 'N'; 13651f65f947SAndy Whitcroft } 13661f65f947SAndy Whitcroft $av_pend_colon = 'O'; 13671f65f947SAndy Whitcroft 13688e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 136913214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 13706c72ffaaSAndy Whitcroft $type = 'N'; 13716c72ffaaSAndy Whitcroft 13720d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 137374048ed8SAndy Whitcroft my $variant; 137474048ed8SAndy Whitcroft 137574048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 137674048ed8SAndy Whitcroft if ($type eq 'V') { 137774048ed8SAndy Whitcroft $variant = 'B'; 137874048ed8SAndy Whitcroft } else { 137974048ed8SAndy Whitcroft $variant = 'U'; 138074048ed8SAndy Whitcroft } 138174048ed8SAndy Whitcroft 138274048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 138374048ed8SAndy Whitcroft $type = 'N'; 138474048ed8SAndy Whitcroft 13856c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1386c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 13876c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 13886c72ffaaSAndy Whitcroft $type = 'N'; 13896c72ffaaSAndy Whitcroft } 13906c72ffaaSAndy Whitcroft 13916c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1392c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 13936c72ffaaSAndy Whitcroft } 13946c72ffaaSAndy Whitcroft if (defined $1) { 13956c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 13966c72ffaaSAndy Whitcroft $res .= $type x length($1); 13976c72ffaaSAndy Whitcroft } 13986c72ffaaSAndy Whitcroft } 13996c72ffaaSAndy Whitcroft 14001f65f947SAndy Whitcroft return ($res, $var); 14016c72ffaaSAndy Whitcroft} 14026c72ffaaSAndy Whitcroft 14038905a67cSAndy Whitcroftsub possible { 140413214adfSAndy Whitcroft my ($possible, $line) = @_; 14059a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 14060776e594SAndy Whitcroft ^(?: 14070776e594SAndy Whitcroft $Modifier| 14080776e594SAndy Whitcroft $Storage| 14090776e594SAndy Whitcroft $Type| 14109a974fdbSAndy Whitcroft DEFINE_\S+ 14119a974fdbSAndy Whitcroft )$| 14129a974fdbSAndy Whitcroft ^(?: 14130776e594SAndy Whitcroft goto| 14140776e594SAndy Whitcroft return| 14150776e594SAndy Whitcroft case| 14160776e594SAndy Whitcroft else| 14170776e594SAndy Whitcroft asm|__asm__| 141889a88353SAndy Whitcroft do| 141989a88353SAndy Whitcroft \#| 142089a88353SAndy Whitcroft \#\#| 14219a974fdbSAndy Whitcroft )(?:\s|$)| 14220776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 14239a974fdbSAndy Whitcroft )}x; 14249a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 14259a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1426c45dcabdSAndy Whitcroft # Check for modifiers. 1427c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1428c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1429c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1430c45dcabdSAndy Whitcroft 1431c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1432c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1433d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 14349a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1435d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1436d2506586SAndy Whitcroft push(@modifierList, $modifier); 1437d2506586SAndy Whitcroft } 14389a974fdbSAndy Whitcroft } 1439c45dcabdSAndy Whitcroft 1440c45dcabdSAndy Whitcroft } else { 144113214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 14428905a67cSAndy Whitcroft push(@typeList, $possible); 1443c45dcabdSAndy Whitcroft } 14448905a67cSAndy Whitcroft build_types(); 14450776e594SAndy Whitcroft } else { 14460776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 14478905a67cSAndy Whitcroft } 14488905a67cSAndy Whitcroft} 14498905a67cSAndy Whitcroft 14506c72ffaaSAndy Whitcroftmy $prefix = ''; 14516c72ffaaSAndy Whitcroft 1452000d1cc1SJoe Perchessub show_type { 1453cbec18afSJoe Perches my ($type) = @_; 145491bfe484SJoe Perches 1455cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1456cbec18afSJoe Perches 1457cbec18afSJoe Perches return !defined $ignore_type{$type}; 1458000d1cc1SJoe Perches} 1459000d1cc1SJoe Perches 1460f0a594c1SAndy Whitcroftsub report { 1461cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1462cbec18afSJoe Perches 1463cbec18afSJoe Perches if (!show_type($type) || 1464cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1465773647a0SAndy Whitcroft return 0; 1466773647a0SAndy Whitcroft } 1467000d1cc1SJoe Perches my $line; 1468000d1cc1SJoe Perches if ($show_types) { 1469cbec18afSJoe Perches $line = "$prefix$level:$type: $msg\n"; 1470000d1cc1SJoe Perches } else { 1471cbec18afSJoe Perches $line = "$prefix$level: $msg\n"; 1472000d1cc1SJoe Perches } 14738905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 14748905a67cSAndy Whitcroft 147513214adfSAndy Whitcroft push(our @report, $line); 1476773647a0SAndy Whitcroft 1477773647a0SAndy Whitcroft return 1; 1478f0a594c1SAndy Whitcroft} 1479cbec18afSJoe Perches 1480f0a594c1SAndy Whitcroftsub report_dump { 148113214adfSAndy Whitcroft our @report; 1482f0a594c1SAndy Whitcroft} 1483000d1cc1SJoe Perches 1484de7d4f0eSAndy Whitcroftsub ERROR { 1485cbec18afSJoe Perches my ($type, $msg) = @_; 1486cbec18afSJoe Perches 1487cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 1488de7d4f0eSAndy Whitcroft our $clean = 0; 14896c72ffaaSAndy Whitcroft our $cnt_error++; 14903705ce5bSJoe Perches return 1; 1491de7d4f0eSAndy Whitcroft } 14923705ce5bSJoe Perches return 0; 1493773647a0SAndy Whitcroft} 1494de7d4f0eSAndy Whitcroftsub WARN { 1495cbec18afSJoe Perches my ($type, $msg) = @_; 1496cbec18afSJoe Perches 1497cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 1498de7d4f0eSAndy Whitcroft our $clean = 0; 14996c72ffaaSAndy Whitcroft our $cnt_warn++; 15003705ce5bSJoe Perches return 1; 1501de7d4f0eSAndy Whitcroft } 15023705ce5bSJoe Perches return 0; 1503773647a0SAndy Whitcroft} 1504de7d4f0eSAndy Whitcroftsub CHK { 1505cbec18afSJoe Perches my ($type, $msg) = @_; 1506cbec18afSJoe Perches 1507cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 1508de7d4f0eSAndy Whitcroft our $clean = 0; 15096c72ffaaSAndy Whitcroft our $cnt_chk++; 15103705ce5bSJoe Perches return 1; 15116c72ffaaSAndy Whitcroft } 15123705ce5bSJoe Perches return 0; 1513de7d4f0eSAndy Whitcroft} 1514de7d4f0eSAndy Whitcroft 15156ecd9674SAndy Whitcroftsub check_absolute_file { 15166ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 15176ecd9674SAndy Whitcroft my $file = $absolute; 15186ecd9674SAndy Whitcroft 15196ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 15206ecd9674SAndy Whitcroft 15216ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 15226ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 15236ecd9674SAndy Whitcroft if (-f "$root/$file") { 15246ecd9674SAndy Whitcroft ##print "file<$file>\n"; 15256ecd9674SAndy Whitcroft last; 15266ecd9674SAndy Whitcroft } 15276ecd9674SAndy Whitcroft } 15286ecd9674SAndy Whitcroft if (! -f _) { 15296ecd9674SAndy Whitcroft return 0; 15306ecd9674SAndy Whitcroft } 15316ecd9674SAndy Whitcroft 15326ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 15336ecd9674SAndy Whitcroft my $prefix = $absolute; 15346ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 15356ecd9674SAndy Whitcroft 15366ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 15376ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1538000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1539000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 15406ecd9674SAndy Whitcroft } 15416ecd9674SAndy Whitcroft} 15426ecd9674SAndy Whitcroft 15433705ce5bSJoe Perchessub trim { 15443705ce5bSJoe Perches my ($string) = @_; 15453705ce5bSJoe Perches 1546b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1547b34c648bSJoe Perches 1548b34c648bSJoe Perches return $string; 1549b34c648bSJoe Perches} 1550b34c648bSJoe Perches 1551b34c648bSJoe Perchessub ltrim { 1552b34c648bSJoe Perches my ($string) = @_; 1553b34c648bSJoe Perches 1554b34c648bSJoe Perches $string =~ s/^\s+//; 1555b34c648bSJoe Perches 1556b34c648bSJoe Perches return $string; 1557b34c648bSJoe Perches} 1558b34c648bSJoe Perches 1559b34c648bSJoe Perchessub rtrim { 1560b34c648bSJoe Perches my ($string) = @_; 1561b34c648bSJoe Perches 1562b34c648bSJoe Perches $string =~ s/\s+$//; 15633705ce5bSJoe Perches 15643705ce5bSJoe Perches return $string; 15653705ce5bSJoe Perches} 15663705ce5bSJoe Perches 156752ea8506SJoe Perchessub string_find_replace { 156852ea8506SJoe Perches my ($string, $find, $replace) = @_; 156952ea8506SJoe Perches 157052ea8506SJoe Perches $string =~ s/$find/$replace/g; 157152ea8506SJoe Perches 157252ea8506SJoe Perches return $string; 157352ea8506SJoe Perches} 157452ea8506SJoe Perches 15753705ce5bSJoe Perchessub tabify { 15763705ce5bSJoe Perches my ($leading) = @_; 15773705ce5bSJoe Perches 15783705ce5bSJoe Perches my $source_indent = 8; 15793705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 15803705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 15813705ce5bSJoe Perches 15823705ce5bSJoe Perches #convert leading spaces to tabs 15833705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 15843705ce5bSJoe Perches #Remove spaces before a tab 15853705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 15863705ce5bSJoe Perches 15873705ce5bSJoe Perches return "$leading"; 15883705ce5bSJoe Perches} 15893705ce5bSJoe Perches 1590d1fe9c09SJoe Perchessub pos_last_openparen { 1591d1fe9c09SJoe Perches my ($line) = @_; 1592d1fe9c09SJoe Perches 1593d1fe9c09SJoe Perches my $pos = 0; 1594d1fe9c09SJoe Perches 1595d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1596d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1597d1fe9c09SJoe Perches 1598d1fe9c09SJoe Perches my $last_openparen = 0; 1599d1fe9c09SJoe Perches 1600d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1601d1fe9c09SJoe Perches return -1; 1602d1fe9c09SJoe Perches } 1603d1fe9c09SJoe Perches 1604d1fe9c09SJoe Perches my $len = length($line); 1605d1fe9c09SJoe Perches 1606d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1607d1fe9c09SJoe Perches my $string = substr($line, $pos); 1608d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1609d1fe9c09SJoe Perches $pos += length($1) - 1; 1610d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1611d1fe9c09SJoe Perches $last_openparen = $pos; 1612d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1613d1fe9c09SJoe Perches last; 1614d1fe9c09SJoe Perches } 1615d1fe9c09SJoe Perches } 1616d1fe9c09SJoe Perches 161791cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 1618d1fe9c09SJoe Perches} 1619d1fe9c09SJoe Perches 16200a920b5bSAndy Whitcroftsub process { 16210a920b5bSAndy Whitcroft my $filename = shift; 16220a920b5bSAndy Whitcroft 16230a920b5bSAndy Whitcroft my $linenr=0; 16240a920b5bSAndy Whitcroft my $prevline=""; 1625c2fdda0dSAndy Whitcroft my $prevrawline=""; 16260a920b5bSAndy Whitcroft my $stashline=""; 1627c2fdda0dSAndy Whitcroft my $stashrawline=""; 16280a920b5bSAndy Whitcroft 16294a0df2efSAndy Whitcroft my $length; 16300a920b5bSAndy Whitcroft my $indent; 16310a920b5bSAndy Whitcroft my $previndent=0; 16320a920b5bSAndy Whitcroft my $stashindent=0; 16330a920b5bSAndy Whitcroft 1634de7d4f0eSAndy Whitcroft our $clean = 1; 16350a920b5bSAndy Whitcroft my $signoff = 0; 16360a920b5bSAndy Whitcroft my $is_patch = 0; 16370a920b5bSAndy Whitcroft 163815662b3eSJoe Perches my $in_header_lines = 1; 163915662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 164015662b3eSJoe Perches 1641fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1642fa64205dSPasi Savanainen 164313214adfSAndy Whitcroft our @report = (); 16446c72ffaaSAndy Whitcroft our $cnt_lines = 0; 16456c72ffaaSAndy Whitcroft our $cnt_error = 0; 16466c72ffaaSAndy Whitcroft our $cnt_warn = 0; 16476c72ffaaSAndy Whitcroft our $cnt_chk = 0; 16486c72ffaaSAndy Whitcroft 16490a920b5bSAndy Whitcroft # Trace the real file/line as we go. 16500a920b5bSAndy Whitcroft my $realfile = ''; 16510a920b5bSAndy Whitcroft my $realline = 0; 16520a920b5bSAndy Whitcroft my $realcnt = 0; 16530a920b5bSAndy Whitcroft my $here = ''; 16540a920b5bSAndy Whitcroft my $in_comment = 0; 1655c2fdda0dSAndy Whitcroft my $comment_edge = 0; 16560a920b5bSAndy Whitcroft my $first_line = 0; 16571e855726SWolfram Sang my $p1_prefix = ''; 16580a920b5bSAndy Whitcroft 165913214adfSAndy Whitcroft my $prev_values = 'E'; 166013214adfSAndy Whitcroft 166113214adfSAndy Whitcroft # suppression flags 1662773647a0SAndy Whitcroft my %suppress_ifbraces; 1663170d3a22SAndy Whitcroft my %suppress_whiletrailers; 16642b474a1aSAndy Whitcroft my %suppress_export; 16653e469cdcSAndy Whitcroft my $suppress_statement = 0; 1666653d4876SAndy Whitcroft 16677e51f197SJoe Perches my %signatures = (); 1668323c1260SJoe Perches 1669c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1670de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1671c2fdda0dSAndy Whitcroft # 1672de7d4f0eSAndy Whitcroft my @setup_docs = (); 1673de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1674773647a0SAndy Whitcroft 1675d8b07710SJoe Perches my $camelcase_file_seeded = 0; 1676d8b07710SJoe Perches 1677773647a0SAndy Whitcroft sanitise_line_reset(); 1678c2fdda0dSAndy Whitcroft my $line; 1679c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1680773647a0SAndy Whitcroft $linenr++; 1681773647a0SAndy Whitcroft $line = $rawline; 1682c2fdda0dSAndy Whitcroft 16833705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 16843705ce5bSJoe Perches 1685773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1686de7d4f0eSAndy Whitcroft $setup_docs = 0; 1687de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1688de7d4f0eSAndy Whitcroft $setup_docs = 1; 1689de7d4f0eSAndy Whitcroft } 1690773647a0SAndy Whitcroft #next; 1691de7d4f0eSAndy Whitcroft } 1692773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1693773647a0SAndy Whitcroft $realline=$1-1; 1694773647a0SAndy Whitcroft if (defined $2) { 1695773647a0SAndy Whitcroft $realcnt=$3+1; 1696773647a0SAndy Whitcroft } else { 1697773647a0SAndy Whitcroft $realcnt=1+1; 1698773647a0SAndy Whitcroft } 1699c45dcabdSAndy Whitcroft $in_comment = 0; 1700773647a0SAndy Whitcroft 1701773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1702773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1703773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1704773647a0SAndy Whitcroft # at context start. 1705773647a0SAndy Whitcroft my $edge; 170601fa9147SAndy Whitcroft my $cnt = $realcnt; 170701fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 170801fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 170901fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 171001fa9147SAndy Whitcroft $cnt--; 171101fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1712721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1713fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1714fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1715fae17daeSAndy Whitcroft ($edge) = $1; 1716fae17daeSAndy Whitcroft last; 1717fae17daeSAndy Whitcroft } 1718773647a0SAndy Whitcroft } 1719773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1720773647a0SAndy Whitcroft $in_comment = 1; 1721773647a0SAndy Whitcroft } 1722773647a0SAndy Whitcroft 1723773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1724773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1725773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1726773647a0SAndy Whitcroft if (!defined $edge && 172783242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1728773647a0SAndy Whitcroft { 1729773647a0SAndy Whitcroft $in_comment = 1; 1730773647a0SAndy Whitcroft } 1731773647a0SAndy Whitcroft 1732773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1733773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1734773647a0SAndy Whitcroft 1735171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1736773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1737171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1738773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1739773647a0SAndy Whitcroft } 1740773647a0SAndy Whitcroft push(@lines, $line); 1741773647a0SAndy Whitcroft 1742773647a0SAndy Whitcroft if ($realcnt > 1) { 1743773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1744773647a0SAndy Whitcroft } else { 1745773647a0SAndy Whitcroft $realcnt = 0; 1746773647a0SAndy Whitcroft } 1747773647a0SAndy Whitcroft 1748773647a0SAndy Whitcroft #print "==>$rawline\n"; 1749773647a0SAndy Whitcroft #print "-->$line\n"; 1750de7d4f0eSAndy Whitcroft 1751de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1752de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1753de7d4f0eSAndy Whitcroft } 1754de7d4f0eSAndy Whitcroft } 1755de7d4f0eSAndy Whitcroft 17566c72ffaaSAndy Whitcroft $prefix = ''; 17576c72ffaaSAndy Whitcroft 1758773647a0SAndy Whitcroft $realcnt = 0; 1759773647a0SAndy Whitcroft $linenr = 0; 17600a920b5bSAndy Whitcroft foreach my $line (@lines) { 17610a920b5bSAndy Whitcroft $linenr++; 17621b5539b1SJoe Perches my $sline = $line; #copy of $line 17631b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 17640a920b5bSAndy Whitcroft 1765c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 17666c72ffaaSAndy Whitcroft 17670a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 17686c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 17690a920b5bSAndy Whitcroft $is_patch = 1; 17704a0df2efSAndy Whitcroft $first_line = $linenr + 1; 17710a920b5bSAndy Whitcroft $realline=$1-1; 17720a920b5bSAndy Whitcroft if (defined $2) { 17730a920b5bSAndy Whitcroft $realcnt=$3+1; 17740a920b5bSAndy Whitcroft } else { 17750a920b5bSAndy Whitcroft $realcnt=1+1; 17760a920b5bSAndy Whitcroft } 1777c2fdda0dSAndy Whitcroft annotate_reset(); 177813214adfSAndy Whitcroft $prev_values = 'E'; 177913214adfSAndy Whitcroft 1780773647a0SAndy Whitcroft %suppress_ifbraces = (); 1781170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 17822b474a1aSAndy Whitcroft %suppress_export = (); 17833e469cdcSAndy Whitcroft $suppress_statement = 0; 17840a920b5bSAndy Whitcroft next; 17850a920b5bSAndy Whitcroft 17864a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 17874a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 17884a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1789773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 17900a920b5bSAndy Whitcroft $realline++; 1791d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 17920a920b5bSAndy Whitcroft 17934a0df2efSAndy Whitcroft # Measure the line length and indent. 1794c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 17950a920b5bSAndy Whitcroft 17960a920b5bSAndy Whitcroft # Track the previous line. 17970a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 17980a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1799c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1800c2fdda0dSAndy Whitcroft 1801773647a0SAndy Whitcroft #warn "line<$line>\n"; 18026c72ffaaSAndy Whitcroft 1803d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1804d8aaf121SAndy Whitcroft $realcnt--; 18050a920b5bSAndy Whitcroft } 18060a920b5bSAndy Whitcroft 1807cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1808cc77cdcaSAndy Whitcroft 18090a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1810773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1811773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1812773647a0SAndy Whitcroft 18136c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 18146c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1815773647a0SAndy Whitcroft 1816773647a0SAndy Whitcroft # extract the filename as it passes 18173bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 18183bf9a009SRabin Vincent $realfile = $1; 18192b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 1820270c49a0SJoe Perches $in_commit_log = 0; 18213bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1822773647a0SAndy Whitcroft $realfile = $1; 18232b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 1824270c49a0SJoe Perches $in_commit_log = 0; 18251e855726SWolfram Sang 18261e855726SWolfram Sang $p1_prefix = $1; 1827e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1828e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 1829000d1cc1SJoe Perches WARN("PATCH_PREFIX", 1830000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 18311e855726SWolfram Sang } 1832773647a0SAndy Whitcroft 1833c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1834000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 1835000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1836773647a0SAndy Whitcroft } 1837773647a0SAndy Whitcroft next; 1838773647a0SAndy Whitcroft } 1839773647a0SAndy Whitcroft 1840389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 18410a920b5bSAndy Whitcroft 1842c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1843c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1844c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 18450a920b5bSAndy Whitcroft 18466c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 18476c72ffaaSAndy Whitcroft 18483bf9a009SRabin Vincent# Check for incorrect file permissions 18493bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 18503bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 185104db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 185204db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 1853000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 1854000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 18553bf9a009SRabin Vincent } 18563bf9a009SRabin Vincent } 18573bf9a009SRabin Vincent 185820112475SJoe Perches# Check the patch for a signoff: 1859d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 18604a0df2efSAndy Whitcroft $signoff++; 186115662b3eSJoe Perches $in_commit_log = 0; 18620a920b5bSAndy Whitcroft } 186320112475SJoe Perches 186420112475SJoe Perches# Check signature styles 1865270c49a0SJoe Perches if (!$in_header_lines && 1866ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 186720112475SJoe Perches my $space_before = $1; 186820112475SJoe Perches my $sign_off = $2; 186920112475SJoe Perches my $space_after = $3; 187020112475SJoe Perches my $email = $4; 187120112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 187220112475SJoe Perches 1873ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 1874ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 1875ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 1876ce0338dfSJoe Perches } 187720112475SJoe Perches if (defined $space_before && $space_before ne "") { 18783705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18793705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 18803705ce5bSJoe Perches $fix) { 18813705ce5bSJoe Perches $fixed[$linenr - 1] = 18823705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18833705ce5bSJoe Perches } 188420112475SJoe Perches } 188520112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 18863705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18873705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 18883705ce5bSJoe Perches $fix) { 18893705ce5bSJoe Perches $fixed[$linenr - 1] = 18903705ce5bSJoe Perches "$ucfirst_sign_off $email"; 18913705ce5bSJoe Perches } 18923705ce5bSJoe Perches 189320112475SJoe Perches } 189420112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 18953705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 18963705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 18973705ce5bSJoe Perches $fix) { 18983705ce5bSJoe Perches $fixed[$linenr - 1] = 18993705ce5bSJoe Perches "$ucfirst_sign_off $email"; 19003705ce5bSJoe Perches } 190120112475SJoe Perches } 190220112475SJoe Perches 190320112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 190420112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 190520112475SJoe Perches if ($suggested_email eq "") { 1906000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 1907000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 190820112475SJoe Perches } else { 190920112475SJoe Perches my $dequoted = $suggested_email; 191020112475SJoe Perches $dequoted =~ s/^"//; 191120112475SJoe Perches $dequoted =~ s/" </ </; 191220112475SJoe Perches # Don't force email to have quotes 191320112475SJoe Perches # Allow just an angle bracketed address 191420112475SJoe Perches if ("$dequoted$comment" ne $email && 191520112475SJoe Perches "<$email_address>$comment" ne $email && 191620112475SJoe Perches "$suggested_email$comment" ne $email) { 1917000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 1918000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 191920112475SJoe Perches } 19200a920b5bSAndy Whitcroft } 19217e51f197SJoe Perches 19227e51f197SJoe Perches# Check for duplicate signatures 19237e51f197SJoe Perches my $sig_nospace = $line; 19247e51f197SJoe Perches $sig_nospace =~ s/\s//g; 19257e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 19267e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 19277e51f197SJoe Perches WARN("BAD_SIGN_OFF", 19287e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 19297e51f197SJoe Perches } else { 19307e51f197SJoe Perches $signatures{$sig_nospace} = 1; 19317e51f197SJoe Perches } 19320a920b5bSAndy Whitcroft } 19330a920b5bSAndy Whitcroft 19347ebd05efSChristopher Covington# Check for unwanted Gerrit info 19357ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 19367ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 19377ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 19387ebd05efSChristopher Covington } 19397ebd05efSChristopher Covington 194000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 19418905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1942000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 1943000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 19446c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1945de7d4f0eSAndy Whitcroft } 1946de7d4f0eSAndy Whitcroft 19476ecd9674SAndy Whitcroft# Check for absolute kernel paths. 19486ecd9674SAndy Whitcroft if ($tree) { 19496ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 19506ecd9674SAndy Whitcroft my $file = $1; 19516ecd9674SAndy Whitcroft 19526ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 19536ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 19546ecd9674SAndy Whitcroft # 19556ecd9674SAndy Whitcroft } else { 19566ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 19576ecd9674SAndy Whitcroft } 19586ecd9674SAndy Whitcroft } 19596ecd9674SAndy Whitcroft } 19606ecd9674SAndy Whitcroft 1961de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1962de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1963171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1964171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1965171ae1a4SAndy Whitcroft 1966171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1967171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1968171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1969171ae1a4SAndy Whitcroft 197034d99219SJoe Perches CHK("INVALID_UTF8", 1971000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 197200df344fSAndy Whitcroft } 19730a920b5bSAndy Whitcroft 197415662b3eSJoe Perches# Check if it's the start of a commit log 197515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 197615662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 1977270c49a0SJoe Perches $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { 197815662b3eSJoe Perches $in_header_lines = 0; 197915662b3eSJoe Perches $in_commit_log = 1; 198015662b3eSJoe Perches } 198115662b3eSJoe Perches 1982fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 1983fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 1984fa64205dSPasi Savanainen if ($in_header_lines && 1985fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 1986fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 1987fa64205dSPasi Savanainen $non_utf8_charset = 1; 1988fa64205dSPasi Savanainen } 1989fa64205dSPasi Savanainen 1990fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 199115662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 1992fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 199315662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 199415662b3eSJoe Perches } 199515662b3eSJoe Perches 199630670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 199730670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 199800df344fSAndy Whitcroft 19990a920b5bSAndy Whitcroft#trailing whitespace 20009c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2001c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2002d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2003d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2004d5e616fcSJoe Perches $fix) { 2005d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/[\s\015]+$//; 2006d5e616fcSJoe Perches } 2007c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2008c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 20093705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 20103705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 20113705ce5bSJoe Perches $fix) { 2012d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 20133705ce5bSJoe Perches } 20143705ce5bSJoe Perches 2015d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 20160a920b5bSAndy Whitcroft } 20175368df20SAndy Whitcroft 20184783f894SJosh Triplett# Check for FSF mailing addresses. 2019109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 20203e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 20213e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 20224783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 20234783f894SJosh Triplett my $msg_type = \&ERROR; 20244783f894SJosh Triplett $msg_type = \&CHK if ($file); 20254783f894SJosh Triplett &{$msg_type}("FSF_MAILING_ADDRESS", 20264783f894SJosh 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) 20274783f894SJosh Triplett } 20284783f894SJosh Triplett 20293354957aSAndi Kleen# check for Kconfig help text having a real description 20309fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 20319fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 20323354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 2033a1385803SAndy Whitcroft $line =~ /.\s*config\s+/) { 20343354957aSAndi Kleen my $length = 0; 20359fe287d7SAndy Whitcroft my $cnt = $realcnt; 20369fe287d7SAndy Whitcroft my $ln = $linenr + 1; 20379fe287d7SAndy Whitcroft my $f; 2038a1385803SAndy Whitcroft my $is_start = 0; 20399fe287d7SAndy Whitcroft my $is_end = 0; 2040a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 20419fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 20429fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 20439fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 20449fe287d7SAndy Whitcroft 20459fe287d7SAndy Whitcroft next if ($f =~ /^-/); 2046a1385803SAndy Whitcroft 2047a1385803SAndy Whitcroft if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { 2048a1385803SAndy Whitcroft $is_start = 1; 2049a1385803SAndy Whitcroft } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { 2050a1385803SAndy Whitcroft $length = -1; 2051a1385803SAndy Whitcroft } 2052a1385803SAndy Whitcroft 20539fe287d7SAndy Whitcroft $f =~ s/^.//; 20543354957aSAndi Kleen $f =~ s/#.*//; 20553354957aSAndi Kleen $f =~ s/^\s+//; 20563354957aSAndi Kleen next if ($f =~ /^$/); 20579fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 20589fe287d7SAndy Whitcroft $is_end = 1; 20599fe287d7SAndy Whitcroft last; 20609fe287d7SAndy Whitcroft } 20613354957aSAndi Kleen $length++; 20623354957aSAndi Kleen } 2063000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 2064a1385803SAndy Whitcroft "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 2065a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 20663354957aSAndi Kleen } 20673354957aSAndi Kleen 20681ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 20691ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 20701ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 20711ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 20721ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 20731ba8dfd1SKees Cook } 20741ba8dfd1SKees Cook 2075c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2076c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2077c68e5878SArnaud Lacombe my $flag = $1; 2078c68e5878SArnaud Lacombe my $replacement = { 2079c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2080c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2081c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2082c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2083c68e5878SArnaud Lacombe }; 2084c68e5878SArnaud Lacombe 2085c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2086c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2087c68e5878SArnaud Lacombe } 2088c68e5878SArnaud Lacombe 2089bff5da43SRob Herring# check for DT compatible documentation 20907dd05b38SFlorian Vaussard if (defined $root && 20917dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 20927dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 20937dd05b38SFlorian Vaussard 2094bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2095bff5da43SRob Herring 2096cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2097cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2098cc93319bSFlorian Vaussard 2099bff5da43SRob Herring foreach my $compat (@compats) { 2100bff5da43SRob Herring my $compat2 = $compat; 2101185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2102185d566bSRob Herring my $compat3 = $compat; 2103185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2104185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2105bff5da43SRob Herring if ( $? >> 8 ) { 2106bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2107bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2108bff5da43SRob Herring } 2109bff5da43SRob Herring 21104fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 21114fbf32a6SFlorian Vaussard my $vendor = $1; 2112cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2113bff5da43SRob Herring if ( $? >> 8 ) { 2114bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2115cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2116bff5da43SRob Herring } 2117bff5da43SRob Herring } 2118bff5da43SRob Herring } 2119bff5da43SRob Herring 21205368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 21215368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 21225368df20SAndy Whitcroft 21236cd7f386SJoe Perches#line length limit 2124c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2125f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 21260fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 21278bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 21286cd7f386SJoe Perches $length > $max_line_length) 2129c45dcabdSAndy Whitcroft { 2130000d1cc1SJoe Perches WARN("LONG_LINE", 21316cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 21320a920b5bSAndy Whitcroft } 21330a920b5bSAndy Whitcroft 2134ca56dc09SJosh Triplett# Check for user-visible strings broken across lines, which breaks the ability 21358c5fcd24SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 21368c5fcd24SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 21378c5fcd24SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 2138ca56dc09SJosh Triplett if ($line =~ /^\+\s*"/ && 2139ca56dc09SJosh Triplett $prevline =~ /"\s*$/ && 21408c5fcd24SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 2141ca56dc09SJosh Triplett WARN("SPLIT_STRING", 2142ca56dc09SJosh Triplett "quoted string split across lines\n" . $hereprev); 2143ca56dc09SJosh Triplett } 2144ca56dc09SJosh Triplett 21455e79d96eSJoe Perches# check for spaces before a quoted newline 21465e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 21473705ce5bSJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 21483705ce5bSJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 21493705ce5bSJoe Perches $fix) { 21503705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 21513705ce5bSJoe Perches } 21523705ce5bSJoe Perches 21535e79d96eSJoe Perches } 21545e79d96eSJoe Perches 21558905a67cSAndy Whitcroft# check for adding lines without a newline. 21568905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2157000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2158000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 21598905a67cSAndy Whitcroft } 21608905a67cSAndy Whitcroft 216142e41c54SMike Frysinger# Blackfin: use hi/lo macros 216242e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 216342e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 216442e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2165000d1cc1SJoe Perches ERROR("LO_MACRO", 2166000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 216742e41c54SMike Frysinger } 216842e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 216942e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2170000d1cc1SJoe Perches ERROR("HI_MACRO", 2171000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 217242e41c54SMike Frysinger } 217342e41c54SMike Frysinger } 217442e41c54SMike Frysinger 2175b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2176b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 21770a920b5bSAndy Whitcroft 21780a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 21790a920b5bSAndy Whitcroft# more than 8 must use tabs. 2180c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2181c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2182c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2183d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 21843705ce5bSJoe Perches if (ERROR("CODE_INDENT", 21853705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 21863705ce5bSJoe Perches $fix) { 21873705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 21883705ce5bSJoe Perches } 21890a920b5bSAndy Whitcroft } 21900a920b5bSAndy Whitcroft 219108e44365SAlberto Panizzo# check for space before tabs. 219208e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 219308e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 21943705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 21953705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 21963705ce5bSJoe Perches $fix) { 2197c76f4cb3SJoe Perches while ($fixed[$linenr - 1] =~ 2198c76f4cb3SJoe Perches s/(^\+.*) {8,8}+\t/$1\t\t/) {} 2199c76f4cb3SJoe Perches while ($fixed[$linenr - 1] =~ 2200c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 22013705ce5bSJoe Perches } 220208e44365SAlberto Panizzo } 220308e44365SAlberto Panizzo 2204d1fe9c09SJoe Perches# check for && or || at the start of a line 2205d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2206d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2207d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2208d1fe9c09SJoe Perches } 2209d1fe9c09SJoe Perches 2210d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2211d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 221291cb5195SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 2213d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2214d1fe9c09SJoe Perches my $oldindent = $1; 2215d1fe9c09SJoe Perches my $rest = $2; 2216d1fe9c09SJoe Perches 2217d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2218d1fe9c09SJoe Perches if ($pos >= 0) { 2219b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2220b34a26f3SJoe Perches my $newindent = $2; 2221d1fe9c09SJoe Perches 2222d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2223d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2224d1fe9c09SJoe Perches " " x ($pos % 8); 2225d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2226d1fe9c09SJoe Perches 2227d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2228d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 22293705ce5bSJoe Perches 22303705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 22313705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 22323705ce5bSJoe Perches $fix && $line =~ /^\+/) { 22333705ce5bSJoe Perches $fixed[$linenr - 1] =~ 22343705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 22353705ce5bSJoe Perches } 2236d1fe9c09SJoe Perches } 2237d1fe9c09SJoe Perches } 2238d1fe9c09SJoe Perches } 2239d1fe9c09SJoe Perches 224023f780c9SJoe Perches if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { 22413705ce5bSJoe Perches if (CHK("SPACING", 22423705ce5bSJoe Perches "No space is necessary after a cast\n" . $hereprev) && 22433705ce5bSJoe Perches $fix) { 22443705ce5bSJoe Perches $fixed[$linenr - 1] =~ 22453705ce5bSJoe Perches s/^(\+.*\*[ \t]*\))[ \t]+/$1/; 22463705ce5bSJoe Perches } 2247aad4f614SJoe Perches } 2248aad4f614SJoe Perches 224905880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2250fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 225185ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 225285ad978cSJoe Perches $realline > 2) { 225305880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 225405880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 225505880600SJoe Perches } 225605880600SJoe Perches 225705880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2258a605e32eSJoe Perches $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* 2259a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 226061135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2261a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 2262a605e32eSJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2263a605e32eSJoe Perches "networking block comments start with * on subsequent lines\n" . $hereprev); 2264a605e32eSJoe Perches } 2265a605e32eSJoe Perches 2266a605e32eSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2267c24f9f19SJoe Perches $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2268c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2269c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2270c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 227105880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 227205880600SJoe Perches "networking block comments put the trailing */ on a separate line\n" . $herecurr); 227305880600SJoe Perches } 227405880600SJoe Perches 22753b617e3bSJoe Perches# check for missing blank lines after declarations 2276*3f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 2277*3f7bac03SJoe Perches # actual declarations 2278*3f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 2279*3f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 2280*3f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 2281*3f7bac03SJoe Perches # known declaration macros 2282*3f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 2283*3f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 2284*3f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 2285*3f7bac03SJoe Perches # other possible extensions of declaration lines 2286*3f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 2287*3f7bac03SJoe Perches # not starting a section or a macro "\" extended line 2288*3f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 2289*3f7bac03SJoe Perches # looks like a declaration 2290*3f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 2291*3f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 2292*3f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 2293*3f7bac03SJoe Perches # known declaration macros 2294*3f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 2295*3f7bac03SJoe Perches # start of struct or union or enum 22963b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 2297*3f7bac03SJoe Perches # start or end of block or continuation of declaration 2298*3f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 2299*3f7bac03SJoe Perches # bitfield continuation 2300*3f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 2301*3f7bac03SJoe Perches # other possible extensions of declaration lines 2302*3f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 2303*3f7bac03SJoe Perches # indentation of previous and current line are the same 2304*3f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 23053b617e3bSJoe Perches WARN("SPACING", 2306*3f7bac03SJoe Perches "Missing a blank line after declarations\n" . $hereprev); 23073b617e3bSJoe Perches } 23083b617e3bSJoe Perches 23095f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 23106b4c5bebSAndy Whitcroft# Exceptions: 23116b4c5bebSAndy Whitcroft# 1) within comments 23126b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 23136b4c5bebSAndy Whitcroft# 3) hanging labels 23143705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 23155f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 23163705ce5bSJoe Perches if (WARN("LEADING_SPACE", 23173705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 23183705ce5bSJoe Perches $fix) { 23193705ce5bSJoe Perches $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 23203705ce5bSJoe Perches } 23215f7ddae6SRaffaele Recalcati } 23225f7ddae6SRaffaele Recalcati 2323b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 2324b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 2325b9ea10d6SAndy Whitcroft 23261ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 23271ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 23281ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 23291ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 23301ba8dfd1SKees Cook } 23311ba8dfd1SKees Cook 2332c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 2333cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 2334000d1cc1SJoe Perches WARN("CVS_KEYWORD", 2335000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 2336c2fdda0dSAndy Whitcroft } 233722f2a2efSAndy Whitcroft 233842e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 233942e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 234042e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2341000d1cc1SJoe Perches ERROR("CSYNC", 2342000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 234342e41c54SMike Frysinger } 234442e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 234542e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2346000d1cc1SJoe Perches ERROR("SSYNC", 2347000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 234842e41c54SMike Frysinger } 234942e41c54SMike Frysinger 235056e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 235156e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 235256e77d70SJoe Perches WARN("HOTPLUG_SECTION", 235356e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 235456e77d70SJoe Perches } 235556e77d70SJoe Perches 23569c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 23572b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 23582b474a1aSAndy Whitcroft $realline_next); 23593e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 23603e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 23611b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 2362170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2363f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2364171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 2365171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 2366171ae1a4SAndy Whitcroft 23673e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 23683e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 23693e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 23703e469cdcSAndy Whitcroft # until we hit end of it. 23713e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 23723e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 23733e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 23743e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 23753e469cdcSAndy Whitcroft } 2376f74bd194SAndy Whitcroft 23772b474a1aSAndy Whitcroft # Find the real next line. 23782b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 23792b474a1aSAndy Whitcroft if (defined $realline_next && 23802b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 23812b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 23822b474a1aSAndy Whitcroft $realline_next++; 23832b474a1aSAndy Whitcroft } 23842b474a1aSAndy Whitcroft 2385171ae1a4SAndy Whitcroft my $s = $stat; 2386171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 2387cf655043SAndy Whitcroft 2388c2fdda0dSAndy Whitcroft # Ignore goto labels. 2389171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 2390c2fdda0dSAndy Whitcroft 2391c2fdda0dSAndy Whitcroft # Ignore functions being called 2392171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 2393c2fdda0dSAndy Whitcroft 2394463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 2395463f2864SAndy Whitcroft 2396c45dcabdSAndy Whitcroft # declarations always start with types 2397d2506586SAndy 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) { 2398c45dcabdSAndy Whitcroft my $type = $1; 2399c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 2400c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 2401c45dcabdSAndy Whitcroft 24026c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 2403a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 2404c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 2405c2fdda0dSAndy Whitcroft } 24068905a67cSAndy Whitcroft 24076c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 240865863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 2409c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 24109c0ca6f9SAndy Whitcroft } 24118905a67cSAndy Whitcroft 24128905a67cSAndy Whitcroft # Check for any sort of function declaration. 24138905a67cSAndy Whitcroft # int foo(something bar, other baz); 24148905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 2415171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 24168905a67cSAndy Whitcroft my ($name_len) = length($1); 24178905a67cSAndy Whitcroft 2418cf655043SAndy Whitcroft my $ctx = $s; 2419773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 24208905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 2421cf655043SAndy Whitcroft 24228905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 2423c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 24248905a67cSAndy Whitcroft 2425c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 24268905a67cSAndy Whitcroft } 24278905a67cSAndy Whitcroft } 24288905a67cSAndy Whitcroft } 24298905a67cSAndy Whitcroft 24309c0ca6f9SAndy Whitcroft } 24319c0ca6f9SAndy Whitcroft 243200df344fSAndy Whitcroft# 243300df344fSAndy Whitcroft# Checks which may be anchored in the context. 243400df344fSAndy Whitcroft# 243500df344fSAndy Whitcroft 243600df344fSAndy Whitcroft# Check for switch () and associated case and default 243700df344fSAndy Whitcroft# statements should be at the same indent. 243800df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 243900df344fSAndy Whitcroft my $err = ''; 244000df344fSAndy Whitcroft my $sep = ''; 244100df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 244200df344fSAndy Whitcroft shift(@ctx); 244300df344fSAndy Whitcroft for my $ctx (@ctx) { 244400df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 244500df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 244600df344fSAndy Whitcroft $indent != $cindent) { 244700df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 244800df344fSAndy Whitcroft $sep = ''; 244900df344fSAndy Whitcroft } else { 245000df344fSAndy Whitcroft $sep = "[...]\n"; 245100df344fSAndy Whitcroft } 245200df344fSAndy Whitcroft } 245300df344fSAndy Whitcroft if ($err ne '') { 2454000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2455000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2456de7d4f0eSAndy Whitcroft } 2457de7d4f0eSAndy Whitcroft } 2458de7d4f0eSAndy Whitcroft 2459de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2460de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 2461c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2462773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2463773647a0SAndy Whitcroft 24649c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 24658eef05ddSJoe Perches 24668eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 24678eef05ddSJoe Perches WARN("DEEP_INDENTATION", 24688eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 24698eef05ddSJoe Perches } 24708eef05ddSJoe Perches 2471de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2472de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 2473de7d4f0eSAndy Whitcroft 2474548596d5SAndy Whitcroft my $ctx_ln = $linenr; 2475548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 2476de7d4f0eSAndy Whitcroft 2477548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2478548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 2479548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 2480548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2481548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2482773647a0SAndy Whitcroft $ctx_ln++; 2483773647a0SAndy Whitcroft } 2484548596d5SAndy Whitcroft 248553210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 248653210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2487773647a0SAndy Whitcroft 2488773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 2489000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2490000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 249101464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 249200df344fSAndy Whitcroft } 2493773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2494773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 2495773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 2496773647a0SAndy Whitcroft { 24979c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 24989c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 2499000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 2500000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 250101464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 25029c0ca6f9SAndy Whitcroft } 25039c0ca6f9SAndy Whitcroft } 250400df344fSAndy Whitcroft } 250500df344fSAndy Whitcroft 25064d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 25074d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 25083e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 25093e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 25103e469cdcSAndy Whitcroft if (!defined $stat); 25114d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 25124d001e4dSAndy Whitcroft 25134d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 25144d001e4dSAndy Whitcroft 25154d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 25164d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 25174d001e4dSAndy Whitcroft # where necessary. 25184d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 25194d001e4dSAndy Whitcroft 25204d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 25216f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 25226f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 25234d001e4dSAndy Whitcroft 25244d001e4dSAndy Whitcroft # We want to check the first line inside the block 25254d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 25264d001e4dSAndy Whitcroft # 1) any blank line termination 25274d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 25284d001e4dSAndy Whitcroft # 3) any do (...) { 25294d001e4dSAndy Whitcroft my $continuation = 0; 25304d001e4dSAndy Whitcroft my $check = 0; 25314d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 25324d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 25334d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 25344d001e4dSAndy Whitcroft $continuation = 1; 25354d001e4dSAndy Whitcroft } 25369bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 25374d001e4dSAndy Whitcroft $check = 1; 25384d001e4dSAndy Whitcroft $cond_lines++; 25394d001e4dSAndy Whitcroft } 25404d001e4dSAndy Whitcroft 25414d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 25424d001e4dSAndy Whitcroft # preprocessor statement. 25434d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 25444d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 25454d001e4dSAndy Whitcroft $check = 0; 25464d001e4dSAndy Whitcroft } 25474d001e4dSAndy Whitcroft 25489bd49efeSAndy Whitcroft my $cond_ptr = -1; 2549740504c6SAndy Whitcroft $continuation = 0; 25509bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 25519bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 25524d001e4dSAndy Whitcroft 2553f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 2554f16fa28fSAndy Whitcroft # is not linear. 2555f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2556f16fa28fSAndy Whitcroft $check = 0; 2557f16fa28fSAndy Whitcroft } 2558f16fa28fSAndy Whitcroft 25599bd49efeSAndy Whitcroft # Ignore: 25609bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 25619bd49efeSAndy Whitcroft # 2) preprocessor lines, and 25629bd49efeSAndy Whitcroft # 3) labels. 2563740504c6SAndy Whitcroft if ($continuation || 2564740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 25659bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 25669bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 2567740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 256830dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 25699bd49efeSAndy Whitcroft $cond_lines++; 25709bd49efeSAndy Whitcroft } 25714d001e4dSAndy Whitcroft } 257230dad6ebSAndy Whitcroft } 25734d001e4dSAndy Whitcroft 25744d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 25754d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 25764d001e4dSAndy Whitcroft 25774d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 25784d001e4dSAndy Whitcroft # this is not this patch's fault. 25794d001e4dSAndy Whitcroft if (!defined($stat_real) || 25804d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 25814d001e4dSAndy Whitcroft $check = 0; 25824d001e4dSAndy Whitcroft } 25834d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 25844d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 25854d001e4dSAndy Whitcroft } 25864d001e4dSAndy Whitcroft 25879bd49efeSAndy 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"; 25884d001e4dSAndy Whitcroft 25894d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 25904d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 2591000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 2592000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 25934d001e4dSAndy Whitcroft } 25944d001e4dSAndy Whitcroft } 25954d001e4dSAndy Whitcroft 25966c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 25976c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 25981f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 25991f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 26006c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 2601c2fdda0dSAndy Whitcroft if ($dbg_values) { 2602c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 2603cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 2604cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 26051f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 2606c2fdda0dSAndy Whitcroft } 26076c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 26086c72ffaaSAndy Whitcroft 260900df344fSAndy Whitcroft#ignore lines not being added 26103705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 261100df344fSAndy Whitcroft 2612653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 26137429c690SAndy Whitcroft if ($dbg_type) { 26147429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 2615000d1cc1SJoe Perches ERROR("TEST_TYPE", 2616000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 26177429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2618000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 2619000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 26207429c690SAndy Whitcroft } 2621653d4876SAndy Whitcroft next; 2622653d4876SAndy Whitcroft } 2623a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2624a1ef277eSAndy Whitcroft if ($dbg_attr) { 26259360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2626000d1cc1SJoe Perches ERROR("TEST_ATTR", 2627000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 26289360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2629000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2630000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2631a1ef277eSAndy Whitcroft } 2632a1ef277eSAndy Whitcroft next; 2633a1ef277eSAndy Whitcroft } 2634653d4876SAndy Whitcroft 2635f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 263699423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 263799423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2638000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2639000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . $hereprev); 2640f0a594c1SAndy Whitcroft } 2641f0a594c1SAndy Whitcroft 264200df344fSAndy Whitcroft# 264300df344fSAndy Whitcroft# Checks which are anchored on the added line. 264400df344fSAndy Whitcroft# 264500df344fSAndy Whitcroft 2646653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2647c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2648653d4876SAndy Whitcroft my $path = $1; 2649653d4876SAndy Whitcroft if ($path =~ m{//}) { 2650000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2651495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 2652495e9d84SJoe Perches } 2653495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 2654495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 2655495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 2656653d4876SAndy Whitcroft } 2657653d4876SAndy Whitcroft } 2658653d4876SAndy Whitcroft 265900df344fSAndy Whitcroft# no C99 // comments 266000df344fSAndy Whitcroft if ($line =~ m{//}) { 26613705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 26623705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 26633705ce5bSJoe Perches $fix) { 26643705ce5bSJoe Perches my $line = $fixed[$linenr - 1]; 26653705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 26663705ce5bSJoe Perches my $comment = trim($1); 26673705ce5bSJoe Perches $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; 26683705ce5bSJoe Perches } 26693705ce5bSJoe Perches } 267000df344fSAndy Whitcroft } 267100df344fSAndy Whitcroft # Remove C99 comments. 26720a920b5bSAndy Whitcroft $line =~ s@//.*@@; 26736c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 26740a920b5bSAndy Whitcroft 26752b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 26762b474a1aSAndy Whitcroft# the whole statement. 26772b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 26782b474a1aSAndy Whitcroft if (defined $realline_next && 26792b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 26802b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 26812b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 26822b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 26833cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 26843cbf62dfSAndy Whitcroft # a prefix: 26853cbf62dfSAndy Whitcroft # XXX(foo); 26863cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 2687653d4876SAndy Whitcroft my $name = $1; 268887a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 26893cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 26903cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 26913cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 26923cbf62dfSAndy Whitcroft 26933cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 26942b474a1aSAndy Whitcroft \n.}\s*$| 269548012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 269648012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 269748012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 26982b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 26992b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 270048012058SAndy Whitcroft )/x) { 27012b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 27022b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 27032b474a1aSAndy Whitcroft } else { 27042b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 27050a920b5bSAndy Whitcroft } 27060a920b5bSAndy Whitcroft } 27072b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 27082b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 27092b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 27102b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 27112b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 27122b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 27132b474a1aSAndy Whitcroft } 27142b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 27152b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 2716000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 2717000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 27182b474a1aSAndy Whitcroft } 27190a920b5bSAndy Whitcroft 27205150bda4SJoe Eloff# check for global initialisers. 2721d5e616fcSJoe Perches if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { 2722d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 2723000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 2724d5e616fcSJoe Perches $herecurr) && 2725d5e616fcSJoe Perches $fix) { 2726d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; 2727d5e616fcSJoe Perches } 2728f0a594c1SAndy Whitcroft } 27290a920b5bSAndy Whitcroft# check for static initialisers. 2730d5e616fcSJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2731d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 2732000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 2733d5e616fcSJoe Perches $herecurr) && 2734d5e616fcSJoe Perches $fix) { 2735d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; 2736d5e616fcSJoe Perches } 27370a920b5bSAndy Whitcroft } 27380a920b5bSAndy Whitcroft 2739cb710ecaSJoe Perches# check for static const char * arrays. 2740cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 2741000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2742000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 2743cb710ecaSJoe Perches $herecurr); 2744cb710ecaSJoe Perches } 2745cb710ecaSJoe Perches 2746cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 2747cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 2748000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 2749000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 2750cb710ecaSJoe Perches $herecurr); 2751cb710ecaSJoe Perches } 2752cb710ecaSJoe Perches 27539b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 27549b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 27559b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 27569b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 27579b0fa60dSJoe Perches $herecurr); 27589b0fa60dSJoe Perches } 27599b0fa60dSJoe Perches 2760b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 2761b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 2762b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 2763b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 2764b36190c5SJoe Perches $fix) { 2765b36190c5SJoe Perches $fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 2766b36190c5SJoe Perches } 2767b36190c5SJoe Perches } 2768b36190c5SJoe Perches 276992e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE 277092e112fdSJoe Perches if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { 277192e112fdSJoe Perches if (WARN("DEFINE_PCI_DEVICE_TABLE", 277292e112fdSJoe Perches "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && 277392e112fdSJoe Perches $fix) { 277492e112fdSJoe 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\[\] = /; 277592e112fdSJoe Perches } 277693ed0e2dSJoe Perches } 277793ed0e2dSJoe Perches 2778653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 2779653d4876SAndy Whitcroft# make sense. 2780653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 27818054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 2782c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 27838ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 2784653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 2785000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 2786000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 27870a920b5bSAndy Whitcroft } 27880a920b5bSAndy Whitcroft 27890a920b5bSAndy Whitcroft# * goes on variable not on type 279065863862SAndy Whitcroft # (char*[ const]) 2791bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2792bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 27933705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 2794d8aaf121SAndy Whitcroft 279565863862SAndy Whitcroft # Should start with a space. 279665863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 279765863862SAndy Whitcroft # Should not end with a space. 279865863862SAndy Whitcroft $to =~ s/\s+$//; 279965863862SAndy Whitcroft # '*'s should not have spaces between. 2800f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 280165863862SAndy Whitcroft } 2802d8aaf121SAndy Whitcroft 28033705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 280465863862SAndy Whitcroft if ($from ne $to) { 28053705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 28063705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 28073705ce5bSJoe Perches $fix) { 28083705ce5bSJoe Perches my $sub_from = $ident; 28093705ce5bSJoe Perches my $sub_to = $ident; 28103705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 28113705ce5bSJoe Perches $fixed[$linenr - 1] =~ 28123705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 28133705ce5bSJoe Perches } 281465863862SAndy Whitcroft } 2815bfcb2cc7SAndy Whitcroft } 2816bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2817bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 28183705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 2819d8aaf121SAndy Whitcroft 282065863862SAndy Whitcroft # Should start with a space. 282165863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 282265863862SAndy Whitcroft # Should not end with a space. 282365863862SAndy Whitcroft $to =~ s/\s+$//; 282465863862SAndy Whitcroft # '*'s should not have spaces between. 2825f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 282665863862SAndy Whitcroft } 282765863862SAndy Whitcroft # Modifiers should have spaces. 282865863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 282965863862SAndy Whitcroft 28303705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 2831667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 28323705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 28333705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 28343705ce5bSJoe Perches $fix) { 28353705ce5bSJoe Perches 28363705ce5bSJoe Perches my $sub_from = $match; 28373705ce5bSJoe Perches my $sub_to = $match; 28383705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 28393705ce5bSJoe Perches $fixed[$linenr - 1] =~ 28403705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 28413705ce5bSJoe Perches } 284265863862SAndy Whitcroft } 28430a920b5bSAndy Whitcroft } 28440a920b5bSAndy Whitcroft 28450a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 28460a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 28470a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 28480a920b5bSAndy Whitcroft# print "$herecurr"; 28490a920b5bSAndy Whitcroft# $clean = 0; 28500a920b5bSAndy Whitcroft# } 28510a920b5bSAndy Whitcroft 28528905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 2853000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 2854000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 28558905a67cSAndy Whitcroft } 28568905a67cSAndy Whitcroft 285717441227SJoe Perches# check for uses of printk_ratelimit 285817441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 2859000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 2860000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 286117441227SJoe Perches } 286217441227SJoe Perches 286300df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 286400df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 286500df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 286625985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 286700df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 2868f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 286900df344fSAndy Whitcroft my $ok = 0; 287000df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 287100df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 287225985edcSLucas De Marchi # we have a preceding printk if it ends 287300df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 287400df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 287500df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 287600df344fSAndy Whitcroft $ok = 1; 287700df344fSAndy Whitcroft } 287800df344fSAndy Whitcroft last; 287900df344fSAndy Whitcroft } 288000df344fSAndy Whitcroft } 288100df344fSAndy Whitcroft if ($ok == 0) { 2882000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 2883000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 28840a920b5bSAndy Whitcroft } 288500df344fSAndy Whitcroft } 28860a920b5bSAndy Whitcroft 2887243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 2888243f3803SJoe Perches my $orig = $1; 2889243f3803SJoe Perches my $level = lc($orig); 2890243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 28918f26b837SJoe Perches my $level2 = $level; 28928f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 2893243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 2894daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 2895243f3803SJoe Perches } 2896243f3803SJoe Perches 2897243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 2898d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 2899d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 2900d5e616fcSJoe Perches $fix) { 2901d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 2902d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 2903d5e616fcSJoe Perches } 2904243f3803SJoe Perches } 2905243f3803SJoe Perches 2906dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 2907dc139313SJoe Perches my $orig = $1; 2908dc139313SJoe Perches my $level = lc($orig); 2909dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 2910dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 2911dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 2912dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 2913dc139313SJoe Perches } 2914dc139313SJoe Perches 2915653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 2916653d4876SAndy Whitcroft# or if closed on same line 2917c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2918c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 2919000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2920000d1cc1SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr); 29210a920b5bSAndy Whitcroft } 2922653d4876SAndy Whitcroft 29238905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 29248905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 29258905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 2926000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2927000d1cc1SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev); 29288905a67cSAndy Whitcroft } 29298905a67cSAndy Whitcroft 29300c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 29313705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 29323705ce5bSJoe Perches if (WARN("SPACING", 29333705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 29343705ce5bSJoe Perches $fix) { 29353705ce5bSJoe Perches $fixed[$linenr - 1] =~ 29363705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 29373705ce5bSJoe Perches } 29380c73b4ebSAndy Whitcroft } 29390c73b4ebSAndy Whitcroft 294031070b5dSJoe Perches# Function pointer declarations 294131070b5dSJoe Perches# check spacing between type, funcptr, and args 294231070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 294391f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 294431070b5dSJoe Perches my $declare = $1; 294531070b5dSJoe Perches my $pre_pointer_space = $2; 294631070b5dSJoe Perches my $post_pointer_space = $3; 294731070b5dSJoe Perches my $funcname = $4; 294831070b5dSJoe Perches my $post_funcname_space = $5; 294931070b5dSJoe Perches my $pre_args_space = $6; 295031070b5dSJoe Perches 295191f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 295291f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 295391f72e9cSJoe Perches# don't need a space so don't warn for those. 295491f72e9cSJoe Perches my $post_declare_space = ""; 295591f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 295691f72e9cSJoe Perches $post_declare_space = $1; 295791f72e9cSJoe Perches $declare = rtrim($declare); 295891f72e9cSJoe Perches } 295991f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 296031070b5dSJoe Perches WARN("SPACING", 296131070b5dSJoe Perches "missing space after return type\n" . $herecurr); 296291f72e9cSJoe Perches $post_declare_space = " "; 296331070b5dSJoe Perches } 296431070b5dSJoe Perches 296531070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 296691f72e9cSJoe Perches# This test is not currently implemented because these declarations are 296791f72e9cSJoe Perches# equivalent to 296891f72e9cSJoe Perches# int foo(int bar, ...) 296991f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 297091f72e9cSJoe Perches# 297191f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 297291f72e9cSJoe Perches# WARN("SPACING", 297391f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 297491f72e9cSJoe Perches# } 297531070b5dSJoe Perches 297631070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 297731070b5dSJoe Perches if (defined $pre_pointer_space && 297831070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 297931070b5dSJoe Perches WARN("SPACING", 298031070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 298131070b5dSJoe Perches } 298231070b5dSJoe Perches 298331070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 298431070b5dSJoe Perches if (defined $post_pointer_space && 298531070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 298631070b5dSJoe Perches WARN("SPACING", 298731070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 298831070b5dSJoe Perches } 298931070b5dSJoe Perches 299031070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 299131070b5dSJoe Perches if (defined $post_funcname_space && 299231070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 299331070b5dSJoe Perches WARN("SPACING", 299431070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 299531070b5dSJoe Perches } 299631070b5dSJoe Perches 299731070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 299831070b5dSJoe Perches if (defined $pre_args_space && 299931070b5dSJoe Perches $pre_args_space =~ /^\s/) { 300031070b5dSJoe Perches WARN("SPACING", 300131070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 300231070b5dSJoe Perches } 300331070b5dSJoe Perches 300431070b5dSJoe Perches if (show_type("SPACING") && $fix) { 300531070b5dSJoe Perches $fixed[$linenr - 1] =~ 300691f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 300731070b5dSJoe Perches } 300831070b5dSJoe Perches } 300931070b5dSJoe Perches 30108d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 30118d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 3012fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 3013fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 30148d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 30158d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 30168d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 3017fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 3018daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 30193705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 30203705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 30213705ce5bSJoe Perches $fix) { 30223705ce5bSJoe Perches $fixed[$linenr - 1] =~ 30233705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 30243705ce5bSJoe Perches } 30258d31cfceSAndy Whitcroft } 30268d31cfceSAndy Whitcroft } 30278d31cfceSAndy Whitcroft 3028f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 30296c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 3030c2fdda0dSAndy Whitcroft my $name = $1; 3031773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 3032773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 3033c2fdda0dSAndy Whitcroft 3034c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 3035773647a0SAndy Whitcroft if ($name =~ /^(?: 3036773647a0SAndy Whitcroft if|for|while|switch|return|case| 3037773647a0SAndy Whitcroft volatile|__volatile__| 3038773647a0SAndy Whitcroft __attribute__|format|__extension__| 3039773647a0SAndy Whitcroft asm|__asm__)$/x) 3040773647a0SAndy Whitcroft { 3041c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 3042c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 3043c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 3044c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 3045773647a0SAndy Whitcroft 3046773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 3047c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 3048c2fdda0dSAndy Whitcroft 3049c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 3050c2fdda0dSAndy Whitcroft # likely a typedef for a function. 3051773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 3052c2fdda0dSAndy Whitcroft 3053c2fdda0dSAndy Whitcroft } else { 30543705ce5bSJoe Perches if (WARN("SPACING", 30553705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 30563705ce5bSJoe Perches $fix) { 30573705ce5bSJoe Perches $fixed[$linenr - 1] =~ 30583705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 30593705ce5bSJoe Perches } 3060f0a594c1SAndy Whitcroft } 30616c72ffaaSAndy Whitcroft } 30629a4cad4eSEric Nelson 3063653d4876SAndy Whitcroft# Check operator spacing. 30640a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 30653705ce5bSJoe Perches my $fixed_line = ""; 30663705ce5bSJoe Perches my $line_fixed = 0; 30673705ce5bSJoe Perches 30689c0ca6f9SAndy Whitcroft my $ops = qr{ 30699c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 30709c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 30719c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 30721f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 307384731623SJoe Perches \?:|\?|: 30749c0ca6f9SAndy Whitcroft }x; 3075cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 30763705ce5bSJoe Perches 30773705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 30783705ce5bSJoe Perches## foreach my $el (@elements) { 30793705ce5bSJoe Perches## print("el: <$el>\n"); 30803705ce5bSJoe Perches## } 30813705ce5bSJoe Perches 30823705ce5bSJoe Perches my @fix_elements = (); 308300df344fSAndy Whitcroft my $off = 0; 30846c72ffaaSAndy Whitcroft 30853705ce5bSJoe Perches foreach my $el (@elements) { 30863705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 30873705ce5bSJoe Perches $off += length($el); 30883705ce5bSJoe Perches } 30893705ce5bSJoe Perches 30903705ce5bSJoe Perches $off = 0; 30913705ce5bSJoe Perches 30926c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 3093b34c648bSJoe Perches my $last_after = -1; 30946c72ffaaSAndy Whitcroft 30950a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 30963705ce5bSJoe Perches 30973705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 30983705ce5bSJoe Perches 30993705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 31003705ce5bSJoe Perches 31014a0df2efSAndy Whitcroft $off += length($elements[$n]); 31024a0df2efSAndy Whitcroft 310325985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 3104773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 3105773647a0SAndy Whitcroft my $cc = ''; 3106773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 3107773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 3108773647a0SAndy Whitcroft } 3109773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 3110773647a0SAndy Whitcroft 31114a0df2efSAndy Whitcroft my $a = ''; 31124a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 31134a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 3114cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 31154a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 31164a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 3117773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 31184a0df2efSAndy Whitcroft 31190a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 31204a0df2efSAndy Whitcroft 31214a0df2efSAndy Whitcroft my $c = ''; 31220a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 31234a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 31244a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 3125cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 31264a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 31274a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 31288b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 31294a0df2efSAndy Whitcroft } else { 31304a0df2efSAndy Whitcroft $c = 'E'; 31310a920b5bSAndy Whitcroft } 31320a920b5bSAndy Whitcroft 31334a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 31344a0df2efSAndy Whitcroft 31354a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 31364a0df2efSAndy Whitcroft 31376c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 3138de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 31390a920b5bSAndy Whitcroft 314074048ed8SAndy Whitcroft # Pull out the value of this operator. 31416c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 31420a920b5bSAndy Whitcroft 31431f65f947SAndy Whitcroft # Get the full operator variant. 31441f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 31451f65f947SAndy Whitcroft 314613214adfSAndy Whitcroft # Ignore operators passed as parameters. 314713214adfSAndy Whitcroft if ($op_type ne 'V' && 314813214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 314913214adfSAndy Whitcroft 3150cf655043SAndy Whitcroft# # Ignore comments 3151cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 315213214adfSAndy Whitcroft 3153d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 315413214adfSAndy Whitcroft } elsif ($op eq ';') { 3155cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 3156cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 31573705ce5bSJoe Perches if (ERROR("SPACING", 31583705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3159b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 31603705ce5bSJoe Perches $line_fixed = 1; 31613705ce5bSJoe Perches } 3162d8aaf121SAndy Whitcroft } 3163d8aaf121SAndy Whitcroft 3164d8aaf121SAndy Whitcroft # // is a comment 3165d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 31660a920b5bSAndy Whitcroft 3167b00e4814SJoe Perches # : when part of a bitfield 3168b00e4814SJoe Perches } elsif ($opv eq ':B') { 3169b00e4814SJoe Perches # skip the bitfield test for now 3170b00e4814SJoe Perches 31711f65f947SAndy Whitcroft # No spaces for: 31721f65f947SAndy Whitcroft # -> 3173b00e4814SJoe Perches } elsif ($op eq '->') { 31744a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 31753705ce5bSJoe Perches if (ERROR("SPACING", 31763705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 3177b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 31783705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 31793705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 31803705ce5bSJoe Perches } 3181b34c648bSJoe Perches $line_fixed = 1; 31823705ce5bSJoe Perches } 31830a920b5bSAndy Whitcroft } 31840a920b5bSAndy Whitcroft 31850a920b5bSAndy Whitcroft # , must have a space on the right. 31860a920b5bSAndy Whitcroft } elsif ($op eq ',') { 3187cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 31883705ce5bSJoe Perches if (ERROR("SPACING", 31893705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3190b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 31913705ce5bSJoe Perches $line_fixed = 1; 3192b34c648bSJoe Perches $last_after = $n; 31933705ce5bSJoe Perches } 31940a920b5bSAndy Whitcroft } 31950a920b5bSAndy Whitcroft 31969c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 319774048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 31989c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 31999c0ca6f9SAndy Whitcroft 32009c0ca6f9SAndy Whitcroft # unary operators should have a space before and 32019c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 32029c0ca6f9SAndy Whitcroft # unary operator, or a cast 32039c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 320474048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 32050d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 3206cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 32073705ce5bSJoe Perches if (ERROR("SPACING", 32083705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 3209b34c648bSJoe Perches if ($n != $last_after + 2) { 3210b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 32113705ce5bSJoe Perches $line_fixed = 1; 32123705ce5bSJoe Perches } 32130a920b5bSAndy Whitcroft } 3214b34c648bSJoe Perches } 3215a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 3216171ae1a4SAndy Whitcroft # A unary '*' may be const 3217171ae1a4SAndy Whitcroft 3218171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 32193705ce5bSJoe Perches if (ERROR("SPACING", 32203705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3221b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 32223705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 32233705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 32243705ce5bSJoe Perches } 3225b34c648bSJoe Perches $line_fixed = 1; 32263705ce5bSJoe Perches } 32270a920b5bSAndy Whitcroft } 32280a920b5bSAndy Whitcroft 32290a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 32300a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 3231773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 32323705ce5bSJoe Perches if (ERROR("SPACING", 32333705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 3234b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 32353705ce5bSJoe Perches $line_fixed = 1; 32363705ce5bSJoe Perches } 32370a920b5bSAndy Whitcroft } 3238773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 3239773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 32403705ce5bSJoe Perches if (ERROR("SPACING", 32413705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3242b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 32433705ce5bSJoe Perches $line_fixed = 1; 32443705ce5bSJoe Perches } 3245653d4876SAndy Whitcroft } 3246773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 32473705ce5bSJoe Perches if (ERROR("SPACING", 32483705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3249b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 32503705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 32513705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3252773647a0SAndy Whitcroft } 3253b34c648bSJoe Perches $line_fixed = 1; 32543705ce5bSJoe Perches } 32553705ce5bSJoe Perches } 32560a920b5bSAndy Whitcroft 32570a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 32589c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 32599c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 32609c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 3261c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 3262c2fdda0dSAndy Whitcroft $op eq '%') 32630a920b5bSAndy Whitcroft { 3264773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 32653705ce5bSJoe Perches if (ERROR("SPACING", 32663705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 3267b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3268b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3269b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3270b34c648bSJoe Perches } 32713705ce5bSJoe Perches $line_fixed = 1; 32723705ce5bSJoe Perches } 32730a920b5bSAndy Whitcroft } 32740a920b5bSAndy Whitcroft 32751f65f947SAndy Whitcroft # A colon needs no spaces before when it is 32761f65f947SAndy Whitcroft # terminating a case value or a label. 32771f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 32781f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 32793705ce5bSJoe Perches if (ERROR("SPACING", 32803705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3281b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 32823705ce5bSJoe Perches $line_fixed = 1; 32833705ce5bSJoe Perches } 32841f65f947SAndy Whitcroft } 32851f65f947SAndy Whitcroft 32860a920b5bSAndy Whitcroft # All the others need spaces both sides. 3287cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 32881f65f947SAndy Whitcroft my $ok = 0; 32891f65f947SAndy Whitcroft 329022f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 32911f65f947SAndy Whitcroft if (($op eq '<' && 32921f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 32931f65f947SAndy Whitcroft ($op eq '>' && 32941f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 32951f65f947SAndy Whitcroft { 32961f65f947SAndy Whitcroft $ok = 1; 32971f65f947SAndy Whitcroft } 32981f65f947SAndy Whitcroft 329984731623SJoe Perches # messages are ERROR, but ?: are CHK 33001f65f947SAndy Whitcroft if ($ok == 0) { 330184731623SJoe Perches my $msg_type = \&ERROR; 330284731623SJoe Perches $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 330384731623SJoe Perches 330484731623SJoe Perches if (&{$msg_type}("SPACING", 33053705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 3306b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3307b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3308b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3309b34c648bSJoe Perches } 33103705ce5bSJoe Perches $line_fixed = 1; 33113705ce5bSJoe Perches } 33120a920b5bSAndy Whitcroft } 331322f2a2efSAndy Whitcroft } 33144a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 33153705ce5bSJoe Perches 33163705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 33173705ce5bSJoe Perches 33183705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 33190a920b5bSAndy Whitcroft } 33203705ce5bSJoe Perches 33213705ce5bSJoe Perches if (($#elements % 2) == 0) { 33223705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 33233705ce5bSJoe Perches } 33243705ce5bSJoe Perches 33253705ce5bSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { 33263705ce5bSJoe Perches $fixed[$linenr - 1] = $fixed_line; 33273705ce5bSJoe Perches } 33283705ce5bSJoe Perches 33293705ce5bSJoe Perches 33300a920b5bSAndy Whitcroft } 33310a920b5bSAndy Whitcroft 3332786b6326SJoe Perches# check for whitespace before a non-naked semicolon 3333d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 3334786b6326SJoe Perches if (WARN("SPACING", 3335786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 3336786b6326SJoe Perches $fix) { 3337786b6326SJoe Perches 1 while $fixed[$linenr - 1] =~ 3338786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 3339786b6326SJoe Perches } 3340786b6326SJoe Perches } 3341786b6326SJoe Perches 3342f0a594c1SAndy Whitcroft# check for multiple assignments 3343f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 3344000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 3345000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 3346f0a594c1SAndy Whitcroft } 3347f0a594c1SAndy Whitcroft 334822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 334922f2a2efSAndy Whitcroft## # continuation. 335022f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 335122f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 335222f2a2efSAndy Whitcroft## 335322f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 335422f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 335522f2a2efSAndy Whitcroft## my $ln = $line; 335622f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 335722f2a2efSAndy Whitcroft## } 335822f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 3359000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 3360000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 336122f2a2efSAndy Whitcroft## } 336222f2a2efSAndy Whitcroft## } 3363f0a594c1SAndy Whitcroft 33640a920b5bSAndy Whitcroft#need space before brace following if, while, etc 336522f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 336622f2a2efSAndy Whitcroft $line =~ /do{/) { 33673705ce5bSJoe Perches if (ERROR("SPACING", 33683705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 33693705ce5bSJoe Perches $fix) { 3370d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/; 33713705ce5bSJoe Perches } 3372de7d4f0eSAndy Whitcroft } 3373de7d4f0eSAndy Whitcroft 3374c4a62ef9SJoe Perches## # check for blank lines before declarations 3375c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 3376c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 3377c4a62ef9SJoe Perches## WARN("SPACING", 3378c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 3379c4a62ef9SJoe Perches## } 3380c4a62ef9SJoe Perches## 3381c4a62ef9SJoe Perches 3382de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 3383de7d4f0eSAndy Whitcroft# on the line 3384de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 3385d5e616fcSJoe Perches if (ERROR("SPACING", 3386d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 3387d5e616fcSJoe Perches $fix) { 3388d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3389d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 3390d5e616fcSJoe Perches } 33910a920b5bSAndy Whitcroft } 33920a920b5bSAndy Whitcroft 339322f2a2efSAndy Whitcroft# check spacing on square brackets 339422f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 33953705ce5bSJoe Perches if (ERROR("SPACING", 33963705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 33973705ce5bSJoe Perches $fix) { 33983705ce5bSJoe Perches $fixed[$linenr - 1] =~ 33993705ce5bSJoe Perches s/\[\s+/\[/; 34003705ce5bSJoe Perches } 340122f2a2efSAndy Whitcroft } 340222f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 34033705ce5bSJoe Perches if (ERROR("SPACING", 34043705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 34053705ce5bSJoe Perches $fix) { 34063705ce5bSJoe Perches $fixed[$linenr - 1] =~ 34073705ce5bSJoe Perches s/\s+\]/\]/; 34083705ce5bSJoe Perches } 340922f2a2efSAndy Whitcroft } 341022f2a2efSAndy Whitcroft 3411c45dcabdSAndy Whitcroft# check spacing on parentheses 34129c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 34139c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 34143705ce5bSJoe Perches if (ERROR("SPACING", 34153705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 34163705ce5bSJoe Perches $fix) { 34173705ce5bSJoe Perches $fixed[$linenr - 1] =~ 34183705ce5bSJoe Perches s/\(\s+/\(/; 34193705ce5bSJoe Perches } 342022f2a2efSAndy Whitcroft } 342113214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 3422c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 3423c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 34243705ce5bSJoe Perches if (ERROR("SPACING", 34253705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 34263705ce5bSJoe Perches $fix) { 34273705ce5bSJoe Perches $fixed[$linenr - 1] =~ 34283705ce5bSJoe Perches s/\s+\)/\)/; 34293705ce5bSJoe Perches } 343022f2a2efSAndy Whitcroft } 343122f2a2efSAndy Whitcroft 34320a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 34334a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 34340a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 34353705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 34363705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 34373705ce5bSJoe Perches $fix) { 34383705ce5bSJoe Perches $fixed[$linenr - 1] =~ 34393705ce5bSJoe Perches s/^(.)\s+/$1/; 34403705ce5bSJoe Perches } 34410a920b5bSAndy Whitcroft } 34420a920b5bSAndy Whitcroft 34435b9553abSJoe Perches# return is not a function 3444507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 3445c45dcabdSAndy Whitcroft my $spacing = $1; 3446507e5141SJoe Perches if ($^V && $^V ge 5.10.0 && 34475b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 34485b9553abSJoe Perches my $value = $1; 34495b9553abSJoe Perches $value = deparenthesize($value); 34505b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 3451000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 3452000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 34535b9553abSJoe Perches } 3454c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 3455000d1cc1SJoe Perches ERROR("SPACING", 3456000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 3457c45dcabdSAndy Whitcroft } 3458c45dcabdSAndy Whitcroft } 3459507e5141SJoe Perches 3460189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 3461189248d8SJoe Perches if ($^V && $^V ge 5.10.0 && 3462189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 3463189248d8SJoe Perches my $openparens = $1; 3464189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 3465189248d8SJoe Perches my $msg = ""; 3466189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 3467189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 3468189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 3469189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 3470189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 3471189248d8SJoe Perches } 3472189248d8SJoe Perches } 3473189248d8SJoe Perches 347453a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 347553a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 347653a3c448SAndy Whitcroft my $name = $1; 347753a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 3478000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 3479000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 348053a3c448SAndy Whitcroft } 348153a3c448SAndy Whitcroft } 3482c45dcabdSAndy Whitcroft 34830a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 34844a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 34853705ce5bSJoe Perches if (ERROR("SPACING", 34863705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 34873705ce5bSJoe Perches $fix) { 34883705ce5bSJoe Perches $fixed[$linenr - 1] =~ 34893705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 34903705ce5bSJoe Perches } 34910a920b5bSAndy Whitcroft } 34920a920b5bSAndy Whitcroft 3493f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 3494f5fe35ddSAndy Whitcroft# statements after the conditional. 3495170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 34963e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 34973e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 34983e469cdcSAndy Whitcroft if (!defined $stat); 3499170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 3500170d3a22SAndy Whitcroft $remain_next, $off_next); 3501170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 3502170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 3503170d3a22SAndy Whitcroft 3504170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 3505170d3a22SAndy Whitcroft # If the statement carries leading newlines, 3506170d3a22SAndy Whitcroft # then count those as offsets. 3507170d3a22SAndy Whitcroft my ($whitespace) = 3508170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 3509170d3a22SAndy Whitcroft my $offset = 3510170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 3511170d3a22SAndy Whitcroft 3512170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 3513170d3a22SAndy Whitcroft $offset} = 1; 3514170d3a22SAndy Whitcroft } 3515170d3a22SAndy Whitcroft } 3516170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 3517c11230f4SJoe Perches defined($stat) && defined($cond) && 3518170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 3519171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 35208905a67cSAndy Whitcroft 3521b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 3522000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 3523000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 35248905a67cSAndy Whitcroft } 35258905a67cSAndy Whitcroft 35268905a67cSAndy Whitcroft # Find out what is on the end of the line after the 35278905a67cSAndy Whitcroft # conditional. 3528773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 35298905a67cSAndy Whitcroft $s =~ s/\n.*//g; 353013214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 353153210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 353253210168SAndy Whitcroft $c !~ /}\s*while\s*/) 3533773647a0SAndy Whitcroft { 3534bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 3535bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 3536bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 353742bdf74cSHidetoshi Seto my $stat_real = ''; 3538bb44ad39SAndy Whitcroft 353942bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 354042bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 3541bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 3542bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 3543bb44ad39SAndy Whitcroft } 3544bb44ad39SAndy Whitcroft 3545000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3546000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 35478905a67cSAndy Whitcroft } 35488905a67cSAndy Whitcroft } 35498905a67cSAndy Whitcroft 355013214adfSAndy Whitcroft# Check for bitwise tests written as boolean 355113214adfSAndy Whitcroft if ($line =~ / 355213214adfSAndy Whitcroft (?: 355313214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 355413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 355513214adfSAndy Whitcroft (?:\&\&|\|\|) 355613214adfSAndy Whitcroft | 355713214adfSAndy Whitcroft (?:\&\&|\|\|) 355813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 355913214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 356013214adfSAndy Whitcroft )/x) 356113214adfSAndy Whitcroft { 3562000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 3563000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 356413214adfSAndy Whitcroft } 356513214adfSAndy Whitcroft 35668905a67cSAndy Whitcroft# if and else should not have general statements after it 356713214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 356813214adfSAndy Whitcroft my $s = $1; 356913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 357013214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 3571000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3572000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 35730a920b5bSAndy Whitcroft } 357413214adfSAndy Whitcroft } 357539667782SAndy Whitcroft# if should not continue a brace 357639667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 3577000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3578000d1cc1SJoe Perches "trailing statements should be on next line\n" . 357939667782SAndy Whitcroft $herecurr); 358039667782SAndy Whitcroft } 3581a1080bf8SAndy Whitcroft# case and default should not have general statements after them 3582a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 3583a1080bf8SAndy Whitcroft $line !~ /\G(?: 35843fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 3585a1080bf8SAndy Whitcroft \s*return\s+ 3586a1080bf8SAndy Whitcroft )/xg) 3587a1080bf8SAndy Whitcroft { 3588000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3589000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 3590a1080bf8SAndy Whitcroft } 35910a920b5bSAndy Whitcroft 35920a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 35930a920b5bSAndy Whitcroft # indent level to be relevant to each other. 35940a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 35950a920b5bSAndy Whitcroft $previndent == $indent) { 3596000d1cc1SJoe Perches ERROR("ELSE_AFTER_BRACE", 3597000d1cc1SJoe Perches "else should follow close brace '}'\n" . $hereprev); 35980a920b5bSAndy Whitcroft } 35990a920b5bSAndy Whitcroft 3600c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 3601c2fdda0dSAndy Whitcroft $previndent == $indent) { 3602c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 3603c2fdda0dSAndy Whitcroft 3604c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 3605c2fdda0dSAndy Whitcroft # conditional. 3606773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 3607c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 3608c2fdda0dSAndy Whitcroft 3609c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 3610000d1cc1SJoe Perches ERROR("WHILE_AFTER_BRACE", 3611000d1cc1SJoe Perches "while should follow close brace '}'\n" . $hereprev); 3612c2fdda0dSAndy Whitcroft } 3613c2fdda0dSAndy Whitcroft } 3614c2fdda0dSAndy Whitcroft 361595e2c602SJoe Perches#Specific variable tests 3616323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 3617323c1260SJoe Perches my $var = $1; 361895e2c602SJoe Perches 361995e2c602SJoe Perches#gcc binary extension 362095e2c602SJoe Perches if ($var =~ /^$Binary$/) { 3621d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 3622d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 3623d5e616fcSJoe Perches $fix) { 3624d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 3625d5e616fcSJoe Perches $fixed[$linenr - 1] =~ 3626d5e616fcSJoe Perches s/\b$var\b/$hexval/; 3627d5e616fcSJoe Perches } 362895e2c602SJoe Perches } 362995e2c602SJoe Perches 363095e2c602SJoe Perches#CamelCase 3631807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 3632be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 363322735ce8SJoe Perches#Ignore Page<foo> variants 3634807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 363522735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 36363445686aSJoe Perches $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { 36377e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 36387e781f67SJoe Perches my $word = $1; 36397e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 3640d8b07710SJoe Perches if ($check) { 3641d8b07710SJoe Perches seed_camelcase_includes(); 3642d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 3643d8b07710SJoe Perches seed_camelcase_file($realfile); 3644d8b07710SJoe Perches $camelcase_file_seeded = 1; 3645d8b07710SJoe Perches } 3646d8b07710SJoe Perches } 36477e781f67SJoe Perches if (!defined $camelcase{$word}) { 36487e781f67SJoe Perches $camelcase{$word} = 1; 3649be79794bSJoe Perches CHK("CAMELCASE", 36507e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 36517e781f67SJoe Perches } 3652323c1260SJoe Perches } 3653323c1260SJoe Perches } 36543445686aSJoe Perches } 36550a920b5bSAndy Whitcroft 36560a920b5bSAndy Whitcroft#no spaces allowed after \ in define 3657d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 3658d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 3659d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 3660d5e616fcSJoe Perches $fix) { 3661d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\s+$//; 3662d5e616fcSJoe Perches } 36630a920b5bSAndy Whitcroft } 36640a920b5bSAndy Whitcroft 3665653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 3666c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 3667e09dec48SAndy Whitcroft my $file = "$1.h"; 3668e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 3669e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 3670e09dec48SAndy Whitcroft $realfile ne $checkfile && 36717840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 3672c45dcabdSAndy Whitcroft { 3673e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 3674000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 3675000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3676e09dec48SAndy Whitcroft } else { 3677000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 3678000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 3679e09dec48SAndy Whitcroft } 36800a920b5bSAndy Whitcroft } 36810a920b5bSAndy Whitcroft } 36820a920b5bSAndy Whitcroft 3683653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 3684653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 3685cf655043SAndy Whitcroft# in a known good container 3686b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 3687b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 3688d8aaf121SAndy Whitcroft my $ln = $linenr; 3689d8aaf121SAndy Whitcroft my $cnt = $realcnt; 3690c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 3691c45dcabdSAndy Whitcroft my $ctx = ''; 3692c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 3693f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3694f74bd194SAndy Whitcroft $ctx = $dstat; 3695c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 3696a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 3697c45dcabdSAndy Whitcroft 3698f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 3699292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 3700c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 3701c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 3702c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 3703c45dcabdSAndy Whitcroft 3704c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 3705bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 3706bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 3707c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 3708bf30d6edSAndy Whitcroft { 3709c45dcabdSAndy Whitcroft } 3710c45dcabdSAndy Whitcroft 3711e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 3712e45bab8eSAndy Whitcroft while ($dstat =~ s/("X*")\s*$Ident/$1/ || 3713e45bab8eSAndy Whitcroft $dstat =~ s/$Ident\s*("X*")/$1/) 3714e45bab8eSAndy Whitcroft { 3715e45bab8eSAndy Whitcroft } 3716e45bab8eSAndy Whitcroft 3717c45dcabdSAndy Whitcroft my $exceptions = qr{ 3718c45dcabdSAndy Whitcroft $Declare| 3719c45dcabdSAndy Whitcroft module_param_named| 3720a0a0a7a9SKees Cook MODULE_PARM_DESC| 3721c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 3722c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 3723383099fdSAndy Whitcroft __typeof__\(| 372422fd2d3eSStefani Seibold union| 372522fd2d3eSStefani Seibold struct| 3726ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 3727ea71a0a0SAndy Whitcroft ^\"|\"$ 3728c45dcabdSAndy Whitcroft }x; 37295eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 3730f74bd194SAndy Whitcroft if ($dstat ne '' && 3731f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 3732f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 37333cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 3734b9df76acSAndy Whitcroft $dstat !~ /^'X'$/ && # character constants 3735f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 3736f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 3737e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 373872f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 3739f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 3740f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 3741f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 3742f95a7e6aSJoe Perches $dstat !~ /^\({/ && # ({... 3743f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 3744c45dcabdSAndy Whitcroft { 3745f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 3746f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 3747f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 3748f74bd194SAndy Whitcroft 3749f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 3750f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 3751c45dcabdSAndy Whitcroft } 3752c45dcabdSAndy Whitcroft 3753f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 3754f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 3755f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 3756f74bd194SAndy Whitcroft } else { 3757000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 3758f74bd194SAndy Whitcroft "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 3759d8aaf121SAndy Whitcroft } 37600a920b5bSAndy Whitcroft } 37615023d347SJoe Perches 3762481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 37635023d347SJoe Perches 37645023d347SJoe Perches } else { 37655023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 3766481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 3767481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 37685023d347SJoe Perches $line =~ /^\+.*\\$/) { 37695023d347SJoe Perches WARN("LINE_CONTINUATIONS", 37705023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 37715023d347SJoe Perches } 3772653d4876SAndy Whitcroft } 37730a920b5bSAndy Whitcroft 3774b13edf7fSJoe Perches# do {} while (0) macro tests: 3775b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 3776b13edf7fSJoe Perches# macro should not end with a semicolon 3777b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 3778b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 3779b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 3780b13edf7fSJoe Perches my $ln = $linenr; 3781b13edf7fSJoe Perches my $cnt = $realcnt; 3782b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 3783b13edf7fSJoe Perches my $ctx = ''; 3784b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 3785b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 3786b13edf7fSJoe Perches $ctx = $dstat; 3787b13edf7fSJoe Perches 3788b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 3789b13edf7fSJoe Perches 3790b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 3791b13edf7fSJoe Perches my $stmts = $2; 3792b13edf7fSJoe Perches my $semis = $3; 3793b13edf7fSJoe Perches 3794b13edf7fSJoe Perches $ctx =~ s/\n*$//; 3795b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 3796b13edf7fSJoe Perches my $herectx = $here . "\n"; 3797b13edf7fSJoe Perches 3798b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 3799b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 3800b13edf7fSJoe Perches } 3801b13edf7fSJoe Perches 3802ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 3803ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 3804b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 3805b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 3806b13edf7fSJoe Perches } 3807b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 3808b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 3809b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 3810b13edf7fSJoe Perches } 3811b13edf7fSJoe Perches } 3812b13edf7fSJoe Perches } 3813b13edf7fSJoe Perches 3814080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 3815080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 3816080ba929SMike Frysinger# . 3817080ba929SMike Frysinger# ALIGN(...) 3818080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 3819080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 3820000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 3821000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 3822080ba929SMike Frysinger } 3823080ba929SMike Frysinger 3824f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 382513214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 382613214adfSAndy Whitcroft my ($level, $endln, @chunks) = 3827cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 382813214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 3829cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 3830cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 3831aad4f614SJoe Perches my @allowed = (); 3832aad4f614SJoe Perches my $allow = 0; 383313214adfSAndy Whitcroft my $seen = 0; 3834773647a0SAndy Whitcroft my $herectx = $here . "\n"; 3835cf655043SAndy Whitcroft my $ln = $linenr - 1; 383613214adfSAndy Whitcroft for my $chunk (@chunks) { 383713214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 383813214adfSAndy Whitcroft 3839773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 3840773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 3841773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 3842773647a0SAndy Whitcroft 3843aad4f614SJoe Perches $allowed[$allow] = 0; 3844773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 3845773647a0SAndy Whitcroft 3846773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 3847773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 3848773647a0SAndy Whitcroft 3849773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 3850cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 3851cf655043SAndy Whitcroft 3852773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 385313214adfSAndy Whitcroft 385413214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 385513214adfSAndy Whitcroft 3856aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 3857cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3858cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3859aad4f614SJoe Perches $allowed[$allow] = 1; 386013214adfSAndy Whitcroft } 386113214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3862cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3863aad4f614SJoe Perches $allowed[$allow] = 1; 386413214adfSAndy Whitcroft } 3865cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3866cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3867aad4f614SJoe Perches $allowed[$allow] = 1; 386813214adfSAndy Whitcroft } 3869aad4f614SJoe Perches $allow++; 387013214adfSAndy Whitcroft } 3871aad4f614SJoe Perches if ($seen) { 3872aad4f614SJoe Perches my $sum_allowed = 0; 3873aad4f614SJoe Perches foreach (@allowed) { 3874aad4f614SJoe Perches $sum_allowed += $_; 3875aad4f614SJoe Perches } 3876aad4f614SJoe Perches if ($sum_allowed == 0) { 3877000d1cc1SJoe Perches WARN("BRACES", 3878000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 3879aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 3880aad4f614SJoe Perches $seen != $allow) { 3881aad4f614SJoe Perches CHK("BRACES", 3882aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 3883aad4f614SJoe Perches } 388413214adfSAndy Whitcroft } 388513214adfSAndy Whitcroft } 388613214adfSAndy Whitcroft } 3887773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 388813214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 3889cf655043SAndy Whitcroft my $allowed = 0; 3890f0a594c1SAndy Whitcroft 3891cf655043SAndy Whitcroft # Check the pre-context. 3892cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 3893cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 3894cf655043SAndy Whitcroft $allowed = 1; 3895f0a594c1SAndy Whitcroft } 3896773647a0SAndy Whitcroft 3897773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 3898773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 3899773647a0SAndy Whitcroft 3900cf655043SAndy Whitcroft # Check the condition. 3901cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 3902773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 3903cf655043SAndy Whitcroft if (defined $cond) { 3904773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3905cf655043SAndy Whitcroft } 3906cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 3907cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 3908cf655043SAndy Whitcroft $allowed = 1; 3909cf655043SAndy Whitcroft } 3910cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 3911cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 3912cf655043SAndy Whitcroft $allowed = 1; 3913cf655043SAndy Whitcroft } 3914cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 3915cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 3916cf655043SAndy Whitcroft $allowed = 1; 3917cf655043SAndy Whitcroft } 3918cf655043SAndy Whitcroft # Check the post-context. 3919cf655043SAndy Whitcroft if (defined $chunks[1]) { 3920cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 3921cf655043SAndy Whitcroft if (defined $cond) { 3922773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 3923cf655043SAndy Whitcroft } 3924cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 3925cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 3926cf655043SAndy Whitcroft $allowed = 1; 3927cf655043SAndy Whitcroft } 3928cf655043SAndy Whitcroft } 3929cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 393069932487SJustin P. Mattock my $herectx = $here . "\n"; 3931f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 3932cf655043SAndy Whitcroft 3933f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 393469932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 3935cf655043SAndy Whitcroft } 3936cf655043SAndy Whitcroft 3937000d1cc1SJoe Perches WARN("BRACES", 3938000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 3939f0a594c1SAndy Whitcroft } 3940f0a594c1SAndy Whitcroft } 3941f0a594c1SAndy Whitcroft 39420979ae66SJoe Perches# check for unnecessary blank lines around braces 394377b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 39440979ae66SJoe Perches CHK("BRACES", 39450979ae66SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 39460979ae66SJoe Perches } 394777b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 39480979ae66SJoe Perches CHK("BRACES", 39490979ae66SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 39500979ae66SJoe Perches } 39510979ae66SJoe Perches 39524a0df2efSAndy Whitcroft# no volatiles please 39536c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 39546c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 3955000d1cc1SJoe Perches WARN("VOLATILE", 3956000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 39574a0df2efSAndy Whitcroft } 39584a0df2efSAndy Whitcroft 395900df344fSAndy Whitcroft# warn about #if 0 3960c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 3961000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 3962000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 3963de7d4f0eSAndy Whitcroft $herecurr); 39644a0df2efSAndy Whitcroft } 39654a0df2efSAndy Whitcroft 396603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 396703df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 396803df4b51SAndy Whitcroft my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 396903df4b51SAndy Whitcroft if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 397003df4b51SAndy Whitcroft WARN('NEEDLESS_IF', 397103df4b51SAndy Whitcroft "$1(NULL) is safe this check is probably not required\n" . $hereprev); 39724c432a8fSGreg Kroah-Hartman } 39734c432a8fSGreg Kroah-Hartman } 3974f0a594c1SAndy Whitcroft 39758716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 39768716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 39778716de38SJoe Perches my $attr = $1; 39788716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 39798716de38SJoe Perches my $ptr = $1; 39808716de38SJoe Perches my $var = $2; 39818716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 39828716de38SJoe Perches ERROR("MISPLACED_INIT", 39838716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 39848716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 39858716de38SJoe Perches WARN("MISPLACED_INIT", 39868716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 39878716de38SJoe Perches $fix) { 39888716de38SJoe 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; 39898716de38SJoe Perches } 39908716de38SJoe Perches } 39918716de38SJoe Perches } 39928716de38SJoe Perches 3993e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 3994e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 3995e970b884SJoe Perches my $attr = $1; 3996e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 3997e970b884SJoe Perches my $attr_prefix = $1; 3998e970b884SJoe Perches my $attr_type = $2; 3999e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4000e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 4001e970b884SJoe Perches $fix) { 4002e970b884SJoe Perches $fixed[$linenr - 1] =~ 4003e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 4004e970b884SJoe Perches } 4005e970b884SJoe Perches } 4006e970b884SJoe Perches 4007e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 4008e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 4009e970b884SJoe Perches my $attr = $1; 4010e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4011e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 4012e970b884SJoe Perches $fix) { 4013e970b884SJoe Perches my $lead = $fixed[$linenr - 1] =~ 4014e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 4015e970b884SJoe Perches $lead = rtrim($1); 4016e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 4017e970b884SJoe Perches $lead = "${lead}const "; 4018e970b884SJoe Perches $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/; 4019e970b884SJoe Perches } 4020e970b884SJoe Perches } 4021e970b884SJoe Perches 4022fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 4023fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 4024fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 4025fbdb8138SJoe Perches my $constant_func = $1; 4026fbdb8138SJoe Perches my $func = $constant_func; 4027fbdb8138SJoe Perches $func =~ s/^__constant_//; 4028fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 4029fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 4030fbdb8138SJoe Perches $fix) { 4031fbdb8138SJoe Perches $fixed[$linenr - 1] =~ s/\b$constant_func\b/$func/g; 4032fbdb8138SJoe Perches } 4033fbdb8138SJoe Perches } 4034fbdb8138SJoe Perches 40351a15a250SPatrick Pannuto# prefer usleep_range over udelay 403637581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 403743c1d77cSJoe Perches my $delay = $1; 40381a15a250SPatrick Pannuto # ignore udelay's < 10, however 403943c1d77cSJoe Perches if (! ($delay < 10) ) { 4040000d1cc1SJoe Perches CHK("USLEEP_RANGE", 404143c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 404243c1d77cSJoe Perches } 404343c1d77cSJoe Perches if ($delay > 2000) { 404443c1d77cSJoe Perches WARN("LONG_UDELAY", 404543c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 40461a15a250SPatrick Pannuto } 40471a15a250SPatrick Pannuto } 40481a15a250SPatrick Pannuto 404909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 405009ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 405109ef8725SPatrick Pannuto if ($1 < 20) { 4052000d1cc1SJoe Perches WARN("MSLEEP", 405343c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 405409ef8725SPatrick Pannuto } 405509ef8725SPatrick Pannuto } 405609ef8725SPatrick Pannuto 405736ec1939SJoe Perches# check for comparisons of jiffies 405836ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 405936ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 406036ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 406136ec1939SJoe Perches } 406236ec1939SJoe Perches 40639d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 40649d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 40659d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 40669d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 40679d7a34a5SJoe Perches } 40689d7a34a5SJoe Perches 406900df344fSAndy Whitcroft# warn about #ifdefs in C files 4070c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 407100df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 407200df344fSAndy Whitcroft# print "$herecurr"; 407300df344fSAndy Whitcroft# $clean = 0; 407400df344fSAndy Whitcroft# } 407500df344fSAndy Whitcroft 407622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 4077c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 40783705ce5bSJoe Perches if (ERROR("SPACING", 40793705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 40803705ce5bSJoe Perches $fix) { 40813705ce5bSJoe Perches $fixed[$linenr - 1] =~ 40823705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 40833705ce5bSJoe Perches } 40843705ce5bSJoe Perches 408522f2a2efSAndy Whitcroft } 408622f2a2efSAndy Whitcroft 40874a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 4088171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 4089171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 40904a0df2efSAndy Whitcroft my $which = $1; 40914a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4092000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 4093000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 40944a0df2efSAndy Whitcroft } 40954a0df2efSAndy Whitcroft } 40964a0df2efSAndy Whitcroft# check for memory barriers without a comment. 40974a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 40984a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4099c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 4100000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 41014a0df2efSAndy Whitcroft } 41024a0df2efSAndy Whitcroft } 41034a0df2efSAndy Whitcroft# check of hardware specific defines 4104c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 4105000d1cc1SJoe Perches CHK("ARCH_DEFINES", 4106000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 41070a920b5bSAndy Whitcroft } 4108653d4876SAndy Whitcroft 4109d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 4110d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 4111000d1cc1SJoe Perches WARN("STORAGE_CLASS", 4112000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 4113d4977c78STobias Klauser } 4114d4977c78STobias Klauser 4115de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 4116de7d4f0eSAndy Whitcroft# storage class and type. 41179c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 41189c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 4119000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 4120000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 4121de7d4f0eSAndy Whitcroft } 4122de7d4f0eSAndy Whitcroft 41238905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 41242b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 41252b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 4126d5e616fcSJoe Perches if (WARN("INLINE", 4127d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 4128d5e616fcSJoe Perches $fix) { 4129d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/; 4130d5e616fcSJoe Perches 4131d5e616fcSJoe Perches } 41328905a67cSAndy Whitcroft } 41338905a67cSAndy Whitcroft 41343d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 41352b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 41362b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 4137000d1cc1SJoe Perches WARN("PREFER_PACKED", 4138000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 41393d130fd0SJoe Perches } 41403d130fd0SJoe Perches 414139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 41422b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 41432b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 4144000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 4145000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 414639b7e287SJoe Perches } 414739b7e287SJoe Perches 41485f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 41492b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 41502b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 4151d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 4152d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 4153d5e616fcSJoe Perches $fix) { 4154d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 4155d5e616fcSJoe Perches 4156d5e616fcSJoe Perches } 41575f14d3bdSJoe Perches } 41585f14d3bdSJoe Perches 41596061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 41602b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 41612b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 4162d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 4163d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 4164d5e616fcSJoe Perches $fix) { 4165d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 4166d5e616fcSJoe Perches } 41676061d949SJoe Perches } 41686061d949SJoe Perches 41698f53a9b8SJoe Perches# check for sizeof(&) 41708f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 4171000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 4172000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 41738f53a9b8SJoe Perches } 41748f53a9b8SJoe Perches 417566c80b60SJoe Perches# check for sizeof without parenthesis 417666c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 4177d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 4178d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 4179d5e616fcSJoe Perches $fix) { 4180d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 4181d5e616fcSJoe Perches } 418266c80b60SJoe Perches } 418366c80b60SJoe Perches 4184428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 4185428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 4186000d1cc1SJoe Perches WARN("LINE_CONTINUATIONS", 4187000d1cc1SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 4188428e2fdcSJoe Perches } 4189428e2fdcSJoe Perches 419088982feaSJoe Perches# check for struct spinlock declarations 419188982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 419288982feaSJoe Perches WARN("USE_SPINLOCK_T", 419388982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 419488982feaSJoe Perches } 419588982feaSJoe Perches 4196a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 419706668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 4198a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 419906668727SJoe Perches if ($fmt ne "" && $fmt !~ /[^\\]\%/) { 4200d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 4201d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 4202d5e616fcSJoe Perches $fix) { 4203d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/; 4204d5e616fcSJoe Perches } 4205a6962d72SJoe Perches } 4206a6962d72SJoe Perches } 4207a6962d72SJoe Perches 4208554e165cSAndy Whitcroft# Check for misused memsets 4209d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 4210d1fe9c09SJoe Perches defined $stat && 4211d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 4212554e165cSAndy Whitcroft 4213d7c76ba7SJoe Perches my $ms_addr = $2; 4214d1fe9c09SJoe Perches my $ms_val = $7; 4215d1fe9c09SJoe Perches my $ms_size = $12; 4216d7c76ba7SJoe Perches 4217554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 4218554e165cSAndy Whitcroft ERROR("MEMSET", 4219d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 4220554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 4221554e165cSAndy Whitcroft WARN("MEMSET", 4222d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 4223d7c76ba7SJoe Perches } 4224d7c76ba7SJoe Perches } 4225d7c76ba7SJoe Perches 422698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 422798a9bba5SJoe Perches if ($^V && $^V ge 5.10.0 && 422898a9bba5SJoe Perches $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { 422998a9bba5SJoe Perches if (WARN("PREFER_ETHER_ADDR_COPY", 423098a9bba5SJoe Perches "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && 423198a9bba5SJoe Perches $fix) { 423298a9bba5SJoe Perches $fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 423398a9bba5SJoe Perches } 423498a9bba5SJoe Perches } 423598a9bba5SJoe Perches 4236d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 4237d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 4238d1fe9c09SJoe Perches defined $stat && 4239d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 4240d1fe9c09SJoe Perches if (defined $2 || defined $7) { 4241d7c76ba7SJoe Perches my $call = $1; 4242d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 4243d7c76ba7SJoe Perches my $arg1 = $3; 4244d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 4245d1fe9c09SJoe Perches my $arg2 = $8; 4246d7c76ba7SJoe Perches my $cast; 4247d7c76ba7SJoe Perches 4248d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 4249d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 4250d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 4251d7c76ba7SJoe Perches $cast = $cast1; 4252d7c76ba7SJoe Perches } else { 4253d7c76ba7SJoe Perches $cast = $cast2; 4254d7c76ba7SJoe Perches } 4255d7c76ba7SJoe Perches WARN("MINMAX", 4256d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 4257554e165cSAndy Whitcroft } 4258554e165cSAndy Whitcroft } 4259554e165cSAndy Whitcroft 42604a273195SJoe Perches# check usleep_range arguments 42614a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 42624a273195SJoe Perches defined $stat && 42634a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 42644a273195SJoe Perches my $min = $1; 42654a273195SJoe Perches my $max = $7; 42664a273195SJoe Perches if ($min eq $max) { 42674a273195SJoe Perches WARN("USLEEP_RANGE", 42684a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 42694a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 42704a273195SJoe Perches $min > $max) { 42714a273195SJoe Perches WARN("USLEEP_RANGE", 42724a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 42734a273195SJoe Perches } 42744a273195SJoe Perches } 42754a273195SJoe Perches 4276823b794cSJoe Perches# check for naked sscanf 4277823b794cSJoe Perches if ($^V && $^V ge 5.10.0 && 4278823b794cSJoe Perches defined $stat && 42796c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 4280823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 4281823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 4282823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 4283823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 4284823b794cSJoe Perches $lc = $lc + $linenr; 4285823b794cSJoe Perches my $stat_real = raw_line($linenr, 0); 4286823b794cSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 4287823b794cSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 4288823b794cSJoe Perches } 4289823b794cSJoe Perches WARN("NAKED_SSCANF", 4290823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 4291823b794cSJoe Perches } 4292823b794cSJoe Perches 429370dc8a48SJoe Perches# check for new externs in .h files. 429470dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 429570dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 4296d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 429770dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 429870dc8a48SJoe Perches $fix) { 429970dc8a48SJoe Perches $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 430070dc8a48SJoe Perches } 430170dc8a48SJoe Perches } 430270dc8a48SJoe Perches 4303de7d4f0eSAndy Whitcroft# check for new externs in .c files. 4304171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 4305c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 4306171ae1a4SAndy Whitcroft { 4307c45dcabdSAndy Whitcroft my $function_name = $1; 4308c45dcabdSAndy Whitcroft my $paren_space = $2; 4309171ae1a4SAndy Whitcroft 4310171ae1a4SAndy Whitcroft my $s = $stat; 4311171ae1a4SAndy Whitcroft if (defined $cond) { 4312171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 4313171ae1a4SAndy Whitcroft } 4314c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 4315c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 4316c45dcabdSAndy Whitcroft { 4317000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4318000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4319de7d4f0eSAndy Whitcroft } 4320de7d4f0eSAndy Whitcroft 4321171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 4322000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 4323000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 4324171ae1a4SAndy Whitcroft } 43259c9ba34eSAndy Whitcroft 43269c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 43279c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 43289c9ba34eSAndy Whitcroft { 4329000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4330000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4331171ae1a4SAndy Whitcroft } 4332171ae1a4SAndy Whitcroft 4333de7d4f0eSAndy Whitcroft# checks for new __setup's 4334de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 4335de7d4f0eSAndy Whitcroft my $name = $1; 4336de7d4f0eSAndy Whitcroft 4337de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 4338000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 4339000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 4340de7d4f0eSAndy Whitcroft } 4341653d4876SAndy Whitcroft } 43429c0ca6f9SAndy Whitcroft 43439c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 4344caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 4345000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 4346000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 43479c0ca6f9SAndy Whitcroft } 434813214adfSAndy Whitcroft 4349a640d25cSJoe Perches# alloc style 4350a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 4351a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 4352a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 4353a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 4354a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 4355a640d25cSJoe Perches } 4356a640d25cSJoe Perches 4357972fdea2SJoe Perches# check for krealloc arg reuse 4358972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 4359972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 4360972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 4361972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 4362972fdea2SJoe Perches } 4363972fdea2SJoe Perches 43645ce59ae0SJoe Perches# check for alloc argument mismatch 43655ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 43665ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 43675ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 43685ce59ae0SJoe Perches } 43695ce59ae0SJoe Perches 4370caf2a54fSJoe Perches# check for multiple semicolons 4371caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 4372d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 4373d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 4374d5e616fcSJoe Perches $fix) { 4375d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g; 4376d5e616fcSJoe Perches } 4377d1e2ad07SJoe Perches } 4378d1e2ad07SJoe Perches 4379c34c09a8SJoe Perches# check for case / default statements not preceeded by break/fallthrough/switch 4380c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 4381c34c09a8SJoe Perches my $has_break = 0; 4382c34c09a8SJoe Perches my $has_statement = 0; 4383c34c09a8SJoe Perches my $count = 0; 4384c34c09a8SJoe Perches my $prevline = $linenr; 4385c34c09a8SJoe Perches while ($prevline > 1 && $count < 3 && !$has_break) { 4386c34c09a8SJoe Perches $prevline--; 4387c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 4388c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 4389c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 4390c34c09a8SJoe Perches next if ($fline =~ /^\-/); 4391c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 4392c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 4393c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 4394c34c09a8SJoe Perches $has_statement = 1; 4395c34c09a8SJoe Perches $count++; 4396c34c09a8SJoe Perches $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); 4397c34c09a8SJoe Perches } 4398c34c09a8SJoe Perches if (!$has_break && $has_statement) { 4399c34c09a8SJoe Perches WARN("MISSING_BREAK", 4400c34c09a8SJoe Perches "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); 4401c34c09a8SJoe Perches } 4402c34c09a8SJoe Perches } 4403c34c09a8SJoe Perches 4404d1e2ad07SJoe Perches# check for switch/default statements without a break; 4405d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 4406d1e2ad07SJoe Perches defined $stat && 4407d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 4408d1e2ad07SJoe Perches my $ctx = ''; 4409d1e2ad07SJoe Perches my $herectx = $here . "\n"; 4410d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 4411d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4412d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4413d1e2ad07SJoe Perches } 4414d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 4415d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 4416caf2a54fSJoe Perches } 4417caf2a54fSJoe Perches 441813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 4419d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 4420d5e616fcSJoe Perches if (WARN("USE_FUNC", 4421d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 4422d5e616fcSJoe Perches $fix) { 4423d5e616fcSJoe Perches $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g; 4424d5e616fcSJoe Perches } 442513214adfSAndy Whitcroft } 4426773647a0SAndy Whitcroft 44272c92488aSJoe Perches# check for use of yield() 44282c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 44292c92488aSJoe Perches WARN("YIELD", 44302c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 44312c92488aSJoe Perches } 44322c92488aSJoe Perches 4433179f8f40SJoe Perches# check for comparisons against true and false 4434179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 4435179f8f40SJoe Perches my $lead = $1; 4436179f8f40SJoe Perches my $arg = $2; 4437179f8f40SJoe Perches my $test = $3; 4438179f8f40SJoe Perches my $otype = $4; 4439179f8f40SJoe Perches my $trail = $5; 4440179f8f40SJoe Perches my $op = "!"; 4441179f8f40SJoe Perches 4442179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 4443179f8f40SJoe Perches 4444179f8f40SJoe Perches my $type = lc($otype); 4445179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 4446179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 4447179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 4448179f8f40SJoe Perches $op = ""; 4449179f8f40SJoe Perches } 4450179f8f40SJoe Perches 4451179f8f40SJoe Perches CHK("BOOL_COMPARISON", 4452179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 4453179f8f40SJoe Perches 4454179f8f40SJoe Perches## maybe suggesting a correct construct would better 4455179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 4456179f8f40SJoe Perches 4457179f8f40SJoe Perches } 4458179f8f40SJoe Perches } 4459179f8f40SJoe Perches 44604882720bSThomas Gleixner# check for semaphores initialized locked 44614882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 4462000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 4463000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 4464773647a0SAndy Whitcroft } 44656712d858SJoe Perches 446667d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 446767d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 4468000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 446967d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 4470773647a0SAndy Whitcroft } 44716712d858SJoe Perches 4472f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 4473f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 4474000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 4475000d1cc1SJoe Perches "please use device_initcall() instead of __initcall()\n" . $herecurr); 4476f3db6639SMichael Ellerman } 44776712d858SJoe Perches 447879404849SEmese Revfy# check for various ops structs, ensure they are const. 447979404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 448079404849SEmese Revfy address_space_operations| 448179404849SEmese Revfy backlight_ops| 448279404849SEmese Revfy block_device_operations| 448379404849SEmese Revfy dentry_operations| 448479404849SEmese Revfy dev_pm_ops| 448579404849SEmese Revfy dma_map_ops| 448679404849SEmese Revfy extent_io_ops| 448779404849SEmese Revfy file_lock_operations| 448879404849SEmese Revfy file_operations| 448979404849SEmese Revfy hv_ops| 449079404849SEmese Revfy ide_dma_ops| 449179404849SEmese Revfy intel_dvo_dev_ops| 449279404849SEmese Revfy item_operations| 449379404849SEmese Revfy iwl_ops| 449479404849SEmese Revfy kgdb_arch| 449579404849SEmese Revfy kgdb_io| 449679404849SEmese Revfy kset_uevent_ops| 449779404849SEmese Revfy lock_manager_operations| 449879404849SEmese Revfy microcode_ops| 449979404849SEmese Revfy mtrr_ops| 450079404849SEmese Revfy neigh_ops| 450179404849SEmese Revfy nlmsvc_binding| 450279404849SEmese Revfy pci_raw_ops| 450379404849SEmese Revfy pipe_buf_operations| 450479404849SEmese Revfy platform_hibernation_ops| 450579404849SEmese Revfy platform_suspend_ops| 450679404849SEmese Revfy proto_ops| 450779404849SEmese Revfy rpc_pipe_ops| 450879404849SEmese Revfy seq_operations| 450979404849SEmese Revfy snd_ac97_build_ops| 451079404849SEmese Revfy soc_pcmcia_socket_ops| 451179404849SEmese Revfy stacktrace_ops| 451279404849SEmese Revfy sysfs_ops| 451379404849SEmese Revfy tty_operations| 451479404849SEmese Revfy usb_mon_operations| 451579404849SEmese Revfy wd_ops}x; 45166903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 451779404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 4518000d1cc1SJoe Perches WARN("CONST_STRUCT", 4519000d1cc1SJoe Perches "struct $1 should normally be const\n" . 45206903ffb2SAndy Whitcroft $herecurr); 45212b6db5cbSAndy Whitcroft } 4522773647a0SAndy Whitcroft 4523773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 4524773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 4525773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 4526c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 4527c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 4528171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 4529171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 4530171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 4531773647a0SAndy Whitcroft { 4532000d1cc1SJoe Perches WARN("NR_CPUS", 4533000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 4534773647a0SAndy Whitcroft } 45359c9ba34eSAndy Whitcroft 453652ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 453752ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 453852ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 453952ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 454052ea8506SJoe Perches } 454152ea8506SJoe Perches 45429c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 45439c9ba34eSAndy Whitcroft my $string; 45449c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 45459c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 45462a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 45479c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 4548000d1cc1SJoe Perches WARN("PRINTF_L", 4549000d1cc1SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 45509c9ba34eSAndy Whitcroft last; 45519c9ba34eSAndy Whitcroft } 45529c9ba34eSAndy Whitcroft } 4553691d77b6SAndy Whitcroft 4554691d77b6SAndy Whitcroft# whine mightly about in_atomic 4555691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 4556691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 4557000d1cc1SJoe Perches ERROR("IN_ATOMIC", 4558000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 4559f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 4560000d1cc1SJoe Perches WARN("IN_ATOMIC", 4561000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 4562691d77b6SAndy Whitcroft } 4563691d77b6SAndy Whitcroft } 45641704f47bSPeter Zijlstra 45651704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 45661704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 45671704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 45681704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 45691704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 45701704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 4571000d1cc1SJoe Perches ERROR("LOCKDEP", 4572000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 45731704f47bSPeter Zijlstra } 45741704f47bSPeter Zijlstra } 457588f8831cSDave Jones 457688f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 457788f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 4578000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 4579000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 458088f8831cSDave Jones } 45812435880fSJoe Perches 4582515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 4583515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 4584515a235eSJoe Perches if ($^V && $^V ge 5.10.0 && 4585515a235eSJoe Perches $line =~ /$mode_perms_search/) { 45862435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 45872435880fSJoe Perches my $func = $entry->[0]; 45882435880fSJoe Perches my $arg_pos = $entry->[1]; 45892435880fSJoe Perches 45902435880fSJoe Perches my $skip_args = ""; 45912435880fSJoe Perches if ($arg_pos > 1) { 45922435880fSJoe Perches $arg_pos--; 45932435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 45942435880fSJoe Perches } 45952435880fSJoe Perches my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; 4596515a235eSJoe Perches if ($line =~ /$test/) { 45972435880fSJoe Perches my $val = $1; 45982435880fSJoe Perches $val = $6 if ($skip_args ne ""); 45992435880fSJoe Perches 46001727cc70SJoe Perches if ($val !~ /^0$/ && 46011727cc70SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 46021727cc70SJoe Perches length($val) ne 4)) { 46032435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 46041727cc70SJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 46052435880fSJoe Perches } 46062435880fSJoe Perches } 46072435880fSJoe Perches } 460813214adfSAndy Whitcroft } 4609515a235eSJoe Perches } 461013214adfSAndy Whitcroft 461113214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 461213214adfSAndy Whitcroft # so just keep quiet. 461313214adfSAndy Whitcroft if ($#rawlines == -1) { 461413214adfSAndy Whitcroft exit(0); 46150a920b5bSAndy Whitcroft } 46160a920b5bSAndy Whitcroft 46178905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 46188905a67cSAndy Whitcroft # things that appear to be patches. 46198905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 46208905a67cSAndy Whitcroft exit(0); 46218905a67cSAndy Whitcroft } 46228905a67cSAndy Whitcroft 46238905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 46248905a67cSAndy Whitcroft # just keep quiet. 46258905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 46268905a67cSAndy Whitcroft exit(0); 46278905a67cSAndy Whitcroft } 46288905a67cSAndy Whitcroft 46298905a67cSAndy Whitcroft if (!$is_patch) { 4630000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 4631000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 46320a920b5bSAndy Whitcroft } 46330a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 4634000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 4635000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 46360a920b5bSAndy Whitcroft } 46370a920b5bSAndy Whitcroft 4638f0a594c1SAndy Whitcroft print report_dump(); 463913214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 464013214adfSAndy Whitcroft print "$filename " if ($summary_file); 46416c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 46426c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 46436c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 46448905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 46456c72ffaaSAndy Whitcroft } 46468905a67cSAndy Whitcroft 4647d2c0a235SAndy Whitcroft if ($quiet == 0) { 4648d1fe9c09SJoe Perches 4649d1fe9c09SJoe Perches if ($^V lt 5.10.0) { 4650d1fe9c09SJoe Perches print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); 4651d1fe9c09SJoe Perches print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); 4652d1fe9c09SJoe Perches } 4653d1fe9c09SJoe Perches 4654d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 4655d2c0a235SAndy Whitcroft # then suggest that. 4656d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 4657d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 4658d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 4659b0781216SMike Frysinger $rpt_cleaners = 0; 4660d2c0a235SAndy Whitcroft } 4661d2c0a235SAndy Whitcroft } 4662d2c0a235SAndy Whitcroft 466391bfe484SJoe Perches hash_show_words(\%use_type, "Used"); 466491bfe484SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 4665000d1cc1SJoe Perches 46663705ce5bSJoe Perches if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { 46679624b8d6SJoe Perches my $newfile = $filename; 46689624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 46693705ce5bSJoe Perches my $linecount = 0; 46703705ce5bSJoe Perches my $f; 46713705ce5bSJoe Perches 46723705ce5bSJoe Perches open($f, '>', $newfile) 46733705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 46743705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 46753705ce5bSJoe Perches $linecount++; 46763705ce5bSJoe Perches if ($file) { 46773705ce5bSJoe Perches if ($linecount > 3) { 46783705ce5bSJoe Perches $fixed_line =~ s/^\+//; 46793705ce5bSJoe Perches print $f $fixed_line. "\n"; 46803705ce5bSJoe Perches } 46813705ce5bSJoe Perches } else { 46823705ce5bSJoe Perches print $f $fixed_line . "\n"; 46833705ce5bSJoe Perches } 46843705ce5bSJoe Perches } 46853705ce5bSJoe Perches close($f); 46863705ce5bSJoe Perches 46873705ce5bSJoe Perches if (!$quiet) { 46883705ce5bSJoe Perches print << "EOM"; 46893705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 46903705ce5bSJoe Perches 46913705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 46923705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 46933705ce5bSJoe Perches 46943705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 46953705ce5bSJoe PerchesNo warranties, expressed or implied... 46963705ce5bSJoe Perches 46973705ce5bSJoe PerchesEOM 46983705ce5bSJoe Perches } 46993705ce5bSJoe Perches } 47003705ce5bSJoe Perches 47010a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 4702c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 47030a920b5bSAndy Whitcroft } 47040a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 4705000d1cc1SJoe Perches print << "EOM"; 4706000d1cc1SJoe Perches$vname has style problems, please review. 4707000d1cc1SJoe Perches 4708000d1cc1SJoe PerchesIf any of these errors are false positives, please report 4709000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 4710000d1cc1SJoe PerchesEOM 47110a920b5bSAndy Whitcroft } 471213214adfSAndy Whitcroft 47130a920b5bSAndy Whitcroft return $clean; 47140a920b5bSAndy Whitcroft} 4715